def test_correct_geometries_cli(tmpdir): from rgitools.cli import correct_geometries rgi_dir = os.path.join(str(tmpdir), 'RGIV60') rgi_reg_dir = os.path.join(str(tmpdir), 'RGIV60', '06_rgi60_Iceland') mkdir(rgi_reg_dir) for e in ['.shp', '.prj', '.dbf', '.shx']: shutil.copyfile(get_demo_file('RGI6_icecap' + e), os.path.join(rgi_reg_dir, '06_rgi60_Iceland' + e)) out_dir = os.path.join(str(tmpdir), 'RGIV61') def replace(s): return s.replace('rgi60', 'rgi61') correct_geometries.run(rgi_dir, out_dir, replace_str=replace) outf = os.path.join(out_dir, '06_rgi61_Iceland', '06_rgi61_Iceland.shp') assert os.path.exists(outf) # All df = get_iceland_df() out = gpd.read_file(outf) assert len(out) == len(df) assert np.all(g.is_valid for g in out.geometry) assert np.any(out.check_geom != '')
def run(input_dir, output_file): """Zips an RGI directory and makes it look like a real one. Parameters ---------- input_dir : str path to the RGI directory output_file : str path to the output file (without zip ending!) """ # First zip the directories and copy the files bname = os.path.basename(input_dir) tmpdir = tempfile.mkdtemp() workdir = os.path.join(tmpdir, bname) funcs.mkdir(workdir, reset=True) for fname in os.listdir(input_dir): abs_p = os.path.join(input_dir, fname) out_f = os.path.join(workdir, fname) if os.path.isfile(abs_p): shutil.copy(abs_p, out_f) else: shutil.make_archive(out_f, 'zip', abs_p) # Compress the working directory shutil.make_archive(output_file, 'zip', workdir) # Delete our working dir shutil.rmtree(tmpdir)
def test_intersects_cli(tmpdir): from rgitools.cli import compute_intersects rgi_dir = os.path.join(str(tmpdir), 'RGIV60') rgi_reg_dir = os.path.join(str(tmpdir), 'RGIV60', '06_rgi60_Iceland') mkdir(rgi_reg_dir) for e in ['.shp', '.prj', '.dbf', '.shx']: shutil.copyfile(get_demo_file('RGI6_icecap' + e), os.path.join(rgi_reg_dir, '06_rgi60_Iceland' + e)) out_dir = os.path.join(str(tmpdir), 'RGIV60_intersects') compute_intersects.run(rgi_dir, out_dir) assert os.path.exists( os.path.join(out_dir, '06_rgi60_Iceland', 'intersects_06_rgi60_Iceland.shp'))
def run(input_dir=None, output_dir=None, *, replace_str=None, n_processes=None): """Corrects the geometries for an entire RGI directory. Parameters ---------- input_dir : str path to the RGI directory output_dir : str path to the output directory replace_str : callable a function to call on the file's basename. A good example is: ``replace_str=lambda x : x.replace('rgi60', 'rgi61')`` n_processes : int, optional the number of processors to use """ # Download RGI files fp = '*_rgi*_*.shp' rgi_shps = list(glob(os.path.join(input_dir, "*", fp))) rgi_shps = sorted([r for r in rgi_shps if 'Regions' not in r]) funcs.mkdir(output_dir) out_paths = [] log_names = [] for rgi_shp in rgi_shps: odir = os.path.basename(os.path.dirname(rgi_shp)) if replace_str: odir = replace_str(odir) odir = os.path.join(output_dir, odir) funcs.mkdir(odir) bn = os.path.basename(rgi_shp) if replace_str: bn = replace_str(bn) of = os.path.join(odir, bn) out_paths.append(of) log_names.append(bn) with mp.Pool(n_processes) as p: p.starmap(funcs.mappable_func, zip([funcs.check_geometries] * len(rgi_shps), rgi_shps, out_paths, log_names), chunksize=1)
def test_zip_cli(tmpdir): from rgitools.cli import zip_rgi_dir rgi_dir = os.path.join(str(tmpdir), 'rgi_61') outf = os.path.join(str(tmpdir), 'rgi_61') regdirs = ['06_rgi61_Iceland', '07_rgi61_Scandinavia'] for regdir in regdirs: rgi_reg_dir = os.path.join(rgi_dir, regdir) mkdir(rgi_reg_dir) for e in ['.shp', '.prj', '.dbf', '.shx']: shutil.copyfile(get_demo_file('RGI6_icecap' + e), os.path.join(rgi_reg_dir, '01_rgi61_Iceland' + e)) zip_rgi_dir.run(rgi_dir, outf) assert os.path.exists(outf)
def test_hypsometries_cli(tmpdir): from rgitools.cli import compute_hypsometries, correct_geometries rgi_dir = os.path.join(str(tmpdir), 'RGIV60') rgi_reg_dir = os.path.join(str(tmpdir), 'RGIV60', '11_rgi60_Europe') mkdir(rgi_reg_dir) for e in ['.shp', '.prj', '.dbf', '.shx']: shutil.copyfile(get_demo_file('rgi_oetztal' + e), os.path.join(rgi_reg_dir, '11_rgi60_Europe' + e)) tmp_dir = os.path.join(str(tmpdir), 'RGIV61') def replace(s): return s.replace('rgi60', 'rgi61') correct_geometries.run(rgi_dir, tmp_dir, replace_str=replace) outf = os.path.join(tmp_dir, '11_rgi61_Europe', '11_rgi61_Europe.shp') assert os.path.exists(outf) # All df = gpd.read_file(get_demo_file('rgi_oetztal.shp')) out = gpd.read_file(outf) assert len(out) == len(df) assert np.all(g.is_valid for g in out.geometry) assert np.any(out.check_geom != '') out_dir = os.path.join(str(tmpdir), 'RGIV62') def replace(s): return s.replace('rgi61', 'rgi62') compute_hypsometries.run(tmp_dir, out_dir, replace_str=replace, set_oggm_params=set_oggm_params) outf = os.path.join(out_dir, '11_rgi62_Europe', '11_rgi62_Europe.shp') assert os.path.exists(outf) outf = os.path.join(out_dir, '11_rgi62_Europe', '11_rgi62_Europe_hypso.csv') assert os.path.exists(outf) outf = os.path.join(out_dir, '11_rgi62_Europe', '11_rgi62_Europe_hypso.csv') assert os.path.exists(outf)
def run(input_dir=None, output_dir=None, *, n_processes=None): """Computes the intersects for an entire RGI directory. Parameters ---------- input_dir : str path to the RGI directory output_dir : str path to the output directory n_processes : int, optional the number of processors to use """ # Download RGI files fp = '*_rgi*_*.shp' rgi_shps = list(glob(os.path.join(input_dir, "*", fp))) rgi_shps = sorted([r for r in rgi_shps if 'Regions' not in r]) funcs.mkdir(output_dir) out_paths = [] log_names = [] for rgi_shp in rgi_shps: odir = os.path.basename(os.path.dirname(rgi_shp)) odir = os.path.join(output_dir, odir) funcs.mkdir(odir) bn = os.path.basename(rgi_shp) of = os.path.join(odir, 'intersects_' + bn) out_paths.append(of) log_names.append(bn) with mp.Pool(n_processes) as p: p.starmap(funcs.mappable_func, zip([funcs.compute_intersects] * len(rgi_shps), rgi_shps, out_paths, log_names), chunksize=1)
def run(input_dir=None, output_dir=None, *, replace_str=None, oggm_working_dir='', set_oggm_params=None, n_processes=None): """Computes the hypsometries for an entire RGI directory. Parameters ---------- input_dir : str path to the RGI directory output_dir : str path to the output directory replace_str : callable a function to call on the file's basename. A good example is: ``replace_str=lambda x : x.replace('rgi60', 'rgi61')`` oggm_working_dir : str str, optional path to the folder where oggm will write its GlacierDirectories. Default is to use a temporary folder (not recommended) set_oggm_params : callable a function which sets the OGGM params on cfg. The default is to turn multiprocessing off. n_processes : int, optional the number of processors to use """ # Input check if set_oggm_params is None: set_oggm_params = _set_oggm_params # Get RGI files fp = '*_rgi*_*.shp' rgi_shps = list(glob(os.path.join(input_dir, "*", fp))) rgi_shps = sorted([r for r in rgi_shps if 'Regions' not in r]) funcs.mkdir(output_dir) out_paths = [] log_names = [] for rgi_shp in rgi_shps: odir = os.path.basename(os.path.dirname(rgi_shp)) if replace_str: odir = replace_str(odir) odir = os.path.join(output_dir, odir) funcs.mkdir(odir) bn = os.path.basename(rgi_shp) if replace_str: bn = replace_str(bn) bn = bn.replace('.shp', '') of = os.path.join(odir, bn) out_paths.append(of) log_names.append(bn) with mp.Pool(n_processes) as p: p.starmap(funcs.mappable_func, zip( [funcs.hypsometries] * len(rgi_shps), rgi_shps, out_paths, log_names, [set_oggm_params] * len(rgi_shps), [oggm_working_dir] * len(rgi_shps), ), chunksize=1)