Changeset 352 in lmdz_wrf for trunk/tools/TS_ASCII_netCDF.py
- Timestamp:
- Mar 10, 2015, 1:00:51 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/TS_ASCII_netCDF.py
r351 r352 1 1 # Python script to transfomr ASCII LIDAR outputs to netCDF 2 ## g.e. # TS_ASCII_netCDF.py -f //media/ExtDiskD/bkup/ciclad/etudes/WL_HyMeX/iop15/wrf/run/control/stations_20121018000000-20121022000000/h0001.d01.TS 2 ## g.e. # TS_ASCII_netCDF.py -f //media/ExtDiskD/bkup/ciclad/etudes/WL_HyMeX/iop15/wrf/run/control/stations_20121018000000-20121022000000/h0001.d01.TS -s 20121018000000 3 3 4 4 import os … … 18 18 parser = OptionParser() 19 19 parser.add_option("-f", "--TS_file", dest="lfile", 20 help="Time Series ASCII text file to use", metavar="FILE") 20 help="Time Series ASCII text file to use", metavar="FILE") 21 parser.add_option("-s", "--SimulationStartTime", dest="stime", 22 help="Starting time of the simulation ([YYYY][MM][DD][HH][MI][SS] format)", metavar="DATE") 21 23 22 24 (opts, args) = parser.parse_args() … … 27 29 tsvln = ['2 m Temperature', '2 m vapor mixing ratio', '10 m U wind (earth-relative)', '10 m V wind (earth-relative)', 'surface pressure', 'downward longwave radiation flux at the ground (downward is positive)', 'net shortwave radiation flux at the ground (downward is positive)', 'surface sensible heat flux (upward is positive)', 'surface latent heat flux (upward is positive)', 'skin temperature', 'top soil layer temperature', 'rainfall from a cumulus scheme', 'rainfall from an explicit scheme', 'total column-integrated water vapor and cloud variables'] 28 30 29 tsvu = ['K', 'kg/kg', 'm/s', 'm/s', 'Pa', 'W/m2', 'W/m2', 'W/m2', 'W/m2', 'K', 'K', 'mm', 'mm', 1']31 tsvu = ['K', 'kg/kg', 'm/s', 'm/s', 'Pa', 'W/m2', 'W/m2', 'W/m2', 'W/m2', 'K', 'K', 'mm', 'mm', '1'] 30 32 31 33 ####### ####### … … 38 40 if not os.path.isfile(opts.lfile): 39 41 print errormsg 40 print ' Time-Series ASCII text file "' + opts.lfile + '" does not exist !!' 42 print ' ' + main + ': Time-Series ASCII text file "' + opts.lfile + \ 43 '" does not exist !!' 41 44 print errormsg 42 45 quit() 46 47 if opts.stime is None: 48 print errormsg 49 print ' ' + main + ': No initial date/time of the simulation is provided!' 50 quit(-1) 51 else: 52 stime = opts.stime 53 refdate = stime[0:4] + '-' + stime[4:6] + '-' + stime[6:8] + ' ' + stime[8:10] + \ 54 ':' + stime[10:12] + ':' + stime[12:14] 43 55 44 56 objlfile = open(opts.lfile, 'r') … … 91 103 ncvar.set_attribute(newvar, 'model_height', \ 92 104 np.float(values[13].replace(',','').replace('(','').replace(')','')) ) 93 simstarttime = values[18]105 simstarttime = refdate 94 106 else: 95 107 tsvals[itz] = values 108 time_step.append(np.float(values[1])) 109 itz = itz + 1 96 110 iline = iline + 1 97 111 98 112 dimt = len(time_step) 99 113 100 print ' Found:',dimt,'time steps , over:',dimz,'vertical levels'114 print ' Found:',dimt,'time steps' 101 115 objlfile.close() 102 116 103 objofile.createDimension('z',dimz)104 105 117 time_stepv = np.zeros((dimt), dtype=np.float) 106 psfcv = np.zeros((dimt), dtype=np.float) 107 raincv = np.zeros((dimt), dtype=np.float) 108 rainncv = np.zeros((dimt), dtype=np.float) 109 drydensv = np.zeros((dimt), dtype=np.float) 110 111 tsvaluesv = np.zeros( (dimt,dimz,Ntsvariables), dtype= np.float) 118 tsvaluesv = np.zeros( (dimt,Ntsvariables), dtype= np.float) 112 119 113 120 itz = 0 114 121 for it in range(dimt): 115 122 time_stepv[it] = np.float(time_step[it]) 116 psfcv[it] = np.float(psfc[it])117 raincv[it] = np.float(rainc[it])118 rainncv[it] = np.float(rainnc[it])119 drydensv[it] = np.float(drydens[it])120 123 121 for iz in range(dimz): 122 for iv in range(Ntsvariables): 123 tsvaluesv[it,iz,iv] = np.float(tsvals[itz][iv+1]) 124 for iv in range(Ntsvariables): 125 tsvaluesv[it,iv] = np.float(tsvals[itz][iv+5]) 124 126 125 127 itz = itz + 1 126 128 # Surface variables 127 newvar = objofile.createVariable('time','f 4',('time',))129 newvar = objofile.createVariable('time','f8',('time')) 128 130 newvar[:] = time_stepv 129 131 newattr = ncvar.basicvardef(newvar, 'time', 'time', 'hours since ' + \ 130 132 simstarttime.replace('_',' ')) 133 ncvar.set_attribute(newvar, 'calendar', 'standard') 131 134 132 newvar = objofile.createVariable('psfc','f4',('time',)) 133 newvar[:] = psfcv 134 newattr = ncvar.basicvardef(newvar, 'psfc', 'surface pressure', 'hPa') 135 136 newvar = objofile.createVariable('rainc','f4',('time',)) 137 newvar[:] = raincv 138 newattr = ncvar.basicvardef(newvar, 'rainc', \ 139 'accumulated precipitation from cumulus scheme', 'mm') 140 141 newvar = objofile.createVariable('rainnc','f4',('time',)) 142 newvar[:] = rainncv 143 newattr = ncvar.basicvardef(newvar, 'rainnc', \ 144 'accumulated precipitation not from cumulus scheme', 'mm') 145 146 newvar = objofile.createVariable('drydens','f4',('time',)) 147 newvar[:] = drydensv 148 newattr = ncvar.basicvardef(newvar, 'drydens', 'total dry air column pressure', 'hPa') 149 150 # Lidar variables 135 # time-series variables 151 136 for iv in range(Ntsvariables): 152 newvar = objofile.createVariable(tsvn[iv], 'f4', ('time' ,'z'))153 newvar[:] = tsvaluesv[:, :,iv]137 newvar = objofile.createVariable(tsvn[iv], 'f4', ('time')) 138 newvar[:] = tsvaluesv[:,iv] 154 139 newattr = ncvar.basicvardef(newvar, tsvn[iv], tsvln[iv], tsvu[iv] ) 155 140 … … 157 142 objofile.close() 158 143 159 print 'Successfull generation of LIDARnetCDF file "' + ofile + '" !!!!!'144 print 'Successfull generation of Time-Series netCDF file "' + ofile + '" !!!!!'
Note: See TracChangeset
for help on using the changeset viewer.