当前位置: 首页>>代码示例>>Python>>正文


Python xarray.open_dataset方法代码示例

本文整理汇总了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) 
开发者ID:spencerahill,项目名称:aospy,代码行数:19,代码来源:model.py

示例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') 
开发者ID:spencerahill,项目名称:aospy,代码行数:19,代码来源:calc.py

示例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] 
开发者ID:spencerahill,项目名称:aospy,代码行数:25,代码来源:calc.py

示例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) 
开发者ID:spencerahill,项目名称:aospy,代码行数:27,代码来源:test_data_loader.py

示例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) 
开发者ID:spencerahill,项目名称:aospy,代码行数:18,代码来源:test_calc_basic.py

示例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 
开发者ID:mathurinm,项目名称:celer,代码行数:23,代码来源:climate.py

示例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 
开发者ID:corteva,项目名称:geocube,代码行数:22,代码来源:test_core_integration.py

示例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,
            ) 
开发者ID:corteva,项目名称:geocube,代码行数:25,代码来源:test_core_integration.py

示例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() 
开发者ID:corteva,项目名称:geocube,代码行数:21,代码来源:test_core_integration.py

示例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() 
开发者ID:corteva,项目名称:geocube,代码行数:23,代码来源:test_core_integration.py

示例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() 
开发者ID:corteva,项目名称:geocube,代码行数:26,代码来源:test_core_integration.py

示例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() 
开发者ID:corteva,项目名称:geocube,代码行数:23,代码来源:test_core_integration.py

示例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() 
开发者ID:corteva,项目名称:geocube,代码行数:27,代码来源:test_core_integration.py

示例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() 
开发者ID:corteva,项目名称:geocube,代码行数:24,代码来源:test_core_integration.py

示例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() 
开发者ID:corteva,项目名称:geocube,代码行数:24,代码来源:test_core_integration.py


注:本文中的xarray.open_dataset方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。