Example #1
0
        def test_attempt_new_glacier_growth_into_unavailable_higher_band(self):
            """ Simulates a (failing) attempt to grow the glacier into a new yet higher elevation band 
                (where there is no 0 pad available in the snow band parameter file) in cell '23456' 
            
                [   [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, 2300, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
            """
            surf_dem[dem_padding_thickness + 3][dem_padding_thickness + 8 + 3] = 2300

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            with pytest.raises(Exception) as message:
                update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)
            assert 'One or more RGM output DEM pixels lies above the bounds of the highest '\
                'defined elevation band (>= 2300.0m) as defined by the Snow Band Parameter File '\
                'for cell 23456. You may need to add or shift the zero padding to '\
                'accommodate this.' in str(message.value)

           # Remove error condition by reinstating glacier in offending pixel in the surface DEM for the next test
            surf_dem[dem_padding_thickness + 3][dem_padding_thickness + 8 + 3] = 2200
            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)
            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)
Example #2
0
        def test_glacier_receding_from_top_band_leaving_band_area_as_zero(self):
            """ Simulates the glacier receding out of the highest band of cell
                '23456' entirely, which consisted only of glacier HRUs, thus
                leaving that band's area fraction as zero
                            
                [   [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, 2150, 2170, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
            """
            surf_dem[dem_padding_thickness + 3][dem_padding_thickness + 8 + 3 : dem_padding_thickness + 8 + 5] = [2150, 2170]

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['23456'][4].num_hrus == 1 # shadow glacier HRU remains
            assert cells['23456'][4].area_frac == 0
            assert cells['23456'][4].area_frac_open_ground == 0
            assert cells['23456'][4].area_frac_glacier == 0

            assert cells['23456'][3].num_hrus == 2
            assert cells['23456'][3].area_frac == 0.25
            assert cells['23456'][3].area_frac_open_ground == 0.125
            assert cells['23456'][3].area_frac_glacier == 0.125

            # Total number of valid bands after
            assert len([band for band in cells['23456'] if band.num_hrus > 0]) == 5
Example #3
0
        def test_glacier_growth_into_new_higher_band(self):
            """ Simulates glacier growing in thickness from the highest existing valid band in cell '23456'
                into a new higher Band 4 (for which there is a 0 pad in the snow band file to accommodate it). 

                [   [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, 2200, 2210, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
            """
            surf_dem[dem_padding_thickness + 3][dem_padding_thickness + 8 + 3 : dem_padding_thickness + 8 + 5] = [2200, 2210]

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['23456'][4].num_hrus == 1
            assert cells['23456'][4].area_frac == 0.03125
            assert cells['23456'][4].area_frac_open_ground == 0
            assert cells['23456'][4].area_frac_glacier == 0.03125   

            # Total number of valid bands after
            assert len([band for band in cells['23456'] if band.num_hrus > 0]) == 5
Example #4
0
        def test_glacier_growth_into_new_lower_band(self):
            """ Simulates glacier growing back over the pixel of the new lowest band in cell '23456'
                (from the previous test), but at a lesser thickness such that the pixel is still within Band 0.

                [   [xxxx, xxxx, 1880, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
            """
            # Copy initial lowest band's area_frac, to use for re-initializing at end
            initial_area_frac = cells['23456'][0].area_frac
            surf_dem[dem_padding_thickness + 0][dem_padding_thickness + 8 + 2] = 1880

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['23456'][0].num_hrus == 1
            assert cells['23456'][0].area_frac == 0.015625
            assert cells['23456'][0].area_frac_open_ground == 0
            assert cells['23456'][0].area_frac_glacier == 0.015625           

            # Reinstate original elevation of changed pixel and remove created glacier HRU for next test 
            surf_dem[dem_padding_thickness + 0][dem_padding_thickness + 8 + 2] = 1850
            cells['23456'][0].delete_hru(22)
            cells['23456'][0].create_hru(19, initial_area_frac)
Example #5
0
        def test_glacier_growth_into_band_with_no_existing_glacier(self):
            """ Simulates Band 0 of cell '12345' acquiring a new glacier HRU. 

                [   [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, 2030],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, 2040],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
            """
            surf_dem[dem_padding_thickness + 0][dem_padding_thickness + 7] = 2030
            surf_dem[dem_padding_thickness + 1][dem_padding_thickness + 7] = 2040

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['12345'][0].num_hrus == 3
            assert cells['12345'][0].area_frac == 0.4375
            assert cells['12345'][0].area_frac_open_ground == 0.21875
            assert cells['12345'][0].area_frac_glacier == 0.03125
            assert cells['12345'][0].hrus[11].area_frac == 0.1875           
