;---------------------------------------------------------------------------------------------------
;
; name:			geoms_qa.pro
; description: 	Wrapper for AVDC/EVDC QA tool
;
; 2010-12-09, v0.1	Christian Retscher	initial implementation
; 2010-12-21, v0.5	Christian Retscher	rename to geoms_qa
; 2011-03-03, v0.6	Christian Retscher	dim ordering support
; 2011-03-29, v0.7	Christian Retscher	dialog; high level error checks
; 2011-04-26, v0.8	Christian Retscher	geoms_tc support added
; 2011-06-07, v1.0	Christian Retscher	code refactored
; 2011-08-19, v1.1	Christian Retscher	accept balloon_sonde.o3 files with DATETIME fill values
; 2012-04-26, v1.2  Ian Boyd  align version number with geoms_tools, update comment to include
;                   QA on netCDF files
; 2013-10-23, v1.3  Ian Boyd  align version with geoms_tools
; 2013-10-28, v1.31 Ian Boyd  align version with geoms_tools
; 2014-01-28, v1.34 Ian Boyd  align geoms_tools version due to change in geoms_tctools
; 2014-03-11, v1.35 Ian Boyd  align version with geoms_tools
; 2014-03-25, v1.36 Ian Boyd  update due to modification to idlcr8ascii
; 2014-03-29, v1.37 Ian Boyd  update due to modification to idlcr8hdf
; 2014-05-22, v1.38 Ian Boyd  update due to modification to idlcr8hdf
; 2014-08-06, v1.39 Ian Boyd  update due to modification to idlcr8hdf and geoms_tools
; 2014-09-09, v1.40 Ian Boyd  update due to modification to idlcr8hdf and geoms_tools
; 2014-11-10, v1.41 Ian Boyd  update due to modification to idlcr8hdf and idlcr8ascii
; 2015-01-27, v1.42 Ian Boyd  update due to modification to idlcr8hdf and idlcr8ascii
; 2015-02-17, v1.43 Ian Boyd  update due to modification to idlcr8hdf and idlcr8ascii
; 2015-03-02, v1.44 Ian Boyd  update due to bug fix in idlcr8hdf
; 2015-04-09, v1.45 Ian Boyd  update due to bug fix in idlcr8hdf
; 2015-08-11, v1.46 Ian Boyd  update due to bug fixes and improvements in idlcr8hdf
; 2015-09-26, v1.47 Ian Boyd  update due to bug fixes and improvements in idlcr8hdf
; 2015-10-21, v1.48 Ian Boyd  update due to bug fixes and improvements in idlcr8hdf
; 2015-11-04, v1.49 Ian Boyd  update due to fix in idlcr8hdf
; 2015-11-09, v1.50 Ian Boyd  update due to bug fixes and improvements in idlcr8hdf and idlcr8ascii
; 2015-11-16, v1.51 Ian Boyd  update due to fix in idlcr8hdf
; 2015-12-15, v1.52 Ian Boyd  update due to fix in idlcr8hdf
; 2016-02-13, v1.53 Ian Boyd  Add extra satellite instruments to the obsolete list for DATA_SOURCE
;                             in idlcr8hdf. Modifications to idlcr8hdf and idlc8ascii
; 2016-06-14, v1.54 Ian Boyd  update due to fix in idlcr8ascii
; 2016-07-25, v1.55 Ian Boyd  update due to fix in idlcr8ascii and idlcr8hdf
; 2016-11-16, v1.56 Ian Boyd  update due to fix in idlcr8hdf
; 2016-11-30, v1.57 Ian Boyd  update due to fix in idlcr8ascii and idlcr8hdf and account for
;                             non-GEOMS variable attributes (ptr_valid check) in geoms_tctools
; 2016-12-17, v1.58 Ian Boyd  update due to fixes in idlcr8ascii
; 2016-12-30, v1.59 Ian Boyd  Add FV keyword to geoms_tools (options 20-22 in geoms_harmon call).
;                             If called then forces the DATA_FILE_VERSION not to increment when
;                             performing harmonization; Incorporates fixes made to idlcr8hdf; QA no
;                             longer checks full ORIGINATOR attributes - removed from TAV file from
; 2017-03-31, v1.60 Ian Boyd  update due to improvements to idlcr8hdf
; 2017-11-21, v1.61 Ian Boyd  update due to fixes in idlcr8hdf
; 2018-02-18, v1.62 Ian Boyd  update due to fix in idlcr8ascii and idlcr8hdf
; 2018-03-14, v1.63 Ian Boyd  update due to fix in idlcr8hdf
; 2018-05-28, v1.64 Ian Boyd  update due to fix in idlcr8hdf
; 2018-09-01, v1.65 Ian Boyd  update due to fix in idlcr8hdf
; 2018-11-16, v1.66 Ian Boyd  update due to fix in idlcr8ascii
; 2019-05-06, v1.67 Ian Boyd  QA checks now carried out using the metadata definitions in the TAV version
;                             given in the FILE_META_VERSION global attribute. If this attribute is invalid
;                             or not present, or the TAV file can't be located, the input TAV file will
;                             be used for checks but an error code will be generated. Fix to idlcr8hdf
;                             that caused a crash if the file does not use a DATA_TEMPLATE
; 2019-05-14, v1.68 Ian Boyd  update due to fix in idlcr8ascii
; 2019-08-07, v1.69 Ian Boyd  update due to modifications to idlcr8ascii and idlcr8hdf
; 2019-08-21, v1.70 Ian Boyd  update due to fix in idlcr8ascii
; 2019-12-05, v2.00 Ian Boyd  Add support for checks on the optional DATA_SOURCE Version Name value
;                             (third field in DATA_SOURCE). Calls new geoms_vntools function (similar
;                             in concept to geoms_tctools)
; 2020-02-26, v2.01 Ian Boyd  update due to geoms_vntools updates
; 2020-03-11, v2.02 Ian Boyd  update due to fix to idlcr8hdf
; 2020-04-27, v2.03 Ian Boyd  update due to geoms_vntools update
; 2020-07-09, v2.04 Ian Boyd  update due to fix to idlcr8hdf
; 2020-09-30, v2.05 Ian Boyd  update due to fix to idlcr8ascii; geoms_tools now returns a QA_error_code
;                             value of -2 to geoms_qa if idlcr8ascii can't read the input file, to stop
;                             template and version name checks
; 2020-10-20, v2.06 Ian Boyd  update due to fixes to idlcr8hdf
; 2020-10-26, v2.07 Ian Boyd  update due to fix to idlcr8hdf
; 2020-11-22, v2.08 Ian Boyd  QA checks carried out using the metadata definitions in the TAV version
;                             determined by the FILE_GENERATION_DATE (FGD), in the first instance, then the
;                             FILE_META_VERSION (FMV), if different. This adds a check for the FMV TAV version
;                             using definitions that are out-of-date. If either TAV can't be determined or found
;                             then the input TAV will be used for checks and an error generated
; 2020-12-11, v2.09 Ian Boyd  Apply fixes and improvements to procedures identifying correct TAV version(s) to
;                             use for checks; Fix IDL Math Error (Floating Illegal Operand) in idlcr8hdf
; 2021-06-17, v2.10 Ian Boyd  update due to fix to idlcr8hdf
; 2022-08-09, v2.11 Ian Boyd  update due to fixes and improvements in geoms_tools, idlcr8hdf and idlcr8ascii
; 2023-12-18, v2.12 Ian Boyd  update due to fixes to idlcr8hdf and geoms_vntools
; 2024-01-12, v2.13 Ian Boyd  update due to fix to idlcr8hdf
; 2024-09-12, v2.14 Ian Boyd  update due to fixes to idlcr8ascii and idlcr8hdf
; 2024-09-27, v2.15 Ian Boyd  update due to fix to idlcr8ascii
; 2024-10-29, v2.16 Ian Boyd  update due to fixes to geoms_tctools and idlcr8hdf
; 2024-11-26, v2.17 Ian Boyd  Add run-time error handling capability in geoms_qa.pro and geoms_harmon.pro
;                             plus fixes in idlcr8hdf and idlcr8ascii
;---------------------------------------------------------------------------------------------------

PRO geoms_qa

	args = command_line_args()
	iNrArgs = size(args, /N_ELEMENTS)

	qaver='version 2.17, 2024-11-26'

	if ( iNrArgs lt 2 ) then begin
		print
		print, 'GEOMS quality assurance (geoms_qa)'
		print, '  Release '+qaver
		print, '  Does GEOMS quality assurance on HDF4/5 and netCDF files.'
		print
		print, 'usage: idl -rt=geoms_qa.sav -args [TAVFILE] [[GEOMSTEFILE]] [ORIGFILE]'
		print
		print, '1: TAVFILE is the path of the TAV file, you want to validate against'
		print, '2: GEOMSTEFILE is the path of the GEOMS template file, you want to test
		print, '      (optional argument; template checks are only performed if GEOMS-TE is provided)'
		print, '3: ORIGFILE is the path of the original HDF file, you want to test'
		print, '------------------------------------------------------------------------------------------------'
		print

		QA_error_code = 3
		exit, status=QA_error_code
	end

	; error codes
	; 0 QA pass, 	no TC
	; 1 QA limit, 	no TC
	; 2 QA fail, 	no TC
	; 3 wrong parameters
	; 4 QA pass, 	TC pass
	; 5 QA limit, 	TC pass
	; 6 QA fail, 	TC pass
	; 7 QA pass,	TC fail
	; 8 QA limit,	TC fail
	; 9 QA fail, 	TC fail
	;99 Run-time error 
	
	chFileTAV 	= STRTRIM( args( 0 ), 2 )

	; check the number of variables provided
	if ( iNrArgs eq 2 ) then begin
		chFileOrig	= STRTRIM( args( 1 ), 2 )
	end
	if ( iNrArgs eq 3 ) then begin
		chFileGEOMSTE	= STRTRIM( args( 1 ), 2 )
		chFileOrig	= STRTRIM( args( 2 ), 2 )
	end

	QA_error_code = 3 ;default QA error code
	TC_error_code = 1 ;default TC error code
	TC_error_code_2 = 1 ;default VN error code

	if ( file_test( chFileTAV ) eq 0 ) then begin
		QA_error_code = 2 ;can still do template checks if HDF and template files are present
		print, 'ERROR: TAV file not found.'
	end else begin
		QA_error_code = 0
	end

	if ( file_test( chFileOrig ) eq 0 ) then begin
		QA_error_code = 3
    print, 'ERROR: GEOMS file not found.'
	end else begin
    if QA_error_code ne 2 then QA_error_code = 0
	end

  if ( iNrArgs eq 3 ) then begin
    if ( file_test( chFileGEOMSTE ) eq 0 ) then begin
      print, 'ERROR: GEOMS-TE file not found.'
    end else begin
      if QA_error_code ne 3 then TC_error_code = 0 ;i.e. HDF file exists
    endelse
  end

  if QA_error_code ne 3 then TC_error_code_2 = 0 ;i.e. HDF file exists

	if ( QA_error_code eq 0 ) then begin
		print, chFileTAV
		print, chFileOrig
		
		CATCH, error_status
		IF error_status NE 0 THEN BEGIN
		  ;Run-time error so exit nicely
		  CLOSE,/ALL
		  ;Delete idlcr8ascii.log and idlcr8qa.log if they exist
		  hdfdir=FILE_DIRNAME(chFileOrig,/MARK_DIRECTORY)
		  FILE_DELETE,hdfdir+'idlcr8ascii.log',/QUIET
		  FILE_DELETE,hdfdir+'idlcr8qa.log',/QUIET
		  ;Write Run-time error to the log file
		  logfile=STRMID(chFileOrig,0,STRPOS(chFileOrig,'.',/REVERSE_SEARCH))+'.log'
		  etxt=' Please contact Ian Boyd at iboyd@bryanscientific.org and nadirteam@nilu.no'
		  OPENW,lu,logfile, /GET_LUN, /Append
		  PRINTF,lu,''
		  PRINTF,lu,'  RUN-TIME ERROR: '+!ERROR_STATE.MSG
		  PRINTF,lu,''
		  PRINTF,lu,'99 (Failed with Run-Time error.'+etxt+')'
		  FREE_LUN,lu
		  CATCH,/CANCEL
		  QATC_error_code=99
		  exit, status=QATC_error_code
		ENDIF
		
		if (STRMID(FILE_BASENAME(chFileOrig),0,16) eq "balloon_sonde.o3") then begin
			QA_error_code = geoms_tools( 0, chFileTAV, chFileOrig ) ;, /DATETIME )
		end else begin
			QA_error_code = geoms_tools( 0, chFileTAV, chFileOrig )
		end
	endif else if (TC_error_code eq 0) or (TC_error_code_2 eq 0) then begin
    ;can't do QA but can do Template and/or Version Name checks so create logfile
	  logfile=STRMID(chFileOrig,0,STRPOS(chFileOrig,'.',/REVERSE_SEARCH))+'.log'
	  OPENW, lu, logfile, /GET_LUN
	  PRINTF,lu, 'geoms_tools '+qaver
	  PRINTF,lu,'GEOMS Quality Assurance Tool'
	  PRINTF,lu,'Maintained by the ESA Validation Data Center, EVDC'
	  PRINTF,lu,'https://evdc.esa.int/documentation/geoms/'
	  PRINTF,lu,''
	  PRINTF,lu,'Log started on '+SYSTIME(0)
	  PRINTF,lu,'Result of performing Quality Assurance on '+chFileOrig
	  PRINTF,lu,'(0=PASS, 1=GEOMS compliant with non-standard components, 2=FAIL)'
    PRINTF,lu,''
    PRINTF,lu,'ERROR: TAV file not found'
    IF iNrArgs eq 3 then $
      printf,lu,'INFORMATION: Required Metadata Template: '+File_Basename(chFileGEOMSTE)
    PRINTF,lu,''
    PRINTF,lu,'2 (Failed QA)'
    Free_Lun, lu
  endif

  if QA_error_code ne -2 then begin
    ;Determine working directory for the version names list file
    ;First try the template directory, but otherwise use the TAV file directory
	  if iNrArgs eq 3 then chDirGEOMSvn = FILE_DIRNAME(chFileGEOMSTE, /Mark_Directory) $
	  else if iNrArgs eq 2 then chDirGEOMSvn = FILE_DIRNAME(chFileTAV, /Mark_Directory) $
	  else chDirGEOMSvn = '' ;can't find directory holding the version names list file

	  if ( TC_error_code eq 0 ) then begin
	  	TC_error_code = geoms_tctools( chFileGEOMSTE, chFileOrig, QA_error_code)
	  	TC_error_code_hold = TC_error_code*3 + 4
    endif else if (iNrArgs eq 3) and (QA_error_code ne 3) then begin
      logfile=STRMID(chFileOrig,0,STRPOS(chFileOrig,'.',/REVERSE_SEARCH))+'.log'
      OPENW, lu, logfile, /GET_LUN, /Append
      printf, lu, ''
      printf, lu, 'ERROR: GEOMS Metadata Template file not found'
      Free_Lun, lu
      QA_error_code = 3
      TC_error_code = 0 & TC_error_code_hold = 0
    endif else begin
  		TC_error_code = 0 & TC_error_code_hold = 0
  	endelse

	  if TC_error_code_2 eq 0 then begin
	    TC_error_code_2 = geoms_vntools( chDirGEOMSvn, chFileOrig, QA_error_code, TC_error_code )
      ;Note TC_error_code_2 will return the highest of TC_error_code or the Version Name error check value
      if (TC_error_code_2 eq -1) or (QA_error_code eq 3) then begin
	      ;File input error as data_version_name list file can't be found and there is a version name in the GEOMS file
	      TC_error_code = 0 & QA_error_code = 3
	    endif else TC_error_code = TC_error_code_2*3 + 4
   	endif else TC_error_code = TC_error_code_hold
  endif

  ; total error
  QATC_error_code = abs(QA_error_code) + TC_error_code
    
  exit, status=QATC_error_code

END