I have a NetCDF file containing bathymetry. I am using the GDAL and NETCDF4 libraries to export the file to ESRI ASCII using the 'AAIGrid' format. However, a common problem is the latitudes are in the reverse order (see here for patch made to gdal and here for Matlab and here for R solutions).

I think the solution can be achieved using Numpy and flipud function. I am not sure whether to flip the bathymetry or the latitude. When plotting using matplotlib, both seem to work to fix the problem.

I seem to be encountering two problems. First, when using gdal.Translate, the output Dataset cannot be passed as the NetCDF4 'Dataset' object - it must be a file. I would like to pass it a Dataset object which I have modified by flipping the latitudes. When I pass in an object (even an unmodified Dataset object which just references the original .nc file), I get the following error:

#gdal.Translate("out.txt", "test4.nc", format="AAIGrid", noData="-10")
gdal.Translate("out.txt", new_netcdf, format="AAIGrid", noData="-10")

    TypeError: in method 'TranslateInternal', argument 2 of type 'GDALDatasetShadow *'

Second, I cannot seem to flip the latitudes by modifying the original file.

from netCDF4 import Dataset
from osgeo import gdal
import numpy as np

file = "ORIGINAL\Bathymetry_1km.nc"
netcdf_f = Dataset(file, "r", format="NETCDF4")

latitudes = netcdf_f.variables['lat']
lat_flipped = np.flipud(latitudes)
netcdf_f.variables['lat'][:] = lat_flipped

RuntimeError                              Traceback (most recent call last)
<ipython-input-126-457cda137417> in <module>
----> 1 netcdf_f.variables['lat'][:] = lat_flipped
netCDF4\_netCDF4.pyx in netCDF4._netCDF4.Variable.__setitem__()
netCDF4\_netCDF4.pyx in netCDF4._netCDF4.Variable._put()
netCDF4\_netCDF4.pyx in netCDF4._netCDF4._ensure_nc_success()
RuntimeError: NetCDF: HDF error

So, next I create a new NetCDF 'Dataset' object (actually as a file on the drive because I can't figure out an alternative) and populate it with the lats, lons, and bathy dimensions / variables from the original dataset. That seems to work except when I go to translate it to ASCII, I end up with a grid full of 9.9692099683868690468e+36 and nothing else.

  • flipped in what way?
    – nmtoken
    Jul 9, 2020 at 10:54

1 Answer 1


Esri ArcGIS can read netCDF files directly. No need to convert to ascii.

  • I'm not reading the ASCII files into ArcGIS
    – r4gt4g
    Oct 14, 2019 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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