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 |
---|
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 = 'TS_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", "--TS_file", dest="lfile", |
---|
20 | help="Time Series ASCII text file to use", metavar="FILE") |
---|
21 | |
---|
22 | (opts, args) = parser.parse_args() |
---|
23 | |
---|
24 | |
---|
25 | tsvn = ['t', 'q', 'u', 'v', 'psfc', 'glw', 'gsw', 'hfx', 'lh', 'tsk', 'tslb1', 'rainc', 'rainnc', 'clw'] |
---|
26 | |
---|
27 | 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 | |
---|
29 | tsvu = ['K', 'kg/kg', 'm/s', 'm/s', 'Pa', 'W/m2', 'W/m2', 'W/m2', 'W/m2', 'K', 'K', 'mm', 'mm', 1'] |
---|
30 | |
---|
31 | ####### ####### |
---|
32 | ## MAIN |
---|
33 | ####### |
---|
34 | |
---|
35 | ofile = 'ts.nc' |
---|
36 | Ntsvariables = len(tsvn) |
---|
37 | |
---|
38 | if not os.path.isfile(opts.lfile): |
---|
39 | print errormsg |
---|
40 | print ' Time-Series ASCII text file "' + opts.lfile + '" does not exist !!' |
---|
41 | print errormsg |
---|
42 | quit() |
---|
43 | |
---|
44 | objlfile = open(opts.lfile, 'r') |
---|
45 | |
---|
46 | objofile = NetCDFFile(ofile, 'w') |
---|
47 | |
---|
48 | # Creation of dimensions |
---|
49 | ## |
---|
50 | objofile.createDimension('time',None) |
---|
51 | |
---|
52 | ncvar.set_attribute(objofile, 'author', 'Lluis Fita Borrell') |
---|
53 | ncvar.set_attribute(objofile, 'institution', 'Laboratoire Meteorologique Dynamique') |
---|
54 | ncvar.set_attribute(objofile, 'university', 'University Pierre et Marie Curie') |
---|
55 | ncvar.set_attribute(objofile, 'center', 'Centre national de la recherche scientifique') |
---|
56 | ncvar.set_attribute(objofile, 'country', 'France') |
---|
57 | ncvar.set_attribute(objofile, 'city', 'Paris') |
---|
58 | ncvar.set_attribute(objofile, 'script', 'TS_ASCII_netCFD.py') |
---|
59 | ncvar.set_attribute(objofile, 'version', '1.0') |
---|
60 | |
---|
61 | time_step = [] |
---|
62 | psfc = [] |
---|
63 | rainc = [] |
---|
64 | rainnc = [] |
---|
65 | drydens = [] |
---|
66 | |
---|
67 | tsvals = {} |
---|
68 | |
---|
69 | iline=0 |
---|
70 | itz = 0 |
---|
71 | for line in objlfile: |
---|
72 | values = ncvar.reduce_spaces(line) |
---|
73 | # print iline, values[0], dimz, Searchdimz |
---|
74 | # Writting general information |
---|
75 | if iline == 0: |
---|
76 | newvar = objofile.createVariable('station','c') |
---|
77 | ncvar.set_attribute(newvar, 'name',values[0]) |
---|
78 | ncvar.set_attribute(newvar, 'acronym',values[3]) |
---|
79 | ncvar.set_attribute(newvar, 'real_lon', \ |
---|
80 | np.float(values[6].replace(',','').replace('(','').replace(')','')) ) |
---|
81 | ncvar.set_attribute(newvar, 'real_lat', \ |
---|
82 | np.float(values[5].replace(',','').replace('(','').replace(')','')) ) |
---|
83 | ncvar.set_attribute(newvar, 'x_grid_point', \ |
---|
84 | int(values[8].replace(',','').replace('(','').replace(')','')) ) |
---|
85 | ncvar.set_attribute(newvar, 'y_grid_point', \ |
---|
86 | int(values[9].replace(',','').replace('(','').replace(')','')) ) |
---|
87 | ncvar.set_attribute(newvar, 'model_lon', \ |
---|
88 | np.float(values[12].replace(',','').replace('(','').replace(')','')) ) |
---|
89 | ncvar.set_attribute(newvar, 'model_lat', \ |
---|
90 | np.float(values[11].replace(',','').replace('(','').replace(')','')) ) |
---|
91 | ncvar.set_attribute(newvar, 'model_height', \ |
---|
92 | np.float(values[13].replace(',','').replace('(','').replace(')','')) ) |
---|
93 | simstarttime = values[18] |
---|
94 | else: |
---|
95 | tsvals[itz] = values |
---|
96 | iline = iline + 1 |
---|
97 | |
---|
98 | dimt = len(time_step) |
---|
99 | |
---|
100 | print ' Found:',dimt,'time steps, over:',dimz,'vertical levels' |
---|
101 | objlfile.close() |
---|
102 | |
---|
103 | objofile.createDimension('z',dimz) |
---|
104 | |
---|
105 | 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) |
---|
112 | |
---|
113 | itz = 0 |
---|
114 | for it in range(dimt): |
---|
115 | 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 | |
---|
121 | for iz in range(dimz): |
---|
122 | for iv in range(Ntsvariables): |
---|
123 | tsvaluesv[it,iz,iv] = np.float(tsvals[itz][iv+1]) |
---|
124 | |
---|
125 | itz = itz + 1 |
---|
126 | # Surface variables |
---|
127 | newvar = objofile.createVariable('time','f4',('time',)) |
---|
128 | newvar[:] = time_stepv |
---|
129 | newattr = ncvar.basicvardef(newvar, 'time', 'time', 'hours since ' + \ |
---|
130 | simstarttime.replace('_',' ')) |
---|
131 | |
---|
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 |
---|
151 | for iv in range(Ntsvariables): |
---|
152 | newvar = objofile.createVariable(tsvn[iv], 'f4', ('time','z')) |
---|
153 | newvar[:] = tsvaluesv[:,:,iv] |
---|
154 | newattr = ncvar.basicvardef(newvar, tsvn[iv], tsvln[iv], tsvu[iv] ) |
---|
155 | |
---|
156 | objofile.sync() |
---|
157 | objofile.close() |
---|
158 | |
---|
159 | print 'Successfull generation of LIDAR netCDF file "' + ofile + '" !!!!!' |
---|