miniufo / xinvert

Invert geophysical fluid dynamic problems (elliptic partial differential equations) using SOR iteration method.
https://xinvert.readthedocs.io/
MIT License
40 stars 15 forks source link

Can't reproduce QG example #3

Closed silver-fox-9244 closed 5 months ago

silver-fox-9244 commented 2 years ago

Hello, I'm very new to python and trying to implement the QG omega test case using the sample data: atmos3D.nc provided, but I keep running into the following error upon executing the testOmegaEq.py script:

Traceback (most recent call last): File "", line 1, in File "", line 18, in File "C:\Users\user-pc\miniconda3\Lib\site-packages\xinvert\tests\GeoApps\GridUtils.py", line 70, in add_latlon_metrics dlonG = grid.diff(ds[lon ], 'X', boundary_discontinuity=360) File "C:\Users\user-pc\miniconda3\lib\site-packages\xgcm\grid.py", line 2093, in diff return self._1d_grid_ufunc_dispatch("diff", da, axis, **kwargs) File "C:\Users\user-pc\miniconda3\lib\site-packages\xgcm\grid.py", line 1836, in _1d_grid_ufunc_dispatch array = grid_ufunc( File "C:\Users\user-pc\miniconda3\lib\site-packages\xgcm\grid_ufunc.py", line 460, in call return apply_as_grid_ufunc( File "C:\Users\user-pc\miniconda3\lib\site-packages\xgcm\grid_ufunc.py", line 770, in apply_as_grid_ufunc results = _apply( File "C:\Users\user-pc\miniconda3\lib\site-packages\xgcm\grid_ufunc.py", line 837, in _apply results = xr.apply_ufunc( TypeError: apply_ufunc() got an unexpected keyword argument 'boundary_discontinuity'

Can you please assist in figuring out what is causing this?

miniufo commented 2 years ago

Hi, thank you for your interests. This is due to the recent update of xgcm. A quick solution would be to use older version of xgcm (< 0.7). I have to see how to recover the original behavior using the newest 'xgcm'.

silver-fox-9244 commented 2 years ago

Thank you for your swift response. Upon downgrading to xgcm v 0.6.1 I unfortunately get the following warnings and error:

ds, grid = add_latlon_metrics(dset, dims={'lev':'LEV', 'lat':'lat', 'lon':'lon'}) C:\Users\user-pc\miniconda3\Lib\site-packages\xinvert\tests\GeoApps\GridUtils.py:177: FutureWarning: xarray.ufuncs is deprecated. Instead, use numpy ufuncs directly. dx = xr.ufuncs.cos(xr.ufuncs.deg2rad(lat)) C:\Users\user-pc\miniconda3\lib\site-packages\xarray\core\dataarray.py:3078: FutureWarning: xarray.ufuncs is deprecated. Instead, use numpy ufuncs directly. da = self.array_wrap(f(self.variable.data, *args, **kwargs)) C:\Users\user-pc\miniconda3\Lib\site-packages\xinvert\tests\GeoApps\GridUtils.py:177: FutureWarning: xarray.ufuncs is deprecated. Instead, use numpy ufuncs directly. dx = xr.ufuncs.cos(xr.ufuncs.deg2rad(lat)) C:\Users\user-pc\miniconda3\lib\site-packages\xarray\core\dataarray.py:3078: FutureWarning: xarray.ufuncs is deprecated. Instead, use numpy ufuncs directly. da = self.array_wrap(f(self.variable.data, *args, kwargs)) C:\Users\user-pc\miniconda3\Lib\site-packages\xinvert\tests\GeoApps\GridUtils.py:177: FutureWarning: xarray.ufuncs is deprecated. Instead, use numpy ufuncs directly. dx = xr.ufuncs.cos(xr.ufuncs.deg2rad(lat)) C:\Users\user-pc\miniconda3\lib\site-packages\xarray\core\dataarray.py:3078: FutureWarning: xarray.ufuncs is deprecated. Instead, use numpy ufuncs directly. da = self.array_wrap(f(self.variable.data, *args, **kwargs)) C:\Users\user-pc\miniconda3\Lib\site-packages\xinvert\tests\GeoApps\GridUtils.py:177: FutureWarning: xarray.ufuncs is deprecated. Instead, use numpy ufuncs directly. dx = xr.ufuncs.cos(xr.ufuncs.deg2rad(lat)) C:\Users\user-pc\miniconda3\lib\site-packages\xarray\core\dataarray.py:3078: FutureWarning: xarray.ufuncs is deprecated. Instead, use numpy ufuncs directly. da = self.array_wrap(f(self.variable.data, *args, kwargs)) Traceback (most recent call last): File "", line 1, in File "C:\Users\user-pc\miniconda3\Lib\site-packages\xinvert\tests\GeoApps\GridUtils.py", line 87, in add_latlon_metrics grid._assign_metrics(metrics) AttributeError: 'Grid' object has no attribute '_assign_metrics'

miniufo commented 2 years ago

Sorry for this inconvience. The dependence of xinvert on xgcm is to take advantage of its ability for finite difference, but since xgcm is still in heavy development, all the public apis can be changed at any time.

Could you please try 0.5.x of 'xgcm'? I write the codes using that version and I guess it should be OK.

silver-fox-9244 commented 2 years ago

Ah...makes sense. Just downgraded to xgcm 0.5.0 and upon execution of the code this then comes up:

File "", line 1, in File "", line 28, in TypeError: init() got an unexpected keyword argument 'arakawa'

Is it because in the Dynamics class, the method init(self,dset,grid=None) doesn't take the grid type argument?

class Dynamics(Application): """ This class is designed for calculating the dynamical methods. """ def init(self, dset, grid=None):

miniufo commented 2 years ago

Oh, I notice that one may need GeoApps to do the tests of xinvert. You can clone it to your local path and import Dynamics from the updated GeoApps.

GeoApps is used for finite differences but it is not ready for release. I realize that xinvert has too many dependencies on those packages, and maybe it is time to make it standalone...

silver-fox-9244 commented 2 years ago

I've just updated/cloned the GeoApps package into the tests folder, and again it throws the initial error I got before downgrading to an older xgcm version.

C:\Users\user-pc\miniconda3\lib\site-packages\scipy__init__.py:138: UserWarning: A NumPy version >=1.16.5 and <1.23.0 is required for this version of SciPy (detected version 1.23.1) warnings.warn(f"A NumPy version >={np_minversion} and <{np_maxversion} is required for this version of " Traceback (most recent call last): File "", line 1, in File "", line 20, in File "C:\Users\user-pc\miniconda3\Lib\site-packages\xinvert\tests\GeoApps\GridUtils.py", line 175, in add_latlon_metrics grid.set_metrics(key, value) AttributeError: 'Grid' object has no attribute 'set_metrics'

Could this be related to the scipy and numpy warnings issued?

miniufo commented 2 years ago

I'll give a try to run this example using the newest versions of packages, fix all the bugs, and let you know the results ASAP.

silver-fox-9244 commented 2 years ago

Awesome!, thanks for your help and patience.

miniufo commented 2 years ago

I've updated the test codes of QG omega, with 0.7.0v of xgcm. Also, GeoApps is updated accordingly. You can try with them.

For inverting QG omega, one needs a 3D forcing function and a 3D stratification. But the calculations of the forcing and stratification are not the part of xinvert. There are two ways to do the calculation of forcings. One is to use GeoApps which is based on xgcm; the other is to use FiniteDiff in xinvert so that the package does not depend on other package (please see the cells in testOmegaEq.py). You can also write your own codes to do the calculation of forcings.

Note that there are NaN in the calculation, which is used to maskout the topography (kwarg undef=NaN). In this case, one need to change inf (the calculation of forcings usually brings up inf) to NaN explicitly so that inf do not propagate in the calculation.

Once the forcings are ready, inverting is much easier. Please tell me if you find other problems.

miniufo commented 5 months ago

Close this now. Feel free to re-open it in any case.