def test_interpolate_to_grid_pandas(): r"""Test whether this accepts a `pd.Series` without crashing.""" df = pd.DataFrame( { 'lat': [38, 39, 31, 30, 41, 35], 'lon': [-106, -105, -86, -96, -74, -70], 'tmp': [-10, -16, 13, 16, 0, 3.5] }, index=[1, 2, 3, 4, 5, 6]) interpolate_to_grid(df['lon'], df['lat'], df['tmp'], interp_type='natural_neighbor', hres=0.5)
def create_slp_grid(proj, df): lon = df['longitude'].values lat = df['latitude'].values xp, yp, _ = proj.transform_points(ccrs.PlateCarree(), lon, lat).T x_masked, y_masked, pres = remove_nan_observations(xp, yp, df['SLP'].values) slpgridx, slpgridy, slp = interpolate_to_grid(x_masked, y_masked, pres, interp_type='cressman', minimum_neighbors=1, search_radius=400000, hres=100000) return slpgridx, slpgridy, slp
def plot_precip_station_jja_cressman(ax, hydrosheds_dir, df_precip_station_jja, stretch_lat, search_rad, grid_spacing, **kwargs): cmap, norm, bounds, cbar_kwargs = load_cmap_data( 'cmap_data/li2018_fig2_cb1.pkl') if stretch_lat: lat = df_precip_station_jja.lat * 1.5 else: lat = df_precip_station_jja.lat gx, gy, griddata = interpolate_to_grid(df_precip_station_jja.lon, lat, df_precip_station_jja.precip, interp_type='cressman', minimum_neighbors=1, hres=grid_spacing, search_radius=search_rad) griddata = np.ma.masked_where(np.isnan(griddata), griddata) lon_min = df_precip_station_jja.lon.min() lon_max = df_precip_station_jja.lon.max() lat_min = df_precip_station_jja.lat.min() lat_max = df_precip_station_jja.lat.max() extent = (lon_min, lon_max, lat_min, lat_max) # Use to mask out ocean. hb = load_hydrobasins_geodataframe(hydrosheds_dir, 'as', [1]) raster = build_raster_from_lon_lat(hb.geometry, lon_min, lon_max, lat_min, lat_max, griddata.shape[1], griddata.shape[0]) griddata = np.ma.masked_array(griddata, raster == 0) im = ax.imshow(griddata, origin='lower', cmap=cmap, norm=norm, extent=extent, interpolation='bilinear') # im = ax.imshow(img, origin='lower', cmap=cmap, norm=norm, extent=extent) # plt.colorbar(im, label='precip. (mm day$^{-1}$)', # orientation='horizontal', norm=norm, spacing='uniform', **cbar_kwargs) return im
def NN_z(x, y, con_ver, nbr_ver, cellsize): """ Compute elevation using NN method """ gx, gy, elevNNGrid = interpolate_to_grid(con_ver[:, 0], con_ver[:, 1], con_ver[:, 2], interp_type="natural_neighbor", hres=cellsize[0]) elev_NN = elevNNGrid[0, 0] if not (np.isnan(elev_NN)): elev_i = elev_NN else: print("elev_NN is nan: evaluating else loop") d_nbr = np.zeros(3) for n in range(0, 3): d_nbr[n] = ((x - nbr_ver[n][0])**2 + (y - nbr_ver[n][1])**2)**0.5 nearest_ver = nbr_ver[d_nbr.argmax(0)] elev_i = nearest_ver[2] return elev_i
########################################### # Project the lon/lat locations to our final projection lon = data['longitude'].values lat = data['latitude'].values xp, yp, _ = to_proj.transform_points(ccrs.Geodetic(), lon, lat).T ########################################### # Remove all missing data from pressure x_masked, y_masked, pres = remove_nan_observations(xp, yp, data['slp'].values) ########################################### # Interpolate pressure using Cressman interpolation slpgridx, slpgridy, slp = interpolate_to_grid(x_masked, y_masked, pres, interp_type='cressman', minimum_neighbors=1, search_radius=400000, hres=100000) ########################################## # Get wind information and mask where either speed or direction is unavailable wind_speed = (data['wind_speed'].values * units('m/s')).to('knots') wind_dir = data['wind_dir'].values * units.degree good_indices = np.where((~np.isnan(wind_dir)) & (~np.isnan(wind_speed))) x_masked = xp[good_indices] y_masked = yp[good_indices] wind_speed = wind_speed[good_indices] wind_dir = wind_dir[good_indices]
def create_map(year, month=None, day=None): """ Create map for given moment return figure """ LOGGER.info('Create map.') LOGGER.debug('Connect to DB %s.', DB_FILE) conn = sqlite3.connect(DB) LOGGER.debug('Get a cursor object.') cursor = conn.cursor() mapname = create_mapname(year, month, day) LOGGER.debug('Map name is %s.', mapname) if day: table = 'daily' elif month: table = 'monthly' elif year: table = 'yearly' query = ''' SELECT dates, cell, temp FROM %s WHERE dates = "%s"; ''' % (table, mapname) LOGGER.debug('SQL query: %s.', query) result_df = pd.read_sql_query(query, conn, index_col='dates') result_df = result_df['temp'] LOGGER.debug('Prepare grid cooardinates.') LOGGER.debug('Apply Albers Equal Area projection.') to_proj = ccrs.AlbersEqualArea(central_longitude=-1., central_latitude=10.) LOGGER.debug('Albers Equal Area projection applied.') query = '''SELECT id, lon, lat FROM %s;''' % 'grid' LOGGER.debug('SQL query: %s', query) grid = pd.read_sql_query(query, conn, index_col='id') cursor.close() conn.close() lon = grid['lon'].values lat = grid['lat'].values LOGGER.debug('Begin transformation to Geodetic coordinate system.') xp_, yp_, _ = to_proj.transform_points(ccrs.Geodetic(), lon, lat).T LOGGER.debug('Transform to Geodetic coordinate system completed.') LOGGER.debug('Remove NaNs.') x_masked, y_masked, temps = remove_nan_observations( xp_, yp_, result_df.values) LOGGER.debug('NaNs removed.') LOGGER.debug('Interpolate to grid.') tempx, tempy, temp = interpolate_to_grid(x_masked, y_masked, temps, interp_type='barnes', minimum_neighbors=8, search_radius=150000, hres=10000) LOGGER.debug('Interpolated to grid.') LOGGER.debug('Apply mask for NaNs.') temp = np.ma.masked_where(np.isnan(temp), temp) LOGGER.debug('Mask applied.') LOGGER.debug('Create map figure %s.', mapname) levels = list(range(-20, 20, 1)) # use viridis colormap cmap = plt.get_cmap('viridis') norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) # TODO plt.figure(figsize=(20, 10)) decrese to 13, 10 fig = plt.figure(figsize=(20, 10)) LOGGER.debug('Add projection to figure.') view = fig.add_subplot(1, 1, 1, projection=to_proj) LOGGER.debug('Projection added.') LOGGER.debug('Add map features to figure.') view.set_extent([27.0, 16.9, 49.5, 44.5]) view.add_feature(cfeature.STATES.with_scale('50m')) view.add_feature(cfeature.OCEAN) view.add_feature(cfeature.COASTLINE.with_scale('50m')) view.add_feature(cfeature.BORDERS, linestyle=':') LOGGER.debug('Map features added.') # make colorbar legend for figure mmb = view.pcolormesh(tempx, tempy, temp, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0.02, boundaries=levels) view.set_title('Srednja temperatura') # TODO: decrease borders, check does it works?? # fig.tight_bbox() # fig.savefig(mapname + '.png', bbox_inches='tight') LOGGER.info('Map figure %s created.', (mapname)) plt.close('all') return fig
grid1 = pd.read_sql_query(tacka, cnx) lon = grid1['lon'].values lat = grid1['lat'].values country = grid1['country'].values altitude = grid1['altitude'].values prec = df['prec'].values #to_proj = ccrs.AlbersEqualArea(central_longitude=14, central_latitude=10) to_proj = ccrs.Mercator() #transfor to Geodetic xp, yp, _ = to_proj.transform_points(ccrs.Geodetic(), lon, lat).T x_masked, y_masked, p = remove_nan_observations(xp, yp, prec) precx, precy, prec_p = interpolate_to_grid(x_masked, y_masked, p, interp_type='linear', hres=2000) prec_p = np.ma.masked_where(np.isnan(prec_p), prec_p) inter_point_Lon = float(input('unesite vrednost za longitudu: ' ' ')) inter_point_Lat = float(input('unesite vrednost za latitudu: ' ' ')) xy = np.vstack([lon, lat]).T #xi = [inter_point_Lon,inter_point_Lat] xi = ([inter_point_Lon, inter_point_Lat]) #xii = np.vstack([inter_point_Lon,inter_point_Lat]).T inter_point = interpolate_to_points(xy, prec, xi, interp_type='linear') #inter_point =natural_neighbor_to_points(xy,prec,xii)
def create_map_prec(year,inter,month=None,day=None): LOGGER.info('Create map.') LOGGER.debug('Connect to DB1 %s.', DB_FILE1) conn = sqlite3.connect(DB1) LOGGER.debug('Get a cursor object.') cursor = conn.cursor() mapname = create_mapname_p(year, month, day) LOGGER.debug('Map name is %s.', mapname) if day: table = 'daily' elif month: table = 'monthly' elif year: table = 'yearly' query = ''' SELECT dates, cell,prec FROM %s WHERE dates = "%s"; ''' % (table, mapname) LOGGER.debug('SQL query: %s.', query) result_df = pd.read_sql_query(query, conn, index_col='dates') result_df = result_df['prec'] LOGGER.debug('Prepare grid cooardinates.') LOGGER.debug('Apply Albers Equal Area projection.') to_proj = ccrs.Mercator() LOGGER.debug('Albers Equal Area projection applied.') query = '''SELECT id, lon, lat FROM %s;''' % 'grid1' LOGGER.debug('SQL query: %s', query) grid1 = pd.read_sql_query(query, conn, index_col='id') cursor.close() conn.close() lon = grid1['lon'].values lat = grid1['lat'].values LOGGER.debug('Begin transformation to Geodetic coordinate system.') xp_, yp_, _ = to_proj.transform_points(ccrs.Geodetic(), lon, lat).T LOGGER.debug('Transform to Geodetic coordinate system completed.') LOGGER.debug('Remove NaNs.') x_masked, y_masked, precipi = remove_nan_observations( xp_, yp_, result_df.values) LOGGER.debug('NaNs removed.') if inter == "linear": LOGGER.debug('Interpolate to grid.') precx, precy, prec = interpolate_to_grid( x_masked, y_masked, precipi, interp_type='linear', search_radius=80000, hres=5000) LOGGER.debug('Interpolated to grid.') LOGGER.debug('Apply mask for NaNs.') prec = np.ma.masked_where(np.isnan(prec),prec) LOGGER.debug('Mask applied.') #a = int(result_df.values.max()) #b = int(result_df.values.min()) a = int(prec.max()) b = int(prec.min()) #clevs = list(range(b,a)) # LOGGER.debug('Crea)te map figure %s.', mapname) if table == 'monthly': clevs = list(range(b,a+10,10)) elif table == 'yearly': clevs = list(range(b,a+100,100)) elif table == 'daily': if a <2: clevs = list(range(b,a+0.1,0.1)) elif a > 20: clevs = list(range(b,a+2,2)) else: clevs = list(range(b,a+2)) # use viridis colormap cmap = plt.get_cmap('Blues') #cmap = mcolors.ListedColormap(cmap_data, 'precipitation') norm = mcolors.BoundaryNorm(clevs, cmap.N) #cmap = plt.get_cmap('viridis') #norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) # TODO plt.figure(figsize=(20, 10)) decrese to 13, 10 fig = plt.figure(figsize=(10,10)) LOGGER.debug('Add projection to figure.') view = fig.add_subplot(1, 1, 1, projection=to_proj) LOGGER.debug('Projection added.') LOGGER.debug('Add map features to figure.') view.set_extent([27.0, 17.1, 50, 44.5]) #view.set_extent([22.7, 18.5, 46.6, 44.1]) view.add_feature(cfeature.BORDERS, linestyle=':') LOGGER.debug('Map features added.') # make colorbar legend for figure #mmb = view.pcolormesh(precx, precy, prec, cmap=cmap, norm=norm) #mmb = view.pcolormesh(precx, precy, prec, cmap=cmap, norm=norm) cs = view.contourf(precx, precy, prec,clevs, cmap=cmap, norm=norm) #fig.colorbar(mmb,shrink=.4, pad=0.02, boundaries=levels) fig.colorbar(cs,shrink=.65, pad=0.06) #view.set_title('Srednje padavine') gl = view.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=1, color='gray', alpha=0.5, linestyle='--') gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER # TODO: decrease borders, check does it works?? # fig.tight_bbox() #fig.savefig(mapname + '.png', bbox_inches='tight') LOGGER.info('Map figure %s created.', (mapname)) plt.close('all') return fig if inter == "barnes" : LOGGER.debug('Interpolate to grid.') precx, precy, prec = interpolate_to_grid( x_masked, y_masked, precipi, interp_type='barnes', search_radius=80000, hres=5000) LOGGER.debug('Interpolated to grid.') LOGGER.debug('Apply mask for NaNs.') prec = np.ma.masked_where(np.isnan(prec),prec) LOGGER.debug('Mask applied.') #a = int(result_df.values.max()) #b = int(result_df.values.min()) a = int(prec.max()) b = int(prec.min()) LOGGER.debug('Create map figure %s.', mapname) #clevs = list(range(b,a)) if table == 'monthly': clevs = list(range(b,a+10,10)) elif table == 'yearly': clevs = list(range(b,a+100,100)) elif table == 'daily': if a <2: clevs = list(range(b,a+0.1,0.1)) elif a > 20: clevs = list(range(b,a+2,2)) else: clevs = list(range(b,a+2)) # use viridis colormap cmap = plt.get_cmap('Blues') #cmap = mcolors.ListedColormap(cmap_data, 'precipitation') norm = mcolors.BoundaryNorm(clevs, cmap.N) #cmap = plt.get_cmap('viridis') #norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) # TODO plt.figure(figsize=(20, 10)) decrese to 13, 10 fig = plt.figure(figsize=(10,10)) LOGGER.debug('Add projection to figure.') view = fig.add_subplot(1, 1, 1, projection=to_proj) LOGGER.debug('Projection added.') LOGGER.debug('Add map features to figure.') view.set_extent([27.0, 17.1, 50, 44.5]) #view.set_extent([22.7, 18.5, 46.6, 44.1]) view.add_feature(cfeature.BORDERS, linestyle=':') LOGGER.debug('Map features added.') # make colorbar legend for figure #mmb = view.pcolormesh(precx, precy, prec, cmap=cmap, norm=norm) #mmb = view.pcolormesh(precx, precy, prec, cmap=cmap, norm=norm) cs = view.contourf(precx, precy, prec,clevs, cmap=cmap, norm=norm) #fig.colorbar(mmb,shrink=.4, pad=0.02, boundaries=levels) fig.colorbar(cs,shrink=.65, pad=0.06) #view.set_title('Srednje padavine') gl = view.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=1, color='gray', alpha=0.5, linestyle='--') gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER # TODO: decrease borders, check does it works?? # fig.tight_bbox() #fig.savefig(mapname + '.png', bbox_inches='tight') LOGGER.info('Map figure %s created.', (mapname)) plt.close('all') return fig if inter == "cressman" : LOGGER.debug('Interpolate to grid.') precx, precy, prec = interpolate_to_grid( x_masked, y_masked, precipi, interp_type='cressman', search_radius=80000, hres=5000) LOGGER.debug('Interpolated to grid.') LOGGER.debug('Apply mask for NaNs.') prec = np.ma.masked_where(np.isnan(prec),prec) LOGGER.debug('Mask applied.') #a = int(result_df.values.max()) #b = int(result_df.values.min()) a = int(prec.max()) b = int(prec.min()) LOGGER.debug('Create map figure %s.', mapname) #clevs = list (range (b,a)) if table == 'monthly': clevs = list(range(b,a+10,10)) elif table == 'yearly': clevs = list(range(b,a+100,100)) elif table == 'daily': if a <2: clevs = list(range(b,a+0.1,0.1)) elif a > 20: clevs = list(range(b,a+2,2)) else: clevs = list(range(b,a+2)) # use viridis colormap cmap = plt.get_cmap('Blues') #cmap = mcolors.ListedColormap(cmap_data, 'precipitation') norm = mcolors.BoundaryNorm(clevs, cmap.N) #cmap = plt.get_cmap('viridis') #norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) # TODO plt.figure(figsize=(20, 10)) decrese to 13, 10 fig = plt.figure(figsize=(10,10)) LOGGER.debug('Add projection to figure.') view = fig.add_subplot(1, 1, 1, projection=to_proj) LOGGER.debug('Projection added.') LOGGER.debug('Add map features to figure.') view.set_extent([27.0, 17.1, 50, 44.5]) #view.set_extent([22.7, 18.5, 46.6, 44.1]) view.add_feature(cfeature.BORDERS, linestyle=':') LOGGER.debug('Map features added.') gl = view.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=1, color='gray', alpha=0.5, linestyle='--') gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER # make colorbar legend for figure #mmb = view.pcolormesh(precx, precy, prec, cmap=cmap, norm=norm) #mmb = view.pcolormesh(precx, precy, prec, cmap=cmap, norm=norm) cs = view.contourf(precx, precy, prec,clevs, cmap=cmap, norm=norm) #fig.colorbar(mmb,shrink=.4, pad=0.02, boundaries=levels) fig.colorbar(cs,shrink=.65, pad=0.06) #view.set_title('Srednje padavine') # TODO: decrease borders, check does it works?? # fig.tight_bbox() #fig.savefig(mapname + inter + '.png', bbox_inches='tight') LOGGER.info('Map figure %s created.', (mapname)) plt.close('all') return fig
to_proj = ccrs.AlbersEqualArea(central_longitude=-97.0000, central_latitude=38.0000) levels = list(range(-20, 20, 1)) cmap = plt.get_cmap('magma') norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) x, y, temp = station_test_data('air_temperature', from_proj, to_proj) x, y, temp = remove_nan_observations(x, y, temp) x, y, temp = remove_repeat_coordinates(x, y, temp) ########################################### # Scipy.interpolate linear # ------------------------ gx, gy, img = interpolate_to_grid(x, y, temp, interp_type='linear', hres=75000) img = np.ma.masked_where(np.isnan(img), img) fig, view = basic_map(to_proj) mmb = view.pcolormesh(gx, gy, img, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0, boundaries=levels) ########################################### # Natural neighbor interpolation (MetPy implementation) # ----------------------------------------------------- # `Reference <https://github.com/Unidata/MetPy/files/138653/cwp-657.pdf>`_ gx, gy, img = interpolate_to_grid(x, y, temp, interp_type='natural_neighbor', hres=75000) img = np.ma.masked_where(np.isnan(img), img)
from_proj = ccrs.Geodetic() to_proj = ccrs.AlbersEqualArea(central_longitude=-97.0000, central_latitude=38.0000) levels = list(range(-20, 20, 1)) cmap = plt.get_cmap('magma') norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) x, y, temp = station_test_data('air_temperature', from_proj, to_proj) x, y, temp = remove_nan_observations(x, y, temp) x, y, temp = remove_repeat_coordinates(x, y, temp) ########################################### # Scipy.interpolate linear # ------------------------ gx, gy, img = interpolate_to_grid(x, y, temp, interp_type='linear', hres=75000) img = np.ma.masked_where(np.isnan(img), img) fig, view = basic_map(to_proj) mmb = view.pcolormesh(gx, gy, img, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0, boundaries=levels) ########################################### # Natural neighbor interpolation (MetPy implementation) # ----------------------------------------------------- # `Reference <https://github.com/Unidata/MetPy/files/138653/cwp-657.pdf>`_ gx, gy, img = interpolate_to_grid(x, y, temp, interp_type='natural_neighbor', hres=75000) img = np.ma.masked_where(np.isnan(img), img) fig, view = basic_map(to_proj) mmb = view.pcolormesh(gx, gy, img, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0, boundaries=levels)
na_values=-99999) ########################################### # Project the lon/lat locations to our final projection lon = data['longitude'].values lat = data['latitude'].values xp, yp, _ = to_proj.transform_points(ccrs.Geodetic(), lon, lat).T ########################################### # Remove all missing data from pressure x_masked, y_masked, pres = remove_nan_observations(xp, yp, data['slp'].values) ########################################### # Interpolate pressure using Cressman interpolation slpgridx, slpgridy, slp = interpolate_to_grid(x_masked, y_masked, pres, interp_type='cressman', minimum_neighbors=1, search_radius=400000, hres=100000) ########################################## # Get wind information and mask where either speed or direction is unavailable wind_speed = (data['wind_speed'].values * units('m/s')).to('knots') wind_dir = data['wind_dir'].values * units.degree good_indices = np.where((~np.isnan(wind_dir)) & (~np.isnan(wind_speed))) x_masked = xp[good_indices] y_masked = yp[good_indices] wind_speed = wind_speed[good_indices] wind_dir = wind_dir[good_indices] ###########################################
# to_proj = ccrs.AlbersEqualArea(central_longitude=110.0000, central_latitude=38.0000) x, y, temp = station_test_data('rain', from_proj, to_proj) x, y, temp = remove_nan_observations(x, y, temp) x, y, temp = remove_repeat_coordinates(x, y, temp) # # #色彩定制:24小时降水量级色标 levels = [0.1, 10., 25., 50., 100., 250., 500] #自定义分级 cdict = ['#A6F28F', '#3DBA3D', '#61B8FF', '#0000FF', '#FA00FA', '#800040'] #自定义颜色列表 cmap = mpl.colors.ListedColormap(cdict) #自定义颜色映射colormap norm = mpl.colors.BoundaryNorm(levels, cmap.N, clip=True) #基于离散区间生成颜色映射索引 ################ 插值方法选择 ######################### # # Scipy的线性插值Scipy.interpolate linear # # ------------------------ gx, gy, img = interpolate_to_grid(x, y, temp, interp_type='linear', hres=1000) img = np.ma.masked_where(np.isnan(img), img) fig, view = basic_map(to_proj, 'Scipy Linear') mmb = view.contourf(gx, gy, img, levels, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, pad=0, boundaries=levels) ########################################### # # Metpy的自然相邻插值 Natural neighbor interpolation (MetPy implementation) # # ----------------------------------------------------- # # `Reference <https://github.com/Unidata/MetPy/files/138653/cwp-657.pdf>`_ # gx, gy, img = interpolate_to_grid(x, y, temp, interp_type='natural_neighbor', hres=75000) # img = np.ma.masked_where(np.isnan(img), img) # fig, view = basic_map(to_proj, 'Metpy Natural Neighbor') # mmb = view.contourf(gx, gy, img, levels, cmap=cmap, norm=norm) # # mmb = view.pcolormesh(gx, gy, img,cmap=cmap, norm=norm) # fig.colorbar(mmb, shrink=.4, pad=0, boundaries=levels)
yvalue = np.array(yvalue) - yshift zvalue = np.array(zvalue) xi = xi - xshift yi = yi - yshift # Tests Kriging # zi,var = Ordinary_Kriging(xvalue, yvalue, zvalue, xi, yi) # xi, yi, zi = interpolate_to_grid(xvalue, yvalue, zvalue, interp_type='rbf', hres=2, rbf_func='linear') # Tests Metpy: # xi, yi, zi = interpolate_to_grid(xvalue, yvalue, zvalue, interp_type='linear', hres=2) # xi, yi, zi = interpolate_to_grid(xvalue, yvalue, zvalue, interp_type='natural_neighbor', hres=2) xi, yi, zi = interpolate_to_grid(xvalue, yvalue, zvalue, interp_type='cressman', minimum_neighbors=1, hres=2, search_radius=3) # xi, yi, zi = interpolate_to_grid(xvalue, yvalue, zvalue, interp_type='barnes', hres=2, # search_radius=3) # xi, yi, zi = interpolate_to_grid(xvalue, yvalue, zvalue, interp_type='rbf', hres=2, rbf_func='gaussian', # rbf_smooth=0) xi = xi + xshift yi = yi + yshift xvalue = np.array(xvalue) + xshift yvalue = np.array(yvalue) + yshift fig, axs = plt.subplots(1, 2, figsize=(18, 5))
#col_names = ['index','lon','lat','country','altitude'] ovo koristimo ako nemama definisane imena kolona #load temp df = pd.read_fwf(fname, na_values='MM') lon = df['lon'].values lat = df['lat'].values xp, yp, _ = to_proj.transform_points(ccrs.Geodetic(), lon, lat).T data1 = pd.read_csv('CARPATGRID_TA_M.ser', sep='\s+') y = int(input('Unesite godinu: ' ' ')) m = int(input('Unesite mesec: ' ' ')) x1 = data1.loc[y, m] x_masked, y_masked, t = remove_nan_observations(xp, yp, x1.values) tempx, tempy, temp = interpolate_to_grid(x_masked, y_masked, t, interp_type='linear', hres=2000) temp = np.ma.masked_where(np.isnan(temp), temp) a = int(temp.max()) b = int(temp.min()) levels = list(range(b, a + 1)) cmap = plt.get_cmap('viridis') norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) fig = plt.figure(figsize=(20, 10)) view = fig.add_subplot(1, 1, 1, projection=to_proj)
def extract_dataset(datadir, dataset, daterange): if dataset == 'cmorph_0p25': datadir = Path(f'{datadir}/cmorph_data/0.25deg-3HLY') season = 'jja' filename = f'cmorph_ppt_{season}.{daterange}.asia_precip.ppt_thresh_0p1.nc' amount_jja = iris.load_cube(f'{datadir}/{filename}', f'amount_of_precip_{season}') amount_jja_china = amount_jja.collapsed( 'time', iris.analysis.MEAN).extract(CONSTRAINT_CHINA) elif dataset == 'cmorph_8km_N1280': datadir = Path(f'{datadir}/cmorph_data/8km-30min') season = 'jja' filename = f'cmorph_ppt_{season}.{daterange}.asia_precip.ppt_thresh_0p1.N1280.nc' amount_jja = iris.load_cube(f'{datadir}/{filename}', f'amount_of_precip_{season}') amount_jja_china = amount_jja.collapsed( 'time', iris.analysis.MEAN).extract(CONSTRAINT_CHINA) elif dataset == 'aphrodite': datadir = Path(f'{datadir}/aphrodite_data/025deg') amount = iris.load_cube( str(datadir / 'APHRO_MA_025deg_V1901.2009.nc'), ' daily precipitation analysis interpolated onto 0.25deg grids') epoch2009 = dt.datetime(2009, 1, 1) time_index = np.array([ epoch2009 + dt.timedelta(minutes=m) for m in amount.coord('time').points ]) jja = ((time_index >= dt.datetime(2009, 6, 1)) & (time_index < dt.datetime(2009, 9, 1))) amount_jja = amount[jja] amount_jja_mean = amount_jja.collapsed('time', iris.analysis.MEAN) amount_jja_china = amount_jja_mean.extract(CONSTRAINT_CHINA) elif dataset == 'gauge_china_2419': df_station_info, df_precip, df_precip_jja, df_precip_station_jja = load_jja_gauge_data( datadir) if False: lat = df_precip_station_jja.lat * 1.5 else: lat = df_precip_station_jja.lat lon = df_precip_station_jja.lon precip = df_precip_station_jja.precip # Front/back pad each array to include a value that fits with a native 0.25 deg grid. lon = np.array([75.125] + list(lon) + [134.375]) lat = np.array([16.375] + list(lat) + [80.375]) precip = np.array([0] + list(precip) + [0]) gx, gy, griddata = interpolate_to_grid(lon, lat, precip, interp_type='cressman', minimum_neighbors=1, hres=0.25, search_radius=0.48) lat_coord = iris.coords.Coord(gy[:, 0], standard_name='latitude', units='degrees') lon_coord = iris.coords.Coord(gx[0], standard_name='longitude', units='degrees') coords = [(lat_coord, 0), (lon_coord, 1)] amount_jja_mean = iris.cube.Cube(griddata, long_name='precipitation', units='mm hr-1', dim_coords_and_dims=coords) amount_jja_china = amount_jja_mean.extract(CONSTRAINT_CHINA) elif dataset[:2] == 'u-': # UM_N1280 run: runid = dataset[2:7] datadir = Path(f'{datadir}/u-{runid}/ap9.pp') season = 'jja' filename = f'{runid}a.p9{season}.{daterange}.asia_precip.ppt_thresh_0p1.nc' amount_jja = iris.load_cube(f'{datadir / filename}', f'amount_of_precip_{season}') amount_jja_china = amount_jja.collapsed( 'time', iris.analysis.MEAN).extract(CONSTRAINT_CHINA) iris.save(amount_jja_china, f'data/{dataset}_china_amount.{daterange}.nc')
def create_map(year,inter,month=None,day=None): """ Create map for given moment return figure """ LOGGER.info('Create map.') LOGGER.debug('Connect to DB %s.', DB_FILE) conn = sqlite3.connect(DB) LOGGER.debug('Get a cursor object.') cursor = conn.cursor() mapname = create_mapname(year, month, day) LOGGER.debug('Map name is %s.', mapname) if day: table = 'daily' elif month: table = 'monthly' elif year: table = 'yearly' query = ''' SELECT dates, cell, temp FROM %s WHERE dates = "%s"; ''' % (table, mapname) LOGGER.debug('SQL query: %s.', query) result_df = pd.read_sql_query(query, conn, index_col='dates') result_df = result_df['temp'] LOGGER.debug('Prepare grid cooardinates.') LOGGER.debug('Apply Albers Equal Area projection.') to_proj = ccrs.Mercator() LOGGER.debug('Albers Equal Area projection applied.') query = '''SELECT id, lon, lat FROM %s;''' % 'grid' LOGGER.debug('SQL query: %s', query) grid = pd.read_sql_query(query, conn, index_col='id') cursor.close() conn.close() lon = grid['lon'].values lat = grid['lat'].values LOGGER.debug('Begin transformation to Geodetic coordinate system.') xp_, yp_, _ = to_proj.transform_points(ccrs.Geodetic(), lon, lat).T LOGGER.debug('Transform to Geodetic coordinate system completed.') LOGGER.debug('Remove NaNs.') x_masked, y_masked, temps = remove_nan_observations( xp_, yp_, result_df.values) LOGGER.debug('NaNs removed.') if inter == "linear" : LOGGER.debug('Interpolate to grid.') tempx, tempy, temp = interpolate_to_grid( x_masked, y_masked, temps, interp_type='linear', hres=2000) LOGGER.debug('Interpolated to grid.') LOGGER.debug('Apply mask for NaNs.') temp = np.ma.masked_where(np.isnan(temp), temp) LOGGER.debug('Mask applied.') #a = int(result_df.values.max()) #b = int(result_df.values.min()) a = int(temp.max()) b = int(temp.min()) LOGGER.debug('Create map figure %s.', mapname) levels = list(range(b, a+1)) # use viridis colormap cmap = plt.get_cmap('viridis') norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) # TODO plt.figure(figsize=(20, 10)) decrese to 13, 10 fig = plt.figure(figsize=(10, 10)) LOGGER.debug('Add projection to figure.') view = fig.add_subplot(1, 1, 1, projection=to_proj) LOGGER.debug('Projection added.') LOGGER.debug('Add map features to figure.') view.set_extent([27.0, 17.1, 50, 44.5]) view.add_feature(cfeature.BORDERS, linestyle=':') LOGGER.debug('Map features added.') gl = view.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=2, color='gray', alpha=0.5, linestyle='--') gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER # make colorbar legend for figure mmb = view.pcolormesh(tempx, tempy, temp, cmap=cmap, norm=norm) fig.colorbar(mmb,shrink=.65, pad=0.06, boundaries=levels) #view.set_title('Srednja temperatura') # TODO: decrease borders, check does it works?? # fig.tight_bbox() # fig.savefig(mapname + '.png', bbox_inches='tight') LOGGER.info('Map figure %s created.', (mapname)) plt.close('all') return fig if inter == "barnes" : LOGGER.debug('Interpolate to grid.') tempx, tempy, temp = interpolate_to_grid( x_masked, y_masked, temps, interp_type='barnes', search_radius=80000, hres=5000) LOGGER.debug('Interpolated to grid.') LOGGER.debug('Apply mask for NaNs.') temp = np.ma.masked_where(np.isnan(temp), temp) LOGGER.debug('Mask applied.') #a = int(result_df.values.max()) #b = int(result_df.values.min()) a = int(temp.max()) b = int(temp.min()) LOGGER.debug('Create map figure %s.', mapname) levels = list(range(b, a+1)) # use viridis colormap cmap = plt.get_cmap('viridis') norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) # TODO plt.figure(figsize=(20, 10)) decrese to 13, 10 fig = plt.figure(figsize=(10, 10)) LOGGER.debug('Add projection to figure.') view = fig.add_subplot(1, 1, 1, projection=to_proj) LOGGER.debug('Projection added.') LOGGER.debug('Add map features to figure.') view.set_extent([27.0, 17.1, 50, 44.5]) view.add_feature(cfeature.BORDERS, linestyle=':') LOGGER.debug('Map features added.') gl = view.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=2, color='gray', alpha=0.5, linestyle='--') gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER # make colorbar legend for figure mmb = view.pcolormesh(tempx, tempy, temp, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.65, pad=0.06, boundaries=levels) #view.set_title('Srednja temperatura') # TODO: decrease borders, check does it works?? # fig.tight_bbox() # fig.savefig(mapname + '.png', bbox_inches='tight') LOGGER.info('Map figure %s created.', (mapname)) plt.close('all') return fig if inter == "cressman" : LOGGER.debug('Interpolate to grid.') tempx, tempy, temp = interpolate_to_grid( x_masked, y_masked, temps, interp_type='cressman', minimum_neighbors=3, search_radius=80000, hres=5000) LOGGER.debug('Interpolated to grid.') LOGGER.debug('Apply mask for NaNs.') temp = np.ma.masked_where(np.isnan(temp), temp) LOGGER.debug('Mask applied.') #a = int(result_df.values.max()) #b = int(result_df.values.min()) a = int(temp.max()) b = int(temp.min()) LOGGER.debug('Create map figure %s.', mapname) levels = list(range(b, a+1)) # use viridis colormap cmap = plt.get_cmap('viridis') norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) # TODO plt.figure(figsize=(20, 10)) decrese to 13, 10 fig = plt.figure(figsize=(10, 10)) LOGGER.debug('Add projection to figure.') view = fig.add_subplot(1, 1, 1, projection=to_proj) LOGGER.debug('Projection added.') LOGGER.debug('Add map features to figure.') view.set_extent([27.0, 17.1, 50, 44.5]) view.add_feature(cfeature.BORDERS, linestyle=':') LOGGER.debug('Map features added.') gl = view.gridlines(crs=ccrs.PlateCarree(), draw_labels=True, linewidth=2, color='gray', alpha=0.5, linestyle='--') gl.xformatter = LONGITUDE_FORMATTER gl.yformatter = LATITUDE_FORMATTER # make colorbar legend for figure mmb = view.pcolormesh(tempx, tempy, temp, cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.65, pad=0.06, boundaries=levels) #view.set_title('Srednja temperatura') # TODO: decrease borders, check does it works?? # fig.tight_bbox() # fig.savefig(mapname + '.png', bbox_inches='tight') LOGGER.info('Map figure %s created.', (mapname)) plt.close('all') return fig
def plot_map_temperature(proj, point_locs, df_t, area='EU', west=-5.5, east=32, south=42, north=62, fonts=14, cm='gist_ncar', path=None, SLP=False): if path is None: # set up the paths and test for existence path = expanduser('~') + '/Documents/Metar_plots' try: os.listdir(path) except FileNotFoundError: os.mkdir(path) else: path = path df = df_t plt.rcParams['savefig.dpi'] = 300 # ========================================================================= # Create the figure and an axes set to the projection. fig = plt.figure(figsize=(20, 16)) ax = fig.add_subplot(1, 1, 1, projection=proj) if area == 'Antarctica': df = df.loc[df['latitude'] < north] ax.set_extent([-180, 180, -90, -60], ccrs.PlateCarree()) theta = np.linspace(0, 2 * np.pi, 100) center, radius = [0.5, 0.5], 0.5 verts = np.vstack([np.sin(theta), np.cos(theta)]).T circle = mpath.Path(verts * radius + center) ax.set_boundary(circle, transform=ax.transAxes) elif area == 'Arctic': df = df.loc[df['latitude'] > south] ax.set_extent([-180, 180, 60, 90], ccrs.PlateCarree()) theta = np.linspace(0, 2 * np.pi, 100) center, radius = [0.5, 0.5], 0.5 verts = np.vstack([np.sin(theta), np.cos(theta)]).T circle = mpath.Path(verts * radius + center) ax.set_boundary(circle, transform=ax.transAxes) else: ax.set_extent((west, east, south, north)) # Set up a cartopy feature for state borders. state_boundaries = feat.NaturalEarthFeature(category='cultural', name='admin_0_countries', scale='10m', facecolor='#d8dcd6', alpha=0.5) ax.coastlines(resolution='10m', zorder=1, color='black') ax.add_feature(state_boundaries, zorder=1, edgecolor='black') # ax.add_feature(cartopy.feature.OCEAN, zorder=0) # Set plot bounds # reset index for easier loop df = df.dropna(how='any', subset=['TT']) df = df.reset_index() cmap = matplotlib.cm.get_cmap(cm) norm = matplotlib.colors.Normalize(vmin=-30.0, vmax=30.0) # Start the station plot by specifying the axes to draw on, as well as the # lon/lat of the stations (with transform). We also the fontsize to 12 pt. index = 0 a = np.arange(-30, 30, 1) for x in a: if index == 0: df_min = df.loc[df['TT'] < min(a)] df_max = df.loc[df['TT'] > max(a)] j = 0 list_ex = [min(a) - 5, max(a) + 5] for arr in [df_min, df_max]: stationplot = StationPlot(ax, arr['longitude'], arr['latitude'], clip_on=True, transform=ccrs.PlateCarree(), fontsize=fonts) Temp = stationplot.plot_parameter('NW', arr['TT'], color=cmap(norm(list_ex[j]))) try: Temp.set_path_effects([ path_effects.Stroke(linewidth=1.5, foreground='black'), path_effects.Normal() ]) except AttributeError: pass j += 1 # slice out values between x and x+1 df_cur = df.loc[(df['TT'] < x + 1) & (df['TT'] >= x)] stationplot = StationPlot(ax, df_cur['longitude'], df_cur['latitude'], clip_on=True, transform=ccrs.PlateCarree(), fontsize=fonts) # plot the sliced values with a different color for each loop Temp = stationplot.plot_parameter('NW', df_cur['TT'], color=cmap(norm(x + 0.5))) try: Temp.set_path_effects([ path_effects.Stroke(linewidth=1.5, foreground='black'), path_effects.Normal() ]) except AttributeError: pass print('x={} done correctly '.format(x)) index += 1 # fontweight = 'bold' # More complex ex. uses custom formatter to control how sea-level pressure # values are plotted. This uses the standard trailing 3-digits of # the pressure value in tenths of millibars. stationplot = StationPlot(ax, df['longitude'].values, df['latitude'].values, clip_on=True, transform=ccrs.PlateCarree(), fontsize=fonts) try: u, v = wind_components(((df['ff'].values) * units('knots')), (df['dd'].values * units.degree)) cloud_frac = df['cloud_cover'] if area != 'Arctic': stationplot.plot_barb(u, v, zorder=1000, linewidth=2) stationplot.plot_symbol('C', cloud_frac, sky_cover) # stationplot.plot_text((2, 0), df['Station']) for val in range(0, 2): wx = df[['ww', 'StationType']] if val == 0: # mask all the unmanned stations wx['ww'].loc[wx['StationType'] > 3] = np.nan wx2 = wx['ww'].fillna(00).astype(int).values.tolist() stationplot.plot_symbol('W', wx2, current_weather, zorder=2000) else: # mask all the manned stations wx['ww'].loc[(wx['StationType'] <= 3)] = np.nan # mask all reports smaller than 9 # =7 is an empty symbol! wx['ww'].loc[wx['ww'] <= 9] = 7 wx2 = wx['ww'].fillna(7).astype(int).values.tolist() stationplot.plot_symbol('W', wx2, current_weather_auto, zorder=2000) # print(u, v) except (ValueError, TypeError) as error: pass if SLP is True: lon = df['longitude'].loc[(df.PressureDefId == 'mean sea level') & (df.Hp <= 750)].values lat = df['latitude'].loc[(df.PressureDefId == 'mean sea level') & (df.Hp <= 750)].values xp, yp, _ = proj.transform_points(ccrs.PlateCarree(), lon, lat).T sea_levelp = df['SLP'].loc[(df.PressureDefId == 'mean sea level') & (df.Hp <= 750)] x_masked, y_masked, pres = remove_nan_observations( xp, yp, sea_levelp.values) slpgridx, slpgridy, slp = interpolate_to_grid(x_masked, y_masked, pres, interp_type='cressman', search_radius=400000, rbf_func='quintic', minimum_neighbors=1, hres=100000, rbf_smooth=100000) Splot_main = ax.contour(slpgridx, slpgridy, slp, colors='k', linewidths=2, extent=(west, east, south, north), levels=list(range(950, 1050, 10))) plt.clabel(Splot_main, inline=1, fontsize=12, fmt='%i') Splot = ax.contour(slpgridx, slpgridy, slp, colors='k', linewidths=1, linestyles='--', extent=(west, east, south, north), levels=[ x for x in range(950, 1050, 1) if x not in list(range(950, 1050, 10)) ]) plt.clabel(Splot, inline=1, fontsize=10, fmt='%i') # stationplot.plot_text((2, 0), df['Station']) # Also plot the actual text of the station id. Instead of cardinal # directions, plot further out by specifying a location of 2 increments # in x and 0 in y.stationplot.plot_text((2, 0), df['station']) if (area == 'Antarctica' or area == 'Arctic'): plt.savefig(path + '/CURR_SYNOP_color_' + area + '.png', bbox_inches='tight', pad_inches=0) else: plt.savefig(path + '/CURR_SYNOP_color_' + area + '.png', bbox_inches='tight', transparent="True", pad_inches=0)
def plot_upper_air_contour(data, date, p_lvl): ##!#cntr2 = ContourPlot() ##!#cntr2.data = data.to_xarray() ##!#cntr2.field = 'height' ##!#cntr2.level = int(p_lvl) * units.hPa ##!#cntr2.time = date ##!#cntr2.contours = list(range(0, 10000, 120)) ##!#cntr2.linecolor = 'black' ##!#cntr2.linestyle = 'solid' ##!#cntr2.clabels = True ##!#cfill = FilledContourPlot() ##!#cfill.data = data.to_xarray() ##!#cfill.field = 'speed' ##!#cfill.level = int(p_lvl) * units.hPa ##!#cfill.time = date ##!#cfill.contours = list(range(10, 201, 20)) ##!#cfill.colormap = 'BuPu' ##!#cfill.colorbar = 'horizontal' ##!#cfill.plot_units = 'knot' ##!#panel = MapPanel() ##!#panel.area = [-125, -74, 20, 55] ##!#panel.projection = 'lcc' ##!#panel.layers = ['states', 'coastline', 'borders'] ##!#panel.title = f'{cfill.level.m}-hPa Heights and Wind Speed at {date}' ##!#panel.plots = [cfill, cntr2] ##!# ##!#pc = PanelContainer() ##!#pc.size = (15, 15) ##!#pc.panels = [panel] ##!# ##!#pc.show() data = data.dropna() local_lon = data['longitude'][data['pressure'] == float(p_lvl)].to_numpy() local_lat = data['latitude'][data['pressure'] == float(p_lvl)].to_numpy() local_spd = data['speed'][data['pressure'] == float(p_lvl)].to_numpy() local_dir = data['direction'][data['pressure'] == float(p_lvl)].to_numpy() local_hgt = data['height'][data['pressure'] == float(p_lvl)].to_numpy() local_tmp = data['temperature'][data['pressure'] == float( p_lvl)].to_numpy() # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = # # Objectively analyze the data # # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = test_lons, test_lats, test_hgt = remove_nan_observations( local_lon, local_lat, local_hgt) test_lons, test_lats, test_tmp = remove_nan_observations( local_lon, local_lat, local_tmp) test_lons, test_lats, test_wspd = remove_nan_observations( local_lon, local_lat, local_spd) test_lons, test_lats, test_wdir = remove_nan_observations( local_lon, local_lat, local_dir) print(test_hgt) print(test_lons) print(test_lats) # Grid the data # ------------- glon, glat, ghgt = interpolate_to_grid(test_lons, test_lats, test_hgt, interp_type='linear', hres=1) glon, glat, gtmp = interpolate_to_grid(test_lons, test_lats, test_tmp, interp_type='linear', hres=1) glon, glat, gwspd = interpolate_to_grid(test_lons, test_lats, test_wspd, interp_type='linear', hres=1) glon, glat, gwdir = interpolate_to_grid(test_lons, test_lats, test_wdir, interp_type='linear', hres=1) # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = # # Generate the figure # # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = # Create a new figure. The dimensions here give a good aspect ratio fig = plt.figure() datacrs = ccrs.PlateCarree() mapcrs = ccrs.AlbersEqualArea(central_longitude=-100) #mapcrs = ccrs.LambertConformal() ax = fig.add_subplot(1, 1, 1, projection=mapcrs) levels = contour_levels[int(p_lvl)] print(levels) #levels = np.arange(4980, 5820, 60) ax.contour(glon, glat, ghgt, levels=levels, transform=datacrs) ax.coastlines() ax.add_feature(cfeature.STATES) ax.set_title(date.strftime('%Y-%m-%d %H:%M')) plt.show()
#x1= xx.loc[0] data1 = pd.read_csv('/home/meteo/Documents/Master_rad/CARPATGRID_TA_M.ser', sep='\s+') #y = int(input('Unesite godinu:')) #m = int(input('Unesite mesec:')) y = 2000 m = 3 x1 = data1.loc[y, m] x_masked, y_masked, t = remove_nan_observations(xp, yp, x1.values) tempx, tempy, temp = interpolate_to_grid(x_masked, y_masked, t, interp_type='cressman', minimum_neighbors=8, search_radius=150000, hres=50000) temp = np.ma.masked_where(np.isnan(temp), temp) levels = list(range(-20, 20, 1)) cmap = plt.get_cmap('viridis') norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) fig = plt.figure(figsize=(20, 10)) view = fig.add_subplot(1, 1, 1, projection=to_proj) view.set_extent([22.7, 19, 46.6, 44.1]) view.add_feature(cfeature.STATES.with_scale('50m'))
def func_map_gwlevel_in2D(df, domain, levels, opt_contour, show_well_loc, odir, well_type, dataset_in): # Plot fig, ax = plt.subplots() fig.set_size_inches(8, 6.5) x = df.XCoor y = df.YCoor #val = df.WLE val = df['Altitude'] - df['Static level'] print(f'Depth min/max (meter): {val.min()}, {val.max()} \n') xmin, xmax, ymin, ymax = domain # map = Basemap(llcrnrlon=xmin, llcrnrlat=ymin, urcrnrlon=xmax, urcrnrlat=ymax, # resolution='h', # projection='lcc', lat_1=10, lat_2=10, lon_0=-95) # lat_1=17, lat_2=10, map = Basemap(llcrnrlon=xmin, llcrnrlat=ymin, urcrnrlon=xmax, urcrnrlat=ymax, projection='lcc', resolution='l', lat_1=10, lat_2=10, lon_0=-95, epsg=4269) #map.shadedrelief() # # map.etopo() map.drawcountries() map.drawstates() # map.drawmapboundary(fill_color='#46bcec') # Plot BACKGROUND ========================================================= showbg = False if showbg == True: # m.arcgisimage(service='ESRI_Imagery_World_2D', # xpixels=1500, ypixel=None, dpi=300, verbose=True, alpha=1) # Background map.arcgisimage(service='ESRI_Imagery_World_2D', xpixels=1500, dpi=300, verbose=True, alpha=0.25) # Background cmap = plt.get_cmap('jet') # RdYlBu, gist_rainbow, bwr, jet, BuGn_r, norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) x1, y1 = map(x.values, y.values) # Delete bad locations # bad_locx = np.where(x1 > 1e9) # delete bad records # bad_locy = np.where(y1 > 1e9) # delete #x1 = np.delete(x1, bad_locx) #y1 = np.delete(y1, bad_locx) #print(x1, y1) val = val.to_numpy() #val = np.delete(val, bad_locx) print('Shape of x1, y1 and v1') print(x1.shape, y1.shape, val.shape) gx, gy, img1 = interpolate_to_grid( x1, y1, val, interp_type='barnes', hres=5000, search_radius=40000) # work: 10000/40000 img1 = np.ma.masked_where(np.isnan(img1), img1) print(gx.shape, gy.shape, img1.shape) # gx, gy, img1 = interpolate_to_grid( # x1, y1, val, interp_type='linear', hres=20000) # #img1 = np.ma.masked_where(np.isnan(img1), img1) if opt_contour == '_contour_': # RdYlBu, gist_rainbow, bwr, jet #print(gx.shape, gy.shape) s = ax.pcolormesh(gx, gy, img1, cmap=cmap, norm=norm, alpha=1) # # Interpolate #rbf = scipy.interpolate.Rbf(gx, gy, val, function='linear') #zi = rbf(gx, gy) # s = plt.imshow(val, vmin=val.min(), vmax=val.max(), origin='lower', # extent=[gx.min(), gx.max(), gy.min(), gy.max()]) #CS = plt.contour(gx, gy, gaussian_filter(img1, 5.), 4, colors='k',interpolation='none') fig.colorbar(s, shrink=.4, pad=0.01, boundaries=levels) show_well_loc = False # map.drawmapscale(-1.0, 10, 4.1, 14, 500) # Why not working now? # Show well locations if show_well_loc: # ax.scatter(x1,y1,s=1,c='k',marker='o') plt.plot(x1, y1, 'o', color='gray', markersize=3, linewidth=0.5, markerfacecolor='white', markeredgecolor='k', markeredgewidth=0.5, alpha=0.9) # drawmapscale(lon, lat, lon0, lat0, length, barstyle=’simple’, units=’km’, # fontsize=9, yoffset=None, labelstyle=’simple’, fontcolor=’k’, fillcolor1=’w’, # fillcolor2=’k’, ax=None, format=’%d’, zorder=None) ofile = odir + 'map gwdepth ' + dataset_in + ' for ' + \ well_type + ' wells ' + opt_contour + '.png' fig.savefig(ofile, dpi=300, transparent=False, bbox_inches='tight') print(f'\nThe figure was saved at {ofile} \n') plt.show()
def main(): ### START OF USER SETTINGS BLOCK ### # FILE/DATA SETTINGS # file path to input datafile = '/home/jgodwin/python/sfc_observations/surface_observations.txt' timefile = '/home/jgodwin/python/sfc_observations/validtime.txt' # MAP SETTINGS # map names (for tracking purposes) maps = ['CONUS','Texas','Floater 1'] restart_domain = [True,False] # map boundaries west = [-120,-108,-108] east = [-70,-93,-85] south = [20,25,37] north = [50,38,52] # OUTPUT SETTINGS # save directory for output savedir = '/var/www/html/images/' # filenames ("_[variable].png" will be appended, so only a descriptor like "conus" is needed) savenames = ['conus','texas','floater1'] # TEST MODE SETTINGS test = False testnum = 3 ### END OF USER SETTINGS BLOCK ### for i in range(len(maps)): if test and i != testnum: continue print(maps[i]) # create the map projection cenlon = (west[i] + east[i]) / 2.0 cenlat = (south[i] + north[i]) / 2.0 sparallel = cenlat if cenlat > 0: cutoff = -30 flip = False elif cenlat < 0: cutoff = 30 flip = True if restart_domain: to_proj = ccrs.LambertConformal(central_longitude=cenlon,central_latitude=cenlat,standard_parallels=[sparallel],cutoff=cutoff) # open the data vt = open(timefile).read() with open(datafile) as f: data = pd.read_csv(f,header=0,names=['siteID','lat','lon','elev','slp','temp','sky','dpt','wx','wdr',\ 'wsp'],na_values=-99999) # filter data by lat/lon data = data[(data['lat'] >= south[i]-2.0) & (data['lat'] <= north[i]+2.0) & (data['lon'] >= west[i]-2.0)\ & (data['lon'] <= east[i]+2.0)] # remove questionable data data = data[(cToF(data['temp']) <= 120) & (cToF(data['dpt']) <= 80)] # project lat/lon onto final projection print("Creating map projection.") lon = data['lon'].values lat = data['lat'].values xp, yp, _ = to_proj.transform_points(ccrs.Geodetic(), lon, lat).T # remove missing data from pressure and interpolate # we'll give this a try and see if it can help with my CPU credit problem if restart_domain: print("Performing Cressman interpolation.") x_masked, y_masked, pres = remove_nan_observations(xp, yp, data['slp'].values) slpgridx, slpgridy, slp = interpolate_to_grid(x_masked, y_masked, pres, interp_type='cressman', minimum_neighbors=1, search_radius=400000, hres=100000) # get wind information and remove missing data wind_speed = (data['wsp'].values * units('knots')) wind_dir = data['wdr'].values * units.degree good_indices = np.where((~np.isnan(wind_dir)) & (~np.isnan(wind_speed))) x_masked = xp[good_indices] y_masked = yp[good_indices] wind_speed = wind_speed[good_indices] wind_dir = wind_dir[good_indices] u, v = wind_components(wind_speed, wind_dir) windgridx, windgridy, uwind = interpolate_to_grid(x_masked, y_masked, np.array(u), interp_type='cressman', search_radius=400000, hres=100000) _, _, vwind = interpolate_to_grid(x_masked, y_masked, np.array(v), interp_type='cressman', search_radius=400000, hres=100000) # get temperature information data['temp'] = cToF(data['temp']) x_masked, y_masked, t = remove_nan_observations(xp, yp, data['temp'].values) tempx, tempy, temp = interpolate_to_grid(x_masked, y_masked, t, interp_type='cressman', minimum_neighbors=3, search_radius=200000, hres=18000) temp = np.ma.masked_where(np.isnan(temp), temp) # get dewpoint information data['dpt'] = cToF(data['dpt']) x_masked,y_masked,td = remove_nan_observations(xp,yp,data['dpt'].values) dptx,dpty,dewp = interpolate_to_grid(x_masked,y_masked,td,interp_type='cressman',\ minimum_neighbors=3,search_radius=200000,hres=18000) dewp = np.ma.masked_where(np.isnan(dewp),dewp) # interpolate wind speed x_masked,y_masked,wspd = remove_nan_observations(xp,yp,data['wsp'].values) wspx,wspy,speed = interpolate_to_grid(x_masked,y_masked,wspd,interp_type='cressman',\ minimum_neighbors=3,search_radius=200000,hres=18000) speed = np.ma.masked_where(np.isnan(speed),speed) # derived values # station pressure data['pres'] = stationPressure(data['slp'],data['elev']) # theta-E data['thetae'] = equivalent_potential_temperature(data['pres'].values*units.hPa,data['temp'].values*units.degF,data['dpt'].values*units.degF) x_masked,y_masked,thetae = remove_nan_observations(xp,yp,data['thetae'].values) thex,they,thte = interpolate_to_grid(x_masked,y_masked,thetae,interp_type='cressman',\ minimum_neighbors=3,search_radius=200000,hres=18000) thte = np.ma.masked_where(np.isnan(thte),thte) # mixing ratio relh = relative_humidity_from_dewpoint(data['temp'].values*units.degF,data['dpt'].values*units.degF) mixr = mixing_ratio_from_relative_humidity(relh,data['temp'].values*units.degF,data['pres'].values*units.hPa) * 1000.0 x_masked,y_masked,mixrat = remove_nan_observations(xp,yp,mixr) mrx,mry,mrat = interpolate_to_grid(x_masked,y_masked,mixrat,interp_type='cressman',\ minimum_neighbors=3,search_radius=200000,hres=18000) mrat = np.ma.masked_where(np.isnan(mrat),mrat) # set up the state borders state_boundaries = cfeature.NaturalEarthFeature(category='cultural',\ name='admin_1_states_provinces_lines',scale='50m',facecolor='none') # SCALAR VARIABLES TO PLOT # variable names (will appear in plot title) variables = ['Temperature','Dewpoint','Wind Speed','Theta-E','Mixing Ratio'] # units (for colorbar label) unitlabels = ['F','F','kt','K','g/kg'] # list of actual variables to plot vardata = [temp,dewp,speed,thte,mrat] # tag in output filename varplots = ['temp','dewp','wspd','thte','mrat'] # levels: (lower,upper,step) levs = [[-20,105,5],[30,85,5],[0,70,5],[250,380,5],[0,22,2]] # colormaps colormaps = ['hsv_r','Greens','plasma','hsv_r','Greens'] for j in range(len(variables)): print("\t%s" % variables[j]) fig = plt.figure(figsize=(20, 10)) view = fig.add_subplot(1, 1, 1, projection=to_proj) # set up the map and plot the interpolated grids levels = list(range(levs[j][0],levs[j][1],levs[j][2])) cmap = plt.get_cmap(colormaps[j]) norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) labels = variables[j] + " (" + unitlabels[j] + ")" # add map features view.set_extent([west[i],east[i],south[i],north[i]]) view.add_feature(state_boundaries,edgecolor='black') view.add_feature(cfeature.OCEAN,zorder=-1) view.add_feature(cfeature.COASTLINE,zorder=2) view.add_feature(cfeature.BORDERS, linewidth=2,edgecolor='black') # plot the sea-level pressure cs = view.contour(slpgridx, slpgridy, slp, colors='k', levels=list(range(990, 1034, 4))) view.clabel(cs, inline=1, fontsize=12, fmt='%i') # plot the scalar background mmb = view.pcolormesh(tempx, tempy, vardata[j], cmap=cmap, norm=norm) fig.colorbar(mmb, shrink=.4, orientation='horizontal', pad=0.02, boundaries=levels, \ extend='both',label=labels) # plot the wind barbs view.barbs(windgridx, windgridy, uwind, vwind, alpha=.4, length=5,flip_barb=flip) # plot title and save view.set_title('%s (shaded), SLP, and Wind (valid %s)' % (variables[j],vt)) plt.savefig('/var/www/html/images/%s_%s.png' % (savenames[i],varplots[j]),bbox_inches='tight') # close everything fig.clear() view.clear() plt.close(fig) f.close() print("Script finished.")
def plot_map_standard(proj, point_locs, df_t, area='EU', west=-9.5, east=28, south=35, north=62, fonts=14, path=None, SLP=False, gust=False): if path == None: # set up the paths and test for existence path = expanduser('~') + '/Documents/Metar_plots' try: os.listdir(path) except FileNotFoundError: os.mkdir(path) else: path = path df = df_t.loc[(df_t['longitude'] >= west - 4) & (df_t['longitude'] <= east + 4) & (df_t['latitude'] <= north + 4) & (df_t['latitude'] >= south - 4)] plt.rcParams['savefig.dpi'] = 300 # ========================================================================= # Create the figure and an axes set to the projection. fig = plt.figure(figsize=(20, 16)) ax = fig.add_subplot(1, 1, 1, projection=proj) if area == 'Antarctica': df = df.loc[df['latitude'] < north] ax.set_extent([-180, 180, -90, -60], ccrs.PlateCarree()) theta = np.linspace(0, 2 * np.pi, 100) center, radius = [0.5, 0.5], 0.5 verts = np.vstack([np.sin(theta), np.cos(theta)]).T circle = mpath.Path(verts * radius + center) ax.set_boundary(circle, transform=ax.transAxes) elif area == 'Arctic': df = df.loc[df['latitude'] > south] ax.set_extent([-180, 180, 60, 90], ccrs.PlateCarree()) theta = np.linspace(0, 2 * np.pi, 100) center, radius = [0.5, 0.5], 0.5 verts = np.vstack([np.sin(theta), np.cos(theta)]).T circle = mpath.Path(verts * radius + center) ax.set_boundary(circle, transform=ax.transAxes) else: ax.set_extent((west, east, south, north)) # Get the wind components, converting from m/s to knots as will # be appropriate for the station plot. df['dd'][df['dd'] > 360] = np.nan u, v = wind_components(df['ff'].values * units('knots'), df['dd'].values * units('deg')) cloud_frac = df['cloud_cover'] # Change the DPI of the resulting figure. Higher DPI drastically improves # look of the text rendering. # Set up a cartopy feature for state borders. # state_boundaries = feat.NaturalEarthFeature(category='cultural', # name='admin_0_countries', # scale='10m', # facecolor='#d8dcd6', # alpha=0.5) # ax.coastlines(resolution='10m', zorder=0, color='black') # ax.add_feature(feat.LAND) ax.add_feature(feat.COASTLINE.with_scale('10m'), zorder=2, edgecolor='black') ax.add_feature(feat.OCEAN.with_scale('50m'), zorder=0) ax.add_feature(feat.STATES.with_scale('10m'), zorder=1, facecolor='white', edgecolor='#5e819d') # ax.add_feature(cartopy.feature.OCEAN, zorder=0) # Set plot bounds # Start the station plot by specifying the axes to draw on, as well as the # lon/lat of the stations (with transform). We also the fontsize to 12 pt. stationplot = StationPlot(ax, df['longitude'].values, df['latitude'].values, clip_on=True, transform=ccrs.PlateCarree(), fontsize=fonts) # Plot the temperature and dew point to the upper and lower left, # respectively, of the center point. Each one uses a different color. Temp = stationplot.plot_parameter('NW', df['TT'], color='#fd3c06', fontweight='bold', zorder=3) Td = stationplot.plot_parameter('SW', df['TD'], color='#01ff07') if gust == True: maxff = stationplot.plot_parameter('SE', df['max_gust'], color='#cb416b', fontweight='bold', zorder=3) maxff.set_path_effects([ path_effects.Stroke(linewidth=1.5, foreground='black'), path_effects.Normal() ]) # fontweight = 'bold' # More complex ex. uses custom formatter to control how sea-level pressure # values are plotted. This uses the standard trailing 3-digits of # the pressure value in tenths of millibars. if (area != 'Antarctica' and area != 'Arctic'): p = stationplot.plot_parameter( 'NE', df['SLP'], formatter=lambda v: format(10 * v, '.0f')[-3:], color="#a2cffe") for x in [Temp, Td, p]: x.set_path_effects([ path_effects.Stroke(linewidth=1.5, foreground='black'), path_effects.Normal() ]) else: for x in [Temp, Td]: x.set_path_effects([ path_effects.Stroke(linewidth=1.5, foreground='black'), path_effects.Normal() ]) # Add wind barbs stationplot.plot_barb(u, v, zorder=3, linewidth=2) # Plot the cloud cover symbols in the center location. This uses the codes # made above and uses the `sky_cover` mapper to convert these values to # font codes for the weather symbol font. stationplot.plot_symbol('C', cloud_frac, sky_cover) # Same this time, but plot current weather to the left of center, using the # `current_weather` mapper to convert symbols to the right glyphs. for val in range(0, 2): wx = df[['ww', 'StationType']] if val == 0: # mask all the unmanned stations wx['ww'].loc[wx['StationType'] > 3] = np.nan wx2 = wx['ww'].fillna(00).astype(int).values.tolist() stationplot.plot_symbol('W', wx2, current_weather, zorder=4) else: # mask all the manned stations wx['ww'].loc[(wx['StationType'] <= 3)] = np.nan # mask all reports smaller than 9 # =7 is an empty symbol! wx['ww'].loc[wx['ww'] <= 9] = 7 wx2 = wx['ww'].fillna(7).astype(int).values.tolist() stationplot.plot_symbol('W', wx2, current_weather_auto, zorder=4) if SLP == True: lon = df['longitude'].loc[(df.PressureDefId == 'mean sea level') & (df.Hp <= 750)].values lat = df['latitude'].loc[(df.PressureDefId == 'mean sea level') & (df.Hp <= 750)].values xp, yp, _ = proj.transform_points(ccrs.PlateCarree(), lon, lat).T sea_levelp = df['SLP'].loc[(df.PressureDefId == 'mean sea level') & (df.Hp <= 750)] x_masked, y_masked, pres = remove_nan_observations( xp, yp, sea_levelp.values) slpgridx, slpgridy, slp = interpolate_to_grid(x_masked, y_masked, pres, interp_type='cressman', search_radius=400000, rbf_func='quintic', minimum_neighbors=1, hres=100000, rbf_smooth=100000) Splot_main = ax.contour(slpgridx, slpgridy, slp, colors='k', linewidths=2, extent=(west, east, south, north), levels=list(range(950, 1050, 10))) plt.clabel(Splot_main, inline=1, fontsize=12, fmt='%i') Splot = ax.contour(slpgridx, slpgridy, slp, colors='k', linewidths=1, linestyles='--', extent=(west, east, south, north), levels=[ x for x in range(950, 1050, 1) if x not in list(range(950, 1050, 10)) ]) plt.clabel(Splot, inline=1, fontsize=10, fmt='%i') # stationplot.plot_text((2, 0), df['Station']) # Also plot the actual text of the station id. Instead of cardinal # directions, plot further out by specifying a location of 2 increments # in x and 0 in y.stationplot.plot_text((2, 0), df['station']) if (area == 'Antarctica' or area == 'Arctic'): plt.savefig(path + '/CURR_SYNOP_' + area + '.png', bbox_inches='tight', pad_inches=0) else: plt.savefig(path + '/CURR_SYNOP_' + area + '.png', bbox_inches='tight', transparent="True", pad_inches=0)