#!/bin/csh
#Script to search for the best reference files usage in the database
# To call this script you should type
# ./search_best_reffile INSTRUMENT TYPE_FILE USEAFTERDATE DETECTOR
# where INSTRUMENT is the INSTRUMENT name for which the search is made
#       TYPE_FILE is the type of reference file we want to use in the search
#       DETECTOR is the detector name
#
# it changes the apropriate file name. At least for ACS files now
# ACS FILES:
# acr best biasfile BIAS IMAGE BIA
# acr best cfltfile CORONAGRAPHIC SPOT FLAT IMAGE CFL
# acr best darkfile DARK IMAGE DRK
# acr best dfltfile DELTA FLAT IMAGE DFL
# acr best dgeofile GEOMETRIC DELTA IMAGE (DISTORTION) DXY
# acr best flshfile POST FLASH IMAGE FLS
# acr best lfltfile LOW ORDER FLAT IMAGE LFL
# acr best pfltfile PIXEL TO PIXEL FLAT FIELD IMAGE PFL
# acr best shadfile SHUTTER SHADING IMAGE SHD
# acr best atodtab ANALOG-TO-DIGITAL TABLE A2D
# acr best bpixtab BAD PIXEL TABLE BPX
# acr best ccdtab CCD PARAMETERS TABLE CCD
# acr best comptab THE HST MASTER COMPONENT TABLE TMC
# acr best crrejtab COSMIC RAY REJECTION PARAMETER TABLE CRR
# acr best graphtab THE HST GRAPH TABLE TMG
# acr best idctab IMAGE DISTORTION COEFFICIENTS TABLE IDC
# acr best mdriztab MULTIDRIZZLE PARAMETER TABLE MDZ
# acr best mlintab MAMA LINEARITY TABLE LIN
# acr best oscntab CCD OVERSCAN REGION TABLE OSC
# acr best phottab PHOTOMETRY and THROUGHPUT TABLE PHT
# acr best spottab SPOT POSITION TABLE CSP
#
#For STIS
#ssr aperture Aperture name
#ssr binaxis1 axis1 data bin size in unbinned detector pixels
#ssr binaxis2 axis2 data bin size in unbinned detector pixels
#ssr ccdamp CCD Amplifier
#ssr ccdgain CCD commanded Gain
#ssr ccdoffst Commanded bias offset of CCD
#ssr cenwave Central wavelength in Angstroms
#ssr crsplit Number of CR split exposures
#ssr detector Detector
#ssr lampset spectral cal lamp current value (milliamps)
#ssr obstype Observation Type (Imaging or Spectroscopic)
#ssr opt elem Optical Element used for observation
#ssr texpstrt UT time of the start of exposure (MMM DD YYY hh:mm:ss)
#ssr wavecal wavecal image file name
#
#ssr best biasfile Bias image file BIA
#ssr best darkfile Dark image file DRK
#ssr best pfltfile Pixel-to-pixel flat file PFL
#ssr best dfltfile Delta flat image file DFL
#ssr best lfltfile Low-order flat image file LFL
#ssr best shadfile Shutter shading correction image file SSC
#ssr best sdstfile Small scale distortion image file SSD
#ssr best atodtab A2D Correction Table A2D
#ssr best apdstab Aperture Description Table APD
#ssr best apertab Aperture Throughput Table APT
#ssr best bpixtab Bad Pixel Table BPX
#ssr best ccdtab CCD Parameters Table CCD
#ssr best crrejtab Cosmic Ray Rejection Parameters Table CRR
#ssr best disptab Dispersion Coefficients Table DSP
#ssr best inangtab Incidence Angle Correction Table IAC
#ssr best idctab Image Distortion Correction Table IDC
#ssr best mlintab MAMA Linearity Table LIN
#ssr best lamptab Calibration Lamp Table LMP
#ssr best mofftab MAMA Offset Correction Table MOC
#ssr best pctab Photometric Correction Table PCT
#ssr best phottab Photometric Conversion Table PHOT
#ssr best sdctab 2-D Spectrum Distortion Correction SDC
#ssr best cdstab Cross-Disperser Scattering Table CDS
#ssr best echsctab Echelle Scattering Table ECH
#ssr best ecstab Echelle Cross-Dispersion Scattering Table EXS
#ssr best halotab Detectore Halo table HAL
#ssr best riptab Echelle Ripple Table RIP
#ssr best srwtab Scattering reference Wavelenght Table SRW
#ssr best psftab Telescope Point Spread Function Table TEL
#ssr best tdctab NUV Dark Correction Table TDC
#ssr best tdstab Time Dependent Sensitivity Table TDS
#ssr best wcptab Wavecal Parameters Table WCP
#ssr best sptrctab 1-D Spectrum Trace Table 1DT
#ssr best xtractab 1-D Extraction Parameters Table 1DX
#
#For WFPC2
#
#w2r obset id observation set id -
#w2r obsnumA observation number base 36
#w2r atodgain A-D Gain electrons
#w2r equinox equinox of celestial coord. system -
#w2r expstart Exposure start time Modified Julian Date
#w2r filter1 First filter Numberf -
#w2r filter2 Second filter Number -
#w2r filtnam1 First filter Name -
#w2r filtnam2 Secondfilter Name -
#w2r mode instrument mode FULL (full res.), AREA (area int.)
#w2r orientat 1,2,3,4 Orientation of the image 1, 3, or 4 posangle
#w2r serials serial clocks ON, OFF
#w2r shutter Shutter in place at beginning of the exposure -
#w2r atodcorr A-D correction applied PERFORM, OMIT,COMPLETE
#w2r biascorr Bias correction applied PERFORM, OMIT,COMPLETE
#w2r blevcorr Bias level correction applied PERFORM, OMIT,COMPLETE
#w2r darkcorr Dark correction applied PERFORM, OMIT,COMPLETE
#w2r dophotom Fill Photometry keywords PERFORM, OMIT,COMPLETE
#w2r flatcorr Flat correction applied PERFORM, OMIT,COMPLETE
#w2r maskcorr Mask correction applied PERFORM, OMIT,COMPLETE
#w2r shadcorr Shaded Shutter correction applied PERFORM, OMIT,COMPLETE
#
#w2r best atodfile Analog to Digital Converter Lookup Table R1d
#w2r best biasfile Bias Frame R2d
#w2r best darkfile Dark Frame R3d
#w2r best flatfile Flat Field File R4d
#w2r best maskfile Static Mask File R0d
#w2r best shadfile Shutter Shading Correction R5d
#w2r best comptab Master Component Table TMC.FITS
#w2r best graphtab The Master Graph Table (Synphot) TMG.FITS
#w2r best idctab Image Distortion Coefficients File IDC.FITS
#w2r best offtab not used -

