英文:
I want to select data using ranges of longitudes and latitudes in a NetCDF4 file using Python on Windows. I can't even open the dataset with xarray
问题
以下是翻译好的部分:
我希望将其转换为DataFrame会有所帮助。我原计划在生成的DataFrame上使用between函数。但是即使使用xarray,转换本身也无法完成。我有另一个原因希望将其转换为DataFrame:使用Pandas的statsmodels。
由于我使用的是Windows,无法使用nctoolkit。
#导入模块
import requests
import netCDF4 as nc
import gzip
#从GISS下载gzipped数据文件并加载到netCDF Dataset中
GISTEMPfile = requests.get('https://data.giss.nasa.gov/pub/gistemp/gistemp250_GHCNv4.nc.gz')
ds = nc.Dataset("dummy_path", mode="r", memory=gzip.decompress(GISTEMPfile.content))
ds.variables
v = ds.variables['tempanomaly']
#从xarray导入DataFrame
import xarray as xr
temp = xr.open_dataset(ds)
df = temp.to_dataframe()
由于某种原因,我一直在循环尝试...
尝试使用'netcdf4'引擎 ->
temp = xr.open_dataset(ds, engine='netcdf4')
出现错误信息:
File "C:\Users\jean-\Desktop\WPy64-31050\python-3.10.5.amd64\lib\site-packages\xarray\backends\api.py", line 495, in open_dataset
backend_ds = backend.open_dataset(
File "C:\Users\jean-\Desktop\WPy64-31050\python-3.10.5.amd64\lib\site-packages\xarray\backends\netCDF4_.py", line 553, in open_dataset
store = NetCDF4DataStore.open(
File "C:\Users\jean-\Desktop\WPy64-31050\python-3.10.5.amd64\lib\site-packages\xarray\backends\netCDF4_.py", line 355, in open
raise ValueError(
ValueError: can only read bytes or file-like objects with engine='scipy' or 'h5netcdf'
尝试使用'h5netcdf'引擎 ->
temp = xr.open_dataset(ds, engine='h5netcdf')
出现错误信息:
File "C:\Users\jean-\Desktop\WPy64-31050\python-3.10.5.amd64\lib\site-packages\xarray\backends\api.py", line 481, in open_dataset
backend = plugins.get_backend(engine)
File "C:\Users\jean-\Desktop\WPy64-31050\python-3.10.5.amd64\lib\site-packages\xarray\backends\plugins.py", line 156, in get_backend
raise ValueError(
ValueError: unrecognized engine h5netcdf must be one of: ['netcdf4', 'scipy', 'store', 'zarr']
我尝试在升级到WinPython 3.11.3.1后再次尝试,但没有成功:
temp = xr.open_dataset(ds)
出现错误信息:
File ~\Desktop\WPy64-31131\python-3.11.3.amd64\Lib\site-packages\xarray\backends\api.py:509 in open_dataset
engine = plugins.guess_engine(filename_or_obj)
File ~\Desktop\WPy64-31131\python-3.11.3.amd64\Lib\site-packages\xarray\backends\plugins.py:197 in guess_engine
raise ValueError(error_msg)
ValueError: did not find a match in any of xarray's currently installed IO backends ['netcdf4', 'h5netcdf', 'scipy', 'zarr']. Consider explicitly selecting one of the installed engines via the ``engine`` parameter, or installing additional IO dependencies
我需要注意的依赖项吗?
<details>
<summary>英文:</summary>
I hoped that converting it into a DataFrame would help. I had planned to use the between function on the resulting dataframe. But the conversion itself does not work, even with xarray. I have another reason to want to convert it into a DataFrame: using Panda statsmodels.
Since I am on Windows, I can't use nctoolkit.
```python
#imports
import requests
import netCDF4 as nc
import gzip
#
# Download the gzipped datafile from GISS and load it into a netCDF Dataset
GISTEMPfile = requests.get('https://data.giss.nasa.gov/pub/gistemp/gistemp250_GHCNv4.nc.gz')
ds = nc.Dataset("dummy_path", mode="r", memory=gzip.decompress(GISTEMPfile.content))
ds.variables
v = ds.variables['tempanomaly']
#
#from pandas import DataFrame
#df = DataFrame(ds)
#
import xarray as xr
temp = xr.open_dataset(ds)
df = temp.to_dataframe()
For some reason, I am going in circles...
Try with 'netcdf4' engine ->
temp = xr.open_dataset(ds, engine='netcdf4')
File "C:\Users\jean-\Desktop\WPy64-31050\python-3.10.5.amd64\lib\site-packages\xarray\backends\api.py", line 495, in open_dataset
backend_ds = backend.open_dataset(
File "C:\Users\jean-\Desktop\WPy64-31050\python-3.10.5.amd64\lib\site-packages\xarray\backends\netCDF4_.py", line 553, in open_dataset
store = NetCDF4DataStore.open(
File "C:\Users\jean-\Desktop\WPy64-31050\python-3.10.5.amd64\lib\site-packages\xarray\backends\netCDF4_.py", line 355, in open
raise ValueError(
ValueError: can only read bytes or file-like objects with engine='scipy' or 'h5netcdf'
Try with 'h5netcdf' engine ->
temp = xr.open_dataset(ds, engine='h5netcdf')
File "C:\Users\jean-\Desktop\WPy64-31050\python-3.10.5.amd64\lib\site-packages\xarray\backends\api.py", line 481, in open_dataset
backend = plugins.get_backend(engine)
File "C:\Users\jean-\Desktop\WPy64-31050\python-3.10.5.amd64\lib\site-packages\xarray\backends\plugins.py", line 156, in get_backend
raise ValueError(
ValueError: unrecognized engine h5netcdf must be one of: ['netcdf4', 'scipy', 'store', 'zarr']
I tried again without specifying the engine after upgrading to WinPython 3.11.3.1, to no avail ->
temp = xr.open_dataset(ds)
Traceback (most recent call last):
Cell In[3], line 1
temp = xr.open_dataset(ds)
File ~\Desktop\WPy64-31131\python-3.11.3.amd64\Lib\site-packages\xarray\backends\api.py:509 in open_dataset
engine = plugins.guess_engine(filename_or_obj)
File ~\Desktop\WPy64-31131\python-3.11.3.amd64\Lib\site-packages\xarray\backends\plugins.py:197 in guess_engine
raise ValueError(error_msg)
ValueError: did not find a match in any of xarray's currently installed IO backends ['netcdf4', 'h5netcdf', 'scipy', 'zarr']. Consider explicitly selecting one of the installed engines via the engine
parameter, or installing additional IO dependencies
Are there dependencies that I should be aware of?
答案1
得分: 1
Xarray支持使用sel
方法和切片对象进行范围选择。例如:
ds = xr.open_dataset(...)
ds_region = ds.sel(lon=slice(-85.0, -80.1), lat=slice(-10.0, 10.0))
英文:
Xarray supports range based selection using the sel
method and the slice object. For example:
ds = xr.open_dataset(...)
ds_region = ds.sel(lon=slice(-85.0, -80.1), lat=slice(-10.0, 10.0))
答案2
得分: 0
我找到了一种方法来打开我的数据集,使用这个问题的第一个答案 -> https://stackoverflow.com/questions/46433812/simple-conversion-of-netcdf4-dataset-to-xarray-dataset?rq=3
而不是使用
temp = xr.open_dataset(ds, engine='netcdf4')
例如,我使用了
temp = xr.open_dataset(xr.backends.NetCDF4DataStore(ds))
,然后我可以在使用我原来问题的第一个答案对其进行切片后将其转换为DataFrame。
英文:
I found a way to open my dataset using the first answer to this question ->
https://stackoverflow.com/questions/46433812/simple-conversion-of-netcdf4-dataset-to-xarray-dataset?rq=3
Instead of using
'''python
temp = xr.open_dataset(ds, engine='netcdf4')
'''
, for instance, I used
'''python
temp = xr.open_dataset(xr.backends.NetCDF4DataStore(ds))
'''
, which I could then convert into a DataFrame after slicing it up using the first answer I got to my original question.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论