I’d appreciate some help with loading MODIS thermal anomaly data into Xarray and am asking here because I’m not really sure where in the stack this question belongs.
Overview
MODIS/Terra Thermal Anomalies/Fire 5-Min L2 Swath 1km data includes a “Fire Pixel Table”, which includes information about fire pixels as 27 separate SDSs (HDF4 scientific dataset objects). If there are no fire pixels in the granule, these SDSs have len 0 dimensions and fail to load in both NetCDF4 and pyhdf, causing issues downstream with xarray.open_dataset
. The HDF4 SDSs are variables, rather than groups, so using group="fire mask"
in xr.open_dataset
doesn’t work, nor does drop_variables
becuase the backends fail before that kwarg is used.
Does anyone have a simple solution for ignoring these 27 extra variables, a different workaround, or a suggestion for where to raise an issue about this?
MVCE
import os
import earthaccess
from netCDF4 import Dataset
earthaccess.login()
# Download some MODIS/Terra Thermal Anomalies/Fire 5-Min L2 Swath 1km data (100 files)
results = earthaccess.search_data(
concept_id="C2271754179-LPCLOUD", count=1, temporal=("2015-06-01", "2015-12-31")
)
file = earthaccess.download(results, "earthaccess_data")[0]
cwd = os.getcwd()
fh = Dataset(os.path.join(cwd, file), mode="r")
# Fire mask works
print(fh.variables["fire mask"].shape)
# FP_line (part of the fire pixel table) doesn't
print(fh.variables["FP_line"].shape)
Error:
Traceback (most recent call last):
File "/Users/max/miniforge3/envs/modis-readers/lib/python3.12/runpy.py", line 198, in _run_module_as_main
return _run_code(code, main_globals, None,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/max/miniforge3/envs/modis-readers/lib/python3.12/runpy.py", line 88, in _run_code
exec(code, run_globals)
File "/Users/max/.vscode/extensions/ms-python.debugpy-2024.10.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
cli.main()
File "/Users/max/.vscode/extensions/ms-python.debugpy-2024.10.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
run()
File "/Users/max/.vscode/extensions/ms-python.debugpy-2024.10.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
runpy.run_path(target, run_name="__main__")
File "/Users/max/.vscode/extensions/ms-python.debugpy-2024.10.0-darwin-arm64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
return _run_module_code(code, init_globals, run_name,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/max/.vscode/extensions/ms-python.debugpy-2024.10.0-darwin-arm64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/Users/max/.vscode/extensions/ms-python.debugpy-2024.10.0-darwin-arm64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
exec(code, run_globals)
File "/Users/max/Documents/Code/maxrjones/modis-readers/MVCE.py", line 17, in <module>
print(fh.variables["FP_line"].shape)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "src/netCDF4/_netCDF4.pyx", line 4563, in netCDF4._netCDF4.Variable.shape.__get__
File "src/netCDF4/_netCDF4.pyx", line 3734, in netCDF4._netCDF4.Dimension.__len__
File "src/netCDF4/_netCDF4.pyx", line 2113, in netCDF4._netCDF4._ensure_nc_success
RuntimeError: NetCDF: HDF error
Full demo
modis-readers/find-errors.ipynb at main · maxrjones/modis-readers · GitHub shows that NetCDF4 fails on all the granules that do not have any fire pixels.