3

I am currently working on a project where I am using historical cloud coverage from MODIS Cloud Fraction data to try and estimate future cloud coverage of any particular month. I currently have 10-years worth of data for January through to December and have generated global mean, minimum and maximum raster layers (Step 1) for each individual month for that 10-year period. I have detailed the methodology in the flow diagram below to show the steps taken. I currently have 3 layers of global statistics (mean, minimum and maximum) for each month of the 10-year period (Raster 2).

As I am doing this is in Graphic Modeller, is there any way that the final output in Step 4 be written to a CSV file? Conversely, is there a way to script this process (I don't code well) that will make things a bit easier/ more specific if I want to specify output statistics such as minimum, maximum, mean, +1 standard deviation and -1 standard deviation? This is all so that I can display (at least) the minimum, maximum and mean cloud cover from January to December for any given AOI on a graph.

Process Workflow

1

For the first part of your question, you could use a Custom script in your model to fetch and write the raster statistics to a csv. This would replace your Step 4. Raster Layer Statistics.

To do this, go to Processing Toolbox > Scripts > Tools > Create new script and copy the following (note: change the path of the text_file_path):

##Raster statistics=name
##Layer=raster

import csv
from qgis.core import QgsRasterBandStats

text_file_path = "C:/Users/You/Desktop//result.csv"  # Change path to desired location
layer = processing.getObject(Layer)
provider=layer.dataProvider()
ext = layer.extent()
stats = provider.bandStatistics(1,QgsRasterBandStats.All,ext,0)

with open(text_file_path, 'wb') as csvfile:
    fieldnames = ['Statistic', 'Value']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'Statistic': 'Band_Number', 'Value': stats.bandNumber})
    writer.writerow({'Statistic': 'Mean', 'Value': stats.mean})
    writer.writerow({'Statistic': 'Std_Dev', 'Value': stats.stdDev})
    writer.writerow({'Statistic': 'Sum', 'Value': stats.sum})
    writer.writerow({'Statistic': 'Sum_of_Squares', 'Value': stats.sumOfSquares})
    writer.writerow({'Statistic': 'Minimum', 'Value': stats.minimumValue})
    writer.writerow({'Statistic': 'Maximum', 'Value': stats.maximumValue})
    writer.writerow({'Statistic': 'Range', 'Value': stats.range})

Save the script into C:/Users/You/.qgis2/processing/scripts.

Then add it to your model using the output of Step 3. as the input for the script. I ran a simple model and the result looked like this in the .csv file (using Excel for viewing):

csv result


For the second part of your question, I'm not sure how to obtain plus minus standard deviations separately as these are not mentioned in the QgsRasterBandStats class.

| improve this answer | |

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.