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

Feature/filters for carra #149

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Keep it human-readable, your future self will thank you!

- Call filters from anemoi-transform
- make test optional when adls is not installed Pull request #110
- Add wz_to_w, orog_to_z, and sum filters (#149)

## [0.5.8](https://github.com/ecmwf/anemoi-datasets/compare/0.5.7...0.5.8) - 2024-10-26

Expand Down
58 changes: 58 additions & 0 deletions src/anemoi/datasets/create/functions/filters/orog_to_z.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# (C) Copyright 2024 Anemoi contributors.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.


from collections import defaultdict

from earthkit.data.indexing.fieldlist import FieldArray


class NewDataField:
def __init__(self, field, data, new_name):
self.field = field
self.data = data
self.new_name = new_name

def to_numpy(self, *args, **kwargs):
return self.data

def metadata(self, key=None, **kwargs):
if key is None:
return self.field.metadata(**kwargs)

value = self.field.metadata(key, **kwargs)
if key == "param":
return self.new_name
return value

def __getattr__(self, name):
return getattr(self.field, name)


def execute(context, input, orog, z="z"):
"""Convert orography [m] to z (geopotential height)"""
result = FieldArray()

processed_fields = defaultdict(dict)

for f in input:
key = f.metadata(namespace="mars")
param = key.pop("param")
if param == orog:
key = tuple(key.items())

if param in processed_fields[key]:
raise ValueError(f"Duplicate field {param} for {key}")

output = f.to_numpy(flatten=True) * 9.80665
result.append(NewDataField(f, output, z))
else:
result.append(f)

return result
71 changes: 71 additions & 0 deletions src/anemoi/datasets/create/functions/filters/sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# (C) Copyright 2024 Anemoi contributors.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.


from collections import defaultdict

from earthkit.data.indexing.fieldlist import FieldArray


class NewDataField:
def __init__(self, field, data, new_name):
self.field = field
self.data = data
self.new_name = new_name

def to_numpy(self, *args, **kwargs):
return self.data

def metadata(self, key=None, **kwargs):
if key is None:
return self.field.metadata(**kwargs)

value = self.field.metadata(key, **kwargs)
if key == "param":
return self.new_name
return value

def __getattr__(self, name):
return getattr(self.field, name)


def execute(context, input, params, output):
"""Computes the sum over a set of variables"""
result = FieldArray()

needed_fields = defaultdict(dict)

for f in input:
key = f.metadata(namespace="mars")
param = key.pop("param")
if param in params:
key = tuple(key.items())

if param in needed_fields[key]:
raise ValueError(f"Duplicate field {param} for {key}")

needed_fields[key][param] = f
else:
result.append(f)

for keys, values in needed_fields.items():

if len(values) != len(params):
raise ValueError("Missing fields")

s = None
for k, v in values.items():
c = v.to_numpy(flatten=True)
if s is None:
s = c
else:
s += c
result.append(NewDataField(values[list(values.keys())[0]], s, output))

return result
79 changes: 79 additions & 0 deletions src/anemoi/datasets/create/functions/filters/wz_to_w.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# (C) Copyright 2024 Anemoi contributors.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.


from collections import defaultdict

from earthkit.data.indexing.fieldlist import FieldArray


class NewDataField:
def __init__(self, field, data, new_name):
self.field = field
self.data = data
self.new_name = new_name

def to_numpy(self, *args, **kwargs):
return self.data

def metadata(self, key=None, **kwargs):
if key is None:
return self.field.metadata(**kwargs)

value = self.field.metadata(key, **kwargs)
if key == "param":
return self.new_name
return value

def __getattr__(self, name):
return getattr(self.field, name)


def execute(context, input, wz, t, w="w"):
"""Convert geometric vertical velocity (m/s) to vertical velocity (Pa / s)"""
result = FieldArray()

params = (wz, t)
pairs = defaultdict(dict)

for f in input:
key = f.metadata(namespace="mars")
param = key.pop("param")
if param in params:
key = tuple(key.items())

if param in pairs[key]:
raise ValueError(f"Duplicate field {param} for {key}")

pairs[key][param] = f
if param == t:
result.append(f)
else:
result.append(f)

for keys, values in pairs.items():

if len(values) != 2:
raise ValueError("Missing fields")

wz_pl = values[wz].to_numpy(flatten=True)
t_pl = values[t].to_numpy(flatten=True)
pressure = keys[4][1] * 100 # TODO: REMOVE HARDCODED INDICES

w_pl = wz_to_w(wz_pl, t_pl, pressure)
result.append(NewDataField(values[wz], w_pl, w))

return result


def wz_to_w(wz, t, pressure):
g = 9.81
Rd = 287.058

return -wz * g * pressure / (t * Rd)
Loading