# Converting between curvilinear and lon/lat

**URL:** <https://discourse.pangeo.io/t/converting-between-curvilinear-and-lon-lat/1333>\
**Category:** Science\
**Created:** [March 24, 2021, 10:23am UTC](https://discourse.pangeo.io/t/converting-between-curvilinear-and-lon-lat/1333 "2021-03-24T10:23:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![WesleyTheGeolien](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/wesleythegeolien/32/554_2.png) [@WesleyTheGeolien](https://discourse.pangeo.io/u/WesleyTheGeolien)\
**Post date:** [March 24, 2021, 10:23am UTC](https://discourse.pangeo.io/t/converting-between-curvilinear-and-lon-lat/1333/1 "2021-03-24T10:23:24Z")

</div>

Hey everyone 🙂

I have a rectilinear xarray file (180 x 360) one of the attributes tells me the outflow point (to the ocean) of each watershed.

I have a curvilinear grid (coarser res x = 149 by y =182) with another variable I want to use.

I am looking for a way to “map” my output points from my rectilinear grid onto my curvilinear grid to find the values of the cells.

I have tried using interp with method=‘nearest’ (linear misses tons of values only 20 basins out of 2600) but this still misses roughly half of my exit points.

I wondered if there is an “easy” way to see which cell(s) a given long / lat falls in in a curvilinear grid? The annoying things is using geoviews I can superpose these datasets!!

Would Spatial pandas help out here?

---

<div class="post-metadata">

**Author:** ![WesleyTheGeolien](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/wesleythegeolien/32/554_2.png) [@WesleyTheGeolien](https://discourse.pangeo.io/u/WesleyTheGeolien)\
**Post date:** [March 24, 2021, 10:24am UTC](https://discourse.pangeo.io/t/converting-between-curvilinear-and-lon-lat/1333/2 "2021-03-24T10:24:22Z")

</div>

I have come across this thread: [Does interp() work on curvilinear grids (2D coordinates) ? · Issue #2281 · pydata/xarray · GitHub](https://github.com/pydata/xarray/issues/2281) but this talks about interpolating.

---

<div class="post-metadata">

**Author:** ![rsignell](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/rsignell/32/447_2.png) [@rsignell](https://discourse.pangeo.io/u/rsignell)\
**Post date:** [March 24, 2021, 11:51am UTC](https://discourse.pangeo.io/t/converting-between-curvilinear-and-lon-lat/1333/3 "2021-03-24T11:51:15Z")

</div>

xESMF has the ability to [interpolate to curvilinear grids](https://pangeo-xesmf.readthedocs.io/en/latest/notebooks/Curvilinear_grid.html). Would that work?

---

<div class="post-metadata">

**Author:** ![WesleyTheGeolien](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/wesleythegeolien/32/554_2.png) [@WesleyTheGeolien](https://discourse.pangeo.io/u/WesleyTheGeolien)\
**Post date:** [March 24, 2021, 2:06pm UTC](https://discourse.pangeo.io/t/converting-between-curvilinear-and-lon-lat/1333/4 "2021-03-24T14:06:37Z")

</div>

Thanks for the link @rsignell, I’ve got the example working quickly with `ignore_degenerate=True` else I was getting some errors 😕

For reference in the mean time I got this pseudo code working:

1. Extract the locations from curvilinear coordinates
2. Use xarray.sel with method=‘nearest’ to find out which cell each point falls into
3. Extract the Lons and Lats
4. Use `numpy.unique` with `return_index=True, return_inverse=True, return_counts=True`
5. Calculate the averages: `average = numpy.bincount(idx\_reverse, weights=data) / counts
6. Create an empty array and use search sorted to find locations and fill in with averaged data values
7. Assign to xarray

Seems a hell of a work around 😕 This also means that my values are continuous now as they are with the xESMF regridding but it will work in my case.

Thanks again

---

<div class="post-metadata">

**Author:** ![rabernat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pangeo.io/rabernat/32/22_2.png) [@rabernat](https://discourse.pangeo.io/u/rabernat)\
**Post date:** [March 24, 2021, 2:11pm UTC](https://discourse.pangeo.io/t/converting-between-curvilinear-and-lon-lat/1333/5 "2021-03-24T14:11:32Z")

</div>

I agree xesmf is the ideal tool for this, particularly if you care about things like conservative interpolation schemes.

If you’re interested in _indexing_ curvilinear data, you might also look into @benbovy’s xoak package:

> **[xarray-contrib/xoak](https://github.com/xarray-contrib/xoak)**
>
> xoak is an xarray extension that provides tree-based indexes used for selecting irregular, n-dimensional data.
