示例#1
0
def write_raster_to_disk(raster,
                         out_filename,
                         in_filename=r"data/Strat4/DTM_metres_clip.tif"):
    import rasterio
    with rasterio.open(
            in_filename
    ) as src:  #src file is needed to output with the same metadata and attributes
        profile = src.profile
    _, wtd_old, _, _, _ = preprocess_data.read_preprocess_rasters(
        in_filename, in_filename, in_filename, in_filename, in_filename)

    profile.update(nodata=None)  # overrun nodata value given by input raster
    profile.update(
        width=wtd_old.shape[1]
    )  # Shape of rater is changed for hydro simulation inside read_preprocess_rasters. Here we take that shape to output consistently.
    profile.update(height=wtd_old.shape[0])
    # profile.update(dtype='float32', compress='lzw') # What compression to use?
    profile.update(
        dtype='float32'
    )  # instead of 64. To save space, we don't need so much precision. float16 is not supported by GDAL, check: https://github.com/mapbox/rasterio/blob/master/rasterio/dtypes.py

    with rasterio.open(out_filename, 'w', **profile) as dst:
        dst.write(raster.astype(dtype='float32'), 1)

    return 0
示例#2
0
def write_raster_multiband(nbands,
                           rasters_array,
                           STUDY_AREA,
                           out_filename,
                           ref_filename=r"data/Strat4/DTM_metres_clip.tif"):
    # Rasters_array should be a numpy array with the form [bands, rows, col]
    # This means that if there are 4 bands in the raster, the shape of the array
    # should be (4, nrows, ncols)
    # Metadata is copied from the raster at ref_filename
    import rasterio
    import preprocess_data

    with rasterio.open(
            ref_filename
    ) as src:  #src file is needed to output with the same metadata and attributes
        profile = src.profile
    _, _, ref_raster, _, _, _, _ = preprocess_data.read_preprocess_rasters(
        STUDY_AREA, ref_filename, ref_filename, ref_filename, ref_filename,
        ref_filename, ref_filename, ref_filename)

    if nbands != rasters_array.shape[0]:
        raise ValueError("Number of bands does not match  raster array shape")

    profile.update(nodata=None)  # overrun nodata value given by input raster
    profile.update(
        width=ref_raster.shape[1]
    )  # Shape of rater is changed for hydro simulation inside read_preprocess_rasters. Here we take that shape to output consistently.
    profile.update(height=ref_raster.shape[0])
    profile.update(count=nbands)
    profile.update(
        dtype='float32'
    )  # instead of 64. To save space, we don't need so much precision. float16 is not supported by GDAL, check: https://github.com/mapbox/rasterio/blob/master/rasterio/dtypes.py

    with rasterio.open(out_filename, 'w', **profile) as dst:
        dst.write(rasters_array.astype(dtype='float32'))

    return 0
示例#3
0
#land_use_rst_fn = preprocessed_datafolder + r"/Landcover2017_clip.tif" # Not used
peat_depth_rst_fn = preprocessed_datafolder + r"/Peattypedepth_clip.tif"  # peat depth, peat type in the same raster

abs_path_data = os.path.abspath(
    './data'
)  # Absolute path to data folder needed for Excel file with parameters
params_fn = abs_path_data + r"/params.xlsx"

if 'CNM' and 'cr' and 'c_to_r_list' not in globals():
    CNM, cr, c_to_r_list = preprocess_data.gen_can_matrix_and_raster_from_raster(
        can_rst_fn=can_rst_fn, dem_rst_fn=dem_rst_fn)

#else:
#    print("Canal adjacency matrix and raster loaded from memory.")

_, dem, peat_type_arr, peat_depth_arr = preprocess_data.read_preprocess_rasters(
    can_rst_fn, dem_rst_fn, peat_depth_rst_fn, peat_depth_rst_fn)

PARAMS_df = preprocess_data.read_params(params_fn)
BLOCK_HEIGHT = PARAMS_df.block_height[0]
CANAL_WATER_LEVEL = PARAMS_df.canal_water_level[0]
DIRI_BC = PARAMS_df.diri_bc[0]
HINI = PARAMS_df.hini[0]
P = PARAMS_df.P[0]
ET = PARAMS_df.ET[0]
TIMESTEP = PARAMS_df.timeStep[0]
KADJUST = PARAMS_df.Kadjust[0]

print(">>>>> WARNING, OVERWRITING PEAT DEPTH")
peat_depth_arr[peat_depth_arr < 2.] = 2.

# catchment mask
示例#4
0
    cnm_sim = cnm + cnm.T

    CNM = scipy.sparse.csr_matrix(cnm) # In order to use the same sparse type as the big ass true adjacency matrix

