Python xarray.open_dataset() Examples

The following are 30 code examples of xarray.open_dataset(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module xarray , or try the search function .
Example #1
Source File: model.py    From aospy with Apache License 2.0 7 votes vote down vote up
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) 
Example #2
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #3
Source File: test_prepro.py    From oggm with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_distribute_climate_grad(self):

        hef_file = get_demo_file('Hintereisferner_RGI5.shp')
        cfg.PARAMS['temp_use_local_gradient'] = True
        entity = gpd.read_file(hef_file).iloc[0]

        gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir)
        gis.define_glacier_region(gdir)
        climate.process_custom_climate_data(gdir)

        ci = gdir.get_climate_info()
        self.assertEqual(ci['baseline_hydro_yr_0'], 1802)
        self.assertEqual(ci['baseline_hydro_yr_1'], 2003)

        with xr.open_dataset(gdir.get_filepath('climate_historical')) as ds:
            grad = ds['gradient'].data
            try:
                assert np.std(grad) > 0.0001
            except TypeError:
                pass
        cfg.PARAMS['temp_use_local_gradient'] = False 
Example #4
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #5
Source File: flowline.py    From oggm with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def glacier_from_netcdf(path):
    """Instanciates a list of flowlines from an xarray Dataset."""

    with xr.open_dataset(path) as ds:
        fls = []
        for flid in ds['flowlines'].values:
            with xr.open_dataset(path, group='fl_{}'.format(flid)) as _ds:
                fls.append(flowline_from_dataset(_ds))

        for i, fid in enumerate(ds['flows_to_id'].values):
            if fid != -1:
                fls[i].set_flows_to(fls[fid])

    # Adds the line level
    for fl in fls:
        fl.order = line_order(fl)

    return fls 
Example #6
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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,
            ) 
Example #7
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #8
Source File: test_obs3.py    From georinex with MIT License 6 votes vote down vote up
def test_one_system(use):
    """
    ./ReadRinex.py -q tests/demo3.10o  -u G -o r3G.nc
    """
    pytest.importorskip('netCDF4')

    truth = xarray.open_dataset(R/'r3G.nc', group='OBS')

    obs = gr.load(R/'demo3.10o', use=use)
    assert obs.equals(truth)

    assert obs.position == approx([4789028.4701, 176610.0133, 4195017.031])
    try:
        assert obs.position_geodetic == approx([41.38871005, 2.11199932, 166.25085213])
    except AttributeError:  # no pymap3d
        pass 
Example #9
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #10
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #11
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #12
Source File: climate.py    From celer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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 
Example #13
Source File: test_calc_basic.py    From aospy with Apache License 2.0 6 votes vote down vote up
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) 
Example #14
Source File: calc.py    From aospy with Apache License 2.0 6 votes vote down vote up
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] 
Example #15
Source File: prepare_climate.py    From oggm with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def example_plot_temp_ts():
    d = xr.open_dataset(gdir.get_filepath('climate_historical'))
    temp = d.temp.resample(time='12MS').mean('time').to_series()
    temp.index = temp.index.year
    try:
        temp = temp.rename_axis(None)
    except AttributeError:
        del temp.index.name
    temp.plot(figsize=(8, 4), label='Annual temp')
    tsm = temp.rolling(31, center=True, min_periods=15).mean()
    tsm.plot(label='31-yr avg')
    plt.legend(loc='best')
    plt.title('HISTALP annual temperature, Hintereisferner')
    plt.ylabel(r'degC')
    plt.tight_layout()
    plt.show() 
