OpenDrift / opendrift

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

Sum the contribution of different readers to the same variable #73

Closed stefraynaud closed 1 year ago

stefraynaud commented 6 years ago

How about summing the contribution of different readers to the same variable, at least for currents? For example, different components of the current (ekman, stokes, geostrophic, tide, ...) may be stored in different files that are not on the same grid. Summing the component at read time can prevent a regridding preprocessing of files.

knutfrode commented 6 years ago

Yes, this is a good idea, which has actually been planned for some time. It will require some careful planning to implement it in a sound and generic way, so I won't have time to prioritise this before the summer, but hopefully before the end of this year.

The basic idea is to overload the "+" (or perhaps "-" or "*") operator for the reader class, so that one can write: >>> reader_current = reader_geostrophic + reader_tide But then there will also be a need to somehow specify which variables shall be added or not.

ivicajan commented 6 years ago

Well, there is something already available within NCO package and is called ncbo (netCDF binary operator). You can do it simply with option -y if all variables are on the same grid

Cheers, Ivica

p.s. for including WRF (or any other atmo model having curvlienar grid output defined on lon2d and lat2d) still no milestone? :(

On Thu, May 17, 2018 at 3:13 AM, Knut-Frode Dagestad < notifications@github.com> wrote:

Yes, this is a good idea, which has actually been planned for some time. It will require some careful planning to implement it in a sound and generic way, so I won't have time to prioritise this before the summer, but hopefully before the end of this year.

The basic idea is to overload the "+" (or perhaps "-" or "*") operator for the reader class, so that one can write:

reader_current = reader_geostrophic + reader_tide But then there will also be a need to somehow specify which variables shall be added or not.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/OpenDrift/opendrift/issues/73#issuecomment-389633468, or mute the thread https://github.com/notifications/unsubscribe-auth/ARvJsHQDE0qoe7P2wI2e9RYpczVBmin3ks5tzHpfgaJpZM4UBcPJ .

simonweppe commented 6 years ago

Hi all - I was in the same situation as @stefraynaud. I needed to add pure tidal currents, with residual currents. The idea of doing reader_cur_total = reader_cur_tide + reader_cur_res is quite elegant !

I have made an attempt on my fork, which works (for current only), see here for an example : https://github.com/simon-msl/opendrift/blob/master/examples_msl/example_msldata_multireaders.py

The overloading of the __add__ function is made here : https://github.com/simon-msl/opendrift/blob/master/opendrift/readers/reader_netCDF_MetOcean.py (keeping my changes out of basereader.py for now for clarity)

The process is basically to save readers to add as an attribute readers_to_add to the initial reader (in __add__), then, if that readers_to_add attribute is present I use a switch to use get_variables_interpolated_multireaders() from within get_variables_interpolated(). This way readers can be on different grids, with different options etc.. and those changes are invisible to any "model" that is used.

The only thing that is bothering me at the moment, is that attributes readers_to_add is added once and for all to the initial reader i.e. if that initial reader need to be re-used in the same script it needs to be re-initialized. There must be a way to deal with that ..but reaching my Python limits here !

@knutfrode - feel free to review and comment. I could make a pull request for that change if needed.

simon

gauteh commented 1 year ago

Now possible to use operators on readers.