2

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.

  • 1
    Where does the date data come from in your output example? – vpipkt Jan 16 at 19:09
  • 1
    How are you acquiring the date and year information? How do you hope to incorporate time into the dataframe? – Aaron Jan 16 at 19:50
  • 1
    @Aaron before doing rasterstack in ENVI i hade the bands with date ( one bands for one date ) exemple : MOD13Q1.006__250m_16_days_EVI_doy2000209_aid0001 first band for julianday 209 = 01/07/2000 so i'm doing srack for all bands in the same years with ordre just in ENVI 5.2 cause i don't know how doing stack in gdal Python 3 and how to export the date from name of band to dataframe – Majda El yacouby Jan 16 at 20:09
  • @Aaron i know the date for all bands in all rasters so just need to add date in dataframe i think ! – Majda El yacouby Jan 16 at 21:41
2

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!

  • thank u for answer i will test it and undersant and if i want to do it for 5 rasters ( 10 bans in the rasters ) and all the raster in the same folder NDVI2000, NDVI2001 , NDVI2003 ,... – Majda El yacouby Jan 16 at 20:02
  • I'd recommend Python's os.scandir() to loop over the files – vpipkt Jan 16 at 20:21
  • i will try it sincerly cause i need to undersan thank u – Majda El yacouby Jan 16 at 20:32
  • date': ??? i don't know how can writhe date:01/02/2000 – Majda El yacouby Jan 16 at 20:54
  • date : 01-02-2000 ^ SyntaxError: invalid token – Majda El yacouby Jan 16 at 20:57

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.