0

I want to convert a netCDF4 dataset (including grouped datasets) to Arc/Info ASCII Grids, using Python's gdal package. I tried to orientate at this question, the only thing I added is a loop over the subdatasets. Now, however, something went wrong and the script doesn't produce any output.

Can anyone help? Here's my code:

#Open existing dataset
src_ds_list = glob("..\\*.nc")

for path in src_ds_list:
    src_ds = gdal.Open(path)
    subdata = src_ds.GetSubDatasets()

    for s in range(len(subdata)):
        sds = gdal.Open(subdata[s][0])

        dst_path = '..\\' + subdata[s][0] + 'arbitrary' + '.asc'

        # Open output format driver, see gdal_translate --formats for list
        oformat = "AAIGrid"
        driver = gdal.GetDriverByName(oformat)
        # Output to new format
        dst_ds = driver.CreateCopy(dst_path, sds, 0)
        dst_ds = None
        sds= None
    src_ds = None
countryman
  • 901
  • 2
  • 8
  • 21

1 Answers1

1

A one-liner would be (if you have gdal installed):

gdal_translate -of aaigrid netcdf:in.nc:Band1 out.txt

EDIT For doing in python

Use subprocess as:

import subprocess
subprocess.call('gdal_translate -of aaigrid netcdf:in.nc:Band1 out.txt')

You can then use the resultant file as normal and continue along with the script in python.

shahryar
  • 694
  • 7
  • 21