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

Implement the Prism DEM datasets, add tests. #28

Merged
merged 3 commits into from
Aug 16, 2023

Conversation

BSchilperoort
Copy link
Contributor

A demo notebook is included in demo/prism_dem_demo.ipynb

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copy link
Member

@geek-yang geek-yang left a comment

Choose a reason for hiding this comment

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

Nice work @BSchilperoort . Very clean implementation. One small issue about the recipe. If you fix the time thingy for xarray (just like canopy-height dataset), then everything should work.

Tbh, at the beginning I thought the implementation of prism is the same as canopy and many things can be merged. But then when diving into the details, just notice that many things are actually different. Devils in the details 😂.

Comment on lines +252 to +261
# The prism DEM variable & coords already follow the CF/Zampy convention
for variable in ds.variables:
variable_name = str(variable) # Cast to string to please mypy.
if variable_name != "time":
ds[variable_name].attrs["units"] = str(
VARIABLE_REFERENCE_LOOKUP[variable_name].unit
)
ds[variable_name].attrs["description"] = VARIABLE_REFERENCE_LOOKUP[
variable_name
].desc
Copy link
Member

Choose a reason for hiding this comment

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

This is duplicated:

# All eth variables & coords already follow the CF/Zampy convention
for variable in ds.variables:
variable_name = str(variable) # Cast to string to please mypy.
if variable_name != "time": # TODO: Check how to write time attrs...
ds[variable_name].attrs["units"] = str(
VARIABLE_REFERENCE_LOOKUP[variable_name].unit
)
ds[variable_name].attrs["description"] = VARIABLE_REFERENCE_LOOKUP[
variable_name
].desc

Well....It is a very tiny part of the code. If you like, we can keep it for now (since snonarcloud doesn't cry 😂 ). Later when we have more datasets which again touches the same code, we can merge them, just for quality of life changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yeah, this does overlap. We can probably make it more generic and wrap it into a separate (util) function, however that might be best left for when we make the whole attribute writing more complete.

Copy link
Member

Choose a reason for hiding this comment

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

That's also my thought. Great!

@geek-yang
Copy link
Member

The API works nicely. But when I tried with the recipe, an error occur:

~/EcoExtreML/temp via 🐍 v3.10.12 (ecoextreml) took 2s
❯ zampy prism90_dem_recipe.yml
Executing recipe: prism90_dem_recipe.yml
Copernicus_DSM_30_N50_00_E003_00.tar: 8.97MB [00:01, 5.29MB/s]
elevation renamed to elevation.
Conversion of dataset 'prism-dem-90' following ALMA convention is complete!
Traceback (most recent call last):
  File "/home/yangliu/venv/ecoextreml/lib/python3.10/site-packages/xarray/core/dataset.py", line 1382, in _construct_dataarray
    variable = self._variables[name]
KeyError: 'time'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yangliu/venv/ecoextreml/bin/zampy", line 8, in <module>
    sys.exit(run_recipe())
  File "/home/yangliu/venv/ecoextreml/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/home/yangliu/venv/ecoextreml/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/yangliu/venv/ecoextreml/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/yangliu/venv/ecoextreml/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/yangliu/EcoExtreML/zampy/src/zampy/cli.py", line 13, in run_recipe
    rm.run()
  File "/home/yangliu/EcoExtreML/zampy/src/zampy/recipe.py", line 121, in run
    ds = ds.resample(time=self.frequency).mean()
  File "/home/yangliu/venv/ecoextreml/lib/python3.10/site-packages/xarray/core/dataset.py", line 9322, in resample
    return self._resample(
  File "/home/yangliu/venv/ecoextreml/lib/python3.10/site-packages/xarray/core/common.py", line 982, in _resample
    dim_coord = self[dim]
  File "/home/yangliu/venv/ecoextreml/lib/python3.10/site-packages/xarray/core/dataset.py", line 1473, in __getitem__
    return self._construct_dataarray(key)
  File "/home/yangliu/venv/ecoextreml/lib/python3.10/site-packages/xarray/core/dataset.py", line 1384, in _construct_dataarray
    _, name, variable = _get_virtual_variable(self._variables, name, self.dims)
  File "/home/yangliu/venv/ecoextreml/lib/python3.10/site-packages/xarray/core/dataset.py", line 196, in _get_virtual_variable
    raise KeyError(key)
KeyError: 'time'

Clearly it is due to the missing time axis, which is required in the recipe for saving the output after conversion. But it is easy to fix, just add a time coord, similar to canopy dataset.

da = da.sortby(["x", "y"]) # sort the dims ascending
da = da.isel(band=0) # get rid of band dim
da = da.drop_vars(["band", "spatial_ref"]) # drop unnecessary coords
ds = da.to_dataset()
Copy link
Member

Choose a reason for hiding this comment

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

Add something similar to canopy-height dataset to fix the recipe, for instance:

ds = ds.assign_coords( # halfway in the year
{"time": np.datetime64("2020-07-01").astype("datetime64[ns]")}
)

@BSchilperoort
Copy link
Contributor Author

@geek-yang thanks for the review! I had fixed the missing time dim issue, but seem to have forgotten to commit it 🤦‍♂️. Instead of adding a time coordinate, I modify recipe.py to allow for variables without a time dim.

@sonarcloud
Copy link

sonarcloud bot commented Aug 16, 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 0 Code Smells

90.1% 90.1% Coverage
0.0% 0.0% Duplication

@BSchilperoort BSchilperoort merged commit 9847127 into main Aug 16, 2023
13 of 14 checks passed
@BSchilperoort BSchilperoort deleted the implement-prism-dem branch August 16, 2023 12:23
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.

2 participants