1 | import numpy as np |
---|
2 | from netCDF4 import Dataset as NetCDFFile |
---|
3 | import os |
---|
4 | import re |
---|
5 | from optparse import OptionParser |
---|
6 | import numpy.ma as ma |
---|
7 | |
---|
8 | # version |
---|
9 | version=1.1 |
---|
10 | |
---|
11 | # Filling values for floats, integer and string |
---|
12 | fillValueF = 1.e20 |
---|
13 | fillValueI = -99999 |
---|
14 | fillValueS = '---' |
---|
15 | |
---|
16 | # Length of the string variables |
---|
17 | StringLength = 200 |
---|
18 | |
---|
19 | # Size of the map for the complementary variables/maps |
---|
20 | Ndim2D = 100 |
---|
21 | |
---|
22 | main = 'create_OBSnetcdf.py' |
---|
23 | errormsg = 'ERROR -- error -- ERROR -- error' |
---|
24 | warnmsg = 'WARNING -- warning -- WARNING -- warning' |
---|
25 | |
---|
26 | fillValue = 1.e20 |
---|
27 | |
---|
28 | def searchInlist(listname, nameFind): |
---|
29 | """ Function to search a value within a list |
---|
30 | listname = list |
---|
31 | nameFind = value to find |
---|
32 | >>> searInlist(['1', '2', '3', '5'], '5') |
---|
33 | True |
---|
34 | """ |
---|
35 | for x in listname: |
---|
36 | if x == nameFind: |
---|
37 | return True |
---|
38 | return False |
---|
39 | |
---|
40 | |
---|
41 | def set_attribute(ncvar, attrname, attrvalue): |
---|
42 | """ Sets a value of an attribute of a netCDF variable. Removes previous attribute value if exists |
---|
43 | ncvar = object netcdf variable |
---|
44 | attrname = name of the attribute |
---|
45 | attrvalue = value of the attribute |
---|
46 | """ |
---|
47 | import numpy as np |
---|
48 | from netCDF4 import Dataset as NetCDFFile |
---|
49 | |
---|
50 | attvar = ncvar.ncattrs() |
---|
51 | if searchInlist(attvar, attrname): |
---|
52 | attr = ncvar.delncattr(attrname) |
---|
53 | |
---|
54 | attr = ncvar.setncattr(attrname, attrvalue) |
---|
55 | |
---|
56 | return ncvar |
---|
57 | |
---|
58 | def basicvardef(varobj, vstname, vlname, vunits): |
---|
59 | """ Function to give the basic attributes to a variable |
---|
60 | varobj= netCDF variable object |
---|
61 | vstname= standard name of the variable |
---|
62 | vlname= long name of the variable |
---|
63 | vunits= units of the variable |
---|
64 | """ |
---|
65 | attr = varobj.setncattr('standard_name', vstname) |
---|
66 | attr = varobj.setncattr('long_name', vlname) |
---|
67 | attr = varobj.setncattr('units', vunits) |
---|
68 | |
---|
69 | return |
---|
70 | |
---|
71 | ####### ###### ##### #### ### ## # |
---|
72 | |
---|
73 | fold='/media/ExtDiskD/DATA/obs/HyMeX/IOP15/HyMeX_rainfal/V2' |
---|
74 | |
---|
75 | lonvals = [] |
---|
76 | latvals = [] |
---|
77 | |
---|
78 | for day in range(19,23): |
---|
79 | if day == 22: |
---|
80 | Nhour = 1 |
---|
81 | else: |
---|
82 | Nhour = 24 |
---|
83 | for hour in range(0,Nhour): |
---|
84 | print day, hour |
---|
85 | filen = fold + '/geo_v2_201210' + str(day) + '{:02d}'.format(hour) + '_RR1.nc' |
---|
86 | onc = NetCDFFile(filen, 'r') |
---|
87 | opr = onc.variables['pracc'] |
---|
88 | olon = onc.variables['lon'] |
---|
89 | olat = onc.variables['lat'] |
---|
90 | |
---|
91 | lonv = olon[:] |
---|
92 | latv = olat[:] |
---|
93 | |
---|
94 | Npts = len(lonv) |
---|
95 | if day == 19 and hour == 0: |
---|
96 | Nptsorig = Npts |
---|
97 | lonvals = list(lonv) |
---|
98 | latvals = list(latv) |
---|
99 | heights = onc.variables['height'][:] |
---|
100 | praccsum = np.zeros((Npts), dtype=np.float) |
---|
101 | NOTfound = np.zeros((Npts), dtype=bool) |
---|
102 | nNOTfound = 0 |
---|
103 | |
---|
104 | found = np.zeros((Nptsorig), dtype=bool) |
---|
105 | |
---|
106 | nNotfounddate = 0 |
---|
107 | for ip in range(Npts): |
---|
108 | if searchInlist(lonvals, lonv[ip]) and searchInlist(latvals, latv[ip]): |
---|
109 | il = lonvals.index(lonv[ip]) |
---|
110 | if lonvals[il] == lonv[ip] and latvals[il] == latv[ip]: |
---|
111 | praccsum[il] = praccsum[il] + opr[ip] |
---|
112 | found[il] = True |
---|
113 | # print lonv[ip], latv[ip], ':', lonvals[il], latvals[il] |
---|
114 | else: |
---|
115 | # print 'Not in the first file!' |
---|
116 | nNotfounddate = nNotfounddate + 1 |
---|
117 | |
---|
118 | print 'On',day,'at',hour,'not found;',nNotfounddate,'stations from',Npts |
---|
119 | for ip in range(Nptsorig): |
---|
120 | if not found[ip] and not NOTfound[ip]: |
---|
121 | NOTfound[ip] = True |
---|
122 | nNOTfound = nNOTfound + 1 |
---|
123 | |
---|
124 | onc.close() |
---|
125 | |
---|
126 | print 'From originally',Nptsorig,'there are not continuosly records from:',nNOTfound |
---|
127 | #finalaccum = ma.masked_array(praccsum) |
---|
128 | for ip in range(Nptsorig): |
---|
129 | if NOTfound[ip]: |
---|
130 | praccsum[ip] = fillValueF |
---|
131 | |
---|
132 | # File creation |
---|
133 | newnc = NetCDFFile('accum.nc', 'w') |
---|
134 | |
---|
135 | # Dimensions definition |
---|
136 | newdim = newnc.createDimension('Npts',Nptsorig) |
---|
137 | |
---|
138 | # Variable creation |
---|
139 | newvar = newnc.createVariable('lon','f8',('Npts')) |
---|
140 | basicvardef(newvar, 'lon', 'Longitudes', 'degrees_East') |
---|
141 | newvar[:] = lonvals |
---|
142 | set_attribute(newvar,'axis','X') |
---|
143 | |
---|
144 | newvar = newnc.createVariable('lat','f8',('Npts')) |
---|
145 | basicvardef(newvar, 'lat', 'Latitudes', 'degrees_North') |
---|
146 | newvar[:] = latvals |
---|
147 | set_attribute(newvar,'axis','Y') |
---|
148 | |
---|
149 | # Total accumulation |
---|
150 | newvar = newnc.createVariable('pracc','f',('Npts'),fill_value=fillValueF) |
---|
151 | basicvardef(newvar, 'pracc', 'accumulated precipitation', 'kgm-2') |
---|
152 | newvar[:] = praccsum |
---|
153 | set_attribute(newvar,'coordinates','lon lat') |
---|
154 | |
---|
155 | # Global attributes |
---|
156 | ## |
---|
157 | filen = fold + '/geo_v2_2012101900_RR1.nc' |
---|
158 | onc = NetCDFFile(filen, 'r') |
---|
159 | |
---|
160 | attrs = onc.ncattrs() |
---|
161 | |
---|
162 | for attr in attrs: |
---|
163 | attrv = onc.getncattr(attr) |
---|
164 | set_attribute(newnc, attr, attrv) |
---|
165 | |
---|
166 | onc.close() |
---|
167 | |
---|
168 | newnc.sync() |
---|
169 | newnc.close() |
---|