def stack_tile(tile, tmp_ref_dir, tmp_aster_dir, tmp_setsm_dir, out_dir):

        lat, lon = SRTMGL1_naming_to_latlon(tile)
        epsg, utm = latlon_to_UTM(lat, lon)

        outfile = os.path.join(out_dir, utm, tile + '.nc')

        if not os.path.exists(outfile):

            print('Stacking tile: ' + tile + ' in UTM zone ' + utm)

            # reference DEM
            ref_utm_dir = os.path.join(tmp_ref_dir, utm)
            ref_vrt = os.path.join(ref_utm_dir, 'tmp_' + utm + '.vrt')
            ref_list = glob(os.path.join(ref_utm_dir, '**/*.tif'),
                            recursive=True)
            if not os.path.exists(ref_vrt):
                gdal.BuildVRT(ref_vrt, ref_list, resampleAlg='bilinear')

            # DEMs to stack
            flist1 = glob(os.path.join(tmp_aster_dir, '**/*_final.zip'),
                          recursive=True)
            if os.path.exists(tmp_setsm_dir):
                flist2 = glob(os.path.join(tmp_setsm_dir, '**/*.tif'),
                              recursive=True)
            else:
                flist2 = []

            flist = flist1 + flist2

            extent = niceextent_utm_latlontile(tile, utm, res)
            bobformat_extent = [extent[0], extent[2], extent[1], extent[3]]

            print('Nice extent is:')
            print(extent)
            if len(flist) > 0:
                nco = create_mmaster_stack(flist,
                                           extent=bobformat_extent,
                                           epsg=int(epsg),
                                           mst_tiles=ref_vrt,
                                           res=res,
                                           outfile=outfile,
                                           coreg=False,
                                           uncert=True,
                                           clobber=True,
                                           add_ref=True,
                                           add_corr=True,
                                           latlontile_nodata=tile,
                                           filt_mm_corr=False,
                                           l1a_zipped=True,
                                           y0=y0,
                                           tmptag=tile)
                nco.close()
            else:
                print('No DEM intersecting tile found. Skipping...')

        else:
            print('Tile ' + tile + ' already exists.')
