#!/usr/bin/ksh # # intermagnet_ascii2all.ksh # # This script generates lines for a IDL batch # file which creates gmag cdfs for data files obtained through INTERMAGNET. # (Currently sites: ABK, FCC, YKC) # # Arguments: # # Author: Lydia Philpott # Created: November 18, 2011 # # Based heavily of green_ascii2all_new.ksh by Pat Cruce # # ## 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 . $THMSOC/src/config/setup_themis.bourne . $THMSOC/src/config/wget_intermagnet_gmag.config processdate=$(date +%Y%m%d) LOGFILE=${LOGDIR}/webdownloads/intermagnet_ascii2all_${processdate}_$$ IDLLOG=${LOGDIR}/webdownloads/intermagnet_ascii2all_idl_${processdate}_$$ # ## ## Begin Work ## # echo ":$$:intermagnet_ascii2all: " >> $LOGFILE echo ":$$:intermagnet_ascii2all: Starting intermagnet_ascii2all at $(date)" >> $LOGFILE echo ":$$:intermagnet_ascii2all: " >> $LOGFILE os=$(uname -s) # ## Find New Files #files=$(ls -1 $GMAGWORKDIR) #files=$(ls -1 $GMAGWORKDIR/*.min) 2>>$LOGFILE files=$(find $GMAGWORKDIR -maxdepth 1 -type f) 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) filename=$f fullfilepath=${GMAGWORKDIR}/${f} #echo "$filename $fullfilepath" echo ":$$:intermagnet_ascii2all:" >> $LOGFILE echo ":$$:intermagnet_ascii2all: Processing $fullfilepath." >> $LOGFILE processTime=$(date -u '+%Y-%m-%d %T') tmpsubdir=tmp_$filename processDir=${TEMPDIR}/${tmpsubdir} mkdir -p $processDir cd $processDir #copy file to temporary directory cp $fullfilepath . #check if it needs to be unzipped isgzip=$(echo $filename |grep .gz) if [[ -n $isgzip ]] then gunzip -f $filename filename=${filename%.gz} fi ## Copy CDF master to processing directory # site=$(echo $filename | awk '{print substr($1,1,3)}') mkdir -p mastercdf cp ${CDFMASTERS}/*${site}* ./mastercdf #new script newscript=$filename.bm echo "!quiet=1" >> $newscript echo "thm_intermagnetmag_l2gen,\"${filename}\",out_dir=\"${processDir}/\"" >> $newscript echo "exit" >> $newscript idl $newscript >> $IDLLOG 2>> $IDLLOG outcdf=$(ls ${processDir}/thg*cdf) 2>/dev/null outtxt=$(ls ${processDir}/thg*txt) 2>/dev/null if [[ -z $outcdf || -z $outtxt ]] then echo ":$$:intermagnet_ascii2all:" >> $LOGFILE echo ":$$:intermagnet_ascii2all: ERROR: The IDL run failed to produce both the cdf and txt!!" >> $LOGFILE echo ":$$:intermagnet_ascii2all: ERROR file: ${filename}" >> $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) ## Update checksum file # ${KSH_SCRIPTS}/fileChkSum.ksh ${cdf_archive_dir}/${cdffile} ## Update checksum file # ${KSH_SCRIPTS}/fileChkSum.ksh ${ascii_archive_dir}/${txtfile} 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}') gmag_cdf.php $mysql_cdftable_name $dataTime $cdffile $processTime $cdffileSize gmag_ascii.php $mysql_asciitable_name $dataTime $txtfile $processTime $txtfileSize echo ":$$:intermagnet_ascii2all:" >> $LOGFILE echo ":$$:intermagnet_ascii2all: Processing of $outcdf & $outtxt completed." >> $LOGFILE fi cd rm -rf $processDir rm -f ${GMAGWORKDIR}/${f} done ## Cleanup # exit 0