Example #16
Source File: test_data_loader.py    From aospy with Apache License 2.0 6 votes vote down vote up
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) 
Example #17
Source File: _workflow.py    From oggm with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_ref_length_data(self):
        """Get the glacier length data from P. Leclercq's data base.

         https://folk.uio.no/paulwl/data.php

         For some glaciers only!
         """

        df = pd.read_csv(get_demo_file('rgi_leclercq_links_2012_RGIV5.csv'))
        df = df.loc[df.RGI_ID == self.rgi_id]
        if len(df) == 0:
            raise RuntimeError('No length data found for this glacier!')
        ide = df.LID.values[0]

        f = get_demo_file('Glacier_Lengths_Leclercq.nc')
        with xr.open_dataset(f) as dsg:
            # The database is not sorted by ID. Don't ask me...
            grp_id = np.argwhere(dsg['index'].values == ide)[0][0] + 1
        with xr.open_dataset(f, group=str(grp_id)) as ds:
            df = ds.to_dataframe()
            df.name = ds.glacier_name
        return df 
Example #18
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_make_geocube__custom_rasterize_function__filter_null(
    function, compare_name, tmpdir
):
    input_geodata = os.path.join(TEST_INPUT_DATA_DIR, "point_with_null.geojson")
    out_grid = make_geocube(
        vector_data=input_geodata,
        resolution=(-0.00001, 0.00001),
        rasterize_function=function,
    )

    # test writing to netCDF
    out_grid.to_netcdf(str(tmpdir.mkdir("geocube_custom").join(compare_name)))

    # test output data
    with xarray.open_dataset(
        os.path.join(TEST_COMPARE_DATA_DIR, compare_name), mask_and_scale=False
    ) as xdc:
        xarray.testing.assert_allclose(out_grid, xdc, rtol=0.1, atol=0.1) 
Example #19
Source File: dsio.py    From xcube with MIT License 6 votes vote down vote up
def open_dataset(input_path: str,
                 format_name: str = None,
                 is_cube: bool = False,
                 **kwargs) -> xr.Dataset:
    """
    Open a dataset from *input_path*.
    If *format* is not provided it will be guessed from *output_path*.
    :param input_path: input path
    :param format_name: format, e.g. "zarr" or "netcdf4"
    :param is_cube: Whether a ValueError will be raised, if the dataset read from *input_path* is not a xcube dataset.
    :param kwargs: format-specific keyword arguments
    :return: dataset object
    """
    format_name = format_name if format_name else guess_dataset_format(input_path)
    if format_name is None:
        raise ValueError("Unknown input format")
    dataset_io = find_dataset_io(format_name, modes=["r"])
    if dataset_io is None:
        raise ValueError(f"Unknown input format {format_name!r} for {input_path}")
    dataset = dataset_io.read(input_path, **kwargs)
    if is_cube:
        assert_cube(dataset)
    return dataset 
Example #20
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_make_geocube__custom_rasterize_function(function, compare_name, tmpdir):
    input_geodata = os.path.join(TEST_INPUT_DATA_DIR, "time_vector_data.geojson")
    out_grid = make_geocube(
        vector_data=input_geodata,
        measurements=["test_attr", "test_time_attr", "test_str_attr"],
        resolution=(-0.00001, 0.00001),
        rasterize_function=function,
        fill=-9999.0,
    )

    # test writing to netCDF
    out_grid.to_netcdf(str(tmpdir.mkdir("geocube_custom").join(compare_name)))

    # test output data
    with xarray.open_dataset(
        os.path.join(TEST_COMPARE_DATA_DIR, compare_name), mask_and_scale=False
    ) as xdc:
        xarray.testing.assert_allclose(out_grid, xdc, rtol=0.1, atol=0.1) 
