print ' ... OBS ...', obs.shape, '\n' outdir = '{0}{1}-{2}'.format(fcst_month, fcst_year, hind_period) if not os.path.exists(outdir): os.makedirs(outdir) print " === OUTPUT DIR: {0} ===\n".format(outdir) # Limites do mapa para fazer o plot # y1, y2, x1, x2 = -40., 40., -150., 70. # Globo #~ y1, y2, x1, x2 = -89., 89., -178., 178. # Globo #~ y1, y2, x1, x2 = -23., 9., -75., -34. # Região Reliability y1, y2, x1, x2 = -60., 15., -90., -30. # América do sul ########### CORRELAÇÃO ########### correl = cs.compute_pearson(hind, obs, n_years) figtitle = u'ECHAM4.6 x CMAP - {0}/{1} ({2})\nCorrelação - Precip Acum'.format(fcst_month_name, target_months, hind_period_name) #~ figtitle = u'ECHAM4.6 x CMAP - {0}/{1} ({2})\nCorrelation - Precip Accum'.format(fcst_month_name, target_months, hind_period_name) figname = "{3}/bra_precip_persistida_{0}_null-{1}_null_{2}_echam46_1dg_cmap_correlacao.png".format(hind_period_name, target_months, n_fcst_month, outdir) levs = (-1.0, -0.9, -0.7, -0.5, -0.3, 0.3, 0.5, 0.7, 0.9, 1.0) #10 my_colors = ('#2372c9', '#3498ed', '#4ba7ef', '#76bbf3', '#93d3f6', '#b0f0f7', '#ffffff', '#fbe78a', '#ff9d37', '#ff5f26', '#ff2e1b', '#ff0219', '#ae000c') #13 pm.plotmap(correl, newlats-1./2., newlons-1./2., latsouthpoint=y1, latnorthpoint=y2, lonwestpoint=x1, loneastpoint=x2, fig_name=figname, barloc='right', barcolor=my_colors, barlevs=levs, fig_title=figtitle, barinf='neither', ocean_mask=1) background = Image.open(figname) foreground = Image.open("FUNCEME_LOGO.png") foreground = foreground.resize((90, 70), Image.ANTIALIAS) background.paste(foreground, (334, 461), foreground)
print 'lons:', obs_lons outdir = '{0}{1}-{2}-rsm97'.format(fcst_month, fcst_year, hind_period) if not os.path.exists(outdir): os.makedirs(outdir) print " === OUTPUT DIR: {0} ===\n".format(outdir) # Limites do mapa para fazer o plot # y1, y2, x1, x2 = -40., 40., -150., 70. # Globo #~ y1, y2, x1, x2 = -89., 89., -178., 178. # Globo #~ y1, y2, x1, x2 = -23., 9., -75., -34. # Região Reliability y1, y2, x1, x2 = -21.3, 7., -55.6, -34 # América do sul ########### CORRELAÇÃO ########### correl = cs.compute_pearson(hind, obs, n_years) figtitle = u'RSM97 x UTEXAS - {0}/{1} ({2})\nCorrelação - Precip Acum'.format(fcst_month_name, target_months, hind_period_name) figname = "{3}/bra_precip_persistida_{0}_null-{1}_null_{2}_rsm97_1dg_utexas_correlacao.png".format(hind_period_name, target_months, n_fcst_month, outdir) levs = (-1.0, -0.9, -0.7, -0.5, -0.3, 0.3, 0.5, 0.7, 0.9, 1.0) #10 my_colors = ('#2372c9', '#3498ed', '#4ba7ef', '#76bbf3', '#93d3f6', '#b0f0f7', '#ffffff', '#fbe78a', '#ff9d37', '#ff5f26', '#ff2e1b', '#ff0219', '#ae000c') #13 pm.plotmap(correl, obs_lats-0.54/2., obs_lons-0.54/2., latsouthpoint=y1, latnorthpoint=y2, lonwestpoint=x1, loneastpoint=x2, fig_name=figname, barloc='right', barcolor=my_colors, barlevs=levs, fig_title=figtitle, barinf='neither', ocean_mask=1) background = Image.open(figname) foreground = Image.open("/home/marcelo/FSCT-ECHAM46/FUNCEME_LOGO.png") foreground = foreground.resize((90, 70), Image.ANTIALIAS) background.paste(foreground, (313, 460), foreground) background.save(figname, optimize=True, quality=95)
# '_echam46_1dg_null_precip.png'.format(hind_period_name, # target_year, target_months, fcst_year, n_fcst_month, outdir) # pm.plotmap(fcst[0, :, :], newlats, newlons, latsouthpoint=-88., # latnorthpoint=88., lonwestpoint=-178., loneastpoint=178., # fig_name=figname, fig_title=figtitle, barcolor=my_colors, # ocean_mask=0, barlevs=lev, barinf='max', barloc='bottom', # meridians=np.arange(-160., 161., 30.), # parallels=np.arange(-90., 91., 20.)) if obs_base == 'cmap': ### Anomalia corrigida metódo do Caio (regressão linear) ### # below, normal, above, f_signal, f_std, o_pad, fcst_sig_anom = \ # cs.compute_probability(fcst, hind, obs, n_years) below, normal, above, f_signal, f_std, o_pad, fcst_sig_anom = \ cs.compute_probability(fcst, hind, obs) hind_corrigido = cs.compute_setrighthind(hind, obs) anomaly_corrigida, anomaly_pad = \ cs.compute_anomaly(fcst_sig_anom, hind_corrigido) figtitle = u'ECHAM4.6 x CMAP - {0}/{3} - {1}/{4}\nAnomalia (mm)' \ ' - Precip Acum ({2})'.format(fcst_month_name, target_months, hind_period_name, fcst_year, target_year) figname_anom = '{5}/bra_precip_persistida_{0}_{1}-{2}_{3}_{4}_' \ 'echam46_1dg_null_anom.png'.format(hind_period_name, target_year, target_months, fcst_year, n_fcst_month, outdir)
# Observado pcp_obs_ce = obs_file['pcp'][:, 29:31, 114] obs_ce = np.asarray(pcp_obs_ce) # Média sobre o CE fsct_aux_ce = np.apply_over_axes(np.mean, fsct_ce, [1, 2]) hind_aux_ce = np.apply_over_axes(np.mean, hind_ce, [1, 2]) obs_aux_ce = np.apply_over_axes(np.mean, obs_ce, [1, 2]) outdir = "%s%s_HIND%s" % (fcst_month.upper(), fcst_year, hind_period) if not os.path.exists(outdir): os.makedirs(outdir) # Curva de probabilidade para o CE figname = outdir + '/' + 'prob_echam46_issue_%s%s_target_%s%s_%s_curve_ce.png' % (fcst_month, fcst_year, target_months, target_year, hind_period) below_ce, normal_ce, above_ce, f_signal_ce, f_std_ce, o_pad_ce = cs.compute_probability(fsct_aux_ce[:, 0, 0], hind_aux_ce[:, 0, 0], obs_aux_ce[:, 0, 0]) pm.plotnorm(f_signal_ce, f_std_ce, o_pad_ce, fig_name=figname, fig_title='') ### Fim Bloco Curva para o CE ### # Tercil mais provável para toda a região do globo file_out = outdir + '/' + "prob_echam46_issue_%s%s_target_%s%s_%s.nc" % (fcst_month, fcst_year, target_months, target_year, hind_period) below, normal, above, f_signal, f_std, o_pad = cs.compute_probability(fsct, hind, obs) cn.create_netcdf_probs(below, normal, above, lats, lons, fileout=file_out) # ferret_command = "/home/marcelo/pyferret-0.0.9/bin/pyferret.sh -nojnl -gif -script plot_tercis_ensemble.jnl {0}".format(file_out) # os.system(ferret_command) # Correlacao correl = cs.corr_pearson(hind, obs)
lats = obs_data.variables['lat'][:] obs_data.close() pcp_obs = np.mean(pcp_obs, axis=0) # pcp_obs_aux, lons_obs_aux = shiftgrid(180., pcp_obs, lons, start=False) obs_path = "/Users/Hulk/clim-verification/CMAP/CMAP89-08-FMA-T42.nc" clim_obs_path = "/Users/Hulk/obs_pcp/cmap/cmap.merge.funceme.precip.standard.FMA.season.accum.1dg.nc" clim_obs_data = pupynere.NetCDFFile(clim_obs_path, 'r') print clim_obs_data.variables clim_pcp_obs = clim_obs_data.variables['precip'][:] print clim_pcp_obs.shape clim_obs_data.close() # clim_pcp_obs_aux, lons_clim_aux = shiftgrid(180., clim_pcp_obs, lons, start=False) mytercis, tercilinf, tercilupp = cs.tercil_verification(clim_pcp_obs, pcp_obs) print mytercis.shape lons -= 1./2. lats -= 1./2. # Plot precip acumulada lev = [0., 50., 100., 200., 300., 500., 700., 900., 1000.] my_colors = ('#CC3333', '#FF6633', '#FF9933', '#FFFF99', '#FFFFCC', '#CCFFCC', '#99FFCC', '#66FF66', '#00CC00', '#009900', '#003300') #11 pm.map_shaded(pcp_obs, lats, lons, meridians=np.arange(-160., 161., 10.), fig_name="cmap.merge.funceme.fma.2015.accum.1dg.png", barlevs=lev, barcolor=my_colors, latsouthpoint=-60., latnorthpoint=15., fig_title="CMAP/FUNCEME - FMA 2015 - ACUM", lonwestpoint=-90., loneastpoint=-30., barinf="max", ocean_mask=1)
# clim_obs_path = "/Users/Hulk/FSCT-ECHAM46/obs_data/cmap/CMAP.glb.89-08-FMA-1.0dg.nc" clim_obs_path = "/Users/Hulk/obs_pcp/cmap.precip.mean.enhanced.1989-2008.FMA.nc3.nc" clim_obs_data = pupynere.NetCDFFile(clim_obs_path, 'r') # clim_pcp_obs = clim_obs_data.variables['pcp'][:] clim_pcp_obs = clim_obs_data.variables['PRECIP'][:] clim_obs_data.close() # exit() # pcp_obs = np.mean(pcp_obs, axis=0) # pcp_obs = pcp_obs * 89. # Fev 28, Mar 31, Abr 30 pcp_obs = pcp_obs * 89 clim_pcp_obs = clim_pcp_obs * 89 mytercis = cs.tercil_verification(clim_pcp_obs, pcp_obs) # Plot categorias lev = [-1, 0., 1., 2.] my_colors = ('#F75026', '#57F255', '#4143F2') pm.map_shaded(mytercis, lats-1./2., lons-1./2., meridians=np.arange(-160., 161., 10.), fig_name="cmap.fma.2015.cat.png", barlevs=lev, barcolor=my_colors, latsouthpoint=-60., latnorthpoint=15., fig_title="CMAP - FMA 2015 - CAT", lonwestpoint=-90., loneastpoint=-30., ocean_mask=1) # Plot precip acumulada lev = [0., 50., 100., 200., 300., 500., 700., 900., 1000.] my_colors = ('#CC3333', '#FF6633', '#FF9933', '#FFFF99', '#FFFFCC', '#CCFFCC', '#99FFCC', '#66FF66', '#00CC00', '#009900', '#003300') #11 pm.map_shaded(pcp_obs, lats-1./2., lons-1./2., meridians=np.arange(-160., 161., 10.),
obs = obs_file.variables['pcp'][:, :, :] # obs_ce = obs_file.variables['pcp'][:, 53:58, 49:53] # obs_mean_ce = np.apply_over_axes(np.mean, obs_ce, [1, 2]) # obs_ce = obs_file.variables['pcp'][:, 52:54, 51:53] # obs_mean_ce = np.apply_over_axes(np.mean, obs_ce, [1, 2]) obs_ce = obs_file.variables['pcp'][:, 52:53, 53:54] obs_mean_ce = np.apply_over_axes(np.mean, obs_ce, [1, 2]) # Longitude e latitude lons = fsct_file.variables['lon'][:] lats = fsct_file.variables['lat'][:] # Curva de probabilidade below_ce, normal_ce, above_ce, f_signal_ce, f_std_ce, o_pad_ce = cm.compute_probability(fsct[:, 0, 0], hind[:, 0, 0], obs[:, 0, 0]) pm.plotnorm(f_signal_ce, f_std_ce, o_pad_ce, fig_name='prob.echam46+nmme.curve.ce.png', fig_title='') # below, normal, above, f_signal, f_std, o_pad = cm.compute_probability(fsct, hind, obs) # cn.create_netcdf_probs(below, normal, above, lats, lons, fileout="prob_nmme+echam46_issue_jan2015_target_fma2015.nc") # Anomalia # anomaly, anomaly_pad = cm.compute_anomaly(fsct, hind) # Shift na longitude e nos dados do modelo # Isso é necessário pois a longitude do NetCDF é no formato 0 - 360 # a função aceita valores de longitude no formato -180 - +180 # Chefinho quer ver todos os oceanos # anomaly, fsct_lon = shiftgrid(30., anomaly, fsct_lon, start=False) # anomaly, lons_anom_aux = shiftgrid(180., anomaly, lons, start=False)
def compute_reliability(model, obs, lentime): ''' Salva um arquivo com a verificação dos tercis mais prováveis comparando o modelado com o observado Os arrays model e obs tem que ter o mesmo shape :param model: numpy.Array do dado modelado :param obs: numpy.Array do dado observado :param path_saida: Caminho/Nome do arquivo txt de saida. :return: arquivo de verificação ''' try: if type(model).__module__ != np.__name__: raise ValueError('A variável "model" não é do tipo numpy array!!!') elif type(obs).__module__ != np.__name__: raise ValueError('A variável "obs" não é do tipo numpy array!!!') elif model.shape != obs.shape: raise Exception('Os arrays não possuem o mesmo shape!!!') except ValueError: print '===> Atenção na entrada dos np.arrays:' raise except Exception: print '===> Cheque o shape dos arrays' raise pcp_masked = np.ma.array(obs) # transforma num masked array para retirar a mascara mask_obs = pcp_masked.mask if mask_obs: pcp_nan = np.where(mask_obs, np.NaN,obs) else: pcp_nan = obs xlow = 1./3.*100 xup = 2./3.*100 pcp_low = np.percentile(pcp_nan, xlow, axis=0) pcp_up = np.percentile(pcp_nan, xup, axis=0) #~ path_below = ''.join((path_saida.split(".")[0], "_below.", path_saida.split(".")[-1])) #~ path_normal = ''.join((path_saida.split(".")[0], "_normal.", path_saida.split(".")[-1])) #~ path_above = ''.join((path_saida.split(".")[0], "_above.", path_saida.split(".")[-1])) path_below = "reliability_below.txt" path_normal = "reliability_normal.txt" path_above = "reliability_above.txt" f_verifc_below = open(path_below, 'w') f_verifc_normal = open(path_normal, 'w') f_verifc_above = open(path_above, 'w') for i in range(0, obs.shape[0]): p_below, p_normal, p_above, fcst_signal, fcst_std, obs_pad, dummy =\ cs.compute_probability(model[i], model, pcp_nan, lentime) p_below, p_normal, p_above = p_below/100., p_normal/100., p_above/100. obs_below = (obs[i, :, :] < pcp_low) obs_normal = ((obs[i, :, :] >= pcp_low) & (obs[i, :, :] <= pcp_up)) obs_above = (obs[i, :, :] > pcp_up) for j in range(0,obs.shape[1]): for k in range(0,obs.shape[2]): if obs_below[j,k] == True: f_verifc_below.write("{0} {1} \n".format(p_below[j,k], 1)) else: f_verifc_below.write("{0} {1} \n".format(p_below[j,k], 0)) if obs_normal[j,k] == True: f_verifc_normal.write("{0} {1} \n".format(p_normal[j,k], 1)) else: f_verifc_normal.write("{0} {1} \n".format(p_normal[j,k], 0)) if obs_above[j,k] == True: f_verifc_above.write("{0} {1} \n".format(p_above[j,k], 1)) else: f_verifc_above.write("{0} {1} \n".format(p_above[j,k], 0))
above = np.loadtxt("echam46_above.txt") normal = np.loadtxt("echam46_normal.txt") below = np.loadtxt("echam46_below.txt") print "\n => Plot diagram...\n" mytitle = "Diagrama de atributos: {2}/{1} - ({0})".format(hind_period, target_months, fcst_month) figura, ax = plot_reliability(below[:,0], below[:,1], maintitle=mytitle, cor='blue') figura, ax = plot_reliability(normal[:,0], normal[:,1], maintitle=mytitle, first=False, fig=figura, cor='y') figura, ax = plot_reliability(above[:,0], above[:,1], maintitle=mytitle, first=False, fig=figura, cor='red') name_fig = "bra_precip_persistida_{0}_null-{1}_null_{2}_echam46_1dg_cmap_reliability.png".format(hind_period, target_months, n_fcst_month) plt.savefig(name_fig) plt.close() correl = cs.compute_pearson(pcp_model, pcp_obs, timelen=14.) # correl_aux, lons_aux = shiftgrid(180., correl, lons, start=False) figtitle = u'RSM97 x PRECL - FMA - 02-15\nPersistida - 0.54x0.54 - Correlação - Precip Acum' figname = "correl_rsm97per_x_precl_jfm_0215.png" levs = (-1.0, -0.9, -0.7, -0.5, -0.3, 0.3, 0.5, 0.7, 0.9, 1.0) #10 my_colors = ('#2372c9', '#3498ed', '#4ba7ef', '#76bbf3', '#93d3f6', '#b0f0f7', '#ffffff', '#fbe78a', '#ff9d37', '#ff5f26', '#ff2e1b', '#ff0219', '#ae000c') #13 print "\n... salvando figura ..."
from PyFuncemeClimateTools import PlotMaps as pm from PyFuncemeClimateTools import CreateNetCDF as cn from read_data import read_data pcp, pcpe, obs = read_data() nla = np.linspace(-90., 90., 181) nlo = np.linspace(-180., 179., 360) x, y = np.meshgrid(nlo, nla) y1, y2, x1, x2 = -60., 15., -90., -30. correl = cs.compute_pearson(pcp[0:30, :, :], obs[0:30, :, :]) figtitle = u'ECHAM4.6 x CMAP - {0}/{1} ({2})\nCorrelação - Precip Acum' \ .format('JAN', 'FMA', '8110') if not os.path.exists('figs_expsolar/map_correl'): os.makedirs('figs_expsolar/map_correl') figname = 'figs_expsolar/map_correl' \ '/bra_precip_persistida_{0}_null-{1}_null_{2}_echam46_1dg_cmap_' \ 'correlacao.png' \ .format('8110', 'FMA', 'JAN') levs = (-1.0, -0.9, -0.7, -0.5, -0.3, 0.3, 0.5, 0.7, 0.9, 1.0) # 10 my_colors = ('#2372c9', '#3498ed', '#4ba7ef', '#76bbf3', '#93d3f6',
figname = "img/curve_prob_{0}_{1}_{2}_{3}_{4}_{5}_{6}_{7}.png".format( mylon, mylat, myopt, myhind, myfmon, mytseason, myfyear, mytyear ) # Se a imagem nao tiver sido gerada anteriomente if not os.path.exists(figrealname): print "entrooou... delicia! =)" fcst, hind, obs = readdata(myhind, myfmon, myfyear, mytyear, mytseason) if not os.path.exists("img"): os.makedirs("img") below, normal, above, fsignal, fstd, opad, fs_anom = cs.compute_probability( fcst[0, i_lat, i_lon], hind[:, i_lat, i_lon], obs[:, i_lat, i_lon], lenhind ) fsignal = np.expand_dims(fsignal, axis=0) pm.plotnorm(fsignal, fstd, opad, fig_name=figname, fig_title="") shutil.copy2(figname, figrealname) else: print "nao entrou =(" # se a imagem ja tiver sido gerada, apenas faz copia para exibir no site shutil.copy2(figrealname, figname)
print "Erro ao ler arquivo do hindcast" exit() # Arquivo observação try: obs_file = "cmap.{0}-{1}-1.0dg.fix.nc".format(hind_period, target_months) read_obs_file = pupynere.NetCDFFile(obs_file, 'r') obs = read_obs_file.variables['pcp'][:, :, :] read_obs_file.close() except: print "Erro ao ler arquivo observado" exit() # RMSE print "... RMSE ..." error_rmse = cs.rmse(hind, obs) rmse_title = u'ECHAM4.6 x CMAP - {0}/{1} ({2})\nRMSE (mm) - Precip Acum'.format(fcst_month, target_months, hind_period) fig_rmse = 'bra_precip_persistida_{0}_null-{1}_null_{2}_echam46_1dg_cmap_rmse.png'.format(hind_period, target_months, n_fcst_month) levs = (0., 50., 100., 200., 400., 600., 800.) #7 my_colors = ('#ffffff', '#E1FFFF', '#B4F0FA', '#96D2FA', '#78B9FA', '#50A5F5', '#3C96F5', '#2882F0') #8 pm.map_shaded(error_rmse, lats-1./2., lons-1./2., latsouthpoint=-60., latnorthpoint=15., lonwestpoint=-90., loneastpoint=-30., fig_name=fig_rmse, barloc='right', barcolor=my_colors, barlevs=levs, fig_title=rmse_title, barinf='max', ocean_mask=1) # Desvio padrão print u"... desvio padrão ..." hind_std = np.nanstd(hind, axis=0) hind_std_title = u'ECHAM4.6 x CMAP - {0}/{1} ({2})\nDesvio Padrão (mm) - Precip Acum'.format(fcst_month, target_months, hind_period) fig_hind_std = 'bra_precip_persistida_{0}_null-{1}_null_{2}_echam46_1dg_cmap_std.png'.format(hind_period, target_months, n_fcst_month) levs = (0., 25., 50., 75., 100., 150., 200.) #7
# Carrega arquivo da climatologia clim_file = Dataset('/Users/Hulk/TestPyFuncemeClimateTools/lib/python2.7/site-packages/PyFuncemeClimateTools/examples/pcp-daily-total-ec4amip_8908JFM.nc') clim_pcp = clim_file.variables['pcp'][:] # Carrega arquivo da previsão fsct_file = Dataset('/Users/Hulk/TestPyFuncemeClimateTools/lib/python2.7/site-packages/PyFuncemeClimateTools/examples/pcp-daily-total-ec4amip_2015JFM.nc') fsct_pcp = fsct_file.variables['pcp'][:] # Longitude e latitudes fsct_lon = fsct_file.variables['longitude'][:] fsct_lat = fsct_file.variables['latitude'][:] # Calcula anomalias # A função abaixo retornar dois parametros myanom, myanom_pad = cs.compute_anomaly(fsct_pcp, clim_pcp) # Shift na longitude e nos dados do modelo # Isso é necessário pois a longitude do NetCDF é no formato 0 - 360 # a função aceita valores de longitude no formato -180 - +180 myanom, fsct_lon = shiftgrid(180., myanom, fsct_lon, start=False) # Plot map # a variavel a ser plotada deve ser 2d pm.anom_map_shaded(myanom[0, :, :], fsct_lon-2.8125/2., fsct_lat-2.8125/2.)
# lons = read_fcst_file.variables['lon'][:] # read_fcst_file.close() # hind_file = "/home/marcelo/FSCT-ECHAM46/APR2015_HIND89-08-1DG/pcp-daily-total-ec4amip_89-08MJJ.1.0dg.fix.nc" # read_hind_file = pupynere.NetCDFFile(hind_file, 'r') # hind = read_hind_file.variables['pcp'][:, :, :] # read_hind_file.close() # # obs_file = "/home/marcelo/FSCT-ECHAM46/APR2015_HIND89-08-1DG/cmap.89-08-MJJ-1.0dg.fix.nc" # read_obs_file = pupynere.NetCDFFile(obs_file, 'r') # obs = read_obs_file.variables['pcp'][:, :, :] # read_obs_file.close() if myopt == "p": n_lon, n_lat, i_lon, i_lat = dg.gridpoint(lons, lats, mylon, mylat) myfcst = fsct[0, i_lat, i_lon] myhind = hind[:, i_lat, i_lon] myobs = obs[:, i_lat, i_lon] below, normal, above, fsignal, fstd, opad = cs.compute_probability(myfcst, myhind, myobs) fsignal = np.expand_dims(fsignal, axis=0) figname = "curve_prob_%s_%s.png" % (mylon, mylat) pm.plotnorm(fsignal, fstd, opad, fig_name=figname, fig_title='') elif myopt == "a": print 't1' pass else: print 't2' pass
obs_aux = obs_file.variables['pre'][:] lons_obs = obs_file.variables['lon'][:] lats_obs = obs_file.variables['lat'][:] obs_file.close() x, y = np.meshgrid(lons_hind, lats_hind) # Interpola hindcast obs = np.full((30, 110, 129), np.nan) for i in range(30): obs[i, ...] = interp(obs_aux[i, ...], lons_obs, lats_obs, x, y, order=1) print(obs.shape) ########### CORRELAÇÃO ########### correl = cs.compute_pearson(hind, obs) figtitle = u'RSM2008 x INMET - JAN/{0} (8110)\nCORRELAÇÃO - PRECIP ACUM'.format(tri.upper()) figname = 'rsm.correl.{0}.png'.format(tri) levs = (-1.0, -0.9, -0.7, -0.5, -0.3, 0.3, 0.5, 0.7, 0.9, 1.0) #10 my_colors = ('#2372c9', '#3498ed', '#4ba7ef', '#76bbf3', '#93d3f6', '#b0f0f7', '#ffffff', '#fbe78a', '#ff9d37', '#ff5f26', '#ff2e1b', '#ff0219', '#ae000c') #13 pm.plotmap(correl, lats_hind, lons_hind, latsouthpoint=-38.27, latnorthpoint=13.63, lonwestpoint=-84.3, loneastpoint=-33., fig_name=figname, barloc='right', barcolor=my_colors, barlevs=levs, fig_title=figtitle, barinf='neither', ocean_mask=1) ########### VIÉS ########### print(hind.shape) print(obs.shape)
def ProbMemb(fcst_month, fcst_year, target_year, target_months, hind_period, nyears, shapef, nmemb=20): """ Esta função calcula a previsão (sinal) para todos os membros. :param fcst_month: mês previsão :type fcst_month: str :param fcst_year: ano do mês previsão :type fcst_year: str :param target_year: ano alvo da previsão :type target_year: str :param target_months: trimestre alvo da previsão :type target_months: str :param hind_period: período do hindcast :type hind_period: str :param nyears: número de anos do hindcast :type nyears: int :param shapef: arquivo de txt com pontos da região :type shapef: arquivo de txt :param nmemb: número de membros da preevisão/hinscast :type nmemb: int """ #### OBSERVADO #### print "\n +++ LENDO A CLIMATOLOGIA OBSERVADA +++\n" url = 'http://opendap2.funceme.br:8001/data/dados-obs/cmap/2.5dg/' \ 'cmap.precip.season.accum.standard.2.5dg.{0}.{1}.nc' \ .format("1981-2010", target_months) #~ print url obs_data = Dataset(url, 'r') obs_aux = obs_data.variables['precip'][:] obs_lons_360 = obs_data.variables['lon'][:] obs_lats = obs_data.variables['lat'][:] obs_data.close() obs_aux, obs_lons = shiftgrid(180., obs_aux, obs_lons_360, start=False) print " +++ INTERPOLANDO CLIMATOLOGIA OBSERVADA +++ \n" # Interpolação para 1 grau # Nova grade de 1 grau newlats = np.linspace(-90, 90, 181) newlons = np.linspace(-180, 179, 360) obs = np.zeros((int(obs_aux.shape[0]), int(len(newlats)), int(len(newlons)))) x, y = np.meshgrid(newlons, newlats) for i in range(0, int(obs_aux.shape[0])): obs[i, :, :] = interp(obs_aux[i, :, :], obs_lons, obs_lats, x, y, order=1) #### PREVISÃO #### print " +++ LENDO OS MEMBROS PREVISÃO +++ \n" #~ Monta a url do ano da previsão com todos os membros url = 'http://opendap2.funceme.br:8001/data/dados-pos-processados-echam46' \ '/forecasts/{0}/{2}/{1}/PCP/TRI/pcp-seasonacc-echam46-hind{0}-en%2.2d-{1}{2}_{3}{4}.ctl' \ .format(hind_period, fcst_month, fcst_year, target_year, target_months) files = [url % d for d in range(51, 71)] # Inicializa array que irá receber os membros do ano da previsão fcst_t42 = np.zeros((20, 64, 128)) * 0 for i, nc in enumerate(files): #~ print "", nc fcst_data = Dataset(nc, 'r') fcst_aux = fcst_data.variables['pcp'][:] fcst_lons_360 = fcst_data.variables['longitude'][:] fcst_lats = fcst_data.variables['latitude'][:] fcst_data.close() fcst_aux, fcst_lons = shiftgrid(180., fcst_aux, fcst_lons_360, start=False) fcst_t42[i, :, :] = fcst_aux[0, :, :] print " +++ INTERPOLANDO OS MEMBROS PREVISÃO +++ \n" # Interpolação para 1 grau # Nova grade de 1 grau fcst = np.zeros((20, int(len(newlats)), int(len(newlons)))) * 0 for i in range(20): fcst[i, :, :] = interp(fcst_t42[i, :, :], fcst_lons, fcst_lats, x, y, order=1) #### HINDCAST #### print " +++ LENDO OS MEMBROS DE CADA ANO DO HINDCAST +++\n" print " ==> AGUARDE...\n" # Inicializa array que irá receber todos os anos e todos os membros hind_t42 = np.zeros((30, 20, 64, 128)) * 0 for i, hind_year in enumerate(range(1981, 2011)): #~ print " ANO:", hind_year # Monta a url de cada ano com todos os membros url = 'http://opendap2.funceme.br:8001/data/dados-pos-processados-echam46' \ '/hindcasts/{0}/{1}/PCP/TRI/pcp-seasonacc-echam46-hind{0}-en%2.2d-{1}{3}_{3}{2}.ctl' \ .format(hind_period, fcst_month, target_months, hind_year) files = [url % d for d in range(51, 71)] # Inicializa array que irá receber os membros para cada ano hindmemb = np.zeros((20, 64, 128)) * 0 for j, nc in enumerate(files): hind_data = Dataset(nc, 'r') hind_aux = hind_data.variables['pcp'][:] hind_lons_360 = hind_data.variables['longitude'][:] hind_lats = hind_data.variables['latitude'][:] hind_data.close() hind_aux, hind_lons = shiftgrid(180., hind_aux, hind_lons_360, start=False) hindmemb[j, :, :] = hind_aux[0, :, :] hind_t42[i, :, :, :] = hindmemb[:, :, :] print " +++ INTERPOLANDO OS ANOS DE CADA MEMBRO DO HINDCAST. +++ \n" print " ==> AGUARDE... \n" # Interpolação para 1 grau hind = np.zeros((30 , 20, int(len(newlats)), int(len(newlons)))) * 0 for i in range(20): #~ print " MEMBRO:", i + 51 for j in range(30): hind[j, i, :, :] = interp(hind_t42[j, i, :, :], hind_lons, hind_lats, x, y, order=1) print " +++ APLICANDO MÁSCARA DA REGIÃO +++ \n" # Retorna matriz com os pontos sobre o Ceará pointsgrid, lonlatgrid, mymatriz = dg.pointinside(newlats, newlons, shapefile=shapef) ce_fcst = np.ma.array(fcst, mask=np.tile(mymatriz, (fcst.shape[0], 1))) ce_hind = np.ma.array(hind, mask=np.tile(mymatriz, (hind.shape[0], hind.shape[1], 1))) ce_obs = np.ma.array(obs, mask=np.tile(mymatriz, (obs.shape[0], 1))) print " +++ MÉDIA DOS PONTOS SOBRE A REGIÃO PARA TODOS OS MEMBROS +++ \n" ave_ce_fcst = np.zeros((int(nmemb), 1)) * 0 ave_ce_hind = np.zeros((int(nyears), int(nmemb), 1)) * 0 ave_ce_obs = np.zeros((int(nyears), 1)) * 0 for i in range(int(nmemb)): ave_ce_fcst[i, 0] = ce_fcst[i, :, :].mean() for j in range(int(nyears)): ave_ce_hind[j, i, 0] = ce_hind[j, i, :, :].mean() for i in range(int(nyears)): ave_ce_obs[i, 0] = ce_obs[i, :, :].mean() sig_membs_ce = np.zeros((int(nmemb))) * 0 print " +++ CALCULANDO SINAL DA PREVISÃO PARA TODOS OS MEMBROS +++\n" for i in range(nmemb): below_ce, normal_ce, above_ce, sig_membs_ce[i], f_std_ce, \ o_pad_ce, fcst_sig_anom = cs.compute_probability(ave_ce_fcst[i, 0], ave_ce_hind[:, i, 0], ave_ce_obs[:, 0], nyears) return sig_membs_ce
# pm.plotmap(anom_diff_p, nla, nlo, # 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) # diff das anomalias: controle menos experimento for i, y in enumerate(xrange(2011, 2017)): directory = 'figs_expsolar/diff_anom' if not os.path.exists(directory): os.makedirs(directory) anom, anom_pad = cs.compute_anomaly(pcp[30 + i, :, :], pcpe[0:30, :, :]) anom_exp, anom_pad_exp = cs.compute_anomaly(pcpe[30 + i, :, :], pcpe[0:30, :, :]) # diff das anomalias anom_diff = anom_exp - anom figtitle = u'Anom Diff (mm) (EXP - CONTROl)' figname_anom = '{0}/diff_anom_{1}.png'.format(directory, y) levs = (-300., -100., -50., -30., -15., -5., 5., 15., 30., 50., 100., 300.) # 12
y1, y2, x1, x2 = -60., 15., -90., -30. # América do sul ########## Apenas um plot ########## lev = [0., 50., 100., 200., 300., 500., 700., 900., 1000.] my_colors = ('#ffffff', '#E5E5E5', '#CBCBCB', '#B2B2B2', '#989898', '#7F7F7F', '#666666', '#4E4E4E') #8 figtitle = u'ECHAM4.6 - Precip Acum (mm)' figname = "{5}/bra_precip_persistida_{0}_{1}-{2}_{3}_{4}_echam46_1dg_null_precip_median.png" \ .format(hind_period_name, target_year, target_months, fcst_year, n_fcst_month, outdir) pm.plotmap(fcst[0, :, :], newlats-1./2., newlons-1./2., latsouthpoint=-88., latnorthpoint=88., lonwestpoint=-178., loneastpoint=178., fig_name=figname, fig_title=figtitle, barcolor=my_colors, ocean_mask=0, barlevs=lev, barinf='max', barloc='bottom', meridians=np.arange(-160., 161., 30.), parallels=np.arange(-90., 91., 20.)) ########## Anomalia corrigida metódo do Caio (regressão linear) ########## below, normal, above, f_signal, f_std, o_pad, fcst_sig_anom = cs.compute_probability(fcst, hind, obs, n_years) hind_corrigido = cs.compute_setrighthind(hind, obs, n_years) anomaly_corrigida, anomaly_pad = cs.compute_anomaly(fcst_sig_anom, hind_corrigido) #~ figtitle = u'ECHAM4.6 x CMAP - OCT/{4} - {1}/{4}\nAnomaly (mm) - Precip Accum ({2})'\ figtitle = u'ECHAM4.6 x CMAP - {0}/{3} - {1}/{4}\nAnomalia (mm) - Precip Acum ({2})'\ .format(fcst_month_name, target_months, hind_period_name, fcst_year, target_year) figname_anom = "{5}/bra_precip_persistida_{0}_{1}-{2}_{3}_{4}_echam46_1dg_null_anom_median.png" \ .format(hind_period_name, target_year, target_months, fcst_year, n_fcst_month, outdir) levs = (-700., -500., -300., -100., -50., -30., 30., 50., 100., 300., 500., 700.) #12 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)
exit(1) try: # Arquivo observação obs_file = open_url(obs_url) pcp_obs = obs_file['precip'] obs = np.asarray(pcp_obs) except: print "+++ Erro ao abrir arquivo do observado +++" print obs_url exit(1) myfcst = fsct[0, i_lat, i_lon] myhind = hind[:, i_lat, i_lon] myobs = obs[:, i_lat, i_lon] below, normal, above, fsignal, fstd, opad, fs_anom = cs.compute_probability(myfcst, myhind, myobs, lenhind) fsignal = np.expand_dims(fsignal, axis=0) pm.plotnorm(fsignal, fstd, opad, fig_name=figname, fig_title='') shutil.copy2(figname, figrealname) else: # se a imagem ja tiver sido gerada, apenas faz copia para exibir no site shutil.copy2(figrealname, figname) elif myopt == "a": print 't1' pass else: print 't2'