Example #6
0
        def test_glacier_growth_over_remaining_vegetation_in_band(self):
            """ Simulates Band 1 of cell '12345' losing its remaining vegetated 
                HRU to glacier growth.

                [   [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, 2115, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, 2110, 2125, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]

            """
            surf_dem[dem_padding_thickness + 5][dem_padding_thickness + 1] = 2115
            surf_dem[dem_padding_thickness + 6][dem_padding_thickness + 1 : dem_padding_thickness + 3] = [2110, 2125]

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['12345'][1].num_hrus == 1
            assert cells['12345'][1].area_frac == 0.3125
            assert cells['12345'][1].area_frac_open_ground == 0
            assert cells['12345'][1].area_frac_glacier == 0.3125
Example #7
0
        def test_glacier_growth_over_some_open_ground_and_vegetation_in_band(self):
            """ Simulates Band 1 of cell '12345' losing all its open ground and some 
                vegetated area to glacier growth.

                [   [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, 2120, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, 2130, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, 2145, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, 2150, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, 2140, xxxx],
                    [xxxx, xxxx, xxxx, 2140, 2160, 2160, 2150, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
            """
            surf_dem[dem_padding_thickness + 1][dem_padding_thickness + 6] = 2120
            surf_dem[dem_padding_thickness + 2][dem_padding_thickness + 6] = 2130
            surf_dem[dem_padding_thickness + 3][dem_padding_thickness + 6] = 2145
            surf_dem[dem_padding_thickness + 4][dem_padding_thickness + 6] = 2150
            surf_dem[dem_padding_thickness + 5][dem_padding_thickness + 6] = 2140
            surf_dem[dem_padding_thickness + 6][dem_padding_thickness + 3: dem_padding_thickness + 7] = [2140, 2160, 2160, 2150]

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['12345'][1].num_hrus == 2
            assert cells['12345'][1].area_frac == 0.3125
            assert cells['12345'][1].area_frac_open_ground == 0
            assert cells['12345'][1].area_frac_glacier == 0.265625
            assert cells['12345'][1].hrus[11].area_frac == 0.046875
Example #8
0
        def test_glacier_growth_over_remaining_open_ground_in_band(self):
            """ Simulates Band 2 of cell '12345' losing all its remaining open ground. 

                Changed elevations due to glacier growth (incremental from last test):

                [   [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, 2240, 2230, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]

            """
            surf_dem[dem_padding_thickness + 5][dem_padding_thickness + 4 : dem_padding_thickness + 6] = [2240, 2230]

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['12345'][2].num_hrus == 1
            assert cells['12345'][2].area_frac == 0.1875
            assert cells['12345'][2].area_frac_open_ground == 0
            assert cells['12345'][2].area_frac_glacier == 0.1875
Example #9
0
        def test_glacier_thickening_to_conceal_lowest_band_of_open_ground(self):
            """ Simulates the glacier growing over open ground areas lying in the new lowest band 
            of cell '23456' so thick that the pixels elevations in that area no longer belong to that band 
            (i.e. all HRUs in the band must be deleted).
                        
                [   [xxxx, xxxx, 1905, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
            """
            surf_dem[dem_padding_thickness + 0][dem_padding_thickness + 8 + 2] = 1905

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['23456'][0].num_hrus == 0 # we delete open ground HRUs
            assert cells['23456'][0].lower_bound == 1800
            assert cells['23456'][0].median_elev == 1800
            assert cells['23456'][0].area_frac == 0
            assert cells['23456'][0].area_frac_open_ground == 0
            assert cells['23456'][0].area_frac_glacier == 0           
 
            assert cells['23456'][1].num_hrus == 3
            assert cells['23456'][1].area_frac == 0.4375
            assert cells['23456'][1].area_frac_open_ground == 0.15625 
            assert cells['23456'][1].area_frac_glacier == 0.03125           
            assert cells['23456'][1].hrus[11].area_frac == 0.25   

            # Total number of valid bands after (should not include the lowest one now, because HRU was deleted)
            assert len([band for band in cells['23456'] if band.num_hrus > 0]) == 3

            # Reinstate lowest band with single glaciated pixel at 1880m for the next test
            surf_dem[dem_padding_thickness + 0][dem_padding_thickness + 8 + 2] = 1880
            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)
            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)
