NCAS-CMS / cf-python

A CF-compliant Earth Science data analysis library
http://ncas-cms.github.io/cf-python
MIT License
127 stars 19 forks source link

3-d conservative regridding #814

Open brogalla opened 2 months ago

brogalla commented 2 months ago

Follow up from issue #715 : maybe this is already planned, but it would be very nice if there was an option for 3-d conservative regridding in addition to the recently added 3-d bilinear spherical regridding (ESMPy seems to have this as an option).

sadielbartholomew commented 2 months ago

Thanks Birgit, firstly sorry it seems David and myself missed your comment on the issue you link - we get a lot of traffic on GitHub so sometimes comments can get lost in the noise.

As far as I can tell from the code and according to our test suite, conservative is supported already as a regridding method with the 3D, vertical coordinate inclusive, case, for both of the spherical (regrids) or Cartesian (regridc) cases. So, unless I am missing something, you should be able to just specify it as the method, like so (and equivalently with regrids):

regridded_f = src_f.regridc(dst_f, axes='Z', method='conservative', z='Z', <plus any other keyword arguments you want>)

where you have to identify the specific z coordinate with e.g. here 'Z' as the identifier, and often you will want to include ln_z=True to the other argument inputs for 3D regridding.

For conservative regridding to work you will need bounds defined for all of the coordinates involved including the 'z'/vertical ones, though, and note 3D regridding won't work for certain cases such as DSG fields.

I am wondering about the background here - did you run into a specific error or issue when you tried to do conservative 3D regridding with cf? If so please let us know and we can guide. (There may be some other limitations I am not aware of.)

brogalla commented 2 months ago

Hi Sadie, no problem at all -- and sorry for my delayed response.

I see! I tested the 'linear' 3D regridding and it worked fine, but I ran into errors when I tried conservative 3D regridding (2D conservative regridding worked fine). Then, while reading the documentation, I misinterpreted the comment that the vertical regridding can be done with linear weights to mean that the 3D regridding was only set up for the "linear" method, so I figured that was the cause of my issues. But, I think I just setup the conservative regridding incorrectly (probably something about the vertical coordinate bounds). I will try a simplified test case and let you know if I still run into issues. Again thanks for the help!

sadielbartholomew commented 2 months ago

Thanks @brogalla. I hope it all works and we will assume so if we don' hear back, but yes, please do let us know in the case it doesn't work like you wish it to or if the documentation isn't clear enough about how to specify what you want in this respect (a separate but related issue which is just as important in my view), for example if we could add more examples to showcase how it can be done.