Example #21
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #22
Source File: test_core_integration.py    From geocube with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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() 
Example #23
Source File: test_shop.py    From oggm with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_ecmwf_workflow(self, class_case_dir):

        # Init
        cfg.initialize()
        cfg.PARAMS['use_intersects'] = False
        cfg.PATHS['working_dir'] = class_case_dir
        cfg.PATHS['dem_file'] = get_demo_file('hef_srtm.tif')

        hef_file = get_demo_file('Hintereisferner_RGI5.shp')
        gdir = workflow.init_glacier_directories(gpd.read_file(hef_file))[0]

        cfg.PARAMS['baseline_climate'] = 'CERA+ERA5L'
        tasks.process_climate_data(gdir)
        f_ref = gdir.get_filepath('climate_historical')
        with xr.open_dataset(f_ref) as his:
            # Let's do some basic checks
            ci = gdir.get_climate_info()
            assert ci['baseline_climate_source'] == 'CERA+ERA5L'
            assert ci['baseline_hydro_yr_0'] == 1902
            assert ci['baseline_hydro_yr_1'] == 2018

        cfg.PARAMS['baseline_climate'] = 'CERA|ERA5'
        tasks.process_climate_data(gdir)
        f_ref = gdir.get_filepath('climate_historical')
        with xr.open_dataset(f_ref) as his:
            # Let's do some basic checks
            ci = gdir.get_climate_info()
            assert ci['baseline_climate_source'] == 'CERA|ERA5'
            assert ci['baseline_hydro_yr_0'] == 1902
            assert ci['baseline_hydro_yr_1'] == 2010 
Example #24
Source File: test_benchmarks.py    From oggm with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_run(self):

        entity = gpd.read_file(self.rgi_file).iloc[0]

        gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir)
        gis.define_glacier_region(gdir)
        gis.glacier_masks(gdir)
        centerlines.compute_centerlines(gdir)
        centerlines.initialize_flowlines(gdir)
        centerlines.compute_downstream_line(gdir)
        centerlines.compute_downstream_bedshape(gdir)
        centerlines.catchment_area(gdir)
        centerlines.catchment_intersections(gdir)
        centerlines.catchment_width_geom(gdir)
        centerlines.catchment_width_correction(gdir)

        # Climate tasks -- only data IO and tstar interpolation!
        tasks.process_dummy_cru_file(gdir, seed=0)
        tasks.local_t_star(gdir)
        tasks.mu_star_calibration(gdir)

        # Inversion tasks
        tasks.find_inversion_calving(gdir)

        # Final preparation for the run
        tasks.init_present_time_glacier(gdir)

        # check that calving happens in the real context as well
        tasks.run_constant_climate(gdir, bias=0, nyears=200,
                                   temperature_bias=-0.5)
        with xr.open_dataset(gdir.get_filepath('model_diagnostics')) as ds:
            assert ds.calving_m3[-1] > 10 
Example #25
Source File: __init__.py    From xrviz with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def data():
    return xr.open_dataset("xrviz/sample_data/great_lakes.nc") 
Example #26
Source File: dataset.py    From xcube with MIT License 5 votes vote down vote up
def open_data(self, data_id: str, **open_params) -> xr.Dataset:
        return xr.open_dataset(data_id, **open_params) 
Example #27
Source File: test_prepro.py    From oggm with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_distribute_climate_cru(self):

        hef_file = get_demo_file('Hintereisferner_RGI5.shp')
        entity = gpd.read_file(hef_file).iloc[0]

        gdirs = []

        gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir)
        gis.define_glacier_region(gdir)
        gdirs.append(gdir)
        gdir = oggm.GlacierDirectory(entity, base_dir=self.testdir_cru)
        gis.define_glacier_region(gdir)
        gdirs.append(gdir)

        climate.process_custom_climate_data(gdirs[0])
        cfg.PATHS['climate_file'] = ''
        cfg.PARAMS['baseline_climate'] = 'CRU'
        tasks.process_cru_data(gdirs[1])
        cfg.PATHS['climate_file'] = get_demo_file('histalp_merged_hef.nc')

        ci = gdir.get_climate_info()
        self.assertEqual(ci['baseline_hydro_yr_0'], 1902)
        self.assertEqual(ci['baseline_hydro_yr_1'], 2014)

        gdh = gdirs[0]
        gdc = gdirs[1]
        f1 = os.path.join(gdh.dir, 'climate_historical.nc')
        f2 = os.path.join(gdc.dir, 'climate_historical.nc')
        with xr.open_dataset(f1) as nc_h:
            with xr.open_dataset(f2) as nc_c:
                # put on the same altitude
                # (using default gradient because better)
                temp_cor = nc_c.temp - 0.0065 * (nc_h.ref_hgt - nc_c.ref_hgt)
                totest = temp_cor - nc_h.temp
                self.assertTrue(totest.mean() < 0.5)
                # precip
                totest = nc_c.prcp - nc_h.prcp
                self.assertTrue(totest.mean() < 100) 
