1 |
#!/usr/bin/env python |
---|
2 |
# Last modified: Time-stamp: <2008-01-08 16:10:22 haines> |
---|
3 |
""" |
---|
4 |
Create, update and load utilities for netcdf files |
---|
5 |
""" |
---|
6 |
|
---|
7 |
from pycdf import * |
---|
8 |
import os |
---|
9 |
import numpy |
---|
10 |
|
---|
11 |
def nc_create(ncFile, (global_atts, var_atts, dim_inits, var_inits, var_data)): |
---|
12 |
""" |
---|
13 |
Create new netcdf file |
---|
14 |
|
---|
15 |
:Parameters: |
---|
16 |
ncFile : string |
---|
17 |
Path and name of file to create |
---|
18 |
""" |
---|
19 |
try: |
---|
20 |
# Open new netCDF file, overwrite if it exists, create if does not |
---|
21 |
nc = CDF(ncFile, NC.WRITE|NC.CREATE|NC.TRUNC) |
---|
22 |
# Automatically set define and data modes. |
---|
23 |
nc.automode() |
---|
24 |
# |
---|
25 |
# GLOBALS |
---|
26 |
for attrName in global_atts.keys(): |
---|
27 |
setattr(nc, attrName, global_atts[attrName]) |
---|
28 |
|
---|
29 |
# DIMENSIONS |
---|
30 |
for dim in dim_inits: |
---|
31 |
dimName, dimValue = dim |
---|
32 |
# print '%s = %d' % (dimName, dimValue) |
---|
33 |
ncdim = nc.def_dim(dimName, dimValue) |
---|
34 |
|
---|
35 |
# VARIABLES |
---|
36 |
for var in var_inits: |
---|
37 |
varName, varType, varDim = var |
---|
38 |
ncvar = nc.def_var(varName, varType, varDim) |
---|
39 |
# add attributes |
---|
40 |
for attrName in var_atts[varName].keys(): |
---|
41 |
setattr(ncvar, attrName, var_atts[varName][attrName]) |
---|
42 |
# setattr(ncvar, '_FillValue', numpy.nan) |
---|
43 |
|
---|
44 |
# add data |
---|
45 |
nrecs = nc.inq_unlimlen() |
---|
46 |
for var in var_data: |
---|
47 |
varName, varData = var |
---|
48 |
# print varName |
---|
49 |
ncvar = nc.var(varName) |
---|
50 |
# e.g. lat = array(var_data['lat']) |
---|
51 |
# if an array |
---|
52 |
if type(varData) == numpy.ndarray: |
---|
53 |
if ncvar.isrecord(): |
---|
54 |
# time, ens, u, v |
---|
55 |
ncvar[nrecs:nrecs+len(varData)] = varData.tolist() |
---|
56 |
else: |
---|
57 |
ncvar[:] = varData.tolist() # z |
---|
58 |
else: |
---|
59 |
# if tuple, sequence or scalar |
---|
60 |
ncvar[:] = varData |
---|
61 |
|
---|
62 |
nc.close() |
---|
63 |
except CDFError, msg: |
---|
64 |
print "CDFError:", msg |
---|
65 |
# if nc: |
---|
66 |
# nc.close() |
---|
67 |
# del(nc) |
---|
68 |
|
---|
69 |
def nc_update(ncFile, (global_atts, var_atts, var_data)): |
---|
70 |
""" |
---|
71 |
Create new netcdf file |
---|
72 |
|
---|
73 |
:Parameters: |
---|
74 |
ncFile : string |
---|
75 |
Path and name of file to create |
---|
76 |
""" |
---|
77 |
try: |
---|
78 |
# Open netCDF in write mode |
---|
79 |
nc = CDF(ncFile, NC.WRITE) |
---|
80 |
# Automatically set define and data modes. |
---|
81 |
nc.automode() |
---|
82 |
# |
---|
83 |
# GLOBALS |
---|
84 |
for attrName in global_atts.keys(): |
---|
85 |
setattr(nc, attrName, global_atts[attrName]) |
---|
86 |
|
---|
87 |
# VARIABLES |
---|
88 |
# update attributes |
---|
89 |
for var in var_atts: |
---|
90 |
varName, atts = var |
---|
91 |
ncvar = nc.var(varName) |
---|
92 |
for attrName in atts.keys(): |
---|
93 |
setattr(ncvar, attrName, atts[attrName]) |
---|
94 |
|
---|
95 |
# update data |
---|
96 |
nrecs = nc.inq_unlimlen() |
---|
97 |
for var in var_data: |
---|
98 |
varName, varData = var |
---|
99 |
ncvar = nc.var(varName) |
---|
100 |
# e.g. lat = array(var_data['lat']) |
---|
101 |
# if an array |
---|
102 |
if type(varData) == numpy.ndarray: |
---|
103 |
if ncvar.isrecord(): |
---|
104 |
# time, ens, u, v (with unlimited dimension) |
---|
105 |
ncvar[nrecs:nrecs+len(varData)] = varData.tolist() |
---|
106 |
else: |
---|
107 |
ncvar[:] = varData.tolist() # z (limited dimension) |
---|
108 |
else: |
---|
109 |
# if tuple, sequence or scalar |
---|
110 |
ncvar[:] = varData |
---|
111 |
|
---|
112 |
nc.close() |
---|
113 |
except CDFError, msg: |
---|
114 |
print "CDFError:", msg |
---|
115 |
# if nc: |
---|
116 |
# nc.close() |
---|
117 |
# del(nc) |
---|
118 |
|
---|
119 |
def nc_get_time(ncFile): |
---|
120 |
"""get time array from file """ |
---|
121 |
try: |
---|
122 |
nc = CDF(ncFile) |
---|
123 |
ncvars = nc.variables() |
---|
124 |
if 'time' in ncvars.keys(): |
---|
125 |
es = nc.var('time')[:] |
---|
126 |
units = nc.var('time').units |
---|
127 |
nc.close() |
---|
128 |
return (es, units) |
---|
129 |
except CDFError, msg: |
---|
130 |
print "CDFError:", msg |
---|
131 |
|
---|
132 |
|
---|
133 |
|
---|
134 |
|
---|
135 |
def nc_load(ncFile, nameType='variable_name', |
---|
136 |
varNames='all', ga_flag=True, va_flag=True): |
---|
137 |
""" |
---|
138 |
Load netcdf file |
---|
139 |
|
---|
140 |
:Parameters: |
---|
141 |
ncFile : string |
---|
142 |
Path and name of file to load |
---|
143 |
|
---|
144 |
:Other Parameters: |
---|
145 |
nameType : string 'variable_name' (default) or 'standard_name' |
---|
146 |
Defines naming convention to use for variable names as data |
---|
147 |
are loaded. Variable name is the name used to store data |
---|
148 |
in file. 'standard_name' means use variable name based on |
---|
149 |
variable attribute called 'standard_name' of netcdf variable. |
---|
150 |
varNames : string or tuple of strings |
---|
151 |
specific variable names to be loaded into a sequence or scalar |
---|
152 |
in python following specification set in nameType |
---|
153 |
By default, all variables will be loaded. |
---|
154 |
ga_flag : boolean flag |
---|
155 |
By default, load the global file attributes |
---|
156 |
va_flag : boolean flag |
---|
157 |
By default, load the variable file attributes |
---|
158 |
|
---|
159 |
""" |
---|
160 |
try: |
---|
161 |
nc = CDF(ncFile) |
---|
162 |
attr = nc.attributes(full=1) |
---|
163 |
dims = nc.dimensions(full=1) |
---|
164 |
ncvars = nc.variables() |
---|
165 |
for var in ncvars.keys(): |
---|
166 |
# load each variable by name?? |
---|
167 |
pass |
---|
168 |
|
---|
169 |
except CDFError, msg: |
---|
170 |
print "CDFError:", msg |
---|
171 |
|
---|
172 |
|
---|