4

A netcdf data structure showed as follows:

Dimensions:  (lat: 360, lon: 720)
Coordinates:
  * lon      (lon) float32 -179.75 -179.25 -178.75 -178.25 -177.75 -177.25 ...
  * lat      (lat) float32 -89.75 -89.25 -88.75 -88.25 -87.75 -87.25 -86.75 ...
    time     datetime64[ns] 1901-01-16
 Data variables:
    cld      (lat, lon) float64 nan nan nan nan nan nan nan nan nan nan nan ...
    stn      (lat, lon) int32 -999 -999 -999 -999 -999 -999 -999 -999 -999 ...

enter image description here

I want to save this layer as .tif raster with wgs84 proj. How to convert the projection?

1

You have a least a few ways to do it:

  1. GDAL command line utilities:

Since version 2.1 GDAL working fine (read and write) with raster and vector netCDF files. You can use both GDAL command line utilities (gdal_translate and gdalwarp) to reproject and output to GeoTiff format file.

gdal_translate -of GTiff -ot Float32 -a_srs "+proj=longlat +ellps=WGS84" input.nc output.tif
  1. Execute GDAL utility from Python scripts:

Here is some example:

import subprocess

def convert_netcdf_to_geotiff(infile, outfile, band_number=1, verbose=False):

    with open(os.devnull, 'w') as devnull:
        return subprocess.check_call([
            "gdal_translate", infile, outfile,
            "-of", "GTiff",
            "-ot", "Float32",
            "-a_srs", "+proj=longlat +ellps=WGS84",
            "-b", str(band_number)

        ],
            stdout=None if verbose else devnull
        )
  1. Also, you can use GDAL utility in Docker container

If you don't want or can't install/build GDAL on your machine you can use prepared Docker images with GDAL 2.1 version or other versions with support netCDF driver.

There is one of the solutions:

cd /path/to/dir/with/netCDF/file
docker run -v $(pwd):/data geodata/gdal gdal_translate -of GTiff -ot Float32 -a_srs "+proj=longlat +ellps=WGS84" input.nc output.tif

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.