#!/bin/ksh
#
. /etc/profile
#
# Source the nwis environment
. /usr/opt/etc/nwis.profile
# Source the wrapper and check for DB lock
. $NWISHOME/util/nwo_set_nwis_env
#
#
############################## CHANGE HISTORY #######################################
#
#  02/08/10  Initial coding - WJW
#  02/09/10  Combined date/time into one column for UVs -WJW
#  03/29/10  Changed default stats to pull to all - WJW
#Version=1.0
#  12/29/10  Fixed bug causing problems with 15-digit station DV pulls (exceeding 31 
#            char filename limit in rdb2excel) -WJW
#  11/22/11  Added warning about Excel row and column limit errors. Added rdb files
#            to the output for manual importing if row/column limits cause issues- WJW
Version=1.1
#
#  09/25/12  Updated for NWIS 5.0/Oracle.  DV stat check was non-functional due to conversion.-WJW
Version=5.0
#
#####################################################################################


# set ADAPS global variable file - not sure where this came from, was in original script I started with to develop this-don't think it's necessary, but its not hurting anything
GVPATH=$NWISHOME/data/auxdata/adaps.gvar
export GVPATH
#
# initialize Oracle date format variables to add to queries
dtFormatQuery="alter session set NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'"
tsFormatQuery="alter session set NLS_TIMESTAMP_TZ_FORMAT='YYYY-MM-DD HH24:MI:SS'"

clear
rm -f temp.stafile tmp.stats tmp.stats.final statcds.tmp tmp.*vs.rdb1 tmp.*vs.rdb2
print "Version=$Version"
print '*****************************************************'
print '** adaps2excel - pull ADAPS rdb tables and convert **'
print '**         them to Excel spreadsheets              **'
print '*****************************************************'
echo
echo 'Please address comments, question, and bug reports to walker@usgs.gov'
echo
echo

# get input from user

# ---------------------------------- retrieve UV or DV data
  print  "Retrieve U-unit value data or D-daily value data for all stations? (CR=D):"
  read datatype
  case $datatype in
  U | u | uv | UV | Uv | uV )
   datatype=uv;;
  q | qu | ex | exit | quit | Q | QU | QUIT | EX | EXIT )
   print "Exiting script"
   rm -f temp.stafile tmp.stats tmp.stats.final statcds.tmp tmp.*vs.rdb1 tmp.*vs.rdb2
   exit;;
  * )
   datatype=dv;;
  esac

#-------------------------------------get dates
if [ -z "$wateryear" ]; then
 print
 print 'Enter water year (yyyy) or <CR> for detailed dates: '"\c"
 read wateryear
fi
numdate=`print $wateryear |wc -c`

if test $numdate -eq 5

then
    print
    print "Using water year "$wateryear
    begm='10'
    begd='01'
    begy=`expr $wateryear - 1`
    endma='09'
    endda='30'
    endya=$wateryear
    begtime=000000
    endtime=235900

date '+ 20%y %m %d' > comparedate.temp
cat comparedate.temp |
while read currentyear currentmonth currentday; do
if test $currentyear$currentmonth$currentday -lt $endya$endma$endda 
then 
endy=$currentyear
endm=$currentmonth
endd=$currentday
else
endy=$endya
endm=$endma
endd=$endda
fi
done
rm -f comparedate.temp
datelbl=$wateryear


else 
    print 
    until [ "$begm" ]
    do
    print 'Enter dates to use'
    print
    print 'Begin date (mm dd yyyy) '"\c"
    read date
    print
    print 'End date (mm dd yyyy) '"\c"
    read edate
    print $date | cut -b1-2 | read begm  # parses out mm to $begm
    print $date | cut -b4-5 | read begd  # parses out dd to $begd
    print $date | cut -b7-10 | read begy  # parses out yyyy to $begy
    print $edate | cut -b1-2 | read endm  # parses out mm to $endm
    print $edate | cut -b4-5 | read endd  # parses out dd to $endd
    print $edate | cut -b7-10 | read endy  # parses out yyyy to $endy
    print
    print 'You entered ' $begm $begd $begy 'to ' $endm $endd $endy
    print 'Is this correct? (CR=y)'"\c"
    read question
    case $question in
    n) begm='';;
    N) begm=;;
    esac
    done
    begtime=001500
    endtime=235900
    datelbl="$begm-$begd-"$begy"_to_"$endm"-$endd-$endy"
fi



#------------------------------------get station
station="dummy"
until [ "$station" = "" ] 
do
clear
statcode=''
if [[ -s temp.stafile ]]
then
 if [[ "$datatype" = "dv" ]]
 then
 print "Stations, DDs, stat codes entered so far:"
 cat temp.stafile
 else
 print "Stations and DDs entered so far:"
 cat temp.stafile
 fi
fi
  echo
  echo
  print "\nEnter station numbers, one at time (cr=done entering stations) (q=quit script): \c"
  read station
  case $station in
   q | qu | quit | ex | Q | QU | QUIT | EX | exit | EXIT )
    rm -f temp.stafile tmp.stats tmp.stats.final statcds.tmp tmp.*vs.rdb1 tmp.*vs.rdb2
    exit;;
  *) ;;
  esac

if [ -n "$station" ]
then
  statest=`/usr/local/bin/tsql $NWISDB  "select s.station_nm from sitefile_01 s 
        where s.agency_cd='USGS' and s.site_no='$station'"`

  case $statest in
  '' ) 
    print $station ' does not exist in the site file. Please try again or enter q to quit.'
    print
    station="dummy";;
  *) echo
      /usr/opt/bin/pick_dd -n$station | read dd  
     echo
     if [[ "$datatype" = "dv" ]]
      then
      until [ "$statcode" ]
      do
       echo
      rm -f statcds.tmp
       /usr/local/bin/tsql $NWISDB   "${dtFormatQuery}" "${tsFormatQuery}" "select dv.stat_cd from dv_comp_inst_01 dv, \
                           dd_01 dd, loc_01 l, sitefile_01 s, ts_comp_inst_01 t \
                           where s.site_no='${station}' and s.site_id=l.site_id and l.loc_id=dd.loc_id and \
                           dd.dd_nu='${dd}' and dd.dd_id=t.dd_id and dd.dd_id=dv.dd_id and t.ts_comp_start_dt < '$begy-$begm-$begd 12:00:00' and \
                           t.ts_comp_end_dt > '$begy-$begm-$begd 00:00:00' and t.ts_comp_inst_id=dv.ts_comp_inst_id" >statcds.tmp

        print "The following stat codes were set up to be stored at the beginning of the period for this site and DD:"
        print
        print "Station: $station    DD: $dd"
        print "Stats:"
        cat statcds.tmp
#        rm -f statcds.tmp
      echo
      print "Enter space separated list of up to 9 DV stat codes to pull (leading zeros not required)"
      print "i.e. entering 1 2 3 would pull max, min, and mean daily stats for that station"
      print "Available stat codes for this station DD time period are shown above (CR=all available stats):"
      read statcode
     print $statcode | read stat1 stat2 stat3 stat4 stat5 stat6 stat7 stat8 stat9 crap
     print $stat1 > tmp.stats
     print $stat2 >> tmp.stats
     print $stat3 >> tmp.stats
     print $stat4 >> tmp.stats
     print $stat5 >> tmp.stats
     print $stat6 >> tmp.stats
     print $stat7 >> tmp.stats
     print $stat8 >> tmp.stats
     print $stat9 >> tmp.stats

rm -f tmp.stats.final
   if [[ -n $stat2 ]]  #if more than one stat, then cycle through stats and screen them
   then
   while read stat
      do
      if [[ -n $stat ]]
      then
      case $stat in
       [0-9])
       print $stat >> tmp.stats.final
           ;;
       [0-3][0-9][0-9][0-9][0-9])
       print $stat >> tmp.stats.final
           ;;
       q | quit | qu | ex | exit | Q | QUIT | EX | EXIT)
        rm -f temp.stafile tmp.stats tmp.stats.final statcds.tmp tmp.*vs.rdb1 tmp.*vs.rdb2
        exit ;;
#       l | L | list | LIST )
#        statcode=''
#        clear
#     
#        /usr/opt/ingres/bin/tsql $NWISDB -Gnwis_select "select dv.stat_cd from dv_comp_inst_01 dv, dd_01 dd, loc_01 l, sitefile_01 s, ts_comp_inst_01 t \
#                           where s.site_no='${station}' and s.site_id=l.site_id and l.loc_id=dd.loc_id and \
#                           dd.dd_nu='${dd}' and dd.dd_id=t.dd_id and dd.dd_id=dv.dd_id and t.ts_comp_start_dt < '$begy-$begm-$begd 00:15:00' and \
#                           t.ts_comp_end_dt > '$begy-$begm-$begd 00:00:00' and t.ts_comp_inst_id=dv.ts_comp_inst_id" >statcds.tmp

#        print "The following stat codes were set up to be stored at the beginning of the period for this site and DD:"
#        print
#        print "Station: $station    DD: $dd"
#        print "Stats:"
#        cat statcds.tmp
#        ;;
       * )       
        print "Didn't understand entry - Defaulting to all avilable stats..."
        statcode="all"
        cp statcds.tmp > tmp.stats.final
        sleep 3;;
       esac  #end statcode
       fi # end stat check
      done  < tmp.stats #end stat loop
     else   # if just one stat, then we need to see if it's a real stat or if we need to default to 3-mean

      case $stat1 in
       [0-9])
       print $stat1 >> tmp.stats.final

           ;;
       [0-3][0-9][0-9][0-9][0-9])
       print $stat1 >> tmp.stats.final
           ;;
       q | quit | qu | ex | exit | Q | QUIT | EX | EXIT)
        rm -f temp.stafile tmp.stats tmp.stats.final statcds.tmp tmp.*vs.rdb1 tmp.*vs.rdb2
        exit ;;
       * )       
        print "Defaulting to all available stats ..."
        statcode="all"
        cp statcds.tmp tmp.stats.final
        sleep 3;;
       esac
     fi # end $stat2 check to see if one or more stats entered (handled slightly differently to allow defaulting to mean...
     done   #end statcode loop
     if [[ -s tmp.stats.final ]]
     then
     while read stats
     do
     print "$station $dd $stats" >>temp.stafile
     done <tmp.stats.final
     fi
    # statcode=''
     else
     print "$station $dd" >> temp.stafile
     fi
     ;;
  esac  # end statest
fi

done
clear
echo
if [[ "$datatype" = "dv" ]]
then
print "Using stations DDs stats:"
cat temp.stafile
else
print "Using stations DDs:"
cat temp.stafile
fi

wc -l < temp.stafile | read numsta
#print $numsta


# Below difficult to implement in Excel spreadsheet - hard to label in single row...
#if ((numsta > 1))
#then
#print
#print 'Do you want all stations in one spreadsheet? (No=separate file for each station) (CR=N) '"\c"
#read onefile
#print
#case $onefile in
#   y | yes | YES | Yes | yES | Y | yep | yesireebob )
# onefile='y';;
#  q | qu | quit | ex | exit | Q | QU | QUIT | EX | EXIT )
# print "exiting script"
#  rm -f temp.stafile tmp.stats tmp.stats.final
#  exit;;
#  *)
# onefile='n' ;;
#esac
#fi


if [[ $datatype = "uv" ]]
then
while read sta dd
do
/usr/opt/bin/nwts2rdb -otmp.$sta.uvs.rdb -tuv -c -sC -b$begy$begm$begd -e$endy$endm$endd -n$sta -d$dd
#if [[ $onefile = "n" ]]
#then
/usr/local/bin/column DATETIME TZCD VALUE REMARK FLAGS QA <tmp.$sta.uvs.rdb> tmp.uvs.rdb1
grep ^# < tmp.uvs.rdb1> $sta.DD$dd.UVs
grep -v ^# <tmp.uvs.rdb1> tmp.uvs.rdb2
print "#Table legend:" >> $sta.DD$dd.UVs
print "#Remark Codes: e=Estimated, 1=Write-protected, A=Affected by ice, B=Affected by backwater, R=Rating undefined,">> $sta.DD$dd.UVs
print "#.......................&=Affected by unspecified, K=Affected by calibration drift, >=Actual value is greater than shown,">> $sta.DD$dd.UVs
print "#.......................<=Actual value is less than shown, E=Computed from estimated value, F=Modified by automated filter" >> $sta.DD$dd.UVs
print "#Flags: I=Exceeds very rapid increase threshold, i=Exceeds rapid increase threshold, L=Exceeds very low threshold" >> $sta.DD$dd.UVs
print "#..........l=Exceeds low threshold, D=Exceed very rapid decrease threshold, d=Exceeds rapid decrease threshold," >> $sta.DD$dd.UVs
print "#..........H=Exceeds very high threshold, h=Exceeds high threshold, T=Exceeds standard difference threshold," >> $sta.DD$dd.UVs
print "#..........U=Value not used for computation of daily value, @=Reviewed by USGS personnel, *=Edited by USGS personnel" >> $sta.DD$dd.UVs
print "#QA Codes: W=working, R=In-Review, A=Approved" >> $sta.DD$dd.UVs
print "#" >> $sta.DD$dd.UVs
cat tmp.uvs.rdb2 >> $sta.DD$dd.UVs

#output to temp file and rename to avoid possibly exceeding allowable 31 char filename in rdb2excel
rdb2excel $sta.DD$dd.UVs temp.adaps2excel.xls
echo
print "IMPORTANT NOTE: If you received an Excel-column or row limit error above you"
print "                will need to manually open and import the rdb file into Excel."
echo
mv temp.adaps2excel.xls $sta.$datelbl.DD$dd.uvdata.xls
mv $sta.DD$dd.UVs $sta.$datelbl.DD$dd.uvdata.rdb

rm -f $sta.DD$dd.UVs tmp.$sta.uvs.rdb
print "File $sta.$datelbl.DD$dd.uvdata.rdb output to $PWD"
print "File $sta.$datelbl.DD$dd.uvdata.xls output to $PWD"
#else
#print
#fi
done <temp.stafile

else # if not UV's then different data retrievals and files.
while read sta dd stat
do
/usr/opt/bin/nwts2rdb -otmp.$sta.dvs.rdb -tdv -s$stat -b$begy$begm$begd -e$endy$endm$endd -n$sta -d$dd
#if [[ $onefile = "n" ]]
#then
/usr/local/bin/column DATE VALUE REMARK QA < tmp.$sta.dvs.rdb > tmp.dvs.rdb1
grep ^# <tmp.dvs.rdb1> $sta.DD$dd.stat$stat.DVs
grep -v ^# <tmp.dvs.rdb1> tmp.dvs.rdb2
print "#Table legend:" >> $sta.DD$dd.stat$stat.DVs
print "#Remark Codes: e=Edited or estimated, 1=value write-protected, 2=remark code won't be printed on DV table,">> $sta.DD$dd.stat$stat.DVs
print "#.......................E=Value computed from estimated unit values, &=Value computed from affected unit values,">> $sta.DD$dd.stat$stat.DVs
print "#.......................>=Actual value is greater than shown, <=Actual value is less than shown" >> $sta.DD$dd.stat$stat.DVs
print "#QA Codes: W=working, R=In-Review, A=Approved" >> $sta.DD$dd.stat$stat.DVs
cat tmp.dvs.rdb2 >> $sta.DD$dd.stat$stat.DVs



#use temp files with rdb2excel to skirt around allowable 31 char filenames
mv $sta.DD$dd.stat$stat.DVs temp.adaps2excel.DVs
rdb2excel temp.adaps2excel.DVs temp.adaps2excel.DVs.xls
echo
print "IMPORTANT NOTE: If you received an Excel-column or row limit error above you"
print "                will need to manually open and import the rdb file into Excel."
echo

mv temp.adaps2excel.DVs.xls $sta.$datelbl.DD$dd.stat$stat.DVdata.xls
mv temp.adaps2excel.DVs $sta.$datelbl.DD$dd.stat$stat.DVdata.rdb

rm -f $sta.DD$dd.stat$stat.DVs tmp.$sta.dvs.rdb
print "File $sta.$datelbl.DD$dd.stat$stat.DVdata.rdb output to $PWD"
print "File $sta.$datelbl.DD$dd.stat$stat.DVdata.xls output to $PWD"
#else
#print
#fi
done <temp.stafile
fi # End if for diff. data types.

print
print "Adaps2excel finished"
print "All spreadsheets should be in $PWD"
print
rm -f temp.stafile tmp.stats tmp.stats.final statcds.tmp tmp.*vs.rdb1 tmp.*vs.rdb2

exit