def verification_points(lat, lon): """ Check whether the model grid points are contained Ceara Shape points. Verifica se os Pontos da Grade do Modelo estao contidos os pontos do Shape do Ceara. :return: points_grid_north, lonlat_grid_north, array_bool_north points_grid_south, lonlat_grid_south, array_bool_south """ south = '/home/radar/gfs-analise/ceara_metade_de_baixo.txt' north = '/home/radar/gfs-analise/ceara_metade_de_cima.txt' points_grid_north, lonlat_grid_north, array_bool_north = defgrid.pointinside(lat, lon, north) points_grid_south, lonlat_grid_south, array_bool_south = defgrid.pointinside(lat, lon, south) return points_grid_north, lonlat_grid_north, array_bool_north, points_grid_south, lonlat_grid_south, array_bool_south
def caso_shape(data, lat, lon, shp): shapeurl = "http://opendap2.funceme.br:8001/data/utils/shapes/{0}".format( shp) line = re.sub('[/]', ' ', shapeurl) line = line.split() Ptshape = '/tmp/' + line[-1] shpn = line[-2].title() if not os.path.exists(Ptshape): Ptshape = wget.download(shapeurl, '/tmp', bar=None) xy = np.loadtxt(Ptshape) xx = xy[:, 0] yy = xy[:, 1] plt.plot(xx, yy, color='k', linewidth=1.0) plt.show() plt.savefig('Contorno_{0}.png'.format(shpn)) plt.close() shpn = line[-2].title() # Quando lon de 0:360 mudando para -180:180 if not np.any(lon < 0): lon = np.where(lon > 180, lon - 360, lon) # PONTOS DO POLIGONO QUE SERA MASCARADO Ptsgrid, lonlatgrid, Ptmask = Dg.pointinside(lat, lon, shapefile=Ptshape) # APLICANDO MASCARA DO POLIGONO NO DADO DE ENTRADA VarMasked_data = np.ma.array( data[:, :, :], mask=np.tile(Ptmask, (data.shape[0], 1))) # Array masked return VarMasked_data
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)) mean_ce_fcst[0] = points_over_ce_fcst.mean() for i in range(0, int(n_years)): mean_ce_hind[i] = points_over_ce_hind[i, :, :].mean() mean_ce_obs[i] = points_over_ce_obs[i, :, :].mean()
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
.format(fcst_month.upper(), fcst_year, target_months, target_year, hind_period_name) pm.maptercis(file_out, figtitle, figout) background = Image.open(figout) foreground = Image.open("FUNCEME_LOGO.png") foreground = foreground.resize((90, 70), Image.ANTIALIAS) background.paste(foreground, (334, 461), foreground) background.save(figout, optimize=True, quality=95) #### Curva para o CE #### # Retorna matriz com os pontos sobre o Ceará shapef = '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))) if obs_base == 'cmap': 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)) if obs_base == 'cmap': mean_ce_obs = np.zeros(int(n_years)) mean_ce_fcst[0] = points_over_ce_fcst.mean() for i in range(0, int(n_years)):
def ProbMemb(fcst_month, fcst_year, target_year, target_months, hind_period, nyears, shapef, nmemb=20): """ Esta função calcula a curva de probabilida 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 :return sig_membs_ce: Sinal da previsão para todos os membros """ #### OBSERVADO #### print('\n +++ LENDO A CLIMATOLOGIA OBSERVADA +++\n') if target_year > fcst_year: url = 'http://opendap2.funceme.br:8001/data/dados-obs/cmap/2.5dg/' \ 'cmap.precip.season.accum.standard.2.5dg.{0}.{1}.nc' \ .format("1982-2011", target_months) else: 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(target_year, fcst_year) 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('\n +++ 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('\n +++ 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)): if target_year > fcst_year: target_hind_year = int(hind_year) + 1 else: target_hind_year = 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}_{4}{2}.ctl' \ .format(hind_period, fcst_month, target_months, hind_year, target_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): print(nc) 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('\n +++ 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): 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() print(' +++ CALCULANDO SINAL DA PREVISÃO PARA TODOS OS MEMBROS +++\n') sig_membs_ce = np.empty((int(nmemb))) sig_membs_ce[:] = np.nan for i in range(nmemb): below_ce, normal_ce, above_ce, sig_membs_ce[i], f_std_ce, \ o_pad_ce, fcst_sig_anom = compute_probability(ave_ce_fcst[i, 0], ave_ce_hind[:, i, 0], ave_ce_obs[:, 0], nyears) return sig_membs_ce
fcst_month.upper(), fcst_year, target_months, target_year, hind_period_name, obs_base.upper() ) pm.maptercisrsm97(file_out, figtitle, figout, maskocean=1) background = Image.open(figout) foreground = Image.open("FUNCEME_LOGO.png") foreground = foreground.resize((90, 70), Image.ANTIALIAS) background.paste(foreground, (bx, by), foreground) background.save(figout, optimize=True, quality=95) ########## Curva para o CE ########## # Retorna matriz com os pontos sobre o Ceará # shapef = 'pontos_ce.txt' # TODO: Usar Thiessen pointsgrid, lonlatgrid, mymatriz = dg.pointinside(fcst_lats, fcst_lons, shapefile="pontos_ce.txt") # Aplica máscara para os pontos sobre o Ceará points_over_ce_fcst = np.ma.array(fcst, mask=mymatriz) mean_ce_fcst = np.zeros(1) mean_ce_fcst[0] = points_over_ce_fcst.mean() points_over_ce_hind = np.ma.array(hind, mask=np.tile(mymatriz, (hind.shape[0], 1))) mean_ce_hind = np.zeros(int(n_years)) if obs_base == "inmet" or obs_base == "chirps" or obs_base == "cru": points_over_ce_obs = np.ma.array(obs, mask=np.tile(mymatriz, (obs.shape[0], 1))) mean_ce_obs = np.zeros(int(n_years)) for i in range(0, int(n_years)): mean_ce_hind[i] = points_over_ce_hind[i, :, :].mean()