Exemplo n.º 1
0
def test_map_params_space():
    args = utils.map_params(space=(0, 10, 50, 60), )
    assert args["lon_bnds"] == (0, 50)
    assert args["lat_bnds"] == (10, 60)
    # allow also strings
    args = utils.map_params(space=("0", "10", "50", "60"), )
    assert args["lon_bnds"] == (0, 50)
    assert args["lat_bnds"] == (10, 60)
Exemplo n.º 2
0
def test_map_params_invalid_time():
    with pytest.raises(InvalidParameterValue):
        utils.map_params(
            ds=CMIP5_TAS_FILE,
            time=("1999-01-01T00:00:00", "maybe tomorrow"),
            area=(0, -90, 360, 90),
        )
    with pytest.raises(InvalidParameterValue):
        utils.map_params(ds=CMIP5_TAS_FILE, time=("", "2100"), area=(0, -90, 360, 90))
Exemplo n.º 3
0
def test_map_params_invalid_area():
    with pytest.raises(InvalidParameterValue):
        utils.map_params(
            ds=CMIP5_TAS_FILE,
            area=(0, 10, 50),
        )
    with pytest.raises(InvalidParameterValue):
        utils.map_params(
            ds=CMIP5_TAS_FILE,
            area=("zero", 10, 50, 60),
        )
Exemplo n.º 4
0
def test_map_params_area():
    args = utils.map_params(
        ds=CMIP5_TAS_FILE,
        area=(0, 10, 50, 60),
    )
    assert args["lon_bnds"] == (0, 50)
    assert args["lat_bnds"] == (10, 60)
    # allow also strings
    args = utils.map_params(
        ds=CMIP5_TAS_FILE,
        area=("0", "10", "50", "60"),
    )
    assert args["lon_bnds"] == (0, 50)
    assert args["lat_bnds"] == (10, 60)
Exemplo n.º 5
0
def test_map_params():
    args = utils.map_params(
        time=("1999-01-01T00:00:00", "2100-12-30T00:00:00"),
        space=(-5.0, 49.0, 10.0, 65),
        level=(1000.0, ),
    )
    assert args["start_date"] == "1999"
    assert args["end_date"] == "2100"
    assert args["lon_bnds"] == (-5, 10)
    assert args["lat_bnds"] == (49, 65)
Exemplo n.º 6
0
def subset(
    ds,
    time=None,
    area=None,
    level=None,
    output_dir=None,
    output_type="netcdf",
    split_method="time:auto",
    file_namer="standard",
):
    """
    Example:
        ds: Xarray Dataset
        time: ("1999-01-01T00:00:00", "2100-12-30T00:00:00")
        area: (-5.,49.,10.,65)
        level: (1000.,)
        output_dir: "/cache/wps/procs/req0111"
        output_type: "netcdf"
        split_method: "time:auto"
        file_namer: "standard"

    :param ds:
    :param time:
    :param area:
    :param level:
    :param output_dir:
    :param output_type:
    :param split_method:
    :param file_namer:
    :return:
    """

    # Convert all inputs to Xarray Datasets
    if isinstance(ds, str):
        ds = xr.open_mfdataset(ds, use_cftime=True, combine="by_coords")

    LOGGER.debug(f"Mapping parameters: time: {time}, area: {area}, level: {level}")
    args = utils.map_params(ds, time, area, level)

    subset_ds = _subset(ds, args)

    outputs = []
    namer = get_file_namer(file_namer)()

    time_slices = get_time_slices(subset_ds, split_method)

    for tslice in time_slices:

        result_ds = subset_ds.sel(time=slice(tslice[0], tslice[1]))
        LOGGER.info(f"Processing subset for times: {tslice}")

        output = get_output(result_ds, output_type, output_dir, namer)
        outputs.append(output)

    return outputs
Exemplo n.º 7
0
def _subset(dset, time=None, space=None, level=None):
    logging.debug(f"Before mapping args: {time}, {space}, {level}")
    args = utils.map_params(time, space, level)
    if space:
        # subset with space and optionally time
        logging.debug(f"subset_bbox with args: {args}")
        result = subset_bbox(dset, **args)
    else:
        # subset with time only
        logging.debug(f"subset_time with args: {args}")
        result = subset_time(dset, **args)
    return result
Exemplo n.º 8
0
def test_map_params():
    args = utils.map_params(
        ds=CMIP5_TAS_FILE,
        time=("1999-01-01T00:00:00", "2100-12-30T00:00:00"),
        area=(-5.0, 49.0, 10.0, 65),
        level=(1000.0, 1000.0),
    )

    # have a look at what date was used in clisops master
    assert args["start_date"] == "1999-01-01T00:00:00"
    assert args["end_date"] == "2100-12-30T00:00:00"
    assert args["lon_bnds"] == (-5, 10)
    assert args["lat_bnds"] == (49, 65)
Exemplo n.º 9
0
def test_map_params_missing_param():
    with pytest.raises(MissingParameterValue):
        utils.map_params()
Exemplo n.º 10
0
def test_map_params_invalid_space():
    with pytest.raises(InvalidParameterValue):
        utils.map_params(space=(0, 10, 50), )
    with pytest.raises(InvalidParameterValue):
        utils.map_params(space=("zero", 10, 50, 60), )
Exemplo n.º 11
0
def test_map_params_invalid_time():
    with pytest.raises(InvalidParameterValue):
        utils.map_params(time=("1999-01-01T00:00:00", "maybe tomorrow"), )
    with pytest.raises(InvalidParameterValue):
        utils.map_params(time=("", "2100"), )
Exemplo n.º 12
0
def test_map_params_time():
    args = utils.map_params(time=("1999-01-01", "2100-12"), )
    assert args["start_date"] == "1999"
    assert args["end_date"] == "2100"
Exemplo n.º 13
0
def test_map_params_time():
    args = utils.map_params(
        ds=CMIP5_TAS_FILE, time=("1999-01-01", "2100-12"), area=(0, -90, 360, 90)
    )
    assert args["start_date"] == "1999-01-01T00:00:00"
    assert args["end_date"] == "2100-12-30T00:00:00"