Giter Site home page Giter Site logo

Comments (4)

kuchaale avatar kuchaale commented on June 13, 2024 1

@lesommer : I understand your issue with the specification of np.nan. However, when I run the code below (without dtype specification):

`import numpy as np
import xarray as xr

def is_numpy(array):
test = bool( isinstance(array,np.ndarray)
+ isinstance(array,np.ma.masked_array) )
return test

def _horizontal_gradient(scalararray):
data = scalararray.data
coords = scalararray.coords
dims = scalararray.dims
chunks = scalararray.chunks
if is_numpy(data):
da_dj,da_di = np.gradient(data)
else:
x_derivative = lambda arr:np.gradient(arr,axis=-1) # req. numpy > 1.11
y_derivative = lambda arr:np.gradient(arr,axis=-2) # req. numpy > 1.11
gx = data.map_overlap(x_derivative,depth=(0,1),boundary={1: np.nan})#, dtype=data.dtype)
gy = data.map_overlap(y_derivative,depth=(1,0),boundary={0: np.nan})#, dtype=data.dtype)
da_di = xr.DataArray(gx,coords,dims)
da_dj = xr.DataArray(gy,coords,dims)
return da_dj,da_di

ds = xr.open_dataset('../data/heigh_2017011715Z_1000hPa.nc')
ds = ds.chunk(chunks = 40)
ds`

I get this error:
ValueError: dtypeinference failed inmap_blocks`.

Original error is below:

ValueError('Shape of array too small to calculate a numerical gradient, at least two elements are required.',)

Traceback:

File "c:\python27\lib\site-packages\dask\array\core.py", line 457, in apply_infer_dtype
o = func(*args, **kwargs)
File "", line 26, in
x_derivative = lambda arr:np.gradient(arr,axis=-1) # req. numpy > 1.11
File "c:\python27\lib\site-packages\numpy\lib\function_base.py", line 1642, in gradient
"Shape of array too small to calculate a numerical gradient, "
`
This issue is induced by the non-specification of dtype.

from oocgcm.

kuchaale avatar kuchaale commented on June 13, 2024

I think your current version would not work anyway if you don't specify a dtype explicitly, see my issue here.

from oocgcm.

lesommer avatar lesommer commented on June 13, 2024

@kuchaale : thanks for your comment and your suggestion. But I am not sure to get what you mean by "would not work" here. The present version of the code actually produce a result, but does not compute the gradient close to the boundary (thanks to the specification of np.nan).

from oocgcm.

redouanelg avatar redouanelg commented on June 13, 2024

Hi @kuchaale, your solution worked for me, please open a PR or allow me to do it

from oocgcm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.