-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
258 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import os | ||
import geopandas as gpd | ||
import numpy as np | ||
import oggm | ||
from oggm import cfg, tasks, graphics | ||
from oggm.utils import get_demo_file | ||
import matplotlib.pyplot as plt | ||
import xarray as xr | ||
import scipy.optimize as optimization | ||
from oggm.sandbox.gmd_paper import PLOT_DIR | ||
from oggm.core.preprocessing.climate import (t_star_from_refmb, | ||
local_mustar_apparent_mb) | ||
from oggm.core.preprocessing.inversion import (mass_conservation_inversion) | ||
from oggm.core.models.flowline import (FluxBasedModel) | ||
from oggm.core.models.massbalance import (RandomMassBalanceModel) | ||
|
||
cfg.initialize() | ||
|
||
# Don't use divides for now, or intersects | ||
cfg.set_divides_db() | ||
cfg.set_intersects_db() | ||
|
||
cfg.PATHS['dem_file'] = get_demo_file('hef_srtm.tif') | ||
cfg.PARAMS['border'] = 60 | ||
cfg.PARAMS['auto_skip_task'] = True | ||
|
||
base_dir = os.path.join(os.path.expanduser('~/tmp'), 'OGGM_GMD', 'dynamics') | ||
entity = gpd.read_file(get_demo_file('Hintereisferner_RGI5.shp')).iloc[0] | ||
gdir = oggm.GlacierDirectory(entity, base_dir=base_dir) | ||
|
||
tasks.define_glacier_region(gdir, entity=entity) | ||
tasks.glacier_masks(gdir) | ||
tasks.compute_centerlines(gdir) | ||
tasks.compute_downstream_lines(gdir) | ||
tasks.initialize_flowlines(gdir) | ||
tasks.compute_downstream_bedshape(gdir) | ||
tasks.catchment_area(gdir) | ||
tasks.catchment_intersections(gdir) | ||
tasks.catchment_width_geom(gdir) | ||
tasks.catchment_width_correction(gdir) | ||
tasks.process_cru_data(gdir) | ||
tasks.mu_candidates(gdir, div_id=0) | ||
tasks.compute_ref_t_stars([gdir]) | ||
tasks.distribute_t_stars([gdir]) | ||
tasks.prepare_for_inversion(gdir) | ||
tasks.volume_inversion(gdir, use_cfg_params={'glen_a':cfg.A, 'fs':0}) | ||
tasks.filter_inversion_output(gdir) | ||
tasks.init_present_time_glacier(gdir) | ||
|
||
|
||
reset = False | ||
tasks.random_glacier_evolution(gdir, nyears=500, bias=0, seed=0, | ||
filesuffix='seed0', reset=reset) | ||
|
||
tasks.random_glacier_evolution(gdir, nyears=500, bias=0, seed=1, | ||
filesuffix='seed1', reset=reset) | ||
|
||
tasks.random_glacier_evolution(gdir, nyears=500, bias=0, seed=2, | ||
filesuffix='seed2', reset=reset) | ||
|
||
f = gdir.get_filepath('model_diagnostics', filesuffix='seed0') | ||
ds1 = xr.open_dataset(f) | ||
f = gdir.get_filepath('model_diagnostics', filesuffix='seed1') | ||
ds2 = xr.open_dataset(f) | ||
f = gdir.get_filepath('model_diagnostics', filesuffix='seed2') | ||
ds3 = xr.open_dataset(f) | ||
|
||
# ds1.volume_m3.plot() | ||
# ds2.volume_m3.plot() | ||
# ds3.volume_m3.plot() | ||
# plt.show() | ||
# | ||
# ds1.area_m2.plot() | ||
# ds2.area_m2.plot() | ||
# ds3.area_m2.plot() | ||
# plt.show() | ||
|
||
ds1.length_m.rolling(time=5, center=True).mean().plot() | ||
ds2.length_m.rolling(time=5, center=True).mean().plot() | ||
ds3.length_m.rolling(time=5, center=True).mean().plot() | ||
plt.show() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""This script reads all the RGI files and computes intersects out of them.""" | ||
import multiprocessing as mp | ||
import os | ||
from glob import glob | ||
|
||
from oggm import cfg | ||
from oggm.utils import get_rgi_dir | ||
|
||
# Download RGI files | ||
cfg.initialize() | ||
rgi_dir = get_rgi_dir() | ||
rgi_shps = list(glob(os.path.join(rgi_dir, "*", '*_rgi50_*.shp'))) | ||
rgi_shps = [r for r in rgi_shps if 'Regions' not in r] | ||
|
||
from oggm.sandbox.rgi_tools.tools import compute_intersects | ||
|
||
with mp.Pool() as p: | ||
p.map(compute_intersects, rgi_shps, chunksize=1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
"""This script reads all the RGI files and computes intersects out of them.""" | ||
import os | ||
import geopandas as gpd | ||
from shapely.ops import linemerge | ||
import shapely.geometry as shpg | ||
from oggm.utils import haversine, mkdir | ||
from oggm.core.preprocessing.gis import _check_geometry | ||
from salem import wgs84 | ||
import time | ||
|
||
OUTDIR = '/home/mowglie/disk/Data/GIS/SHAPES/RGI/RGI_V5_Intersects' | ||
|
||
|
||
def compute_intersects(rgi_shp): | ||
"""Processes the rgi file and writes the interscects to out_path""" | ||
|
||
out_path = os.path.basename(rgi_shp) | ||
odir = os.path.basename(os.path.dirname(rgi_shp)) | ||
odir = os.path.join(OUTDIR, odir) | ||
mkdir(odir) | ||
out_path = os.path.join(odir, 'intersects_' + out_path) | ||
|
||
print('Start ' + os.path.basename(rgi_shp) + ' ...') | ||
start_time = time.time() | ||
|
||
gdf = gpd.read_file(rgi_shp) | ||
|
||
# clean geometries like OGGM does | ||
gdf['geometry'] = [_check_geometry(g) for g in gdf.geometry] | ||
|
||
out_cols = ['RGIId_1', 'RGIId_2', 'geometry'] | ||
out = gpd.GeoDataFrame(columns=out_cols) | ||
|
||
for i, major in gdf.iterrows(): | ||
|
||
# Exterior only | ||
major_poly = major.geometry.exterior | ||
|
||
# sort by distance to the current glacier | ||
gdf['dis'] = haversine(major.CenLon, major.CenLat, | ||
gdf.CenLon, gdf.CenLat) | ||
gdfs = gdf.sort_values(by='dis').iloc[1:] | ||
|
||
# Keep glaciers in which intersect | ||
gdfs = gdfs.loc[gdfs.dis < 200000] | ||
gdfs = gdfs.loc[gdfs.intersects(major_poly)] | ||
|
||
for i, neighbor in gdfs.iterrows(): | ||
|
||
if neighbor.RGIId in out.RGIId_1 or neighbor.RGIId in out.RGIId_2: | ||
continue | ||
|
||
# Exterior only | ||
# Buffer is needed for numerical reasons | ||
neighbor_poly = neighbor.geometry.exterior.buffer(0.0001) | ||
|
||
# Go | ||
try: | ||
mult_intersect = major_poly.intersection(neighbor_poly) | ||
except: | ||
continue | ||
|
||
if isinstance(mult_intersect, shpg.Point): | ||
continue | ||
if isinstance(mult_intersect, shpg.linestring.LineString): | ||
mult_intersect = [mult_intersect] | ||
if len(mult_intersect) == 0: | ||
continue | ||
mult_intersect = [m for m in mult_intersect if | ||
not isinstance(m, shpg.Point)] | ||
if len(mult_intersect) == 0: | ||
continue | ||
mult_intersect = linemerge(mult_intersect) | ||
if isinstance(mult_intersect, shpg.linestring.LineString): | ||
mult_intersect = [mult_intersect] | ||
for line in mult_intersect: | ||
assert isinstance(line, shpg.linestring.LineString) | ||
line = gpd.GeoDataFrame([[major.RGIId, neighbor.RGIId, line]], | ||
columns=out_cols) | ||
out = out.append(line) | ||
|
||
out.crs = wgs84.srs | ||
out.to_file(out_path) | ||
|
||
print(os.path.basename(rgi_shp) + | ||
' took {0:.2f} seconds'.format(time.time() - start_time)) | ||
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters