0% found this document useful (0 votes)
21 views2 pages

Python

This document contains code to import necessary libraries, read in netcdf and shapefile data, calculate a weighted mask for a watershed shapefile over a grid, plot precipitation data masked over the watershed, calculate the average precipitation over the watershed for each time step, plot the time series of average precipitation, and save the results to a dataframe.

Uploaded by

elmer
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
21 views2 pages

Python

This document contains code to import necessary libraries, read in netcdf and shapefile data, calculate a weighted mask for a watershed shapefile over a grid, plot precipitation data masked over the watershed, calculate the average precipitation over the watershed for each time step, plot the time series of average precipitation, and save the results to a dataframe.

Uploaded by

elmer
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 2

# -*- coding: utf-8 -*-

"""
Spyder Editor

This is a temporary script file.


"""

# Importar librerias
import fiona, numpy as np
import pylab as pl
from netCDF4 import Dataset
from pyscissor import scissor # pip install pyscissor
from cartopy.feature import ShapelyFeature
import cartopy.feature as cfeature
import pandas as pd
import cartopy.crs as ccrs

ncf = Dataset("D:/NESTOR/PPDIARIA/RAIN4PE_daily_0.1d_1981_2015_v1.0.nc")
cuenca = fiona.open("D:/NESTOR/PPDIARIA/cuenca_utm.shp")
print(cuenca.crs)
#cuenca_s18 = cuenca.to_crs(epsg=32718)
print(ncf)
# Ajustes generales para los plots..
from matplotlib.font_manager import *
from matplotlib.pylab import *

ioff()

rc(
'lines',
linewidth=1)

rc(
'text',
usetex=False)# Para poder usar 'usetex=True' es necesario haber instalado latex

rc(
'font',
family='Times New Roman')

font_title = FontProperties(size=24)
font_label = FontProperties(size=22)
font_ticks = FontProperties(size=16)
font_legend = FontProperties(size=15)
print(ncf.variables['time'])
lats =ncf.variables['Latitude'][:]
lons =ncf.variables['Longitude'][:]
prec =ncf.variables['pcp'][:]
times =ncf.variables['time'][:]
# calcular.. mascara para .shp
from shapely.geometry import shape
record = next(iter(cuenca))
shapely_shape = shape(record['geometry'])
ncf.variables
pys = scissor(shapely_shape,lats,lons)
wg = pys.get_masked_weight()
# Cuadricula ponderada
cart_ft =
ShapelyFeature([shapely_shape],ccrs.PlateCarree(),facecolor='none',edgecolor='cyan'
,linewidth=2)

# plot
fig =pl.figure(figsize=(5,5))
ax=pl.axes(projection=ccrs.PlateCarree())
pl.pcolormesh(lons,lats,wg,cmap='ocean',shading='nearest')
pl.colorbar()
ax.set_title("$Cuenca\ Cachi$", fontproperties=font_title)
ax.add_feature(cart_ft)
ax.set_xlim(-74.8, -73.8)
ax.set_ylim(-13.6,-12.8)
pl.show()
# Asignar mascara
prec.mask=wg.mask

# Plot de precipitación enmascarada del primer paso de tiempo


fig =pl.figure(figsize=(5,5))
ax=pl.axes(projection=ccrs.PlateCarree())
pl.pcolormesh(lons,lats,prec[-1],cmap='jet')
pl.colorbar()
ax.add_feature(cart_ft)
ax.set_xlim(-74.8, -73.8)
ax.set_ylim(-13.6,-12.8)
ax.annotate('Calculemos la prec. promedio...\nareal de cuenca!!',
xy=(-74.5,-13.1),#xy=(-74.4,-13.45)
xytext=(-75.5,-12.9),#xytext=(-74.3,-13.35)
arrowprops={'facecolor':'#23bf97','shrink':0.01},fontsize=9.5)
pl.show()
# Serie de precipitación promedio areal de cuenca..
prec_prom = np.zeros(times.shape[0])

for t in range(times.shape[0]):
prec_prom[t] = np.average(prec[t],weights=wg)

# plot
pl.figure(figsize=(12,5))
pl.plot(times,prec_prom,'o-',label='Precipitación promedio areal de cuenca', color=
'blue')
pl.legend(loc=2, prop=font_legend)
pl.xlabel("$Date\ (meses)$")
pl.grid('on', color='.8',linestyle='--')
pl.show()
# Guardas datos!!!
df= pd.DataFrame(prec_prom)
df
#df.to_csv("D:/T_JONA/TESIS_PISCO/Entrada/Pisco_Pp/Python_PISCO/Prec_areal.csv")

You might also like