I have a xarray DataSet ds
created from a Pandas DataFrame whose info are the following:
Dimensions: (lat: 144, lon: 140, year: 9)
Coordinates:
* year (year) object 1980 1985 1990 1995 2000 2005 2010 2015 2020
* lat (lat) float64 -34.75 -34.25 -33.75 ... 35.75 36.25 36.75
* lon (lon) float64 19.75 19.25 20.25 ... -17.25 -23.75 -24.75
Data variables: (12/13)
iso3 (year, lat, lon) object 'ZAF' nan nan nan ... nan nan nan
GHS_DENS_5avg (year, lat, lon) float64 0.4566 nan nan ... nan nan nan
rural (year, lat, lon) float64 1.0 nan nan nan ... nan nan nan
cities (year, lat, lon) float64 0.0 nan nan nan ... nan nan nan
temp_meanavg (year, lat, lon) float64 17.4 nan nan nan ... nan nan nan
temp_anom_5y (year, lat, lon) float64 -0.01 nan nan nan ... nan nan nan
... ...
prec_anom_5y (year, lat, lon) float64 1.16 nan nan nan ... nan nan nan
grid_id (year, lat, lon) object '-34.75_19.75' nan nan ... nan nan
best (year, lat, lon) float64 nan nan nan nan ... nan nan nan
type_of_violence (year, lat, lon) float64 nan nan nan nan ... nan nan nan
n_event (year, lat, lon) float64 nan nan nan nan ... nan nan nan
any_event (year, lat, lon) float64 0.0 nan nan nan ... nan nan nan>
I exported this with ds.to_netcdf('my_data.nc')
, and then I tried to add it to QGIS as a Mesh Layer with Layer > Add Layer > Add Mesh Layer ...
.
I received the error message Invalid Data Source: /home/umberto/Documents/my_data.nc is not a valid or recognized data source.
I guess it might be an issue related either to setting the coordinate system (which I don't know how/where to do), or the version of the NetCDF written by xarray and not compatible with QGIS, but as I am new to NetCDF and xarray I might be completely wrong.
Here is the output of gdalinfo
if this could help better understanding the cause of my issue:
Warning 1: No UNIDATA NC_GLOBAL:Conventions attribute
Driver: netCDF/Network Common Data Format
Files: /home/umberto/Documents/my_data.nc
Size is 512, 512
Subdatasets:
SUBDATASET_1_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":iso3
SUBDATASET_1_DESC=[9x144x140] iso3 ()
SUBDATASET_2_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":GHS_DENS_5avg
SUBDATASET_2_DESC=[9x144x140] GHS_DENS_5avg (64-bit floating-point)
SUBDATASET_3_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":rural
SUBDATASET_3_DESC=[9x144x140] rural (64-bit floating-point)
SUBDATASET_4_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":cities
SUBDATASET_4_DESC=[9x144x140] cities (64-bit floating-point)
SUBDATASET_5_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":temp_meanavg
SUBDATASET_5_DESC=[9x144x140] temp_meanavg (64-bit floating-point)
SUBDATASET_6_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":temp_anom_5y
SUBDATASET_6_DESC=[9x144x140] temp_anom_5y (64-bit floating-point)
SUBDATASET_7_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":prec_meanavg
SUBDATASET_7_DESC=[9x144x140] prec_meanavg (64-bit floating-point)
SUBDATASET_8_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":prec_anom_5y
SUBDATASET_8_DESC=[9x144x140] prec_anom_5y (64-bit floating-point)
SUBDATASET_9_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":grid_id
SUBDATASET_9_DESC=[9x144x140] grid_id ()
SUBDATASET_10_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":best
SUBDATASET_10_DESC=[9x144x140] best (64-bit floating-point)
SUBDATASET_11_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":type_of_violence
SUBDATASET_11_DESC=[9x144x140] type_of_violence (64-bit floating-point)
SUBDATASET_12_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":n_event
SUBDATASET_12_DESC=[9x144x140] n_event (64-bit floating-point)
SUBDATASET_13_NAME=NETCDF:"/home/umberto/Documents/my_data.nc":any_event
SUBDATASET_13_DESC=[9x144x140] any_event (64-bit floating-point)
Corner Coordinates:
Upper Left ( 0.0, 0.0)
Lower Left ( 0.0, 512.0)
Upper Right ( 512.0, 0.0)
Lower Right ( 512.0, 512.0)
Center ( 256.0, 256.0)
EDIT
I tried as suggested to have a look at the info of another nc file which QGIS can read. I found it in this other question.
They look very similar apart for the Warning 1: No UNIDATA NC_GLOBAL:Conventions attribute
part (which I guess might be important maybe).
First of all, I changed my coordinates names to look like this dataset, namely lat
long
are now latitude
and longitude
, and year
became time
.
Also, I made sure they had the same dtype, so latitude and longitude are numpy.float32
now (before they were numpy.float62
), and time
is of type datetime64[ns]
.
Still, I receive the same error with QGIS trying to import as Mesh layer.
I can import the sample dataset and there seem to be no coordinate system set, so I can set it directly inside its properties in QGIS.
I can apparently import my nc file by drag-and-dropping it directly in QGIS, but it is treated like a sort of multiband raster (a layer for each variable I have), and there seem to be no way to access the time information (I don't know which year I am visualizing).
Is there a way to set the coordinate system with xarray (if this is the problem)?