-
Notifications
You must be signed in to change notification settings - Fork 48
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
Value of cells in the new grid that are outside the old grid's domain #15
Comments
Currently only zero is used, which is the default behavior in ESMPy. Adding A quick and dirty way to get
Notes on ESMPy: The UnmappedAction option can only take |
Thank you for the quick reply and the temporary fix! It would be great to have nan as an option for the values in a future version. |
Also running into this issue but the above fix is not working in my case. I have data between 0-1 and use an offset value of +10.0. Below shows an example image of the remapped grid where the target cells that contained both a missing and valid source cell have values between -10 to 0 (after subtracting back the offset value). I guess this verifies that conservative method is working correctly! But it doesn't allow me to mask correctly. So +1 on adding NaN. @JiaweiZhuang, let me know if I can help, if you can point me in the right direction I can give it a shot. |
Ah, you are right, I overlooked this case. Conservative regridding will average the cell on the input grid's boundary with non-existing "cells" outside the boundary (which are assumed to have zero values). This means the added constant (10 in your case) will be diluted to, say ~5, after regridding, and will then become negative after you abstract 10. So that scaling-up-and-down approach won't work correctly. There is actually a conservative regridding option that won't dilute boundary cells (#17), but it is not in xESMF yet. A more robust way to get If there are unmapped cells, several rows in the above matrix will be empty. (no empty row in this case since the output grid is fully mapped) Just add one |
Thanks for the suggestion! Below is function for adding nans to empty rows. It currently throws a warning that editing a csr matrix is slow... but the lil_matrix doesn't have a indptr function (I am new to sparse matrices so there probably is a way).
|
@NicWayand Thanks for the code! Does this work correctly for your use case? For performance issues there should be many ways around. |
Yes it did! |
add_matrix_NaNs doesn't work as a workaround since 0.2.0
Does anyone have a new workaround? |
@Plantain Use |
Is there any update about this topic? I used the function define by @NicWayand by replacing import scipy
def add_matrix_NaNs(regridder):
X = regridder.weights
M = scipy.sparse.csr_matrix(X)
num_nonzeros = np.diff(M.indptr)
M[num_nonzeros == 0, 0] = np.NaN
regridder.weights = scipy.sparse.coo_matrix(M)
return regridder It works, but it would be more convenient to have an option directly when we create the regridder. Did I miss something or there is no other way to do this so far? |
No updates yet from my side. I agree that it's a useful feature, and welcome discussion on the API design. I am thinking about Also welcome PR, especially a unit test with small, synthetic data. This is better done after #75, which will allow you to save the modified weights. Currently the weights are saved to disk before you have the chance to apply |
@JiaweiZhuang I tried the method as illustrated by @mickaellalande :
But, I still got the diluted grids as same as #17 (comment). |
Hi any updates on this topic? Can we set the nan values now? |
Trying this solution (actually using Nic's ESIO package, I get the following error: Any suggestions home/axel/anaconda3/envs/my_env39/lib/python3.9/site-packages/scipy/sparse/_index.py:125: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient. AttributeError Traceback (most recent call last) ~/ax_python_lib/ESIO/esio/import_data.py in add_matrix_NaNs(regridder) AttributeError: can't set attribute |
Hi,
xESMF seems like a great tool, I've just started testing it!
I have one issue though. I am regridding a limited domain with UTM-projection to a larger domain with a regular lat lon grid. In my case it seems that the cells in the new grid that are outside the old grid's domain are set to zero and not nan. Are there any options concerning what the default values are outside the input-grid's domain?
Regards, Helene
I'm using Python 3.6.3, xarray 0.10.0 xesmf 0.1.1, scipy 0.19.1
The script I am using:
import matplotlib.pyplot as plt
import numpy as np
import xarray as xr
from pyproj import Proj,transform
import xesmf as xe
SeN=xr.open_dataset('http://thredds.met.no/thredds/dodsC/metusers/senorge2/seNorge2/archive/PREC1d/seNorge2_PREC1d_grid_1957.nc')
projsrs=SeN['UTM_Zone_33'].proj4
LowerLeftEast= -75000
LowerLeftNorth= 6450000
UpperRightEast= 1120000
UpperRightNorth= 8000000
dx=1000.
ZoneNo = "33"
myP=Proj(SeN['UTM_Zone_33'].proj4)
Xcorners=np.arange(SeN['X'].data[0]-dx/2., SeN['X'].data[-1]+3dx/2., dx)
Ycorners=np.flipud(np.arange(SeN['Y'].data[-1]-dx/2., SeN['Y'].data[0]+3dx/2., dx))
Lon2, Lat2 = myP(*np.meshgrid(SeN['X'].data,SeN['Y'].data),inverse=True)
Lon2b, Lat2b = myP(*np.meshgrid(Xcorners,Ycorners),inverse=True) #
lons=np.asarray(Lon2)
lats=np.asarray(Lat2)
SeN.coords['lat'] = (('Y','X'),Lat2)
SeN.coords['lon'] = (('Y','X'),Lon2)
SeN.set_coords(['lat','lon'])
SeN.coords['Xb'] = (Xcorners)
SeN.coords['Yb'] = (Ycorners)
SeN.set_coords(['Xb','Yb'])
SeN.coords['lat_b'] = (('Yb','Xb'),Lat2b)
SeN.coords['lon_b'] = (('Yb','Xb'),Lon2b)
SeN.set_coords(['lat_b','lon_b'])
res=0.025
reslon=0.05
ds_out = xe.util.grid_2d(lons.min()-0.06,lons.max()+0.2,0.05,lats.min()-0.08,lats.max()+0.03, 0.025)
dr=SeN['precipitation_amount'][0:110]
regridder_cons = xe.Regridder(SeN, ds_out, 'conservative',reuse_weights=True)
dr_out = regridder_cons(dr)
plt.imshow(np.isnan(dr_out[0].data),origin='lower')
The text was updated successfully, but these errors were encountered: