#!/bin/csh

# neo_observing.csh

#  This script finds currently observable NEOs.  This procedure will
#  be run as a cron job by koehn so file protections will not be
#  an issue. It will have to be run on the computer named tibro.

#  Establish a safe working directory

set base = `pwd`
cd $home >& /dev/null
set report_file = neo_observing.rpt
echo "         *** Observable NEO ephemerides generation ***" > $report_file
echo "" >> $report_file

#  Get the current UT date.  This is the default value.

set date_string = `date -u +%y%m%d`

#  Read the command line arguments
#  -d corresponds to a user-supplied date in yymmdd format
#  The user-supplied  date supercedes the date supplied by the 'date' command.

@ matcher = 2 * $#argv / 2
if ( $matcher != $#argv ) then
  echo "***ERROR***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
  >> $report_file
  echo "Command line arguments must occur in pairs (-flag value)." \
  >> $report_file
  goto report
endif
while ( $#argv != 0 )
  if ( "$argv[1]" == \-d ) then
    set date_string = $argv[2]
  else
    echo "***ERROR***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
  >> $report_file
    echo "$argv[1] is an unrecognized command line flag." \
  >> $report_file
    goto report
  endif
  shift
  shift
end

#  Verify the date is a number.

if { @ date_string = $date_string } then
else
  echo "***ERROR***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
  >> $report_file
  echo "The date string, $date_string, must be purely numeric." \
  >> $report_file
  goto report
endif

#  Verify the date has reasonable values

if ( $date_string < 0 ) then
  echo "***ERROR***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
  >> $report_file
  echo "The date string, $date_string, must not be negative." \
  >> $report_file
  goto report
endif
@ year = $date_string / 10000
if ( $year < 10 ) then
  set year = 0${year}
endif
@ month = $date_string - ( $year * 10000 )
@ month = $month / 100
if ( $month < 10 ) then
  set month = 0${month}
endif
@ day = $date_string - ( $year * 10000 )
@ day = $day - ( $month * 100 )
if ( $day < 10 ) then
  set day = 0${day}
endif
if ( $year > 99 ) then
  echo "***ERROR***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
  >> $report_file
  echo "The year, $year, must be less than 99." \
  >> $report_file
  goto report
endif
if ( $month < 1 || $month > 12 ) then
  echo "***ERROR***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
  >> $report_file
  echo "The month, $month, must lie between 1 and 12 inclusive." \
  >> $report_file
  goto report
endif
if ( $day < 1 || $day > 31 ) then
  echo "***ERROR***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
  >> $report_file
  echo "The day, $day, must lie between 1 and 31 inclusive." \
  >> $report_file
  goto report
endif

#  Put the date into yyyymmdd format

if ( $date_string < 960000 ) then 
  @ date_string = $date_string + 20000000
else
  @ date_string = $date_string + 19000000
endif
@ year = $date_string / 10000

#  Remove any the temporary files that may remain from previous invocations

rm -f slop.par >& /dev/null
rm -f slopsort.in >& /dev/null
rm -f slopsort.par >& /dev/null
rm -f slop.out >& /dev/null
rm -f slopsort.out >& /dev/null
rm -f sloppart?? >& /dev/null
rm -f slopnames >& /dev/null
rm -f ef8.par >& /dev/null
rm -f tmp.F* >& /dev/null
rm -f ephem_*.out >& /dev/null

#  Generate the user responses requred for 'slop'

echo "***STATUS***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
>> $report_file
echo "Generating NEO targets for $year $month ${day}." \
>> $report_file
echo  8  > slop.par     # Declination
echo  9 >> slop.par     # Solar elongation
echo 13 >> slop.par     # Magnitude
echo 15 >> slop.par     # Galactic latitude
echo 20 >> slop.par     # Perihelion distance
echo  0 >> slop.par     # and no others
echo y >> slop.par      # Suppress ephemeris error calculation
echo ${year},${month},${day}.3125 >> slop.par  # for the selected date
echo -25.0 >> slop.par  # declination > -25 deg
echo 90.0 >> slop.par   # declination < 90 deg
echo 60.0 >> slop.par   # solar elongation > 60 deg
echo 180.0 >> slop.par  # solar elongation < 180 deg
echo v >> slop.par      # V magnitude
echo 10.0 >> slop.par   # V > 10
echo 17.5 >> slop.par   # V < 18
echo 20.0 >> slop.par   # Galactic latitude > 20 deg
echo 90.0 >> slop.par   # Galactic Latitude < 90 deg
echo 0.0 >> slop.par    # Perihelion > 0 au
echo 1.3 >> slop.par    # Perihelion < 1.3 au
echo q >> slop.par      # all objects in database
echo 2.5 >> slop.par    # orbit quality 2.5
echo y >> slop.par      # Generate binary output file for slopsort
echo slopsort.in >> slop.par  # Put file in slop.par
echo o >> slop.par      # Generate file only

# Run slop to get a list of observable NEOs.  Slop produces lots of
# status information.  Put status info into the bit bucket.

if { /tibro/data/bin/slop < slop.par > /dev/null } then
else
  echo "***ERROR***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
  >> $report_file
  echo "Error while running /tibro/data/bin/slop." \
  >> $report_file
  goto report
endif
rm -f slop.par >& /dev/null

# Cut all headers out of the 'slop' output file.

/home/koehn/support/elgb/remove_slop_headers/remove_slop_headers slop.out sloppart01
rm -f slop.out

#  Cut all the information off the 'slop' output file except NEO
#  names.

cut -b 1-27 sloppart01 > slopnames
rm -f sloppart01 >& /dev/null

#  Keep only the first of the the NEO name.

awk ' { print $1 } ' slopnames > slopnames2
rm -f slopnames >& /dev/null

#  Generate an ephemeris for each of the selected NEOs

rm -f /info/pub/koehn/targets/* >& /dev/null
set neo_names = `cat slopnames2`
@ count = 0
foreach neo ( $neo_names )

  # Generate the user-supplied responses for ef8

  @ count = $count + 1
  echo o > ef8.par
  echo o >> ef8.par
  echo 699 >> ef8.par
  echo y >> ef8.par
  echo v >> ef8.par
  echo e >> ef8.par
  echo 1 >> ef8.par
  echo '' >> ef8.par
  echo $neo >> ef8.par                        #  For this asteroid
  echo ${year},${month},${day},00 >> ef8.par  #  Time starts as 17:00 MST
  echo 0,0,15 >> ef8.par  # 15 minute intervals
  echo 1,0,0 >> ef8.par   # for one day
  echo n >> ef8.par

  #  Generate the ephemeris

  if { /tibro/data/bin/ef8 < ef8.par > /dev/null } then
  else
    echo "***ERROR***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
    >> $report_file
    echo "Error while running /tibro/data/bin/ef8." \
    >> $report_file
    goto report
  endif
  
  #  Move it to a file for safekeeping
  
  if ( -e /info/pub/koehn/targets/ephem_${neo}.out ) then
    echo "***WARNING***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
  >> $report_file
    echo "A previous version of ef8.out ephem_${neo}.out exists.  This" \
  >> $report_file
    echo "script will destroy the older version." \
  >> $report_file
  endif
  mv -f ef8.out /info/pub/koehn/targets/ephem_${neo}.out >& /dev/null
end

rm -f ef8.par >& /dev/null
rm -f tmp.F* >& /dev/null
echo "***STATUS***  `date -u '+%Y %b %d %T'` UT (neo_observing.csh)" \
>> $report_file
echo "Created $count ephemeris files is /info/pub/koehn/targets." \
>> $report_file

report:

echo "" >> $report_file
echo "This script is run as a cron job by user koehn on tibro.lowell.edu" \
>> $report_file
echo "The source file is /home/koehn/support/elgb/neo_observing/neo_observing.csh" \
>> $report_file
if ( $month == 01 ) then
  set monthstr = Jan
endif 
if ( $month == 02 ) then
  set monthstr = Feb
endif 
if ( $month == 03 ) then
  set monthstr = Mar
endif 
if ( $month == 04 ) then
  set monthstr = Apr
endif 
if ( $month == 05 ) then
  set monthstr = May
endif 
if ( $month == 06 ) then
  set monthstr = Jun
endif 
if ( $month == 07 ) then
  set monthstr = Jul
endif 
if ( $month == 08 ) then
  set monthstr = Aug
endif 
if ( $month == 09 ) then
  set monthstr = Sep
endif 
if ( $month == 10 ) then
  set monthstr = Oct
endif 
if ( $month == 11 ) then
  set monthstr = Nov
endif 
if ( $month == 12 ) then
  set monthstr = Dec
endif
 
mailx -s "LONEOS STATUS $year $monthstr $day - NEO Target Generation Report" \
         koehn@lowell.edu < $report_file
sleep 20
rm -f $report_file slopnames2 >& /dev/null
cd $base >& /dev/null
exit(0)
