"""parseWXTFiles.py Parses data from Vaisala WXT saved on the PSD data system. Data lines have the following formats: 0000906 2R0,Dm=271D,Sm=1.8M,Ta=29.3C,Ua=83.2P,Pa=1003.3H,Rc=0.00M,Rd=0s,Ri=0.0M,Hc=0.0M,Hd=0s,Hi=0.0M wdir wspd Tair RH Pmb cum_rain dur mm/hr cum_hail dur intens 0001716 2R1,Dm=269D,Sm=1.5M wdir wspd 0001742 2R2,Ta=29.3C,Ua=83.2P,Pa=1003.4H Tair RH Pmb 0007913 2R3,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 0009945 2R5,Th=28.9C,Vh=12.0#,Vs=12.2V,Vr=3.518V Theata status supplyV refV Fields from 2R0 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 2019 """ 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 '2R0' 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