MCPcopy Create free account
hub / github.com/NCAR/wrf-python / _build_data_array

Function _build_data_array

src/wrf/util.py:1222–1393  ·  view source on GitHub ↗

Return a :class:`xarray.DataArray` object for the desired variable in a single NetCDF file object. Args: wrfnc (:class:`netCDF4.Dataset`, :class:`Nio.NioFile`): A single WRF NetCDF file object. varname (:obj:`str`) : The variable name. timeidx (:obj:`i

(wrfnc, varname, timeidx, is_moving_domain, is_multifile,
                      _key)

Source from the content-addressed store, hash-verified

1220
1221
1222def _build_data_array(wrfnc, varname, timeidx, is_moving_domain, is_multifile,
1223 _key):
1224 """Return a :class:`xarray.DataArray` object for the desired variable in
1225 a single NetCDF file object.
1226
1227 Args:
1228
1229 wrfnc (:class:`netCDF4.Dataset`, :class:`Nio.NioFile`): A single
1230 WRF NetCDF file object.
1231
1232 varname (:obj:`str`) : The variable name.
1233
1234 timeidx (:obj:`int` or :data:`wrf.ALL_TIMES`, optional): The
1235 desired time index. This value can be a positive integer,
1236 negative integer, or
1237 :data:`wrf.ALL_TIMES` (an alias for None) to return
1238 all times in the file or sequence. The default is 0.
1239
1240 is_moving_domain (:obj:`bool`): A boolean type that indicates if the
1241 NetCDF file object came from a moving nest.
1242
1243 is_multifile (:obj:`bool`): A boolean type that indicates if the NetCDF
1244 file object came from a sequence.
1245
1246 _key (:obj:`int`, optional): Cache key for the coordinate variables.
1247 This is used for internal purposes only. Default is None.
1248
1249 Returns:
1250
1251 :class:`xarray.DataArray`: An array object that contains metadata.
1252
1253 """
1254
1255 # Note: wrfnc is always a single netcdf file object
1256 # is_moving_domain and is_multifile are arguments indicating if the
1257 # single file came from a sequence, and if that sequence is has a moving
1258 # domain. Both arguments are used mainly for coordinate extraction and
1259 # caching.
1260 multitime = is_multi_time_req(timeidx)
1261 time_idx_or_slice = timeidx if not multitime else slice(None)
1262 var = wrfnc.variables[varname]
1263 if len(var.shape) > 1:
1264 data = var[time_idx_or_slice, :]
1265 else:
1266 data = var[time_idx_or_slice]
1267
1268 # Want to preserve the time dimension
1269 if not multitime:
1270 if len(var.shape) > 1:
1271 data = data[np.newaxis, :]
1272 else:
1273 data = data[np.newaxis]
1274
1275 attrs = OrderedDict()
1276 for dkey, val in viewitems(var.__dict__):
1277 # scipy.io adds these but don't want them
1278 if dkey in ("data", "_shape", "_size", "_typecode", "_attributes",
1279 "maskandscale", "dimensions"):

Callers 5

_find_forwardFunction · 0.85
_find_reverseFunction · 0.85
_cat_filesFunction · 0.85
_join_filesFunction · 0.85
_extract_varFunction · 0.85

Calls 9

is_multi_time_reqFunction · 0.85
viewitemsFunction · 0.85
isstrFunction · 0.85
_get_coord_namesFunction · 0.85
get_cached_itemFunction · 0.85
cache_itemFunction · 0.85
get_proj_paramsFunction · 0.85
getprojFunction · 0.85
extract_timesFunction · 0.85

Tested by

no test coverage detected