# -*- coding: utf-8 -*- """ Created on Thu Aug 22 09:45:46 2013 Had to recreate the spot forecast request parsing script...it appears to have disappeared since the last bundle of forecasts I had to deal with. This script outputs a text file for each day that aggregates the meat of the forecast request into a csv file that can be easily split. The only modification of text that occurs is that the lines describing for what periods a given variable forecast is requested are converted from 1,1,1 to a binary representation and then converted to a decimal number 0-7. @author: Matt Lammers """ import os,datetime initdate = datetime.datetime(2013,8,20) directory = '/uufs/chpc.utah.edu/common/home/horel-group/jfsp/' inc = 0 while inc<477: fulldt = initdate - datetime.timedelta(days=inc) date = fulldt.strftime("%Y%m") d = fulldt.strftime("%d") print date+d try: outfile = open(directory+date+d+'/'+date+d+'.txt','w') outfile.write('File Name, Priority, Date, Time, Name, Type, Agency, Official, Reason, Fax, Phone, Location, State, Lat, Long, Exposure, Fuel Type, Sheltering, Bottom Elev., Top Elev., Size, Conditions Reported, Remarks, Requested Parameters\n') reqlist = os.listdir(directory+date+d) for x in reqlist: if x[0:3]=='STQ': loadstring = x+',' reqfile = open(directory+date+d+'/'+x,'r') reqlines = reqfile.readlines() printer0 = True printer1 = False printer2 = False printer3 = False for line in reqlines: if (line.find(':')!=-1 and printer3!=True and printer2!=True and printer0): loadstring = loadstring+line.split(':')[1].strip().replace(',','')+',' elif (line.find('WEATHER CONDITIONS')!=-1): printer1 = True printer0 = False elif (printer1 and len(line.strip())>0): loadstring = loadstring+line.strip().replace(',','')+' ' elif line.find('REMARKS')!=-1: printer2 = True remstring = '' elif (printer2 and len(line.strip())>0): remstring = remstring+line.replace(',','').replace('\n',' ') elif line.find('WEATHER PARAMETERS')!=-1: printer3 = True elif (printer3 and len(line.strip())>0): try: split = line.split(':') #print line #print split loadstring=loadstring+split[0].strip()+':' num = split[1].split(',') outnum = 0 if num[0]=='1': outnum+=4 if num[1]=='1': outnum+=2 if num[2]=='1': outnum+=1 loadstring=loadstring+str(outnum)+',' except IndexError: print x continue elif len(line.strip())==0: if printer1: loadstring=loadstring+',' printer1 = False elif printer2: printer2 = False loadstring = loadstring+remstring+',' elif printer3: printer3 = False break outfile.write(loadstring[:-1]+'\n') outfile.close() except IOError: pass inc+=1