def test_get_dz(self):
        mdo = self.mdo
        ds = mdo.dataset
        dz = mdo.get_dz("CWD")

        self.assertEqual(dz.name, "dz")
        self.assertEqual(dz.unit, "m")
        res2 = ds["dz"]
        self.assertTrue(np.all(dz.data == res2[:]))

        ## no layers, no grid
        mdo = self.mdos_inc[0]
        dz = mdo.get_dz("CWD")
        self.assertTrue(np.all(dz.data == np.array([1])))
        self.assertEqual(dz.unit, "1")

        ## test manually given dz variable
        mdo = self.mdo
        dz = Variable(name="dz", data=np.cumsum(np.arange(10)), unit="km")
        time = Variable(data=np.arange(10), unit="d")
        mdo_dz = ModelDataObject(
            model_structure=mdo.model_structure,
            dataset=mdo.dataset,
            dz_var_names={"dz": dz},
            nstep=mdo.nstep,
            stock_unit=mdo.stock_unit,
            time=time,
        )

        dz = mdo_dz.get_dz("CWD")
        self.assertEqual(dz.name, "dz")
        self.assertEqual(dz.unit, "km")
        res2 = np.cumsum(np.arange(10))
        self.assertTrue(np.all(dz.data == res2[:]))
Exemple #2
0
def check_data_consistency(ds_single_site, time_step_in_days):
    ms = load_model_structure_with_vegetation()

    # data_test.py says tht for labile 31.0 is constantly right
    time = Variable(name="time",
                    data=np.arange(len(ds_single_site.time)) *
                    time_step_in_days,
                    unit="d")

    mdo = ModelDataObject(model_structure=ms,
                          dataset=ds_single_site,
                          stock_unit="gC/m2",
                          time=time)

    abs_err, rel_err = mdo.check_data_consistency()
    return abs_err, rel_err
Exemple #3
0
def load_mdo_12C(parameter_set):
    #    dataset         = parameter_set['dataset']
    ds_filename = parameter_set["ds_filename"]
    stock_unit = parameter_set["stock_unit"]
    cftime_unit_src = parameter_set["cftime_unit_src"]
    calendar_src = parameter_set["calendar_src"]
    nr_layers = parameter_set["nr_layers"]

    dz_var_name = parameter_set.get("dz_var_name", None)
    dz_var_names = parameter_set.get("dz_var_names", dict())
    min_time = parameter_set.get("min_time", 0)
    max_time = parameter_set.get("max_time", None)
    nstep = parameter_set.get("nstep", 1)
    time_shift = parameter_set.get("time_shift", 0)

    mdo_12C = ModelDataObject(
        model_structure=model_structure,
        #        dataset         = dataset,
        ds_filename=ds_filename,
        nstep=nstep,
        stock_unit=stock_unit,
        cftime_unit_src=cftime_unit_src,
        calendar_src=calendar_src,
        min_time=min_time,
        max_time=max_time,
        time_shift=time_shift,
        dz_var_names=dz_var_names,
    )

    return mdo_12C
Exemple #4
0
def _load_mdo(ds_dict, time_step_in_days, check_units=True): # time step in days
    ms = load_model_structure()

    # no unit support for dictionary version
    time = Variable(
        name="time",
        data=np.arange(len(ds_dict['time'])) * time_step_in_days,
        unit="d"
#        unit="1"
    )

    mdo = ModelDataObject(
        model_structure=ms,
        dataset=ds_dict, 
        stock_unit="gC/m2", 
#        stock_unit="1", 
        time=time,
        check_units=check_units
    )

    return mdo
Exemple #5
0
def load_mdo(ds, parameter_set):
    nstep = parameter_set.get("nstep", 1)
    stock_unit = parameter_set["stock_unit"]
    nr_layers = parameter_set["nr_layers"]
    dz_var_name = parameter_set["dz_var_name"]
    dz_var_names = parameter_set["dz_var_names"]

    ms = load_model_structure(nr_layers, dz_var_name)

    time = Variable(name="time", data=np.arange(len(ds.time)), unit="d")

    mdo = ModelDataObject(
        model_structure=ms,
        dataset=ds,
        nstep=nstep,
        stock_unit=stock_unit,
        time=time,
        dz_var_names=dz_var_names,
    )

    return mdo
