2

How do I translate (extract) multiband netCDF to individual raster geotiff?

I am new to scripting, so I am hoping you could help me with how I should write my gdal translate script starting from setting my working directory.


Instructions that come with the data:

The maps can be transformed to the GTiff format using GDAL as follows, replacing Year, Band_Name and Output_Filename with the parameters of interest:

gdalwarp -of Gtiff -co COMPRESS=LZW -co TILED=YES -ot Byte -te -180.0000000 -90.0000000 180.0000000 90.0000000 -tr 0.002777777777778 0.002777777777778 -t_srs EPSG:4326 NETCDF:xxxYEAR.nc:BandName OutputFilename.tif

So far:

import os, sys # the standard imports

BaseFolder = r'C:\Users\Dlan\Desktop\netCDF_TIFF' # change this to match your data

for FullPath, dirs, files in os.walk(BaseFolder):
for ThisFile in files:                   # iterate the files
    fN,fE = os.path.splitext(ThisFile)   # separate file name and extension
    if fE.lower() == 'LCCS-2018.nc':
        # this is a netCDF file, though other extensions might exist
        # you will need to provide these details or ascertain them from
        # the input NetCDF file name.

        Year = '2018'
        Band = 'lccs_class'
        OutputName = 'LCCS-2018'


        GDAL_Command = 'gdalwarp -of Gtiff -co COMPRESS=LZW -co TILED=YES -ot Byte -te '\
                                     '-180.0000000 -90.0000000 180.0000000 90.0000000 -tr 0.002777777777778 0.002777777777778 '\
                                     '-t_srs EPSG:4326 NETCDF:{}YEAR.nc:{} {}.tif'.format( Year, Band, os.path.join(FullPath,OutputName))
        os.system(GDAL_Command) # run the command

There are some other instructions:

The maps can be transformed to the GTiff format using GDAL as follows, replacing Year, Band_Name and Output_Filename with the parameters of interest:

with Year the year : 2016, 2017 or 2018; and BandName one of the following: lccs_class; processed_flag; current_pixel_state; observation_count; change_count.

Format: gdalwarp -of Gtiff -co COMPRESS=LZW -co TILED=YES -ot Byte -te -180.0000000 -90.0000000 180.0000000 90.0000000 -tr 0.002777777777778 0.002777777777778 -t_srs EPSG:4326 NETCDF:XXX-YEAR.nc:BandName OutputFilename.tif

Example code: gdalwarp -of Gtiff -co COMPRESS=LZW -co TILED=YES -ot Byte -te -180.0000000 -90.0000000 180.0000000 90.0000000 -tr 0.002777777777778 0.002777777777778 -t_srs EPSG:4326 NETCDF:LCCS-2018.nc:lccs_class LCCS-2018.tif

8
  • 1
    You are talking about two different platforms: ArcGIS and GDAL, which one is it? If you're trying to iterate with arcpy.da.walk and then launch GDAL_Translate you will have to ensure that GDAL_Translate executes from a CMD window (anything other than 'gdal_translate' is not recognized as an internal or external command should be enough). Do you have any code to work from? We would need this to cater an answer to your level of experience, if you have code, no matter how bad or sloppy it is, please edit it into your question. Commented Feb 17, 2020 at 1:29
  • Hi Michael, Thanks so much for your reply. Actually, I don't have any script to start from. What I have is the instructions that come with the data, as edited above. thank you
    – Dlan
    Commented Feb 17, 2020 at 1:34
  • Oh, I see. When you open a CMD window and type in gdalwarp is it recognized by the CMD processor? With no options specified it should print a 'usage' message. If not you will have to open a GDAL shell window, OSGeo4W should work, from here you can drag and drop your python file with arcpy commands and theoretically it should work, I can help with a basic framework if you can ensure the gdalwarp command is recognized. Commented Feb 17, 2020 at 1:45
  • It says --- 'gdalwarp' is not recognized as an internal or external command, operable program or batch file.--- I'm sorry, I don't really know what to do. Could you please explain a bit more about OSGeo4W? thank you
    – Dlan
    Commented Feb 17, 2020 at 1:55
  • 1
    PolyGeo, thanks so much. My apologies for any inconvenience as I am new to this support system and to the topic I am working on. ty
    – Dlan
    Commented Feb 17, 2020 at 3:40

1 Answer 1

2

It can be achieved with a simple walk or listdir:

import os, sys # the standard imports

BaseFolder = r'c:\your\folder\with\data' # change this to match your data

for FullPath, dirs, files in os.walk(BaseFolder):
    for ThisFile in files:                   # iterate the files
        fN,fE = os.path.splitext(ThisFile)   # separate file name and extension
        if fE.lower() == '.nc':
            # this is a netCDF file, though other extensions might exist
            # you will need to provide these details or ascertain them from
            # the input NetCDF file name.
            Year = 'XXXX'
            Band = 'bandName'
            OutputName = 'OutputName.tif'

            GDAL_Command = 'gdalwarp -of Gtiff -co COMPRESS=LZW -co TILED=YES -ot Byte -te '\
                                         '-180.0000000 -90.0000000 180.0000000 90.0000000 -tr 0.002777777777778 0.002777777777778 '\
                                         '-t_srs EPSG:4326 NETCDF:{}YEAR.nc:{} {}.tif'.format( Year, Band, os.path.join(FullPath,OutputName))
            os.system(GDAL_Command) # run the command 

Hopefully you can derive your output name from your input, I don't have enough information to decide that but this will help loop through the folders and run the command when you have decided what the command should contain. Note the '\ on the end of each line is a continuation character to split the sting onto multiple lines for readability.

Before you can start with the coding portion of this you need to have your warp command recognized. If you don't have OSGeo4W shell there is a shell batch in your GDAL install folder or you could navigate to your GDAL\bin folder with a command like CD /D "C:\Program Files\GDAL" where the gdalwarp executable resides.

10
  • I have installed OSGeo4W. Did you mean to say that I shall copy the script above to the OSGeo4W shell and change the necessary strings? thank you
    – Dlan
    Commented Feb 17, 2020 at 2:48
  • No, the OSGeo shell simply has the right paths to call (find) GDALWarp. You need to copy the text into a notepad (notepad++ or pywin is better) document then save as file type .py (make sure it isn't .py.txt as Windows will do if you have 'hide extensions for known types' turned on) then drag from Windows explorer (or dOpus) into the command window and press enter to execute the python code. Ideally part of the output file name should be a substring of the input file name stackoverflow.com/questions/663171/… Commented Feb 17, 2020 at 3:13
  • C:\>"C:\Users\Dlan\Desktop\netCDF_TIFF\test1.py" File "C:\Users\Dlan\Desktop\netCDF_TIFF\test1.py", line 2 >>> import os, sys # the standard imports ^ SyntaxError: invalid syntax
    – Dlan
    Commented Feb 17, 2020 at 3:32
  • That is very odd, can you post the code you have so far in your question. I think it might have something to do with the imports being on line 2 rather than the first line.. python can be fussy about the first line and what it should contain. Commented Feb 17, 2020 at 3:41
  • How can I post the script? ty
    – Dlan
    Commented Feb 17, 2020 at 3: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.