1 | ### A. Colaitis -- LMD -- 11/11/2011 |
---|
2 | |
---|
3 | def make_gcm_netcdf (zfilename="filename.nc", \ |
---|
4 | zdescription=None, \ |
---|
5 | zlon=None, \ |
---|
6 | zlat=None, \ |
---|
7 | zalt=None, \ |
---|
8 | ztime=None, \ |
---|
9 | zvariables=None, \ |
---|
10 | znames=None): |
---|
11 | |
---|
12 | from os import system |
---|
13 | from netCDF4 import Dataset |
---|
14 | import numpy as np |
---|
15 | |
---|
16 | ##################### |
---|
17 | ## Run some checks ## |
---|
18 | ##################### |
---|
19 | |
---|
20 | # Number of variables and number of variables' names |
---|
21 | |
---|
22 | nvar = len(zvariables) |
---|
23 | nnames = len(znames) |
---|
24 | |
---|
25 | if nnames < nvar: |
---|
26 | print "not enough variables name specified, using automatic labbeling" |
---|
27 | i=0 |
---|
28 | zznames=[[]]*nvar |
---|
29 | for zvar in zvariables: |
---|
30 | zznames[i]="var"+str(i+1) |
---|
31 | i=i+1 |
---|
32 | |
---|
33 | elif nnames > nvar: |
---|
34 | print "Warning, more variables than names are specified." |
---|
35 | i=0 |
---|
36 | zznames=[[]]*nvar |
---|
37 | for zvar in zvariables: |
---|
38 | zznames[i]=znames[i] |
---|
39 | i=i+1 |
---|
40 | else: |
---|
41 | zznames=znames[:] |
---|
42 | |
---|
43 | # Dimensions |
---|
44 | |
---|
45 | nx,ny,nz,nt = 0,0,0,0 |
---|
46 | |
---|
47 | if zlon is not None: |
---|
48 | nx=len(zlon) |
---|
49 | if zlat is not None: |
---|
50 | ny=len(zlat) |
---|
51 | if zalt is not None: |
---|
52 | nz=len(zalt) |
---|
53 | if ztime is not None: |
---|
54 | nt=len(ztime) |
---|
55 | |
---|
56 | zdims={} |
---|
57 | zdims['longitude']=nx |
---|
58 | zdims['latitude']=ny |
---|
59 | zdims['altitude']=nz |
---|
60 | zdims['Time']=nt |
---|
61 | print zdims |
---|
62 | # Find which variable uses which dimensions |
---|
63 | |
---|
64 | i=0 |
---|
65 | zvarcarac={} |
---|
66 | for zvar in zvariables: |
---|
67 | zvardim=np.array(zvar).shape |
---|
68 | ndim=len(zvardim) |
---|
69 | zzvardim=[[]]*ndim |
---|
70 | j=0 |
---|
71 | for dim in zvardim: |
---|
72 | if dim not in zdims.values(): |
---|
73 | print "WARNING -----------------------------" |
---|
74 | print "Dimensions given to subroutine do not match variables dimensions :" |
---|
75 | print "Dimensions: ",zdims |
---|
76 | print "Variable: ",zznames[i],", dimensions: ",zvardim |
---|
77 | print " PROGRAM EXIT" |
---|
78 | exit() |
---|
79 | else: |
---|
80 | a=get_key(zdims,dim) |
---|
81 | if len(a) is not 1: |
---|
82 | if j is 0: ##this should solve most conflicts with Time |
---|
83 | zzvardim[j]=a[1] |
---|
84 | elif j is 3: |
---|
85 | zzvardim[j]=a[1] |
---|
86 | else: |
---|
87 | zzvardim[j]=a[0] |
---|
88 | else: |
---|
89 | zzvardim[j]=a[0] |
---|
90 | j=j+1 |
---|
91 | zvarcarac[zznames[i]]=zzvardim |
---|
92 | i=i+1 |
---|
93 | |
---|
94 | print "creating "+zfilename |
---|
95 | |
---|
96 | print zvarcarac |
---|
97 | |
---|
98 | ######################### |
---|
99 | ## Clean previous file ## |
---|
100 | ######################### |
---|
101 | |
---|
102 | system("rm -f "+zfilename) |
---|
103 | |
---|
104 | ## Open file |
---|
105 | file = Dataset(zfilename, 'w', format='NETCDF3_64BIT') #netcdf4 does not work with ncview and ncdump yet |
---|
106 | if zdescription is not None: |
---|
107 | file.description = zdescription |
---|
108 | ## Dimensions |
---|
109 | if ztime is not None: |
---|
110 | file.createDimension('Time', None) |
---|
111 | if zalt is not None: |
---|
112 | file.createDimension('altitude',nz) |
---|
113 | if zlat is not None: |
---|
114 | file.createDimension('latitude',ny) |
---|
115 | if zlon is not None: |
---|
116 | file.createDimension('longitude',nx) |
---|
117 | ## Variables for dimensions & Data assignment |
---|
118 | if ztime is not None: |
---|
119 | times = file.createVariable('Time', 'f', ('Time',)) |
---|
120 | times[:] = ztime[:] |
---|
121 | if zalt is not None: |
---|
122 | altitudes = file.createVariable('altitude', 'f', ('altitude',)) |
---|
123 | altitudes[:] = zalt[:] |
---|
124 | if zlat is not None: |
---|
125 | latitudes = file.createVariable('latitude', 'f', ('latitude',)) |
---|
126 | latitudes[:] = zlat[:] |
---|
127 | if zlon is not None: |
---|
128 | longitudes = file.createVariable('longitude', 'f', ('longitude',)) |
---|
129 | longitudes[:] = zlon[:] |
---|
130 | ## Other Variables Creation & Data assignment |
---|
131 | i=0 |
---|
132 | for name in zznames: |
---|
133 | za = file.createVariable(name, 'f', tuple(zvarcarac[name])) |
---|
134 | if len(zvarcarac[name]) is 1: |
---|
135 | za[:] = zvariables[i][:] |
---|
136 | elif len(zvarcarac[name]) is 2: |
---|
137 | za[:,:] = zvariables[i][:,:] |
---|
138 | elif len(zvarcarac[name]) is 3: |
---|
139 | za[:,:,:] = zvariables[i][:,:,:] |
---|
140 | elif len(zvarcarac[name]) is 4: |
---|
141 | za[:,:,:,:] = zvariables[i][:,:,:,:] |
---|
142 | i=i+1 |
---|
143 | ## close file |
---|
144 | file.close() |
---|
145 | |
---|
146 | print "closing "+zfilename |
---|
147 | |
---|
148 | return |
---|
149 | |
---|
150 | def find_key(dic, val): |
---|
151 | """return the key of dictionary dic given the value""" |
---|
152 | return [k for k, v in dic.iteritems() if v == val][0] |
---|
153 | |
---|
154 | def get_key(self, value): |
---|
155 | """find the key(s) as a list given a value""" |
---|
156 | return [item[0] for item in self.items() if item[1] == value] |
---|