set INSTRUMENT=$1
set FILETYPE=$2

set months = ( Jan JAN jan Feb FEB feb MAR Mar mar APR Apr apr MAY May may Jun JUN jun JUL jul Jul Aug aug AUG SEP Sep sep OCT Oct oct NOV Nov nov Dec DEC dec)

set found = 0
foreach mm ($months)
echo "$3--and--$mm"
  if ($3 == $mm) then
     set found = 1
   endif
   if ($found == 1) then
       break
   endif
end

if ($found == 1) then
   set month=$3
else 
   echo "The command has an incorrect order of parameters. THe correct order is INSTRUMENT FILE_TYPE Month Day Year Hour DETECTOR."
     exit  
endif 
 
set day=$4
set year=$5
set hour=$6
set DETECTOR=$7

if ($INSTRUMENT == "") then
   echo "Give the instrument name for the search (for now only ACS, WFPC2, and STIS) USE CAPS:"
   set INSTRUMENT = $<
   echo $INSTRUMENT
  echo " you gave instrument $INSTRUMENT"
endif

if ($DETECTOR == "") then
   echo "Name of the detector (upercase). In no detector press enter ?:"
   set DETECTOR = $<
  echo " you gave instrument $DETECTOR"
endif

if ($FILETYPE == "") then
   echo "Give the type of reference files for which you want to make the search (use CDBS abreviation):"
   set FILETYPE = $<
  echo " you gave instrument $FILETYPE"