Exemple #2
0
def add_inset(fig,
              extent,
              position,
              bounds=None,
              label=None,
              polygon=None,
              shades=True,
              hillshade=True,
              list_shp=None,
              main=False,
              markup=None,
              markpos='left',
              markadj=0,
              markup_sub=None,
              sub_pos='lt'):

    main_pos = [0.375, 0.21, 0.25, 0.25]

    if polygon is None and bounds is not None:
        polygon = poly_from_extent(bounds)

    if shades:
        shades_main_to_inset(main_pos,
                             position,
                             latlon_extent_to_robinson_axes_verts(polygon),
                             label=label)

    sub_ax = fig.add_axes(position, projection=ccrs.Robinson(), label=label)
    sub_ax.set_extent(extent, ccrs.Geodetic())

    sub_ax.add_feature(
        cfeature.NaturalEarthFeature('physical',
                                     'ocean',
                                     '50m',
                                     facecolor='gainsboro'))
    sub_ax.add_feature(
        cfeature.NaturalEarthFeature('physical',
                                     'land',
                                     '50m',
                                     facecolor='dimgrey'))

    if hillshade:

        def out_of_poly_mask(geoimg, poly_coords):

            poly = poly_from_coords(inter_poly_coords(poly_coords))
            srs = osr.SpatialReference()
            srs.ImportFromEPSG(4326)

            # put in a memory vector
            ds_shp = create_mem_shp(poly, srs)

            return geoimg_mask_on_feat_shp_ds(ds_shp, geoimg)

        def inter_poly_coords(polygon_coords):
            list_lat_interp = []
            list_lon_interp = []
            for i in range(len(polygon_coords) - 1):
                lon_interp = np.linspace(polygon_coords[i][0],
                                         polygon_coords[i + 1][0], 50)
                lat_interp = np.linspace(polygon_coords[i][1],
                                         polygon_coords[i + 1][1], 50)

                list_lon_interp.append(lon_interp)
                list_lat_interp.append(lat_interp)

            all_lon_interp = np.concatenate(list_lon_interp)
            all_lat_interp = np.concatenate(list_lat_interp)

            return np.array(list(zip(all_lon_interp, all_lat_interp)))

        img = GeoImg(fn_hs)
        hs_tmp = hs_land.copy()
        hs_tmp_nl = hs_notland.copy()
        mask = out_of_poly_mask(img, polygon)

        hs_tmp[~mask] = 0
        hs_tmp_nl[~mask] = 0

        sub_ax.imshow(hs_tmp[:, :],
                      extent=ext,
                      transform=ccrs.Robinson(),
                      cmap=cmap2,
                      zorder=2,
                      interpolation='nearest')
        sub_ax.imshow(hs_tmp_nl[:, :],
                      extent=ext,
                      transform=ccrs.Robinson(),
                      cmap=cmap22,
                      zorder=2,
                      interpolation='nearest')

    if main:
        shape_feature = ShapelyFeature(Reader(list_shp).geometries(),
                                       ccrs.PlateCarree(),
                                       alpha=1,
                                       facecolor='indigo',
                                       linewidth=0.35,
                                       edgecolor='indigo')
        sub_ax.add_feature(shape_feature)

    if bounds is not None:
        verts = mpath.Path(latlon_extent_to_robinson_axes_verts(polygon))
        sub_ax.set_boundary(verts, transform=sub_ax.transAxes)

    if not main:
        for i in range(len(tiles)):
            lat, lon = SRTMGL1_naming_to_latlon(tiles[i])
            if group_by_spec:
                lat, lon = latlon_to_spec_center(lat, lon)
            else:
                lat = lat + 0.5
                lon = lon + 0.5

            if label == 'Arctic West' and ((lat < 71 and lon > 60) or
                                           (lat < 76 and lon > 100)):
                continue

            if label == 'HMA' and lat >= 46:
                continue

            # fac = 0.02
            fac = 1000

            if areas[i] > 10:
                rad = 15000 + np.sqrt(areas[i]) * fac
            else:
                rad = 15000 + 10 * fac
            # cmin = -1
            # cmax = 1
            col_bounds = np.array([
                -1.5, -1.1, -0.8, -0.6, -0.4, -0.2, 0, 0.1, 0.2, 0.3, 0.4, 0.5,
                0.6
            ])
            # col_bounds = np.array([-1, -0.7, -0.4, -0.2, -0.1, -0.05, 0.05, 0.1, 0.15, 0.2, 0.3, 0.5])
            cb = []
            cb_val = np.linspace(0, 1, len(col_bounds))
            for j in range(len(cb_val)):
                cb.append(mpl.cm.RdYlBu(cb_val[j]))
            cmap_cus = mpl.colors.LinearSegmentedColormap.from_list(
                'my_cb',
                list(
                    zip((col_bounds - min(col_bounds)) /
                        (max(col_bounds - min(col_bounds))), cb)),
                N=1000)

            if ~np.isnan(dhs[i]) and areas[i] > 0.2 and errs[
                    i] < 0.5:  #and ((areas[i]>=5.) or label in ['Mexico','Indonesia','Africa']):
                dhdt = dhs[i]
                dhdt_col = max(
                    0.0001,
                    min(0.9999, (dhdt - min(col_bounds)) /
                        (max(col_bounds) - min(col_bounds))))

                # ind = max(0, min(int((dhs[i]/20. - cmin) / (cmax - cmin) * 100), 99))
                # if dhs[i]>=0:
                #     ind = max(0, min(int((np.sqrt(dhs[i]/20.) - cmin) / (cmax - cmin) * 100), 99))
                # else:
                #     ind = max(0, min(int((-np.sqrt(-dhs[i]/20.) - cmin) / (cmax - cmin) * 100), 99))
                col = cmap_cus(dhdt_col)
            # elif areas[i] <= 5:
            #     continue
            elif areas[i] > 0.2:
                col = plt.cm.Greys(0.7)
                # col = 'black'

            # xy = [lon,lat]
            xy = coordXform(ccrs.PlateCarree(), ccrs.Robinson(),
                            np.array([lon]), np.array([lat]))[0][0:2]

            sub_ax.add_patch(
                mpatches.Circle(xy=xy,
                                radius=rad,
                                facecolor=col,
                                edgecolor='None',
                                alpha=1,
                                transform=ccrs.Robinson(),
                                zorder=30))
            # sub_ax.add_patch(
            #     mpatches.Circle(xy=xy, radius=rad, facecolor='None', edgecolor='dimgrey', alpha=1, transform=ccrs.Robinson(), zorder=30))

    if markup is not None:
        if markpos == 'left':
            lon_upleft = np.min(list(zip(*polygon))[0])
            lat_upleft = np.max(list(zip(*polygon))[1])
        else:
            lon_upleft = np.max(list(zip(*polygon))[0])
            lat_upleft = np.max(list(zip(*polygon))[1])

        robin = coordXform(ccrs.PlateCarree(), ccrs.Robinson(),
                           np.array([lon_upleft]), np.array([lat_upleft]))

        rob_x = robin[0][0]
        rob_y = robin[0][1]

        size_y = 200000
        size_x = 80000 * len(markup) + markadj

        if markpos == 'right':
            rob_x = rob_x - 50000
        else:
            rob_x = rob_x + 50000

        sub_ax_2 = fig.add_axes(position,
                                projection=ccrs.Robinson(),
                                label=label + 'markup')

        # sub_ax_2.add_patch(mpatches.Rectangle((rob_x, rob_y), size_x, size_y , linewidth=1, edgecolor='grey', facecolor='white',transform=ccrs.Robinson()))

        sub_ax_2.set_extent(extent, ccrs.Geodetic())
        verts = mpath.Path(rect_units_to_verts([rob_x, rob_y, size_x, size_y]))
        sub_ax_2.set_boundary(verts, transform=sub_ax.transAxes)

        sub_ax_2.text(rob_x,
                      rob_y + 50000,
                      markup,
                      horizontalalignment=markpos,
                      verticalalignment='bottom',
                      transform=ccrs.Robinson(),
                      color='black',
                      fontsize=4.5,
                      fontweight='bold',
                      bbox=dict(facecolor='white',
                                alpha=1,
                                linewidth=0.35,
                                pad=1.5))

    if markup_sub is not None:

        lon_min = np.min(list(zip(*polygon))[0])
        lon_max = np.max(list(zip(*polygon))[0])
        lon_mid = 0.5 * (lon_min + lon_max)

        lat_min = np.min(list(zip(*polygon))[1])
        lat_max = np.max(list(zip(*polygon))[1])
        lat_mid = 0.5 * (lat_min + lat_max)

        size_y = 150000
        size_x = 150000

        lat_midup = lat_min + 0.87 * (lat_max - lat_min)

        robin = coordXform(
            ccrs.PlateCarree(), ccrs.Robinson(),
            np.array([
                lon_min, lon_min, lon_min, lon_mid, lon_mid, lon_max, lon_max,
                lon_max, lon_min
            ]),
            np.array([
                lat_min, lat_mid, lat_max, lat_min, lat_max, lat_min, lat_mid,
                lat_max, lat_midup
            ]))

        if sub_pos == 'lb':
            rob_x = robin[0][0]
            rob_y = robin[0][1]
            ha = 'left'
            va = 'bottom'
        elif sub_pos == 'lm':
            rob_x = robin[1][0]
            rob_y = robin[1][1]
            ha = 'left'
            va = 'center'
        elif sub_pos == 'lm2':
            rob_x = robin[8][0]
            rob_y = robin[8][1]
            ha = 'left'
            va = 'center'
        elif sub_pos == 'lt':
            rob_x = robin[2][0]
            rob_y = robin[2][1]
            ha = 'left'
            va = 'top'
        elif sub_pos == 'mb':
            rob_x = robin[3][0]
            rob_y = robin[3][1]
            ha = 'center'
            va = 'bottom'
        elif sub_pos == 'mt':
            rob_x = robin[4][0]
            rob_y = robin[4][1]
            ha = 'center'
            va = 'top'
        elif sub_pos == 'rb':
            rob_x = robin[5][0]
            rob_y = robin[5][1]
            ha = 'right'
            va = 'bottom'
        elif sub_pos == 'rm':
            rob_x = robin[6][0]
            rob_y = robin[6][1]
            ha = 'right'
            va = 'center'
        elif sub_pos == 'rt':
            rob_x = robin[7][0]
            rob_y = robin[7][1]
            ha = 'right'
            va = 'top'

        if sub_pos[0] == 'r':
            rob_x = rob_x - 50000
        elif sub_pos[0] == 'l':
            rob_x = rob_x + 50000

        if sub_pos[1] == 'b':
            rob_y = rob_y + 50000
        elif sub_pos[1] == 't':
            rob_y = rob_y - 50000

        sub_ax_3 = fig.add_axes(position,
                                projection=ccrs.Robinson(),
                                label=label + 'markup2')

        # sub_ax_3.add_patch(mpatches.Rectangle((rob_x, rob_y), size_x, size_y , linewidth=1, edgecolor='grey', facecolor='white',transform=ccrs.Robinson()))

        sub_ax_3.set_extent(extent, ccrs.Geodetic())
        verts = mpath.Path(rect_units_to_verts([rob_x, rob_y, size_x, size_y]))
        sub_ax_3.set_boundary(verts, transform=sub_ax.transAxes)

        sub_ax_3.text(rob_x,
                      rob_y,
                      markup_sub,
                      horizontalalignment=ha,
                      verticalalignment=va,
                      transform=ccrs.Robinson(),
                      color='black',
                      fontsize=4.5,
                      bbox=dict(facecolor='white',
                                alpha=1,
                                linewidth=0.35,
                                pad=1.5),
                      fontweight='bold',
                      zorder=25)

    if not main:
        sub_ax.outline_patch.set_edgecolor('white')
    else:
        sub_ax.outline_patch.set_edgecolor('lightgrey')
