#! /usr/opt/bin/python
####################################################################
# convert_status
#
# dec, 1997
# by Dave Briar
####################################################################
#
#  Python program which converts /RT_HOME/rt_station.status:
#
#         from rev 1.1 to -->> rev 2.0 format
#
####################################################################

RT_HOME="/mt/www/rt11"

# ------------------------------------------------------------------------
# -- Do imports and system level househeeping
# ------------------------------------------------------------------------
import sys, DateTime, RDB, urllib
sys.path.insert(1,RT_HOME)     # <<== add path for modules
import string,posix,posixpath,time,RDB
from rt_config_defs import *   # <<== sets config variables
from __builtin__ import *      # <<== speeds up use of builtins

startsecs=time.time()
logsdir= RT_HOME+'/logs'
posix.chdir(RT_HOME+'/data')
posix.umask(002)

#
# -- Definition from posixpath.py
#
def exists(path):
        try:
                st = posix.stat(path)
        except posix.error:
                return 0
        return 1

dow,month_name,day,hrminsec,year=tuple(string.split(time.ctime(time.time())))
banner_time=dow+'  '+month_name+' '+day+', '+year+'  '+hrminsec[:5]

# ----------------------------------------------------------------------
# -- Default output to "print"
output=sys.stdout.write

output('\n========================================================================\n')
output('==  convert_status.py         '+banner_time+'\n')

# ----------------------------------------------------------------------
# -- Check to make sure path for Prime transfer file has been supplied
# --    as agrument on the command line  Then execute
# --    uv_rdb to build raw rdb files for each station
# ----------------------------------------------------------------------
if len(sys.argv) > 1 and string.upper(sys.argv[1]) <> '-L':
   # -- See if file sys.argv[1] exists
   try:
      posix.stat(sys.argv[1])
      exists=1
   except:
      exists=0

   if exists:
      output('==\n')
      output('==  Executing '+RT_HOME+'/util/uv.rdb\n')
      output('==    ..reading data from  '+sys.argv[1]+'\n')
      output('==    ..updating station-data files in  '+RT_HOME+'/data\n')
      posix.system(RT_HOME+'/util/uv_rdb '+sys.argv[1]+' '+RT_HOME+'/data\n')

   # -- Catch file not exist
   else:
      output('==\n==  Prime transfer file '+sys.argv[1]+'\n')
      output('==     not found or unreadable\n')
      output('==  Preparing existing station data in '+RT_HOME+'/data\n')

else:
   output('==\n==  Preparing existing station data in '+RT_HOME+'/data\n')

# ------------------------------------------------------------------------
# -- Set aliases and invariants
# ------------------------------------------------------------------------
RTError=           'RTError'
string_find=        string.find
string_splitfields= string.splitfields
string_joinfields=  string.joinfields
string_rjust=       string.rjust
string_upper=       string.upper
digits=             string.digits
page=1

# ------------------------------------------------------------------------
# -- Create RDB instance from rt_station.dat
# ------------------------------------------------------------------------
sdatD={}
STATION_NUMBERS=[]

sdatT=RDB.Table(RT_HOME+'/rt_station.dat')
for i in range(len(sdatT)):
   if sdatT[i].STATION_NUMBER[0] in digits:
      # -- Append station number to list
      STATION_NUMBERS.append(sdatT[i].STATION_NUMBER)
      # -- Assign sdatD dictionary; <key=>station: <value=>recno_in_sdatT
      sdatD[sdatT[i].STATION_NUMBER]=i

# ------------------------------------------------------------------------
# --  Create RDB instance from rt_station.status
# ------------------------------------------------------------------------
rfo=open(RT_HOME+'/rt_station.status')
stsT=RDB.Table(rfo)
rfo.close()
sts_D={}

# -- Initialize sts Dictionary with empty list
for i in STATION_NUMBERS: sts_D[i]=[]

# -- Append record numbers to stsD.values which match each station
for i in range(len(stsT)):
   if stsT[i][0] in STATION_NUMBERS:
      sts_D[stsT[i][0]].append(i)

# ------------------------------------------------------------------------
# -- Build pageD -- Records which recnos in station.dat or cache file
# --                  correspond to each page
# --   A string version of this dict gets imbedded in cache file
# --   <key=>page : <value=> [TABLE_TITLE, PAGE_TITLE, recno, recno, recno...]
# ------------------------------------------------------------------------
page=0
pageD={}
for i in range(len(sdatT)):
   stn_no=sdatT[i].STATION_NUMBER
   if stn_no == "PAGE":
      page=page + 1

      # -- Add key = page to pageD if not already has key
      if not pageD.has_key(page): pageD[page]=[]

      # -- Set PAGE_TITLE
      PAGE_TITLE=sdatT[i].SITE_NAME_LONG

      # -- Parse out PAGE_TITLE on upper <BR>
      table_title_list=string.splitfields(PAGE_TITLE,'<BR>')
      # -- Try again on lower <br>
      if len(table_title_list) == 1:
         table_title_list=string.splitfields(PAGE_TITLE,'<br>')
      TABLE_TITLE=table_title_list[0]
      pageD[page].append(TABLE_TITLE)
      pageD[page].append(PAGE_TITLE)

   else: 
      # -- Append recno to value list
      pageD[page].append(i)

