Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict size 1 limitation to source field in regridding #249

Merged
merged 10 commits into from
Sep 30, 2021

Conversation

ThibHlln
Copy link

@ThibHlln ThibHlln commented Aug 24, 2021

resolves #250

Hi David, Hi Sadie,

With the simple change contained in this PR, the code below would work, which is not currently the case (i.e. a regridding towards a single grid cell domain).

import cf
import numpy

# source field (2x3 grid)
fld1 = cf.Field()
fld1.set_construct(
    cf.DimensionCoordinate(
        properties={'standard_name': 'longitude',
                    'units': 'degrees_east',
                    'axis': 'X'},
        data=cf.Data([5, 15, 25]),
        bounds=cf.Bounds(data=cf.Data([[0, 10], [10, 20], [20, 30]]))
    ),
    axes=fld1.set_construct(cf.DomainAxis(size=3))
)
fld1.set_construct(
    cf.DimensionCoordinate(
        properties={'standard_name': 'latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'},
        data=cf.Data([55, 65]),
        bounds=cf.Bounds(data=cf.Data([[50, 60], [60, 70]]))
    ),
    axes=fld1.set_construct(cf.DomainAxis(size=2))
)

# destination field (1x1 grid)
fld2 = cf.Field()
fld2.set_construct(
    cf.DimensionCoordinate(
        properties={'standard_name': 'longitude',
                    'units': 'degrees_east',
                    'axis': 'X'},
        data=cf.Data([15]),
        bounds=cf.Bounds(data=cf.Data([[0, 30]]))
    ),
    axes=fld2.set_construct(cf.DomainAxis(size=1))
)
fld2.set_construct(
    cf.DimensionCoordinate(
        properties={'standard_name': 'latitude',
                    'units': 'degrees_north',
                    'axis': 'Y'},
        data=cf.Data([60]),
        bounds=cf.Bounds(data=cf.Data([[50, 70]]))
    ),
    axes=fld2.set_construct(cf.DomainAxis(size=1))
)

# set some data
fld1.set_data(numpy.arange(6).reshape(2, 3), axes=('Y', 'X'))
fld2.set_data(numpy.ones((1, 1)), axes=('Y', 'X'))

# test regridding on all existing methods
for m in ['linear', 'conservative', 'conservative_2nd', 
          'patch', 'nearest_stod', 'nearest_dtos']:
    
    fld3 = fld1.regrids(fld2, method=m)
    
    print(fld3.data)

@ThibHlln ThibHlln changed the title restrict size 1 limitation to source grid restrict size 1 limitation to source grid in regridding Aug 24, 2021
@ThibHlln ThibHlln changed the title restrict size 1 limitation to source grid in regridding restrict size 1 limitation to source field in regridding Aug 24, 2021
@ThibHlln ThibHlln changed the title restrict size 1 limitation to source field in regridding Restrict size 1 limitation to source field in regridding Aug 24, 2021
@davidhassell
Copy link
Collaborator

Hi Thibault,

Would you mind copying the above into an issue and referencing that issue from this PR? (https://ncas-cms.github.io/cf-python/contributing.html#preparing-pull-requests) - I'll reply over there ...

Thanks, David

cf/regrid/utils.py Outdated Show resolved Hide resolved
@ThibHlln
Copy link
Author

Hi David,

As discussed in #250, it seems that the source grid can be with size 1 dimensions for some of the methods.

My latest commit reflects that and also un-indent the check in regrid_get_latlon to cover both coords_2D = False and coords_2D = True. However, for it to work, I also needed to remove the squeeze on src_data in regrids, I am not sure whether this is a problem?

The testing script below should work now (same fields as above):

for m in ['conservative_1st', 'conservative_2nd', 'nearest_stod', 'nearest_dtos']:
    fld3 = fld2.regrids(fld1, method=m)
    print(fld3.array)

@davidhassell
Copy link
Collaborator

Hi @ThibHlln, I wasn't sure that anything would happen here, but I've managed to push to this PR of yours. This commit (7dfb455) tidies up the removal of the "squeeze", which needs to go back in sometimes to get rid of size 1 dimensions that are not being regridded. Hope that's OK! (this commit can always be nuked, of course).

@davidhassell
Copy link
Collaborator

Hi @ThibHlln - I think we can merge this, now. I'll await your OK, though ...

@ThibHlln
Copy link
Author

ThibHlln commented Sep 30, 2021

Hi @davidhassell,

I was not sure what the squeezing was for, but I was guessing it would require more than just removing it.
Thank you for your help with this.

Yes, I think it can be merged now, I ran the tests above successfully after your commits.
So, it looks all good to me.

@davidhassell
Copy link
Collaborator

Hi @ThibHlln - I think the squeezing is all about making sure we pass the correctly shaped numpy array down to the ESMF layer. Thanks for your help with this. We expect a release of 3.11.0 in a couple of weeks (ish). Merging now ...

@davidhassell davidhassell merged commit f739b68 into NCAS-CMS:master Sep 30, 2021
@davidhassell davidhassell added this to the 3.11.0 milestone Sep 30, 2021
ThibHlln pushed a commit to ThibHlln/unifhy that referenced this pull request Oct 20, 2021
This version includes:
- fix for memory leak due to ESMF objects NCAS-CMS/cf-python#225
- lift for size 1 restriction on grid NCAS-CMS/cf-python#249
- fix bug preventing cyclic domain (e.g. global runs) NCAS-CMS/cf-python#237
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Restrict size 1 dimension to source field in regridding
3 participants