#------------------------------------------------------------------------------
# To compare bundle with OPS dirs to look for updated kernels
#------------------------------------------------------------------------------

set OPSDIR = /ftp/pub/naif/MAVEN/kernels
set BUNDIR = /ftp/pub/naif/pds/pds4/maven/maven_spice/spice_kernels

dir `ls -1 $OPSDIR/lsk/naif????.tls            | tail -1` \
    `ls -1 $BUNDIR/lsk/naif????.tls            | tail -1`

dir `ls -1 $OPSDIR/pck/pck?????.tpc            | tail -1` \
    `ls -1 $BUNDIR/pck/pck?????.tpc            | tail -1`

dir `ls -1 $OPSDIR/fk/maven_v??.tf             | tail -1` \
    `ls -1 $BUNDIR/fk/maven_v??.tf             | tail -1`

dir `ls -1 $OPSDIR/sclk/MVN_SCLKSCET.?????.tsc | tail -10` \
    `ls -1 $BUNDIR/sclk/mvn_sclkscet_?????.tsc | tail -10`

dir `ls -1 $OPSDIR/ik/maven_ant_v??.ti         | tail -1` \
    `ls -1 $BUNDIR/ik/maven_ant_v??.ti         | tail -1`

dir `ls -1 $OPSDIR/ik/maven_euv_v??.ti         | tail -1` \
    `ls -1 $BUNDIR/ik/maven_euv_v??.ti         | tail -1`

dir `ls -1 $OPSDIR/ik/maven_iuvs_v??.ti        | tail -1` \
    `ls -1 $BUNDIR/ik/maven_iuvs_v??.ti        | tail -1`

dir `ls -1 $OPSDIR/ik/maven_ngims_v??.ti       | tail -1` \
    `ls -1 $BUNDIR/ik/maven_ngims_v??.ti       | tail -1`

dir `ls -1 $OPSDIR/ik/maven_sep_v??.ti         | tail -1` \
    `ls -1 $BUNDIR/ik/maven_sep_v??.ti         | tail -1`

dir `ls -1 $OPSDIR/ik/maven_static_v??.ti      | tail -1` \
    `ls -1 $BUNDIR/ik/maven_static_v??.ti      | tail -1`

dir `ls -1 $OPSDIR/ik/maven_swea_v??.ti        | tail -1` \
    `ls -1 $BUNDIR/ik/maven_swea_v??.ti        | tail -1`

dir `ls -1 $OPSDIR/ik/maven_swia_v??.ti        | tail -1` \
    `ls -1 $BUNDIR/ik/maven_swia_v??.ti        | tail -1`


#------------------------------------------------------------------------------
# To check CKs for gaps
#------------------------------------------------------------------------------

cd /ftp/pub/naif/MAVEN/kernels/ck

set FK   = `ls -1 /ftp/pub/naif/MAVEN/kernels/fk/maven_v??.tf             | tail -1`
set LSK  = `ls -1 /ftp/pub/naif/MAVEN/kernels/lsk/naif????.tls            | tail -1`
set SCLK = `ls -1 /ftp/pub/naif/MAVEN/kernels/sclk/MVN_SCLKSCET.?????.tsc | tail -1`

frmdiff -k $LSK $SCLK $FK \
        [s/c ck list] \
        -t dumpg -f YYYY-MM-DDTHR:MN:SC ::RND -f1 J2000 -t1 MAVEN_SPACECRAFT

frmdiff -k $LSK $SCLK $FK \
        [app ck list] \
        -t dumpg -f YYYY-MM-DDTHR:MN:SC ::RND -f1 MAVEN_APP_BP -t1 MAVEN_APP_IG

frmdiff -k $LSK $SCLK $FK \
        [app ck list] \
        -t dumpg -f YYYY-MM-DDTHR:MN:SC ::RND -f1 MAVEN_APP_IG -t1 MAVEN_APP_OG


#------------------------------------------------------------------------------
# to make list entries for LSKs:
#------------------------------------------------------------------------------

foreach FF ( )
   echo "FILE             = spice_kernels/lsk/$FF"
   echo "MAKLABEL_OPTIONS = DEF_TIMES"
   if ( `echo $FF | grep -c '^naif[0-9][0-9][0-9][0-9].tls$'` != 0 ) then
      echo "DESCRIPTION      = SPICE LSK file incorporating leapseconds up to TBD, created by NAIF, JPL."
   endif
end

#------------------------------------------------------------------------------
# to make list entries for PCKs:
#------------------------------------------------------------------------------

foreach FF ( ) 
   echo "FILE             = spice_kernels/pck/$FF"
   echo "MAKLABEL_OPTIONS = DEF_TIMES"
   if ( `echo $FF | grep -c '^pck[0-9][0-9][0-9][0-9][0-9].tpc$'` != 0 ) then
      echo "DESCRIPTION      = SPICE text PCK file containing constants from the IAU 2009 report, created by NAIF, JPL."
   endif
end

#------------------------------------------------------------------------------
# to make list entries for main FK:
#------------------------------------------------------------------------------

foreach FF ( ) 
   echo "FILE             = spice_kernels/fk/$FF"
   echo "MAKLABEL_OPTIONS = DEF_TIMES"
   if ( `echo $FF | grep -c '^maven_v[0-9][0-9].tf$'` != 0 ) then
      echo "DESCRIPTION      = SPICE FK file defining reference frames the MAVEN spacecraft, its structures, and science instruments, created by NAIF, JPL."
   endif
end

#------------------------------------------------------------------------------
# to make list entries for IKs:
#------------------------------------------------------------------------------

foreach FF ( ) 
   echo "FILE             = spice_kernels/ik/$FF"
   echo "MAKLABEL_OPTIONS = DEF_TIMES"
   if ( `echo $FF | grep -c '^maven_ant_v[0-9][0-9].ti$'` != 0 ) then
      echo "DESCRIPTION      = SPICE IK file providing FOV definitions for the MAVEN communication antennae, created by NAIF, JPL."
   endif
   if ( `echo $FF | grep -c '^maven_euv_v[0-9][0-9].ti$'` != 0 ) then
      echo "DESCRIPTION      = SPICE IK file providing FOV definitions and other instrument parameters for the MAVEN Extreme Ultraviolet (EUV) monitor instrument, created by NAIF, JPL."
   endif
   if ( `echo $FF | grep -c '^maven_iuvs_v[0-9][0-9].ti$'` != 0 ) then
      echo "DESCRIPTION      = SPICE IK file providing FOV definitions and other instrument parameters for the MAVEN Imaging Ultraviolet Spectrograph (IUVS) instrument, created by IUVS Team, CU/LASP."
   endif
   if ( `echo $FF | grep -c '^maven_ngims_v[0-9][0-9].ti$'` != 0 ) then
      echo "DESCRIPTION      = SPICE IK file providing FOV definitions and other instrument parameters for the MAVEN Neutral Gas and Ion Mass Spectrometer (NGIMS) instrument, created by NGIMS Team, GSFC."
   endif
   if ( `echo $FF | grep -c '^maven_sep_v[0-9][0-9].ti$'` != 0 ) then
      echo "DESCRIPTION      = SPICE IK file providing FOV definitions and other instrument parameters for the Solar Energetic Particle (SEP) instrument, created by SEP Team, UC Berkeley."
   endif
   if ( `echo $FF | grep -c '^maven_static_v[0-9][0-9].ti$'` != 0 ) then
      echo "DESCRIPTION      = SPICE IK file providing FOV definitions and other instrument parameters for the SupraThermal And Thermal Ion Composition (STATIC) instrument, created by STATIC Team, UC Berkeley."
   endif
   if ( `echo $FF | grep -c '^maven_swea_v[0-9][0-9].ti$'` != 0 ) then
      echo "DESCRIPTION      = SPICE IK file providing FOV definitions and other instrument parameters for the MAVEN Solar Wind Electron Analyzer (SWEA) instrument, created by SWEA Team, UC Berkeley."
   endif
   if ( `echo $FF | grep -c '^maven_swia_v[0-9][0-9].ti$'` != 0 ) then
      echo "DESCRIPTION      = SPICE IK file providing FOV definitions and other instrument parameters for the MAVEN Solar Wind Ion Analyzer (SWIA) instrument, created by SWIA Team, UC Berkeley."
   endif
