#!/bin/ksh # 20100808 # added several exist codes & traps to # coad.k, # sfit2.k # fastc.k, # sfit2 v395lp # fitbn (fitbntab) # fitn.k # specasc6 (ratio, baselinecorrect, interpolation) # 20100603 Changed zpt to not read water out of zptw file to use WACCM mean instead # to see if we can make the MLO retrievals converge! RB # 200711 7 311 16:39:54UT : # - added radius of earth to output of ckopus # - so read in and pass to fitbn # 15 August 2005 # - added filter to region line in rlist and now loop out if this is not the # right filter - similar to the region stuff # had to move extraction of filter # from filename from fitn.k to here & # now we pass that to fitn.k # 10 March 2005 # - big re-edit diffed with current scripts on yampa and pave # 12 Feb 2003 - jwh # 12 March 2003 - jwh - hacked away for OSX in Thule # script to set up an sfit run using multiple spectra with sifferent zenith angles # only for opus files print "" print -R " fitbn : fitbatch script for multiple scans (sza's)" # EDIT HERE--------------------- # open site file por p[articulars to retrieval run site, cinput ver, sfit ver, etc exec 7< 'this.site' read -u7 junk read -u7 ratiodir junk read -u7 slist junk read -u7 rlist junk read -u7 ratioflag junk read -u7 chemprofiles junk read -u7 basedir junk read -u7 thisdir junk read -u7 bin junk read -u7 ftmplt junk read -u7 Sver junk read -u7 Cver junk read -u7 obslat junk read -u7 obslon junk read -u7 obsalt junk read -u7 zptext junk read -u7 block junk read -u7 loc junk read -u7 watervers junk exec 7<&- print -R $ratiodir print -R $slist print -R $rlist print -R $ratioflag print -R $chemprofiles print -R $basedir print -R $thisdir print -R $bin print -R $Sver print -R $ftmplt print -R $Cver print -R $obslat print -R $obslon print -R $obsalt print -R $zptext print -R $block print -R $loc print -R $watervers # END EDIT HERE--------------------- # Check that location works for ckopus n=0 for locvar in TAB SGP MLO FL0 MSA PKF KPK DEF do if [[ ${loc} = ${locvar} ]] then n=1 fi done if (( n == 0 )) then print "fitbn : No Location match : " ${loc} exit 1 fi umask 111 pause=0 if [[ $# == 2 ]] then slist=$1 pause=$2 else pause=$1 fi # check that input slist file exists if [[ ! -a ${slist} ]] then print 'fitbn : slist file does not exist : ' ${slist} exit 1 fi # check that input rlist file exists if [[ ! -a ${rlist} ]] then print 'fitbn : rlist file does not exist : ' ${rlist} exit 1 fi err="${thisdir}/Rslt" # open "slist" file & get spectra type flag from first line exec 9< $slist read -u9 stype junk print " Processing $stype spectra." # open "rlist" file & get output directory from first line odflag="SDIREC" odirec='' exec 7< $rlist read -u7 odirec junk exec 7<&- if [[ ${odirec} != "SDIREC" ]] then print " Storing output spectra in $odirec" odflag="ODIREC" fi # set UTC shift to zero it is not used with archive data let sUTC=0 # loop through file $slist of directories and spectra integer n=1 while read -u9 flag line do case $flag in "END" ) print "END record in <$slist> file reached ...exiting" exit 0 ;; "CLEAN" ) print "CLEAN record in <$slist> file reached ...exiting" make clean exit 0 ;; "SKIP" ) print -R " Processing input line # $n : ${flag}" print "SKIP record in <$slist> file reached ...continue" let n=$n+1 continue ;; "BAD" ) print -R " Processing input line # $n : ${flag}" print "BAD record in <$slist> file reached ...continue" let n=$n+1 continue ;; "#" ) print -R " Processing input line # $n : ${flag}" print ${line} let n=$n+1 continue ;; "" ) print -R " Processing input line # $n : ${flag}" print ${line} let n=$n+1 continue ;; "SKIPLINE" ) let skipline=${line}+$n #print $skipline $line $n print -R " Skipping input lines : $skipline" while (( ${n} <= ${skipline} )) do read -u9 flag line let n=$n+1 done #print $line #exit continue ;; esac # end $flag case set ${line} sdirec=$1 if [ $odflag = "SDIREC" ] then odirec=$1 fi fastcode_sw=1 print " " date print -R " Processing input line # $n : ${flag}" print -R "" >>$err print -R "Processing input line # $n : ${flag}" >>$err case ${flag} in "FIT" ) # Normal segment for single OPUS spectra preprocessing block=$2 sfname=$3 sUTC=$4 let nscan=1 print -R " Processing spectrum: $sdirec$sfname # $n, Type: ${flag} " print -R "" >>$err print -R "Processing spectrum: $sdirec$sfname # $n, Type: ${flag}" >>$err # Convert the OPUS data file to a bnr3 and get the zenith # angle, start and ending wavenumbers from the file. print -R " fitbn : Running ckopus..." print ${bin}ckopus -S${loc} -t${sUTC} -F${block} ${sdirec}${sfname} o2b_out=$(${bin}ckopus -S${loc} -t${sUTC} -F${block} ${sdirec}${sfname} 2>>ckopus.err) bnrOK=$? outfile="ckopus.out" # We should end with a single bnr if (( $bnrOK == 0 )) then mv ${block}.bnr temp.bnr.00 else print " ckopus error" exit 1 fi finfile=${sfname} ;; "COAD" ) # This is the case of nscan=1 but call it COAD in slist and process it here # This segment does several things: # all the files to be coaded are in the same directory # they are given on the same line in slist # first we read in the line and parse it to create coad.inp # call coad.k which reads the input and runs ckopus for # each OPUS file # saves each bnr then calls coad which coads the bnrs into a # new bnr # called temp.bnr (like ckopus) and saves the output in coad.out # which has the same format as ckopus.out # so we skip over the normal segment which would call # ckopus for a single OPUS file let nscan=1 # shuffle around already read in variables ncoad=$2 block=$3 sfname='' # ncoad should be 2, 3, ... print -R " Number of spectra in coad: $ncoad, Type: ${flag}" print -R "Number of spectra in coad: $ncoad, Type: ${flag}" >>$err let icoad=0 exec 8>coad.inp print -u8 -R ${ncoad} print -u8 -R ${block} #Latitude of observation print -u8 -R ${loc} #Longitude of observation while (( ${icoad} < ${ncoad} )) do shift shift if (( $icoad == 0 )) then sfname=$2 typeset -R5 suf1=$2 else typeset -R5 truncname=$2 typeset -R1 suf2=$2 sfname=${sfname},${truncname} fi print -u8 ${sdirec}$2 $3 print " " ${sdirec}$2 $3 print " " ${sdirec}$2 >>$err let icoad=${icoad}+1 done exec 8>&- print " fitbn : Running coad.k..." o2b_out=$(${bin}coad.k) bnrOK=$? outfile="coad.out" #print -R ' result of call to coad : ' $bnrOK # We should end with a single bnr if (( $bnrOK == 0 )) then mv ${block}.bnr temp.bnr.00 else print " !!! fitbn : coad error." exit 1 fi finfile=${sfname} ;; "NSCAN" ) # Here we handle coad or single single spectra to end with more than 1 bnr & sza # need to check / not updated -- jwh 2010 # claim nscan from $line nscan=$2 sfname='' icnt=0 bnrOK=1 # nscan should be 1, 2, 3, ... print -R " Number of scans to process:, $nscan, Type: ${flag}" print -R "Number of scans to process: $nscan, Type: ${flag}" >>$err shift shift #print $1 $2 $3 #exit outfile="coad.rslt" rm -f ${outfile} # loop over nscan, save sza let iscan=0 typeset -Z2 isc=$iscan while (( ${iscan} < ${nscan} )) do isc=$iscan print -R " Processing scan(s) number :" $iscan $isc # find number of scans to coad ncoad=$1 if (( ${ncoad} == 1 )) then print " Processing single spectrum" if (( ${icnt} == 0 )) then sfname=$2 icnt=1 else typeset -R5 truncname=$2 sfname=${sfname},${truncname} fi print -R " Running ckopus..." o2b_out=$(${bin}ckopus -S${loc} -t${3} -F${block} ${sdirec}${2} ) bnrOK=$? cat "ckopus.out" >>${outfile} mv ${block}.bnr temp.bnr.${isc} shift shift shift else # ncoad is greater than 1 print -R " Coading spectra to single scan :" $ncoad typeset -R5 truncname shift let icoad=0 exec 8>coad.inp print -u8 ${ncoad} print -u8 -R ${block} print -u8 -R ${loc} while (( ${icoad} < ${ncoad} )) do if (( ${icnt} == 0 )) then sfname=$1 icnt=1 else typeset -R5 truncname=$1 sfname=${sfname},${truncname} fi print -u8 ${sdirec}$1 $2 print " " ${sdirec}$1 $2 print " " ${sdirec}$1 >>$err let icoad=${icoad}+1 shift shift done exec 8>&- print " Running coad.k..." o2b_out=$(${bin}coad.k) bnrOK=$? cat "coad.out" >>${outfile} print 'isc : ' $isc mv ${block}.bnr temp.bnr.${isc} #cat $outfile fi let iscan=${iscan}+1 done # iscan ;; esac # end # Check that we created the bnr ok - return code from coad.k or ckopus if (( bnrOK != 0 )) then print -R " fitbn: Error with creating bnr, see ckopus.err" print -R " fitbn: Error with creating bnr, see ckopus.err">>$err print -R " fitbn: Skipping to next entry in $slist" print -R " fitbn: Skipping to next entry in $slist" >>$err continue fi # Finish OPUS preprocesser - common to both COAD and non-COAD set -A sza # make sza an array integer iscan=0 exec 8<$outfile while (( ${iscan} < ${nscan} )) do read -u8 sza[iscan] tsza=${sza[iscan]##*.} if (($tsza == -999)) then # Should not ever get here print -R " fitbn : Error !! with ckopus, see ckopus.err" $tsza print -R " fitbn : Error !! with ckopus, see ckopus.err" $tsza >>$err exit 1 fi read -u8 mpd read -u8 fov read -u8 apd read -u8 fwn read -u8 lwn read -u8 observ # for obs.dat : yyyy mm dd hh mm ss.s read -u8 stitle read -u8 azm # not scan array !!!20050629 180 6:19:37UT : read -u8 dur read -u8 radofearth #radofearth=06372.2700 let iscan=${iscan}+1 done exec 8<&- case $apd in "BX") apd=000 ;; "NBW") apd=001 ;; "NBM") apd=002 ;; "NBS") apd=003 ;; esac alt=${obsalt} # BEGIN CODE FOR ALL INPUT SPECTRA TYPES #print -R " Wavenumber range: $fwn to $lwn, zenith angle: ${sza##*00}" #print -R " Wavenumber range: $fwn to $lwn, zenith angle: $sza" >>$err #print -R " $stitle" #print -R " bnr3 comment line:" >>$err #print -R " $stitle" >>$err # Next get the reference profile for the day of the observation. We are # using 41 layers already defined in the other two fastcode input files # so the profile must also. This file should be made ahead of time and # stored in the spectra directory (first item in a lin in slist). zpt=${sdirec}zpt${zptext} if [ ! -a ${zpt} ] then print ' fitbn : Error zptw file does not exist : ' ${zptw} exit 1 fi print " fitbn : ZPT from zptfile : " ${zpt} # want a bit in here that will find a good water file # first determine the correct water profile to use #print 'watervers= ' $watervers if (( ${watervers} == 9 )) then #print 'water version requested= ' $watervers waterprof=${sdirec}w-120.v9 if [ ! -a ${waterprof} ] then print ' fitbn: No w-120 v9' waterprof=${sdirec}w-120.v8 if [ ! -a ${waterprof} ] then print ' fitbn: No w v8' waterprof=${sdirec}w-120.v7 if [ ! -a ${waterprof} ] then print ' fitbn: No w v7' waterprof=${sdirec}w-120.v6 if [ ! -a ${waterprof} ] then print ' fitbn: No w v6' waterprof=${sdirec}w-120.v5 if [ ! -a ${waterprof} ] then print ' fitbn: No w v5' waterprof=${sdirec}w-120.v4 if [ ! -a ${waterprof} ] then print ' fitbn: No w v4' waterprof=${sdirec}w-120.v3 if [ ! -a ${waterprof} ] then print ' fitbn: No w v3' waterprof=${sdirec}w-120.v2 if [ ! -a ${waterprof} ] then print ' fitbn: No w v2' waterprof=${sdirec}w-120.v1 if [ ! -a ${waterprof} ] then print ' fitbn: No w v1' waterprof=${sdirec}w-120.v0 if [ ! -a ${waterprof} ] then print ' fitbn: No w-120.v0' print ' fitbn : Water file does not exist : ' ${water} exit 1 fi fi fi fi fi fi fi fi fi fi else waterprof=${sdirec}w-120.v${watervers} if [ ! -a ${waterprof} ] then print ' fitbn: No w-120.' ${watervers} exit 1 fi fi print ' fitbn : water profile : ' ${waterprof} #print 'with this refmod:' ${chemprofiles} # now put the water into its own file (might want to think about outputting this?) # head -n 9 ${waterprof} > water #this fixes the blank line at the end: clugey... fix this cat ${zpt} ${waterprof} ${chemprofiles} >fasc41.prf # At this point we have a bnr3, zenith angle, altitude and reference profile # and along with the two fastcode input files here we can process # the spectrum with fastcode. # Now we loop on all entries in the region list file getting the binputs # and setting up the input file for fitone.k and calling it. exec 7< $rlist read -u7 junk while read -u7 region filter do print -R ' fitbn : Region/Filter : ' $region $filter print -R ' fitbn : Region/Filter : ' $region $filter >>$err # Get new spectrum if we've exhausted the region list if [ $region = END ] then print " fitbn : END record in file : ${rlist} reached cycle to next spectrum" break fi # get optical filter number for this spectrum thisfltr=${sfname:1:1} print ' fitbn : optical filter # : ' ${thisfltr} if [[ $thisfltr != [0123456789a] ]] then print " !!! Filter ${thisfltr} is out of range cycling..." continue fi # if we are sorting the fit by filter then check : $filter should be # the filter # # $filter is passed in with the region if [ -n ${filter} ] then if [ ${thisfltr} != ${filter} ] then print " fitbn : Spectrum filter ${thisfltr} not requested (${filter}) cycle to next region..." print " fitbn : Spectrum filter ${thisfltr} not requested (${filter}) cycle to next region..." >>$err continue fi fi # Check that the requested fitting region is within the limits of the # bnr3 file. They may not usually be if we are processing an entire # day's worth of data from all filters and we do not want to set up # multiple input files (rlist, slist). # using last values read for nscan > 1 linepos=${region##*\.} ((ln1=$linepos-2)) #print $ln1 ((ln2=$linepos+2)) print -R " fitbn : Required bandpass : " $ln1 "-" $ln2 if (($ln1<$fwn)) then print -R " fitbn : requested fit region :" $region " below spectrum min : " $fwn " cycle to next region." continue fi if (($ln2>$lwn)) then print -R " fitbn : requested fit region :" $region " above spectrum max : " $lwn " cycle to next region." continue fi # When we get here the region is within the range of the bnr. # See if in the region directory we have a specific binput for this # day if we do it has tha name b.'daydir' # change binput to cinput for sfit2 daydir=${sdirec%%/} daydir=${daydir##*/} if [ -a ${basedir}/${region}/c.${daydir} ] then cp -f -p ${basedir}/${region}/c.${daydir} ./cinput #print "found daydir binput" else cp -f -p ${basedir}${region}/cinput.${Cver} ./cinput if [[ $? -ne 0 ]] then print "!!! cp cinput not ok !!!" exit 5 fi fi # Put observation strings into sfit/obs.dat for F Hase solar3 routine cd ./sfit make clean cd .. print -R ${observ} >sfit/obs.dat print -R ${obslat} -${obslon} >>sfit/obs.dat print " fitbn : Running fitn.k... region: ${region} file : " ${sdirec}${sfname} print "" >>$err filename_line="Rgn:${region}, Spec:${sdirec}${sfname}" #rint -R " Fitting region: ${region} in file ${sdirec}${sfname}" >>$err set $observ #print $observ typeset -Z4 yyyy=$1 typeset -Z2 mm=$2 typeset -Z2 dd=$3 # typeset -Z2 hh=$4 # acd # typeset -Z2 nn=$5 # acd typeset -Z hh=$4 # mac typeset -Z nn=$5 # mac typeset -Z4 sss=$6 # Make directory in the spectrum directory for the fit region and store # the results by the spectrum filename. # Make a directory name with the time outdir=${hh}${nn}${sss} #%.*} #.${suf1},${suf2} #print "outdir : " $outdir # Create the input file for fitone.k exec 8>fitone.inp print -u8 -R $thisdir print -u8 -R "${sza[*]}" print -u8 -R $alt print -u8 -R $mpd print -u8 -R $fov print -u8 -R $apd print -u8 -R $fastcode_sw print -u8 -R $err print -u8 -R $linepos print -u8 -R $filename_line print -u8 -R $nscan print -u8 -R $Sver print -u8 -R $bin print -u8 -R $yyyy/$mm/$dd $hh:$nn:$sss # not used for Thule print -u8 -R ${obslat} ${obslon} # not used for Thule print -u8 -R ${ratioflag} print -u8 -R ${azm} print -u8 -R ${radofearth} print -u8 -R ${yyyy}${mm}${dd} print -u8 -R ${ftmplt} print -u8 -R ${ratiodir} print -u8 -R ${region} ${finfile} print -u8 -R ${thisfltr} print -u8 -R ${odirec}${region}/${outdir} # path to poutput for .l file print -u8 -R $dur exec 8>&- ${bin}fitn.k if [[ $? -ne 0 ]] then print "!!! fitn.k returned not ok !!!" print "... continue" exit 99 continue fi if [[ $pause == '1' ]] then read "des?Enter (k to not save, r to repeat once) ? " if [[ $des == 'r' ]] then fitn.k read "des?Enter (k to not save) ? " fi if [[ $des != 'k' ]] then #sav='no' if [[ ! -d ${odirec}${region}/${outdir} ]] then umask 000 if [[ ! -d ${odirec}${region} ]] then mkdir ${odirec}${region} fi mkdir ${odirec}${region}/${outdir} umask 111 fi print " Copying output files to ${odirec}${region}/${outdir}" cp -f -p sfit/cdetail ${odirec}${region}/${outdir} cp -f -p sfit/rdrv.ctl ${odirec}${region}/${outdir} cp -f -p sfit/sa.input ${odirec}${region}/${outdir} cp -f -p sfit/isotope.input ${odirec}${region}/${outdir} cp -f -p sfit/cinput ${odirec}${region}/${outdir} cp -f -p sfit/fasc.mix ${odirec}${region}/${outdir} cp -f -p sfit/fasc.ms ${odirec}${region}/${outdir} cp -f -p sfit/fasc.pt ${odirec}${region}/${outdir} cp -f -p sfit/fasc.prf ${odirec}${region}/${outdir} cp -f -p sfit/fasc.inp ${odirec}${region}/${outdir} cp -f -p sfit/statevec.mxf ${odirec}${region}/${outdir} cp -f -p sfit/statevec.prc ${odirec}${region}/${outdir} cp -f -p sfit/statevec ${odirec}${region}/${outdir} cp -f -p sfit/t15asc ${odirec}${region}/${outdir} #cp -f -p sfit/zsum.t ${odirec}${region}/${outdir} cp -f -p sfit/pbpfile ${odirec}${region}/${outdir} cp -f -p sfit/summary.st ${odirec}${region}/${outdir} #cp -f -p sfit/zsum ${odirec}${region}/${outdir} cp -f -p sfit/K.out ${odirec}${region}/${outdir} cp -f -p sfit/AK.out ${odirec}${region}/${outdir} cp -f -p sfit/ils.dat ${odirec}${region}/${outdir} cp -f -p sfit/obs.dat ${odirec}${region}/${outdir} cp -f -p sfit/PRFS.out ${odirec}${region}/${outdir} cp -f -p sfit/SM.out ${odirec}${region}/${outdir} cp -f -p sfit/SS.out ${odirec}${region}/${outdir} cp -f -p sfit/AEIGEN.out ${odirec}${region}/${outdir} cp -f -p sfit/Parm.out ${odirec}${region}/${outdir} cp -f -p sfit/temp.bnr.00 ${odirec}${region}/${outdir} #cp -f -p sfit/oex.ps ${odirec}${region}/${outdir} #cp -f -p sfit/*.1.1 ${odirec}${region}/${outdir} #rm -f sfit/oex.ps fi read "des?Enter (q to quit, f finish dataset) ? " if [[ $des == 'q' ]] then exit 0 elif [[ $des == 'f' ]] then pause=0 fi else if [[ ! -d ${odirec}${region}/${outdir} ]] then umask 000 if [[ ! -d ${odirec}${region} ]] then mkdir ${odirec}${region} fi mkdir ${odirec}${region}/${outdir} umask 111 fi print " Copying output files to ${odirec}${region}/${outdir}" cp -f -p sfit/cdetail ${odirec}${region}/${outdir} cp -f -p sfit/rdrv.ctl ${odirec}${region}/${outdir} cp -f -p sfit/sa.input ${odirec}${region}/${outdir} cp -f -p sfit/isotope.input ${odirec}${region}/${outdir} cp -f -p sfit/cinput ${odirec}${region}/${outdir} cp -f -p sfit/fasc.mix ${odirec}${region}/${outdir} cp -f -p sfit/fasc.ms ${odirec}${region}/${outdir} cp -f -p sfit/fasc.pt ${odirec}${region}/${outdir} cp -f -p sfit/fasc.prf ${odirec}${region}/${outdir} cp -f -p sfit/fasc.inp ${odirec}${region}/${outdir} cp -f -p sfit/statevec.mxf ${odirec}${region}/${outdir} cp -f -p sfit/statevec.prc ${odirec}${region}/${outdir} cp -f -p sfit/statevec ${odirec}${region}/${outdir} cp -f -p sfit/t15asc ${odirec}${region}/${outdir} #cp -f -p sfit/zsum.t ${odirec}${region}/${outdir} cp -f -p sfit/pbpfile ${odirec}${region}/${outdir} cp -f -p sfit/summary.st ${odirec}${region}/${outdir} #cp -f -p sfit/zsum ${odirec}${region}/${outdir} cp -f -p sfit/K.out ${odirec}${region}/${outdir} cp -f -p sfit/AK.out ${odirec}${region}/${outdir} cp -f -p sfit/ils.dat ${odirec}${region}/${outdir} cp -f -p sfit/obs.dat ${odirec}${region}/${outdir} cp -f -p sfit/PRFS.out ${odirec}${region}/${outdir} cp -f -p sfit/SM.out ${odirec}${region}/${outdir} cp -f -p sfit/SS.out ${odirec}${region}/${outdir} cp -f -p sfit/AEIGEN.out ${odirec}${region}/${outdir} cp -f -p sfit/Parm.out ${odirec}${region}/${outdir} cp -f -p sfit/temp.bnr.00 ${odirec}${region}/${outdir} #cp -f -p sfit/oex.ps ${odirec}${region}/${outdir} #cp -f -p sfit/*.1.1 ${odirec}${region}/${outdir} #rm -f sfit/oex.ps fi fastcode_sw=0 done # region list let n=$n+1 done # spectra list print "should never get here!" exit 0