endif

echo " Te selection was : $INSTRUMENT, $DETECTOR, $FILETYPE"
if($INSTRUMENT == "ACS" || $INSTRUMENT == "Acs" || $INSTRUMENT == "acs") then
   if(($FILETYPE == "DRK") || ($FILETYPE == "drk")) then
       set FILETYPE = "darkfile"
   else if(($FILETYPE == "BIA") || ($FILETYPE == "bia")) then
       set FILETYPE = "biasfile"
   else if(($FILETYPE == "LFL") || ($FILETYPE == "lfl")) then
       set FILETYPE = "lfltfile"
   else if(($FILETYPE == "LFL") || ($FILETYPE == "lfl")) then
       set FILETYPE = "lfltfile"
   else if(($FILETYPE == "CFL") || ($FILETYPE == "cfl")) then
      set FILETYPE = "cfltfile" 
   else if(($FILETYPE == "DFL") || ($FILETYPE == "dfl")) then
      set FILETYPE = "dfltfile" 
   else if(($FILETYPE == "DXY") || ($FILETYPE == "dxy")) then
      set FILETYPE = "dgeofile" 
   else if(($FILETYPE == "FLS") || ($FILETYPE == "fsl")) then
      set FILETYPE = "flshfile" 
   else if(($FILETYPE == "LFL") || ($FILETYPE == "lfl")) then
      set FILETYPE = "lfltfile" 
   else if(($FILETYPE == "PFL") || ($FILETYPE == "pfl")) then
      set FILETYPE = "pfltfile" 
   else if(($FILETYPE == "SHD") || ($FILETYPE == "shd")) then
      set FILETYPE = "shadfile" 
   else if(($FILETYPE == "A2D") || ($FILETYPE == "a2d")) then
      set FILETYPE = "atod"
   else if(($FILETYPE == "BPX") || ($FILETYPE == "bpx")) then
      set FILETYPE = "bpix"
   else if(($FILETYPE == "CCD") || ($FILETYPE == "ccd")) then
      set FILETYPE = "ccd"
   else if(($FILETYPE == "TMC") || ($FILETYPE == "tmc")) then
      set FILETYPE = "comp"
   else if(($FILETYPE == "CRR") || ($FILETYPE == "crr")) then
      set FILETYPE = "crrej"
   else if(($FILETYPE == "TMG") || ($FILETYPE == "tmg")) then
      set FILETYPE = "graph"
   else if(($FILETYPE == "IDC") || ($FILETYPE == "idc")) then
      set FILETYPE = "idc"
   else if(($FILETYPE == "mdz") || ($FILETYPE == "MDZ")) then
      set FILETYPE = "mdriz"
   else if(($FILETYPE == "LIN") || ($FILETYPE == "lin")) then
      set FILETYPE = "mlin"
   else if(($FILETYPE == "OSC") || ($FILETYPE == "osc")) then
      set FILETYPE = "oscn"
   else if(($FILETYPE == "PHT") || ($FILETYPE == "pht")) then
      set FILETYPE = "phot"
   else if(($FILETYPE == "CSP") || ($FILETYPE == "csp")) then
      set FILETYPE = "spot"
   endif
endif

