Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forcing builder #365

Merged
merged 50 commits into from
Sep 20, 2023
Merged

Forcing builder #365

merged 50 commits into from
Sep 20, 2023

Conversation

sverhoeven
Copy link
Member

@sverhoeven sverhoeven commented Aug 16, 2023

Use fluent builder pattern to construct esmvaltool recipes.

Recipes for models with esmvaltool recipes are build using builder and existing diagnostic.

TODO

  • DefaultForcing
  • GenericDistributedForcing
  • GenericLumpedForcing
  • PCRGlobWBForcing
  • LisfloodForcing
  • MarrmotForcing
  • WflowForcing
  • HypeForcing
  • tests
  • mypy
  • flake8
  • documentation
  • for generic forcing remove '*' from esmvaltool output file names.
  • non ERA5 / ERA-Interim dataset

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Got
Traceback (most recent call last):
  File "/home/verhoes/git/eWaterCycle/ewatercycle/src/ewatercycle/esmvaltool/diagnostic/copy.py", line 7, in <module>
    from esmvaltool.diag_scripts.shared import (
  File "/home/verhoes/mambaforge/envs/ewatercycle/lib/python3.10/site-packages/esmvaltool/diag_scripts/shared/__init__.py", line 2, in <module>
    from . import io, iris_helpers, names, plot
  File "/home/verhoes/mambaforge/envs/ewatercycle/lib/python3.10/site-packages/esmvaltool/diag_scripts/shared/io.py", line 5, in <module>
    from pprint import pformat
  File "/home/verhoes/mambaforge/envs/ewatercycle/lib/python3.10/pprint.py", line 38, in <module>
    import dataclasses as _dataclasses
  File "/home/verhoes/mambaforge/envs/ewatercycle/lib/python3.10/dataclasses.py", line 3, in <module>
    import copy
  File "/home/verhoes/git/eWaterCycle/ewatercycle/src/ewatercycle/esmvaltool/diagnostic/copy.py", line 7, in <module>
    from esmvaltool.diag_scripts.shared import (
ImportError: cannot import name 'ProvenanceLogger' from partially initialized module 'esmvaltool.diag_scripts.shared' (most likely due to a circular import) (/home/verhoes/mambaforge/envs/ewatercycle/lib/python3.10/site-packages/esmvaltool/diag_scripts/shared/__init__.py)
@sverhoeven sverhoeven changed the base branch from default-gridded-forcing to main August 16, 2023 13:34
@BSchilperoort
Copy link
Member

I tried to use the following dataset definition to use CMIP data (as that can be automatically downloaded by esmvaltool. It failed on this error:

Error
File [~/micromamba/envs/ewatercycle310/lib/python3.10/site-packages/esmvalcore/_recipe/check.py:110](https://file+.vscode-resource.vscode-cdn.net/home/bart/git/ewatercycle/~/micromamba/envs/ewatercycle310/lib/python3.10/site-packages/esmvalcore/_recipe/check.py:110), in variable(var, required_keys)
    104 missing = required - set(var)
    105 if missing:
    106     raise RecipeError(
    107         f"Missing keys {missing} in\n"
    108         f"{pformat(var)}\n"
    109         "for variable {var['variable_group']} in diagnostic "
--> 110         f"{var['diagnostic']}")

KeyError: 'diagnostic'
Dataset
from ewatercycle.esmvaltool.models import Dataset
cmip_dataset = Dataset(**{
    "dataset": "EC-Earth3",
    "project": "CMIP6", 
    "grid": "gr", 
    "exp": ["historical",], 
    "ensemble": "r6i1p1f1",
    "start_year": 2000,
    "end_year": 2022,
})
forcing = GenericDistributedForcing.generate(
    dataset=cmip_dataset,
    start_time="2020-01-01T00:00:00Z",
    end_time="2020-01-01T00:00:00Z",
    shape="/home/bart/git/ewatercycle/src/ewatercycle/testing/data/Rhine/Rhine.shp",
)

'*' in forcing files was caused by version attribute in Dataset being silently forgotten.
By allowing extra attributes the generate file has version value instead of `*`.
@sverhoeven
Copy link
Member Author

Your CMIP example now works and is part of docstring at https://ewatercycle--365.org.readthedocs.build/en/365/autoapi/ewatercycle/base/forcing/index.html#ewatercycle.base.forcing.GenericDistributedForcing

@sverhoeven sverhoeven marked this pull request as ready for review September 4, 2023 12:25
@Peter9192 Peter9192 added this to the V2 milestone Sep 11, 2023
@sverhoeven
Copy link
Member Author

See https://github.com/eWaterCycle/infra/blob/main/README.md#mount-dcache-on-local-machine how to mount ERA data locally.

Copy link
Member

@BSchilperoort BSchilperoort left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've successfully tested generating generic forcing for lumped & distributed data, both CMIP6 and ERA5 👍

Thanks for adding the cmip support, having esmvaltool download the data on the fly makes testing the forcing generation a lot easier!

Copy link
Collaborator

@Peter9192 Peter9192 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work! I really love that we now have standard forcings for generic and lumped models, and that we can support much more ESMValTool functionality, most importantly any dataset.

Since Bart already tried it out, I just quickly skimmed over the code and left a couple of small comments.

src/ewatercycle/base/forcing.py Outdated Show resolved Hide resolved
src/ewatercycle/base/forcing.py Outdated Show resolved Hide resolved
src/ewatercycle/base/model.py Show resolved Hide resolved
src/ewatercycle/esmvaltool/diagnostic/copier.py Outdated Show resolved Hide resolved
src/ewatercycle/forcing.py Show resolved Hide resolved
src/ewatercycle/plugins/hype/forcing.py Outdated Show resolved Hide resolved
src/ewatercycle/plugins/hype/forcing.py Show resolved Hide resolved
@@ -0,0 +1,65 @@
from pathlib import Path
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the name of this file test_models to be quite confusing. Can we make it more pydantic-specific?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are confused these models with hydrological models

I could rename the module to

  1. test_schema
  2. test_recipe_models
  3. test_io
  4. something else?

That models are implemented in pydantic should not be important to consumers so I dont like adding pydantic in the module name.

To make consistent we should also rename ewatercycle.esmvaltool.models .

I like schema.
@Peter9192 do you have a preference or suggestion?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like schema too

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

schema it is

When we talk about models we mean hydrological models.
@sonarcloud
Copy link

sonarcloud bot commented Sep 20, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 26 Code Smells

90.7% 90.7% Coverage
0.0% 0.0% Duplication

@sverhoeven
Copy link
Member Author

Thanks for testing and reviewing.

@sverhoeven sverhoeven merged commit 9cc544d into main Sep 20, 2023
5 checks passed
@sverhoeven sverhoeven deleted the forcing_builder branch September 20, 2023 08:49
sverhoeven added a commit that referenced this pull request Oct 5, 2023
* Replace test_abstract with base/test_model

* Add more tests for base.model module

* Dont depend on fake models from grpc4bmi

* Make type of eWaterCycleModel.parameters property an ItemsView

To be more inline with pymt

See #365 (comment)

* Use local var instead of prop

* Fix more tests

* More and better tests

* Use public interface where possible

* Update model.py

* Sort imports
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants