regrid with iris.analysis.AreaWeighted()

141 views
Skip to first unread message

Roger Bodman

unread,
Oct 24, 2019, 7:48:40 PM10/24/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Hi,

Having trouble getting iris.analysis.AreaWeighted() to work ok.  Linear works fine.  I keep hitting this error:

/g/data3/hh5/public/apps/miniconda3/envs/analysis3-19.04/lib/python3.6/site-packages/iris/experimental/regrid.py in regrid_area_weighted_rectilinear_src_and_grid(src_cube, grid_cube, mdtol)
    676     # Condition 1: All x and y coordinates must have contiguous bounds to
    677     # define areas.
--> 678     if not src_x.is_contiguous() or not src_y.is_contiguous() or \
    679             not grid_x.is_contiguous() or not grid_y.is_contiguous():
    680         raise ValueError("The horizontal grid coordinates of both the source "

TypeError: 'bool' object is not callable


Is this from my data or a bug in Iris?

My code currently has:

Y_annual_mean.coord('latitude').bounds = None
Y_annual_mean.coord('longitude').bounds = None
Y_annual_mean.coord('latitude').guess_bounds()
Y_annual_mean.coord('longitude').guess_bounds()

X_annual_mean.coord('latitude').bounds = None
X_annual_mean.coord('longitude').bounds = None
X_annual_mean.coord('latitude').guess_bounds()
X_annual_mean.coord('longitude').guess_bounds()

Y_annual_mean.coord('latitude').is_contiguous = True
Y_annual_mean.coord('longitude').is_contiguous = True

X_annual_mean.coord('latitude').is_contiguous = True
X_annual_mean.coord('longitude').is_contiguous = True 

X_regrid = X_annual_mean.regrid(Y_annual_mean, iris.analysis.AreaWeighted())


at which point error occurs.

Can anyone advise?  Thanks,

Roger

Klaus Zimmermann

unread,
Oct 25, 2019, 4:00:07 AM10/25/19
to scitoo...@googlegroups.com
Hi Roger,

the problem is with your code. You cannot just declare your bounds to be
contiguous. Instead, `is_contiguous` is a function of the coords that
checks whether they have bounds and whether these bounds are contiguous.
Take a look at the documentation of `is_contiguous` at [1,2] for
dimensional coords and auxiliary coords respectively.

With your lines of
```
coord.is_contiguous = True
```
you effectively remove this method, putting a simple `bool` variable in
its place; then when iris tries to call the method it stumbles over the
variable.

Good news is, simply removing the four lines that end in `.is_contiguous
= True` should make it work.


So far so good. Allow me to make some more comments on your code though.

Your code suggests that the coordinates already have bounds to begin
with; why assign `None` to them first otherwise? In that case it seems
questionable to remove them, since probably they are as they are for a
reason and you risk replacing them with wrong bounds that don't fit the
data.

If you insist on doing this (perhaps you know for a fact that the
existing bounds are erroneous?), you can use local variables to simply
the code. I would write in this situation:

```
lat = Y_annual_mean.coord('latitude')
lat.bounds = None
lat.guess_bounds()
```
and similarly for the other three coords. Indeed, since all coords
receive the same treatment, a short function is in order, something like

```
def replace_bounds(coord):
coord.bounds = None
coord.guess_bounds()

replace_bounds(Y_annual_mean.coord('latitude')
replace_bounds(Y_annual_mean.coord('longitude')
replace_bounds(X_annual_mean.coord('latitude')
replace_bounds(X_annual_mean.coord('longitude')
```

there, much clearer, don't you think?


Hope that helps.

Cheers
Klaus


[1]
https://scitools.org.uk/iris/docs/latest/iris/iris/coords.html#iris.coords.DimCoord.is_contiguous
[2]
https://scitools.org.uk/iris/docs/latest/iris/iris/coords.html#iris.coords.AuxCoord.is_contiguous
> --
> You received this message because you are subscribed to the Google
> Groups "SciTools (iris, cartopy, cf_units, etc.) -
> https://github.com/scitools" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to scitools-iri...@googlegroups.com
> <mailto:scitools-iri...@googlegroups.com>.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/scitools-iris/64e5e541-d38b-42ad-96c2-706f13b0f719%40googlegroups.com
> <https://groups.google.com/d/msgid/scitools-iris/64e5e541-d38b-42ad-96c2-706f13b0f719%40googlegroups.com?utm_medium=email&utm_source=footer>.

Roger Bodman

unread,
Oct 25, 2019, 5:52:48 PM10/25/19
to SciTools (iris, cartopy, cf_units, etc.) - https://github.com/scitools
Hi Klaus,

Thanks for your help, makes issue clearer for me.  Reason for removing bounds was to deal (crudely) with problem that some of my X and Y data cubes have bounds, some don't.  A better approach would be to test for that first. Still learning how to do things with Iris ...

Cheers,
Roger

Klaus Zimmermann

unread,
Oct 28, 2019, 6:58:46 AM10/28/19
to scitoo...@googlegroups.com
Hi Roger,

ah I see. In that case I suggest something like

```
def ensure_bounds(coord):
if not coord.has_bounds():
coord.guess_bounds()

ensure_bounds(Y_annual_mean.coord('latitude'))
ensure_bounds(Y_annual_mean.coord('longitude'))
ensure_bounds(X_annual_mean.coord('latitude'))
ensure_bounds(X_annual_mean.coord('longitude'))
```

Of course, you just might find that some of the original bounds are not
contiguous. But if that is the case, well, then that is the case and you
cannot apply this form of regridding.

Cheers
Klaus
> > an email to scitoo...@googlegroups.com <javascript:>
> > <mailto:scitools-iri...@googlegroups.com <javascript:>>.
> <https://groups.google.com/d/msgid/scitools-iris/64e5e541-d38b-42ad-96c2-706f13b0f719%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/msgid/scitools-iris/64e5e541-d38b-42ad-96c2-706f13b0f719%40googlegroups.com?utm_medium=email&utm_source=footer>>.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "SciTools (iris, cartopy, cf_units, etc.) -
> https://github.com/scitools" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to scitools-iri...@googlegroups.com
> <mailto:scitools-iri...@googlegroups.com>.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/scitools-iris/27c2acb8-22cf-4aa1-9f58-81c87d0b9e17%40googlegroups.com
> <https://groups.google.com/d/msgid/scitools-iris/27c2acb8-22cf-4aa1-9f58-81c87d0b9e17%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages