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)
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
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))
# 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)
# 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
# '_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)
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)
my_colors = ('#2372c9', '#3498ed', '#4ba7ef', '#76bbf3', '#93d3f6', '#b0f0f7', '#ffffff', '#fbe78a', '#ff9d37', '#ff5f26', '#ff2e1b', '#ff0219', '#ae000c') # 13 pm.plotmap(correl, nla, nlo, 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) # 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)
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)
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'