OpenDrift / opendrift

Open source framework for ocean trajectory modelling
https://opendrift.github.io
GNU General Public License v2.0
249 stars 120 forks source link

Dropping variables from ROMS reader that won't be used #1226

Closed kthyng closed 9 months ago

kthyng commented 9 months ago

Hi! I'd like to drop variables from the ROMS reader (Dataset and "required_variables") that won't be used but are listed in the reader "required_variables". For example, if o.disable_vertical_motion() is True, "upward_sea_water_velocity" should be removed from "required_variables" so that it isn't read in. Another circumstances would be to remove wind variables if they won't be used, though this might be more complicated to track down if they might be used or not.

I am going to either implement this in the ROMS reader or in my code wrapping opendrift — do you have a preference for where I do this?

knutfrode commented 9 months ago

Hi,

For the ROMS (native and generic) readers, I believe you can open the datasets with Xarray, drop variables (and do other things), and then create the reader from this modified dataset: r = Reader(ds)

Alternatively, you can avoid variables being read by specifying constant values to the simulation object, e.g. o.set_config('environment:constant:upward_sea_water_velocity', 0)

A more general and automated solution would be to if OpenDrift would automatically avoid reading variables that are not used. I have been pondering that this could be specified in required variables something like this:

required_variables = {
        .....
        'upward_sea_water_velocity': {
             'fallback': 0,
             'only_if': {'drift:vertical_advection': True},
          }
     }

I believe this could be fairly straightforward to implement (at least if avoiding nested conditions...), and that it should suffice with only_if and not_if, and value that could be either True/False as in above example, or <greater_than, smaller_than, equal>: <value>

kthyng commented 9 months ago

@knutfrode Yeah changing the definition for required_variables would be a good solution long term to be more general. For now I have some logic in my wrapper code to look for certain scenarios and dropping variables if possible. Thank you!