end

#------------------------------------------------------------------------------
# to make list entries for SCLKs:
#------------------------------------------------------------------------------

foreach FF ( ) 
   echo "FILE             = spice_kernels/sclk/$FF"
   echo "MAKLABEL_OPTIONS = DEF_TIMES"
   if ( `echo $FF | grep -c '^mvn_sclkscet_[0-9][0-9][0-9][0-9][0-9].tsc$'` != 0 ) then
      set VER = `echo $FF | sed 's/_/ /g' | sed 's/\./ /g' | awk '{print $3}'`
      echo "DESCRIPTION      = SPICE SCLK file containing time correlation data for the main MAVEN on-board clock, created by NAIF, JPL. The original name of this file was MVN_SCLKSCET.$VER.tsc."
   endif
end

#------------------------------------------------------------------------------
# to make list entries for SPKs:
#------------------------------------------------------------------------------

foreach FF ( ) 
   echo "FILE             = spice_kernels/spk/$FF"
   echo "MAKLABEL_OPTIONS = "
   if ( `echo $FF | grep -c '^de[0-9][0-9][0-9]s*.bsp$'` != 0 ) then
      set VER = `echo $FF | sed 's/s*\.bsp$//g' | tr a-z A-Z`
      echo "DESCRIPTION      = SPICE SPK file containing JPL planetary ephemerides version $VER, created by NAIF, JPL."
   endif
   if ( `echo $FF | grep -c '^mar[0-9][0-9][0-9]s*.bsp$'` != 0 ) then
      set VER = `echo $FF | sed 's/s*\.bsp$//g' | tr a-z A-Z`
      echo "DESCRIPTION      = SPICE SPK file containing JPL Martian satellite ephemerides version $VER, created by NAIF, JPL."
   endif
   if ( `echo $FF | grep -c '^maven_struct_v[0-9][0-9].bsp$'` != 0 ) then
      echo "DESCRIPTION      = SPICE SPK file containing relative locations of selected MAVEN structures and science instruments, created by NAIF, JPL."
   endif
   if ( `echo $FF | grep -c '^maven_cru_rec_[0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9]_v[0-9].bsp$'` != 0 ) then
      set ORIG = `echo $FF | sed 's/maven_cru_rec_//' | sed 's/_/-/' | sed 's/_v/_rec_v/' | sed 's/^/trj_c_/'`
      echo "DESCRIPTION      = SPICE SPK file containing reconstructed cruise trajectory of the MAVEN spacecraft, created by MAVEN NAV Team, JPL. The original name of this file was $ORIG."
   endif
   if ( `echo $FF | grep -c '^maven_orb_rec_[0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9]_v[0-9].bsp$'` != 0 ) then
      echo "DESCRIPTION      = SPICE SPK file containing reconstructed orbital trajectory of the MAVEN spacecraft, created by NAIF, JPL by merging operational weekly reconstructed SPK files produced by MAVEN NAV Team, JPL."
   endif
end

#------------------------------------------------------------------------------
# to make list entries for CKs:
#------------------------------------------------------------------------------

