I am using python to convert multiple tif files to netCDF. The code is the following:

def tif_to_nc(input_name):
   # Convert TIF to netCDF
   inputfile = project+"process/{}.tif".format(input_name)
   outputfile = project+"output/{}.nc".format(input_name)
   ds = gdal.Translate(outputfile, inputfile, format='NetCDF')

It works for most files, but it fails for a particular tiff image which cointains just a few attributes, returning just NaN.

The following warning is also shown (for all files), which I think might be related:

Warning 1: Recode from UTF-8 to CP_ACP failed with the error: "Invalid argument".

Another way to convert from tiff to gdal that might fix this issue is also welcome.

The file I am trying to convert can be found at http://filedropper.com/u4FRM9Ea

  • Impossible to answer without test data. If your tiff files are not public create a test file that does not contain confidential contents but is otherwise similar.
    – user30184
    Aug 31 at 8:50
  • Thanks, I have added a path to download the test file. When I run the code above with it, I get only NaN in my output nc file Aug 31 at 9:07
  • Works for me from the command line (GDAL 3.6dev) gdal_translate -f NetCDF example_file.tif example_file.nc Input file size is 90, 85 0...10...20...30...40...50...60...70...80...90...100 - done. All the values in the NC file are 10 just like in the source tif. I do not quite understand what you mean by "attributes" in this context. Is that something specific to NetCDF?
    – user30184
    Aug 31 at 9:35
  • By attibutes I meant the values assigned as 10 in this example file. After you mentioned it was working, I was able to trace back the error to how I was generating the tif, before converting. It was not closing the file. After adding a line with file = None it worked. Thanks! Sep 1 at 2:42

1 Answer 1


gdal_translate (or gdal.Translate() in python) was not working because the tiff file was still "open" after I created and edited it with gdal. It works after adding file_tiff = None before converting it.

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.