#STIS
if($INSTRUMENT == "STIS" || $INSTRUMENT == "stis" || $INSTRUMENT == "Stis") then
    if(($FILETYPE == "BIA")  || ($FILETYPE == "bia")) then
     set FILETYPE = "biasfile"
    else if(($FILETYPE == "DRK")  || ($FILETYPE == "drk")) then
     set FILETYPE = "darkfile"
    else if(($FILETYPE == "PFL")  || ($FILETYPE == "pfl")) then
     set FILETYPE = "pfltfile"
    else if(($FILETYPE == "DFL")  || ($FILETYPE == "dfl")) then
     set FILETYPE = "dflt
    else if(($FILETYPE == "LFL")  || ($FILETYPE == "lfl")) then
     set FILETYPE = "lfltfile"
    else if(($FILETYPE == "SSC")  || ($FILETYPE == "ssc")) then
     set FILETYPE = "shadfile"
    else if(($FILETYPE == "SSD")  || ($FILETYPE == "ssd")) then
     set FILETYPE = "sdstfile"
    else if(($FILETYPE == "A2D")  || ($FILETYPE == "a2d")) then
     set FILETYPE = "atodtab"
    else if(($FILETYPE == "APD")  || ($FILETYPE == "apd")) then
     set FILETYPE = "apdstab"
    else if(($FILETYPE == "APT")  || ($FILETYPE == "apt")) then
     set FILETYPE = "apertab"
    else if(($FILETYPE == "BPX")  || ($FILETYPE == "bpx")) then
     set FILETYPE = "bpixtab"
    else if(($FILETYPE == "CCD")  || ($FILETYPE == "ccd")) then
     set FILETYPE = "ccdtab"
    else if(($FILETYPE == "CRR")  || ($FILETYPE == "crr")) then
     set FILETYPE = "crrejtab"
    else if(($FILETYPE == "DSP")  || ($FILETYPE == "dsp")) then
     set FILETYPE = "disptab"
    else if(($FILETYPE == "IAC")  || ($FILETYPE == "iac")) then
     set FILETYPE = "inangtab"
    else if(($FILETYPE == "IDC")  || ($FILETYPE == "idc")) then
     set FILETYPE = "idctab"
    else if(($FILETYPE == "LIN")  || ($FILETYPE == "lin")) then
     set FILETYPE = "mlintab"
    else if(($FILETYPE == "LMP")  || ($FILETYPE == "lmp")) then
     set FILETYPE = "lamptab"
    else if(($FILETYPE == "MOC")  || ($FILETYPE == "moc")) then
     set FILETYPE = "mofftab"
    else if(($FILETYPE == "PCT")  || ($FILETYPE == "pct")) then
     set FILETYPE = "pctab"
    else if(($FILETYPE == "PHOT")  || ($FILETYPE == "phot")) then   
     set FILETYPE = "phottab"
    else if(($FILETYPE == "SDC")  || ($FILETYPE == "sdc")) then
     set FILETYPE = "sdctab"
    else if(($FILETYPE == "CDS")  || ($FILETYPE == "cds")) then
     set FILETYPE = "cdstab"
    else if(($FILETYPE == "ECH")  || ($FILETYPE == "ech")) then
     set FILETYPE = "echsctab"
    else if(($FILETYPE == "EXS")  || ($FILETYPE == "exs")) then
     set FILETYPE = "ecstab"
   else if(($FILETYPE == "HAL")  || ($FILETYPE == "hal")) then
     set FILETYPE = "halotab"
    else if(($FILETYPE == "RIP")  || ($FILETYPE == "rip")) then
     set FILETYPE = "riptab"
    else if(($FILETYPE == "SRW")  || ($FILETYPE == "srw")) then
     set FILETYPE = "srwtab"
    else if(($FILETYPE == "TEL")  || ($FILETYPE == "tel")) then
     set FILETYPE = "psftab"
    else if(($FILETYPE == "TDC")  || ($FILETYPE == "tdc")) then
     set FILETYPE = "tdctab"
    else if(($FILETYPE == "TDS")  || ($FILETYPE == "tds")) then
     set FILETYPE = "tdstab"
    else if(($FILETYPE == "WCP")  || ($FILETYPE == "wcp")) then
     set FILETYPE = "wcptab"
    else if(($FILETYPE == "1DT")  || ($FILETYPE == "1dt")) then
     set FILETYPE = "sptrctab"
    else if(($FILETYPE == "1DX")  || ($FILETYPE == "1dX")) then
     set FILETYPE = "xtractab"
    endif
endif

