[350] | 1 | # Python script to transfomr ASCII LIDAR outputs to netCDF |
---|
| 2 | ## g.e. # LIDAR_ASCII_netCDF.py -f /d4/lflmd/etudes/REMEMBER/WRFmeas/tests/WL_HyMeX/gir.d00.01DAR |
---|
| 3 | |
---|
| 4 | import os |
---|
| 5 | from optparse import OptionParser |
---|
| 6 | import numpy as np |
---|
| 7 | from netCDF4 import Dataset as NetCDFFile |
---|
| 8 | import nc_var_tools as ncvar |
---|
| 9 | |
---|
| 10 | main = 'LIDR_ASCII_netCDF.py' |
---|
| 11 | errormsg='ERROR -- error -- ERROR -- error' |
---|
| 12 | warnmsg='WARNING -- warning -- WARNING -- warning' |
---|
| 13 | |
---|
| 14 | fillValue = 1.e20 |
---|
| 15 | |
---|
| 16 | ####### ###### ##### #### ### ## # |
---|
| 17 | |
---|
| 18 | parser = OptionParser() |
---|
| 19 | parser.add_option("-f", "--LIDAR_file", dest="lfile", |
---|
| 20 | help="LIDAR ASCII text file to use", metavar="FILE") |
---|
| 21 | |
---|
| 22 | (opts, args) = parser.parse_args() |
---|
| 23 | |
---|
| 24 | lidarvn = ['z', 'p', 'u', 'v', 'w', 't_pot', 'qv', 'qc', 'qr', 'qs', 'qh', 'qi', \ |
---|
| 25 | 'qg', 'dens','cldfra'] |
---|
| 26 | lidarvln = ['height', 'pressure','x-wind direction', 'y-wind direction', \ |
---|
| 27 | 'z-wind direction', 'potential temperature', 'water vapor mixing ratio', \ |
---|
| 28 | 'cloud mixing ratio', 'rain mixing ratio', 'snow mixing ratio', \ |
---|
| 29 | 'hail mixing ratio', 'ice mixing ratio', 'graupel mixing ratio', 'air density', \ |
---|
| 30 | 'cloud fraction'] |
---|
| 31 | lidarvu = ['m', 'hPa', 'ms-1', 'ms-1', 'ms-1', 'K', 'kgkg-1', 'kgkg-1', 'kgkg-1', \ |
---|
| 32 | 'kgkg-1', 'kgkg-1', 'kgkg-1', 'kgkg-1', 'kg m-3','1'] |
---|
| 33 | |
---|
| 34 | ####### ####### |
---|
| 35 | ## MAIN |
---|
| 36 | ####### |
---|
| 37 | |
---|
| 38 | ofile = 'lidar.nc' |
---|
| 39 | Nlidarvariables = len(lidarvn) |
---|
| 40 | |
---|
| 41 | if not os.path.isfile(opts.lfile): |
---|
| 42 | print errormsg |
---|
| 43 | print ' LIDAR ASCII text file "' + opts.lfile + '" does not exist !!' |
---|
| 44 | print errormsg |
---|
| 45 | quit() |
---|
| 46 | |
---|
| 47 | objlfile = open(opts.lfile, 'r') |
---|
| 48 | |
---|
| 49 | objofile = NetCDFFile(ofile, 'w') |
---|
| 50 | |
---|
| 51 | # Creation of dimensions |
---|
| 52 | ## |
---|
| 53 | objofile.createDimension('time',None) |
---|
| 54 | |
---|
| 55 | ncvar.set_attribute(objofile, 'author', 'Lluis Fita Borrell') |
---|
| 56 | ncvar.set_attribute(objofile, 'institution', 'Laboratoire Meteorologique Dynamique') |
---|
| 57 | ncvar.set_attribute(objofile, 'university', 'University Pierre et Marie Curie') |
---|
| 58 | ncvar.set_attribute(objofile, 'center', 'Centre national de la recherche scientifique') |
---|
| 59 | ncvar.set_attribute(objofile, 'country', 'France') |
---|
| 60 | ncvar.set_attribute(objofile, 'city', 'Paris') |
---|
| 61 | ncvar.set_attribute(objofile, 'script', 'LIDAR_ASCII_netCFD.py') |
---|
| 62 | ncvar.set_attribute(objofile, 'version', '1.0') |
---|
| 63 | |
---|
| 64 | time_step = [] |
---|
| 65 | psfc = [] |
---|
| 66 | rainc = [] |
---|
| 67 | rainnc = [] |
---|
| 68 | drydens = [] |
---|
| 69 | |
---|
| 70 | lidarvals = {} |
---|
| 71 | |
---|
| 72 | iline=0 |
---|
| 73 | dimz = 0 |
---|
| 74 | Searchdimz = True |
---|
| 75 | itz = 0 |
---|
| 76 | |
---|
| 77 | for line in objlfile: |
---|
| 78 | values = ncvar.reduce_spaces(line) |
---|
| 79 | # print iline, values[0], dimz, Searchdimz |
---|
| 80 | # Writting general information |
---|
| 81 | if iline == 0: |
---|
| 82 | newvar = objofile.createVariable('station','c') |
---|
| 83 | ncvar.set_attribute(newvar, 'name',values[0]) |
---|
| 84 | ncvar.set_attribute(newvar, 'acronym',values[3]) |
---|
| 85 | ncvar.set_attribute(newvar, 'real_lon', \ |
---|
| 86 | np.float(values[6].replace(',','').replace('(','').replace(')','')) ) |
---|
| 87 | ncvar.set_attribute(newvar, 'real_lat', \ |
---|
| 88 | np.float(values[5].replace(',','').replace('(','').replace(')','')) ) |
---|
| 89 | ncvar.set_attribute(newvar, 'x_grid_point', \ |
---|
| 90 | int(values[8].replace(',','').replace('(','').replace(')','')) ) |
---|
| 91 | ncvar.set_attribute(newvar, 'y_grid_point', \ |
---|
| 92 | int(values[9].replace(',','').replace('(','').replace(')','')) ) |
---|
| 93 | ncvar.set_attribute(newvar, 'model_lon', \ |
---|
| 94 | np.float(values[12].replace(',','').replace('(','').replace(')','')) ) |
---|
| 95 | ncvar.set_attribute(newvar, 'model_lat', \ |
---|
| 96 | np.float(values[11].replace(',','').replace('(','').replace(')','')) ) |
---|
| 97 | ncvar.set_attribute(newvar, 'model_height', \ |
---|
| 98 | np.float(values[13].replace(',','').replace('(','').replace(')','')) ) |
---|
| 99 | simstarttime = values[18] |
---|
| 100 | else: |
---|
| 101 | if values[0] == 'new_time': |
---|
| 102 | time_step.append(np.float(values[2])) |
---|
| 103 | psfc.append(np.float(values[6])) |
---|
| 104 | rainc.append(np.float(values[7])) |
---|
| 105 | rainnc.append(np.float(values[8])) |
---|
| 106 | drydens.append(np.float(values[9])) |
---|
| 107 | if iline == 1: |
---|
| 108 | Searchdimz = True |
---|
| 109 | else: |
---|
| 110 | Searchdimz = False |
---|
| 111 | else: |
---|
| 112 | if not values[0] == 'k': |
---|
| 113 | lidarvals[itz] = values |
---|
| 114 | if Searchdimz: |
---|
| 115 | dimz = dimz + 1 |
---|
| 116 | itz = itz + 1 |
---|
| 117 | iline = iline + 1 |
---|
| 118 | |
---|
| 119 | dimt = len(time_step) |
---|
| 120 | |
---|
| 121 | print ' Found:',dimt,'time steps, over:',dimz,'vertical levels' |
---|
| 122 | objlfile.close() |
---|
| 123 | |
---|
| 124 | objofile.createDimension('z',dimz) |
---|
| 125 | |
---|
| 126 | time_stepv = np.zeros((dimt), dtype=np.float) |
---|
| 127 | psfcv = np.zeros((dimt), dtype=np.float) |
---|
| 128 | raincv = np.zeros((dimt), dtype=np.float) |
---|
| 129 | rainncv = np.zeros((dimt), dtype=np.float) |
---|
| 130 | drydensv = np.zeros((dimt), dtype=np.float) |
---|
| 131 | |
---|
| 132 | lidarvaluesv = np.zeros( (dimt,dimz,Nlidarvariables), dtype= np.float) |
---|
| 133 | |
---|
| 134 | itz = 0 |
---|
| 135 | for it in range(dimt): |
---|
| 136 | time_stepv[it] = np.float(time_step[it]) |
---|
| 137 | psfcv[it] = np.float(psfc[it]) |
---|
| 138 | raincv[it] = np.float(rainc[it]) |
---|
| 139 | rainncv[it] = np.float(rainnc[it]) |
---|
| 140 | drydensv[it] = np.float(drydens[it]) |
---|
| 141 | |
---|
| 142 | for iz in range(dimz): |
---|
| 143 | for iv in range(Nlidarvariables): |
---|
| 144 | lidarvaluesv[it,iz,iv] = np.float(lidarvals[itz][iv+1]) |
---|
| 145 | |
---|
| 146 | itz = itz + 1 |
---|
| 147 | # Surface variables |
---|
| 148 | newvar = objofile.createVariable('time','f4',('time',)) |
---|
| 149 | newvar[:] = time_stepv |
---|
| 150 | newattr = ncvar.basicvardef(newvar, 'time', 'time', 'hours since ' + \ |
---|
| 151 | simstarttime.replace('_',' ')) |
---|
| 152 | |
---|
| 153 | newvar = objofile.createVariable('psfc','f4',('time',)) |
---|
| 154 | newvar[:] = psfcv |
---|
| 155 | newattr = ncvar.basicvardef(newvar, 'psfc', 'surface pressure', 'hPa') |
---|
| 156 | |
---|
| 157 | newvar = objofile.createVariable('rainc','f4',('time',)) |
---|
| 158 | newvar[:] = raincv |
---|
| 159 | newattr = ncvar.basicvardef(newvar, 'rainc', \ |
---|
| 160 | 'accumulated precipitation from cumulus scheme', 'mm') |
---|
| 161 | |
---|
| 162 | newvar = objofile.createVariable('rainnc','f4',('time',)) |
---|
| 163 | newvar[:] = rainncv |
---|
| 164 | newattr = ncvar.basicvardef(newvar, 'rainnc', \ |
---|
| 165 | 'accumulated precipitation not from cumulus scheme', 'mm') |
---|
| 166 | |
---|
| 167 | newvar = objofile.createVariable('drydens','f4',('time',)) |
---|
| 168 | newvar[:] = drydensv |
---|
| 169 | newattr = ncvar.basicvardef(newvar, 'drydens', 'total dry air column pressure', 'hPa') |
---|
| 170 | |
---|
| 171 | # Lidar variables |
---|
| 172 | for iv in range(Nlidarvariables): |
---|
| 173 | newvar = objofile.createVariable(lidarvn[iv], 'f4', ('time','z')) |
---|
| 174 | newvar[:] = lidarvaluesv[:,:,iv] |
---|
| 175 | newattr = ncvar.basicvardef(newvar, lidarvn[iv], lidarvln[iv], lidarvu[iv] ) |
---|
| 176 | |
---|
| 177 | objofile.sync() |
---|
| 178 | objofile.close() |
---|
| 179 | |
---|
| 180 | print 'Successfull generation of LIDAR netCDF file "' + ofile + '" !!!!!' |
---|