Skip to content

earthkitfieldlambda: specify fn as string not working #154

@bastien-francois

Description

@bastien-francois

What happened?

When specifying the parameter fn for the earthkitfieldlambda filter as a string like this:

    - earthkitfieldlambda:
        param: r
        fn: "lambda x, s: x.clone(values=x.values * s)"
        fn_args: [100]

It tries to import it directly:
ValueError: Could not import function lambda x, s: x.clone(values=x.values * s)

This error could be solved by simply modifying init() of EarthkitFieldLambdaFilter:

self.fn = eval(fn) instead of self.fn = self._import_fn(fn) if isinstance(fn, str) else fn

Optimally, the _import_fn() function might be modified to differentiate when the string is in the form "package.module.function" or a Python function "lambda x, s: x.clone(values=x.values * s)".

What are the steps to reproduce the bug?

Applying the filter earthkitfieldlambda with Python function as string does not work properly. Example of a recipe to reproduce the issue:

input:
  join: 
  - pipe:
    - join:
      - grib:
          path: path1
          param: [ t ]
          level: [2]
          typeOfLevel: heightAboveGround
          levtype: sfc
      - grib:
          path: path1
          param: [ r ]
          typeOfLevel: heightAboveGround
          level: [2]
          levtype: sfc
    - earthkitfieldlambda:
        param: r
        fn: "lambda x, s: x.clone(values=x.values * s)"
        fn_args: [100]
    - r_to_d:
        relative_humidity: r
        temperature: t
        dewpoint: 2d

Version

0.1.16

Platform (OS and architecture)

Linux pc190605.dynamic.knmi.nl 6.15.3-100.fc41.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Jun 19 15:09:31 UTC 2025 x86_64 GNU/Linux

Relevant log output

ValueError: Could not import function lambda x, s: x.clone(values=x.values * s)
2025-08-11 08:09:31 ERROR 
💣 Could not import function lambda x, s: x.clone(values=x.values * s)

Accompanying data

No response

Organisation

KNMI

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    To be triaged

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions