{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Select glaciers within region shapes and basic attributes: Level 2 files" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "L0 and L1 files are too large: they are selected roughly by lon lat boxes and a buffer, so there is a lot of duplication. Here we select glaciers by region by overlaying the glaciers representative points with the region outlines. This reduces the size of the files and make them ready for regional processing.\n", "\n", "In addition, we compute glacier area and store the CenLon, CenLat attributes for later use." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import utils \n", "import geopandas as gpd\n", "import pandas as pd\n", "import numpy as np\n", "import shutil\n", "import glob\n", "import os\n", "import subprocess\n", "import tarfile\n", "import shapely.geometry as shpg\n", "import progressbar\n", "import matplotlib.pyplot as plt\n", "import warnings" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def xy_coord(geom):\n", " x, y = geom.xy\n", " return x[0], y[0]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "# go down from rgi7_scripts/workflow/preprocessing\n", "data_dir = '../../../rgi7_data/'" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "reg_file = os.path.join(data_dir, '00_rgi70_regions', '00_rgi70_O1Regions')\n", "reg_f = gpd.read_file(reg_file)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "../../../rgi7_data//l1_with_interiors/RGI01/RGI01.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI01/\n", "RGI01/RGI01.dbf\n", "RGI01/RGI01.shp\n", "RGI01/RGI01.prj\n", "RGI01/RGI01.cpg\n", "RGI01/RGI01.shx\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI01.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI01'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI02/RGI02.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI02/\n", "RGI02/RGI02.dbf\n", "RGI02/RGI02.shp\n", "RGI02/RGI02.prj\n", "RGI02/RGI02.cpg\n", "RGI02/RGI02.shx\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI02.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI02'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI03/RGI03.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI03/\n", "RGI03/RGI03.dbf\n", "RGI03/RGI03.shp\n", "RGI03/RGI03.cpg\n", "RGI03/RGI03.prj\n", "RGI03/RGI03.shx\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI03.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI03'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI04/RGI04.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI04/\n", "RGI04/RGI04.cpg\n", "RGI04/RGI04.prj\n", "RGI04/RGI04.shx\n", "RGI04/RGI04.shp\n", "RGI04/RGI04.dbf\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI04.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI04'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI05/RGI05.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI05/\n", "RGI05/RGI05.shp\n", "RGI05/RGI05.dbf\n", "RGI05/RGI05.cpg\n", "RGI05/RGI05.prj\n", "RGI05/RGI05.shx\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI05.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI05'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI06/RGI06.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI06/\n", "RGI06/RGI06.shp\n", "RGI06/RGI06.dbf\n", "RGI06/RGI06.shx\n", "RGI06/RGI06.prj\n", "RGI06/RGI06.cpg\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI06.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI06'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI07/RGI07.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI07/\n", "RGI07/RGI07.cpg\n", "RGI07/RGI07.prj\n", "RGI07/RGI07.shx\n", "RGI07/RGI07.dbf\n", "RGI07/RGI07.shp\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI07.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI07'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI08/RGI08.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI08/\n", "RGI08/RGI08.shp\n", "RGI08/RGI08.dbf\n", "RGI08/RGI08.shx\n", "RGI08/RGI08.cpg\n", "RGI08/RGI08.prj\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI08.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI08'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI09/RGI09.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI09/\n", "RGI09/RGI09.dbf\n", "RGI09/RGI09.shp\n", "RGI09/RGI09.shx\n", "RGI09/RGI09.prj\n", "RGI09/RGI09.cpg\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI09.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI09'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI10/RGI10.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI10/\n", "RGI10/RGI10.shp\n", "RGI10/RGI10.dbf\n", "RGI10/RGI10.shx\n", "RGI10/RGI10.prj\n", "RGI10/RGI10.cpg\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI10.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI10'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI11/RGI11.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI11/\n", "RGI11/RGI11.dbf\n", "RGI11/RGI11.shp\n", "RGI11/RGI11.shx\n", "RGI11/RGI11.prj\n", "RGI11/RGI11.cpg\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI11.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI11'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI12/RGI12.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI12/\n", "RGI12/RGI12.prj\n", "RGI12/RGI12.cpg\n", "RGI12/RGI12.shx\n", "RGI12/RGI12.dbf\n", "RGI12/RGI12.shp\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI12.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI12'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI13/RGI13.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI13/\n", "RGI13/RGI13.prj\n", "RGI13/RGI13.cpg\n", "RGI13/RGI13.shx\n", "RGI13/RGI13.shp\n", "RGI13/RGI13.dbf\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI13.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI13'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI14/RGI14.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI14/\n", "RGI14/RGI14.cpg\n", "RGI14/RGI14.prj\n", "RGI14/RGI14.shx\n", "RGI14/RGI14.dbf\n", "RGI14/RGI14.shp\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI14.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI14'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI15/RGI15.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI15/\n", "RGI15/RGI15.shx\n", "RGI15/RGI15.cpg\n", "RGI15/RGI15.prj\n", "RGI15/RGI15.dbf\n", "RGI15/RGI15.shp\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI15.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI15'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI16/RGI16.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI16/\n", "RGI16/RGI16.shp\n", "RGI16/RGI16.dbf\n", "RGI16/RGI16.shx\n", "RGI16/RGI16.cpg\n", "RGI16/RGI16.prj\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI16.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI16'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI17/RGI17.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI17/\n", "RGI17/RGI17.shp\n", "RGI17/RGI17.dbf\n", "RGI17/RGI17.shx\n", "RGI17/RGI17.prj\n", "RGI17/RGI17.cpg\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI17.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI17'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI18/RGI18.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI18/\n", "RGI18/RGI18.shp\n", "RGI18/RGI18.dbf\n", "RGI18/RGI18.shx\n", "RGI18/RGI18.cpg\n", "RGI18/RGI18.prj\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI18.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI18'], returncode=0)\n", "../../../rgi7_data//l1_with_interiors/RGI19/RGI19.shp\n", "Reading...\n", "Pointing...\n", "Area-ing...\n", "Writing...\n", "Taring...\n", "RGI19/\n", "RGI19/RGI19.prj\n", "RGI19/RGI19.cpg\n", "RGI19/RGI19.shx\n", "RGI19/RGI19.dbf\n", "RGI19/RGI19.shp\n", "CompletedProcess(args=['tar', '-zcvf', '../../../rgi7_data//l2_sel_reg_tars/RGI19.tar.gz', '-C', '../../../rgi7_data//l2_sel_reg', 'RGI19'], returncode=0)\n" ] } ], "source": [ "idir = utils.mkdir(data_dir + '/l1_with_interiors')\n", "odir = utils.mkdir(data_dir + '/l2_sel_reg')\n", "odir_tar = utils.mkdir(data_dir + '/l2_sel_reg_tars')\n", "\n", "for reg in range(1, 20):\n", "#for reg in [4, 19]:\n", "\n", " fp = f'{idir}/RGI{reg:02d}/RGI{reg:02d}.shp'\n", " print(fp)\n", " print('Reading...')\n", " shp = gpd.read_file(fp)\n", " \n", " print('Pointing...')\n", " \n", " rp = shp.representative_point()\n", " \n", " coordinates = np.array(list(rp.apply(xy_coord)))\n", " shp['CenLon'] = coordinates[:, 0]\n", " shp['CenLat'] = coordinates[:, 1]\n", " \n", " rp = rp.to_frame('geometry')\n", " rp['orig_index'] = shp.index\n", " \n", " intersect = gpd.overlay(rp, reg_f.loc[reg_f.o1region == f'{reg:02d}'], how='intersection')\n", " \n", " odf = shp.loc[intersect['orig_index']]\n", " \n", " print('Area-ing...')\n", " odf['area'] = odf.to_crs({'proj':'cea'}).area\n", " \n", " dd = utils.mkdir(f'{odir}/RGI{reg:02d}/', reset=True)\n", " \n", " print('Writing...')\n", " odf.to_file(dd + f'RGI{reg:02d}.shp')\n", " \n", " print('Taring...')\n", " print(subprocess.run(['tar', '-zcvf', f'{odir_tar}/RGI{reg:02d}.tar.gz', '-C', odir, f'RGI{reg:02d}']))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Done!\n" ] } ], "source": [ "print('Done!')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.8" } }, "nbformat": 4, "nbformat_minor": 4 }