错误地读取使用Python的NetCDF和HDF文件
我有以下代码,可以让我打开 .nc
或 .h5
文件,并且可以打开其内容。
import gdal
import glob
import os
import numpy as np
import pandas
def nch5read(fn, ncmaskscale=True):
import netCDF4 as nc
import h5py
from pyhdf.SD import SD, SDC
d = {'VARS':{}, 'ATTRS':{}}
if os.path.splitext(fn)[1] == '.nc':
f = nc.Dataset(fn)
f.set_auto_maskandscale(ncmaskscale)
for i in f.ncattrs():
try:
d['ATTRS'][i] = f.getncattr(i)
except:
pass
for i in f.variables:
try:
d['VARS'][i] = {'DATA':np.squeeze(f.variables[i][:]),
'ATTRS':{j:f.variables[i].getncattr(j) for j in f.variables
}}
except:
pass
if os.path.splitext(fn)[1]== '.h5':
f = h5py.File(fn)
d['ATTRS']=dict(f.attrs)
for i in f:
try:
d['VARS'][i] = {'DATA':np.squeeze(f[i][()]),
'ATTRS':dict(f[i].attrs)}
except:
pass
return d
fn = "cons_dom.nc"
d = nch5read(fn)
print(d)
我正在使用从该网站获取的示例生活用水文件 cons_dom.nc
https://zenodo.org/record/897933#.YkH_SefMKUn
但在运行代码时,我得到一个空白结果 {'VARS': {}, 'ATTRS': {}}
另外,我尝试在代码中添加 .hdf
部分
import gdal
import glob
import os
import numpy as np
import pandas
def nch5read(fn, ncmaskscale=True):
import netCDF4 as nc
import h5py
from pyhdf.SD import SD, SDC
d = {'VARS':{}, 'ATTRS':{}}
if os.path.splitext(fn)[1] == '.nc':
f = nc.Dataset(fn)
f.set_auto_maskandscale(ncmaskscale)
for i in f.ncattrs():
try:
d['ATTRS'][i] = f.getncattr(i)
except:
pass
for i in f.variables:
try:
d['VARS'][i] = {'DATA':np.squeeze(f.variables[i][:]),
'ATTRS':{j:f.variables[i].getncattr(j) for j in f.variables
}}
except:
pass
if os.path.splitext(fn)[1]== '.h5':
f = h5py.File(fn)
d['ATTRS']=dict(f.attrs)
for i in f:
try:
d['VARS'][i] = {'DATA':np.squeeze(f[i][()]),
'ATTRS':dict(f[i].attrs)}
except:
pass
# Segment added to read .hdf files
if os.path.splitext(fn)[1]== '.hdf':
f = SD(fn, SDC.READ)
f.set_auto_maskandscale(ncmaskscale)
for i in f.variables:
try:
d['VARS'][i] = {'DATA':np.squeeze(f.variables[i][:]),
'ATTRS':{j:f.variables[i].getncattr(j) for j in f.variables}}
except:
pass
return d
fn = "MCD12Q1.A2016001.h10v05.006.2018149125004.hdf"
d = nch5read(fn)
print(d)
但是当我尝试跑步示例 MODIS
文件,MCD12Q1.A2016001.h10v05.006.2018149125004.hdf
我收到错误
File "C:\Users\Jay_Oliver\Anaconda3\envs\rasterio\lib\site-packages\pyhdf\SD.py", line 3222, in _getattr
index = a.index()
File "C:\Users\Jay_Oliver\Anaconda3\envs\rasterio\lib\site-packages\pyhdf\SD.py", line 1193, in index
_checkErr('find', self._index, 'illegal attribute name')
File "C:\Users\Jay_Oliver\Anaconda3\envs\rasterio\lib\site-packages\pyhdf\error.py", line 23, in _checkErr
raise HDF4Error(err)
pyhdf.error.HDF4Error: find (59): Invalid arguments to routine
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "nc_example.py", line 63, in <module>
d = nch5read(fn)
File "nc_example.py", line 52, in nch5read
f.set_auto_maskandscale(ncmaskscale)
File "C:\Users\Jay_Oliver\Anaconda3\envs\rasterio\lib\site-packages\pyhdf\SD.py", line 1446, in __getattr__
return _getattr(self, name)
File "C:\Users\Jay_Oliver\Anaconda3\envs\rasterio\lib\site-packages\pyhdf\SD.py", line 3224, in _getattr
raise AttributeError("attribute not found")
AttributeError: attribute not found
I have the following code that would allow me to open .nc
or .h5
files, and would supposidly open its contents.
import gdal
import glob
import os
import numpy as np
import pandas
def nch5read(fn, ncmaskscale=True):
import netCDF4 as nc
import h5py
from pyhdf.SD import SD, SDC
d = {'VARS':{}, 'ATTRS':{}}
if os.path.splitext(fn)[1] == '.nc':
f = nc.Dataset(fn)
f.set_auto_maskandscale(ncmaskscale)
for i in f.ncattrs():
try:
d['ATTRS'][i] = f.getncattr(i)
except:
pass
for i in f.variables:
try:
d['VARS'][i] = {'DATA':np.squeeze(f.variables[i][:]),
'ATTRS':{j:f.variables[i].getncattr(j) for j in f.variables
}}
except:
pass
if os.path.splitext(fn)[1]== '.h5':
f = h5py.File(fn)
d['ATTRS']=dict(f.attrs)
for i in f:
try:
d['VARS'][i] = {'DATA':np.squeeze(f[i][()]),
'ATTRS':dict(f[i].attrs)}
except:
pass
return d
fn = "cons_dom.nc"
d = nch5read(fn)
print(d)
I am using an example domestic water use file, cons_dom.nc
obtained from this website https://zenodo.org/record/897933#.YkH_SefMKUn
but when running the code, I get a blank result {'VARS': {}, 'ATTRS': {}}
Additionally, I tried adding a section for .hdf
into the code
import gdal
import glob
import os
import numpy as np
import pandas
def nch5read(fn, ncmaskscale=True):
import netCDF4 as nc
import h5py
from pyhdf.SD import SD, SDC
d = {'VARS':{}, 'ATTRS':{}}
if os.path.splitext(fn)[1] == '.nc':
f = nc.Dataset(fn)
f.set_auto_maskandscale(ncmaskscale)
for i in f.ncattrs():
try:
d['ATTRS'][i] = f.getncattr(i)
except:
pass
for i in f.variables:
try:
d['VARS'][i] = {'DATA':np.squeeze(f.variables[i][:]),
'ATTRS':{j:f.variables[i].getncattr(j) for j in f.variables
}}
except:
pass
if os.path.splitext(fn)[1]== '.h5':
f = h5py.File(fn)
d['ATTRS']=dict(f.attrs)
for i in f:
try:
d['VARS'][i] = {'DATA':np.squeeze(f[i][()]),
'ATTRS':dict(f[i].attrs)}
except:
pass
# Segment added to read .hdf files
if os.path.splitext(fn)[1]== '.hdf':
f = SD(fn, SDC.READ)
f.set_auto_maskandscale(ncmaskscale)
for i in f.variables:
try:
d['VARS'][i] = {'DATA':np.squeeze(f.variables[i][:]),
'ATTRS':{j:f.variables[i].getncattr(j) for j in f.variables}}
except:
pass
return d
fn = "MCD12Q1.A2016001.h10v05.006.2018149125004.hdf"
d = nch5read(fn)
print(d)
But when I try running a sample MODIS
file, MCD12Q1.A2016001.h10v05.006.2018149125004.hdf
I get an error
File "C:\Users\Jay_Oliver\Anaconda3\envs\rasterio\lib\site-packages\pyhdf\SD.py", line 3222, in _getattr
index = a.index()
File "C:\Users\Jay_Oliver\Anaconda3\envs\rasterio\lib\site-packages\pyhdf\SD.py", line 1193, in index
_checkErr('find', self._index, 'illegal attribute name')
File "C:\Users\Jay_Oliver\Anaconda3\envs\rasterio\lib\site-packages\pyhdf\error.py", line 23, in _checkErr
raise HDF4Error(err)
pyhdf.error.HDF4Error: find (59): Invalid arguments to routine
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "nc_example.py", line 63, in <module>
d = nch5read(fn)
File "nc_example.py", line 52, in nch5read
f.set_auto_maskandscale(ncmaskscale)
File "C:\Users\Jay_Oliver\Anaconda3\envs\rasterio\lib\site-packages\pyhdf\SD.py", line 1446, in __getattr__
return _getattr(self, name)
File "C:\Users\Jay_Oliver\Anaconda3\envs\rasterio\lib\site-packages\pyhdf\SD.py", line 3224, in _getattr
raise AttributeError("attribute not found")
AttributeError: attribute not found
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论