#!/bin/bash # #------------------------------------------------------------------------------ # Identify the files needed to support the NPP Polar Wander file # binary and ascii files needed for running the CSPP software. # The script will first try to find the file locally and if it is # not there, try to download it from the JPSS_REMOTE_ANC_DIR (see below). # The files that it will identify will be a pair of files converted # from the original H5 - one Blob and one accompanying ascii file. # # January 2012 Written by # Kathleen Strabala UW-Madison, SSEC, kathy.strabala@ssec.wisc.edu # # Modified April 2012 because the polar wander files started # appearing on Fridays, instead of Thursdays. The script will now # check for a file each day. KIS # # Fixed bug introduced by April update. Mistakenly took out part # of script that checked if the file date was a date that one # of the files was issued. If it is before 12UTC on that date, # you need to use the last file issued because this one is not # valid until 12 UTC. KIS 6/8/2012 # # Added Option to only check local directory - no downloads. # KIS 3/15/2013 # # Note that the following environment variables are needed in the # in the get_anc_jpss_tle.bash. If they are not set, they will # default to: # $JPSS_LOCAL_ANC_DIR is the local ancillary data directory # e.g. ${PWD}/ancillary # $JPSS_REMOTE_ANC_DIR is the remote FTP ancillary data directory (URL format) # e.g. http://jpssdb.ssec.wisc.edu/cspp_v_2_0/ancillary/ #------------------------------------------------------------------------------ # Check number of arguments if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then echo "Usage: get_anc_jpss_polarwander.bash date time [search_flag]" echo "where" echo " date is the required date (yyyyddd)" echo " yyyy is the year " echo " ddd is the day of the year " echo " time is the required time (hhmm)" echo " hh is the hour " echo " mm is the minute " echo " search_flag - Optional:" echo " 1 - Search locally only, 0 (default) - Search local and remotely" echo "The output is an echo of the blob filename and path, and the echo " echo " of the matching ascii file and path with a space separating them." exit 1 fi # Before we begin, make sure that wget is available which wget > /dev/null if [ $? != 0 ] ; then echo " " > /dev/stderr echo "**** ERROR: Could not find the wget utility. The ancillary" > /dev/stderr echo "**** data fetchers will not work without it. Please install" > /dev/stderr echo "**** and try again." > /dev/stderr echo " " > /dev/stderr exit 1 fi # If the local ancillary directory variable is not set, then default to # current directory if [ -z "$JPSS_LOCAL_ANC_DIR" ] ; then export JPSS_LOCAL_ANC_DIR=${PWD} fi # If the remote ancillary directory variable is not set, then default to # the if [ -z "$JPSS_REMOTE_ANC_DIR" ] ; then export JPSS_REMOTE_ANC_DIR=http://jpssdb.ssec.wisc.edu/cspp_v_2_0/ancillary fi # Set Polar Wander filename head and tail file_head="off_USNO-PolarWander-UT1-ANC_Ser7_USNO_000f_" file_tail="Internal" # Set Polar Wander date range (window centered on current day) # The files are created on Thursday's date_range=(0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 \ -17 -18 -19 -20 -21 -22 -23 -24 -25 -26 -27 -28 -29 -30 ) #------------------------------------ # The rest of the script searches directories named yyyy/ddd in the # local and remote ancillary directories for files named # ${file_head}yymmdd${tail} within the search window defined by date_range # Check local directory if [ ! -d $JPSS_LOCAL_ANC_DIR ] ; then echo "Local ancillary data directory does not exist:JPSS_LOCAL_ANC_DIR " ${JPSS_LOCAL_ANC_DIR} exit 1 fi # Get arguments date=$1 if [[ $date -lt 2000001 || $date -gt 2100365 ]] ; then echo "Invalid beginning date string:" $date exit 4 fi data_time=$2 data_time=$((10#$data_time)) if [[ $data_time -lt 0 || $data_time -gt 2359 ]] ; then echo "Invalid time entered " $data_time exit 5 fi # Do you want to search for files locally only? search_flag=${3:-0} if [ "$search_flag" != "0" ] && [ "$search_flag" != "1" ]; then echo "Search flag must be set to either 0 or 1 " > /dev/stderr exit 1 fi # Compute Gregorian date (yyyymmdd) # Find the last Thursday jday1=`echo $date | cut -c5-7` jday=`expr $jday1 - 1` data_year=`echo $date | cut -c1-4` greg_date=`date --date="$data_year-01-01 + $jday days" "+%y%m%d"` greg_month=`date --date="$data_year-01-01 + $jday days" "+%m"` greg_year=`date --date="$data_year-01-01 + $jday days" "+%y"` greg_day=`date --date="$data_year-01-01 + $jday days" "+%d"` try=1 # Search date range for an acceptable file for delta in "${date_range[@]}" do if [[ $try -eq 1 && $data_time -lt 1200 ]] ; then # Compute Gregorian date adjusted by delta (yymmdd) file_date=`date --date="20$greg_year-$greg_month-$greg_day -1 days" "+%y%m%d"` else # Compute Gregorian date adjusted by delta (yymmdd) file_date=`date --date="20$greg_year-$greg_month-$greg_day $delta days" "+%y%m%d"` fi # Set binary file name file_name="*"$file_head"20"$file_date"*"$file_tail echo "Searching for file "$file_name > /dev/stderr # Get year, month, day and day of month for directory name year=`echo "20"$file_date | cut -c1-4` month=`echo $file_date | cut -c3-4` dd=`echo $file_date | cut -c5-6` day=`date --date="$year-$month-$dd" "+%j"` # Set local file path and name DAY_DIR=${year}_${month}_${dd}_${day} local_file=$JPSS_LOCAL_ANC_DIR/$DAY_DIR/$file_name # Check for file in local directory # echo "find -L $JPSS_LOCAL_ANC_DIR/$DAY_DIR -nowarn -name $file_name" > /dev/stderr success=`find -L $JPSS_LOCAL_ANC_DIR/$DAY_DIR -nowarn -name $file_name 2> /dev/null` if [ "${success}" != "" ] ; then ascii=`basename $success | cut -d. -f1` ascii_file=${ascii}.asc local_ascii_filename=$JPSS_LOCAL_ANC_DIR/$DAY_DIR/$ascii_file success=`find -L $JPSS_LOCAL_ANC_DIR/$DAY_DIR -nowarn -name "$ascii_file" 2> /dev/null` if [ "${success}" != "" ] ; then # Files were found locally echo "Files were found on local disk" > /dev/stderr echo $local_file echo $local_ascii_filename exit 0 fi elif [ $search_flag -eq 0 ] ; then # File was not found locally, so try to download it # echo "Trying to download file from "$JPSS_REMOTE_ANC_DIR/$DAY_DIR/$file_name > /dev/stderr wget -t 5 -T 15 -c -r -np -nH --cut-dirs=3 -q -N -A*off_USNO-PolarWander-UT1-ANC_Ser7_USNO_000f*Z_*_Internal $JPSS_REMOTE_ANC_DIR/$DAY_DIR/ # Was it successful? blob=`find . -nowarn -name $file_name -print 2> /dev/null` if [ "$blob" != "" ] ; then tmpascii=`basename $file_name | cut -d. -f1` ascii=${tmpascii}.asc wget -t 5 -T 15 -c -r -np -nH --cut-dirs=3 -q -N -A${ascii} $JPSS_REMOTE_ANC_DIR/$DAY_DIR/ ascii_file=`find . -nowarn -name $ascii -print 2> /dev/null` if [ "$ascii_file" != "" ] ; then if [ ! -d $JPSS_LOCAL_ANC_DIR/$DAY_DIR ] ; then mkdir $JPSS_LOCAL_ANC_DIR/$DAY_DIR fi else #If you are here, then you have a blob file but no matching ascii file echo "Error: Could not find Polar Wander ascii file to match blob for day $DAY_DIR" exit 2 fi mv $blob $JPSS_LOCAL_ANC_DIR/$DAY_DIR/. mv $ascii_file $JPSS_LOCAL_ANC_DIR/$DAY_DIR/. if [ $? -eq 0 ] ; then echo "Files were downloaded successfully" > /dev/stderr echo $JPSS_LOCAL_ANC_DIR/$DAY_DIR/$blob echo $JPSS_LOCAL_ANC_DIR/$DAY_DIR/$ascii_file exit 0 fi # Delete the file if it has zero length elif [ -z $file_name ] ; then rm $file_name fi fi try=$[ $try + 1 ] done # Ancillary file was not found echo "ERROR: Ancillary JPSS Polar Wander blob file could not be found for day $date" > /dev/stderr exit 1