/*-------------------------------------------------------------------------------------*\
|  PROGRAM NAME:                                                                        |
|     scdm_qasignaturerequest.sas                                                       |
|                                                                                       |
|---------------------------------------------------------------------------------------|
|  PURPOSE:                                                                             |
|     The purpose of the program is to create request-level metadata "signature" files  |
|                                                                                       |
|---------------------------------------------------------------------------------------|
|  PROGRAM INPUT:                                                                       |
|     see 00.0_scdm_data_qa_review_master_file.sas                                      |
|                                                                                       |
|  PROGRAM OUTPUT:                                                                      |
|     see Workplan PDF                                                                  |
|---------------------------------------------------------------------------------------|
|  CONTACT:                                                                             |
|     Sentinel Coordinating Center                                                      |
|     info@sentinelsystem.org                                                           |
\*-------------------------------------------------------------------------------------*/

*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;
*  PLEASE DO NOT EDIT BELOW WITHOUT CONTACTING THE SENTINEL OPERATIONS CENTER           ;
*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;

%MACRO SOC_QASIGNATUREREQUEST;

/*--Get the names of all the SIGNATURE files in the MSOC folder--*/
  data _sigfiles;
   keep fname;
   length filref $8 fname $80; 
   rc = filename(filref, "&MSOC"); 
   if rc = 0 then do; 
      dir_id = dopen(filref); 
      rc = filename(filref); 
   end; 
   else do; 
      length msg $200.; 
      msg = sysmsg(); 
      put msg=; 
      dir_id = .; 
   end;
   if dir_id <= 0 then do;
      putlog '================================================================';
      putlog ' ERR'"OR: Unable to open directory.";
      abort cancel;
      putlog '================================================================';
   end;
   n_members = dnum(dir_id);
   do i = 1 to n_members; 
      fname = dread(dir_id, i); 
      fid = mopen(dir_id, fname); 
      if fid > 0 then output; 
   end;
   rc = dclose(dir_id);
run;

/*--Put all the signature file names in a string and create macro variables with 
    the string and count of names--*/
data _null_; set _sigfiles end=eof;
   length strng $ 1000 filname $ 36;;
   retain strng;
   retain nfiles 0;

   /*-Look for all files with the phase 'signature' in them, but only with mXXrX or mXXrXX run/scenario numbers
      in the name.  If a req signature file had already been created, it will have an mXX in the name and,
      therefore, [correctly] not included in the list of signature files being read in-*/

   /* has phrase '_signature' and not the output file prefix 'allrun' from a prior run */
   if index(lowcase(fname),'_signature') > 0 and
      index(lowcase(fname),'alltable') = 0 then do; 
         filname = substr(fname,1,index(fname,'.')-1);
         strng = strip(strng) || ' ' || strip(filname);
         nfiles = nfiles + 1;
   end;
   if eof then do;
      call symput("FLIST",strng);
      call symput("NFILS",put(nfiles,3.));
   end;
run;

%PUT FILE COUNT: &NFILS;
%PUT FILE LIST: &FLIST;

/*--Loop through all signature files gathering information for request-level sig file--*/
%DO F = 1 %TO &NFILS;

data _tmp_signature; 
   length DP $ 6 ReqID $ 36 ProjID $16 WPType WPID DPID VerID QAVer SCDMVer 
          OSAbbr OSName SASVersion $ 12 SASVersionLong $ 30 RunType NCPU $ 12;
   set msoc.%SCAN(&FLIST,&F) end=eof;
   retain DP ReqID ProjID WPType WPID DPID VerID QAVer SCDMVer OSAbbr 
          OSName SASVersion SASVersionLong RunType NCPU;
   keep   DP ReqID ProjID WPType WPID DPID VerID QAVer SCDMVer OSAbbr 
          OSName SASVersion SASVersionLong RunType NCPU;

   length svalue $ 49;

   /* Save necessary signature file values for displaying in Request Summary output */

   svalue = strip(value);
   select (upcase(strip(variable)));
      when ('DP') DP = svalue;
      when ('REQID') REQID = svalue;
      when ('PROJID') PROJID = svalue;
      when ('WPTYPE') WPTYPE = svalue;
      when ('WPID') WPID = svalue;
      when ('DPID') DPID = svalue;
      when ('VERID') VERID = svalue;
      when ('QAVER') QAVer = svalue;
      when ('SCDMVER') SCDMVer= svalue;
      when ('OSABBR') OSAbbr = svalue;
      when ('OSNAME') OSName = svalue;
      when ('SASVERSION') SASVersion = svalue;
      when ('SASVERSIONLONG') SASVersionLong = svalue;
      when ('RUNTYPE') RunType = svalue;
      when ('NCPU') NCPU = svalue;
      otherwise;
   end;
         
   if eof then do;
      output;
   end;
run;

proc append base=_all_sigs data=_tmp_signature;
run;

%END;

/*--Summarize signature file information from all runs into a single record of information--*/
/*     (allowing any number of scenarios per summary run in this version of the program)     */
data _signatures; set _all_sigs end=eof;
   if eof then output;
run;

/*--Reorganize information and output to request-level signature file--*/
proc transpose data=_signatures out=_signature(rename=_NAME_=Variable rename=COL1=Value);
   var _ALL_;
run;

data msoc.alltable_signature; 
  set _signature;
  value = strip(value);
  label variable = "Metadata Variable";
run;

proc datasets lib=work nolist;
   delete _sigfiles _tmp_signature _all_sigs _sig1 _signatures _signature;
quit;

%mend soc_qasignaturerequest; 


*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;
*  End scdm_qasignaturerequest.sas                                                      ;
*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-;