Posts

Showing posts from April, 2019

An example to read a NetCDF file with Python

Image
For this post I'm going to use this post and the following files as reference.

NetCDF files:  air.sig995.2012.nc air.departure.sig995.2012.nc darwin_2012.nc Source code:  netcdf_example.py

If you want to see air.sig995.2012.nc content you can try with Integrated Data Viewer (IDV).
To download just execute:

wget https://www.unidata.ucar.edu/downloads/idv/current/ftp/idv_5_5_linux64_installer.sh
bash idv_5_5_linux64_installer.sh


Now, execute the python example file:
python netcdf_example.py
You probably will get the following errors:
ImportError: No module named functools_lru_cache ImportError: No module named basemap
You can install modules from pip, but in my case I can't solve the issue. To fix it install them from apt as follows:
sudo apt install python-backports.functools-lru-cache  python-mpltoolkits.basemap
Enjoy it!


How to convert a NetCDF file TIFF to import in QGIS 3.x

NetCDF is a data format and library designed to store multidimensional arrays of scientific data.
If you try to load it in Qgis, probably you will get the following message:

Invalid Layer: GDAL provider Cannot open GDAL dataset file.nc' not recognized as a supported file format. Raster layer Provider is not valid (provider: gdal, URI: file.nc

In QGIS 2.x you can use NetCDF plugin to load the layer, but it not exist yet in QGIS 3.x
To import the layer you need to convert the .nc file to another like .tiff

To do it, you can use gdal toolkit as follows:

gdal_translate file.nc file.tiff 
Warning 1: No UNIDATA NC_GLOBAL:Conventions attribute
Input file contains subdatasets. Please, select one of them for reading.

This issue is because the .nc contains subdatasets and you must specify only one.

To see the subdatasets names just execute the following sentence in your terminal:

gdalinfo file.nc

Subdatasets:
  SUBDATASET_1_NAME=NETCDF:"FWI.GPM.LATE.v5.Monthly.Default.201903.nc":GPM.LATE…

ERROR (internal_error): Operation on mixed SRID geometries

I got this issue when I tried to optimize a pgsql code with postgis.
See the following example that works with if you join it with other sentence:

SELECT ST_SetSRID(ST_MakeLine(ST_MakePoint(4.51, 52.2), ST_MakePoint(23.42, 37.58)),4326)

But, if you only replace ST_MakePoint by a geom selection you will have an error:

SELECT ST_SetSRID(ST_MakeLine( (select point from nodes where name='3'), (select point from nodes where name='20')) ,4326)

ERROR: Operation on mixed SRID geometries SQL state: XX000

To solve it just delete ST_SetSRID as follows:

SELECT ST_MakeLine( (select point from nodes where name='3'), (select point from nodes where name='20'))

Now it works
Enjoy it!