#!/bin/bash
#---------------------------------------------------------------------------------------------------
#
# name:			cds_get.sh
# description: 	Gets data and templates from data archives (AVDC, NDACC, WOUDC)
#
# 2010-01-25, v1.0	Christian Retscher	initial implementation
#
#---------------------------------------------------------------------------------------------------

echo 
echo "=================================================================================================================="
echo 
echo "Convert Data Suite (CDS): get data from archives"
echo 
echo "=================================================================================================================="
echo 

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

if [ $# -ne 1 ]; then
	echo
	echo "usage: cds_get [data archive name]"
	echo
	echo "------------------------------------------------------------------------------------------"
	echo "1: archive database link: AVDC"
	echo "                          NDACC"
	echo "                          WOUDC"
	echo "------------------------------------------------------------------------------------------"
	exit
fi

# input variables
DATA_ARCHIVE=$1

# recognized data link lists
avdc_list=(
	"http://avdc.gsfc.nasa.gov/PDF/GEOMS-TE/GEOMS-TE-LIDAR-VA.csv"
	"http://avdc.gsfc.nasa.gov/PDF/GEOMS-TE/GEOMS-TE-MWR-VA.csv"
	"http://avdc.gsfc.nasa.gov/PDF/GEOMS-TE/GEOMS-TE-FTIR-VA.csv"
	"http://avdc.gsfc.nasa.gov/PDF/GEOMS-TE/GEOMS-TE-O3SONDE-VA.csv"
)

woudc_list=(
#	"ftp://ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN257/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN018/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN021/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN024/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN076/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN077/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN107/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN257/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN315/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN338/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN456/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN457/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN458/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN480/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN053/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN089/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN156/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN175/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN190/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN191/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN221/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN256/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN265/ECC/"
	"ftp://woudc:woudc*@ftp.tor.ec.gc.ca/Archive-NewFormat/OzoneSonde_1.0_1/STN328/ECC/"
)

ndacc_list=(
#	"ftp://ftp.cpc.ncep.noaa.gov/ndacc/station/maunakea/ames/mwave/"
	"ftp://ftp.cpc.ncep.noaa.gov/ndacc/station/"
)

if [ $DATA_ARCHIVE == "AVDC" ]; then
	echo "Synchronizing "$DATA_ARCHIVE

	TARGET_DIR="GEOMS-TE-VA/"
	CUT_DURS=3
	data_list=${avdc_list[@]}
fi

if [ $DATA_ARCHIVE == "NDACC" ]; then
	echo "Synchronizing "$DATA_ARCHIVE

	TARGET_DIR="Archives/"$DATA_ARCHIVE
	CUT_DURS=2
	START_LOOP=""
	data_list=${ndacc_list[@]}
fi

if [ $DATA_ARCHIVE == "WOUDC" ]; then
	echo "Synchronizing "$DATA_ARCHIVE

	TARGET_DIR="Archives/"$DATA_ARCHIVE
	CUT_DURS=2
	START_LOOP="2002"
	STOP_LOOP=`date "+%Y"`
	data_list=${woudc_list[@]}
fi

# create and change to local archive data directory
mkdir -p $TARGET_DIR
cd $TARGET_DIR

# short view of data directories which will by synchronized
for data_link_here in ${data_list[@]}
do
	echo $data_link_here
done

# loop over data directories
for data_link_here in ${data_list[@]}
do
	if [ -z $START_LOOP ]; then
		
		LINK_HERE=$data_link_here
		
		echo "----------------------------------------------------------------------------------------------------"
		echo "synchronizing data from: "$LINK_HERE
		echo "----------------------------------------------------------------------------------------------------"
	
		wget -nv --timestamping -r -nH -np -m --cut-dirs=$CUT_DURS $LINK_HERE
	else
		# loop over Envisat and Aura Validation years
		for ((YY=$START_LOOP;YY<=$STOP_LOOP;YY+=1)); do
			
			LINK_HERE=$data_link_here$YY

			echo "----------------------------------------------------------------------------------------------------"
			echo "synchronizing data from: "$LINK_HERE
			echo "----------------------------------------------------------------------------------------------------"
	
			wget -nv --timestamping -r -nH -np -m --cut-dirs=$CUT_DURS $LINK_HERE
		done
	fi	
done