else:
    filenames_df = pd.read_excel('file_pointers.xlsx', header=2, dtype=str)
    
    dem_rst_fn = Path(filenames_df[filenames_df.Content == 'DEM'].Path.values[0])
    can_rst_fn = Path(filenames_df[filenames_df.Content == 'canal_raster'].Path.values[0])
    # TODO: use peat depth as depth of canals??
    peat_depth_rst_fn = Path(filenames_df[filenames_df.Content == 'peat_depth_raster'].Path.values[0])

    # Choose smaller study area
    STUDY_AREA = (0,-1), (0,-1) # E.g., a study area of (0,-1), (0,-1) is the whole domain
    
    can_arr, wtd_old , dem, _, peat_depth_arr, _, _ = preprocess_data.read_preprocess_rasters(STUDY_AREA, dem_rst_fn, can_rst_fn, dem_rst_fn, peat_depth_rst_fn, peat_depth_rst_fn, dem_rst_fn, dem_rst_fn)  
    labelled_canals = preprocess_data.label_canal_pixels(can_arr, dem)
    CNM, c_to_r_list = preprocess_data.gen_can_matrix_and_label_map(labelled_canals, dem)
    dem_nodes = [dem[loc] for loc in c_to_r_list]
    dem_nodes[0] = 3.0 # something strange happens with this node
    dem_nodes = np.array(dem_nodes)
        


n_edges = np.sum(CNM)
n_nodes = CNM.shape[0]
DIST_BETWEEN_NODES = 100. # m

#%%
# Hydrology-relevant data: BC, IC, rainfall, etc.
示例#5
0
            [get_day_rainfall(api_url)]
        ) * 25.4  # From inches to mm. array type is to allow for list of precip. Usually, single value is used.
    except:
        raise ConnectionError(
            "Could not get precipitation data from the weather station.")
"""
LOOP FOR HISTORICAL PRECIPITATION DATA
If only daily precipitation, then precip is a list of length 1.
"""
for d, P in enumerate(precip):

    if HISTORIC_PRECIPITATION and d == 0:
        wtd_old = 0.  # The initial condition is fully saturated.
        wtd_old_fn = dem_rst_fn  # this works for raster reading and writing functions
        _, _, dem, peat_type_arr, peat_depth_arr = preprocess_data.read_preprocess_rasters(
            wtd_old_fn, can_rst_fn, dem_rst_fn, peat_depth_rst_fn,
            peat_depth_rst_fn)

    else:
        wtd_old_fn = previous_wtd_fname(WTD_folder, hp=HISTORIC_PRECIPITATION)
        _, wtd_old, dem, peat_type_arr, peat_depth_arr = preprocess_data.read_preprocess_rasters(
            wtd_old_fn, can_rst_fn, dem_rst_fn, peat_depth_rst_fn,
            peat_depth_rst_fn)
    """
    RUN HYDROLOGICAL MODEL
    """
    DAYS = 1  # number of days with daily timestep for hydrological module
    N_BLOCKS = 0

    # Generate adjacency matrix, and dictionary. Need to do this every time?
    CNM, cr, c_to_r_list = preprocess_data.gen_can_matrix_and_raster_from_raster(
示例#6
0
"""
filenames_df = pd.read_excel('file_pointers.xlsx', header=2, dtype=str)

dem_rst_fn = Path(filenames_df[filenames_df.Content == 'DEM'].Path.values[0])
can_rst_fn = Path(filenames_df[filenames_df.Content == 'canal_raster'].Path.values[0])
peat_depth_rst_fn = Path(filenames_df[filenames_df.Content == 'peat_depth_raster'].Path.values[0])
blocks_fn = Path(filenames_df[filenames_df.Content == 'canal_blocks_raster'].Path.values[0])
sensor_loc_fn = Path(filenames_df[filenames_df.Content == 'sensor_locations'].Path.values[0])
params_fn = Path(filenames_df[filenames_df.Content == 'parameters'].Path.values[0])
WTD_folder = Path(filenames_df[filenames_df.Content == 'WTD_input_and_output_folder'].Path.values[0])
weather_fn = Path(filenames_df[filenames_df.Content == 'historic_precipitation'].Path.values[0])
# Choose smaller study area
STUDY_AREA = (0,-1), (0,-1)

wtd_old_fn = dem_rst_fn # not reading from previous wtd raster
can_arr, wtd_old , dem, peat_type_arr, peat_depth_arr, blocks_arr, sensor_loc_arr = preprocess_data.read_preprocess_rasters(STUDY_AREA, wtd_old_fn, can_rst_fn, dem_rst_fn, peat_depth_rst_fn, peat_depth_rst_fn, blocks_fn, sensor_loc_fn)

sensor_loc_indices = utilities.get_sensor_loc_array_indices(sensor_loc_arr)

if 'CNM' and 'labelled_canals' and 'c_to_r_list' not in globals():
    labelled_canals = preprocess_data.label_canal_pixels(can_arr, dem)
    CNM, c_to_r_list = preprocess_data.gen_can_matrix_and_label_map(labelled_canals, dem)
 
built_block_positions = utilities.get_already_built_block_positions(blocks_arr, labelled_canals)

PARAMS_df = preprocess_data.read_params(params_fn)
BLOCK_HEIGHT = PARAMS_df.block_height[0]; CANAL_WATER_LEVEL = PARAMS_df.canal_water_level[0]
DIRI_BC = PARAMS_df.diri_bc[0]; HINI = PARAMS_df.hini[0]; P = PARAMS_df.P[0]
ET = PARAMS_df.ET[0]; TIMESTEP = PARAMS_df.timeStep[0]; KADJUST = PARAMS_df.Kadjust[0]

print(">>>>> WARNING, OVERWRITING PEAT DEPTH")