Changeset 352 in lmdz_wrf for trunk


Ignore:
Timestamp:
Mar 10, 2015, 1:00:51 PM (10 years ago)
Author:
lfita
Message:

Working version of the script

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/TS_ASCII_netCDF.py

    r351 r352  
    11# 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
    33
    44import os
     
    1818parser = OptionParser()
    1919parser.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")
     21parser.add_option("-s", "--SimulationStartTime", dest="stime",
     22  help="Starting time of the simulation ([YYYY][MM][DD][HH][MI][SS] format)", metavar="DATE")
    2123
    2224(opts, args) = parser.parse_args()
     
    2729tsvln = ['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']
    2830
    29 tsvu = ['K', 'kg/kg', 'm/s', 'm/s', 'Pa', 'W/m2', 'W/m2', 'W/m2', 'W/m2', 'K', 'K', 'mm', 'mm', 1']
     31tsvu = ['K', 'kg/kg', 'm/s', 'm/s', 'Pa', 'W/m2', 'W/m2', 'W/m2', 'W/m2', 'K', 'K', 'mm', 'mm', '1']
    3032
    3133#######    #######
     
    3840if not os.path.isfile(opts.lfile):
    3941    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 !!'
    4144    print errormsg
    4245    quit()
     46
     47if opts.stime is None:
     48    print errormsg
     49    print '  ' + main + ': No initial date/time of the simulation is provided!'
     50    quit(-1)
     51else:
     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]
    4355
    4456objlfile = open(opts.lfile, 'r')
     
    91103        ncvar.set_attribute(newvar, 'model_height',                                  \
    92104          np.float(values[13].replace(',','').replace('(','').replace(')','')) )
    93         simstarttime = values[18]
     105        simstarttime = refdate
    94106    else:
    95107        tsvals[itz] = values
     108        time_step.append(np.float(values[1]))
     109        itz = itz + 1
    96110    iline = iline + 1
    97111
    98112dimt = len(time_step)
    99113
    100 print '  Found:',dimt,'time steps, over:',dimz,'vertical levels'
     114print '  Found:',dimt,'time steps'
    101115objlfile.close()
    102116
    103 objofile.createDimension('z',dimz)
    104 
    105117time_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)
     118tsvaluesv = np.zeros( (dimt,Ntsvariables), dtype= np.float)
    112119
    113120itz = 0
    114121for it in range(dimt):
    115122    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])
    120123
    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])
    124126
    125         itz = itz + 1
     127    itz = itz + 1
    126128# Surface variables
    127 newvar = objofile.createVariable('time','f4',('time',))
     129newvar = objofile.createVariable('time','f8',('time'))
    128130newvar[:] = time_stepv
    129131newattr = ncvar.basicvardef(newvar, 'time', 'time', 'hours since ' +                 \
    130132  simstarttime.replace('_',' '))
     133ncvar.set_attribute(newvar, 'calendar', 'standard')
    131134
    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
    151136for 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]
    154139    newattr = ncvar.basicvardef(newvar, tsvn[iv], tsvln[iv], tsvu[iv] )
    155140
     
    157142objofile.close()
    158143
    159 print 'Successfull generation of LIDAR netCDF file "' + ofile + '" !!!!!'
     144print 'Successfull generation of Time-Series netCDF file "' + ofile + '" !!!!!'
Note: See TracChangeset for help on using the changeset viewer.