Example #10
0
        def test_existing_glacier_shrink_revealing_new_lower_band(self):
            """ Simulates glacier recession out of the lowest existing band of cell '23456', to reveal a yet
            lower elevation band (consisting of one pixel).
            
            Available bands as per snow band parameter file:
            '12345': [2000, 2100, 2200, 2300, 0] # allows for glacier growth at top
            '23456': [0, 1900, 2000, 2100, 0] # allows for glacier growth at top, and revelation of lower band at bottom

                [   [xxxx, xxxx, 1850, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
            """
            # Total number of valid bands before
            assert len([band for band in cells['23456'] if band.num_hrus > 0]) == 3

            surf_dem[dem_padding_thickness + 0][dem_padding_thickness + 8 + 2] = 1850

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['23456'][1].num_hrus == 3
            assert cells['23456'][1].area_frac == 0.421875
            assert cells['23456'][1].area_frac_open_ground == 0.15625 
            assert cells['23456'][1].area_frac_glacier == 0.015625           
            assert cells['23456'][1].hrus[11].area_frac == 0.25   
       
            # New lowest band
            assert cells['23456'][0].num_hrus == 1
            assert cells['23456'][0].area_frac == 0.015625
            assert cells['23456'][0].area_frac_open_ground == 0.015625
            assert cells['23456'][0].area_frac_glacier == 0           
      
            # Total number of valid bands after
            assert len([band for band in cells['23456'] if band.num_hrus > 0]) == 4
Example #11
0
        def test_glacier_growth_over_some_open_ground_in_band(self):
            """ Simulates Band 2 of cell '12345' losing some of its open ground area to glacier growth.
                
                Initial surf_dem for cell '12345':

                [   [2065, 2055, 2045, 2035, 2025, 2015, 2005, 2000],
                    [2075, 2100, 2120, 2140, 2130, 2120, 2100, 2005],
                    [2085, 2110, 2250, 2270, 2260, 2240, 2110, 2010],
                    [2090, 2120, 2260, 2377, 2310, 2250, 2125, 2015],
                    [2070, 2110, 2250, 2340, 2320, 2250, 2130, 2020],
                    [2090, 2105, 2200, 2210, 2220, 2220, 2120, 2015],
                    [2090, 2100, 2105, 2110, 2140, 2150, 2130, 2010],
                    [2080, 2075, 2065, 2055, 2045, 2035, 2020, 2000]    ]

                Changed elevations due to glacier growth:

                [   [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, 2230, 2240, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
                """
            surf_dem[dem_padding_thickness + 5][dem_padding_thickness + 2 : dem_padding_thickness + 4] = [2230, 2240]

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['12345'][2].num_hrus == 2
            assert cells['12345'][2].area_frac == 0.1875
            assert cells['12345'][2].area_frac_open_ground == 0.03125
            assert cells['12345'][2].area_frac_glacier == 0.15625

            # Total number of valid bands
            assert len([band for band in cells['12345'] if band.num_hrus > 0]) == 4
Example #12
0
        def test_glacier_thickening_to_conceal_lowest_band_of_glacier(self):
            """ Simulates the glacier thickening over areas lying in the lowest band of cell '23456' 
            so much that the pixels elevations in that area no longer belong to that band 
            (i.e. all HRUs in the band must be deleted, except glacier which is set to zero 
            area fraction).
                        
                [   [xxxx, xxxx, 1900, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
            """
            surf_dem[dem_padding_thickness + 0][dem_padding_thickness + 8 + 2] = 1900

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['23456'][0].num_hrus == 1 # we never delete glacier HRUs, vis-a-vis VIC's shadow glaciers
            assert cells['23456'][0].lower_bound == 1800
            assert cells['23456'][0].median_elev == 1800
            assert cells['23456'][0].area_frac == 0
            assert cells['23456'][0].area_frac_open_ground == 0
            assert cells['23456'][0].area_frac_glacier == 0           
 
            assert cells['23456'][1].num_hrus == 3
            assert cells['23456'][1].area_frac == 0.4375
            assert cells['23456'][1].area_frac_open_ground == 0.15625 
            assert cells['23456'][1].area_frac_glacier == 0.03125           
            assert cells['23456'][1].hrus[11].area_frac == 0.25   

            # Total number of valid bands after (should include the lowest one now, because of the glacier HRU)
            assert len([band for band in cells['23456'] if band.num_hrus > 0]) == 5