Exemple #3
0
errs = df_all.err_dhdt.tolist()

# for tile2 in tiles2:
#     if tile2 not in tiles:
#         tiles.append(tile2)
#         areas.append(areas2[tiles2.index(tile2)])
#         dhs.append(np.nan)
#         errs.append(np.nan)

if group_by_spec:
    list_tile_grouped = []
    final_areas = []
    final_dhs = []
    final_errs = []
    for tile in tiles:
        lat, lon = SRTMGL1_naming_to_latlon(tile)
        list_tile_grouped.append(latlon_to_spec_tile_naming(lat, lon))
    final_tiles = list(set(list_tile_grouped))
    for final_t in final_tiles:
        orig_tiles = [
            orig_t for orig_t in tiles
            if list_tile_grouped[tiles.index(orig_t)] == final_t
        ]

        group_areas = np.array(
            [areas[tiles.index(orig_t)] for orig_t in orig_tiles])
        group_dhs = np.array(
            [dhs[tiles.index(orig_t)] for orig_t in orig_tiles])
        group_errs = np.array(
            [errs[tiles.index(orig_t)] for orig_t in orig_tiles])
        final_areas.append(np.nansum(group_areas))
    latlon_to_SRTMGL1_naming(df_all.tile_latmin.values[i],
                             df_all.tile_lonmin.values[i])
    for i in range(len(df_all))
]

