MetOffice / CSET

Toolkit for evaluation and investigation of numerical models for weather and climate applications.
https://metoffice.github.io/CSET/
Apache License 2.0
8 stars 1 forks source link

Interpolate to nearest hour when time is only slightly offset #659

Open jfrost-mo opened 1 month ago

jfrost-mo commented 1 month ago
ValueError: Constraint doesn't produce single cube. ConstraintCombination(ConstraintCombination(ConstraintCombination(ConstraintCombination(Constraint(), Constraint(name='air_temperature'), <built-in function and_>), Constraint(cube_func=<function generate_cell_methods_constraint.<locals>.check_cell_methods at 0x7f82d8fc4cc0>), <built-in function and_>), Constraint(cube_func=<function generate_pressure_level_constraint.<locals>.no_pressure_coordinate at 0x7f82d8fc4e00>), <built-in function and_>), Constraint(coord_values={'time': <function generate_time_constraint.<locals>.<lambda> at 0x7f82d8fc4ea0>}), <built-in function and_>)
< No cubes >

The error is quite obvious, and I am wondering whether it is due to the time constraint. For example, in job.err, you can see the variables defined to produce the constraints: 024-05-27 02:44:52,706 DEBUG Recipe variables: {'VARNAME': 'air_temperature', 'VALIDITY_TIME': '20220716T0000Z'}

However, the first model output of the data is offset by +12 seconds, which corresponds to the first model time step. This can be verified on an interactive python interpreter session:

...
>>> print(cb)
air_temperature / (K)               (time: 13; grid_latitude: 1250; grid_longitude: 1800)
    Dimension coordinates:
        time                             x                  -                     -
        grid_latitude                    -                  x                     -
        grid_longitude                   -                  -                     x
    Auxiliary coordinates:
        forecast_period                  x                  -                     -
    Scalar coordinates:
        forecast_reference_time     2022-07-16 00:00:00
        height                      1.5 m
    Attributes:
        STASH                       m01s03i236
        source                      'Data from Met Office Unified Model'
        um_version                  '13.0'

>>> cube_dt = cf_units.num2date(cb.coord('time').points, cb.coord('time').units.name, calendar='standard')
>>> cube_dt
array([cftime.DatetimeGregorian(2022, 7, 16, 0, 0, 12, 0, has_year_zero=False),
       cftime.DatetimeGregorian(2022, 7, 16, 1, 0, 0, 0, has_year_zero=False),
...

If this (time offset) indeed is the case, an option is to interpolate to the nearest hour.

Originally posted by @vinodk-bom in https://github.com/MetOffice/CSET/issues/472#issuecomment-2132556048