#!/bin/csh -f
# To develop a time series of flow data from a "DSS"-formatted data file.
# Extracts DSS time series by simply specifying B-Part of path
#   1. assumes all DSS tags are from 1/1/65 through 12/31/1995 (NO LONGER restricted, see below)
#   2. takes first occurrence of the B-Part, thus ideal for str2x2.dss files
#      Use with caution if DSS file has multiple entries with same B-Part.
#   3. output in columnar form with yr_mo_da row header and B-Part names
#      as column headers
#   4. written by lbrion on May 2000
# Modified by cfitz July 2001 to:
#   a. read a file with a line/list of structure names (instead of cut/paste)
#   b. use 2 args, for start year and end year (nov 2003)
# screen prompts
    
echo ' '
echo 'You must supply a start and end year (e.g., getQ.scr 1966 1991)'

echo ' '
echo -n 'Enter input DSS (base)filename, (DO NOT write the .dss extension.): '
set dssfn = $<
echo ' '
echo -n 'Enter output ASCII (full)filename: '
set outfn = $<
echo ' '
#echo -n 'Enter list of structure names (DSS B-Parts) whose time series will be extracted: '
#set str = $<

# read the list from a file in this version of script
echo -n 'Enter filename with list of structure names (DSS B-Parts) whose time series will be extracted: '
set listFile = $<
set str = `cat $listFile`

set hsmBin = "/vol/hsm/bin/solaris"
# create a soft link for dss file before doing data extraction
echo " "
echo "Working on DSS file $dssfn.dss"
echo Working . . .
if(! -e $dssfn.dss )  echo Sorry, File $dssfn.dss not found
if(! -e $dssfn.dss )  echo EXITING SCRIPT
if(! -e $dssfn.dss )  exit
# create symbolic link "tmp$$.dss" to $dssfn.dss in the cwd to make up
# for catDSS max_65char_dss_file_length limitation.
ln -s $dssfn.dss tmp$$.dss
# check to see if the catalog does not exist
if(! -e $dssfn.dssc) then
# catalog file does not exist; do catDSS in cwd & then copy it over to original
# location; note: This will update the date stamp of the original dss file
   $hsmBin/catDSS -q tmp$$.dss
   cp -p tmp$$.dssc $dssfn.dssc
   /usr/bin/cp -p tmp$$.dssd $dssfn.dssd
else
# catalog file exists
# check to see if catalog is empty or not
   if ( -z $dssfn.dssc ) then
# catalog is empty, redo the catalog
# note: This will update the date stamp of the original dss file
      $hsmBin/catDSS -q tmp$$.dss
      /usr/bin/cp -p tmp$$.dssc $dssfn.dssc
      /usr/bin/cp -p tmp$$.dssd $dssfn.dssd
   else
# catalog is not empty, just do a soft link
      ln -s $dssfn.dssc tmp$$.dssc
   endif
endif
# initialize variables
# query: Will a simple          set SFWMM_START_YR = 1965          work?
# modified to read 2 args for start and end year
set SFWMM_START_YR = $1
set SFWMM_START_MONTH = "1"
set SFWMM_START_DAY = "1"
set SFWMM_END_YR = $2
set SFWMM_END_MONTH = "12"
set SFWMM_END_DAY = "31"
#echo "" >! /tmp/out.$$      <== this works but has a size of 1 byte, not 0 !!
echo "" | sed -n 2,\$p > /tmp/out.$$
echo "" | sed -n 2,\$p > /tmp/combts.$$
echo "" | sed -n 2,\$p > /tmp/combts_add.$$
echo "   YR MO DA" >! /tmp/header.$$
# Create ts file with zero flows
set tag = "T23"
$hsmBin/getDSS -s ${SFWMM_START_YR}/${SFWMM_START_MONTH}/${SFWMM_START_DAY} -e ${SFWMM_END_YR}/${SFWMM_END_MONTH}/${SFWMM_END_DAY} -t $tag -d tmp$$.dss -o /tmp/out.$$ -q -h no -f %20.2f -n no
#$hsmBin/getDSS -t $tag -d tmp$$.dss -o /tmp/out.$$ -q -h no -f %10.2f -n no
awk '{printf("%5d%3d%3d%10.2f\n",$1,$2,$3,0)}' /tmp/out.$$ > /tmp/zero_flow.$$
# go through all structures
set j = 0
foreach struc ($str)
   set j = `expr $j + 1`
   set tag = `grep -i \/$struc\/ tmp$$.dssc | grep  JAN${SFWMM_START_YR} | awk '{print $2}'`
   if ($tag == "") then
      echo Structure or B-Part $struc not in $dssfn.dss
      echo Zeroes will be written under the heading $struc
      /bin/cp /tmp/zero_flow.$$ /tmp/out.$$
   else
#echo DSS Tag is $tag
echo structure is $struc
     $hsmBin/getDSS -s ${SFWMM_START_YR}/${SFWMM_START_MONTH}/${SFWMM_START_DAY} -e ${SFWMM_END_YR}/${SFWMM_END_MONTH}/${SFWMM_END_DAY} -t $tag -d tmp$$.dss -o /tmp/out.$$ -q -h no -f %20.2f -n no
#      $hsmBin/getDSS -t $tag -d tmp$$.dss -o /tmp/out.$$ -q -h no -f %10.2f -n no
   endif
   if ($j == 1) then
# print with date tags; this becomes the first /tmp/combts.$$
      awk '{printf("%5d%3d%3d%10.2f\n",$1,$2,$3,$4)}' /tmp/out.$$ >! /tmp/combts.$$
   else
# print without date tags
      awk '{printf("%10.2f\n",$4)}' /tmp/out.$$ >! /tmp/combts_add.$$
# combine time series
      paste -d"\0" /tmp/combts.$$ /tmp/combts_add.$$ > /tmp/combts_tmp.$$
      /bin/mv /tmp/combts_tmp.$$ /tmp/combts.$$
      /usr/bin/rm /tmp/combts_add.$$
   endif
# extend current header to include this structure
   echo $struc >! /tmp/str_hdr.$$
   awk '{printf("%10s",$1)}' /tmp/str_hdr.$$ > /tmp/header_add.$$
# combine header
   paste -d"\0" /tmp/header.$$ /tmp/header_add.$$ >! /tmp/header_tmp.$$
   /bin/mv /tmp/header_tmp.$$ /tmp/header.$$
   /usr/bin/rm /tmp/str_hdr.$$ /tmp/header_add.$$
# next structure
end
/usr/bin/rm tmp$$.dss*
/usr/bin/rm /tmp/out.$$
/usr/bin/rm /tmp/zero_flow.$$
cat /tmp/header.$$ /tmp/combts.$$ > $outfn
/usr/bin/rm /tmp/combts.$$ /tmp/header.$$
echo Done !