areas = df_all.area.tolist()
dhs = df_all.dhdt.tolist()
errs = df_all.err_dhdt.tolist()

if group_by_spec:
    list_tile_grouped = []
    final_areas = []
    final_dhs = []
    final_errs = []
    for tile in tiles:
        lat, lon = SRTMGL1_naming_to_latlon(tile)
        list_tile_grouped.append(latlon_to_spec_tile_naming(lat, lon))
    final_tiles = list(set(list_tile_grouped))
    for final_t in final_tiles:
        orig_tiles = [
            orig_t for orig_t in tiles
            if list_tile_grouped[tiles.index(orig_t)] == final_t
        ]

        group_areas = np.array(
            [areas[tiles.index(orig_t)] for orig_t in orig_tiles])
        group_dhs = np.array(
            [dhs[tiles.index(orig_t)] for orig_t in orig_tiles])
        group_errs = np.array(
            [errs[tiles.index(orig_t)] for orig_t in orig_tiles])
        final_areas.append(np.nansum(group_areas))
Exemple #5
0
def add_inset(fig,
              extent,
              pos,
              bounds,
              label=None,
              polygon=None,
              anom=None,
              draw_cmap_y=None,
              hillshade=True,
              markup_sub=None,
              sub_pos=None,
              sub_adj=None):

    sub_ax = fig.add_axes(pos, projection=ccrs.Robinson(), label=label)
    sub_ax.set_extent(extent, ccrs.Geodetic())

    sub_ax.add_feature(
        cfeature.NaturalEarthFeature('physical',
                                     'ocean',
                                     '50m',
                                     facecolor='gainsboro'))
    sub_ax.add_feature(
        cfeature.NaturalEarthFeature('physical',
                                     'land',
                                     '50m',
                                     facecolor='dimgrey'))
    # if anom is not None:
    #     shape_feature = ShapelyFeature(Reader(shp_buff).geometries(), ccrs.PlateCarree(), edgecolor='black', alpha=0.5,
    #                                    facecolor='black', linewidth=1)
    #     sub_ax.add_feature(shape_feature)

    if polygon is None and bounds is not None:
        polygon = poly_from_extent(bounds)

    if bounds is not None:
        verts = mpath.Path(latlon_extent_to_robinson_axes_verts(polygon))
        sub_ax.set_boundary(verts, transform=sub_ax.transAxes)

    if hillshade:

        def out_of_poly_mask(geoimg, poly_coords):

            poly = ot.poly_from_coords(inter_poly_coords(poly_coords))
            srs = osr.SpatialReference()
            srs.ImportFromEPSG(54030)

            # put in a memory vector
            ds_shp = ot.create_mem_shp(poly, srs)

            return ot.geoimg_mask_on_feat_shp_ds(ds_shp, geoimg)

        def inter_poly_coords(polygon_coords):
            list_lat_interp = []
            list_lon_interp = []
            for i in range(len(polygon_coords) - 1):
                lon_interp = np.linspace(polygon_coords[i][0],
                                         polygon_coords[i + 1][0], 50)
                lat_interp = np.linspace(polygon_coords[i][1],
                                         polygon_coords[i + 1][1], 50)

                list_lon_interp.append(lon_interp)
                list_lat_interp.append(lat_interp)

            all_lon_interp = np.concatenate(list_lon_interp)
            all_lat_interp = np.concatenate(list_lat_interp)

            return np.array(list(zip(all_lon_interp, all_lat_interp)))

        img = GeoImg(fn_hs)
        hs_tmp = hs_land.copy()
        hs_tmp_nl = hs_notland.copy()
        mask = out_of_poly_mask(img, polygon)

        hs_tmp[~mask] = 0
        hs_tmp_nl[~mask] = 0

        sub_ax.imshow(hs_tmp[:, :],
                      extent=ext,
                      transform=ccrs.Robinson(),
                      cmap=cmap2,
                      zorder=2,
                      interpolation='nearest')
        sub_ax.imshow(hs_tmp_nl[:, :],
                      extent=ext,
                      transform=ccrs.Robinson(),
                      cmap=cmap22,
                      zorder=2,
                      interpolation='nearest')

    sub_ax.outline_patch.set_edgecolor('white')

    if anom is not None:

        if anom == 'dhs_1' or 'dhs_2' or 'dhs_3' or 'dhs_3':

            col_bounds = np.array([0, 1, 2, 3, 5, 7, 10, 15, 20])
            cb = []
            cb_val = np.linspace(0, 1, len(col_bounds))
            for j in range(len(cb_val)):
                cb.append(mpl.cm.viridis(cb_val[j]))
            cmap_cus = mpl.colors.LinearSegmentedColormap.from_list(
                'my_cb',
                list(
                    zip((col_bounds - min(col_bounds)) /
                        (max(col_bounds - min(col_bounds))), cb)),
                N=1000)

            if anom == 'dhs_1':
                vals = dhs_1
            elif anom == 'dhs_2':
                vals = dhs_2
            elif anom == 'dhs_3':
                vals = dhs_3
            elif anom == 'dhs_4':
                vals = dhs_4

            lab = 'Number of valid observations'

        # elif anom == 'dt':
        #     col_bounds = np.array([-0.3, -0.15, 0, 0.3, 0.6])
        #     cb = []
        #     cb_val = np.linspace(0, 1, len(col_bounds))
        #     for j in range(len(cb_val)):
        #         cb.append(mpl.cm.RdBu_r(cb_val[j]))
        #     cmap_cus = mpl.colors.LinearSegmentedColormap.from_list('my_cb', list(
        #         zip((col_bounds - min(col_bounds)) / (max(col_bounds - min(col_bounds))), cb)), N=1000)
        #
        #     vals = dts
        #     lab = 'Decadal difference in temperature (K)'
        #
        # elif anom == 'dp':
        #     col_bounds = np.array([-0.2, -0.1, 0, 0.1, 0.2])
        #     cb = []
        #     cb_val = np.linspace(0, 1, len(col_bounds))
        #     for j in range(len(cb_val)):
        #         cb.append(mpl.cm.BrBG(cb_val[j]))
        #     cmap_cus = mpl.colors.LinearSegmentedColormap.from_list('my_cb', list(
        #         zip((col_bounds - min(col_bounds)) / (max(col_bounds - min(col_bounds))), cb)), N=1000)
        #
        #     vals = dps
        #     lab = 'Decadal difference in precipitation (m)'

        # elif anom == 'du':
        #     col_bounds = np.array([-1, -0.5, 0, 0.5, 1])
        #     cb = []
        #     cb_val = np.linspace(0, 1, len(col_bounds))
        #     for j in range(len(cb_val)):
        #         cb.append(mpl.cm.RdBu_r(cb_val[j]))
        #     cmap_cus = mpl.colors.LinearSegmentedColormap.from_list('my_cb', list(
        #         zip((col_bounds - min(col_bounds)) / (max(col_bounds - min(col_bounds))), cb)), N=1000)
        #
        #     vals = dus
        #     lab = 'Wind speed anomaly (m s$^{-1}$)'
        #
        # elif anom == 'dz':
        #
        #     col_bounds = np.array([-100, -50, 0, 50, 100])
        #     cb = []
        #     cb_val = np.linspace(0, 1, len(col_bounds))
        #     for j in range(len(cb_val)):
        #         cb.append(mpl.cm.RdBu_r(cb_val[j]))
        #     cmap_cus = mpl.colors.LinearSegmentedColormap.from_list('my_cb', list(
        #         zip((col_bounds - min(col_bounds)) / (max(col_bounds - min(col_bounds))), cb)), N=1000)
        #
        #     vals = dzs
        #     lab = 'Geopotential height anomaly at 500 hPa (m)'
        #
        # elif anom =='dk':
        #
        #     col_bounds = np.array([-200000, -100000, 0, 100000, 200000])
        #     cb = []
        #     cb_val = np.linspace(0, 1, len(col_bounds))
        #     for j in range(len(cb_val)):
        #         cb.append(mpl.cm.RdBu_r(cb_val[j]))
        #     cmap_cus = mpl.colors.LinearSegmentedColormap.from_list('my_cb', list(
        #         zip((col_bounds - min(col_bounds)) / (max(col_bounds - min(col_bounds))), cb)), N=1000)
        #
        #     vals = dks
        #     lab = 'Net clear-sky downwelling SW surface radiation anomaly (J m$^{-2}$)'

        if draw_cmap_y is not None:

            sub_ax_2 = fig.add_axes([0.2, draw_cmap_y, 0.6, 0.05])
            sub_ax_2.set_xticks([])
            sub_ax_2.set_yticks([])
            sub_ax_2.spines['top'].set_visible(False)
            sub_ax_2.spines['left'].set_visible(False)
            sub_ax_2.spines['right'].set_visible(False)
            sub_ax_2.spines['bottom'].set_visible(False)

            cbaxes = sub_ax_2.inset_axes([0, 0.85, 1, 0.2],
                                         label='legend_' + label)
            norm = mpl.colors.Normalize(vmin=min(col_bounds),
                                        vmax=max(col_bounds))
            sm = plt.cm.ScalarMappable(cmap=cmap_cus, norm=norm)
            sm.set_array([])
            cb = plt.colorbar(sm,
                              cax=cbaxes,
                              ticks=col_bounds,
                              orientation='horizontal',
                              extend='both',
                              shrink=0.9)
            # cb.ax.tick_params(labelsize=12)
            cb.set_label(lab)

        for i in range(len(tiles)):
            lat, lon = SRTMGL1_naming_to_latlon(tiles[i])
            if group_by_spec:
                lat, lon, s = latlon_to_spec_center(lat, lon)
            else:
                lat = lat + 0.5
                lon = lon + 0.5
                s = (1, 1)
            # fac = 0.02
            fac = 7000000.

            if anom == 'dhs_1':
                errs = errs_1
            elif anom == 'dhs_2':
                errs = errs_2
            elif anom == 'dhs_3':
                errs = errs_3
            elif anom == 'dhs_4':
                errs = errs_4

            if np.isnan(errs[i]):
                continue

            #need to square because Rectangle already shows a surface
            f = np.sqrt(((1 / min(max(errs[i], 0.25), 1)**2 - 1 / 1**2) /
                         (1 / 0.25**2 - 1 / 1**2))) * (1 - np.sqrt(0.1))

            if ~np.isnan(vals[i]) and areas[i] > 0.2:
                val = vals[i]
                val_col = max(
                    0.0001,
                    min(0.9999, (val - min(col_bounds)) /
                        (max(col_bounds) - min(col_bounds))))
                col = cmap_cus(val_col)
            elif areas[i] <= 5:
                continue
            else:
                col = plt.cm.Greys(0.7)

            # xy = [lon,lat]
            xy = coordXform(ccrs.PlateCarree(), ccrs.Robinson(),
                            np.array([lon]), np.array([lat]))[0][0:2]
            # sub_ax.add_patch(
            #     mpatches.Circle(xy=xy, radius=rad, color=col, alpha=1, transform=ccrs.Robinson(), zorder=30))
            xl = np.sqrt(0.1) * s[0] + f * s[0]
            yl = np.sqrt(0.1) * s[1] + f * s[1]
            sub_ax.add_patch(
                mpatches.Rectangle((lon - xl / 2, lat - yl / 2),
                                   xl,
                                   yl,
                                   facecolor=col,
                                   alpha=1,
                                   transform=ccrs.PlateCarree(),
                                   zorder=30))

    if markup_sub is not None and anom == 'dhs_1':

        lon_min = np.min(list(zip(*polygon))[0])
        lon_max = np.max(list(zip(*polygon))[0])
        lon_mid = 0.5 * (lon_min + lon_max)

        lat_min = np.min(list(zip(*polygon))[1])
        lat_max = np.max(list(zip(*polygon))[1])
        lat_mid = 0.5 * (lat_min + lat_max)

        robin = np.array(
            list(
                zip([
                    lon_min, lon_min, lon_min, lon_mid, lon_mid, lon_max,
                    lon_max, lon_max
                ], [
                    lat_min, lat_mid, lat_max, lat_min, lat_max, lat_min,
                    lat_mid, lat_max
                ])))

        if sub_pos == 'lb':
            rob_x = robin[0][0]
            rob_y = robin[0][1]
            ha = 'left'
            va = 'bottom'
        elif sub_pos == 'lm':
            rob_x = robin[1][0]
            rob_y = robin[1][1]
            ha = 'left'
            va = 'center'
        elif sub_pos == 'lt':
            rob_x = robin[2][0]
            rob_y = robin[2][1]
            ha = 'left'
            va = 'top'
        elif sub_pos == 'mb':
            rob_x = robin[3][0]
            rob_y = robin[3][1]
            ha = 'center'
            va = 'bottom'
        elif sub_pos == 'mt':
            rob_x = robin[4][0]
            rob_y = robin[4][1]
            ha = 'center'
            va = 'top'
        elif sub_pos == 'rb':
            rob_x = robin[5][0]
            rob_y = robin[5][1]
            ha = 'right'
            va = 'bottom'
        elif sub_pos == 'rm':
            rob_x = robin[6][0]
            rob_y = robin[6][1]
            ha = 'right'
            va = 'center'
        elif sub_pos == 'rt':
            rob_x = robin[7][0]
            rob_y = robin[7][1]
            ha = 'right'
            va = 'top'

        if sub_pos[0] == 'r':
            rob_x = rob_x - 100000
        elif sub_pos[0] == 'l':
            rob_x = rob_x + 100000

        if sub_pos[1] == 'b':
            rob_y = rob_y + 100000
        elif sub_pos[1] == 't':
            rob_y = rob_y - 100000

        if sub_adj is not None:
            rob_x += sub_adj[0]
            rob_y += sub_adj[1]

        sub_ax.text(rob_x,
                    rob_y,
                    markup_sub,
                    horizontalalignment=ha,
                    verticalalignment=va,
                    transform=ccrs.Robinson(),
                    color='black',
                    fontsize=4.5,
                    bbox=dict(facecolor='white',
                              alpha=1,
                              linewidth=0.35,
                              pad=1.5),
                    fontweight='bold',
                    zorder=25)
Exemple #6
0
    def fit_tile(tile, tmp_ref_dir, base_dir, out_dir):

        method = 'gpr'
        subspat = None
        ref_dem_date = np.datetime64('2013-01-01')
        gla_mask = '/calcul/santo/hugonnet/outlines/rgi60_merge.shp'
        inc_mask = '/calcul/santo/hugonnet/outlines/rgi60_buff_10.shp'
        write_filt = True
        clobber = True
        tstep = 1. / 12.
        time_filt_thresh = [-50, 50]
        opt_gpr = False
        filt_ref = 'both'
        filt_ls = False
        conf_filt_ls = 0.99
        # specify the exact temporal extent needed to be able to merge neighbouring stacks properly
        tlim = [np.datetime64('2000-01-01'), np.datetime64('2020-01-01')]

        #for sensitivity test: force final fit only, and change kernel parameters in entry of script
        force_final_fit = True
        k1 = PairwiseKernel(1, metric='linear')  # linear kernel
        k2 = C(period_var) * ESS(length_scale=1,
                                 periodicity=1)  # periodic kernel
        k3 = C(base_var * 0.6) * RBF(base_length * 0.75) + C(
            base_var * 0.3) * RBF(base_length * 1.5) + C(base_var * 0.1) * RBF(
                base_length * 3)
        k4 = PairwiseKernel(1, metric='linear') * C(nonlin_var) * RQ(
            nonlin_length, nonlin_alpha)
        kernel = k1 + k2 + k3 + k4

        lat, lon = SRTMGL1_naming_to_latlon(tile)
        epsg, utm = latlon_to_UTM(lat, lon)
        print('Fitting tile: ' + tile + ' in UTM zone ' + utm)

        # reference DEM
        ref_utm_dir = os.path.join(tmp_ref_dir, utm)
        ref_vrt = os.path.join(ref_utm_dir, 'tmp_' + utm + '.vrt')
        infile = os.path.join(base_dir, utm, tile + '.nc')
        outfile = os.path.join(out_dir, utm, tile + '_final.nc')

        fn_filt = os.path.join(base_dir, utm, tile + '_filtered.nc')

        if True:  #not os.path.exists(outfile):
            ft.fit_stack(infile,
                         fn_filt=fn_filt,
                         fit_extent=subspat,
                         fn_ref_dem=ref_vrt,
                         ref_dem_date=ref_dem_date,
                         exc_mask=gla_mask,
                         tstep=tstep,
                         tlim=tlim,
                         inc_mask=inc_mask,
                         filt_ref=filt_ref,
                         time_filt_thresh=time_filt_thresh,
                         write_filt=True,
                         outfile=outfile,
                         method=method,
                         filt_ls=filt_ls,
                         conf_filt_ls=conf_filt_ls,
                         nproc=nproc,
                         clobber=True,
                         kernel=kernel,
                         force_final_fit=force_final_fit)

            # write dh/dts for visualisation
            ds = xr.open_dataset(outfile)

            t0 = np.datetime64('2000-01-01')
            t1 = np.datetime64('2020-01-01')

            ft.get_full_dh(ds,
                           os.path.join(
                               os.path.dirname(outfile),
                               os.path.splitext(os.path.basename(outfile))[0]),
                           t0=t0,
                           t1=t1)

        else:
            print('Tile already processed.')
