22pygemfxns_preprocessing.py is a list of the model functions that are used to preprocess the data into the proper format.
33
44"""
5+ print ('PROCESS FILE USING RASTERENV ENVIRONMENT' )
56
67# Built-in libraries
78import os
89import argparse
910# External libraries
1011from osgeo import gdal
11- import geopandas as gpd
12+ # import geopandas as gpd
1213import matplotlib .pyplot as plt
1314import numpy as np
1415import pandas as pd
15- import shapely
16+ # import shapely
1617
1718from pygeotools .lib import iolib , warplib , geolib , timelib , malib
1819
@@ -45,39 +46,43 @@ def plot3panel(dem_list, clim=None, titles=None, cmap='inferno', label=None, ove
4546 fig .savefig (fn , bbox_inches = 'tight' , pad_inches = 0 , dpi = 150 )
4647
4748#Input DEM filenames
48- dem_ref_fn = '/Users/davidrounce/Documents/Dave_Rounce/HiMAT/DEMs/Alaska_albers_V3_mac/Alaska_albers_V3.tif'
49+ dem_ref_fn = None
50+ # dem_ref_fn = '/Users/davidrounce/Documents/Dave_Rounce/HiMAT/DEMs/Alaska_albers_V3_mac/Alaska_albers_V3.tif'
4951thickness_fp_prefix = ('/Users/davidrounce/Documents/Dave_Rounce/HiMAT/IceThickness_Farinotti/' +
5052 'composite_thickness_RGI60-all_regions/' )
51- dem_farinotti_fp = ('/Users/davidrounce/Documents/Dave_Rounce/HiMAT/IceThickness_Farinotti/surface_DEMs_RGI60/' +
52- 'surface_DEMs_RGI60-01/' )
53+ # dem_farinotti_fp = ('/Users/davidrounce/Documents/Dave_Rounce/HiMAT/IceThickness_Farinotti/surface_DEMs_RGI60/' +
54+ # 'surface_DEMs_RGI60-01/')
55+ dem_farinotti_fp_prefix = '/Users/davidrounce/Documents/Dave_Rounce/HiMAT/IceThickness_Farinotti/surface_DEMs_RGI60/'
5356output_fp = '/Users/davidrounce/Documents/Dave_Rounce/HiMAT/IceThickness_Farinotti/output/'
5457fig_fp = output_fp + 'figures/'
5558if os .path .exists (output_fp ) == False :
5659 os .makedirs (output_fp )
5760if os .path .exists (fig_fp ) == False :
5861 os .makedirs (fig_fp )
5962
60- rgi_regionsO1 = [1 ] # RGI Order 1 regions
63+ rgi_regionsO1 = [14 ] # RGI Order 1 regions
6164binsize = 10 # elevation bin (must be an integer greater than 1)
65+ dem_poorquality_switch = True # Switch to filter poor quality DEMs if another DEM is available
6266dem_poorquality_threshold = 200 # threshold used to identify problems with Farinotti DEM
6367option_plot_DEMsraw = True # Option to plot the raw DEMs
6468option_plot_DEMs = False # Option to plot the masked DEMs
6569debug = False
6670
67- # ===== LOAD GLACIERS ===== =
71+ # ======
6872glacno_wpoor_DEM = []
6973for region in rgi_regionsO1 :
7074
7175 thickness_fp = thickness_fp_prefix + 'RGI60-' + str (region ).zfill (2 ) + '/'
76+ dem_farinotti_fp = dem_farinotti_fp_prefix + 'surface_DEMs_RGI60-' + str (region ).zfill (2 ) + '/'
7277
7378 glacno_list = []
7479 for i in os .listdir (thickness_fp ):
7580 if i .endswith ('_thickness.tif' ):
7681 glacno_list .append (i .split ('-' )[1 ].split ('_' )[0 ])
7782 glacno_list = sorted (glacno_list )
7883
79- # print('\n\nDELETE ME - SWITCH TO COMPLETE LIST\n\n')
80- # glacno_list = ['01.03622 ']
84+ # print('\n\nDELETE ME - SWITCH TO COMPLETE LIST\n\n')
85+ # glacno_list = ['15.02228 ']
8186 # glacno_list = glacno_list[10000:10010]
8287
8388 # Load RGI glacier data
@@ -97,32 +102,39 @@ def plot3panel(dem_list, clim=None, titles=None, cmap='inferno', label=None, ove
97102 main_glac_width ['RGIId' ] = main_glac_rgi .RGIId .values
98103 main_glac_length ['RGIId' ] = main_glac_rgi .RGIId .values
99104 main_glac_slope ['RGIId' ] = main_glac_rgi .RGIId .values
100-
105+
101106 # ===== PROCESS EACH GLACIER ======
102107 for nglac , glacno in enumerate (glacno_list ):
103108 # print(nglac, glacno)
104109 thickness_fn = thickness_fp + 'RGI60-' + glacno + '_thickness.tif'
105110 dem_farinotti_fn = dem_farinotti_fp + 'surface_DEM_RGI60-' + glacno + '.tif'
106-
111+
107112 # Reproject, resample, warp rasters to common extent, grid size, etc.
108113 # note: use thickness for the reference to avoid unrealistic extrapolations, e.g., negative thicknesses
109114 # also using equal area increases areas significantly compared to RGI
110- raster_fn_list = [dem_ref_fn , dem_farinotti_fn , thickness_fn ]
115+ raster_fn_list = [dem_farinotti_fn , thickness_fn ]
116+ if dem_ref_fn is not None :
117+ raster_fn_list .append (dem_ref_fn )
118+
119+ print (raster_fn_list )
120+
111121 ds_list = warplib .memwarp_multi_fn (raster_fn_list , extent = 'intersection' , res = 'min' , t_srs = thickness_fn )
112- # print('\n\nSWITCH BACK TO THICKNESS_FN AFTER OTHERS CORRECTED!\n\n')
113- # ds_list = warplib.memwarp_multi_fn(raster_fn_list, extent='intersection', res='min', t_srs=dem_ref_fn)
114122
115123 # masked arrays using ice thickness estimates
116- dem_ref_raw , dem_far_raw , thickness = [iolib .ds_getma (i ) for i in ds_list ]
117- dem_ref = dem_ref_raw .copy ()
118- dem_ref .mask = thickness .mask
124+ if dem_ref_fn is not None :
125+ dem_ref_raw , dem_far_raw , thickness = [iolib .ds_getma (i ) for i in ds_list ]
126+ dem_ref = dem_ref_raw .copy ()
127+ dem_ref .mask = thickness .mask
128+ else :
129+ dem_far_raw , thickness = [iolib .ds_getma (i ) for i in ds_list ]
119130 dem_far = dem_far_raw .copy ()
120131 dem_far .mask = thickness .mask
121-
132+
122133 # DEM selection for binning computations
123134 # if exceeds threshold, then use the reference
124- if (abs (main_glac_rgi .loc [nglac ,'Zmin' ] - dem_far .min ()) > dem_poorquality_threshold or
125- abs (main_glac_rgi .loc [nglac ,'Zmax' ] - dem_far .max ()) > dem_poorquality_threshold ):
135+ if ((abs (main_glac_rgi .loc [nglac ,'Zmin' ] - dem_far .min ()) > dem_poorquality_threshold or
136+ abs (main_glac_rgi .loc [nglac ,'Zmax' ] - dem_far .max ()) > dem_poorquality_threshold )
137+ and dem_ref_fn is not None ):
126138 print (' Check Glacier ' + glacno + ': use Christian DEM instead of Farinotti' )
127139 print ('\n RGI Zmin/Zmax:' , main_glac_rgi .loc [nglac ,'Zmin' ], '/' , main_glac_rgi .loc [nglac ,'Zmax' ])
128140 print (' Farinotti Zmin/Zmax:' , np .round (dem_far .min (),0 ), '/' , np .round (dem_far .max (),0 ))
@@ -148,7 +160,7 @@ def plot3panel(dem_list, clim=None, titles=None, cmap='inferno', label=None, ove
148160 else :
149161 dem = dem_far
150162 dem_raw = dem_far_raw
151-
163+
152164 #Extract x and y pixel resolution (m) from geotransform
153165 gt = ds_list [0 ].GetGeoTransform ()
154166 px_res = (gt [1 ], - gt [5 ])
@@ -212,7 +224,7 @@ def plot3panel(dem_list, clim=None, titles=None, cmap='inferno', label=None, ove
212224
213225 elev_bin_edges = np .arange (elev_bin_min , elev_bin_max + binsize , binsize )
214226 elev_bins = (elev_bin_edges [0 :- 1 ] + binsize / 2 ).astype (int )
215-
227+
216228 # Hypsometry [km2]
217229 # must used .compressed() in histogram to exclude masked values
218230 hist , elev_bin_edges = np .histogram (dem .reshape (- 1 ).compressed (), bins = elev_bin_edges )
@@ -243,6 +255,13 @@ def plot3panel(dem_list, clim=None, titles=None, cmap='inferno', label=None, ove
243255
244256 # Width [km] - based on length (inherently slope) and bin area
245257 bin_width = bin_hyps / bin_length
258+
259+ # Remove negative values
260+ bin_hyps [bin_hyps < 0 ] = 0
261+ bin_thickness [bin_thickness < 0 ] = 0
262+ bin_width [bin_width < 0 ] = 0
263+ bin_length [bin_length < 0 ] = 0
264+ bin_slope [bin_slope < 0 ] = 0
246265
247266 # Record properties
248267 # Check if need to expand columns
@@ -267,12 +286,12 @@ def plot3panel(dem_list, clim=None, titles=None, cmap='inferno', label=None, ove
267286 main_glac_width = main_glac_width .fillna (0 )
268287 main_glac_length = main_glac_length .fillna (0 )
269288 main_glac_slope = main_glac_slope .fillna (0 )
270- # Remove negative values
271- main_glac_hyps [main_glac_hyps < 0 ] = 0
272- main_glac_thickness [main_glac_thickness < 0 ] = 0
273- main_glac_width [main_glac_width < 0 ] = 0
274- main_glac_length [main_glac_length < 0 ] = 0
275- main_glac_slope [main_glac_slope < 0 ] = 0
289+ # # Remove negative values
290+ # main_glac_hyps[main_glac_hyps < 0] = 0
291+ # main_glac_thickness[main_glac_thickness < 0] = 0
292+ # main_glac_width[main_glac_width < 0] = 0
293+ # main_glac_length[main_glac_length < 0] = 0
294+ # main_glac_slope[main_glac_slope < 0] = 0
276295 # Export results
277296 main_glac_hyps .to_csv (output_fp + 'area_km2_' + "{:02d}" .format (region ) + '_Farinotti2019_' +
278297 str (binsize ) + 'm.csv' , index = False )
@@ -284,12 +303,3 @@ def plot3panel(dem_list, clim=None, titles=None, cmap='inferno', label=None, ove
284303 str (binsize ) + 'm.csv' , index = False )
285304 main_glac_slope .to_csv (output_fp + 'slope_deg_' + "{:02d}" .format (region ) + '_Farinotti2019_' +
286305 str (binsize ) + 'm.csv' , index = False )
287-
288- ##%%
289- #import pandas as pd
290- #import pygem_input as input
291- #area = pd.read_csv(input.hyps_filepath + 'area_km2_01_Farinotti2019_10m_old.csv')
292- #thickness = pd.read_csv(input.hyps_filepath + 'thickness_m_01_Farinotti2019_10m_old.csv')
293- #length = pd.read_csv(input.hyps_filepath + 'length_km_01_Farinotti2019_10m_old.csv')
294- #slope = pd.read_csv(input.hyps_filepath + 'slope_deg_01_Farinotti2019_10m_old.csv')
295- #width = pd.read_csv(input.hyps_filepath + 'width_km_01_Farinotti2019_10m_old.csv')
0 commit comments