Skip to content

Refactor methods to operate on and return DataArrays #7

@jgerardsimcock

Description

@jgerardsimcock
  • climate_toolbox version:
  • Python version:
  • Operating System:

Description

Right now methods for projecting from gridded to iso/hierid operate on datasets. This is kind of a problem when we have multiple climate variables in the same dataset (spline-tas, degree-days) or something like below.

Dimensions:  (lat: 720, lon: 1440, time: 365)
Coordinates:
  * time     (time) datetime64[ns] 2006-01-01T12:00:00 2006-01-02T12:00:00 ...
  * lat      (lat) float32 -89.875 -89.625 -89.375 -89.125 -88.875 -88.625 ...
  * lon      (lon) float32 0.125 0.375 0.625 0.875 1.125 1.375 1.625 1.875 ...
Data variables:
    tas      (time, lat, lon) float32 271.728 271.744 271.745 271.733 ...
    tas_2    (time, lat, lon) float32 73836.0 73844.9 73845.5 73838.8 ...
    tas_3    (time, lat, lon) float32 2.00633e+07 2.00669e+07 2.00672e+07 ...

I am uncomfortable with the current implementation where, in order to project to hierid, we would have to iterate over each variable like the following:

for var in ds.data_vars.keys():
            ds = weighted_aggregate_grid_to_regions(
                ds, var, aggwt, agglev, weights=weights)

The assignment and reassingment of datasets in xarray makes me uncomfortable.

One possible implementation:

xformd_das = []
vars = []
for var in ds.data_vars.keys():
        da = ds[var]
        da = weighted_aggregate_grid_to_regions(da, aggwt, agglev, weights
        xformd.append(da)
        vars.append(var)

da = xr.concat(xformd_das, pd.Index(vars))
...

da.to_netcdf(...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions