Ejemplo n.º 1
0
def plot_dem(inputs, outputs):
    hydrosheds_dir = PATHS['hydrosheds_dir']
    bounds, tx, dem, mask = load_hydrosheds_dem(hydrosheds_dir, 'as')
    hb_gdf = load_hydrobasins_geodataframe(hydrosheds_dir, 'as', [1])
    raster = build_raster_from_geometries(hb_gdf.geometry, dem.shape, tx)

    extent = (bounds.left, bounds.right, bounds.bottom, bounds.top)

    ma_dem = np.ma.masked_array(dem, (mask == -1) | (raster == 0))
    plt.figure(figsize=(5, 3.6))
    ax = plt.axes(projection=ccrs.PlateCarree())
    cmap = plt.cm.get_cmap('terrain')
    # Fills masked values.
    cmap.set_bad(color='k', alpha=0.1)
    im = ax.imshow(ma_dem[::-1, :], extent=extent, origin='lower', cmap=cmap)

    configure_ax_asia(ax, tight_layout=False)
    rect = Rectangle((97.5, 18),
                     125 - 97.5,
                     41 - 18,
                     linewidth=1.5,
                     edgecolor='k',
                     facecolor='none')
    ax.add_patch(rect)

    plt.colorbar(im, orientation='horizontal', label='elevation (m)', pad=0.1)
    plt.subplots_adjust(left=0.11, right=0.94, top=0.95, bottom=0.04)

    plt.savefig(outputs[0])
Ejemplo n.º 2
0
def basin_overlay_4349():
    logger.info(f'Running {__file__}: basin_overlay_4349()')
    hydrosheds_dir = os.getenv('HYDROSHEDS_DIR')
    hb_gdf = load_hydrobasins_geodataframe(hydrosheds_dir, 'as', range(4, 6))
    bounds, tx, dem, mask = load_hydrosheds_dem(hydrosheds_dir, 'as')
    extent = (bounds.left, bounds.right, bounds.bottom, bounds.top)

    hb_gdf4 = hb_gdf[hb_gdf.LEVEL == 4]
    raster = build_raster_from_geometries(hb_gdf4.geometry, dem.shape, tx)

    fig, ax = plt.subplots()
    plt.title('DEM of 4349')
    plt.xlim((90, 115))
    plt.ylim((20, 40))
    ax.imshow(np.ma.masked_array(dem, raster != 47), extent=extent)
    hb_gdf[(hb_gdf.LEVEL == 5) & hb_gdf.PFAF_STR.str.startswith('4349')]\
        .geometry.boundary.plot(ax=ax, color=None, edgecolor='k', linewidth=0.5)

    plt.title('DEM of 4349')
    plt.xlim((90, 115))
    plt.ylim((20, 40))

    output_filename = 'basmati_demo_figs/dem_of_pfaf_id_4349.png'
    logger.info(f'Saving figure to: {output_filename}')
    plt.savefig(output_filename)
    plt.close('all')
Ejemplo n.º 3
0
def hydrobasins_geopandas():
    logger.info(f'Running {__file__}: hydrobasins_geopandas()')
    hydrosheds_dir = os.getenv('HYDROSHEDS_DIR')
    hb_gdf = load_hydrobasins_geodataframe(hydrosheds_dir, 'as', range(1, 9))

    plot_selected_basins(hb_gdf[hb_gdf.LEVEL == 8])
    plot_basin_area_stats(hb_gdf)
    plt.close('all')
Ejemplo n.º 4
0
def gen_hydrobasins_files(inputs, outputs, hb_name):
    hydrosheds_dir = PATHS['hydrosheds_dir']
    hb = load_hydrobasins_geodataframe(hydrosheds_dir, 'as', range(1, 9))
    if hb_name[0] == 'S':
        hb_size = hb.area_select(*SLIDING_SCALES[hb_name])
    else:
        hb_size = hb.area_select(*SCALES[hb_name])
    hb_size.to_file(outputs['shp'])
Ejemplo n.º 5
0
def gen_hydrobasins_raster_cubes(inputs, outputs, scales=SCALES):
    diurnal_cycle_cube = iris.load_cube(str(inputs[0]), 'amount_of_precip_jja')
    hydrosheds_dir = PATHS['hydrosheds_dir']
    hb = load_hydrobasins_geodataframe(hydrosheds_dir, 'as', range(1, 9))
    raster_cubes = []
    for scale, (min_area, max_area) in scales.items():
        hb_filtered = hb.area_select(min_area, max_area)
        raster_cube = build_raster_cube_from_cube(
            hb_filtered.geometry, diurnal_cycle_cube,
            f'hydrobasins_raster_{scale}')
        raster_cubes.append(raster_cube)
    raster_cubes = iris.cube.CubeList(raster_cubes)
    iris.save(raster_cubes, str(outputs[0]))
Ejemplo n.º 6
0
 def __init__(self, datadir, hydrosheds_dir, figsdir, runid, daterange,
              seasons, precip_thresh, resolution):
     self.datadir = datadir
     self.hydrosheds_dir = hydrosheds_dir
     self.figsdir = Path(f'{figsdir}/seasonal_analysis/{runid}/{daterange}')
     self.runid = runid
     self.daterange = daterange
     if seasons == 'all':
         self.seasons = SEASONS
     else:
         self.seasons = seasons
     self.resolution = resolution
     self.precip_thresh = precip_thresh
     self.thresh_text = str(precip_thresh).replace('.', 'p')
     self.cubes = {}
     self.load_cubes()
     self.hb = load_hydrobasins_geodataframe(self.hydrosheds_dir, 'as',
                                             range(1, 9))
     self.raster_cache = {}
