0

I am working with some netCDF files and want to import netCDF parameter's data as a Raster and build a contour layer for it. I am using gdal_contour for this.

When I import the netCDF and choose a parameter (water_temp) in QGIS, the raster is loaded into the map with no problem and displays values in the range of roughly 4 degC to 31.25 degC.

However, when I use gdal_contour to make a contour layer for it, the values are in the range of -15944 to 11250. It certainly doesn't help that among other issues, it takes forever to generate the layer because I'm specifying an interval of 1.0 and the value range is far larger than the expected temperatures for Celsius.

From what I can tell, it looks like perhaps gdal_contour either isn't respecting the raster band's offset and scale_factor or has no knowledge of it. I understand that the netCDF is storing the temperature values as integers instead of floats to optimize file size, but I'm a bit confused by why QGIS can understand the offset when reading the netCDF into a raster layer, but not when generating a contour layer.

Am I missing something, or is there perhaps a caveat to using gdal_contour of which I'm unaware?


The command I am using to generate the conotur layer is:

gdal_contour -b 1 -a water_temp -i 1.0 -snodata -30000.0 -f "ESRI Shapefile" NETCDF:"C:/path/to/input/netcdf/INPUT.nc":water_temp C:/path/to/output/layer/OUTPUT.shp

The scale_factor, offset, and associated metadata for the band are:

  • add_offset=20
  • missing_value=-30000
  • NETCDF_VARNAME=water_temp
  • scale_factor=0.001
  • STATISTICS_MAXIMUM=11250
  • STATISTICS_MEAN=5475.011083141
  • STATISTICS_MINIMUM=-15944
  • STATISTICS_STDDEV=5863.9957197805
  • units=degC
  • _FillValue=-30000
  • What is your GDAL version? Have you tried to convert raster layer first into GeoTIFF and create contours from that for testing if the problem is netCDF specific? – user30184 Nov 14 '19 at 20:33
  • I'm using GDAL v2.4.1. I have tried converting to a GeoTIFF first, and same result. – Andrew Fink Nov 14 '19 at 20:40
  • I am not sure if scale and offset is supposed to have an effect or are they just showed for user as metadata. I found this ticket github.com/qgis/QGIS/pull/1066. But I think that conversion into GeoTIFF should work with the -unscale option gdal.org/programs/gdal_translate.html. – user30184 Nov 14 '19 at 21:09
  • @user30184 That did it! I had to convert to a GeoTIFF using gdal_translate, with the -unscale option, and then perform the gdal_contour operation on that. If you re-post it as an answer, I'd be happy to mark it as the accepted answer. Thanks! – Andrew Fink Nov 14 '19 at 22:12
0

It seems that while GDAL reads and interprets the metadata about scale and offset it does not automatically adjust the raw pixel values.

User can ask gdal_translate https://gdal.org/programs/gdal_translate.html to apply scale and offset to source data and output a new raster by using the -unscale option.

-unscale

Apply the scale/offset metadata for the bands to convert scaled values to unscaled values. It is also often necessary to reset the output datatype with the -ot switch.

The remark about a need to reset the datatype means that it may not be possible to unscale data and keep the original datatype. For example if data fits into 8 bit as scaled it may require 16 bit as unscaled.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.