isciences / exactextract

Fast and accurate raster zonal statistics
Apache License 2.0
246 stars 32 forks source link

Error raised when calling a raster with multiple data variables #56

Closed njdepsky closed 1 week ago

njdepsky commented 8 months ago

Is there a way to run zone stats on a NetCDF file that has multiple data variables, while just referencing one of them in the exactextract call? I commonly construct NC files with multiple vars in xarray and realized that any with more than one data variable result in the following error:

ERROR 10: Pointer 'hBand' is NULL in 'GDALGetRasterNoDataValue'.

Example function call: exactextract -r "maxtemp:NETCDF:my_temp_dataset.nc[1]:maxtemp" -p gadm41_adm0.gpkg -f GID_0 -s "mean(var1)" -o output_test.csv

The above functions as long as 'maxtemp' is the only data variable in the NC file, but if I have multiple (e.g. 'maxtemp, 'mintemp'), I get the above error. I'd like to avoid reading/writing separate NC files for each individual data variable in order to run exactextract, if possible.

Thanks!

dbaston commented 8 months ago

It works for me on the netCDF files I have with multiple variables. Do you have a file you can share so I can reproduce the error?

In the not-too-distant future you should be able to call exactextract on the xarray objects directly from Python.

njdepsky commented 8 months ago

Sure thing. I just downloaded a single monthly average dewpoint 2m temperature NC file from ERA5 and then created a second data var that is 2x the 'd2m' values provided, and even copied the same attributes from the 'd2m' variable over to the newly created one. Still getting the hBand when trying to run EE on either var.

era5_d2m_modified.nc.zip

dbaston commented 8 months ago

This works for me:

exactextract -r "d2m:NETCDF:/home/dan/Downloads/era5_d2m_modified.nc:d2m[1]" -p /home/dan/data/gadm36_level_0.gpkg -f GID_0 -s "mean(d2m)" -o /tmp/test.csv
GID_0,d2m_mean
ABW,-nan
AFG,4685.02294921875
AGO,26304.724609375
AIA,-nan
ALA,3962.07885742188
...

This is the latest master version of exactextract? What is your platform / GDAL version?

njdepsky commented 8 months ago

Ok interesting. I just re-installed exact extract and tried again - still getting the error but functions fine with single variable datasets. But I am getting a warning about a pybind11 path inconsistency upon running the cmake commmand when installing exact extract. Perhaps it's related to that issue? I'm running GDAL 3.8.1.1 and my platform is a 2022 Macbook Pro running OS Ventura (13.5) and an M2 processor. How not-to-distant is this python package future? :)

dbaston commented 8 months ago

But I am getting a warning about a pybind11 path inconsistency upon running the cmake commmand when installing exact extract.

It doesn't sound related, but please feel free to open an issue with the warning you're getting.

How not-to-distant is this python package future?

It's committed to the repository, I just need to add a setup.py script. I don't have support direct xarray yet, though.

dbaston commented 8 months ago

With https://github.com/isciences/exactextract/pull/59 you can do, e.g.

from exactextract import exact_extract
import fiona
import rioxarray
import xarray

xds = xarray.open_dataset('/tmp/era5.nc')

polys = fiona.open('/home/dan/data/ne_50m_admin_0_countries.shp')

results = exact_extract(xds['d2m'], polys, 'mean')

this will produce a result for each of the 24 values of the time dimension in the input file:

{'properties': {'mean_band_24': 274.90179443359375, 'mean_band_19': 274.3922119140625, 'mean_band_17': 274.0401611328125, 'mean_band_16': 274.1055603027344, 'mean_band_15': 274.87017822265625, 'mean_band_14': 274.70465087890625, 'mean_band_1': 277.6044616699219, 'mean_band_5': 277.9237365722656, 'mean_band_21': 274.9862060546875, 'mean_band_20': 274.6240539550781, 'mean_band_3': 277.5119323730469, 'mean_band_22': 274.4286193847656, 'mean_band_4': 277.70111083984375, 'mean_band_11': 276.3414611816406, 'mean_band_6': 277.5834045410156, 'mean_band_2': 277.75408935546875, 'mean_band_7': 277.19952392578125, 'mean_band_8': 277.51275634765625, 'mean_band_9': 276.6864013671875, 'mean_band_23': 274.390625, 'mean_band_12': 275.48687744140625, 'mean_band_10': 276.0666809082031, 'mean_band_18': 274.7309265136719, 'mean_band_13': 274.86041259765625}}