Ejemplo n.º 7
0
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 compare_mean_precip(hydrosheds_dir,
                        figsdir,
                        dataset1,
                        dataset2,
                        dataset1daterange,
                        dataset2daterange,
                        land_only=False,
                        check_calcs=False,
                        plot_type='scatter'):
    cube1 = iris.load_cube(
        f'data/{dataset1}_china_amount.{dataset1daterange}.nc')
    cube2 = iris.load_cube(
        f'data/{dataset2}_china_amount.{dataset2daterange}.nc')

    min_lat1, max_lat1 = cube1.coord('latitude').points[[0, -1]]
    min_lon1, max_lon1 = cube1.coord('longitude').points[[0, -1]]

    min_lat2, max_lat2 = cube2.coord('latitude').points[[0, -1]]
    min_lon2, max_lon2 = cube2.coord('longitude').points[[0, -1]]

    # I'm not going to pretend this isn't confusing.
    # Want the maximum min_lat so that I can extract cubes with same dims, because
    # e.g. CMORPH N1280 has one fewer coord in some dims that native N1280 runs.
    min_lat = max(min_lat1, min_lat2)
    max_lat = min(max_lat1, max_lat2)

    min_lon = max(min_lon1, min_lon2)
    max_lon = min(max_lon1, max_lon2)

    lat_fn = lambda cell: min_lat <= cell <= max_lat
    lon_fn = lambda cell: min_lon < cell < max_lon

    constraint = (iris.Constraint(coord_values={'latitude': lat_fn})
                  & iris.Constraint(coord_values={'longitude': lon_fn}))

    cube1 = cube1.extract(constraint)
    cube2 = cube2.extract(constraint)

    assert np.all(
        cube1.coord('latitude').points == cube2.coord('latitude').points)
    assert np.all(
        cube1.coord('longitude').points == cube2.coord('longitude').points)

    raw_data1 = cube1.data
    raw_data2 = cube2.data

    # Convert these from mm hr-1 to mm day-1
    if dataset1[:6] == 'cmorph' or dataset1[:2] == 'u-':
        raw_data1 *= 24
    if dataset2[:6] == 'cmorph' or dataset2[:2] == 'u-':
        raw_data2 *= 24

    full_mask = raw_data1.mask | raw_data2.mask
    full_mask |= np.isnan(raw_data1)
    full_mask |= np.isnan(raw_data2)
    if land_only:
        hb = load_hydrobasins_geodataframe(hydrosheds_dir, 'as', [1])
        raster = build_raster_from_cube(hb.geometry, cube1)
        full_mask |= raster == 0

    title = f'{dataset1} ({dataset1daterange}) vs {dataset2} ({dataset1daterange})'
    if land_only:
        title += ' land only'
    # compressed removes masked values (identically for each array as mask the same).
    data1 = np.ma.masked_array(raw_data1, full_mask).flatten().compressed()
    data2 = np.ma.masked_array(raw_data2, full_mask).flatten().compressed()

    max_precip = max(data1.max(), data2.max())

    if check_calcs:
        extent = [min_lon, max_lon, min_lat, max_lat]
        fig = plt.figure('check-calc: ' + title, figsize=(10, 5))

        ax1 = fig.add_subplot(121, aspect='equal')
        ax2 = fig.add_subplot(122, aspect='equal')
        ax1.set_title(dataset1)
        ax2.set_title(dataset2)
        ax1.imshow(np.ma.masked_array(raw_data1, full_mask),
                   origin='lower',
                   extent=extent,
                   vmax=max_precip,
                   norm=LogNorm())
        im = ax2.imshow(np.ma.masked_array(raw_data2, full_mask),
                        origin='lower',
                        extent=extent,
                        vmax=max_precip,
                        norm=LogNorm())
        fig.subplots_adjust(bottom=0.2)
        cbar_ax = fig.add_axes([0.15, 0.11, 0.7, 0.03])
        fig.colorbar(im,
                     cax=cbar_ax,
                     orientation='horizontal',
                     label='precip (mm day$^{-1}$)')
        if land_only:
            savefig(
                f'{figsdir}/compare/{dataset1}_vs_{dataset2}/{dataset1daterange}_{dataset2daterange}/'
                f'compare_check_calcs.land_only.png')
        else:
            savefig(
                f'{figsdir}/compare/{dataset1}_vs_{dataset2}/{dataset1daterange}_{dataset2daterange}/'
                f'compare_check_calcs.png')

    fig = plt.figure(title, figsize=(10, 10))
    plt.clf()
    ax = fig.add_subplot(111, aspect='equal')
    ax.set_title(title)

    if plot_type == 'scatter':
        ax.scatter(data1, data2)
        ax.set_xlim((0, max_precip))
        ax.set_ylim((0, max_precip))
        ax.plot([0, max_precip], [0, max_precip], 'k--')

    elif plot_type == 'heatmap':
        hist_kwargs = {}
        if dataset1[:2] == 'u-' or dataset2[:2] == 'u-':
            hist_kwargs['bins'] = np.linspace(0, 40, 41)
            max_xy = 40
        else:
            max_xy = math.ceil(max_precip)
            hist_kwargs['bins'] = np.linspace(0, max_xy, int(max_xy + 1))
        ax.plot([0, max_xy], [0, max_xy], 'k--')
        ax.set_xlim((0, max_xy))
        ax.set_ylim((0, max_xy))
        # Why are these the other way round?
        H, xedges, yedges = np.histogram2d(data2,
                                           data1,
                                           density=True,
                                           **hist_kwargs)
        im = ax.imshow(H,
                       origin='lower',
                       extent=(0, max_xy, 0, max_xy),
                       norm=LogNorm())
        plt.colorbar(im, orientation='horizontal')

    ax.set_xlabel(f'{dataset1} (mm day$^{{-1}}$)')
    ax.set_ylabel(f'{dataset2} (mm day$^{{-1}}$)')

    res = linregress(data1, data2)
    x = np.linspace(0, max_precip, 2)
    y = res.slope * x + res.intercept
    ax.plot(x,
            y,
            'r--',
            label=(f'm = {res.slope:.2f}\n'
                   f'c = {res.intercept:.2f}\n'
                   f'r$^2$ = {res.rvalue**2:.2f}\n'
                   f'p = {res.pvalue:.2f}'))
    ax.legend(loc=2)
    fname = f'{figsdir}/compare/{dataset1}_vs_{dataset2}/{dataset1daterange}_{dataset2daterange}/compare'
    if land_only:
        fname += '.land_only'
    if plot_type == 'heatmap':
        fname += '.heatmap'

    savefig(fname + '.png')
    plt.close(fig)