Example #28
Source File: test_gen.py    From xcube with MIT License 5 votes vote down vote up
def test_process_inputs_single_nc_processed_vars(self):
        status, output = gen_cube_wrapper(
            [get_inputdata_path('20170101-IFR-L4_GHRSST-SSTfnd-ODYSSEA-NWE_002-v2.0-fv1.0.nc')],
            'l2c-single.nc',
            processed_variables=(
                ('analysed_sst', dict(valid_pixel_expression=None)),
                ('analysis_error', dict(valid_pixel_expression=None)),
                ('sea_ice_fraction', dict(valid_pixel_expression=None)),
                ('water_mask', dict(expression='(mask.sea or mask.lake) and not mask.ice', load=True)),
                ('ice_mask', dict(expression='mask.sea and mask.ice')),
                ('analysed_sst', dict(valid_pixel_expression='water_mask')),
                ('analysis_error', dict(valid_pixel_expression='water_mask')),
                ('sea_ice_fraction', dict(valid_pixel_expression='ice_mask')),
            ),
            output_variables=(
                ('analysed_sst', dict(name='SST')),
                ('analysis_error', dict(name='SST_uncertainty')),
                ('sea_ice_fraction', None),
            ),
        )
        self.assertEqual(True, status)
        self.assertTrue('\nstep 9 of 9: creating input slice in l2c-single.nc...\n' in output)
        self.assert_cube_ok(xr.open_dataset('l2c-single.nc', autoclose=True),
                            expected_time_dim=1,
                            expected_output_vars=('SST', 'SST_uncertainty', 'sea_ice_fraction'),
                            expected_extra_attrs=dict(date_modified=None,
                                                      time_coverage_start='2016-12-31T12:00:00.000000000',
                                                      time_coverage_end='2017-01-01T12:00:00.000000000')) 
Example #29
Source File: dsio.py    From xcube with MIT License 5 votes vote down vote up
def open_cube(input_path: str,
              format_name: str = None,
              **kwargs) -> xr.Dataset:
    """
    Open a xcube dataset from *input_path*.
    If *format* is not provided it will be guessed from *input_path*.
    :param input_path: input path
    :param format_name: format, e.g. "zarr" or "netcdf4"
    :param kwargs: format-specific keyword arguments
    :return: xcube dataset
    """
    return open_dataset(input_path, format_name=format_name, is_cube=True, **kwargs) 
Example #30
Source File: test_gen.py    From xcube with MIT License 5 votes vote down vote up
def test_process_inputs_append_multiple_nc(self):
        status, output = gen_cube_wrapper(
            [get_inputdata_path('201701??-IFR-L4_GHRSST-SSTfnd-ODYSSEA-NWE_002-v2.0-fv1.0.nc')], 'l2c.nc',
            no_sort_mode=False)
        self.assertEqual(True, status)
        self.assertTrue('\nstep 9 of 9: creating input slice in l2c.nc...\n' in output)
        self.assertTrue('\nstep 9 of 9: appending input slice to l2c.nc...\n' in output)
        self.assert_cube_ok(xr.open_dataset('l2c.nc', autoclose=True),
                            expected_time_dim=3,
                            expected_extra_attrs=dict(date_modified=None,
                                                      time_coverage_start='2016-12-31T12:00:00.000000000',
                                                      time_coverage_end='2017-01-03T12:00:00.000000000'))