I have 5 rasters, each having 10 bands, where each band corresponds to a specific date.
How do I calculate mean values for all 10 bands and save them?
I need to save the mean values in a dataframe like this example:
rasters bands mean date years
raster1 band1 1234 24/01/2000 2000
band2 45213 01/03/2000 2000
band3 4221 12/04/2000 2000
band4 ... ... ....
band10
raster 2 band 1 4521 01/02/2001 2001
band2 1234 04/05/2001 2001
... .... ....
This script calculates mean value for each band in my raster:
raster = gdal.Open("D:/script/NDVI2000.tif")
bands = raster.RasterCount
for band in range(1, bands+1):
data = raster.GetRasterBand(band).ReadAsArray().astype('float')
mean = np.mean(data[data != 0]) #calculate mean without value 0
print("Band %s: Mean = %s" % (band, round(mean, 2)))
Out[2]:
Band 1: Mean = 1712.83
Band 2: Mean = 1803.14
Band 3: Mean = 1662.33
Band 4: Mean = 1868.77
Band 5: Mean = 1900.97
Band 6: Mean = 2031.13
Band 7: Mean = 1847.89
Band 8: Mean = 2185.66
Band 9: Mean = 2081.14
Band 10: Mean = 8248.7
How can I save the mean result stats in the dataframe using gdal python 3 after I have the results for all rasters?
I need this dataframe to plot times series for NDVI.
Answer
Something like this, append dicts to an empty list. It seems from your example output, you may want to do this inside a function that you can pass the path of the target raster. Note the ??? means you will have to implement something. Maybe your year comes from the filename?
import pandas as pd
raster_filename = "D:/script/NDVI2000.tif"
raster = gdal.Open(raster_filename)
bands = raster.RasterCount
row_list = []
for band in range(1, bands+1):
data = raster.GetRasterBand(band).ReadAsArray().astype('float')
mean = np.mean(data[data != 0]) #calculate mean without value 0
row_list.append({
'raster': os.path.basename(raster_filename),
'band': band,
'mean': mean,
'year': ???,
'date': ???,
})
print("Band %s: Mean = %s" % (band, round(mean, 2)))
# create DataFrame with columns from the dict keys
df = pd.DataFrame(row_list)
df.to_csv('band_means.csv')
Good luck!
No comments:
Post a Comment