本文整理匯總了Python中xarray.open_dataset方法的典型用法代碼示例。如果您正苦於以下問題:Python xarray.open_dataset方法的具體用法?Python xarray.open_dataset怎麽用?Python xarray.open_dataset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類xarray
的用法示例。
在下文中一共展示了xarray.open_dataset方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _get_grid_files
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def _get_grid_files(self):
"""Get the files holding grid data for an aospy object."""
grid_file_paths = self.grid_file_paths
datasets = []
if isinstance(grid_file_paths, str):
grid_file_paths = [grid_file_paths]
for path in grid_file_paths:
try:
ds = xr.open_dataset(path, decode_times=False)
except (TypeError, AttributeError):
ds = xr.open_mfdataset(path, decode_times=False,
combine='by_coords').load()
except (RuntimeError, OSError) as e:
msg = str(e) + ': {}'.format(path)
raise RuntimeError(msg)
datasets.append(ds)
return tuple(datasets)
示例2: _save_files
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def _save_files(self, data, dtype_out_time):
"""Save the data to netcdf files in direc_out."""
path = self.path_out[dtype_out_time]
if not os.path.isdir(self.dir_out):
os.makedirs(self.dir_out)
if 'reg' in dtype_out_time:
try:
reg_data = xr.open_dataset(path)
except (EOFError, RuntimeError, IOError):
reg_data = xr.Dataset()
reg_data.update(data)
data_out = reg_data
else:
data_out = data
if isinstance(data_out, xr.DataArray):
data_out = xr.Dataset({self.name: data_out})
data_out.to_netcdf(path, engine='netcdf4')
示例3: _load_from_disk
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def _load_from_disk(self, dtype_out_time, dtype_out_vert=False,
region=False):
"""Load aospy data saved as netcdf files on the file system."""
ds = xr.open_dataset(self.path_out[dtype_out_time])
if region:
arr = ds[region.name]
# Use region-specific pressure values if available.
if (self.dtype_in_vert == internal_names.ETA_STR
and not dtype_out_vert):
reg_pfull_str = region.name + '_pressure'
arr = arr.drop_vars([r for r in arr.coords.iterkeys()
if r not in (internal_names.PFULL_STR,
reg_pfull_str)])
# Rename pfull to pfull_ref always.
arr = arr.rename({internal_names.PFULL_STR:
internal_names.PFULL_STR + '_ref'})
# Rename region_pfull to pfull if its there.
if hasattr(arr, reg_pfull_str):
return arr.rename({reg_pfull_str:
internal_names.PFULL_STR})
return arr
return arr
return ds[self.name]
示例4: test_load_variable_preprocess
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_load_variable_preprocess(load_variable_data_loader):
def preprocess(ds, **kwargs):
if kwargs['start_date'] == DatetimeNoLeap(5, 1, 1):
ds['condensation_rain'] = 10. * ds['condensation_rain']
return ds
load_variable_data_loader.preprocess_func = preprocess
result = load_variable_data_loader.load_variable(
condensation_rain, DatetimeNoLeap(5, 1, 1),
DatetimeNoLeap(5, 12, 31),
intvl_in='monthly')
filepath = os.path.join(os.path.split(ROOT_PATH)[0], 'netcdf',
'00050101.precip_monthly.nc')
expected = 10. * xr.open_dataset(filepath)['condensation_rain']
np.testing.assert_allclose(result.values, expected.values)
result = load_variable_data_loader.load_variable(
condensation_rain, DatetimeNoLeap(4, 1, 1),
DatetimeNoLeap(4, 12, 31),
intvl_in='monthly')
filepath = os.path.join(os.path.split(ROOT_PATH)[0], 'netcdf',
'00040101.precip_monthly.nc')
expected = xr.open_dataset(filepath)['condensation_rain']
np.testing.assert_allclose(result.values, expected.values)
示例5: test_recursive_calculation
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_recursive_calculation(recursive_test_params):
basic_params, recursive_params = recursive_test_params
calc = Calc(intvl_out='ann', dtype_out_time='av', **basic_params)
calc = calc.compute()
expected = xr.open_dataset(
calc.path_out['av'])['condensation_rain']
_test_files_and_attrs(calc, 'av')
calc = Calc(intvl_out='ann', dtype_out_time='av', **recursive_params)
calc = calc.compute()
result = xr.open_dataset(
calc.path_out['av'])['recursive_condensation_rain']
_test_files_and_attrs(calc, 'av')
xr.testing.assert_equal(expected, result)
示例6: get_data
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def get_data(filename):
data = xarray.open_dataset(
pjoin(CELER_PATH, 'climate/surface', filename), decode_times=False)
n_times = data[list(data.data_vars.keys())[0]].shape[0]
X = np.array(data[list(data.data_vars.keys())[0]]).reshape(n_times, -1)
# remove seasonality
period = 12
for m in range(period):
# TODO using sklearn for preprocessing would be an improvement
X[m::period] -= np.mean(X[m::period], axis=0)[None, :]
X[m::period] /= np.std(X[m::period], axis=0)[None, :]
if np.sum(np.isnan(X[m::period])) > 0:
X[m::period] = np.where(np.isnan(X[m::period]), 0, X[m::period])
# remove trend
X = detrend(X, axis=0, type='linear')
return X
示例7: test_make_geocube__convert_time
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_make_geocube__convert_time(input_geodata, tmpdir):
out_grid = make_geocube(
vector_data=input_geodata,
measurements=["test_attr", "test_time_attr", "test_str_attr"],
datetime_measurements=["test_time_attr"],
resolution=(-0.00001, 0.00001),
fill=-9999.0,
)
# test writing to netCDF
out_grid.to_netcdf(str(tmpdir.mkdir("geocube_time").join("time_vector_data.nc")))
# test output data
with xarray.open_dataset(
os.path.join(TEST_COMPARE_DATA_DIR, "time_vector_data.nc"), mask_and_scale=False
) as xdc:
xarray.testing.assert_allclose(out_grid, xdc)
assert out_grid.test_time_attr.attrs["units"] == "seconds from 1970-01-01T00:00:00"
assert out_grid.test_time_attr.attrs["_FillValue"] == 0
示例8: test_make_geocube__like_error_invalid_args
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_make_geocube__like_error_invalid_args(load_extra_kwargs):
soil_attribute_list = [
"om_r",
"sandtotal_r",
"silttotal_r",
"claytotal_r",
"cec7_r",
"ph1to1h2o_r",
"dbthirdbar_r",
"awc_r",
]
with xarray.open_dataset(
os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_flat.nc"), mask_and_scale=False
) as xdc:
with pytest.raises(AssertionError):
make_geocube(
vector_data=os.path.join(TEST_INPUT_DATA_DIR, "soil_data_flat.geojson"),
measurements=soil_attribute_list,
like=xdc,
fill=-9999.0,
**load_extra_kwargs,
)
示例9: test_make_geocube__no_measurements
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_make_geocube__no_measurements(input_geodata, tmpdir):
out_grid = make_geocube(
vector_data=input_geodata,
output_crs=TEST_GARS_PROJ,
geom=json.dumps(mapping(TEST_GARS_POLY)),
resolution=(-10, 10),
fill=-9999.0,
)
# test writing to netCDF
out_grid.to_netcdf(str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_flat.nc")))
# test output data
with xarray.open_dataset(
os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_flat.nc"), mask_and_scale=False
) as xdc:
xarray.testing.assert_allclose(out_grid, xdc)
tmpdir.remove()
示例10: test_make_geocube__no_geom
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_make_geocube__no_geom(tmpdir):
out_grid = make_geocube(
vector_data=os.path.join(TEST_INPUT_DATA_DIR, "soil_data_flat.geojson"),
measurements=["sandtotal_r"],
resolution=(-0.001, 0.001),
fill=-9999.0,
)
# test writing to netCDF
out_grid.to_netcdf(
str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_flat_no_geom.nc"))
)
# test output data
with xarray.open_dataset(
os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_flat_no_geom.nc"),
mask_and_scale=False,
) as xdc:
xarray.testing.assert_allclose(out_grid, xdc)
tmpdir.remove()
示例11: test_make_geocube__group_by_only_resolution
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_make_geocube__group_by_only_resolution(input_geodata, tmpdir):
soil_attribute_list = ["sandtotal_r", "silttotal_r", "claytotal_r"]
out_grid = make_geocube(
vector_data=input_geodata,
measurements=soil_attribute_list,
group_by="hzdept_r",
resolution=(-0.001, 0.001),
fill=-9999.0,
)
# test writing to netCDF
out_grid.to_netcdf(
str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_grouped_original_crs.nc"))
)
# test output data
with xarray.open_dataset(
os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_grouped_original_crs.nc"),
mask_and_scale=False,
) as xdc:
xarray.testing.assert_allclose(out_grid, xdc)
tmpdir.remove()
示例12: test_make_geocube__group_by_time
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_make_geocube__group_by_time(input_geodata, tmpdir):
out_grid = make_geocube(
vector_data=input_geodata,
datetime_measurements=["test_time_attr"],
resolution=(-0.00001, 0.00001),
group_by="test_time_attr",
fill=-9999.0,
)
# test writing to netCDF
out_grid.to_netcdf(
str(tmpdir.mkdir("make_geocube_time").join("vector_time_data_group.nc"))
)
# test output data
with xarray.open_dataset(
os.path.join(TEST_COMPARE_DATA_DIR, "vector_time_data_group.nc"),
mask_and_scale=False,
) as xdc:
xarray.testing.assert_allclose(out_grid, xdc)
tmpdir.remove()
示例13: test_make_geocube__group_by_convert_with_time
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_make_geocube__group_by_convert_with_time(input_geodata, tmpdir):
out_grid = make_geocube(
vector_data=input_geodata,
datetime_measurements=["test_time_attr"],
resolution=(-0.00001, 0.00001),
group_by="test_attr",
fill=-9999.0,
)
# test writing to netCDF
out_grid.to_netcdf(
str(tmpdir.mkdir("make_geocube_time").join("vector_data_group.nc"))
)
# test output data
with xarray.open_dataset(
os.path.join(TEST_COMPARE_DATA_DIR, "vector_data_group.nc"),
mask_and_scale=False,
) as xdc:
xarray.testing.assert_allclose(out_grid, xdc)
assert out_grid.test_time_attr.attrs["units"] == "seconds from 1970-01-01T00:00:00"
assert out_grid.test_time_attr.attrs["_FillValue"] == 0
tmpdir.remove()
示例14: test_make_geocube__group_by_no_measurements
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_make_geocube__group_by_no_measurements(input_geodata, tmpdir):
out_grid = make_geocube(
vector_data=input_geodata,
output_crs=TEST_GARS_PROJ,
geom=json.dumps(mapping(TEST_GARS_POLY)),
group_by="hzdept_r",
resolution=(-10, 10),
fill=-9999.0,
)
# test writing to netCDF
out_grid.to_netcdf(
str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_group.nc"))
)
# test output data
with xarray.open_dataset(
os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_group.nc"), mask_and_scale=False
) as xdc:
xarray.testing.assert_allclose(out_grid, xdc)
tmpdir.remove()
示例15: test_make_geocube__group_by__no_geom
# 需要導入模塊: import xarray [as 別名]
# 或者: from xarray import open_dataset [as 別名]
def test_make_geocube__group_by__no_geom(tmpdir):
out_grid = make_geocube(
vector_data=os.path.join(TEST_INPUT_DATA_DIR, "soil_data_group.geojson"),
measurements=["sandtotal_r"],
group_by="hzdept_r",
resolution=(-0.001, 0.001),
fill=-9999.0,
)
# test writing to netCDF
out_grid.to_netcdf(
str(tmpdir.mkdir("make_geocube_soil").join("soil_grid_group_no_geom.nc"))
)
# test output data
with xarray.open_dataset(
os.path.join(TEST_COMPARE_DATA_DIR, "soil_grid_group_no_geom.nc"),
mask_and_scale=False,
) as xdc:
xarray.testing.assert_allclose(out_grid, xdc)
tmpdir.remove()