Ejemplo n.º 9
0
# coding: utf-8
import basmati.utils
import cosmic.util as util
from importlib import reload
import iris
from basmati.hydrosheds import load_hydrobasins_geodataframe
get_ipython().run_line_magic('pinfo', 'load_hydrobasins_geodataframe')
load_hydrobasins_geodataframe('/home/markmuetz/HydroSHEDS/', 'as', range(1, 6))
hb = _
hb_large = hb.area_select(200000, 2000000)
cube = iris.load_cube('/home/markmuetz/mirrors/jasmin/gw_cosmic/mmuetz/data/PRIMAVERA_HighResMIP_MOHC/HadGEM3-GC31-HM/highresSST-present/r1i1p1f1/E1hr/pr/gn/v20170831/pr_E1hr_HadGEM3-GC31-HM_highresSST-present_r1i1p1f1_gn_201404010030-201406302330.nc')
cube = iris.load_cube('/home/markmuetz/mirrors/jasmin/gw_cosmic/mmuetz/data/PRIMAVERA_HighResMIP_MOHC/HadGEM3-GC31-LM/highresSST-present/r1i1p1f1/E1hr/pr/gn/v20170906/pr_E1hr_HadGEM3-GC31-LM_highresSST-present_r1i1p1f1_gn_201401010030-201412302330.nc')
cube
basmati.utils.build_weights_cube_from_cube(hb_large, cube, 'weights_large')
reload(util)
basmati.utils.build_weights_cube_from_cube(hb_large, cube, 'weights_large')
reload(util)
basmati.utils.build_weights_cube_from_cube(hb_large, cube, 'weights_large')
get_ipython().run_line_magic('debug', '')
reload(util)
basmati.utils.build_weights_cube_from_cube(hb_large, cube, 'weights_large')
weights = basmati.utils.build_weights_cube_from_cube(hb_large, cube, 'weights_large')
weights.data[0].sum()
weights.data.sum(axis=(1, 2))
len(hb_large)
get_ipython().run_line_magic('pinfo', 'weights.slices_over')
weights.slices_over('basin_index')
next(weights.slices_over('basin_index'))
for w, basin in zip(weights.slices_over('basin_index'), [r for i, r in hb_large.iterrows()]):
    print(w)
    print(basin)
Ejemplo n.º 10
0
 def setUpClass(cls):
     cls.gdf = load_hydrobasins_geodataframe(HYDROSHEDS_DIR, 'as')
Ejemplo n.º 11
0
 def test3_load_all(self):
     gdf = load_hydrobasins_geodataframe(HYDROSHEDS_DIR, 'as', range(1, 13))
     assert len(gdf.LEVEL.unique()) == 12
Ejemplo n.º 12
0
 def test2_load_default(self):
     gdf = load_hydrobasins_geodataframe(HYDROSHEDS_DIR, 'as')
     assert len(gdf.LEVEL.unique()) == 6
Ejemplo n.º 13
0
 def test1_load_level1(self):
     gdf = load_hydrobasins_geodataframe(HYDROSHEDS_DIR, 'as', [1])
     assert len(gdf) == 1
Ejemplo n.º 14
0
 def test0_load_bad_region(self):
     with self.assertRaises(OSError):
         gdf = load_hydrobasins_geodataframe(HYDROSHEDS_DIR, 'minmus', [1])
Ejemplo n.º 15
0
 def test0_load_bad_dir(self):
     with self.assertRaises(OSError):
         gdf = load_hydrobasins_geodataframe('bad_dir', 'as', [1])
Ejemplo n.º 16
0
                extents[i, 0] -= (new_dx - dx) / 2
                extents[i, 1] += (new_dx - dx) / 2
            elif extent_aspect > aspect:
                new_dy = dx / aspect
                extents[i, 2] -= (new_dy - dy) / 2
                extents[i, 3] += (new_dy - dy) / 2

        plt.xlim(extents[i, :2])
        plt.ylim(extents[i, 2:])

        plt.savefig(f'figs/level_anim{i:03d}.png')
        plt.close('all')

if __name__ == '__main__':
    project = BasmatiProject()
    hb_gdf = load_hydrobasins_geodataframe(project.hydrosheds_dir, 'as', range(1, 9))
    bounds, tx, dem, mask = load_hydrosheds_dem(project.hydrosheds_dir, 'as')
    dem_extent = [bounds.left, bounds.right, bounds.bottom, bounds.top]
    aspect = 1.7

    hb_gdf1 = hb_gdf[hb_gdf.LEVEL == 1]
    raster = build_raster_from_geometries(hb_gdf1.geometry, dem.shape, tx)
    ma_dem = np.ma.masked_array(dem, raster == 0)
    n_per_level = 24
    N = 7 * n_per_level
    x = np.linspace(1, 8, 7 * n_per_level)
    slider = np.zeros_like(x)
    for i in range(7):
        slider[i * n_per_level: (i + 1) * n_per_level] = -0.5 * np.cos((x[:n_per_level] - 1) * np.pi) + 1.5 + i

    hb_gdf_pfaf, extents = get_levels_for_furthest(hb_gdf)