# NICMOS
set found = 0
if($INSTRUMENT == "NICMOS" || $INSTRUMENT == "nicmos" || $INSTRUMENT == "") then
    if(($FILETYPE == "FLT") || ($FILETYPE == "flt")) then
      set FILETYPE = "flatfile"
    else if(($FILETYPE == "DRK") || ($FILETYPE == "drk")) then
     set FILETYPE = "darkfile"
    else if(($FILETYPE == "ILM")  || ($FILETYPE == "ilm")) then
     set FILETYPE = "illmfile"
     set found = 1
    else if(($FILETYPE == "LIN")  || ($FILETYPE == "lin")) then
     set FILETYPE = "nlinfile"
     set found = 1
    else if(($FILETYPE == "NOI")  || ($FILETYPE == "noi")) then
     set FILETYPE = "noisfile"
     set found = 1
    else if(($FILETYPE == "SCN")  || ($FILETYPE == "SCN")) then
     set FILETYPE = "saacntab"
     set found = 1
    else if(($FILETYPE == "TDD")  || ($FILETYPE == "tdd")) then
     set FILETYPE = "tempfile"
     set found = 1
    else if(($FILETYPE == "PHT")  || ($FILETYPE == "pht")) then
     set FILETYPE = "phottab"
     set found = 1
    else if(($FILETYPE == "IDC")  || ($FILETYPE == "idc")) then
     set FILETYPE = "idctab"
     set found = 1
    else if(($FILETYPE == "PSB")  || ($FILETYPE == "psb")) then
     set FILETYPE = "pedsbtab"
     set found = 1
    else if ($found == 0) then
     echo 'filetype not set; choosing DRK by default'
     set FILETYPE = "darkfile"  
    endif
#WFPC2
set found = 0
if($INSTRUMENT == "WFPC2" || $INSTRUMENT == "wfpc2" || $INSTRUMENT == "Wfpc2") then
    if(($FILETYPE == "BIA") || ($FILETYPE == "bia")) then
      set FILETYPE = "biasfile"
    else if(($FILETYPE == "DRK") || ($FILETYPE == "drk")) then
     set FILETYPE = "darkfile"
    else if(($FILETYPE == "R1D")  || ($FILETYPE == "r1d")) then
     set FILETYPE = "atodfile"
     set found = 1
    else if(($FILETYPE == "R2D")  || ($FILETYPE == "r2d")) then
     set FILETYPE = "biasfile"
     set found = 1
    else if(($FILETYPE == "R3D")  || ($FILETYPE == "r3d") || ($FILETYPE == "drk") || ($FILETYPE == "DRK") || ($FILETYPE == 'dark') || (SFILETYPE == 'DARK')) then
     set FILETYPE = "darkfile"
     set found = 1
    else if(($FILETYPE == "R4D")  || ($FILETYPE == "r4d")) then
     set FILETYPE = "flatfile"
     set found = 1
    else if(($FILETYPE == "R0D")  || ($FILETYPE == "r0d")) then
     set FILETYPE = "maskfile"
     set found = 1
    else if(($FILETYPE == "R5D")  || ($FILETYPE == "r5d")) then
     set FILETYPE = "shadfile"
     set found = 1
    else if(($FILETYPE == "IDC")  || ($FILETYPE == "idc")) then
     set FILETYPE = "idctab"
     set found = 1
    else if ($found == 0) then
     echo 'filetype not set; choosing DRK by default'
     set FILETYPE = "darkfile"  
    endif
endif

if ($month == "") then
   if($day == "") then
      if($year == "") then
         if($hour == "") then
              echo "Give the time when the exposure start (format Nov 22 2006 04:41:00):"
              set EXPSTART = $<
              echo $EXPSTART
              set count = 0
              foreach dd ($EXPSTART) 
                @ count ++
                if($count == 1) then
                    set month = $dd
                endif
                if($count == 2) then
                    set day = $dd
                endif
                if($count == 3) then
                    set year = $dd
                endif
                if($count == 4) then
                    set hour = $dd
                endif
              end
         endif
      endif
   endif
endif

echo " Year Mont day = $year $hour $month $day"
/bin/rm best_query.tmp
/bin/rm /usr/local/home/srefpipe/bin/best_query.sql
if(($INSTRUMENT == "ACS") || ($INSTRUMENT == "acs")) then
  /bin/cp -f  /usr/local/home/srefpipe/bin/best_query_acs.sql.bkp /usr/local/home/srefipipe/bin/best_query.sql
