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 crtanje_m(): to_proj = ccrs.AlbersEqualArea(central_longitude=-1., central_latitude=10.) #load cordinates fname = '/home/martin/Master_rad/PredtandfilaGrid.dat' #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('/home/martin/Master_rad/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(x_masked, y_masked, t, interp_type='barnes', minimum_neighbors=8, search_radius=150000, hres=30000) 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([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=':') 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') plt.show()
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(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
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(x, y, temp, interp_type='linear', hres=75000) img = np.ma.masked_where(np.isnan(img), img) view = basic_map(to_proj) mmb = view.pcolormesh(gx, gy, img, cmap=cmap, norm=norm) plt.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 plot_map(df, date, value, cbarlabel='', title='', cmap="Spectral_r", zmin=0, zmax=100, save=False): cities = [ 'Williston', 'Minot', 'Grand Forks', 'Fargo', 'Bismarck', 'Devils Lake' ] fig, ax = plt.subplots(figsize=(15, 10)) to_plot = df[df["Date"] == date] lon = to_plot['Longitude (deg)'].values lat = to_plot['Latitude (deg)'].values #make basemap map print("Making map...") m = Basemap(llcrnrlon=min(lon), llcrnrlat=min(lat), urcrnrlon=max(lon), urcrnrlat=max(lat), projection='merc', resolution='h') #Convert lon, lat to map coordinates and mask any missing points xp, yp = m(lon, lat) x_masked, y_masked, z = remove_nan_observations(xp, yp, to_plot[value].values) print("Interpolating data...") #Uncomment one of the interpolation methods below. rbf is chosen as a default #linear #gridx, gridy, z = interpolate(x_masked, y_masked, z, interp_type='linear', hres=5000) #rbf gridx, gridy, z = interpolate(x_masked, y_masked, z, interp_type='rbf', hres=5000, rbf_func='linear', rbf_smooth=0) #cressman #gridx, gridy, z = interpolate(x_masked, y_masked, z, interp_type='cressman', minimum_neighbors=1, hres=5000, #search_radius=100000) #barnes #gridx, gridy, z = interpolate(x_masked, y_masked, z, interp_type='barnes', hres=5000, #search_radius=100000) #Natural neighbor #gridx, gridy, z = interpolate(x_masked, y_masked, z, interp_type='natural_neighbor', hres=5000) z = np.ma.masked_where(np.isnan(z), z) #Find max and min of z if not input manually if zmax == 'max': zmax = np.max(z) if zmin == 'min': zmin = np.min(z) #normalize colormap cmap = plt.get_cmap(cmap) norm = Normalize(vmin=zmin, vmax=zmax) #Draw map lines m.drawcoastlines() m.drawcountries() m.drawstates() #Set contour line parameters. must be integers low = int(round(np.min(z))) high = int(round(np.max(z))) step = int(round((high - low) / 10)) #Set step at least 2 so there are not contour lines set for every increase of 1 (unless that's what you want) if step < 2: step = 2 #plot colormesh mmb = m.pcolormesh(gridx, gridy, z, cmap=cmap, norm=norm) cbar = m.colorbar(mmb, location='bottom') cs = m.contour(gridx, gridy, z, colors='k', levels=list(range(low, high, step)), alpha=0.3) plt.clabel(cs, inline=1, fontsize=12, fmt='%i') #Plot city markers geolocator = Nominatim() for city in cities: loc = geolocator.geocode(city) x, y = m(loc.longitude, loc.latitude) m.plot(x, y, marker='o', color='None', markeredgecolor='k', markersize=8) plt.text(x + 10000, y + 5000, city, fontsize=12, alpha=0.7) m.drawcounties() cbar.set_label(cbarlabel) plt.title(title, fontsize=18) if save: # Make sure a directory exists for the gdd data print("Checking for file pathways...") if not os.path.isdir("./maps"): os.mkdir('maps') print("Saving figure...") fig.savefig('./maps/' + str(title) + '.png', format='png') return m
if proj_from is not None and proj_to is not None: proj_points = proj_to.transform_points(proj_from, lon, lat) return proj_points[:, 0], proj_points[:, 1], value return lon, lat, value ########################################### # Get pressure information using the sample station data xp, yp, pres = station_test_data(['slp'], from_proj, to_proj) ########################################### # Remove all missing data from pressure pres = np.array([p[0] for p in pres]) xp, yp, pres = remove_nan_observations(xp, yp, pres) ########################################### # Interpolate pressure as usual slpgridx, slpgridy, slp = interpolate(xp, yp, pres, interp_type='cressman', minimum_neighbors=1, search_radius=400000, hres=100000) ########################################### # Get wind information x, y, wind = station_test_data(['wind_speed', 'wind_dir'], from_proj, to_proj)
if proj_from is not None and proj_to is not None: proj_points = proj_to.transform_points(proj_from, lon, lat) return proj_points[:, 0], proj_points[:, 1], value return lon, lat, value ########################################### # Get pressure information using the sample station data xp, yp, pres = station_test_data(['slp'], from_proj, to_proj) ########################################### # Remove all missing data from pressure pres = np.array([p[0] for p in pres]) xp, yp, pres = remove_nan_observations(xp, yp, pres) ########################################### # Interpolate pressure as usual slpgridx, slpgridy, slp = interpolate(xp, yp, pres, interp_type='cressman', minimum_neighbors=1, search_radius=400000, hres=100000) ########################################### # Get wind information x, y, wind = station_test_data(['wind_speed', 'wind_dir'], from_proj, to_proj) ########################################### # Remove bad data from wind information wind_speed = np.array([w[0] for w in wind]) wind_dir = np.array([w[1] for w in wind])