#!/bin/csh

#message to remind user we are in batch mode
echo " "
echo "QTMC v1.0.5 batch mode operation"
echo " "

#location of qtmc bin directory
setenv QTMC_BIN  /home/jgeach/qtmc_kdevelop/qtmc_v1.0.6/bin

#location of calibration files (CL1_LEEA.cal, CL1_HEEA.cal, etc)
setenv CAL  /home/jgeach/CALIBRATION

#destination of output files (cef/qft and log files)
setenv DATA /home/jgeach/CORRECTED_MOMENTS/2002_025

#spacecraft number definitions
set SC1 = "1"
set SC2 = "2"
set SC3 = "3"
set SC4 = "4"

#moments sum to perform (e.g. L1L2T, BH1H2, etc)
#solver decides whether to use B or not based on some criteria (see documentation)
#this is communicated in the output diagnositic err_code
set SUML = "BL1L2T"
set SUMH = "BH1H2T"

#set the spacecraft potential in volts if overriding EFW U_probe_sc,
#or for when EFW data cannot be found (e.g. early mission)
set POTENTIAL = "7.56" #Volts

#don't look for EFW U_probe_sc and use the value POTENTIAL set above?
set OVERRIDE_EFW = "no"

#choose the output format of data either cef or qft
set CEF = "cef"
set CDF = "cdf"
set QFT = "qft"

#difference in volts between lower energy limit and s/c potential below
#which not to use the BOTTOM moments
set DELTA_PHI = "4.1" #Volts

#set guess loop parameters -- decrease for speed (shouldn't need to change)
set UPPER_GUESS = "200"
set LOWER_GUESS = "-200"
set GUESS_STEP = "1"

#########################################################################################
#########################################################################################
#
#	HERE FOLLOW SOME EXAMPLES TO RUN QTMC OVER LOOPS OF DATA, E.G. LOOP OVER
#	ALL 4 SPACECRAFT, OR LOOP OVER A WEEK'S WORTH OF DATA, ETC.
#
#########################################################################################
#########################################################################################


#========================================================================================
# IN THIS EXAMPLE QTMC IS LOOPED OVER THE FOUR SPACECRAFT BETWEEN THE INTERVAL START->END
#========================================================================================

#start and end times (note - can have YYYY:MM:DD or YYYY:DDD)
#note -- require end time to be greater than start+2.5mins else return failure (input error)
set START  = "2002-05-03Z00:00:00.000"
set END    = "2002-05-03Z23:59:00.000"

set i = 4

while( $i <= 4 ) #four spacecraft in total

#execute qtmc -- uncomment next line to activate
${QTMC_BIN}/QTMC_noQt -S${START} -E${END} -M${i} --history

#${QTMC_BIN}/QTMC -S${START} -E${END} -M${i}${SUMH} -C${CAL} -D${DATA} -V${POTENTIAL} -l${LOWER_GUESS} -u${UPPER_GUESS} -r${GUESS_STEP} -U${DELTA_PHI} -o${OVERRIDE_EFW} -O${CDF} --clobber

@ i++ #next spacecraft

end


#========================================================================================
# IN THIS EXAMPLE QTMC IS LOOPED OVER SPACECRAFT 1 OVER ONE WEEK STARTING 13 MAY 2002
# IN 24 HOUR CHUNKS (BL1L2T)
#========================================================================================

set year = "2002" #initialize time interval strings
set month
set day
set nextday
set time

set d   = 13
set nd
@ nd = $d + 1

set m = 5 #month

set end  	  #initialize data end
@ end = $d + 7    #set 'end' as start day + one week

while( $d <= $end ) #loop over days

if( $d < 10 || $nd < 10 ) then #makes sure the day string is padded with a '0' if < 10, ditto for month
  set day = "0$d"
  set nextday = "0$nd"
  set month = "0$m"
else
  set day = $d
  set nextday = $nd
  set month = $m
endif

set START = ${year}-${month}-${day}Z${time}
set END   = ${year}-${month}-${nextday}Z${time}

#execute qtmc -- uncomment next line to activate
#${QTMC_BIN}/QTMC -S${START} -E${END} -M${SC1}${SUML} -C${CAL} -D${DATA} -V${POTENTIAL} -l${LOWER_GUESS} -u${UPPER_GUESS} -r${GUESS_STEP} -U${DELTA_PHI} -o${OVERRIDE_EFW} -O${OUTPUT}

@ d++  #increment day
@ nd++ #increment next day

end

#========================================================================================
# SAME AS ABOVE, BUT WITH THE TIME FORMAT YYYY-DDD, ETC.
#========================================================================================

set year = "2002" #initialize time interval strings
set doy
set nexdoy
set time

set d   = 101     #set the start doy here
set nd
@ nd = $d + 1    
set end  	  #initialize data end
@ end = $d + 7    #set 'end' as start day + one week (could do similar for hours, mins, etc.)

while( $d <= $end ) #loop over days

if( $d < 10 || $nd < 10 ) then #makes sure the day string is padded with a '0' if < 100 etc.
  set doy = "00$d"
  set nextdoy = "00$nd"
else if( ($d > 10 && $d <100) || ($nd > 10 && $nd < 100) ) then
  set doy = "0$d"
  set nextdoy = "0$nd"
else
  set doy = $d
  set nextdoy = $nd
endif

set START = ${year}-${doy}Z${time}
set END   = ${year}-${nextdoy}Z${time}

#execute qtmc -- uncomment next line to activate
#${QTMC_BIN}/QTMC -S${START} -E${END} -M${SC1}${SUML} -C${CAL} -D${DATA} -V${POTENTIAL} -l${LOWER_GUESS} -u${UPPER_GUESS} -r${GUESS_STEP} -U${DELTA_PHI} -o${OVERRIDE_EFW} -O${OUTPUT}

@ d++  #increment day
@ nd++ #increment next day

end