endif
if(($INSTRUMENT == "STIS") || ($INSTRUMENT == "stis")) then
  /bin/cp -f  /usr/local/home/srefpipe/bin/best_query_stis.sql.bkp /usr/local/home/srefpipe/bin/best_query.sql
endif
if(($INSTRUMENT == "WFPC2") || ($INSTRUMENT == "wfpc2")) then
  /bin/cp -f  /usr/local/home/srefpipe/bin/best_query_wfpc2.sql.bkp /usr/local/home/srefpipe/bin/best_query.sql
endif
echo "Looking for the best $FILETYPE reference files for instrument $INSTRUMENT"
set s1 = "use dadsops"
set FILELINES=`cat /usr/local/home/srefpipe/bin/best_query.sql`
#echo $FILELINES

set final = 0
#Check each line in the SQL file to change only that with the filename
foreach FILE1 ($FILELINES)
   set t1=`echo $FILE1 | grep best`
   if ($t1 != "") then
      if ($INSTRUMENT == "ACS" || $INSTRUMENT == "acs") then 
         set PRETYPE="acr"
         set PRETYPEINS="acs"
      endif
      if ($INSTRUMENT == "STIS" || $INSTRUMENT == "stis") then 
         set PRETYPE="ssr"
         set PRETYPEINS="stis"
      endif
      if ($INSTRUMENT == "WFPC2" || $INSTRUMENT == "wfpc2") then 
         set PRETYPE="w2r"
         set PRETYPEINS="wfpc2"
      endif
      set FILE1=`echo $PRETYPE\_best_$FILETYPE\,`
   endif

   if ($INSTRUMENT == "ACS" || $INSTRUMENT == "acs") then 
         set PRETYPE="acr"
      set t1=`echo $FILE1 | grep _ccdgain`
      if ($t1 != "") then
          set FILE1 = `echo $PRETYPE\_ccdgain,`
      endif
      set t1=`echo $FILE1 | grep _ccdamp`
      if ($t1 != "") then
          set FILE1 = `echo $PRETYPE\_ccdamp,`
      endif
      set t1=`echo $FILE1 | grep _detector`
      if ($t1 != "") then
          set FILE1 = `echo  $PRETYPE\_detector`
      endif
      set t1=`echo $FILE1 | grep from`
      if ($t1 != "") then
         if ($FILETYPE == "pflt") then
            set s1 = `echo "$s1,acr_filter1,acr_filter2"`
         endif
      endif
      set t1=`echo $FILE1 | grep acs_ref_data`
      if ($t1 != "") then
          set FILE1 = `echo  $PRETYPEINS\_ref_data`
      endif
      set t1=`echo $FILE1 | grep exp`
      if ($t1 != "") then
          if ($INSTRUMENT == "ACS" || $INSTRUMENT == "WFPC2") then
              set FILE1 = `echo  $PRETYPE\_expstart`
          else if ($INSTRUMENT == "STIS") then
              set FILE1 = `echo  $PRETYPE\_texpstrt`
          endif
      endif
      set t2=`echo $FILE1 | grep HRC`
      if ($t2 != "") then
        set FILE1 = "'$DETECTOR'"
      endif
   endif

   if ($INSTRUMENT == "WFPC2" || $INSTRUMENT == "wfpc2") then 
         set PRETYPE="w2r"
      set t1=`echo $FILE1 | grep _detector`
      if ($t1 != "") then
          set FILE1 = `echo `
      endif
      set t1=`echo $FILE1 | grep _ccdgain`
      if ($t1 != "") then
          set FILE1 = `echo $PRETYPE\_atodgain`
      endif
      set t1=`echo $FILE1 | grep acs_ref_data`
      if ($t1 != "") then
          set FILE1 = `echo  $PRETYPEINS\_ref_data`
      endif
      set t1=`echo $FILE1 | grep from`
      if ($t1 != "") then
         if ($FILETYPE == "r4d") then
            set s1 = `echo "$s1,$PRETYPE_filtnam1,$PRETYPE_filtnam2"`
         endif
      endif
      set t2=`echo $FILE1 | grep =`
      if ($t2 != "") then
        set FILE1 = `echo `
      endif
      set t2=`echo $FILE1 | grep and`
      if ($t2 != "") then
        set FILE1 = `echo `
      endif
      set t2=`echo $FILE1 | grep HRC`
      if ($t2 != "") then
        set FILE1 = `echo `
      endif
   endif

   if ($INSTRUMENT == "STIS" || $INSTRUMENT == "stis") then 
         set PRETYPE="ssr"
      set t1=`echo $FILE1 | grep _ccdamp`
      if ($t1 != "") then
          if(($DETECTOR == "CCD") || ($DETECTOR == "ccd")) then
             set FILE1 = `echo $PRETYPE\_ccdamp`
          else if(($DETECTOR == "FUV-MAMA) || ($DETECTOR == "fuv-mama") || ($DETECTOR == "fuvmama") || ($DETECTOR == "NUV-MAMA) || ($DETECTOR == "nuv-mama") || ($DETECTOR == "nuvmama")) then
             set FILE1 = `echo `
          endif
