Shell Script for Directing Optics Data Ingest

After the campbell data ingest, Bigelow Labs retrieves the optics and SBE37 data. After their processing is done, it is made available via HTTP. We grab several days of data each time, process what's needed, then archive the daily files.

#!/bin/bash

do_it {

	#
	# define a working subdirectory
	cd $download_dir
	
	
	#
	# loop thru the last few days, grabbing all the data from Bigelow
	for day_back in 0 1 2 3 4 5; do
		
		pwd
	
		echo `date`
		#
		# Construct the date information
		YYYY=`date "-d -$day_back days" +%Y`
		year=`date "-d -$day_back days" +%y`
		month=`date "-d -$day_back days" +%m`
		day=`date "-d -$day_back days" +%d`
		
	
		remote_fname=optics_${current_buoy}.${YYYY}-${month}-${day}
		echo $remote_fname
	
		#
		# Grab all files for the specified days
		wget -O tmp.dat http://www.bigelow.org/~ahb/GoMOOSData/daily/${current_buoy}/${remote_fname}
		tail +2 tmp.dat >> optics.dat
		echo cp tmp.dat /data/gomoos/buoy/${current_buoy}/archive/optics/${remote_fname}
		cp tmp.dat /data/gomoos/buoy/${current_buoy}/archive/optics/${remote_fname}
		input_file=optics.dat
	
	done
	
	if [ -s $input_file ]; then
	
		nice matlab -nojvm -nosplash -r  'process_bigelow_optics( '\'$current_buoy\'' ',' '\'$input_file\'' )'  
	
		#
		# copy the files into the permanent repository
		mkdir -p /data/gomoos/buoy/${mooring_id}/archive/optics 
		cp ${current_buoy}* /data/gomoos/buoy/${mooring_id}/archive/optics 
	
	fi
	
	
	#
	# Delete the files, we're done with them.
	cd ..
	rm -rf $download_dir

}






#
# Were we passed the string "testing" as an argument?
# Set the testing env variable accordingly.
if [ $# != "0" ]; then

	if [ $1 = "testing" ]; then
		export GOMOOS_TESTING="1"
		export GOMOOS_ROOT=/home/jevans/www/gomoos
		download_dir=./
		shift
	else
		export GOMOOS_TESTING="0"
		export GOMOOS_ROOT=/data/gomoos
		download_dir=${GOMOOS_ROOT}/scratch/$$
		echo creating temporary directory $download_dir
		mkdir $download_dir
	fi
	
else
	export GOMOOS_TESTING="0"
	export GOMOOS_ROOT=/data/gomoos
fi



#
# Set up the rest of the environment.
. ${GOMOOS_ROOT}/bin/setup_gomoos_environment.sh


#
# Master log file
master_log_file=${GOMOOS_ROOT}/log/buoy.log


#
# process the buoy
current_buoy=${1}


#
# check the pid file.  If it exists, then don't run
pidfile=${GOMOOS_ROOT}/run/optics_${current_buoy}
if ls ${pidfile}
then
	#
	# pid file exists. must already be running?
	echo "Pid file ${pidfile} exists, too much going on.  Remove this file if in error." | mail jevans
	exit 1
fi



do_it >> ${pidfile}


#
# Save the pidfile to the master log
cat ${pidfile} >> ${master_log_file}






#
# removing pid file
echo removing pid file ${pidfile}
rm -f ${pidfile}

echo all done, bye

exit