source: trunk/UTIL/PYTHON/make_netcdf.py @ 402

Last change on this file since 402 was 395, checked in by acolaitis, 13 years ago

PYTHON

M 393 mymath.py
----------------- Cosmetic change

M 393 make_netcdf.py
----------------- Cosmetic change

M 393 planetoplot.py
----------------- corrected bug with varname and --tsat

M 393 myplot.py
----------------- added possibility for the script to read ncdf files with NaN value and treat them correctly without messing with the mathematical operations.

File size: 4.3 KB
Line 
1### A. Colaitis -- LMD -- 11/11/2011
2
3def 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"+np.string(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                 else:
85                    zzvardim[j]=a[0]
86              else:
87                 zzvardim[j]=a[0]
88              j=j+1
89       zvarcarac[zznames[i]]=zzvardim
90       i=i+1
91
92   print "creating "+zfilename
93
94   print zvarcarac 
95 
96   #########################
97   ## Clean previous file ##
98   #########################
99   
100   system("rm -f "+zfilename)
101   
102   ## Open file
103   file = Dataset(zfilename, 'w', format='NETCDF3_64BIT') #netcdf4 does not work with ncview and ncdump yet
104   if zdescription is not None:
105      file.description = zdescription
106   ## Dimensions
107   if ztime is not None:
108      file.createDimension('Time', None)
109   if zalt is not None:
110      file.createDimension('altitude',nz)
111   if zlat is not None:
112       file.createDimension('latitude',ny)
113   if zlon is not None:
114      file.createDimension('longitude',nx)
115   ## Variables for dimensions & Data assignment
116   if ztime is not None:
117      times = file.createVariable('Time', 'f', ('Time',))
118      times[:] = ztime[:]
119   if zalt is not None:
120      altitudes = file.createVariable('altitude', 'f', ('altitude',))
121      altitudes[:] = zalt[:]
122   if zlat is not None:
123      latitudes = file.createVariable('latitude', 'f', ('latitude',))
124      latitudes[:] = zlat[:]
125   if zlon is not None:
126      longitudes = file.createVariable('longitude', 'f', ('longitude',))
127      longitudes[:] = zlon[:]
128   ## Other Variables Creation & Data assignment
129   i=0
130   for name in zznames:
131       za = file.createVariable(name, 'f', tuple(zvarcarac[name]))
132       if len(zvarcarac[name]) is 1:
133          za[:] = zvariables[i][:]
134       elif len(zvarcarac[name]) is 2:
135          za[:,:] = zvariables[i][:,:]
136       elif len(zvarcarac[name]) is 3:
137          za[:,:,:] = zvariables[i][:,:,:]
138       elif len(zvarcarac[name]) is 4:
139          za[:,:,:,:] = zvariables[i][:,:,:,:]
140       i=i+1
141   ## close file
142   file.close()
143
144   print "closing "+zfilename
145
146   return
147
148def find_key(dic, val):
149    """return the key of dictionary dic given the value"""
150    return [k for k, v in dic.iteritems() if v == val][0]
151
152def get_key(self, value):
153    """find the key(s) as a list given a value"""   
154    return [item[0] for item in self.items() if item[1] == value]
Note: See TracBrowser for help on using the repository browser.