这似乎是一个非常基本的操作,但我无法弄清楚如何使用 xarray 文档进行操作。
我有一个 xarray 数据集:
dss
<xarray.DataArray (y: 1000, x: 1334)>
dask.array<shape=(1000, 1334), dtype=uint8, chunksize=(222, 58)>
Coordinates:
band int32 1
* y (y) float64 2.218e+06 2.218e+06 2.218e+06 2.218e+06 2.218e+06 ...
* x (x) float64 1.891e+06 1.891e+06 1.891e+06 1.891e+06 1.891e+06 ...
Attributes:
transform: (30.0, 0.0, -2493045.0, 0.0, -30.0, 3310005.0, 0.0, 0.0, 1.0)
crs: +ellps=GRS80 +lat_0=23 +lat_1=29.5 +lat_2=45.5 +lon_0=-96 +n...
res: (30.0, 30.0)
is_tiled: 1
nodatavals: (nan,)
Run Code Online (Sandbox Code Playgroud)
和一个具有正确维度的 numpy 数组:
print(np.shape(nmap))
(1000, 1334)
nmap
array([[ 0.15, 0.1 , 0.15, ..., 0.05, …Run Code Online (Sandbox Code Playgroud) 在函数 apply_ufunc的xarray 文档中,它说:
dask: ‘forbidden’, ‘allowed’ or ‘parallelized’, optional
How to handle applying to objects containing lazy data in the form of dask arrays:
‘forbidden’ (default): raise an error if a dask array is encountered.
‘allowed’: pass dask arrays directly on to func.
‘parallelized’: automatically parallelize func if any of the inputs are a dask array.
If used, the output_dtypes argument must also be provided.
Multiple output arguments are not yet supported.
Run Code Online (Sandbox Code Playgroud)
在Parallel Computing的文档页面中,有一个注释:
对于大多数已经由 dask …
我有 5 个带有系泊电流计数据的 netCDF 文件。每个文件看起来像这样:
<xarray.Dataset>
Dimensions: (BINDEPTH: 50, INSTRDEPTH: 3, LATITUDE: 5, LONGITUDE: 5, TIME: 44106)
Coordinates:
* INSTRDEPTH (INSTRDEPTH) float64 100.0 280.0 600.0
* LATITUDE (LATITUDE) float64 -34.04 -33.8 -33.67 -33.56 -33.51
* LONGITUDE (LONGITUDE) float64 27.57 27.59 27.64 27.72 27.86
* TIME (TIME) datetime64[ns] 2015-04-11T15:00:00 ...
Dimensions without coordinates: BINDEPTH
Data variables:
PRES (TIME, INSTRDEPTH) float32 dask.array<shape=(44106, 3), chunksize=(44106, 3)>
VCUR (TIME, BINDEPTH) float32 dask.array<shape=(44106, 50), chunksize=(44106, 50)>
UCUR (TIME, BINDEPTH) float32 dask.array<shape=(44106, 50), chunksize=(44106, 50)>
WCUR …Run Code Online (Sandbox Code Playgroud) 上下文:我有一个包含 30 年(每日)数据的 netCDF。我想选择特定月份的跨年数据,例如每个 5 月到 3 月期间。
我可以用一个单独的函数进行选择,但我希望有一种直接的方法可以用 xarray 来做到这一点。我安装的版本是0.9.6
我正在尝试使用 xarray 打开 .netcdf 文件,但它显示此错误。我无法解决此错误,并且没有找到解决此错误的解决方案。我尝试过不同版本的 Anaconda 和 Ubuntu,但问题仍然存在。
ValueError:在 xarray 当前安装的 IO 后端 ['scipy'] 中找不到匹配项。
engine考虑通过xarray.open_dataset() 的参数 明确选择已安装的后端之一,或安装其他 IO 依赖项: http: //xarray.pydata.org/en/stable/getting-started-guide/installing.html http://xarray.pydata.org/en/stable/getting-started-guide/installing.html http://xarray.pydata.org/en/stable/getting-started-guide/installing.html /xarray.pydata.org/en/stable/user-guide/io.html
我正在尝试打开 zarr 文件,
import pandas as pd
import xarray as xr
xf = xr.open_zarr("../../data/processed/geolink_norge_dataset/geolink_norge_well_logs.zarr")
Run Code Online (Sandbox Code Playgroud)
但出现错误:
ValueError Traceback (most recent call last) <ipython-input-17-ff38d9c54463> in <module>
1 import pandas as pd
2 import xarray as xr
----> 3 xf = xr.open_zarr("../../data/processed/geolink_norge_dataset/geolink_norge_well_logs.zarr")
4
5 # We will use just the 30* wells
C:\ProgramData\Anaconda3\lib\site-packages\xarray\backends\zarr.py in open_zarr(store, group, synchronizer, chunks, decode_cf, mask_and_scale, decode_times, concat_characters, decode_coords, drop_variables, consolidated, overwrite_encoded_chunks, chunk_store, storage_options, decode_timedelta, use_cftime, **kwargs)
685 }
686
--> 687 ds = open_dataset(
688 filename_or_obj=store,
689 group=group, …Run Code Online (Sandbox Code Playgroud) 我有一个具有多个时间维度的 xarray slow_time,fast_time一个维度代表不同的对象object,一个维度反映每个对象在每个时间点的位置coords。
scipy.spatial.transform.Rotation现在的目标是针对每个时间点对该数组中的每个位置应用旋转。
我正在努力弄清楚如何使用来做我想做的事情,主要是因为我不太清楚xarray.apply_ufunc这个概念。input_core_dimensions
下面的代码显示了我正在尝试做的事情:
import numpy as np
import xarray as xr
from scipy.spatial.transform import Rotation
# dummy initial positions
initial_position = xr.DataArray(np.arange(6).reshape((-1,3)), dims=["object", "coords"])
# dummy velocities
velocity = xr.DataArray(np.array([[1, 0, 0], [0, 0.5, 0]]), dims=["object", "coords"])
slow_time = xr.DataArray(np.linspace(0, 1, 10, endpoint=False), dims=["slow_time"])
fast_time = xr.DataArray(np.linspace(0, 0.1, 100, endpoint=False), dims=["fast_time"])
# times where to evaluate my function
times = slow_time + fast_time
# this is …Run Code Online (Sandbox Code Playgroud) 我有一个数组,我想选择前 2 个或范围,跳过下一个 2,选择下一个 2,然后继续直到列表末尾
list = [2, 4, 6, 7, 9,10, 13, 11, 12,2]
results_wanted = [2,4,9,10,12,2] # note how it skipping 2. 2 is used here as and example
Run Code Online (Sandbox Code Playgroud)
有没有办法在Python中实现这一点?
我想绘制两个时间序列,其中一个在 cftime 中,另一个在 datetime 中。
一种可能性是将cftime 转换为 datetime,但这可能会给非标准 cftime 日历(例如 NoLeap)带来奇怪的结果。因此,我正在尝试将日期时间转换为 cftime。
我可以按如下方式暴力破解它,但是有可用的内置方法吗?
>>> import pandas as pd
>>> import xarray as xr
>>>
>>> da = xr.DataArray(
... [1, 2], coords={"time": pd.to_datetime(["2000-01-01", "2000-02-02"])}, dims=["time"]
... )
>>> print(da.time)
<xarray.DataArray 'time' (time: 2)>
array(['2000-01-01T00:00:00.000000000', '2000-02-02T00:00:00.000000000'],
dtype='datetime64[ns]')
Coordinates:
* time (time) datetime64[ns] 2000-01-01 2000-02-02
>>>
>>>
>>> import cftime
>>>
>>>
>>> def datetime_to_cftime(dates, kwargs={}):
... return [
... cftime.datetime(
... date.dt.year,
... date.dt.month,
... date.dt.day,
... date.dt.hour,
... …Run Code Online (Sandbox Code Playgroud) 我正在使用xarray. 组合多个 netcdf 文件xarray.open_mfdataset。但是我在运行命令时收到错误,下面是命令和错误。
nc_all = xarray.open_mfdataset(files,combine = 'nested', concat_dim="time")
files = glob.glob("/filepath/*")
Run Code Online (Sandbox Code Playgroud)
我收到以下错误 -
Traceback (most recent call last):
File "/home/lsrathore/GLEAM/GLEAM_HPC.py", line 85, in <module>
nc_1980_90 = xarray.open_mfdataset(files[1:11],combine = 'nested', concat_dim="time")
File "/home/lsrathore/.local/lib/python3.9/site-packages/xarray/backends/api.py", line 1038, in open_mfdataset
datasets = [open_(p, **open_kwargs) for p in paths]
File "/home/lsrathore/.local/lib/python3.9/site-packages/xarray/backends/api.py", line 1038, in <listcomp>
datasets = [open_(p, **open_kwargs) for p in paths]
File "/home/lsrathore/.local/lib/python3.9/site-packages/xarray/backends/api.py", line 572, in open_dataset
ds = _dataset_from_backend_dataset(
File "/home/lsrathore/.local/lib/python3.9/site-packages/xarray/backends/api.py", line 367, in _dataset_from_backend_dataset
ds …Run Code Online (Sandbox Code Playgroud) python-xarray ×10
python ×9
numpy ×4
netcdf ×3
scipy ×2
dask ×1
datetime ×1
io ×1
netcdf4 ×1
numpy-ufunc ×1
python-3.x ×1
zarr ×1