Exemple #1
0
def write_gauge_stage_all_cells(reccsv, dirtif, wdttif, gaugelfp, stagelfp):

    print("     writing gauge and stage files...")

    # Reading rec file
    rec = pd.read_csv(reccsv)

    # Create a width dataframe
    dat = gdalutils.get_data(wdttif)
    geo = gdalutils.get_geo(wdttif)
    wdt = gdalutils.array_to_pandas(dat, geo, 0, 'gt')
    wdt.columns = ['x', 'y', 'width']

    # Create directions dataframe
    dat = gdalutils.get_data(dirtif)
    geo = gdalutils.get_geo(dirtif)
    drc = gdalutils.array_to_pandas(dat, geo, 0, 'gt')
    drc.columns = ['x', 'y', 'direction']

    # Find widths and directions for every lon, lat in river network
    gdalutils.assign_val(df2=rec,
                         df2_x='lon',
                         df2_y='lat',
                         df1=wdt,
                         df1_x='x',
                         df1_y='y',
                         label='width',
                         copy=False)
    gdalutils.assign_val(df2=rec,
                         df2_x='lon',
                         df2_y='lat',
                         df1=drc,
                         df1_x='x',
                         df1_y='y',
                         label='direction',
                         copy=False)

    # Change numbers (1,2,3,4,5,6,7) to letters (N,S,E,W)
    rec['direction_let'] = rec['direction'].apply(getdirletter)

    # Writing .gauge file
    with open(gaugelfp, 'w') as f:
        f.write(str(rec.shape[0]) + '\n')
    rec[['lon', 'lat', 'direction_let', 'width']].to_csv(gaugelfp,
                                                         index=False,
                                                         sep=' ',
                                                         header=False,
                                                         float_format='%.7f',
                                                         mode='a')

    # Writing .stage file
    with open(stagelfp, 'w') as f:
        f.write(str(rec.shape[0]) + '\n')
    rec[['lon', 'lat']].to_csv(stagelfp,
                               index=False,
                               sep=' ',
                               header=False,
                               float_format='%.7f',
                               mode='a')
Exemple #2
0
def getdir(rec, dirtif):

    dat = gdalutils.get_data(dirtif)
    geo = gdalutils.get_geo(dirtif)
    dirdf = gdalutils.array_to_pandas(dat, geo, 0, 'gt')
    recdf = gdalutils.assign_val(df2=rec.reset_index(
    ), df2_x='lon', df2_y='lat', df1=dirdf, df1_x='x', df1_y='y', label='z', copy=True)

    # Direction of outlet is given by the maximum repetitions of directions in the last 10 points
    _dir = recdf.sort_values(by='distance').iloc[0:10].groupby('z')[
        'z'].count().idxmax()

    return _dir
Exemple #3
0
def find_nearest_mean_mask(ncf,
                           ncproj,
                           lon,
                           lat,
                           proj,
                           thresh_dis,
                           thresh_mean=5):
    """
    Apply a threshold to the mean discharge
    Based on the thresholded map, find nearest value to lon, lat in a given perimiter and var threhsold
    For JRC data default values are 5 m3s-1 and 2.5 Km
    """

    # JRC data set projection is EPSG:3035
    # It's required to convert to WGS84 to perform distance calculation
    crs_wgs84 = Proj(init=proj)
    crs_nc = Proj(init=ncproj)

    # Reading mean mask
    dat = gu.get_data(ncf)
    geo = gu.get_geo(ncf)

    # Create df, a pandas dataframe with values larger than "thresh_mean=5"
    df = gu.array_to_pandas(dat, geo, thresh_mean, 'ge')

    # Creating two new columns with projected values
    coords = transform(crs_nc, crs_wgs84, df['x'].values, df['y'].values)
    df['lon'] = coords[0]
    df['lat'] = coords[1]

    # Calcualte distance to lat and lon point to every point in the dataframe
    vec = gu.haversine.haversine_array(np.array(df['lat'], dtype='float32'),
                                       np.array(df['lon'], dtype='float32'),
                                       np.float32(lat), np.float32(lon))

    idx = np.argmin(vec)
    dis = gu.haversine.haversine(df.loc[idx, 'lat'], df.loc[idx, 'lon'], lat,
                                 lon)

    if dis <= thresh_dis:
        near_x = df.loc[idx, 'x']
        near_y = df.loc[idx, 'y']
        mymean = df.loc[idx, 'z']
        df = None
        return near_x, near_y, mymean, dis
    else:
        df = None
        return None, None, None, None
Exemple #4
0
def getbankelevs(output, recf, netf, hrdemf, proj, method, hrnodata, thresh,
                 outlier):

    print("    running getbankelevs.py...")

    fname = output

    w = shapefile.Writer(shapefile.POINT)
    w.field('x')
    w.field('y')
    w.field('elev')

    # Coordinates for bank elevations are based on the Rec file
    rec = pd.read_csv(recf)

    for x, y in zip(rec['lon'], rec['lat']):

        xmin = x - thresh
        ymin = y - thresh
        xmax = x + thresh
        ymax = y + thresh

        dem, dem_geo = gdalutils.clip_raster(hrdemf, xmin, ymin, xmax, ymax)
        ddem = np.ma.masked_where(dem == hrnodata, dem)

        if method == 'near':
            nodata = dem_geo[11]
            dfdem = gdalutils.array_to_pandas(dem, dem_geo, nodata, 'gt')
            arr = haversine.haversine_array(
                np.array(dfdem['y'].values, dtype='float32'),
                np.float32(dfdem['x'].values), np.float32(y), np.float32(x))
            dfdem['dis'] = np.array(arr)
            dfdem.sort_values(by='dis', inplace=True)
            elev = dfdem.iloc[0, 2]

        elif method == 'meanmin':
            if outlier == "yes":
                ddem = check_outlier(dem, ddem, hrnodata, 3.5)
            elev = np.mean([ddem.mean(), ddem.min()])

        elif method == 'mean':
            if outlier == "yes":
                ddem = check_outlier(dem, ddem, hrnodata, 3.5)
            elev = ddem.mean()

        elif method == 'min':
            if outlier == "yes":
                ddem = check_outlier(dem, ddem, hrnodata, 3.5)
            elev = ddem.min()

        # Write final file in a shapefile

        if np.isfinite(elev):
            w.point(x, y)
            w.record(x, y, elev)

    w.save("%s.shp" % fname)

    # Write .prj file
    prj = open("%s.prj" % fname, "w")
    srs = osr.SpatialReference()
    srs.ImportFromProj4(proj)
    prj.write(srs.ExportToWkt())
    prj.close()

    geo = gdalutils.get_geo(netf)

    fmt = "GTiff"
    nodata = -9999
    bnkname1 = output + ".shp"
    bnkname2 = output + ".tif"
    subprocess.call([
        "gdal_rasterize", "-a_nodata",
        str(nodata), "-of", fmt, "-co", "COMPRESS=DEFLATE", "-tr",
        str(geo[6]),
        str(geo[7]), "-a", "elev", "-a_srs", proj, "-te",
        str(geo[0]),
        str(geo[1]),
        str(geo[2]),
        str(geo[3]), bnkname1, bnkname2
    ])