Example #13
0
        def test_glacier_receding_to_reveal_open_ground_in_band(self):
            """ Simulates Band 1 of cell '12345', which is completely covered in glacier, 
                ceding some area to open ground.

                [   [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, 2100, 2105, xxxx, xxxx, xxxx, xxxx, xxxx],
                    [xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx, xxxx]    ]
            """
            surf_dem[dem_padding_thickness + 6][dem_padding_thickness + 1 : dem_padding_thickness + 3] = [2100, 2105]

            glacier_mask = update_glacier_mask(surf_dem, bed_dem, num_rows_dem, num_cols_dem)

            update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,\
                      surf_dem, num_rows_dem, num_cols_dem, glacier_mask)

            assert cells['12345'][1].num_hrus == 2
            assert cells['12345'][1].area_frac == 0.3125
            assert cells['12345'][1].area_frac_open_ground == 0.03125
            assert cells['12345'][1].area_frac_glacier == 0.28125           
def main():
    print('\n\nVIC + RGM ... together at last!')

    # Parse command line parameters
    vic_global_file, rgm_params_file, surf_dem_in_file, bed_dem_file, \
        pixel_cell_map_file, init_glacier_mask_file, output_trace_files, \
        glacier_root_zone_parms, open_ground_root_zone_parms, band_size = parse_input_parms()

    # Get all initial VIC global parameters from the global parameter file
    with open(vic_global_file, 'r') as f:
        global_parms = Global(f)

    assert global_parms.state_format == 'NETCDF', \
        "{} only supports NetCDF input statefile input as opposed "\
        "to the specified {}. Please change change this in your "\
        "global file {}".format(__name__, global_parms.state_format,
                                vic_global_file)

    # Initial VIC output state filename prefix is determined by STATENAME in the global file
    state_filename_prefix = global_parms.statename

    # Apply custom glacier_id and open_ground_id Band attributes, if provided
    if global_parms.glacier_id is None:
        print(
            'No value for GLACIER_ID was provided in the VIC global file. Assuming default value of {}.'
            .format(Band.glacier_id))
    else:
        Band.glacier_id = global_parms.glacier_id
    # FIXME: reinstate the following commented-out code once OPEN_GROUND_ID is supported in VIC
    # Numeric code indicating an open ground vegetation tile (HRU)
    # if global_parms.open_ground_id is None:
    #     print('No value for OPEN_GROUND_ID was provided in the VIC global file. Assuming default value of {}.'.format(Band.open_ground_id))
    # else:
    #     Band.open_ground_id = global_parms.open_ground_id
    if band_size:
        Band.band_size = band_size

    # Load parameters from Snow Band Parameters File
    num_snow_bands, snb_file = global_parms.snow_band.split()
    num_snow_bands = int(num_snow_bands)
    elevation_cell_dict = load_snb_parms(snb_file, num_snow_bands)

    # Load vegetation parameters from initial Vegetation Parameter File
    hru_cell_dict = vegparams.load_veg_parms(global_parms.vegparam)

    # Apply custom HRU root_zone_parms attributes, if provided
    if glacier_root_zone_parms or open_ground_root_zone_parms:
        cells.apply_custom_root_zone_parms(hru_cell_dict,
                                           glacier_root_zone_parms,
                                           open_ground_root_zone_parms)
        Band.glacier_root_zone_parms = glacier_root_zone_parms
        Band.open_ground_root_zone_parms = open_ground_root_zone_parms

    # Merge all VIC cells info gathered from Snow Band and Vegetation Parameter files and custom parameters
    cells = merge_cell_input(hru_cell_dict, elevation_cell_dict)

    # TODO: Do a sanity check to make sure band area fractions in Snow Band Parameters file add up to sum of HRU
    # area fractions in Vegetation Parameter File for each cell?
    #assert (area_fracs == [sums of HRU area fracs for all bands])

    # The RGM will always output a DEM file of the same name (if running RGM for a single year at a time)
    rgm_surf_dem_out_file = temp_files_path + 's_out_00001.grd'

    # Open and read VIC-grid-to-RGM-pixel mapping file
    cellid_map, elevation_map, cell_areas, num_cols_dem, num_rows_dem = get_rgm_pixel_mapping(
        pixel_cell_map_file)

    # Get DEM xmin, xmax, ymin, ymax metadata of Bed DEM and check file header validity
    dem_xmin, dem_xmax, dem_ymin, dem_ymax, num_rows, num_cols = read_gsa_headers(
        bed_dem_file)
    # Verify number of columns & rows agree with what's stated in the pixel_to_cell_map_file
    assert (num_cols == num_cols_dem) and (
        num_rows == num_rows_dem), 'Mismatch of stated dimension(s) \
        between Bed DEM in {} (num rows: {}, num columns: {}) and the VIC-grid-to-RGM-pixel map in {} \
        (num rows: {}, num columns: {}). Exiting.\n'.format(
            bed_dem_file, num_rows, num_cols, pixel_cell_map_file,
            num_rows_dem, num_cols_dem)
    # Read in the provided Bed Digital Elevation Map (BDEM) file to 2D bed_dem array
    bed_dem = np.loadtxt(bed_dem_file, skiprows=5)

    # Check header validity of Surface DEM file
    _, _, _, _, num_rows, num_cols = read_gsa_headers(surf_dem_in_file)
    # Verify number of columns & rows agree with what's stated in the pixel_to_cell_map_file
    assert (num_cols == num_cols_dem) and (
        num_rows == num_rows_dem), 'Mismatch of stated dimension(s) \
        between Surface DEM in {} (num rows: {}, num columns: {}) and the VIC-grid-to-RGM-pixel map in {} \
        (num rows: {}, num columns: {}). Exiting.\n'.format(
            surf_dem_in_file, num_rows, num_cols, pixel_cell_map_file,
            num_rows_dem, num_cols_dem)
    # Read in the provided Surface Digital Elevation Map (SDEM) file to 2D surf_dem array
    surf_dem_initial = np.loadtxt(surf_dem_in_file, skiprows=5)

    # Check agreement between elevation map from VIC-grid-to-RGM-pixel file and the initial Surface DEM
    assert (np.equal(elevation_map, surf_dem_initial)), 'Values mismatch between provided initial Surface DEM \
        file (num rows:{}, num columns:{}) and VIC-grid-to-RGM-pixel file (num rows:{}, num columns:{}). Exiting.\n'\
        .format(num_rows, num_cols, num_rows_dem, num_cols_dem)

    # Check header validity of initial Glacier Mask file
    _, _, _, _, num_rows, num_cols = read_gsa_headers(init_glacier_mask_file)
    # Verify number of columns & rows agree with what's stated in the pixel_to_cell_map_file
    assert (num_cols == num_cols_dem) and (
        num_rows == num_rows_dem), 'Mismatch of stated dimension(s) \
        between Glacier Mask in {} (num rows: {}, num columns: {}) and the VIC-grid-to-RGM-pixel map in {} \
        (num rows: {}, num columns: {}). Exiting.\n'.format(
            init_glacier_mask_file, num_rows, num_cols, pixel_cell_map_file,
            num_rows_dem, num_cols_dem)
    # Read in the provided initial glacier mask file to 2D glacier_mask array
    glacier_mask = np.loadtxt(init_glacier_mask_file, skiprows=5)

    # Apply the initial glacier mask and modify the band and glacier area fractions accordingly
    update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands,
                      surf_dem_initial, num_rows_dem, num_cols_dem,
                      glacier_mask)

    temp_snb = temp_files_path + 'snb_temp_' + global_parms.startdate.isoformat(
    ) + '.txt'
    snbparams.save_snb_parms(cells, temp_snb, band_map)
    temp_vpf = temp_files_path + 'vpf_temp_' + global_parms.startdate.isoformat(
    ) + '.txt'
    vegparams.save_veg_parms(cells, temp_vpf)

    # Run the coupled VIC-RGM model for the time range specified in the VIC global parameters file
    time_iterator = run_ranges(global_parms.startdate, global_parms.enddate,
                               global_parms.glacier_accum_startdate)
    for start, end in time_iterator:
        print('\nRunning VIC from {} to {}'.format(start, end))

        # 1. Write / Update temporary Global Parameters File, temp_gpf
        temp_gpf = temp_files_path + 'gpf_temp_{}.txt'.format(
            start.isoformat())

        # set global parameters for this VIC run
        global_parms.vegparm = temp_vpf
        global_parms.snow_band = '{} {}'.format(num_snow_bands, temp_snb)
        global_parms.startdate = start
        global_parms.enddate = end
        global_parms.statedate = end

        global_parms.write(temp_gpf)
        print('invoking VIC with global parameter file {}'.format(temp_gpf))

        # 2. Run VIC for a year.  This will save VIC model state at the end of the year, along with a Glacier Mass Balance (GMB) polynomial for each cell
        subprocess.check_call([vic_full_path, "-g", temp_gpf],
                              shell=False,
                              stderr=subprocess.STDOUT)

        # 3. Open VIC NetCDF state file and get the most recent GMB polynomial for each grid cell being modeled
        state_file = state_filename_prefix + "_" + start.isoformat()  ## FIXME
        print('opening VIC state file {}'.format(state_file))
        state = h5py.File(state_file, 'r+')
        gmb_polys = get_mass_balance_polynomials(state, state_file, cell_ids)

        # 4. Translate mass balances using grid cell GMB polynomials and current veg_parm_file into a 2D RGM mass balance grid (MBG)
        mass_balance_grid = mass_balances_to_rgm_grid(gmb_polys,
                                                      pixel_to_cell_map,
                                                      num_rows_dem,
                                                      num_cols_dem, cell_ids)
        # write Mass Balance Grid to ASCII file to direct the RGM to use as input
        mbg_file = temp_files_path + 'mass_balance_grid_' + start.isoformat(
        ) + '.gsa'
        write_grid_to_gsa_file(mass_balance_grid, mbg_file, num_cols_dem,
                               num_rows_dem, dem_xmin, dem_xmax, dem_ymin,
                               dem_ymax)

        # 5. Run RGM for one year, passing MBG, BDEM, SDEM
        subprocess.check_call([
            rgm_full_path, "-p", rgm_params_file, "-b", bed_dem_file, "-d",
            surf_dem_in_file, "-m", mbg_file, "-o", temp_files_path, "-s", "0",
            "-e", "0"
        ],
                              shell=False,
                              stderr=subprocess.STDOUT)
        # remove temporary files if not saving for offline inspection
        if not output_trace_files:
            os.remove(mbg_file)
            os.remove(rgm_surf_dem_file)

        # 6. Read in new Surface DEM file from RGM output
        rgm_surf_dem_out = np.loadtxt(rgm_surf_dem_out_file, skiprows=5)
        temp_surf_dem_file = temp_files_path + 'rgm_surf_dem_out_' + start.isoformat(
        ) + '.gsa'
        os.rename(rgm_surf_dem_out_file, temp_surf_dem_file)
        # this will be fed back into RGM on next time step
        surf_dem_in_file = temp_surf_dem_file

        # 7. Update glacier mask
        glacier_mask = update_glacier_mask(rgm_surf_dem_out, bed_dem,
                                           num_rows_dem, num_cols_dem)
        if output_trace_files:
            glacier_mask_file = temp_files_path + 'glacier_mask_' + start.isoformat(
            ) + '.gsa'
            write_grid_to_gsa_file(glacier_mask, glacier_mask_file)

        # 8. Update areas of each elevation band in each VIC grid cell, and update snow band and vegetation parameters
        update_area_fracs(cells, cell_areas, cellid_map, num_snow_bands, \
            rgm_surf_dem_out, num_rows_dem, num_cols_dem, glacier_mask)
        temp_snb = temp_files_path + 'snb_temp_' + start.isoformat() + '.txt'
        snbparams.save_snb_parms(cells, temp_snb, band_map)
        temp_vpf = temp_files_path + 'vpf_temp_' + start.isoformat() + '.txt'
        vegparams.save_veg_parms(cells, temp_vpf)

        # 11 Update HRUs in VIC state file
        # don't forget to close the state file

        # Get ready for the next loop
        global_parms.init_state = "{}_{}.txt".format(global_parms.statename,
                                                     end.strftime("%Y%m%d"))
        global_parms.statedate = end