# -*- coding: utf-8 -*- """ Created on Tue Aug 27 15:13:06 2013 Taken from one of my analysis scripts, this code goes through and generates a dictionary object containing all of the forecasts that pass the requirements that I set out. The first chunk, beginning with "for line in lines:", opens the csv files output by reqparser.py, and creates dictionary entries for those forecasts that pass the tests outlined in the section (Prescribed burn, after noon, within acreage range, making sure temp and rh are forecast for the first period). The second chunk goes through the list generated in the first chunk and opens the forecast text file itself, pulling out the data relating to temperature and relative humidity and storing it in the dictionary. Data relating to timing and location are also processed. @author: Matt Lammers """ import datetime,os,time import numpy as np initdate = datetime.datetime(2013,8,20) #dlist = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30'] fcnum = 0 total = 0 wfodict = {} timedict = {} approved = {} inc = 0 while inc<1693: #1126 for entire period, 1693 for 2009-8/20/2013 daytotal = 0 dayfcnum = 0 dayapp = 0 fulldt = initdate - datetime.timedelta(days=inc) date = fulldt.strftime("%Y%m") d = fulldt.strftime("%d") #print date+d try: directory = "/uufs/chpc.utah.edu/common/home/horel-group/jfsp/"+date+d+"/" filename = directory+date+d+".txt" datfile = open(filename,'rb') except IOError: inc = inc+1 continue lines = datfile.readlines() del lines[0] for line in lines: try: try: if len(line)<20: continue if line[4:7] not in wfodict: wfodict[line[4:7]] = [1,0,0,0,0,0] else: wfodict[line[4:7]][0] = wfodict[line[4:7]][0]+1 total = total+1 daytotal = daytotal+1 splitline = line.split(',') #print splitline if splitline[5] != 'PRESCRIBED': continue wfodict[line[4:7]][1] = wfodict[line[4:7]][1]+1 if int(splitline[3]) > 1200: continue if float(splitline[20]) < 100 or float(splitline[20]) > 1000: continue if splitline[24].split(':')[1] < 4: continue if splitline[25].split(':')[1] < 4: continue reqid = line[4:25] igdate = splitline[2] igtime = splitline[3] lat = splitline[13] lon = splitline[14] elev = np.mean([int(splitline[18]),int(splitline[19])]) timedict[reqid] = {'igdate':igdate,'igtime':igtime,'latitude':lat,'longitude':lon,'elevation':elev} except ValueError: continue except IndexError: print splitline continue spotlist = os.listdir(directory) for x in spotlist: if x[0:3] == 'FWS' and x[4:25] in timedict: forefile = open(directory+x,'rb') forelines = forefile.readlines() if len(forelines)>5: timeline = forelines[5] else: continue wfodict[x[4:7]][2] = wfodict[x[4:7]][2]+1 tstring = "" hstring = "" tcopy = False hcopy = False for g in forelines: if g.find("TODAY")!=-1: tcopy = True hcopy = True if g.find("TONIGHT")!=-1: tcopy = False hcopy = False if (g.find("TEMPERATURE")!=-1 or g.find("TEMP")!=-1) and tcopy: tstring = g tcopy = False elif (g.find("HUMIDITY")!=-1 or g.find("RH")!=-1) and hcopy: hstring = g hcopy = False tstring = tstring.replace('.',' ').split() hstring = hstring.replace('.',' ').split() if len(tstring) == 0 or len(hstring) == 0: continue tshort = [] hshort = [] t = 0 while t<(len(tstring)-1): #print t if tstring[t].isdigit() or tstring[t].find('-')!=-1: if (tstring[t+1].find('PM')==-1 and tstring[t].find('MID')==-1): tshort.append(tstring[t]) t = t+1 #print t if tstring[t].isdigit() or tstring[t].find('-')!=-1: tshort.append(tstring[t]) h = 0 while h<(len(hstring)-1): if hstring[h].strip('%').isdigit() or hstring[h].find('-')!=-1: if hstring[h+1].find('PM')==-1: hshort.append(hstring[h].strip('%')) h = h+1 if hstring[h].strip('%').isdigit() or hstring[h].find('-')!=-1: hshort.append(hstring[h].strip('%')) fcnum = fcnum + 1 wfodict[x[4:7]][3] = wfodict[x[4:7]][3]+1 dayfcnum = dayfcnum + 1 tzloc = timeline.find("T") tzname = timeline[tzloc-3:tzloc+1].strip() #print tzname if tzname == "EDT": tz = 4 elif tzname == "EST" or tzname == "CDT": tz = 5 elif tzname == "CST" or tzname == "MDT": tz = 6 elif tzname == "MST" or tzname == "PDT": tz = 7 elif tzname == "PST" or tzname == "AKDT": tz = 8 elif tzname == "AKST" or tzname == "HDT" or tzname == "HADT": tz = 9 elif tzname == "HAST" or tzname == "HST": tz = 10 #print tz foredate = x[26:34] foretime = x[35:39] try: ignitionloc = datetime.datetime(int("20"+timedict[x[4:25]]['igdate'].split('/')[2]),int(timedict[x[4:25]]['igdate'].split('/')[0]),int(timedict[x[4:25]]['igdate'].split('/')[1]),int(timedict[x[4:25]]['igtime'][:2]),int(timedict[x[4:25]]['igtime'][2:])) except ValueError: print "ValErr" continue lati = timedict[x[4:25]]['latitude'] longi = timedict[x[4:25]]['longitude'] elevi = timedict[x[4:25]]['elevation'] utcdiff = datetime.timedelta(hours=tz) ignition = ignitionloc + utcdiff forecast = datetime.datetime(int(foredate[:4]),int(foredate[4:6]),int(foredate[6:]),int(foretime[:2]),int(foretime[2:])) tdiff = ignition-forecast if tdiff > datetime.timedelta(hours=6) or ignition-forecast < datetime.timedelta(hours=3): continue else: dayapp = dayapp+1 wfodict[x[4:7]][4] = wfodict[x[4:7]][4]+1 approved[x] = {'igdate':foredate,'igtime':foretime,'foretime': forecast,'igtimeloc':ignition,'tempstring':tstring,'rhstring':hstring,'temparray':tshort,'rharray':hshort,'latitude':lati,'longitude':longi,'elevation':elevi,'leadtime':tdiff} #print daytotal #print dayfcnum #print dayapp inc = inc+1 #for i in approved: #print i[0] #print i[3] #print i[4] #print i[5] #print i[6] #print "" print total print len(timedict) print fcnum print str(len(approved))+' Forecasts Left' print str(time.time()-sttm)+' Seconds Elapsed'