def load_mdo(ds):
    #    days_per_month = np.array(np.diff(ds.time), dtype='timedelta64[D]').astype(float)

    ms = load_model_structure()

    # bring fluxes from gC/m2/day to gC/m2/month

    ## all months are supposed to comprise 365.25/12 days
    days_per_month = 365.25 / 12.0

    time = Variable(
        name="time",
        data=np.arange(len(ds.time)) * days_per_month,
        unit="d"
    )

    mdo = ModelDataObject(
        model_structure=ms,
        dataset=ds, 
        stock_unit="gC/m2", 
        time=time
    )
    return mdo
Exemple #7
0
def load_mdo_14C(parameter_set):
    #    dataset         = parameter_set['dataset']
    ds_filename = parameter_set["ds_filename"]
    stock_unit = parameter_set["stock_unit"]
    cftime_unit_src = parameter_set["cftime_unit_src"]
    calendar_src = parameter_set["calendar_src"]
    nr_layers = parameter_set["nr_layers"]

    dz_var_name = parameter_set.get("dz_var_name", None)
    dz_var_names = parameter_set.get("dz_var_names", dict())
    min_time = parameter_set.get("min_time", 0)
    max_time = parameter_set.get("max_time", None)
    nstep = parameter_set.get("nstep", 1)
    time_shift = parameter_set.get("time_shift", 0)

    pool_structure_14C = [
        {
            "pool_name": "CWD",
            "stock_var": "C14_CWDC_vr",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Litter 1",
            "stock_var": "C14_LITR1C_vr",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Litter 2",
            "stock_var": "C14_LITR2C_vr",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Litter 3",
            "stock_var": "C14_LITR3C_vr",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Soil 1",
            "stock_var": "C14_SOIL1C_vr",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Soil 2",
            "stock_var": "C14_SOIL2C_vr",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Soil 3",
            "stock_var": "C14_SOIL3C_vr",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
    ]

    external_input_structure_14C = {
        "CWD": [
            "C14_fire_mortality_c_to_cwdc",
            "C14_gap_mortality_c_to_cwdc",
            "C14_harvest_c_to_cwdc",
        ],
        "Litter 1": [
            "C14_gap_mortality_c_to_litr_met_c",
            "C14_harvest_c_to_litr_met_c",
            "C14_m_c_to_litr_met_fire",
            "C14_phenology_c_to_litr_met_c",
        ],
        "Litter 2": [
            "C14_gap_mortality_c_to_litr_cel_c",
            "C14_harvest_c_to_litr_cel_c",
            "C14_m_c_to_litr_cel_fire",
            "C14_phenology_c_to_litr_cel_c",
        ],
        "Litter 3": [
            "C14_gap_mortality_c_to_litr_lig_c",
            "C14_harvest_c_to_litr_lig_c",
            "C14_m_c_to_litr_lig_fire",
            "C14_phenology_c_to_litr_lig_c",
        ],
    }

    external_output_structure_14C = {
        "CWD":
        ["C14_CWD_HR_L2_vr", "C14_CWD_HR_L3_vr", "C14_M_CWDC_TO_FIRE_vr"],
        "Litter 1": ["C14_LITR1_HR_vr", "C14_M_LITR1C_TO_FIRE_vr"],
        "Litter 2": ["C14_LITR2_HR_vr", "C14_M_LITR2C_TO_FIRE_vr"],
        "Litter 3": ["C14_LITR3_HR_vr", "C14_M_LITR2C_TO_FIRE_vr"],
        "Soil 1": ["C14_SOIL1_HR_S2_vr", "C14_SOIL1_HR_S3_vr"],
        "Soil 2": ["C14_SOIL2_HR_S1_vr", "C14_SOIL2_HR_S3_vr"],
        "Soil 3": ["C14_SOIL3_HR_vr"],
    }

    model_structure_14C = ModelStructure(
        pool_structure=pool_structure_14C,
        external_input_structure=external_input_structure_14C,
        horizontal_structure=[],
        external_output_structure=external_output_structure_14C,
    )

    mdo_14C = ModelDataObject(
        model_structure=model_structure_14C,
        #        dataset         = dataset,
        ds_filename=ds_filename,
        nstep=nstep,
        stock_unit=stock_unit,
        cftime_unit_src=cftime_unit_src,
        calendar_src=calendar_src,
        min_time=min_time,
        max_time=max_time,
        time_shift=time_shift,
        dz_var_names=dz_var_names,
    )

    return mdo_14C
Exemple #8
0
def MDO_3pools_3layers():
    ## set up test model strcture
    nr_layers = 3
    dz_var_name = "dz"

    pool_structure = [
        {
            "pool_name": "CWD",
            "stock_var": "CWDC",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Litter",
            "stock_var": "LITRC",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Soil",
            "stock_var": "SOILC",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
    ]

    external_input_structure = {
        "CWD": ["fire_CWD", "gap_CWD", "harvest_CWD"],
        "Litter": ["gap_LITR", "harvest_LITR", "m_c_LITR", "phenology_LITR"],
    }

    horizontal_structure = {
        ("CWD", "Litter"): ["CWD_TO_LITR"],
        ("Litter", "Soil"): ["LITR_TO_SOIL"],
    }

    vertical_structure = {
        "Litter": {
            "to_below": ["LITR_flux_down_tb"],
            "from_below": ["LITR_flux_up_fb"],
            "to_above": [],
            "from_above": [],
        },
        "Soil": {
            "to_below": [],
            "from_below": [],
            "to_above": ["SOIL_flux_up_ta"],
            "from_above": ["SOIL_flux_down_fa"],
        },
    }

    external_output_structure = {
        "CWD": ["CWD_HR1", "CWD_HR2"],
        "Litter": ["LITR_HR"],
        "Soil": ["SOIL_HR1", "SOIL_HR2"],
    }

    model_structure = ModelStructure(
        pool_structure=pool_structure,
        external_input_structure=external_input_structure,
        horizontal_structure=horizontal_structure,
        vertical_structure=vertical_structure,
        external_output_structure=external_output_structure,
    )

    ## set up a test dataset
    time = np.arange(10)
    dz = np.arange(nr_layers) + 1

    ds = Dataset("test_dataset.nc", "w", diskless=True, persist=False)
    ds.createDimension("time", len(time))
    ds.createDimension("level", nr_layers)

    time_var = ds.createVariable("time", "f8", ("time", ))
    time_var[:] = time
    time_var.units = "d"

    dz_var = ds.createVariable("dz", "f4", ("level"))
    dz_var[:] = dz
    dz_var.units = "m"

    ## introduce some test variables to test dataset

    ## stocks
    var = ds.createVariable("CWDC", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers).reshape((len(time), nr_layers))
    var[...] = data
    var.units = "gC/m^3"
    var.cell_methods = "time: instantaneous"

    var = ds.createVariable("LITRC", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.1).reshape(
        (len(time), nr_layers))
    var[...] = data
    var.units = "gC/m^3"
    var.cell_methods = "time: instantaneous"

    var = ds.createVariable("SOILC", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.2).reshape(
        (len(time), nr_layers))
    var[...] = data.copy()
    var.units = "gC/m^3"
    var.cell_methods = "time: instantaneous"

    ## external input fluxes
    var = ds.createVariable("fire_CWD", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("gap_CWD", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.01).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("harvest_CWD", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.02).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("gap_LITR", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.10).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("harvest_LITR", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.11).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("m_c_LITR", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.12).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("phenology_LITR", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.13).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    ## horizontal fluxes
    var = ds.createVariable("CWD_TO_LITR", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("LITR_TO_SOIL", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.1).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    ## vertical fluxes
    var = ds.createVariable("LITR_flux_down_tb", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^2/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("LITR_flux_up_fb", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers).reshape(
        (len(time), nr_layers)) * 1e-04
    var[...] = data
    var.units = "gC/m^2/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("SOIL_flux_down_fa", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^2/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("SOIL_flux_up_ta", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers).reshape(
        (len(time), nr_layers)) * 1e-04
    var[...] = data
    var.units = "gC/m^2/s"
    var.cell_methods = "time: mean"

    ## external_output_fluxes
    var = ds.createVariable("CWD_HR1", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("CWD_HR2", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.01).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("LITR_HR", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.10).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("SOIL_HR1", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.20).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("SOIL_HR2", "f8", ("time", "level"))
    data = (np.arange(len(time) * nr_layers) + 0.21).reshape(
        (len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    ## set up a ModelDataObject
    nstep = 2
    stock_unit = "g/m^2"
    #    cftime_unit_src  = 'days since 1851-01-01 00:00:00'
    #    calendar_src     = 'noleap'
    #    time_shift       = 5
    #    max_time         = 8

    time = Variable(data=time, unit="d")
    mdo = ModelDataObject(
        model_structure=model_structure,
        dataset=ds,
        nstep=nstep,
        stock_unit=stock_unit,
        time=time
        #        cftime_unit_src = cftime_unit_src,
        #        calendar_src    = calendar_src,
        #        time_shift      = time_shift,
        #        max_time        = max_time
    )
    return mdo
Exemple #9
0
def MDO_3pools_3layers_discretizable():
    ## set up test model strcture
    nr_layers = 3
    dz_var_name = "dz"

    pool_structure = [
        {
            "pool_name": "CWD",
            "stock_var": "CWDC",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Litter",
            "stock_var": "LITRC",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Soil",
            "stock_var": "SOILC",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
    ]

    external_input_structure = {"CWD": ["input_CWD"], "Litter": ["input_LITR"]}

    horizontal_structure = {
        ("CWD", "Litter"): ["CWD_TO_LITR"],
        ("Litter", "Soil"): ["LITR_TO_SOIL"],
    }

    external_output_structure = {
        "CWD": ["CWD_HR"],
        "Litter": ["LITR_HR"],
        "Soil": ["SOIL_HR"],
    }

    model_structure = ModelStructure(
        pool_structure=pool_structure,
        external_input_structure=external_input_structure,
        horizontal_structure=horizontal_structure,
        #        vertical_structure        = vertical_structure,
        external_output_structure=external_output_structure,
    )

    ## set up a test dataset
    time = np.arange(10)
    dz = np.arange(nr_layers) + 1

    ds = Dataset("test_dataset_discretizable1.nc",
                 "w",
                 diskless=True,
                 persist=False)
    ds.createDimension("time", len(time))
    ds.createDimension("level", nr_layers)

    time_var = ds.createVariable("time", "f8", ("time", ))
    time_var[:] = time
    time_var.units = "d"

    dz_var = ds.createVariable("dz", "f4", ("level"))
    dz_var[:] = dz
    dz_var.units = "m"

    ## introduce some test variables to test dataset

    ## stocks
    var = ds.createVariable("CWDC", "f8", ("time", "level"))
    data = np.ones((len(time), nr_layers)) * 1e05
    var[...] = data
    var.units = "gC/m^3"
    var.cell_methods = "time: instantaneous"

    var = ds.createVariable("LITRC", "f8", ("time", "level"))
    data = np.ones((len(time), nr_layers)) * 1e05
    var[...] = data
    var.units = "gC/m^3"
    var.cell_methods = "time: instantaneous"

    var = ds.createVariable("SOILC", "f8", ("time", "level"))
    data = np.ones((
        len(time),
        nr_layers,
    )) * 1e05
    var[...] = data.copy()
    var.units = "gC/m^3"
    var.cell_methods = "time: instantaneous"

    ## external input fluxes
    var = ds.createVariable("input_CWD", "f8", ("time", "level"))
    data = np.ones((len(time), nr_layers)) * 1e-02
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("input_LITR", "f8", ("time", "level"))
    data = np.ones((len(time), nr_layers)) * 1e-02
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    ## horizontal fluxes
    var = ds.createVariable("CWD_TO_LITR", "f8", ("time", "level"))
    data = np.ones((len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("LITR_TO_SOIL", "f8", ("time", "level"))
    data = np.ones((len(time), nr_layers)) * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    ## external_output_fluxes
    var = ds.createVariable("CWD_HR", "f8", ("time", "level"))
    data = np.ones((len(time), nr_layers)) * 2 * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("LITR_HR", "f8", ("time", "level"))
    data = np.ones((len(time), nr_layers)) * 2 * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    var = ds.createVariable("SOIL_HR", "f8", ("time", "level"))
    data = np.ones((len(time), nr_layers)) * 2 * 1e-03
    var[...] = data
    var.units = "gC/m^3/s"
    var.cell_methods = "time: mean"

    ## set up a ModelDataObject
    nstep = 2
    stock_unit = "g/m^2"
    #    cftime_unit_src  = 'days since 1850-01-01 00:00:00'
    #    calendar_src     = 'noleap'
    #    max_time         = 8

    time = Variable(data=time, unit="d")
    mdo = ModelDataObject(
        model_structure=model_structure,
        dataset=ds,
        nstep=nstep,
        stock_unit=stock_unit,
        time=time
        #        cftime_unit_src = cftime_unit_src,
        #        calendar_src    = calendar_src,
        #        max_time        = max_time
    )
    return mdo
Exemple #10
0
def MDO_3pools_3layers_nogrid():
    ## set up test model strcture

    nr_layers = 3
    dz_var_name = "dz"

    pool_structure = [
        {
            "pool_name": "CWD",
            "stock_var": "CWDC",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Litter",
            "stock_var": "LITRC",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
        {
            "pool_name": "Soil",
            "stock_var": "SOILC",
            "nr_layers": nr_layers,
            "dz_var": dz_var_name,
        },
    ]

    external_input_structure = {
        "CWD": ["fire_CWD"],
    }

    horizontal_structure = {("CWD", "Litter"): ["CWD_TO_LITR"]}

    external_output_structure = {
        "CWD": ["CWD_HR"],
    }

    model_structure = ModelStructure(
        pool_structure=pool_structure,
        external_input_structure=external_input_structure,
        horizontal_structure=horizontal_structure,
        external_output_structure=external_output_structure,
    )

    ## set up a test dataset
    time = np.arange(10)
    dz = np.arange(nr_layers) + 1

    ds = Dataset("test_dataset4.nc", "w", diskless=True, persist=False)
    ds.createDimension("time", len(time))
    ds.createDimension("level", nr_layers)

    time_var = ds.createVariable("time", "f8", ("time", ))
    time_var[:] = time
    time_var.units = "d"

    dz_var = ds.createVariable("dz", "f4", ("level"))
    dz_var[:] = dz
    dz_var.units = "m"

    ## introduce some test variables to test dataset

    ## stocks
    var = ds.createVariable("CWDC", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers)
    var[...] = data.reshape(len(time), nr_layers)
    var.units = "gC14/m^3"
    var.cell_methods = "time: instantaneous"

    var = ds.createVariable("LITRC", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers) + 0.1
    var[...] = data.reshape(len(time), nr_layers)
    var.units = "gC14/m^3"
    var.cell_methods = "time: instantaneous"

    var = ds.createVariable("SOILC", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers) + 0.2
    var[...] = data.reshape(len(time), nr_layers)
    var.units = "gC14/m^3"
    var.cell_methods = "time: instantaneous"

    ## external input fluxes
    var = ds.createVariable("fire_CWD", "f8", ("time", "level"))
    masked_data = np.ma.masked_array(
        data=np.arange(len(time) * nr_layers) * 1e-03,
        mask=np.zeros(len(time) * nr_layers),
    )
    masked_data.mask[21] = 1
    var[...] = masked_data.reshape(len(time), nr_layers)
    var.units = "gC14/m^3/s"
    var.cell_methods = "time: mean"

    ## horizontal fluxes
    var = ds.createVariable("CWD_TO_LITR", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers) * 1e-03
    var[...] = data.reshape(len(time), nr_layers)
    var.units = "gC14/m^3/s"
    var.cell_methods = "time: mean"

    ## external_output_fluxes
    var = ds.createVariable("CWD_HR", "f8", ("time", "level"))
    data = np.arange(len(time) * nr_layers)
    var[...] = data.reshape(len(time), nr_layers)
    var.units = "gC14/m^3/s"
    var.cell_methods = "time: mean"

    ## set up a ModelDataObject
    nstep = 2
    stock_unit = "g/m^2"
    #    cftime_unit_src  = 'days since 1900-01-01 00:00:00'
    #    calendar_src     = 'noleap'
    #    max_time         = 8

    time = Variable(data=time, unit="d")
    mdo = ModelDataObject(
        model_structure=model_structure,
        dataset=ds,
        nstep=nstep,
        stock_unit=stock_unit,
        time=time
        #        cftime_unit_src = cftime_unit_src,
        #        calendar_src    = calendar_src,
        #        max_time        = max_time
    )
    return mdo
Exemple #11
0
def MDO_3pools_nolayers_nogrid():
    ## set up test model strcture

    pool_structure = [
        {
            "pool_name": "CWD",
            "stock_var": "CWDC",
        },
        {
            "pool_name": "Litter",
            "stock_var": "LITRC",
        },
        {
            "pool_name": "Soil",
            "stock_var": "SOILC",
        },
    ]

    external_input_structure = {
        "CWD": ["fire_CWD"],
    }

    horizontal_structure = {
        ("CWD", "Litter"): ["CWD_TO_LITR"],
    }

    external_output_structure = {
        "CWD": ["CWD_HR"],
    }

    model_structure = ModelStructure(
        pool_structure=pool_structure,
        external_input_structure=external_input_structure,
        horizontal_structure=horizontal_structure,
        external_output_structure=external_output_structure,
    )

    ## set up a test dataset
    time = np.arange(10)

    ds = Dataset("test_dataset2.nc", "w", diskless=True, persist=False)
    ds.createDimension("time", len(time))

    time_var = ds.createVariable("time", "f8", ("time", ))
    time_var[:] = time
    time_var.units = "d"

    ## introduce some test variables to test dataset

    ## stocks
    var = ds.createVariable("CWDC", "f8", ("time", ))
    data = np.arange(len(time))
    var[...] = data
    var.units = "gC/m^2"
    var.cell_methods = "time: instantaneous"

    var = ds.createVariable("LITRC", "f8", ("time", ))
    data = np.arange(len(time)) + 0.1
    var[...] = data
    var.units = "gC/m^2"
    var.cell_methods = "time: instantaneous"

    var = ds.createVariable("SOILC", "f8", ("time", ))
    data = np.arange(len(time)) + 0.2
    var[...] = data.copy()
    var.units = "gC/m^2"
    var.cell_methods = "time: instantaneous"

    ## external input fluxes
    var = ds.createVariable("fire_CWD", "f8", ("time", ))
    data = np.arange(len(time)) * 1e-03
    var[...] = data
    var.units = "gC/m^2/s"
    var.cell_methods = "time: mean"

    ## horizontal fluxes
    var = ds.createVariable("CWD_TO_LITR", "f8", ("time", ))
    data = np.arange(len(time)) * 1e-03
    var[...] = data
    var.units = "gC/m^2/s"
    var.cell_methods = "time: mean"

    ## external_output_fluxes
    var = ds.createVariable("CWD_HR", "f8", ("time", ))
    data = np.arange(len(time))
    var[...] = data
    var.units = "gC/m^2/s"
    var.cell_methods = "time: mean"

    ## set up a ModelDataObject
    nstep = 2
    stock_unit = "g/m^2"
    #    cftime_unit_src  = 'days since 1850-01-01 00:00:00'
    #    calendar_src     = 'noleap'
    #    max_time         = 8

    time = Variable(data=time, unit="d")
    mdo = ModelDataObject(
        model_structure=model_structure,
        dataset=ds,
        nstep=nstep,
        stock_unit=stock_unit,
        time=time
        #        cftime_unit_src = cftime_unit_src,
        #        calendar_src    = calendar_src,
        #        max_time        = max_time
    )
    return mdo