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 -s 20121018000000 |
---|
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 | parser.add_option("-s", "--SimulationStartTime", dest="stime", |
---|
22 | help="Starting time of the simulation ([YYYY][MM][DD][HH][MI][SS] format)", metavar="DATE") |
---|
23 | |
---|
24 | (opts, args) = parser.parse_args() |
---|
25 | |
---|
26 | |
---|
27 | tsvn = ['t', 'q', 'u', 'v', 'psfc', 'glw', 'gsw', 'hfx', 'lh', 'tsk', 'tslb1', 'rainc', 'rainnc', 'clw'] |
---|
28 | |
---|
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'] |
---|
30 | |
---|
31 | tsvu = ['K', 'kg/kg', 'm/s', 'm/s', 'Pa', 'W/m2', 'W/m2', 'W/m2', 'W/m2', 'K', 'K', 'mm', 'mm', '1'] |
---|
32 | |
---|
33 | ####### ####### |
---|
34 | ## MAIN |
---|
35 | ####### |
---|
36 | |
---|
37 | ofile = 'ts.nc' |
---|
38 | Ntsvariables = len(tsvn) |
---|
39 | |
---|
40 | if not os.path.isfile(opts.lfile): |
---|
41 | print errormsg |
---|
42 | print ' ' + main + ': Time-Series ASCII text file "' + opts.lfile + \ |
---|
43 | '" does not exist !!' |
---|
44 | print errormsg |
---|
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] |
---|
55 | |
---|
56 | objlfile = open(opts.lfile, 'r') |
---|
57 | |
---|
58 | objofile = NetCDFFile(ofile, 'w') |
---|
59 | |
---|
60 | # Creation of dimensions |
---|
61 | ## |
---|
62 | objofile.createDimension('time',None) |
---|
63 | |
---|
64 | ncvar.set_attribute(objofile, 'author', 'Lluis Fita Borrell') |
---|
65 | ncvar.set_attribute(objofile, 'institution', 'Laboratoire Meteorologique Dynamique') |
---|
66 | ncvar.set_attribute(objofile, 'university', 'University Pierre et Marie Curie') |
---|
67 | ncvar.set_attribute(objofile, 'center', 'Centre national de la recherche scientifique') |
---|
68 | ncvar.set_attribute(objofile, 'country', 'France') |
---|
69 | ncvar.set_attribute(objofile, 'city', 'Paris') |
---|
70 | ncvar.set_attribute(objofile, 'script', 'TS_ASCII_netCFD.py') |
---|
71 | ncvar.set_attribute(objofile, 'version', '1.0') |
---|
72 | |
---|
73 | time_step = [] |
---|
74 | psfc = [] |
---|
75 | rainc = [] |
---|
76 | rainnc = [] |
---|
77 | drydens = [] |
---|
78 | |
---|
79 | tsvals = {} |
---|
80 | |
---|
81 | iline=0 |
---|
82 | itz = 0 |
---|
83 | for line in objlfile: |
---|
84 | values = ncvar.reduce_spaces(line) |
---|
85 | # print iline, values[0], dimz, Searchdimz |
---|
86 | # Writting general information |
---|
87 | if iline == 0: |
---|
88 | newvar = objofile.createVariable('station','c') |
---|
89 | valuespar=line.split('(') |
---|
90 | |
---|
91 | sec = ncvar.reduce_spaces(valuespar[0]) |
---|
92 | Nsec = len(sec) |
---|
93 | ncvar.set_attribute(newvar, 'name', ncvar.numVector_String(sec[0:Nsec-3],' ')) |
---|
94 | ncvar.set_attribute(newvar, 'acronym',sec[Nsec-1]) |
---|
95 | |
---|
96 | sec = ncvar.reduce_spaces(valuespar[1]) |
---|
97 | Nsec = len(sec) |
---|
98 | ncvar.set_attribute(newvar, 'real_lon', \ |
---|
99 | np.float(sec[1].replace(',','').replace('(','').replace(')','')) ) |
---|
100 | ncvar.set_attribute(newvar, 'real_lat', \ |
---|
101 | np.float(sec[0].replace(',','').replace('(','').replace(')','')) ) |
---|
102 | |
---|
103 | sec = ncvar.reduce_spaces(valuespar[2]) |
---|
104 | Nsec = len(sec) |
---|
105 | ncvar.set_attribute(newvar, 'x_grid_point', \ |
---|
106 | int(sec[0].replace(',','').replace('(','').replace(')','')) ) |
---|
107 | ncvar.set_attribute(newvar, 'y_grid_point', \ |
---|
108 | int(sec[1].replace(',','').replace('(','').replace(')','')) ) |
---|
109 | |
---|
110 | sec = ncvar.reduce_spaces(valuespar[3]) |
---|
111 | Nsec = len(sec) |
---|
112 | ncvar.set_attribute(newvar, 'model_lon', \ |
---|
113 | np.float(sec[1].replace(',','').replace('(','').replace(')','')) ) |
---|
114 | ncvar.set_attribute(newvar, 'model_lat', \ |
---|
115 | np.float(sec[0].replace(',','').replace('(','').replace(')','')) ) |
---|
116 | ncvar.set_attribute(newvar, 'model_height', \ |
---|
117 | np.float(sec[2].replace(',','').replace('(','').replace(')','')) ) |
---|
118 | simstarttime = refdate |
---|
119 | else: |
---|
120 | tsvals[itz] = values |
---|
121 | time_step.append(np.float(values[1])) |
---|
122 | itz = itz + 1 |
---|
123 | iline = iline + 1 |
---|
124 | |
---|
125 | dimt = len(time_step) |
---|
126 | |
---|
127 | print ' Found:',dimt,'time steps' |
---|
128 | objlfile.close() |
---|
129 | |
---|
130 | time_stepv = np.zeros((dimt), dtype=np.float) |
---|
131 | tsvaluesv = np.zeros( (dimt,Ntsvariables), dtype= np.float) |
---|
132 | |
---|
133 | pracc = np.zeros((dimt), dtype=np.float) |
---|
134 | |
---|
135 | itz = 0 |
---|
136 | for it in range(dimt): |
---|
137 | time_stepv[it] = np.float(time_step[it]) |
---|
138 | |
---|
139 | for iv in range(Ntsvariables): |
---|
140 | tsvaluesv[it,iv] = np.float(tsvals[itz][iv+5]) |
---|
141 | |
---|
142 | pracc[it] = np.float(tsvals[it][16]) + np.float(tsvals[it][17]) |
---|
143 | itz = itz + 1 |
---|
144 | |
---|
145 | # time |
---|
146 | newvar = objofile.createVariable('time','f8',('time')) |
---|
147 | newvar[:] = time_stepv*3600. |
---|
148 | newattr = ncvar.basicvardef(newvar, 'time', 'time', 'seconds since ' + \ |
---|
149 | simstarttime.replace('_',' ')) |
---|
150 | ncvar.set_attribute(newvar, 'calendar', 'standard') |
---|
151 | |
---|
152 | # time-series variables |
---|
153 | for iv in range(Ntsvariables): |
---|
154 | newvar = objofile.createVariable(tsvn[iv], 'f4', ('time')) |
---|
155 | newvar[:] = tsvaluesv[:,iv] |
---|
156 | newattr = ncvar.basicvardef(newvar, tsvn[iv], tsvln[iv], tsvu[iv] ) |
---|
157 | |
---|
158 | # Extra vars |
---|
159 | |
---|
160 | # pr |
---|
161 | varvals = np.zeros((dimt), dtype=np.float) |
---|
162 | varvals[1:dimt] = pracc[1:dimt] - pracc[0:dimt-1] |
---|
163 | |
---|
164 | newvar = objofile.createVariable('pr', 'f4', ('time')) |
---|
165 | newvar[:] = varvals |
---|
166 | newattr = ncvar.basicvardef(newvar, 'pr', 'precipitation', 'mm' ) |
---|
167 | |
---|
168 | objofile.sync() |
---|
169 | objofile.close() |
---|
170 | |
---|
171 | print 'Successfull generation of Time-Series netCDF file "' + ofile + '" !!!!!' |
---|