def test_remove_nan_observations(test_coords): r"""Test remove observations equal to nan function.""" x, y = test_coords[0], test_coords[1] z = np.array([np.nan, np.nan, np.nan, 1, 1, 1, 1, 1, 1, 1]) x_, y_, z_ = remove_nan_observations(x, y, z) truthx = np.array([10, 52, 53, 98, 34, 15, 58]) truthy = np.array([94, 98, 66, 14, 24, 60, 16]) truthz = np.array([1, 1, 1, 1, 1, 1, 1]) assert_array_almost_equal(truthx, x_) assert_array_almost_equal(truthy, y_) assert_array_almost_equal(truthz, z_)
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
usecols=(2, 3, 4, 5, 18, 19), names=[ 'latitude', 'longitude', 'slp', 'temperature', 'wind_dir', 'wind_speed' ], 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
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
df = pd.read_sql_query(query, cnx) tacka = '''SELECT id, lon, lat,country,altitude FROM %s;''' % 'grid1' 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
def period_year_prec(year,year1,lon,lat,inter): cnx = sqlite3.connect(DB1) cursor = cnx.cursor() table = 'yearly' year = int(year) year1 = int(year1) newlist = [] newlist1 = [] newlist2 = [] for i in range (year,year1+1,1): newlist2.append(i) query = ''' SELECT dates, cell, prec FROM %s WHERE dates = "%s" ; ''' % (table, i) df = pd.read_sql_query(query, cnx) tacka = '''SELECT id, lon, lat,country,altitude FROM %s;''' % 'grid1' grid1 = pd.read_sql_query(tacka, cnx) lon_n = grid1['lon'].values lat_n = grid1['lat'].values prec = df['prec'].values x_masked, y_masked, prec_p = remove_nan_observations(lon_n, lat_n, prec) lon = float(lon) lat = float(lat) xy = np.vstack([x_masked,y_masked]).T xi = np.vstack([lon,lat]).T if inter == "linear": inter_point = interpolate_to_points(xy,prec_p,xi, interp_type='linear') elif inter == "cressman": inter_point =interpolate_to_points(xy,prec_p,xi, interp_type='cressman', minimum_neighbors=3, gamma=0.25, kappa_star=5.052, search_radius=None, rbf_func='linear', ) elif inter == "barnes": inter_point =interpolate_to_points(xy,prec_p,xi, interp_type='cressman', minimum_neighbors=3, gamma=0.25, kappa_star=5.052, search_radius=None, rbf_func='linear', ) for y in inter_point: newlist.append(y) for z in xi: newlist1.append(z) xi= str(xi) newlist_fix = [str(a) for a in newlist] #sarr1 = [str(a) for a in newlist1] d = {'Year':newlist2,'Lon&Lat':newlist1, 'Rainfall':newlist} #d = {'Year':newlist2,'Rainfall':newlist_fix} df = pd.DataFrame(d) return (df)
return None return lon, lat, value 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>`_
print(e) return None return lon, lat, value 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>`_
def merge_hgt_grad(grid, param, st_data, lat, lon, st_lats, st_lons, st_height, topo_height, savedir, plot_opt=False, interp = None, distance = None, variogram_model = None, neighbor=None): ''' This function merges gridded temperature observations with additional stationdata with the following steps: 1. Calculate monthly height gradient of gridded data with linear regression 2. Remove height dependency from gridded and station data 3. Interpolation of the gridded observations to the stations using nearest neighbour interpolation method 4. Calculate residuals (difference between station data and the gridded observations interpolated to the stations) at the stations 5. Interpolate the residuals to the grid defined by 'lat' and 'lon' (grid of the gridded observations) Default interpolation algorithm: Inverse Distance Weighting Interpolation (idw) Other method: Ordinary Kriging https://pykrige.readthedocs.io/en/latest/generated/pykrige.ok.OrdinaryKriging.html 6. Merge interpolated residuals with grid (withoud height dependency) 7. Add height dependency back to the merged grid 8. Optional: Plot mean original and new observations for comparison **************************************************** Input data: grid dataset of gridded observations; dimensions: (time,lat,lon) param string with name of the parameter (tasmax or tasmin) st_data dataframe with the station timeseries; dimensions: (time, station) lat 1d array with the latitude values of grid_data lon 1d array with the latitude values of grid_data st_lats 1d array with the latitude values of the stations st_lons 1d array with the longitude values of the stations st_height 1d array with the height values of the stations topo_height 2d array with gridded height data; dimensions: (lat, lon) plot_opt default: False; set True to create simple plots of mean original and new observations interp specifies the interpolation algorithm that should be used to interpolate the residuals default: 'idw'; other options: 'kriging', 'rbf' Parameters for inverse distance interpolation: neighbor Minimum number of neighbors needed to perform barnes or cressman interpolation for a point. Default is 3. See also: https://unidata.github.io/MetPy/latest/api/generated/metpy.interpolate.inverse_distance_to_grid.html distance distance (in km) at which the weights should be 0 (default: 50km) Parameters for kriging: variogram_model variogram model for the kriging interpolation: gaussian(default), spherical, exponential or linear Output data: grid_merge 3d array with the merged observations; dimensions: (time,lat,lon) ''' #%% # 1. Remove height dependency for calculating residuals # 1.1. Get mean height gradient from gridded and station data # Calculate mean monthly height gradient and detrend data # add coordinate month to dataframe (for monthly selection of data) grid.coords['month'] = grid['time.month'] # empty array that gets filled with monthly mean gradient for every gridpoint grad = np.zeros((12,1))*np.nan c = np.zeros((12,1))*np.nan grid_det = grid.copy(deep=True) topo_1d = topo_height.reshape(np.size(topo_height),1) st_det = st_data.copy(deep=True) for month in range(0,12): # get data for month month_data = grid[param][grid['month']==month+1,:,:] month_mean = month_data.mean(dim='time') grid_1d = month_mean.data.reshape(np.size(month_mean),1) grad[month], c[month] = linreg(grid_1d, topo_1d, plot_opt=False) # station data month_st = st_data[st_data.index.month==month+1] # 2. Remove height dependency from data grid_det[param][grid['month']==month+1,:,:] = month_data - (grad[month]* topo_height) st_det[st_data.index.month==month+1] = month_st - (grad[month]* st_height) #%% # 3. Interpolate gridded observations to stations (nearest neighbour interpolation) grid_at_st = np.zeros((grid[param].shape[0], st_data.shape[1])) # loop over every day for ii in range(grid[param].shape[0]): grid_at_st[ii, :] = sp.interpolate.interpn((lat, lon), grid_det[param][ii, :, :].data, (st_lats, st_lons), method='linear') # 4. Calculate residuals at stations: station data - gridded observations interpolated to stations res_data = st_det.values - grid_at_st #%% # 5. Interpolate residuals res_int = np.zeros(grid[param].shape) #np.zeros_like(grid_data) lons, lats = np.meshgrid(lon, lat) if (interp == 'idw'): # inverse distance interpolation (for small number of stations) for x, val in enumerate(res_data): # print(val, x) lon_masked, lat_masked, val_masked = remove_nan_observations(st_lons, st_lats, val) res_int[x,:,:] = inverse_distance_to_grid(lon_masked, lat_masked, val_masked, lons, lats, r=distance, min_neighbors=neighbor ) else: for i in range(grid[param].shape[0]): # check for nans in data (missing values) mask = ~np.isnan(res_data[i, :]) if interp == 'rbf': intfunc_rbf = sp.interpolate.Rbf(st_lons[mask], st_lats[mask], res_data[i,mask], function='gaussian') res_int[i,:,:] = intfunc_rbf(lons, lats) elif interp == 'kriging': # ordinary kriging res_int[i,:,:] = kriging(st_lons[mask], st_lats[mask], res_data[i, mask], lon, lat, var_model = variogram_model) else: print('interpolation algorithm '+str(interp)+' unknown!') print('... aborting interpolation - no merged observations created!') return np.nan # 6. Calculate final field res_int[np.isnan(res_int)] = 0 # 6.1 Merge residuals with gridded data grid_merge_det = grid_det[param].data + res_int # 6.2 Add height dependency grid_merge = np.ones_like(grid_merge_det)*np.nan for month in range(0,12): grid_merge[grid['month']==month+1,:,:] = grid_merge_det[grid['month']==month+1,:,:] + (grad[month]*topo_height) # check for unrealistic values if (param == 'rsds') | (param == 'sfcWind') | (param == 'hurs'): grid_merge[grid_merge < 0] = 0 if param == 'hurs': grid_merge[grid_merge < 10] = 10 grid_merge[grid_merge > 100] = 100 print(grid_merge) if plot_opt == True: plot_result(param, grid_merge, grid[param].values, savedir+'/PLOTS/', lat, lon, st_lats, st_lons, '1981', '2010', interp,'original obs.', 'improved obs.', scatter=True) return grid_merge
def merge_no_grad(grid_data, param, st_data, lat, lon, st_lats, st_lons, savedir, plot_opt = False, interp = None, distance = None, variogram_model = None, neighbor=None): ''' This function merges gridded observations with additional stationdata with the following steps: 1. Interpolation of the gridded observations to the stations using nearest neighbour interpolation method 2. Calculate residuals (difference between station data and the gridded observations interpolated to the stations) at the stations 3. Interpolate the residuals to the grid defined by 'lat' and 'lon' (grid of the gridded observations) Interpolation algorithm: Ordinary kriging https://pykrige.readthedocs.io/en/latest/generated/pykrige.ok.OrdinaryKriging.html 4. Merge the interpolated residuals with the original gridded observations to get the new gridded observations 5. Optional: Plot mean original and new observations for comparison **************************************************** Input data: grid_data 3d array of gridded observations; dimensions: (time,lat,lon) param string with name of the parameter st_data 2d array with the station timeseries; dimensions: (time, station) lat 1d array with the latitude values of grid_data lon 1d array with the latitude values of grid_data st_lats 1d array with the latitude values of the stations st_lons 1d array with the longitude values of the stations plot_opt default: False; set True to create simple plots of mean original and new observations interp interpolation algorithm: kriging (default) or inverse distance (default if number of stations <=10) Parameters for inverse distance interpolation: neighbor Minimum number of neighbors needed to perform barnes or cressman interpolation for a point. Default is 3. See also: https://unidata.github.io/MetPy/latest/api/generated/metpy.interpolate.inverse_distance_to_grid.html distance distance (in km) at which the weights should be 0 (default: 50km) Parameters for kriging: variogram_model variogram model for the kriging interpolation: gaussian(default), spherical, exponential or linear Output data: grid_merge 3d array with the merged observations; dimensions: (time,lat,lon) ''' nst = np.shape(st_data)[1] # 1. Interpolate gridded observations to stations (nearest neighbour interpolation) grid_at_st = np.zeros((np.shape(grid_data)[0], nst)) # loop over every day for i in range(np.shape(grid_data)[0]): grid_at_st[i, :] = sp.interpolate.interpn((lat, lon), grid_data[i, :, :], (st_lats, st_lons), method='linear') # 2. Calculate residuals at stations: station data - gridded observations interpolated to stations res_data = st_data - grid_at_st # 3. Interpolate residuals (ordinary kriging to 0.01° grid or inverse distance) res_int = np.zeros_like(grid_data) lons, lats = np.meshgrid(lon, lat) if (interp == 'idw'): #res_int = idw_interpolation(res_data, st_lats, st_lons, lat, lon, weights = weights, distance = distance, low_lim = low_lim) for x, val in enumerate(res_data): #print(val, x) lon_masked, lat_masked, val_masked = remove_nan_observations(st_lons, st_lats, val) res_int[x,:,:] = inverse_distance_to_grid(lon_masked, lat_masked, val_masked, lons, lats, r=distance, min_neighbors=neighbor ) elif interp == 'kriging': # loop over evey day for i in range(np.shape(grid_data)[0]): # check for nans in observations mask = ~np.isnan(res_data[i, :]) # check if all observations on this day are zero if np.all(res_data[i, mask] == 0.): res_int[i, :, :] = 0. # print 'all zeros at day ', str(i) continue else: res_int[i,:,:] = kriging(st_lons[mask], st_lats[mask], res_data[i, mask], lon, lat, var_model = variogram_model) res_int[np.isnan(res_int)] = 0 # final merged grid = original observation grid + interpolated residuals grid_merge = grid_data + res_int # check for values < 0 and set them to zero grid_merge[grid_merge < 0] = 0 if plot_opt == True: plot_result(param, grid_merge, grid_data, savedir+'/PLOTS/', lat, lon, st_lats, st_lons, '1981', '2010', interp,'original obs.', 'improved obs.', scatter=True) return grid_merge
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 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
def period_daily_prec(year,month,day,year1,month1,day1,lon,lat,inter): cnx = sqlite3.connect(DB1) cursor = cnx.cursor() table = 'daily' year = int(year) year1 = int(year1) month = int(month) month1 = int(month1) day = int (day) day1 = int(day1) a = date(year, month, day) b = date(year1, month1, day1) newlist = [] newlist1 = [] newlist2 = [] for dt in rrule(DAILY, dtstart=a, until=b): i= dt.strftime("%Y-%-m-%-d") newlist2.append(i) query = ''' SELECT dates, cell, prec FROM %s WHERE dates = "%s" ; ''' % (table,i) df = pd.read_sql_query(query, cnx) tacka = '''SELECT id, lon, lat,country,altitude FROM %s;''' % 'grid1' grid1 = pd.read_sql_query(tacka, cnx) lon_n = grid1['lon'].values lat_n = grid1['lat'].values prec = df['prec'].values x_masked, y_masked, prec_p = remove_nan_observations(lon_n, lat_n, prec) lon = float(lon) lat =float(lat) xy = np.vstack([x_masked,y_masked]).T xi = np.vstack([lon,lat]).T if inter == "linear": inter_point = interpolate_to_points(xy,prec_p,xi, interp_type='linear') elif inter == "cressman": inter_point =interpolate_to_points(xy,prec_p,xi, interp_type='cressman', minimum_neighbors=3, gamma=0.25, kappa_star=5.052, search_radius=None, rbf_func='linear', rbf_smooth=0) elif inter == "barnes": inter_point =interpolate_to_points(xy,prec_p,xi, interp_type='cressman', minimum_neighbors=3, gamma=0.25, kappa_star=5.052, search_radius=None, rbf_func='linear', rbf_smooth=0) for y in inter_point: newlist.append(y) for z in xi: newlist1.append(z) xi =str(xi) newlist_fix = [str(a) for a in newlist] d = {'Year-Month-Day':newlist2,'Lon&Lat':newlist1, 'Rainfall':newlist} df = pd.DataFrame(d) return (df)
def period_month_temp(year,month,year1,month1,lon,lat,inter): cnx = sqlite3.connect(DB) cursor = cnx.cursor() table = 'monthly' year = int(year) year1 = int(year1) month = int(month) month1 = int(month1) a=[] for m in month_iter(year,month,year1,month1): temp_m = list(filter(lambda x: x != None, m)) result = '-'.join(list(map(str, temp_m))) a.append(result) newlist = [] newlist1 = [] newlist2 = [] for i in a: newlist2.append(i) query = ''' SELECT dates, cell, temp FROM %s WHERE dates = "%s" ; ''' % (table,i) df = pd.read_sql_query(query, cnx) tacka = '''SELECT id, lon, lat,country,altitude FROM %s;''' % 'grid' grid = pd.read_sql_query(tacka, cnx) lon_n = grid['lon'].values lat_n = grid['lat'].values temp = df['temp'].values x_masked, y_masked, temp_p = remove_nan_observations(lon_n, lat_n, temp) lon = float(lon) lat =float(lat) xy = np.vstack([x_masked,y_masked]).T xi = np.vstack([lon,lat]).T if inter == "linear": inter_point = interpolate_to_points(xy,temp_p,xi, interp_type='linear') elif inter == "cressman": inter_point =interpolate_to_points(xy,temp_p,xi, interp_type='cressman', minimum_neighbors=3, gamma=0.25, kappa_star=5.052, search_radius=None, rbf_func='linear', rbf_smooth=0) elif inter == "barnes": inter_point =interpolate_to_points(xy,temp_p,xi, interp_type='cressman', minimum_neighbors=3, gamma=0.25, kappa_star=5.052, search_radius=None, rbf_func='linear', rbf_smooth=0) for y in inter_point: newlist.append(y) for z in xi: newlist1.append(z) xi=str(xi) newlist_fix = [str(a) for a in newlist] d = {'Year-Month':newlist2,'Lon&Lat':newlist1,'Temperature':newlist_fix} df = pd.DataFrame(d) return (df)
def period_year_temp(year,year1,lon,lat,inter): cnx = sqlite3.connect(DB) cursor = cnx.cursor() table = 'yearly' year = int(year) year1 = int(year1) newlist = [] newlist1 = [] newlist2 = [] for i in range (year,year1+1,1): newlist2.append(i) query = ''' SELECT dates, cell, temp FROM %s WHERE dates = "%s" ; ''' % (table, i) df = pd.read_sql_query(query, cnx) tacka = '''SELECT id, lon, lat,country,altitude FROM %s;''' % 'grid' grid = pd.read_sql_query(tacka, cnx) podaci = pd.merge(df,grid,left_on='cell',right_on='id') podaci_a = podaci.drop(['cell','id','country','altitude'],axis=1) lon_n = podaci_a['lon'].values lat_n = podaci_a['lat'].values temp =podaci_a['temp'].values x_masked, y_masked, temp_p = remove_nan_observations(lon_n, lat_n, temp) lon = float(lon) lat =float(lat) xy = np.vstack([x_masked,y_masked]).T xi = np.vstack([lon,lat]).T if inter == "linear": inter_point = interpolate_to_points(xy,temp_p,xi, interp_type='linear') elif inter == "cressman": inter_point =interpolate_to_points(xy,temp_p,xi, interp_type='cressman', minimum_neighbors=3, gamma=0.25, kappa_star=5.052, search_radius=None, rbf_func='linear', rbf_smooth=0) elif inter == "barnes": inter_point =interpolate_to_points(xy,temp_p,xi, interp_type='cressman', minimum_neighbors=3, gamma=0.25, kappa_star=5.052, search_radius=None, rbf_func='linear', rbf_smooth=0) for y in inter_point: newlist.append(y) for z in xi: newlist1.append(z) xi= str(xi) newlist_fix = [str(a) for a in newlist] d = {'Year':newlist2,'Lon&Lat':newlist1,'Temperature':newlist_fix} df = pd.DataFrame(d) return (df)
# Read in data with get_test_data('station_data.txt') as f: data = pd.read_csv(f, header=0, usecols=(2, 3, 4, 5, 18, 19), names=['latitude', 'longitude', 'slp', 'temperature', 'wind_dir', 'wind_speed'], 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]
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)
query = ''' SELECT cell, prec FROM %s WHERE dates = "%s-%s"; ''' % ('monthly', i,j) df = pd.read_sql_query(query, cnx) tacka = '''SELECT id, lon, lat,country,altitude FROM %s;''' % 'grid1' grid1 = pd.read_sql_query(tacka, cnx) podaci = pd.merge(df,grid1,left_on='cell',right_on='id') podaci_a = podaci.drop(['cell','id','country','altitude'],axis=1) lon = podaci_a['lon'].values lat = podaci_a['lat'].values prec =podaci_a['prec'].values x_masked, y_masked, prec_p = remove_nan_observations(lon, lat, prec) xy = np.vstack([x_masked,y_masked]).T xi = ([inter_point_Lon,inter_point_Lat]) inter_point =interpolate_to_points(xy,prec_p,xi, interp_type='linear') # r = {'Year':i,'Lon':inter_point_Lon,'Lat':inter_point_Lat,'Prec':inter_point} # output_data = pd.DataFrame(r, columns = ['Year','Lon','Lat','Prec']) # output_data.to_csv("data.csv",index = False ) writer.writerow({'Year':i,'Lon':inter_point_Lon,'Lat':inter_point_Lat,'Prec':inter_point}) print ( i,j,inter_point_Lon,inter_point_Lat,int(inter_point))
#data1 = pd.read_fwf("CARPATGRID_TA_M.ser") #data1.rename( columns={'Unnamed: 0':'Year','Unnamed: 1':'Month'}, inplace=True ) #xx = data1.drop(['Year','Month'], axis=1) #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))
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()
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)