foreach FF ( ) 
   echo "FILE             = spice_kernels/ck/$FF"
   echo "MAKLABEL_OPTIONS = "
   if ( `echo $FF | grep -c '^mvn_swea_nom_[0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9]_v[0-9][0-9].bc$'` != 0 ) then
      echo "DESCRIPTION      = SPICE CK file containing nominal orientation of the MAVEN SWEA instrument boom, created by NAIF, JPL."
   endif
   if ( `echo $FF | grep -c '^mvn_app_rel_[0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9]_v[0-9][0-9].bc$'` != 0 ) then
      echo "DESCRIPTION      = SPICE CK file containing reconstructed orientation of the MAVEN Articulated Payload Platform (APP), created by NAIF, JPL."
   endif
   if ( `echo $FF | grep -c '^mvn_iuvs_rem_[0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9]_v[0-9][0-9].bc$'` != 0 ) then
      echo "DESCRIPTION      = SPICE CK file containing reconstructed orientation of the MAVEN IUVS instrument internal mirror, created by NAIF, JPL by merging data from daily IUVS CKs produced by the IUVS Team, CU/LASP."
   endif
   if ( `echo $FF | grep -c '^mvn_sc_rel_[0-9][0-9][0-9][0-9][0-9][0-9]_[0-9][0-9][0-9][0-9][0-9][0-9]_v[0-9][0-9].bc$'` != 0 ) then
      echo "DESCRIPTION      = SPICE CK file containing reconstructed orientation of the MAVEN spacecraft, created by NAIF, JPL."
   endif
end

#------------------------------------------------------------------------------
# to make list entries for MKs:
#------------------------------------------------------------------------------

foreach FF ( ) 
   echo "FILE             = spice_kernels/mk/$FF"
   echo "MAKLABEL_OPTIONS = "
   if ( `echo $FF | grep -c '^maven_[0-9][0-9][0-9][0-9]_v[0-9][0-9].tm$'` != 0 ) then
      set YEAR = `echo $FF | sed 's/_/ /g' | awk '{print $2}'`
      echo "DESCRIPTION      = SPICE MK file listing kernels for $YEAR, created by NAIF, JPL."
   endif
end


#------------------------------------------------------------------------------
# To check that the list has the same number of FILE, MAKLABEL_OPTIONS, 
# and DESCRIPTION entries
#------------------------------------------------------------------------------

set PDSDIR  = "/home/bsemenov/maven/pds"
set STAGING = "/usr1/maven/pds4/staging/maven/maven_spice" 
set FINAL   = "/usr1/maven/pds4/final/$STAGING:h:t/$STAGING:t"
set FTPDIR  = "/ftp/pub/naif/pds/pds4/$FINAL:h:t/$FINAL:t"
set OPSDIR  = "/ftp/pub/naif/MAVEN/kernels"

set LIST = $PDSDIR/maven_release_13.kernel_list

grep '^FILE'             $LIST | wc -l
grep '^MAKLABEL_OPTIONS' $LIST | wc -l
grep '^DESCRIPTION'      $LIST | wc -l

#------------------------------------------------------------------------------
# To check that list for duplicate files
#------------------------------------------------------------------------------

cd $PDSDIR

cat maven_release_??.kernel_list >! maven_complete.kernel_list

~/pds/scripts/Step3/check_list_for_dups.csh maven_complete.kernel_list


#------------------------------------------------------------------------------
# To generate list for meta kernel.
#------------------------------------------------------------------------------

cd $PDSDIR

grep '^FILE' $LIST | sed 's/FILE             = spice_kernels/                          '"'"'\$KERNELS/g' | sed 's/$/'"'"'/g'


#------------------------------------------------------------------------------
# To check that all files listed in the list are on the naif server
#------------------------------------------------------------------------------

cd $PDSDIR

foreach FF ( `grep 'FILE' $LIST | sed 's/=/ /g' | awk '{print $2}'` )
   echo -------------------------------------------------------------- $FF
   if ( $FF:e == "tm" ) then
      set ONNAIF = $PDSDIR/$FF
   else if ( `echo $FF | grep -c '^spice_kernels/sclk/mvn_sclkscet_.....\.tsc$'` == 1 ) then
      set ONNAIF = `echo $FF | sed 's/spice_kernels\/sclk\/mvn_sclkscet_/\/ftp\/pub\/naif\/MAVEN\/kernels\/sclk\/MVN_SCLKSCET./g'`
   else if ( `echo $FF | grep -c '^spice_kernels/ck/mvn_iuvs_rem_......_......_v..\.bc$'` == 1 ) then
      set ONNAIF = `echo $FF | sed 's/spice_kernels\/ck/\/home\/bsemenov\/maven\/iuvs/g'`
   else 
      set ONNAIF = `echo $FF | sed 's/spice_kernels/\/ftp\/pub\/naif\/MAVEN\/kernels/g'`
   endif
   dir $ONNAIF
