0
gdal_translate -a_srs EPSG:4326 NETCDF:"file_name.nc":variable_name output_file_name.tif  -a_ullr -9.21200 53.28058 -8.88045 53.11338

I am working on some NetCDF files that include multiple variables. The goal is to transform them into GeoTIFF files and georeference them given known x,y-coordinates and if possible apply this process for all the variables included in the file at once. The abovementioned command has successfully transformed the NetCDF file variable into a GeoTIFF file, but I want to execute this inside a Python script.

gdalinfo : enter image description here

The code I used is here:

transform = gdal.Translate(outname,ds_read,format = 'Gtiff', outputSRS = 'EPSG:4326',outputBounds = [-9.21200, 53.28058, -8.88045, 53.11338] )

The problems I am facing are the following:

When running the code, without specifying a band I get the following Traceback :

Warning 1: Recode from UTF-8 to CP_ACP failed with the error: "Invalid argument".
ERROR 1: Band 1 requested, but only bands 1 to 0 available.

I tried to change the code a bit to test if the variables are interpreted as bands and I added the argument : bandList = 1 inside the gdal_translate fuction. After that, I got the same traceback call as before.

When using the gdal_translate function with the arguments mentioned here : https://gdal.org/python/osgeo.gdal-module.html#Translate , I do not know how to specify a variable (or all variables) of the NetCDF file inside the function. I also tried (unsuccessfully) to parse all the arguments using :

input_format = '-if NETCDF' 
output_format = ' -of GTiff'
projection = '-a_srs EPSG:4326 '
translateOptionText  = projection + input_format + output_format + ' -a_ullr' + str(-9.21200) + '' + str(53.28058) + '' + str(-8.88045) + '' + str(53.11338) 
translateOptions = gdal.TranslateOptions(gdal.ParseCommandLine(translateOptionText))
gdal.Translate(outname,new_name, options = translateOptions)

Any advice or idea?

3
  • Are your nc data gridded? If not, you will need to grid them to convert to GeoTIFF. Can you update your question with the output of gdalinfo filename.nc?
    – Jose
    May 18 at 14:36
  • Yes, I have just posted a capture of the METADATA and SUBDATASETS
    – GeorgeK
    May 18 at 15:00
  • Your file has 8 datasets in it, and it looks to me that only the last 3 are actual data, the first 5 are basically metadata (long/lat, timing). See if this post helps?
    – Jose
    May 18 at 15:25

1 Answer 1

0

Using vrt is one solution for sure. Apparently it worked like that for the votemper layer only:

#old_name = file name with path etc. , var_name = subdataset prefix name
#the prints are just for quick check

read_nc = gdal.Open(old_name,gdal.GA_ReadOnly)
sd = read_nc.GetMetadata('SUBDATASETS')
print(sd)
var_name = 'votemper'
sublayername =  gdal.Open('NETCDF:"{0}":{1}'.format(old_name, var_name ),gdal.GA_ReadOnly)
print('\n The sublayer name is: ',sublayername)
outname = old_name.replace('.nc','') + '_' + var_name + '.tif'
print('\n The output name is: ',outname)
projection = '-a_srs EPSG:4326 '
output_format = ' -of GTiff'
translateOptionText = projection + output_format + ' -a_ullr '  + str(28.60704) + ' ' + str(44.395184) + ' ' + str(28.87371) + ' ' + str(44.00740)
print('\n The command is: ', translateOptionText) 
translateOptions = gdal.TranslateOptions(gdal.ParseCommandLine(translateOptionText))
gdal.Translate(outname,sublayername,options = translateOptions)

Giving the subdataset name as an input in the gdal_translate function, together with the correct format and the options text is producing the correct results.

It was double checked in QGIS and everything was as it should be.

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.