Zonal_Stats_Colab

PHOTO EMBED

Tue Sep 05 2023 20:48:23 GMT+0000 (Coordinated Universal Time)

Saved by @leandrorbc #python

!pip install rasterstats
!pip install rasterio
!pip install leafmap

import glob
import pandas as pd
import rasterio
from rasterio.mask import mask
import geopandas as gps
from rasterstats import zonal_stats

from google.colab import drive
drive.mount('/content/drive')

files = glob.glob(f"/content/drive/MyDrive/Sura/Datos TMax/2011_15/SAMeT_CPTEC_TMAX_2013*.nc")
print(len(files))

grid_str = '/content/drive/MyDrive/Sura/Seccionales_Policiales_WGS84_Selected.shp'
poligonos = gps.read_file(grid_str)

df = pd.DataFrame([], columns=['fecha', 'median', 'CODIGOSECC'])

for file in files:

    with rasterio.open('netcdf:'+ file + ':tmax') as msla_nc:
        msla_nc_data = msla_nc.read(1)

    affine= msla_nc.transform

    print(file)
    for index, row in poligonos.iterrows():
        features = row.geometry

        zs = zonal_stats(features, msla_nc_data,affine=affine,all_touched= False,
                    stats="median")

        df = df.append(pd.DataFrame([[zs[0].get('median'), str(file)[-11:-3],row['CODIGOSECC']]],
                    columns=['median', 'fecha','CODIGOSECC']))

        print(row['CODIGOSECC'])

df.to_csv('/content/drive/MyDrive/Sura/Tmax2013.csv',header=True, index=False)


import glob
import pandas as pd

df = pd.DataFrame([], columns=['fecha', 'median', 'CODIGOSECC'])

files = glob.glob(f"/content/drive/MyDrive/Sura/Datos TMax/Tmax2*.csv")
print(len(files))
for file in files:
  df = df.append(pd.read_csv(file))
#print(df)
df['fecha'] = df['fecha'].astype(str)
df['year'] = df.fecha.str[:4]
df['month'] = df.fecha.str[4:6]
df['day'] = df.fecha.str[-2:]
#display(df.groupby('year').count())

df.to_csv('/content/drive/MyDrive/Sura/TmaxCompleto.csv',header=True, index=False)
content_copyCOPY

https://colab.research.google.com/drive/1YtqoStL70t-qtPqPR-UJa4DAvbsj1Smh#scrollTo=XnrhXTXNKKUO