RasterToNetcdf

Writes rasters to a NetCDF file that follows the CF conventions: a single raster, or a time series of rasters on the same grid, as the daily outputs of a model, with a time axis. The file keeps the crs of the rasters, so GDAL, QGIS, the other CF readers and NetcdfGridDumper place it right.

For a time series, the rasters are the ones of a folder, in the order of their file names, and the timestamps are given one per line in the same order, as 2024-07-01 00:00, in UTC. The rasters are written one at a time, so a long series doesn’t need to fit in memory.

Details

The file is a NetCDF 3 classic file following the CF 1.8 conventions, written in pure Java, readable by GDAL, and so QGIS, by netcdf-java and by the other CF readers. The values are written as 32 bit floats, with the novalue of the raster as fill value; the classic format limits a variable to about 2 GB.

A raster in lat/long WGS84 gets lat and lon axes in degrees, a projected one x and y axes in the units of its crs. The axes hold the cell centers, with the rows from south to north, as most CF files have them.

The crs is described in the grid mapping variable crs: as WKT in the attributes crs_wkt (CF) and spatial_ref (GDAL), with its EPSG code in epsg_code when it has one, and with the ellipsoid. For the transverse Mercator projections, as UTM and the Gauss-Boaga zones, also the CF projection parameters are written, which other CF readers, as netcdf-java, use.

A time series has an unlimited time axis in seconds since 1970-01-01 UTC, and the rasters are written one at a time, so long series don’t need to fit in memory. All its rasters must have the same grid and crs. Without timestamp the file has no time axis and holds a single raster.

NetcdfGridDumper reads the files back with their crs and grid unchanged.

Parameters

Writes rasters to a CF NetCDF file: a single raster, or a time series of rasters on the same grid, one per timestep.

Toolbox folder: NetCDF
Status: Certified
Keywords: IO, NetCDF, CF, Raster, Time series, Writing
Authors: Andrea Antonello
Class: org.hortonmachine.modules.RasterToNetcdf

Inputs

Parameter

Type

Unit

Default

Description

inRaster

raster file

The raster to write, for a file with a single raster.

inFolder

folder

Alternative to the raster: the folder with the rasters of a time series, all on the same grid, taken in the order of their file names.

pTimestamps

String

The UTC timestamps of the rasters, as yyyy-MM-dd HH:mm, one per line in the order of the rasters. Optional for a single raster, which then has no time.

pVariableName

String

data

The name of the variable of the rasters.

pLongName

String

The optional long name of the variable.

pUnits

String

The optional units of the values of the variable.

Outputs

Parameter

Type

Unit

Description

outNetcdf

output file

The NetCDF file to write.

Example

The example writes the sample elevation model of the manual, dtm_flanginec.tif in docs/manual/modules/maps/data, as a single raster without time:

import org.hortonmachine.modules.RasterToNetcdf

// the folder with the elevation model: change it to yours
var folder = "/data/flanginec/"

// the elevation model as a NetCDF file, a single raster without time
var toNetcdf = new RasterToNetcdf()
toNetcdf.inRaster = folder + "dtm_flanginec.tif"
toNetcdf.pVariableName = "elevation"
toNetcdf.pLongName = "terrain elevation"
toNetcdf.pUnits = "m"
toNetcdf.outNetcdf = folder + "dtm_flanginec.nc"
toNetcdf.process()

NetcdfInfo shows the result: the grid elevation, with its long name and units, and the projection of the elevation model, Monte Mario / Italy zone 1, a transverse Mercator that netcdf-java reads from the CF parameters of the grid mapping variable crs, with the axes converted to kilometers:

0) elevation:
	Attributes:
		long_name: terrain elevation
		units: m
		_FillValue: -9999.0
		grid_mapping: crs
	Projection: TransverseMercatorProjection
	Proj X Axis (x): 1637.145 -> 1640.685   (355)  extent: 3.5399999999999636
	Proj Y Axis (y): 5110.835 -> 5114.435   (361)  extent: 3.600000000000364
	Longitude: 10.775456546318322 -> 10.822330421671925  extent: 0.046873875353602656
	Latitude: 46.13570831830624 -> 46.16881288768088  extent: 0.03310456937463613

The second example of NetcdfGridDumper reads the file back to the same elevation model.