def fit_tile(tile, tmp_ref_dir, out_dir):

    method = 'gpr'
    # subspat = [383000,400000,5106200,5094000]
    subspat = None
    ref_dem_date = np.datetime64('2013-01-01')
    gla_mask = '/calcul/santo/hugonnet/outlines/rgi60_merge.shp'
    inc_mask = '/calcul/santo/hugonnet/outlines/rgi60_buff_10.shp'
    write_filt = True
    clobber = True
    tstep = 1. / 12.
    time_filt_thresh = [-50, 50]
    opt_gpr = False
    kernel = None
    filt_ref = 'both'
    filt_ls = False
    conf_filt_ls = 0.99
    # specify the exact temporal extent needed to be able to merge neighbouring stacks properly
    tlim = [np.datetime64('2000-01-01'), np.datetime64('2020-01-01')]

    lat, lon = SRTMGL1_naming_to_latlon(tile)
    epsg, utm = latlon_to_UTM(lat, lon)
    print('Fitting tile: ' + tile + ' in UTM zone ' + utm)

    # reference DEM
    ref_utm_dir = os.path.join(tmp_ref_dir, utm)
    ref_vrt = os.path.join(ref_utm_dir, 'tmp_' + utm + '.vrt')
    infile = os.path.join(out_dir, utm, tile + '.nc')
    outfile = os.path.join(out_dir, utm, tile + '_final.nc')

    if True:  #not os.path.exists(outfile):
        ft.fit_stack(infile,
                     fit_extent=subspat,
                     fn_ref_dem=ref_vrt,
                     ref_dem_date=ref_dem_date,
                     gla_mask=gla_mask,
                     tstep=tstep,
                     tlim=tlim,
                     inc_mask=inc_mask,
                     filt_ref=filt_ref,
                     time_filt_thresh=time_filt_thresh,
                     write_filt=True,
                     outfile=outfile,
                     method=method,
                     filt_ls=filt_ls,
                     conf_filt_ls=conf_filt_ls,
                     nproc=nproc,
                     clobber=True)

        # write dh/dts for visualisation
        ds = xr.open_dataset(outfile)

        t0 = np.datetime64('2000-01-01')
        t1 = np.datetime64('2020-01-01')

        ft.get_full_dh(ds,
                       os.path.join(
                           os.path.dirname(outfile),
                           os.path.splitext(os.path.basename(outfile))[0]),
                       t0=t0,
                       t1=t1)

    else:
        print('Tile already processed.')