I have many nc files and for each one I would like to create a tif file with the average of the bands using r.series of grass processing in QGIS. I wrote the code below but it doesn't work but no tif file is created and it gives me no error messages.

Using the processing tool, with one file it works.

import glob, os
for file in glob.glob("*.nc"):
        input_layer = (path+file)
        output_layer = input_layer.replace(".nc",".tif")  
    param = { '-n' : False, 
                'GRASS_RASTER_FORMAT_META' : '',
                'GRASS_RASTER_FORMAT_OPT' : '',
            'GRASS_REGION_PARAMETER' : None,
            'input' : input_layer,
            'method' : [0],
            'output' : output_layer,
            'quantile' : '',
            #'range' : [nan,nan],
            'weights' : '' 

    processing.runAndLoadResults('grass7:r.series', param)
    ###########r.series average of bands of file nc

    ####### Reload your output layer ########

    #output_layer = QgsRasterLayer(path, file_tif,'ogr')

1 Answer 1


Code below is working when I try it (with tif files as inputs). I ran the tool manually in QGIS then used the command from History (Ctrl+Alt+H) and changed your code from input_layer = (path+file) to input_layer = os.path.join(path, file)

import glob, os

for file in glob.glob("*.nc"):
    input_layer = os.path.join(path,file)
    output_layer = input_layer.replace(".nc","_output.tif")
    params = {'input':input_layer,'-n':False,
    processing.runAndLoadResults("grass7:r.series", params)
  • for every nc file "2020-12-29T14:45:17 CRITICAL ERRORE: Mappa raster non trovata 2020-12-29T14:45:17 INFO C:\Users\Daniele\Documents\nc>r.out.gdal -t -m input="outputa58c73310aa14886adb6bc8316d7bec5" output="C:\Users\Daniele\Documents\nc\2020-11-01_2020-11-30_output.tif" format="GTiff" createopt="TFW=YES,COMPRESS=LZW" --overwrite" Dec 29, 2020 at 13:46
  • 1
    @ BRERA Now i understand, nc files need to be uploaded to qgis before running python code. Thank you Dec 29, 2020 at 13:53
  • By upload you mean add them to QGIS as layers?
    – BERA
    Dec 29, 2020 at 17:30
  • 1
    yes, I noticed that the same thing happens when using the processing tools Dec 30, 2020 at 8:24

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.