From e307c379ce6c012467f54b874e84cadfb21b3e55 Mon Sep 17 00:00:00 2001
From: Mike Birchall <mike.birchall@mq.edu.au>
Date: Wed, 6 Feb 2019 13:41:31 +1100
Subject: [PATCH] Added aaorun command SKYSUB_PCA 	Added aaorun command
 "SKYSUB_PCA"

---
 main.c         | 38 +++++++++++++++++++++++++++++++++
 skysub_pca.F95 | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+)

diff --git a/main.c b/main.c
index 0514f7f..925f9b0 100644
--- a/main.c
+++ b/main.c
@@ -32,6 +32,7 @@ extern void reduce_gen(DitsArgType*,StatusType*);
 extern void make_im___(DitsArgType*,StatusType*);
 extern void make_ex_(DitsArgType*,StatusType*);
 extern void make_red_(DitsArgType*,StatusType*);
+extern void skysub_pca_(DitsArgType*,StatusType*);
 extern void splice_aaomega_(DitsArgType*,StatusType*);
 extern void detect_cosmic_streaks_(DitsArgType*,StatusType*);
 extern void transfer_function_correction_(DitsArgType*,StatusType*);
@@ -57,6 +58,7 @@ static void reduceGenAction(StatusType*);
 static void makeIMAction(StatusType*);
 static void makeEXAction(StatusType*);
 static void makeREDAction(StatusType*);
+static void skysubPCAAction(StatusType*);
 static void spliceAction(StatusType*);
 static void transfuncAction(StatusType*);
 static void detectCosmicStreaksAction(StatusType*);
@@ -106,6 +108,7 @@ int main(int argc,char** argv)
 	{ makeIMAction,        plotKick,0,0,0,0,"MAKE_IM"           },
 	{ makeEXAction,        plotKick,0,0,0,0,"MAKE_EX"           },
 	{ makeREDAction,       plotKick,0,0,0,0,"MAKE_RED"          },
+	{ skysubPCAAction,     plotKick,0,0,0,0,"SKYSUB_PCA"        },
 	{ spliceAction,        plotKick,0,0,0,0,"SPLICE"            },
 	{ transfuncAction,     plotKick,0,0,0,0,"TRANSFUNC"         },
 	{ detectCosmicStreaksAction,plotKick,0,0,0,0,"DETECT_CSTRKS"},
@@ -792,6 +795,41 @@ void makeREDAction(
 
     DitsPutRequest(DITS_REQ_END,pStatus);
 }
+/*---------------------------------------------------------------------------*/
+
+void skysubPCAAction(
+    StatusType* pStatus)
+/*
+ * Handler for "make_IM" DRAMA action.
+ */
+{
+    DitsArgType args;
+    SdsIdType localArgs = 0;
+    StatusType ignore = STATUS__OK;
+
+    if (*pStatus != DITS__OK) return;
+
+    /* get our SDS argument structure */
+    args = DitsGetArgument();
+
+    /* for debug print our argument structure on stdout*/
+    SdsList(args,pStatus); 
+
+    /* make copy so it can be changed (external SDS -> internal) */
+    SdsCopy(args,&localArgs,pStatus);
+
+    /* call Fortran make_red() */
+    skysub_pca_(&localArgs,pStatus);
+    if (*pStatus!=DITS__OK) {
+        ErsRep(0,pStatus,"%s","PCA Sky Subtraction error");
+    }
+
+    /* delete local SDS copy and free its id*/
+    SdsDelete(localArgs,&ignore);
+    SdsFreeId(localArgs,&ignore);
+
+    DitsPutRequest(DITS_REQ_END,pStatus);
+}
 
 /*---------------------------------------------------------------------------*/
 
diff --git a/skysub_pca.F95 b/skysub_pca.F95
index 5026418..4cab5dc 100644
--- a/skysub_pca.F95
+++ b/skysub_pca.F95
@@ -1,6 +1,63 @@
 ! Copyright (c) Anglo-Australian Telescope Board, 1999.
 ! Not to be used for commercial purposes without AATB permission.
 
+SUBROUTINE SKYSUB_PCA(ARGS,STATUS)
+
+
+!  --------
+!  Purpose:
+!  --------
+!     Command called from main.c as an aaorun execution
+!
+!	aaorun skysub_pca [filename] [arg1] [arg2] etc.
+!
+!    Where filename is stored in the ARGS SDS stucture as "OBJECT_FILENAME" 
+
+!  ------------
+!  Description:
+!  ------------
+!     Determines from the supplied arguments what is the object 
+!     file in question.  Open specified object file for udate
+!     then  call SKYSUBPCA to process it and finally close.
+
+!   
+
+   IMPLICIT NONE
+   INCLUDE 'TDFRED_PAR'
+   INCLUDE 'tdfio.inc'
+   
+   ! Arguments:
+   INTEGER,INTENT(IN)   ::ARGS    ! SDS identifier of structure containing
+                                  ! method arguments, see MAKE_EX_FROM_IM
+   INTEGER,INTENT(INOUT)::STATUS  ! Global status
+   
+   ! Local variables:
+   CHARACTER(LEN=MAXNAMELEN_FILE) :: OBJECT_FILENAME
+   INTEGER :: OBJECT
+   
+   
+   print *, ''
+   print *, '        =================='
+   print *, '        SKY SUB PCA CALLED'
+   print *, '        =================='
+   print *, ''
+      
+   CALL ARG_GET0C(ARGS,'OBJECT_FILENAME',OBJECT_FILENAME,STATUS)
+   PRINT *, 'OBJ FILE='//TRIM(OBJECT_FILENAME)
+   
+   ! Open the object frame for update
+   CALL TDFIO_OPEN(OBJECT,OBJECT_FILENAME,'UPDATE',TDFIO_STD_TYPE,STATUS)
+
+   ! Call PCA sky subtraction
+   CALL SKYSUBPCA(OBJECT,ARGS,STATUS)
+   
+   ! Close object frame
+   CALL TDFIO_CLOSE(OBJECT,STATUS)
+   
+END SUBROUTINE SKYSUB_PCA
+
+! ----------------------------------------------------------------------
+! ----------------------------------------------------------------------
 
 SUBROUTINE SKYSUBPCA(OBJECT,ARGS,STATUS)
 !+
-- 
GitLab