iyear = 1981 # Ano inicial clim_year = 30 # Quantidade de anos ntime = clim_year * 12 nmon = ntime/clim_year nyear = ntime/12. tri = ['JFM', 'FMA', 'MAM', 'AMJ', 'MJJ', 'JJA', 'JAS', 'ASO', 'SON', 'OND', 'NDJ', 'DJF'] ini = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'] aux = np.zeros((int(ntime), int(nlat), int(nlon))) ; aux[:] = np.nan for k in range(int(ntime)): aux[k, :, :] = np.sum(var[k:3+k, :, :], axis=0) aux = np.where(np.isnan(aux), -999, aux) # Só um teste print np.min(aux) print np.max(aux) a = aux.reshape(nyear, nmon, nlat, nlon) # 1989: JFM, 1990: JFM... # ESCREVE NETCDF for k in range(int(nmon)): #~ print k fname = output_name + '{0}.nc'.format(tri[k]) cn.create_netcdf(a[:, k, :, :], lat, lon, fileout=fname, ntime=int(clim_year), varname='precip', vartimeunits='years since 1981-01-01 00:00', varunits='mm', fillvalue=-999, varlongname='Precipitation', ncauthor='Funceme', nccomments='Try to recreat UTEXAS', ncsource='UTEXAS', nchistory='Funceme')
my_colors = ('#340003', '#ae000c', '#ff2e1b', '#ff5f26', '#ff9d37', '#fbe78a', '#ffffff', '#b0f0f7', '#93d3f6', '#76bbf3', '#4ba7ef', '#3498ed', '#2372c9') #13 pm.plotmap(anomaly_corrigida[0, :, :], newlats-1./2., newlons-1./2., latsouthpoint=y1, latnorthpoint=y2, lonwestpoint=x1, loneastpoint=x2, fig_name=figname_anom, barloc='right', barcolor=my_colors, barlevs=levs, fig_title=figtitle, barinf='both', ocean_mask=1) background = Image.open(figname_anom) foreground = Image.open("/home/marcelo/FSCT-ECHAM46/FUNCEME_LOGO.png") foreground = foreground.resize((90, 70), Image.ANTIALIAS) background.paste(foreground, (334, 461), foreground) background.save(figname_anom, optimize=True, quality=95) ########## Tercil mais provável para toda a região do globo ########## file_out = "{5}/prob_echam46_issue_{0}{1}_target_{2}{3}_{4}_median.nc".format(fcst_month, fcst_year, target_months, target_year, hind_period, outdir) below, normal, above, f_signal, f_std, o_pad, fcst_sig_anom = cs.compute_probability(fcst, hind, obs, n_years) cn.create_netcdf_probs(below, normal, above, newlats, newlons, fileout=file_out) ########## Curva para o CE ########## # Retorna matriz com os pontos sobre o Ceará shapef = '/home/marcelo/FSCT-ECHAM46/pontos_ce.txt' pointsgrid, lonlatgrid, mymatriz = dg.pointinside(newlats, newlons, shapefile=shapef) # Aplica máscara para os pontos sobre o Ceará points_over_ce_fcst = np.ma.array(fcst[0, :, :], mask=mymatriz) points_over_ce_hind = np.ma.array(hind, mask=np.tile(mymatriz, (hind.shape[0], 1))) points_over_ce_obs = np.ma.array(obs, mask=np.tile(mymatriz, (obs.shape[0], 1))) mean_ce_fcst = np.zeros(1) mean_ce_hind = np.zeros(int(n_years)) mean_ce_obs = np.zeros(int(n_years))
import numpy as np from scipy.io import FortranFile from PyFuncemeClimateTools import CreateNetCDF as cn from decimal import * lons = [ -55.637 + (0.54 * i) for i in range(109)] lons = [ float(Decimal("%.2f" % elem)) for elem in lons] lats = [-21.397, -20.893, -20.387, -19.880, -19.371, -18.861, -18.349, -17.835, -17.320, -16.803, -16.285, -15.766, -15.246, -14.724, -14.200, -13.676, -13.150, -12.624, -12.096, -11.567, -11.037, -10.506, -9.975, -9.442, -8.909, -8.375, -7.840, -7.304, -6.768, -6.231, -5.694, -5.156, -4.617, -4.079, -3.539, -3.000, -2.460, -1.920, -1.380, -0.840, -0.300, 0.241, 0.781, 1.321, 1.861, 2.401, 2.941, 3.480, 4.019, 4.558, 5.097, 5.635, 6.172, 6.709, 7.245, 7.781, 8.316, 8.850, 9.384, 9.916, 10.448, 10.979, 11.509, 12.038, 12.566, 13.093, 13.618, 14.143, 14.666, 15.188, 15.709, 16.229] f = FortranFile('./plev.198811.DAILY.PER11.51', 'r') myvar = np.full((30, 72, 109), np.nan) for i in range(30): var = (f.read_reals(dtype='float32')).reshape(72, 109) myvar[i, ...] = var dummy = (f.read_reals(dtype='float32')) cn.create_netcdf(myvar, lats, lons, ntime=30) #
# Faz a média dos trimestres # for k in range(int(ntime)): # if k in leep_years: # print "Leep year!", k # aux[k, :, :] = np.nanmean(var[k:3+k, :, :], axis=0) * 90 # else: # aux[k, :, :] = np.nanmean(var[k:3+k, :, :], axis=0) * 89 for k in range(int(ntime)): aux[k, :, :] = np.sum(var[k:3+k, :, :], axis=0) print np.min(aux) print np.max(aux) # a = aux.reshape(nmon,nyear,nlat,nlon) # 1989: JFM, FMA, MAM... aux = np.where(aux >= 9.96920996838687**36, -999, aux) a = aux.reshape(nyear, nmon, nlat, nlon) # 1989: JFM, 1990: JFM... # ESCREVE NETCDF for k in range(int(nmon)): print k fname = '/Users/Hulk/obs_pcp/cru/' + output_name[i] + '{0}.nc'.format(tri[k]) cn.create_netcdf(a[:, k, :, :], lat, lon, fileout=fname, ntime=int(clim_year), varname='pre', vartimeunits='years since 1989-01-01 00:00', varunits='mm', fillvalue=-999) # cdo Versão 3 # cdo genbil,pcp-daily-ec4amip51_2015AMJ.ctl.nc cmap.precip.mon.mean.1981-2010.JFM.2.5dg.nc weights.nc # cdo remap,pcp-daily-ec4amip51_2015AMJ.ctl.nc,weights.nc cmap.precip.mon.mean.1981-2010.JFM.2.5dg.nc cmap.precip.mon.mean.1981-2010.JFM.T42.nc
# Arquivo hindcasts hind_file = "pcp-daily-total-ec4amip_%s%s.1.0dg.nc" % (hind_period, target_months) read_hind_file = pupynere.NetCDFFile(hind_file, 'r') hind = read_hind_file.variables['pcp'][:, ::-1, :] read_hind_file.close() # Arquivo observação obs_file = "CMAP.glb.%s-%s-1.0dg.nc" % (hind_period, target_months) read_obs_file = pupynere.NetCDFFile(obs_file, 'r') obs = read_obs_file.variables['pcp'][:, ::-1, :] read_obs_file.close() fcst_file_out = "pcp-daily-total-ec4amip_%s%s.1.0dg.fix.nc" % (fcst_year, target_months) fcst_shift, lons_fcst_shift = shiftgrid(180., fcst, lons, start=False) cn.create_netcdf(fcst_shift, lons_fcst_shift, lats, fileout=fcst_file_out, ntime=1, varlongname='Accum Precip', varunits='mm', vartimeunits='years since 1900-01-01 00:00:00', fillvalue=-999., varname='pcp') hind_file_out = "pcp-daily-total-ec4amip_%s%s.1.0dg.fix.nc" % (hind_period, target_months) hind_shift, lons_hind_shift = shiftgrid(180., hind, lons, start=False) cn.create_netcdf(hind_shift, lons_hind_shift, lats, fileout=hind_file_out, ntime=20, varlongname='Accum Precip', varunits='mm', vartimeunits='years since 1989-05-01 00:00:00', fillvalue=-999., varname='pcp') obs_file_out = "cmap.%s-%s-1.0dg.fix.nc" % (hind_period, target_months) obs_shift, lons_obs_shift = shiftgrid(180., obs, lons, start=False) cn.create_netcdf(obs_shift, lons_obs_shift, lats, fileout=obs_file_out, ntime=20, varlongname='Accum Precip', varunits='mm', vartimeunits='years since 1989-05-01 00:00:00', fillvalue=-999., varname='pcp') print fcst_file_out
for k in range(int(ntime)): aux[k, :, :] = np.sum(var[k : 3 + k, :, :], axis=0) print np.min(aux) print np.max(aux) aux = np.where(aux >= 9.96920996838687 ** 36, -999, aux) a = aux.reshape(nyear, nmon, nlat, nlon) # 1989: JFM, 1990: JFM... # ESCREVE NETCDF for k in range(int(nmon)): print tri[k] fname = "/Users/Hulk/obs_pcp/cru/" + output_name[i] + "{0}.nc".format(tri[k]) cn.create_netcdf( a[:, k, :, :], lat, lon, fileout=fname, ntime=int(clim_year), varname="pre", vartimeunits="years since 1971-01-01 00:00", varunits="mm", fillvalue=-999, varlongname="Preciptation", ) # cdo Versão 3 # cdo genbil,pcp-daily-ec4amip51_2015AMJ.ctl.nc cmap.precip.mon.mean.1981-2010.JFM.2.5dg.nc weights.nc # cdo remap,pcp-daily-ec4amip51_2015AMJ.ctl.nc,weights.nc cmap.precip.mon.mean.1981-2010.JFM.2.5dg.nc cmap.precip.mon.mean.1981-2010.JFM.T42.nc
myfile = "/Users/Hulk/obs_pcp/cmap/cmap.precip.mon.mean.standard.2015.FMA.accum.t42.nc" nc_file = pupynere.NetCDFFile(myfile, 'r') print nc_file.variables var = nc_file.variables['PREC'][:].copy() # Mude aqui print var.shape lon = nc_file.variables['LONGITUDE'][:] # Mude aqui lat = nc_file.variables['LATITUDE'][:] # Mude aqui nc_file.close() var[30, 114] = 460.15 # norte ce var[29, 114] = 302.99 # sul ce fname = '/Users/Hulk/obs_pcp/cmap/cmap.merge.funceme.precip.mon.mean.standard.2015.FMA.accum.t42.nc' cn.create_netcdf(var, lat, lon, fileout=fname, ntime=1, varname='precip', vartimeunits='years since 2015-02-01 00:00', varunits='mm', fillvalue=np.nan) # 1989 - 2008 # obs_funceme_fma_accum_norte = [ 635.98, 391.46, 536.61, 443.23, 260.96, 651.89, 685.73, 763.43, 427.6, 292.96, 487.32, 614.0, 495.22, 470.58, 722.79, 482.24, 350.25, 553.88, 545.35, 653.38 ] # 1989 - 2008 # obs_funceme_fma_accum_sul = [ 646.32, 355.03, 400.28, 422.76, 223.76, 447.75, 518.49, 514.79, 464.84, 245.4, 378.17, 473.52, 331.32, 340.8, 461.63, 449.94, 324.29, 473.72, 494.44, 677.09 ] # for i in range(20): # print var[i, 30, 114], '=>', obs_funceme_fma_accum_norte[i] # var[i, 30, 114] = obs_funceme_fma_accum_norte[i] # for i in range(20): # print var[i, 29, 114], '=>', obs_funceme_fma_accum_sul[i] # var[i, 29, 114] = obs_funceme_fma_accum_sul[i]
elif k in ond: aux[k, :, :] = np.nanmean(var[k:3+k, :, :], axis=0) * 92 elif k in ndj: aux[k, :, :] = np.nanmean(var[k:3+k, :, :], axis=0) * 92 elif k in djf: aux[k, :, :] = np.nanmean(var[k:3+k, :, :], axis=0) * 90 # aux = np.where(aux == np.nan, -999, aux) aux[np.isnan(aux)] = -999. # a = aux.reshape(nmon, nyear, nlat, nlon) # 1989: JFM, FMA, MAM... a = aux.reshape(nyear, nmon, nlat, nlon) # 1989: JFM, 1990: JFM... # ESCREVE NETCDF for k in range(int(nmon)): fname = '/home/marcelo/Downloads/{0}{1}.nc'.format(output_name, tri[k]) print fname cn.create_netcdf(a[:, k, :, :], lat, lon, fileout=fname, ntime=int(clim_year), varname='precip', vartimeunits='years since 1981-01-01 00:00', varunits='mm', fillvalue=-999., varlongname='Accum Mon Precip') # cdo Versão 3 # cdo genbil,pcp-daily-ec4amip51_2015AMJ.ctl.nc cmap.precip.mon.mean.1981-2010.JFM.2.5dg.nc weights.nc # cdo remap,pcp-daily-ec4amip51_2015AMJ.ctl.nc,weights.nc cmap.precip.mon.mean.1981-2010.JFM.2.5dg.nc cmap.precip.mon.mean.1981-2010.JFM.T42.nc
# tercil mais provável 2011-2016 for i, y in enumerate(xrange(2011, 2017)): below, normal, above, f_signal, f_std, o_pad, fcst_sig_anom = \ cs.compute_probability(pcp[30 + i, :, :], pcp[0:30, :, :], obs[0:30, :, :]) if not os.path.exists('figs_expsolar/map_tercis'): os.makedirs('figs_expsolar/map_tercis') file_out = 'figs_expsolar/map_tercis' \ '/prob_echam46_issue_{0}{1}_target_{2}{3}_{4}.nc' \ .format("JAN", y, "FMA", y, "8110") cn.create_netcdf_probs(below, normal, above, nla, nlo, fileout=file_out) figout = 'figs_expsolar/map_tercis' \ '/bra_precip_persistida_{1}_{2}-{3}_{4}_{0}' \ '_echam46_1dg_cmap_tercilmaisprovavel.png' \ .format("JAN", "8110", y, "FMA", y) figtitle = "ECHAM4.6 - {0}/{1} - {2}/{3}\nPROB PREC (%) ({4})" \ .format("JAN", y, "FMA", y, "8110") pm.maptercis(file_out, figtitle, figout) os.remove(file_out) # tercil mais provável 2011-2016 experimento for i, y in enumerate(xrange(2011, 2017)):
# -*- coding: utf-8 -*- from pupynere import NetCDFFile from PyFuncemeClimateTools import CreateNetCDF f = NetCDFFile( "/home/rodrigues/AmbientePython27/lib/python2.7/site-packages/PyFuncemeClimateTools/examples/pcp-daily-total-ec4amip_8908JFM.nc", "r", ) myvar = f.variables["pcp"][:] lons = f.variables["longitude"][:] lats = f.variables["latitude"][:] f.close() CreateNetCDF.create_netcdf(myvar, lons, lats, varname="precip", varunits="mm", ntime=20)
__date__ = "04/12/2015" from PyFuncemeClimateTools import PostRSM97 as PostModels from PyFuncemeClimateTools import CreateNetCDF as cn from netCDF4 import Dataset import numpy as np np.set_printoptions(precision=3) pcp, seaccum, monaccum, seamean, monmean, lat, lon = PostModels.Daily(target_months = range(1, 4), model = 'ECHAM46') print pcp.shape exit() cn.create_netcdf(pcp[0,0,:,:,:], lat, lon, fileout='plev.1981JFM.accum.PER11.t1.nc', varname='pcp', varlongname='Longname Description', varunits='mm', ntime=31, vartimeunits='months since 1900-01-01 00:00', fillvalue=np.nan, ncauthor='', nccomments='', nctitle='', ncsource='', ncdocumentation='', ncreferences='', nchistory='') cn.create_netcdf(pcp[1,0,:,:,:], lat, lon, fileout='plev.1981JFM.accum.PER11.t2.nc', varname='pcp', varlongname='Longname Description', varunits='mm', ntime=31, vartimeunits='months since 1900-01-01 00:00', fillvalue=np.nan, ncauthor='', nccomments='', nctitle='', ncsource='', ncdocumentation='', ncreferences='', nchistory='') cn.create_netcdf(pcp[2,0,:,:,:], lat, lon, fileout='plev.1981JFM.accum.PER11.t3.nc', varname='pcp', varlongname='Longname Description', varunits='mm', ntime=31, vartimeunits='months since 1900-01-01 00:00', fillvalue=np.nan, ncauthor='', nccomments='', nctitle='', ncsource='', ncdocumentation='', ncreferences='', nchistory='') exit() # media dos membros avemembs = np.nanmean(pcp, axis=1) print avemembs.shape # soma os dias sumdays = np.nansum(avemembs, axis=1) print sumdays.shape
elif k in leep_years12: aux[k, :, :] = np.nanmean(var[k:3+k, :, :], axis=0) * 91 elif k in leep_years13: aux[k, :, :] = np.nanmean(var[k:3+k, :, :], axis=0) * 92 elif k in leep_years14: aux[k, :, :] = np.nanmean(var[k:3+k, :, :], axis=0) * 92 elif k in leep_years15: aux[k, :, :] = np.nanmean(var[k:3+k, :, :], axis=0) * 90 else: print "Saindo..." exit() print np.nanmin(aux) print np.nanmax(aux) aux = np.where(np.isnan(aux), -999., aux) # a = aux.reshape(nmon,nyear,nlat,nlon) # 1989: JFM, FMA, MAM... a = aux.reshape(nyear, nmon, nlat, nlon) # 1989: JFM, 1990: JFM... # ESCREVE NETCDF for k in range(int(nmon)): print k fname = "cmap.precip.mon.acc.standard.2.5g.1982-2011.{0}.nc".format(tri[k]) cn.create_netcdf(a[:, k, :, :], lats, lons, fileout=fname, ntime=int(nyear), varname='prec', vartimeunits='years since 1982-01-01 00:00', varunits='mm', fillvalue=-999.)
### Simples conferencia dos dados... ### print "fcst:", fcst.shape, "type:", type(fcst), "min:", np.min(fcst), "max:", np.max(fcst) print "hind:", hind.shape, "type:", type(hind), "min:", np.min(hind), "max:", np.max(hind) if obs_base == "inmet" or obs_base == "chirps" or obs_base == "cru": print "obs:", obs.shape, "type:", type(obs), "min:", np.min(obs), "max:", np.max(obs) elif obs_base == "funceme": print "obs funceme:", mean_ce_obs.shape, "type:", type(mean_ce_obs), "min:", np.min( mean_ce_obs ), "max:", np.max(mean_ce_obs) ### Salvando arquivos interpolados para simples conferencia ### # fcst myfileout = "{4}/pcp-seasonacc-rsm97-hind8110-{0}{1}_{2}{3}.1dg.nc".format( fcst_month, fcst_year, target_year, target_months, outdir ) cn.create_netcdf(fcst, fcst_lats, fcst_lons, fileout=myfileout, varname="pcp", varlongname="Precipitation") # hind myfileout = "{3}/pcp-seasonacc-rsm97-hind8110-{0}-{1}-{2}.1dg.nc".format( fcst_month, hind_period_name2, target_months, outdir ) cn.create_netcdf( hind, fcst_lats, fcst_lons, fileout=myfileout, varname="pcp", varlongname="Precipitation", ntime=30 ) # obs if obs_base == "inmet" or obs_base == "chirps" or obs_base == "cru": myfileout = "{3}/obs_data_{0}_{1}_{2}.1dg.nc".format(obs_base, obs_hind_period, target_months, outdir) cn.create_netcdf( obs, fcst_lats, fcst_lons, fileout=myfileout, varname="pcp", varlongname="Precipitation", ntime=30 )