set -xa
date

echo "------------------------------------------------"
echo "Executing exradar_canrad.sh.sms XXXX "
echo "------------------------------------------------"

ntask=$1
echo "running ntask=$ntask"
export PS4='NTASK${ntask}:$SECONDS + '

#############################################################
# Process each station for this task
#############################################################

for stng in `cat file.station_$ntask`
do
    cfiles=`find ${RAWDATA}/${stng} -name '*VOL*IRIS' -exec ls -1 {} \; | wc -l`
    cfilesm1=`expr $cfiles - 1`
    find ${RAWDATA}/${stng} -name '*VOL*IRIS' -exec ls -1 {} \; | sort | head -$cfilesm1 > current_files_${stng}

    new_files_found=YES

    if [ -s $HIST/radar_canrad_${stng}.history ]
    then

	##  Sort the history file (i.e. the files that have already been processed for this station)

	sort -d -o radar_canrad_${stng}.history_tempsort $HIST/radar_canrad_${stng}.history
	if [ $? -eq 0 ]
	then
	    awk ' { print $1 } ' radar_canrad_${stng}.history_tempsort > $HIST/radar_canrad_${stng}.history
	else
	    msg="**FAILURE IN SORT OF FILES IN $HIST/radar_canrad_${stng}.history in task $ntask - non-fatal"
	    postmsg "$jlogfile" "$msg"
	    set +x
	    echo
	    echo $msg
	    echo
	    set -x
	fi

	##  Determine if there are any new files to process, and update the history file

	comm -13 $HIST/radar_canrad_${stng}.history current_files_${stng} > new_files_${stng}
	comm -23 $HIST/radar_canrad_${stng}.history current_files_${stng} > old_files_${stng}
	if [ -s old_files_${stng} ]
	then
	    set +x
	    echo
	    echo
	    echo "The following files are no longer in $RAWDATA and are being removed from the history file:"
	    cat old_files_${stng}
	    echo
	    echo
	    set -x
	    comm -12 $HIST/radar_canrad_${stng}.history current_files_${stng} > remaining_files_${stng}
	    mv remaining_files_${stng} $HIST/radar_canrad_${stng}.history
	fi

	if [ ! -s new_files_${stng} ]
	then
	    msg="There were new files to process in $RAWDATA for station ${stng}"
	    set +x
	    echo
	    echo
	    echo $msg
	    echo
	    echo
	    set -x
	    new_files_found=NO
	fi
    else 
	##  No history file previously existed for this station, so one will be created later on
	echo "Copying current files to new files"
	cp current_files_${stng} new_files_${stng}
    fi

    if [ $new_files_found = YES ]
    then
	set +x
	echo
	echo
	echo "The following new files in $RAWDATA are available for processing :"
	cat new_files_${stng}
	echo
	echo
	set -x

	for filename in `cat new_files_${stng}`
	do
	    ## Get the current date/hour/minute
	    curtim=`/nwprod/util/exec/mdate`
	    echo "current datetime = " $curtim

	    ## Calculate the date/hour/minute $mxagemin minutes ago.  We will only process the file
	    ## if it is dated between then and the current date/hour/minute.
	    curtim_mam=`/nwprod/util/exec/mdate -$mxagemin $curtim`
	    echo "current datetime - $mxagemin minutes = " $curtim_mam

	    ## Get the date/hour/minute of the file.
	    filtim=`basename $filename | cut -c1-12`

	    ## Check if the file is within the specified window.
	    if [ $filtim -ge $curtim_mam -a $filtim -le $curtim ]
	    then
		msg="CANADIAN RADAR FILE $filename IS WITHIN ALLOWABLE TIME WINDOW AND WILL BE PROCESSED"
		set +x
		echo
		echo $msg
		echo
		set -x

		## Process the file.
		export pgm=decod_dccrad
		set +u
		. prep_step
		set -u
		${DECDROOT}/exec/decod_dccrad -v 2 -d ${DCOMROOT}/decoder_logs/decod_dccrad.log \
			$filename ${FIXbufr}/bufrtab.006
		err=$?
		if [ $err -eq 0 ]
		then
		    set +x
		    echo "$pgm completed NORMALLY for file $filename"
		    set -x
		else
		    msg1="**ERROR: $pgm FAILED  with r.c. $err for $filename in task $ntask -"
		    msg2="DATA IN THIS FILE NOT PROCESSED, MOVE ON TO NEXT FILE --> non-fatal"
		    postmsg "$jlogfile" "$msg1 $msg2"
		fi
	    else
		msg1="CANADIAN RADAR FILE $filename IS OUTSIDE OF THE ALLOWABLE TIME WINDOW AND WILL"
		msg2="NOT BE PROCESSED --> non-fatal"
		postmsg "$jlogfile" "$msg1 $msg2"
	    fi

	    ## Save the filename to a temporary history list.
	    echo $filename >> canrad_${stng}.history_temp

	done

	## Update the history list for this station.
	cat canrad_${stng}.history_temp >> $HIST/radar_canrad_${stng}.history
    fi

done

#############################################################
# Gather up all of the BUFR output files that were generated by this task.
#############################################################

bufrfiles=`ls ${DBNROOT}/tmp/*`
if [ -n $bufrfiles ]
then
    cat $bufrfiles > bufrout.task${ntask}
fi

msg="Canadian radar data processing has completed NORMALLY in task $ntask !"
postmsg "$jlogfile" "$msg"

exit 0
