#!/bin/bash
#---------------------------------------------------------------------------------------------------
#
# name:			run_cds.sh
# description: 	Runs through all recognized data archives and provides links to data (AVDC, NDACC, WOUDC)
#
# 2010-01-25, v1.0	Christian Retscher	initial implementation
# 2010-02-16, v1.1	Christian Retscher	create log dir
#
#---------------------------------------------------------------------------------------------------

echo 
echo "=================================================================================================================="
echo 
echo "Convert Data Suite (CDS): run suite"
echo 
echo "=================================================================================================================="
echo 

#---------------------------------------------------------------------------------------------------
# runtime options

chText=(
	"get/synchronize all compatible data"
	"match files with tranlation tables"
	"convert files in matched files list"
)

# common variables
chDirExe='./'
chDateHere=`date "+%Y%m%dT%H%M%S"`
chDirGEOMS="GEOMS-TE-VA/"
chDirArchives="Archives/"
chDirTranslation="Translation/"
chDirHDF="HDF/"
chDirLog="log/"
chFileTAV="tableattrvalue_03R004_cds.dat"
chFileOutput="output"

if [ $# -ne 3 ] && [ $# -ne 4 ]; then
	echo
	echo "usage: run_cds [cds_get] [cds_match] [cds_convert] [output file]"
	echo
	echo "------------------------------------------------------------------------------------------"
	echo "1: cds_get:     0: do not "${chText[ 0 ]}
	echo "                1:        "${chText[ 0 ]}" (may take a couple of hours)"
	echo "------------------------------------------------------------------------------------------"
	echo "2: cds_match:   0: do not "${chText[ 1 ]}
	echo "                1:        "${chText[ 1 ]}
	echo "------------------------------------------------------------------------------------------"
	echo "3: cds_convert: 0: do not "${chText[ 2 ]}
	echo "                1:        "${chText[ 2 ]}
	echo "------------------------------------------------------------------------------------------"
	echo "4: output file: Path to output file, where matching files are stored." 
	echo "                This is needed for cds_match and cds_convert only."
	echo "                If the output file is not given, a file with timestamp will be created" 
	echo "                in cds_match and passed on to cds_convert."
	echo "                Note, calling cds_convert only requires to explicitly give an file" 
	echo "                which contains cds_match compliant output."
	echo "------------------------------------------------------------------------------------------"
	exit
fi

#---------------------------------------------------------------------------------------------------
# input variables
OPTION1=$1
OPTION2=$2
OPTION3=$3
OUTPUT=$4

#---------------------------------------------------------------------------------------------------
# verify input data

if [ $OPTION2 -eq 0 ] && [ $OPTION3 -eq 1 ] && [ -z $OUTPUT ]; then
	echo "Please specify on output file, with a cds_match compliant output format."
	exit
fi

#---------------------------------------------------------------------------------------------------
# synchronize data
if [ $OPTION1 -eq 1 ]; then
	chDateHereSpecial=`date "+%Y%m%dT%H%M%S"`

	echo "----------------------------------------------------------------------------------------------------"
	echo $chDateHereSpecial": "${chText[ 0 ]}
	echo "----------------------------------------------------------------------------------------------------"

	echo 
	echo "AVDC"
	./cds_get.sh "AVDC"

	echo 
	echo "NDACC"
	./cds_get.sh "NDACC"

	echo 
	echo "WOUDC"
	./cds_get.sh "WOUDC"
fi

#---------------------------------------------------------------------------------------------------
# match data
chDateHereSpecial=`date "+%Y%m%dT%H%M%S"`

# create log dir
mkdir -p $chDirExe$chDirLog
mkdir -p $chDirHDF

if [ -z $OUTPUT ]; then
	chFileOutputHere=$chDirExe$chDirLog$chFileOutput'_'$chDateHere'.txt'
	chFileOutputTmpHere=$chDirExe$chDirLog$chFileOutput'_'$chDateHere'.txt_tmp'
else
	chFileOutputHere=$OUTPUT
	chFileOutputTmpHere=$OUTPUT'_tmp'
fi

if [ $OPTION2 -eq 1 ]; then
	echo "----------------------------------------------------------------------------------------------------"
	echo $chDateHereSpecial": "${chText[ 1 ]}
	echo "----------------------------------------------------------------------------------------------------"

	echo
	echo "output file: "$chFileOutputHere
	echo

	# remove old output file
	\rm -f $chFileOutputHere
	\rm -f $chFileOutputTmpHere

	# get all Archives sub-directories
	chDirSource1=`find $chDirArchives/NDACC/*/ames/* -type d`
	chDirSource2=`find $chDirArchives/WOUDC/STN*/ECC/* -type d`

	chDirSource=( 
		"${chDirSource1[@]}"
		"${chDirSource2[@]}" 
	)
	
	# run first find results
	for chDirSourceHere in ${chDirSource[@]};
	do
		chDateHereSpecial=`date "+%Y%m%dT%H%M%S"`

		echo $chDateHereSpecial": "$chDirSourceHere
		idl -rt=./cds_match.sav -args $chDirExe$chDirSourceHere $chDirExe$chDirGEOMS $chDirExe$chDirTranslation $chFileOutputTmpHere
		
		cat $chFileOutputTmpHere >> $chFileOutputHere
	done

	\rm $chFileOutputTmpHere
	
	echo
	echo "output file: "$chFileOutputHere
	echo
fi

#---------------------------------------------------------------------------------------------------
# convert data
if [ $OPTION3 -eq 1 ]; then
	chDateHereSpecial=`date "+%Y%m%dT%H%M%S"`

	echo "----------------------------------------------------------------------------------------------------"
	echo $chDateHereSpecial": "${chText[ 2 ]}
	echo "----------------------------------------------------------------------------------------------------"

	idl -rt=./cds_convert.sav -args $chFileOutputHere $chDirExe$chFileTAV $chDirExe$chDirHDF
fi