end

#------------------------------------------------------------------------------
# To copy files to staging area
#------------------------------------------------------------------------------

echo $LIST
echo $STAGING

cd $PDSDIR

foreach FF ( `grep 'FILE' $LIST | sed 's/=/ /g' | awk '{print $2}'` )
   echo -------------------------------------------------------------- $FF
   if ( $FF:e == "tm" ) then
      set ONNAIF = $PDSDIR/$FF
   else if ( `echo $FF | grep -c '^spice_kernels/sclk/mvn_sclkscet_.....\.tsc$'` == 1 ) then
      set ONNAIF = `echo $FF | sed 's/spice_kernels\/sclk\/mvn_sclkscet_/\/ftp\/pub\/naif\/MAVEN\/kernels\/sclk\/MVN_SCLKSCET./g'`
   else if ( `echo $FF | grep -c '^spice_kernels/ck/mvn_iuvs_rem_......_......_v..\.bc$'` == 1 ) then
      set ONNAIF = `echo $FF | sed 's/spice_kernels\/ck/\/home\/bsemenov\/maven\/iuvs/g'`
   else 
      set ONNAIF = `echo $FF | sed 's/spice_kernels/\/ftp\/pub\/naif\/MAVEN\/kernels/g'`
   endif
   set INSTAGE = $STAGING/$FF
   if ( ! -e $INSTAGE ) then
      echo cp $ONNAIF $INSTAGE
      cp $ONNAIF $INSTAGE
      echo
   else
      echo ERROR:  $INSTAGE already exists
      echo
   endif
end

foreach FF ( `grep 'FILE' $LIST | sed 's/=/ /g' | awk '{print $2}'` )
   dir `find $STAGING -name $FF:t -print`
end

#------------------------------------------------------------------------------
# To check MK and data
#------------------------------------------------------------------------------

cd $STAGING

cd spice_kernels/mk 


set LATESTMKS = `ls -1r *tm | uniq -w 10`

dir -tr `echo $LATESTMKS`


brief $LATESTMKS -a -t

brief $LATESTMKS -a -t --202

spkdiff -k $LATESTMKS -c1 MARS -b1 MAVEN -r1 J2000 -b2 PHOBOS -n 100000 -t stats


ckbrief -a -t -rel -n $LATESTMKS


frmdiff -k $LATESTMKS -t dumpg -f1 J2000 -t1 MAVEN_SPACECRAFT -f YYYY-MM-DDTHR:MN:SC ::RND

frmdiff -k $LATESTMKS -t dumpg -f1 MAVEN_SPACECRAFT -t1 MAVEN_APP -c1 MAVEN_APP_OG -f YYYY-MM-DDTHR:MN:SC ::RND

#
# this ones is very slow due to too many intervals.
#
#frmdiff -b 2016-01-01 -k $LATESTMKS -t dumpg -f1 MAVEN_APP -t1 MAVEN_IUVS -c1 MAVEN_IUVS_SCAN -f YYYY-MM-DDTHR:MN:SC ::RND



frmdiff -k $LATESTMKS -n 100000 -f1 J2000 -t1 MAVEN_SPACECRAFT

frmdiff -k $LATESTMKS -n 100000 -f1 MAVEN_SPACECRAFT -t1 MAVEN_APP -c1 MAVEN_APP_OG

frmdiff -k $LATESTMKS -n 100000 -f1 MAVEN_APP -t1 MAVEN_IUVS -c1 MAVEN_IUVS_SCAN



#------------------------------------------------------------------------------
# OLD OLD OLD Make staging directories
#------------------------------------------------------------------------------

mkdir maven
cd maven/

mkdir maven_spice
cd maven_spice/

mkdir spice_kernels/ spice_kernels/sclk spice_kernels/lsk spice_kernels/spk spice_kernels/fk spice_kernels/ck spice_kernels/ik spice_kernels/pck spice_kernels/mk document




#------------------------------------------------------------------------------
# End.
#------------------------------------------------------------------------------
