#!/usr/bin/ksh
#
# green_ascii2all_new.ksh 
#
# This script generates lines for a IDL batch
# file which creates gmag cdfs for Greenland data files. (New provider) 
#
# Arguments:
#	
#    Author: Patrick Cruce
#    Created: May 14,2010
#
#    Based heavily of green_ascii2all.ksh by Tim Quinn
#

#
## Set the environment variables
#
  if [[ -z $THMSOC ]]
  then
    THMSOC=/disks/socware/thmsoc_dp_current
    export THMSOC
  fi
  
  #. $THMSOC/src/config/soc_it_to_me.config
  . /home/thmsoc/astraea2_test/soc_it_to_me.config
  . $THMSOC/src/config/setup_themis.bourne
 # . $THMSOC/src/config/wget_green_gmag.config
  . /home/thmsoc/astraea2_test/wget_green_gmag_test.config

  processdate=$(date +%Y%m%d)
	
  LOGFILE=${LOGDIR}/webdownloads/green_ascii2all_new_${processdate}_$$
  IDLLOG=${LOGDIR}/webdownloads/green_ascii2all_new_idl_${processdate}_$$
  
#
##
## Begin Work
##
#
  echo ":$$:green_ascii2all_new: " >> $LOGFILE
  echo ":$$:green_ascii2all_new: Starting green_ascii2all_new at $(date)" >> $LOGFILE
  echo ":$$:green_ascii2all_new: " >> $LOGFILE

  os=$(uname -s)

#
## Find New Files

  #files=$(ls -1 $GMAGWORKDIR)
# temporarily modified to read files from production directory but not delete them
# after processing
  GMAGWORKDIR=/disks/themisdata/workdir/greenMag2Do
  files=$(ls -1 $GMAGWORKDIR/*.min) 2>>$LOGFILE
  for f in $files
  do
  	# workaround to avoid other changes because when filetype is given to ls it prints full path rather than just file name
		f=$(basename $f)
    fullfilepath=${GMAGWORKDIR}/${f}
    
    echo ":$$:green_ascii2all_new:" >> $LOGFILE
    echo ":$$:green_ascii2all_new: Processing $fullfilepath." >> $LOGFILE
    
    processTime=$(date -u '+%Y-%m-%d %T')
    
    #remove old junk
    rm -f ${TEMPDIR}/*.bm
    rm -f ${TEMPDIR}/*.txt
    rm -f ${TEMPDIR}/*.cdf
    #new script
    newscript=${TEMPDIR}/${f}.bm

    echo "!quiet=1" >> $newscript
    echo "thm_greenmag_l2gen_new,\"${fullfilepath}\",mastercdf_dir=\"${GREENMASTERDIR}/\",out_dir=\"${TEMPDIR}/\"" >> $newscript
    echo "exit" >> $newscript

    idl $newscript >> $IDLLOG 2>> $IDLLOG

# This check is redundant with following check for file creation
#    idlStatus=$?
#    if (( $idlStatus != 0 ))
#    then
#      echo ":$$:green_ascii2all_new: " >> $LOGFILE
#      echo ":$$:green_ascii2all_new: Failed CDF conversion for ${f}" >> $LOGFILE
#      echo ":$$:green_ascii2all_new: " >> $LOGFILE
#    fi
        
    outcdf=$(ls ${TEMPDIR}/thg*cdf) 2>/dev/null
    outtxt=$(ls ${TEMPDIR}/thg*txt) 2>/dev/null
    
    if [[ -z $outcdf || -z $outtxt ]]
    then
      echo ":$$:green_ascii2all_new:" >> $LOGFILE
      echo ":$$:green_ascii2all_new: ERROR: The IDL run failed to produce both the cdf and txt!!" >> $LOGFILE
      echo ":$$:green_ascii2all: ERROR file: ${f}" >> $LOGFILE
    else

    outname=$(echo $outcdf | awk -F/ '{print $NF}')
    month=$(echo $outname | awk -F_ '{print substr($5,5,2)}')
    day=$(echo $outname | awk -F_ '{print substr($5,7,2)}')
    year=$(echo $outname | awk -F_ '{print substr($5,1,4)}')
    site=$(echo $outname | awk -F_ '{print $4}')


      if [[ $os = "SunOS" ]]
      then
        ucsite=$(echo $site | nawk '{print toupper($1)}')
      else
        ucsite=$(echo $site | awk '{print toupper($1)}')
      fi

      cdf_archive_dir=${L2MAGDIR}/${site}/${year}
      ascii_archive_dir=${ASCIIMAGDIR}/${site}/${year}${month}

      mkdir -p $cdf_archive_dir
      mv $outcdf $cdf_archive_dir
      mkdir -p $ascii_archive_dir
      mv $outtxt $ascii_archive_dir
      
      ## Database update
			#
			cdffile=$(basename $outcdf)
			txtfile=$(basename $outtxt)
			dataTime=$(echo $year $month $day | awk '{printf("%s-%02s-%02s 00:00:00",$1,$2,$3)}')
			mysql_cdftable_name=gbo_${ucsite}_cdffiles
			mysql_asciitable_name=gbo_${ucsite}_asciifiles
			cdffileSize=$(ls -l ${cdf_archive_dir}/${cdffile} |awk '{print $5}')
			txtfileSize=$(ls -l ${ascii_archive_dir}/${txtfile} |awk '{print $5}')
			/home/thmsoc/astraea2_test/php/gmag_cdf_test.php $mysql_cdftable_name $dataTime $cdffile $processTime $cdffileSize
			/home/thmsoc/astraea2_test/php/gmag_ascii_test.php $mysql_asciitable_name $dataTime $txtfile $processTime $txtfileSize
      
      echo ":$$:green_ascii2all_new:" >> $LOGFILE
      echo ":$$:green_ascii2all_new: Processing of $outcdf & $outtxt completed." >> $LOGFILE
    fi
    #rm -f ${GMAGWORKDIR}/${f}
  done

## Cleanup
#
  exit 0
