# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# %matplotlib inline
import xarray as xr
import cartopy
import cartopy.crs as ccrs
import salem
import pandas as pd
import geopandas as gpd
from oggm import utils
import os, glob

# +
# Get the RGI
rgi_dir = utils.get_rgi_dir(version='62')

fs = list(sorted(glob.glob(rgi_dir + "/*/*_rgi6*_*.shp")))[2:]
out = []
for f in fs:
    sh = gpd.read_file(f).set_index('RGIId')
    del sh['geometry']
    out.append(pd.DataFrame(sh))
mdf = pd.concat(out)

mdf['O1Region'] = ['{:02d}'.format(int(i)) for i in mdf['O1Region']]
mdf['O2Region'] = ['{:02d}'.format(int(i)) for i in mdf['O2Region']]

# Read glacier attrs
gtkeys = {0: 'Glacier',
          1: 'Ice cap',
          2: 'Perennial snowfield',
          3: 'Seasonal snowfield',
          9: 'Not assigned',
          }
ttkeys = {0: 'Land-terminating',
          1: 'Marine-terminating',
          2: 'Lake-terminating',
          3: 'Dry calving',
          4: 'Regenerated',
          5: 'Shelf-terminating',
          9: 'Not assigned',
          }
stkeys = {0: 'Glacier or ice cap',
          1: 'Glacier complex',
          2: 'Nominal glacier',
          9: 'Not assigned',
          }
mdf['GlacierType'] = [gtkeys[g] for g in mdf.Form]
mdf['TerminusType'] = [ttkeys[g] for g in mdf.TermType]
mdf['GlacierStatus'] = [stkeys[g] for g in mdf.Status]
mdf['IsTidewater'] = [ttype in ['Marine-terminating', 'Lake-terminating'] for ttype in mdf.TerminusType]
mdf['IsNominal'] = [stype == 'Nominal glacier' for stype in mdf.GlacierStatus]
# -

mdf = mdf.drop(['check_geom'], axis=1)

for i, d in mdf.iterrows():
    assert i[6:8] == d['O1Region']

mdf.to_hdf('rgi62_stats.h5', key='df', mode='w', complevel=5)

df = pd.read_hdf('rgi62_stats.h5')

df.columns

df = df.loc[df['Connect'] != 2]

df['rgi_year'] = [int(s[0:4]) for s in df.BgnDate]

len(df.loc[df['rgi_year'] < 0])

df = df.loc[df['rgi_year'] > 0]

df['rgi_year'].min()

df.groupby('O1Region').median()['rgi_year']



StarcoderdataJupyterScriptsDedupFiltered