# mkorbitskrtp.csh
# leel 2006_FEB_13 extract from merged files for each tour
# do both 1m tour for a year (krtpkso)
# do both 1s tour for a year (krtp) only
# args 1 = tourid 2 = tour starttime 3 = tour stoptime 4 = {leap}
# input /cassini3/data/merge{days}
#merge files needed 
@ leap = 0
if ($#argv >= 4) @ leap = 1
if ($#argv >= 4) echo set leap

echo "mkorbitskrtp.csh : tour : " $1 "starttime" $2 "stoptime" $3
set m = {"jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec"}
set tour = $1
set year1 = `echo $2 | cut -f1 -d ' '`
set yy1   = `echo $year1 | cut -c3,4`
set index = `echo $2 | cut -f2 -d ' '`
set mont1 = $m[$index]
set dom_1 = `echo $2 | cut -f3 -d ' '`
set hhmm1 = `echo $2 | cut -f4 -d ' '`

set year2 = `echo $3 | cut -f1 -d ' '`
set yy2   = `echo $year2 | cut -c3,4`
set index = `echo $3 | cut -f2 -d ' '`
set mont2 = $m[$index]
set dom_2 = `echo $3 | cut -f3 -d ' '`
set hhmm2 = `echo $3 | cut -f4 -d ' '`
#echo  $#date $year1 $mont1 $dom_1
#echo $yy1 $yy2
#if ($yy1 == $yy2 ) echo same year
#if ($yy1 != $yy2 ) echo not same year ooos
 set dkrtp = "/cassini3/data/krtp"
 set dkso  = "/cassini3/data/kso"
 set  dt   = "1m"
 set  dt   = "1s"
 set base  = "${dt}/sdfgm"
 set basef = "sdfgm_krtp"
 set dkrtpkso = "/cassini3/data/merge/$base"
 set dkrtp    = "/cassini3/data/krtp"
 set doy1 = `doy ${year1} ${mont1} ${dom_1} | tail -1 | cut -f4 -d ' '` 
 set doy  = `doy ${year1} ${mont1} ${dom_1} | tail -1 `
 set doy1 = `echo $doy | cut -f3 -d ' '`
 set doy  = `doy ${year2} ${mont2} ${dom_2} | tail -1 `
 set doy2 = `echo $doy | cut -f3 -d ' '`
 set nlines = 0
 set d1 = 0
 set d2 = 0
 set d1 = $doy1
 set d2 = $doy2
 if ($leap == 1) then
echo $#argv $argv[3]
    echo leap year
    set yearfile = "lyear"
    @ yeardiff = 366
 else
    echo normalyear
    set yearfile = "year"
    @ yeardiff = 365
 endif
 if ($d2 > $d1 ) then
   @ nlines = 1 + $d2 - $d1 
 else
   @ nlines = 1 + $yeardiff - $d1 
   @ d2 = $yeardiff
   if ($nlines == 0 ) @ nlines = 1
 endif
# echo nlines $nlines $d2
  set doy = `head -$d2 $yearfile `#| tail -$nlines year `
  set doy = `head -$d2 $yearfile | tail -$nlines `
  set list = tour${1}.list
  if (-f tour${1}.missing ) /bin/rm tour${1}.missing
  if (-f $list ) /bin/rm $list
  echo "#days " $#doy "use days " $doy
  foreach  DOY ($doy)
    set l = ${dkrtp}/y${yy1}/$base/${yy1}${DOY}
    ls ${l}*.ffh > /dev/null
    set okay = $status
    if ($okay == 1)  then
      echo ${l} >> tour${1}.missing
    endif
    if ($okay != 1)  then
      set afile = `ls ${l}*ffh| head -1`
#    echo afile $afile:r
     set item = "$afile:r"
     if (-f $list ) then 
        echo $item >> $list
     else
        echo $item  > $list
     endif
    endif
  end  
  if (-f $list ) then
    echo "has a list :" $list
    cat $list
  else
    echo "no list" $list
    exit
  endif

#create ffmerge input file
set input = mkorbitskrtp_input
set start = "$year1 $mont1 $dom_1 $hhmm1"
set stop  = "$year2 $mont2 $dom_2 $hhmm2"
echo starttime $start  stoptime $stop
set output = ${tour}_${dt}_$basef
if (-f $input) /bin/rm $input
echo $list > $input
echo "2 3 4 5 6 7 8" >> $input
echo "x" >> $input
echo "$start" >> $input
echo "$stop" >> $input
echo "$output" >> $input
echo "n" >> $input
echo "y" >> $input
#cat $input
#input to ffmerge
if (-f $output.ffh) /bin/rm $output.ffh
if (-f $output.ffd) /bin/rm $output.ffd
if (-f $SCRATCH/ffmerge.parm) /bin/rm $SCRATCH/ffmerge.parm
ffmergesuncmp < $input
/bin/mv $output.ff? /cassini3/data/merge/orbits/krtp/$base/
