Skip to content

Conversation

@sandorkertesz
Copy link
Collaborator

@sandorkertesz sandorkertesz commented May 1, 2025

This now works for +, -, *, /, %, //, ** and for their 'r' versions. Unary +- are also implemented.

import earthkit.data as ekd
import numpy as np

# ds contains 2 fields with the same shape
ds = ekd.from_source("sample", "test.grib")

arr0 = [1,2]
arr1 = ds[0].values + 1
arr2 = ds.values + 1

# left operand
r = ds + ds
r = ds + ds[0]
r = ds + 1
r = ds + arr0
r = ds + arr1
r = ds + arr2
r = ds[0] + 1
r = ds[0] + arr1

# right operand
r = ds[0] + ds
r = 1 + ds

# ufunc
def func(x, y):
    return np.sin(x) + 2*y

# math methods
r = ds.sin()
r = ds[0].sin()

ds1 = ds + 1
r = ekd.utils.compute.apply_ufunc(func, ds, ds1)

TODO:

  • comparison operators ?
  • add control on how the resulting fields are stored. Currently all are in memory
  • clarify relation to earthkit-transforms
  • refactor field/fieldlist so that cloning should work for any derived field classes automatically

Problems:

In earthkit-data the + operator was already used as the concatenation operator. In Python it is not possible to add custom operators so we need to use a method e.g. concat() in the future. E.g.

# addition
r = ds1 + ds2 

# concat
r = ds1.concat(ds2)
# or
r = ekd.concat(ds1, ds2)

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