# -- Build pageL_string to pass to cache;  Include 4 items for EACH PAGE
# --  formatted as follows => TABLE_TITLE,PAGE_TITLE,first_recno_in_cache,last_recno_in_cache
pageL_data=''
for page in range(1,len(pageD)+1):
   pageL_data=pageL_data+pageD[page][0]+','+pageD[page][1]+','+str(pageD[page][2])+','+str(pageD[page][-1])+'?'

# -- Strip off last "?"
pageL_data=pageL_data[:-1]

# -- Open revised status file

wfo=open('/mt/www/rt2/rt_station.status.new','w')
wfo.write('STATION_NUMBER\tPARAMETER\tOFFDATE\tONDATE\tTBL_LABEL\tCOMMENT\n')
wfo.write('15s\t8s\t19D\t19D\t3s\t25s\n')

# ==================================================================================
# ================     Start main loop through stations here    ====================
# ==================================================================================
stn_seq_num=0
outlist=[]

for i in range(len(sdatT)) :
   if sdatT[i].STATION_NUMBER[0] in digits:
      stn_seq_num=stn_seq_num+1
      stn=sdatT[i].STATION_NUMBER
      stn_name=sdatT[i].SITE_NAME_LONG
      output('\n'+string_rjust(str(stn_seq_num),2)+'  '+stn+'  '+stn_name+'\n')

      # ----------------------------------------------------------------------
      # -- Search rt_station.status list to get periods of bad data
      # ----------------------------------------------------------------------
      # -- Initialize vars and lists
      # --   *_status=1 == "currently on";  *_status=0 == "currently off"
      s_status=q_status=t_status=1
      purgelist=[]

      S_OFFDATE=[]
      S_COMMENT=[]
      S_ONDATE= []

      Q_OFFDATE=[]
      Q_ONDATE= []
      Q_COMMENT=[]

      T_OFFDATE=[]
      T_ONDATE= []
      T_COMMENT=[]

      # -- Step through records in station_status list and append
      # --   S_OFFDATE, S_ONDATE, Q_OFFDATE, Q_ONDATE, T_OFFDATE, and T_ONDATE
      # --   to appropriate list when STATION_NUMBERS=status_STATION_NUMBER
      # --   Set TBL_COMMENT to stsT[s].TBL_COMMENT of last offdate found
      for s in sts_D[stn]:
        if stsT[s].S_ONDATE: S_ONDATE.append(stsT[s].S_ONDATE)
        if stsT[s].S_OFFDATE:
           S_OFFDATE.append(stsT[s].S_OFFDATE)
           S_COMMENT.append(stsT[s].TBL_COMMENT)

        if stsT[s].Q_ONDATE: Q_ONDATE.append(stsT[s].Q_ONDATE)
        if stsT[s].Q_OFFDATE:
           Q_OFFDATE.append(stsT[s].Q_OFFDATE)
           Q_COMMENT.append(stsT[s].TBL_COMMENT)

        if stsT[s].T_ONDATE: T_ONDATE.append(stsT[s].T_ONDATE)
        if stsT[s].T_OFFDATE:
           T_OFFDATE.append(stsT[s].T_OFFDATE)
           T_COMMENT.append(stsT[s].TBL_COMMENT)

      # -- If last *_ONDATE is null; Set *_ONDATE to '2100.01.01' and
      # --    *_status=0
      while len(S_OFFDATE) > len(S_ONDATE):
         S_ONDATE.append('')
         s_status=0
      while len(Q_OFFDATE) > len(Q_ONDATE):
         Q_ONDATE.append('')
         q_status=0
      while len(T_OFFDATE) > len(T_ONDATE):
         T_ONDATE.append('')
         t_status=0

      stn=string.strip(stn)

      if len(S_OFFDATE):
         for i in range(len(S_OFFDATE)):
            outlist.append(stn+'\t00065\t'+str(S_OFFDATE[i])+'\t'+str(S_ONDATE[i])+'\t'+S_COMMENT[i]+'\t\n')

      if len(Q_OFFDATE):
         for i in range(len(Q_OFFDATE)):
            outlist.append(stn+'\t00060\t'+str(Q_OFFDATE[i])+'\t'+str(Q_ONDATE[i])+'\t'+Q_COMMENT[i]+'\t\n')

      if len(T_OFFDATE):
         for i in range(len(T_OFFDATE)):
            outlist.append(stn+'\t00010\t'+str(T_OFFDATE[i])+'\t'+str(T_ONDATE[i])+'\t'+T_COMMENT[i]+'\t\n')

outlist.sort()
wfo.writelines(outlist)

runsecs=round((time.time()-startsecs)+2,2)
persecs=runsecs/len(STATION_NUMBERS)
output('\n||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||\n')
output(' Finished -- Total execution time = '+str(runsecs)+' seconds\n')
output('                                     '+str(round(persecs,2))+' seconds per station\n\n\n')