xCDAT / xcdat

An extension of xarray for climate data analysis on structured grids.
https://xcdat.readthedocs.io/en/latest/
Apache License 2.0
101 stars 11 forks source link

[Bug]: Regrid2Regridder yields NANs #599

Closed ShihengDuan closed 3 months ago

ShihengDuan commented 5 months ago

Originally discussed here: https://github.com/xCDAT/xcdat/discussions/598

What happened?

Initially I have a problem with Regrid2Regridder since it shows NANs: Regrid2Regridder.

When I try to create an example, I found that if the target_grid only has one variable, it works well. I'm not sure if this is related to this.

Code:

import xarray as xa
import xcdat as xc
from xcdat.regridder.regrid2 import Regrid2Regridder
from xcdat.regridder.xesmf import XESMFRegridder
from matplotlib import pyplot as plt

print(xc.__version__)
ua = xc.open_dataset('UA_swe.nc')
vic_ele = xc.open_dataset('vic_elev.nc')
vic_grid = xc.open_dataset('/p/lustre1/shiduan/HydroSimulation/VIC/VIC/samples/domain.vic.WUS.0625deg.nc')
print(vic_grid)
vic_ele.lat.attrs['standard_name'] = 'Y'
vic_ele.lon.attrs['standard_name'] = 'X'
vic_grid.lat.attrs['standard_name'] = 'Y'
vic_grid.lon.attrs['standard_name'] = 'X'
ua.lat.attrs['standard_name'] = 'Y'
ua.lon.attrs['standard_name'] = 'X'
regrid2_gridder_single = Regrid2Regridder(ua, vic_ele)
regrid2_gridder_all = Regrid2Regridder(ua, vic_grid)

regrid2_swe_single = regrid2_gridder_single.horizontal("SWE", ua)
regrid2_swe_single.SWE.isel(time=10).plot()
plt.show()

regrid2_swe_all = regrid2_gridder_all.horizontal("SWE", ua)
regrid2_swe_all.SWE.isel(time=10).plot()
plt.show()

vic_ele is the elevation data extracted from vic_grid.

2024-02-02 17:38:17,872 [WARNING]: dataset.py(open_dataset:128) >> "No time coordinates were found in this dataset to decode. If time coordinates were expected to exist, make sure they are detectable by setting the CF 'axis' or 'standard_name' attribute (e.g., ds['time'].attrs['axis'] = 'T' or ds['time'].attrs['standard_name'] = 'time'). Afterwards, try decoding again with `xcdat.decode_time`."
2024-02-02 17:38:17,872 [WARNING]: dataset.py(open_dataset:128) >> "No time coordinates were found in this dataset to decode. If time coordinates were expected to exist, make sure they are detectable by setting the CF 'axis' or 'standard_name' attribute (e.g., ds['time'].attrs['axis'] = 'T' or ds['time'].attrs['standard_name'] = 'time'). Afterwards, try decoding again with `xcdat.decode_time`."
2024-02-02 17:38:17,898 [WARNING]: dataset.py(open_dataset:128) >> "No time coordinates were found in this dataset to decode. If time coordinates were expected to exist, make sure they are detectable by setting the CF 'axis' or 'standard_name' attribute (e.g., ds['time'].attrs['axis'] = 'T' or ds['time'].attrs['standard_name'] = 'time'). Afterwards, try decoding again with `xcdat.decode_time`."
2024-02-02 17:38:17,898 [WARNING]: dataset.py(open_dataset:128) >> "No time coordinates were found in this dataset to decode. If time coordinates were expected to exist, make sure they are detectable by setting the CF 'axis' or 'standard_name' attribute (e.g., ds['time'].attrs['axis'] = 'T' or ds['time'].attrs['standard_name'] = 'time'). Afterwards, try decoding again with `xcdat.decode_time`."
0.6.1
<xarray.Dataset>
Dimensions:   (lon: 312, lat: 264, bnds: 2)
Coordinates:
  * lon       (lon) float64 -123.0 -122.9 -122.8 -122.8 ... -103.7 -103.6 -103.5
  * lat       (lat) float64 32.53 32.59 32.66 32.72 ... 48.78 48.84 48.91 48.97
Dimensions without coordinates: bnds
Data variables:
    elev      (lat, lon) float64 ...
    mask      (lat, lon) int32 ...
    frac      (lat, lon) float64 ...
    area      (lat, lon) float64 ...
    lon_bnds  (lon, bnds) float64 -123.0 -122.9 -122.9 ... -103.6 -103.6 -103.5
    lat_bnds  (lat, bnds) float64 32.5 32.56 32.56 32.62 ... 48.94 48.94 49.0
Attributes:
    title:         VIC domain data
    Conventions:   CF-1.6
    history:       created by shiduan, 2023-11-14 11:56:44.828806
    user_comment:  VIC domain data
    source:        generated from VIC North American 1/16 deg. model paramete...

What I get:

image

What did you expect to happen? Are there are possible answers you came across?

No response

Minimal Complete Verifiable Example (MVCE)

No response

Relevant log output

No response

Anything else we need to know?

Example files: UA_swe.nc domain.vic.WUS.0625deg.nc vic_ele.nc

Environment

INSTALLED VERSIONS

commit: None python: 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:45:29) [GCC 10.4.0] python-bits: 64 OS: Linux OS-release: 4.18.0-513.9.1.1toss.t4.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.12.1 libnetcdf: 4.8.1

xarray: 2023.1.0 pandas: 1.5.3 numpy: 1.26.3 scipy: 1.12.0 netCDF4: 1.5.8 pydap: None h5netcdf: None h5py: None Nio: None zarr: None cftime: 1.6.3 nc_time_axis: 1.4.1 PseudoNetCDF: None rasterio: None cfgrib: None iris: None bottleneck: None dask: 2022.6.1 distributed: 2022.6.1 matplotlib: 3.3.2 cartopy: 0.21.0 seaborn: None numbagg: None fsspec: 2023.12.2 cupy: None pint: None sparse: 0.15.1 flox: None numpy_groupies: None setuptools: 69.0.3 pip: 23.3.2 conda: None pytest: None mypy: None IPython: 8.12.0 sphinx: None

pochedls commented 5 months ago

I can't reproduce this (datasets aren't readable/publicly accessible with full path names).

This may not be the issue, but have you tried calling the regridder as is shown in the docs?

ShihengDuan commented 5 months ago

xcdat-test.zip It seems I cannot upload nc files directly but this should work.

lee1043 commented 3 months ago

Screenshot 2024-03-12 at 10 46 05 AM

The above is what I am getting when running @ShihengDuan's code above with the latest main branch after #533 being merged. @ShihengDuan Does this look reasonable to you?

ShihengDuan commented 3 months ago

Yes I think this makes sense to me. Thanks!

lee1043 commented 3 months ago

@ShihengDuan thanks for confirming! I am closing this issue but please feel free to reopen when you have any further issue.