;---------------------------------------------------------------------------------------------------
;
; name:			geoms_mdext.pro
; description: 	AVDC GEOMS metadata extract
;
; 2011-08-30, v0.1	Christian Retscher	initial implementation
;
;---------------------------------------------------------------------------------------------------

;---------------------------------------------------------------------------------------------------
FUNCTION geoms_mdexttools, hdffile, fileid, chRelease, MDE_error_code

	reterr=''
	idlcr8ascii, hdffile, ga, sds, reterr

	chMDSearch=[ $
		'DATA_DISCIPLINE',$
		'DATA_LOCATION',$
		'DATA_START_DATE',$
		'DATA_STOP_DATE',$
		'DATA_SOURCE',$
		'DATA_GROUP',$
		'PI_NAME',$
		'DO_NAME',$
		'DS_NAME',$
		'DATA_VARIABLES',$
		'DATA_DESCRIPTION',$
		'FILE_GENERATION_DATE',$
		'PI_AFFILIATION',$
		'DO_AFFILIATION',$
		'DS_AFFILIATION',$]
		'FILE_META_VERSION']

	iSize1 = size(ga, /N_ELEMENTS)
	iSize2 = size(chMDSearch, /N_ELEMENTS)

	chMDVar = STRARR(iSize2)

	; loop trough all metadata elements
	for iR2 = 0, iSize2 - 1 do begin
		for iR1 = 0, iSize1 - 1 do begin
			chMD = STRSPLIT(ga(iR1), '=', ESCAPE='\', /EXTRACT)

			chMDTrim =STRTRIM(chMD[0], 1)

			if (chMDTrim eq chMDSearch[iR2]) then begin

				; find SQL variables which not full, rather than parts of GEOMS variables
				if (chMDTrim eq 'DATA_DISCIPLINE') then begin
					chMDSplit = STRSPLIT(chMD[1], ';', ESCAPE='\', /EXTRACT)
					chPlatform = chMDSplit[2]
					chDiscipline = chMDSplit[0]
					chAcquisition = chMDSplit[1]
				end	else if (chMDTrim eq 'DATA_GROUP') then begin
					chMDSplit = STRSPLIT(chMD[1], ';', ESCAPE='\', /EXTRACT)
					chOrigin = chMDSplit[0]
					chDataGroup2 = chMDSplit[1]
				end	else if (chMDTrim eq 'DATA_SOURCE') then begin
					chMDSplit = STRSPLIT(chMD[1], '_', ESCAPE='\', /EXTRACT)
					chDataSource1 = chMDSplit[0]
					chDataSource2 = chMDSplit[1]
				end	else if (chMDTrim eq 'PI_AFFILIATION') then begin
					chMDSplit = STRSPLIT(chMD[1], ';', ESCAPE='\', /EXTRACT)
					cdPIAff = chMDSplit[1]
				end	else if (chMDTrim eq 'DO_AFFILIATION') then begin
					chMDSplit = STRSPLIT(chMD[1], ';', ESCAPE='\', /EXTRACT)
					cdDOAff = chMDSplit[1]
				end	else if (chMDTrim eq 'DS_AFFILIATION') then begin
					chMDSplit = STRSPLIT(chMD[1], ';', ESCAPE='\', /EXTRACT)
					cdDSAff = chMDSplit[1]
				end	else begin
					chMDVar[iR2] = chMD[1]
				end
			end
		end
	end

	; file preparation for final AVDC pub2 location from /Volumes/OPS4/Data/GEOMS/Ingested/QA-pass/
	chDir1 = '/Volumes/OPS4/Data/GEOMS/Ingested/QA-pass/'
	chDir2 = 'http://avdc.gsfc.nasa.gov/pub2/GEOMS/registered/'

	iSize3 = strlen(chDir1)
	iSize4 = strlen(hdffile)

	if (STRMID(hdffile, 0, strlen(chDir1)) eq chDir1) then begin
		chFileSplit = STRMID(hdffile, strlen(chDir1), strlen(hdffile))
		chFileURL = chDir2+chFileSplit
	end else begin
		chFileURL = hdffile
	end

	chSQL1 = 'insert into geoms_file (datastartdate,datastopdate,file_generation_date,location,datasource,'
	chSQL1 = chSQL1 + 'datasource2,platform,discipline,acquisition,origin,datagroup2,datavariables,description,'
	chSQL1 = chSQL1 + 'originator,submitter,investigator,fileid,updated,originator_id,investigator_id,submitter_id,'
	chSQL1 = chSQL1 + 'location_id,datasource_id,platform_id,hdf_creator,avdc_data_validator,fileurl) values'
	chSQL2 = "('"+chMDVar[2]+"','"+chMDVar[3]+"','"+chMDVar[11]+"','"+chMDVar[1]+"','"+chDataSource1+"','"+chDataSource2+ $
	         "','"+chPlatform+"','"+chDiscipline+"','"+chAcquisition+"','"+chOrigin+"','"+chDataGroup2+"','"+chMDVar[9]+ $
	         "','"+chMDVar[10]+"','"+chMDVar[6]+";"+cdPIAff+"','"+chMDVar[7]+";"+cdDOAff+"','"+chMDVar[8]+";"+cdDSAff+"',"+ $
	         strtrim(string(fileid), 2)+",timezone('utc'::text, now())"+",NULL"+",NULL"+",NULL"+",NULL"+",NULL"+",NULL"+ $
	         ",'"+chMDVar[15]+"','geoms_qa "+chRelease+"','"+chFileURL+"');"
	print, 'SQL: ',chSQL1, chSQL2

END

;---------------------------------------------------------------------------------------------------
PRO geoms_mdext

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

	chRelease = 'v1.1, 2011-08-25'

	if ( iNrArgs lt 1 ) then begin
		print
		print, 'GEOMS metadata extract (geoms_mdext)'
		print, '  Release '+chRelease
		print, '  Extracts selected GEOMS metadata from HDF4/5 files.'
		print
		print, 'usage: idl -rt=geoms_mdext.sav -args [HDFFILE] [FILEID]'
		print
		print, '1: HDFFILE is the path of the HDF file, you want to check for template compliance'
		print, '(2): FILEID as from AVDC SQL DB; if given, it is part of the SQL output'
		print, '------------------------------------------------------------------------------------------------'
		print

		MDE_error_code = 3
		exit, status=MDE_error_code
	end

	hdffile = STRTRIM( args( 0 ), 2 )

	if ( iNrArgs eq 2 ) then begin
		fileid = STRTRIM( args( 1 ), 2 )
	end else begin
		fileid = string(0)
	end

	MDE_error_code = 3

	if ( file_test( hdffile ) eq 0 ) then begin
		MDE_error_code = 3
		print, '  ERROR: HDF file not found.'
	end else begin
		MDE_error_code = 0
	end

	if ( MDE_error_code eq 0 ) then begin
		MDE_error_code = geoms_mdexttools( hdffile, fileid, chRelease, 0 )
	end

	exit, status=MDE_error_code

END