set FILE1 = `echo `
      endif
      set t1=`echo $FILE1 | grep _detector`
      if ($t1 != "") then
          set FILE1 = `echo `
      endif
      set t1=`echo $FILE1 | grep _ccdgain`
      if ($t1 != "") then
          if(($DETECTOR == "CCD") || ($DETECTOR == "ccd")) then
            set FILE1 = `echo $PRETYPE\_ccdgain`
          else if(($DETECTOR == "FUV-MAMA) || ($DETECTOR == "fuv-mama") || ($DETECTOR == "fuvmama") || ($DETECTOR == "NUV-MAMA) || ($DETECTOR == "nuv-mama") || ($DETECTOR == "nuvmama")) then
             set FILE1 = `echo `
          endif
      endif
      set t1=`echo $FILE1 | grep acs_ref_data`
      if ($t1 != "") then
          set FILE1 = `echo  $PRETYPEINS\_ref_data`
      endif
      set t1=`echo $FILE1 | grep from`
      if ($t1 != "") then
         if ($FILETYPE == "r4d") then
            set s1 = `echo "$s1,$PRETYPE_filtnam1,$PRETYPE_filtnam2"`
         endif
      endif
      set t2=`echo $FILE1 | grep =`
      if ($t2 != "") then
        set FILE1 = `echo `
      endif
      set t2=`echo $FILE1 | grep and`
      if ($t2 != "") then
        set FILE1 = `echo `
      endif
      set t2=`echo $FILE1 | grep HRC`
      if ($t2 != "") then
        set FILE1 = `echo `
      endif
   endif

   set t1=`echo $FILE1 | grep exp`
      if ($t1 != "") then
        if ($INSTRUMENT == "ACS" || $INSTRUMENT == "WFPC2") then
          set FILE1 = `echo  $PRETYPE\_expstart`
       else if ($INSTRUMENT == "STIS") then
           set FILE1 = `echo  $PRETYPE\_texpstrt`
      endif
   endif

   set s2 = ""
   if ($FILE1 == "go") then
     if(final == 0) then
          echo $s1  >> best_query.tmp
     else if($final == 1) then
#        echo "$s1"  >> best_query.tmp
        echo "$s1 >= '$month $day $year $hour' "  >> best_query.tmp
        set final = 2
     endif
     echo $FILE1 >> best_query.tmp
     set s1 = ""
   else if ($FILE1 != "go") then
       set s1 = `echo $s1 $FILE1`
       set t1=`echo $FILE1 | grep exp`
       if ($t1 != "") then
            set final = 1
        endif
   endif
end

/bin/rm best_query.sql
cp -f best_query.tmp best_query.sql
echo ""
echo "Searching the database"
echo ""
isql -e -i ./best_query.sql -S ZEPPO
