"""parse_Dship.py Parses data from Dship 1 min extraction file and saves to daily files. User enters Dship extract file name on line 17 below. Dship extract file and this py script should be in the current dir. Output file names follow the convention: dsp0yyddd_raw.txt Byron Blomquist, NOAA/ESRL/PSD, Nov 2019 """ import os import string idx = [] fin = open('Oct15_Nov3_ShipNavMet.txt') # assume this is in current directory # vars = list of all variable names vars = fin.readline() vars = vars.strip() vars = vars.split('\t') # idx = list of column indices for variables of interest idx.append(vars.index('date time')) #0 idx.append(vars.index('TRIMB_2.PTNL.latitude')) #1 idx.append(vars.index('TRIMB_2.PTNL.longitude')) #2 idx.append(vars.index('TRIMB_2.GPVTG.speed')) #3 idx.append(vars.index('TRIMB_2.GPVTG.course')) #4 idx.append(vars.index('MAGNETICS.PSMAG.gyro_heading')) #5 idx.append(vars.index('MAGNETICS.PSMAG.gyro_pitch')) #6 idx.append(vars.index('MAGNETICS.PSMAG.gyro_roll')) #7 idx.append(vars.index('WEATHER.PAWIBWW.air_temperature')) #8 idx.append(vars.index('WEATHER.PAWIBWW.rel_humidity')) #9 idx.append(vars.index('WEATHER.PAWIBWW.dewpoint')) #10 idx.append(vars.index('WEATHER.PAWIBWW.air_pressure')) #11 idx.append(vars.index('WEATHER.PAWIBWW.direct_radiation')) #12 idx.append(vars.index('WEATHER.PAWIBWW.global_radiation')) #13 idx.append(vars.index('WEATHER.PAWIBWW.rel_wind_velocity')) #14 idx.append(vars.index('WEATHER.PAWIBWW.rel_wind_direction')) #15 idx.append(vars.index('WEATHER.PAWIBWW.true_wind_velocity')) #16 idx.append(vars.index('WEATHER.PAWIBWW.true_wind_direction')) #17 idx.append(vars.index('WEATHER.PAWIBWW.visibility')) #18 idx.append(vars.index('WEATHER.PAWIBWW.water_temperature')) #19 idx.append(vars.index('WEATHER.PAWIBWW.ceiling')) #20 # skip over next two lines in the file null = fin.readline() null = fin.readline() # format output lines and write to daily files DD_prior = '' first = 1 for line in fin: # remove troublesome characters line = line.strip() line = line.replace('∞','') line = line.replace('\'','') # split line into list of data values data = line.split('\t') # reformat lat l1 = data[idx[1]] l2 = l1.split() lat = float(l2[0]) + float(l2[1])/60 if l2[2] is 'S': lat = -lat data[idx[1]] = str(lat) # reformat lon l1 = data[idx[2]] l2 = l1.split() lon = float(l2[0]) + float(l2[1])/60 if l2[2] is 'W': lon = 360-lon data[idx[2]] = str(lon) DD = data[idx[0]][8:10] # day string MM = data[idx[0]][5:7] # month string YY = data[idx[0]][2:4] # 2 digit year string # decide if we need to open a daily output file if DD != DD_prior: if not first: fdsp.close() # close output file else: first = 0 DD_prior = data[idx[0]][8:10] # remember current day # open output file, new name begins with mdp0 outp = 'dsp0'+YY+MM+DD+'_raw.txt' print('file: ',outp) fdsp = open(outp, 'w') # write header line to file hdr = 'datetime,lat,lon,sog,cog,hed,pitch,roll,tair,rh,dpt,' hdr += 'p_mb,dir_rad,glb_rad,rwspd,rwdir,wspd,wdir,vis,tsea,ceil\n' fdsp.write(hdr) # format comma delimited output line and write to file lineOut = '' for ii in idx: lineOut += data[ii] + ',' lineOut = lineOut[0:-1] + '\n' # remove last comma, add line feed fdsp.write(lineOut) # end