"""parseWXTFiles.py Parses data from Vaisala WXT saved on the PSD data system. Data lines have the following formats: 0010870 1R0,Dm=022D,Sm=6.4M,Ta=26.1C,Ua=65.6P,Pa=1014.6H,Rc=0.00M,Rd=0s,Ri=0.0M,Hc=0.0M,Hd=0s,Hi=0.0M,Th=27.5C,Vh=24.1N wdir wspd Tair RH Pmb cum_rain dur mm/hr cum_hail dur intens Theat status 0011675 1R1,Dn=022D,Dm=022D,Dx=022D,Sn=6.3M,Sm=6.3M,Sx=6.4M wdirMax wdir wdirMin wspdMin wspd wspdMax 0011699 1R2,Ta=26.1C,Ua=65.6P,Pa=1014.6H Tair RH Pmb 0005873 1R3,Rc=0.00M,Rd=0s,Ri=0.0M,Hc=0.0M,Hd=0s,Hi=0.0M cum_rain dur mm/hr cum_hail dur intens 0007920 1R5,Th=27.5C,Vh=24.1N,Vs=24.4V,Vr=3.501V Theat status supplyV refV Fields from 1R0 lines are parsed and saved to a single output file. source file names follow the convention: met3yydddhh_raw.txt output file names follow the convention: wxt0yydddhh_raw.txt Byron Blomquist, NOAA/ESRL/PSD, Aug 2016 """ import os import string # assume current directory contains raw met3 files, # dump list of data file names into a list variable files = [f for f in os.listdir('.') if f.startswith('met3')] # parse data files and save into txt files for file in files: # open output file, new name begins with wxt0 outp = 'wxt0'+file[4:] fwxt = open(outp, 'w') print file fin = open(file) for line in fin: cleanLine = line.strip() fields = cleanLine.split(',') if '1R0' in fields[0]: # select PSD timecode from field[0] lineOut = fields[0][0:7]+',' raw = fields[1:] # remaining fields # strip all alphabet and '=' characters data = [y.strip('=#'+string.ascii_letters) for y in raw] for item in data: # concatenate numeric fields to output lineOut += item + ',' printLine = lineOut[0:-1] + '\n' # add return character fwxt.write(printLine) print outp+' finished' fwxt.close() # end