Skip to content

Commit

Permalink
working on w-avg
Browse files Browse the repository at this point in the history
  • Loading branch information
cehbrecht committed Nov 7, 2023
1 parent 1452348 commit 2b40c31
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
- requests
# daops
- cftime>=1.2.1
- xarray>=0.20,<0.22
- xarray>=0.21
- cf_xarray>=0.7,<=0.8.4
- dask>=2021.12
- netcdf4>=1.4
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ daops>=0.8.1,<0.9
clisops>=0.9.6,<0.11
roocs-utils>=0.6.4,<0.7
# roocs-utils @ git+https://github.com/roocs/roocs-utils@master#egg=roocs-utils
xarray>=0.20,<0.22
xarray>=0.21
cf-xarray<=0.8.4
dask[complete]
netcdf4
Expand Down
1 change: 1 addition & 0 deletions rook/processes/wps_average_weighted.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def _handler(self, request, response):
inputs = {
"collection": collection,
"output_dir": self.workdir,
"dims": ["latitude", "longitude"],
"apply_fixes": False,
"pre_checked": False,
}
Expand Down
37 changes: 19 additions & 18 deletions rook/utils/weighted_average_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@
from daops.ops.base import Operation
from daops.utils import normalise

from clisops.ops.average import average_over_dims as average
from clisops.ops import subset

# from clisops.ops.average import average_over_dims as average


def apply_weights(ds):
ds["time"] = ds["time"].astype("int64")
ds["time_bnds"] = ds["time_bnds"].astype("int64")
# ds["time"] = ds["time"].astype("int64")
# ds["time_bnds"] = ds["time_bnds"].astype("int64")
# weights
weights = np.cos(np.deg2rad(ds.lat))
weights.name = "weights"
weights.fillna(0)
# apply weights
ds_weighted = ds.weighted(weights)
return ds_weighted
# apply mean
ds_weighted_mean = ds_weighted.mean(["lat", "lon"])
return ds_weighted_mean


class WeightedAverage(Operation):
Expand Down Expand Up @@ -65,27 +69,23 @@ def _calculate(self):

rs = normalise.ResultSet(vars())

# dims = dimension_parameter.DimensionParameter(
# self.params.get("dims", None)
# ).value

# apply weights
datasets = []
for ds_id in norm_collection.keys():
ds = norm_collection[ds_id]
ds_mod = apply_weights(ds)
datasets.append(ds_mod)

dims = dimension_parameter.DimensionParameter(
self.params.get("dims", None)
).value

# processed_ds = xr.concat(
# datasets,
# "time",
# )
processed_ds = datasets[0]

# average over dimensions
outputs = average(
# processed_ds,
datasets,
dims=dims,
# subset
outputs = subset(
processed_ds,
time=None,
output_type="nc",
)
# result
Expand All @@ -101,12 +101,13 @@ def run_weighted_average(args):

def weighted_average(
collection,
dims=None,
ignore_undetected_dims=False,
output_dir=None,
output_type="netcdf",
split_method="time:auto",
file_namer="standard",
apply_fixes=False,
):
result_set = WeightedAverage(**locals()).calculate()
result_set = WeightedAverage(**locals())._calculate()
return result_set
23 changes: 22 additions & 1 deletion tests/test_wps_average_weighted.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest

import xarray as xr
from bs4 import BeautifulSoup

from pywps import Service
from pywps.tests import assert_response_success, client_for

Expand All @@ -8,7 +11,23 @@
from .common import PYWPS_CFG, get_output


def test_wps_average_weighted_cmip6():
def extract_paths_from_metalink(path):
path = path.replace("file://", "")
doc = BeautifulSoup(open(path, "r").read(), "xml")
paths = [el.text.replace("file://", "") for el in doc.find_all("metaurl")]
return paths


def assert_weighted_average(path):
assert "meta4" in path
paths = extract_paths_from_metalink(path)
assert len(paths) > 0
print(paths)
# ds = xr.open_dataset(paths[0])
# assert "time" in ds.coords


def test_wps_weighted_average_cmip6():
# test the case where the inventory is used
client = client_for(Service(processes=[WeightedAverage()], cfgfiles=[PYWPS_CFG]))
datainputs = "collection=c3s-cmip6.ScenarioMIP.INM.INM-CM5-0.ssp245.r1i1p1f1.Amon.rlds.gr1.v20190619"
Expand All @@ -18,3 +37,5 @@ def test_wps_average_weighted_cmip6():
print(resp)
assert_response_success(resp)
assert "output" in get_output(resp.xml)
assert_weighted_average(path=get_output(resp.xml)["output"])
assert False

0 comments on commit 2b40c31

Please sign in to comment.