Database Reference
In-Depth Information
Importing netCDF datasets with Python
and GDAL
Inthisrecipe,youwillwriteaPythonscripttoimportdatafromthenetCDFformatto
PostGIS.
netCDF is an open standard format, widely used for scientific applications, that can
containmultiplerasterdatasets,eachcomposedofaspectrumofbands.Forthispur-
pose,youwillusetheGDALPythonbindingsandthepopularNumpyscientificlibrary.
Getting ready
1. If you are using Windows, be sure to install OSGeo4W, as suggested in the
initial instructions of this chapter, that will include Python and GDAL Python
bindings with Numpy support.
ForLinuxusers,ifyoudidnotdoit,followtheinitialinstructionsofthischapter
and create a Python virtual environment, in order to keep a Python-isolated
environment to be used for all the Python recipes of this topic, and activate it:
$ source postgis-cb-env/bin/activate
2. Forthisrecipe,youneedtheGDALPythonbindingsandNumpy,thelatestbe-
ingneededbysomeGDALmethods( ReadAsArray )forarrays.Inthemost
likely scenario, you have already installed GDAL in your virtual environment
asyouhavebeenusingitforotherrecipes;sobesuretoremoveitandrein-
stallitafterinstallingNumpy.Infact,GDALneedstobecompiledwithNumpy
support if you want to use its array's features:
(postgis-cb-env)$ pip uninstall gdal
(postgis-cb-env)$ pip install numpy
(postgis-cb-env)$ pip install gdal
3. Forthepurposeofthisrecipe,youwilluseasampledatasetfromtheNOAA
Earth System Research Laboratory (ESRL). The excellent ESRL web portal
offers a plethora of data in the netCDF format to be freely downloaded. For
example, download the following dataset from the ESRL CPC Soil Moisture
Search WWH ::




Custom Search