def read_ncfile3D(filename, varname, make_cuts=False):
"""
Reads a 3D field along the time for variable "varname", i.e. results a 4D numpy array.
If "make_cuts" is not False, it should be a list of pairs of tuples representing cuts to be made and returned.
Example:
make_cuts = ((lat1, lon1),(lat2, lon2))
:param filename:
:param varname:
:param make_cut:
:return:
"""
try:
file = nc.Dataset(filename, 'r')
except IOError as e:
logging.error("%s\n\tFile: %s", "I/O error({0}): {1}".format(e.errno, e.strerror), filename)
return None
except RuntimeError as e:
logging.error("%s\n\tRequested file: %s", e.message, filename)
pdb.set_trace()
return None
try:
f_data_pointer = file.variables[varname]
time = file.variables['Time']
lat = file.variables['lat'][:, 0]
lon = file.variables['lon'][0, :]
try:
c_Units = f_data_pointer.units
except AttributeError:
c_Units = None
if make_cuts:
f_data, lat, lon, elev = get_planes(f_data_pointer, make_cuts, lat, lon, file.variables['height'])
else:
# These two lines are a lot of data!
elev = file.variables['height'][:]
f_data = f_data_pointer[:]
try:
f_data = np.where(f_data == f_data_pointer._FillValue, IncF.f_FillValue, f_data)
except AttributeError:
logging.warning('No FillValue')
except KeyError:
logging.warning('Variable %s is not in %s, so it will created a NaN matrix', varname, filename)
f_data = np.empty(shape=file.variables['height'].shape)
f_data.fill(IncF.f_FillValue)
c_Units = None
try:
d_time = nc.num2date(time[:], units=time.units, calendar=time.calendar)
except AttributeError:
c_time = [''.join(str(t) for t in TT) for TT in time[:]]
d_time = [dt.datetime.strptime(c_T[:], '%Y-%m-%d_%H:%M:%S') for c_T in c_time]
d_TimeArray = d_time + dt.timedelta(hours=IncF.i_TimeZone)
file.close()
return f_data, lat, lon, elev, d_TimeArray, c_Units
评论列表
文章目录