insarlab / MintPy

Miami InSAR time-series software in Python
https://mintpy.readthedocs.io
Other
595 stars 255 forks source link

MintPy subset error on alosStack products #1273

Closed ditafaith closed 1 day ago

ditafaith commented 1 day ago

Description of the problem

I ran ISCE2 and MintPy in same environment. However, I installed ISCE2 installed using conda while MintPy using pip. We try to subset ALOS2 data generated from ISCE2 using latitude and longitude information. A metadata file, data.rsc, has been successfully generated using prep_isce.py command.

However, it gives an error and asks for information such as 'EARTH_RADIUS' as described below.

Full error message

  File "/home/saka/miniconda3/envs/insar/lib/python3.11/site-packages/mintpy/load_data.py", line 828, in load_data
    iDict = read_subset_box(iDict)
            ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/saka/miniconda3/envs/insar/lib/python3.11/site-packages/mintpy/load_data.py", line 200, in read_subset_box
    pix_box = coord.bbox_geo2radar(geo_box)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/saka/miniconda3/envs/insar/lib/python3.11/site-packages/mintpy/objects/coord.py", line 501, in bbox_geo2radar
    y, x, y_res, x_res = self.geo2radar(lat, lon, print_msg=print_msg)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/saka/miniconda3/envs/insar/lib/python3.11/site-packages/mintpy/objects/coord.py", line 343, in geo2radar
    az_step = ut0.azimuth_ground_resolution(self.src_metadata)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/saka/miniconda3/envs/insar/lib/python3.11/site-packages/mintpy/utils/utils0.py", line 232, in azimuth_ground_resolution
    Re = float(atr['EARTH_RADIUS'])
               ~~~^^^^^^^^^^^^^^^^
KeyError: 'EARTH_RADIUS'

System information

codeautopilot[bot] commented 1 day ago

Potential Solution

The bug is caused by the absence of the 'EARTH_RADIUS' key in the metadata dictionary used by MintPy. This key is crucial for calculating azimuth ground resolution. The solution involves ensuring that the 'EARTH_RADIUS' key is present in the metadata file generated by the prep_isce.py script. If it is missing, a default value should be set. Additionally, the azimuth_ground_resolution function in utils0.py should be modified to handle missing keys more gracefully by using a default value.

What is Causing This Bug?

The bug is caused by the missing 'EARTH_RADIUS' key in the metadata dictionary. The prep_isce.py script, which prepares metadata files, does not explicitly include or check for this key. Consequently, when the azimuth_ground_resolution function in utils0.py attempts to access this key, it results in a KeyError. The fallback mechanism to use a default value is not functioning as expected, leading to the error.

Code

  1. Modify prep_isce.py to Include 'EARTH_RADIUS':

    Add a check to ensure 'EARTH_RADIUS' is included in the metadata. If missing, set a default value.

    # src/mintpy/cli/prep_isce.py
    
    def add_earth_radius_to_metadata(metadata):
       if 'EARTH_RADIUS' not in metadata:
           metadata['EARTH_RADIUS'] = '6371000'  # Default value in meters
       return metadata
    
    # Call this function before finalizing the metadata file
    metadata = add_earth_radius_to_metadata(metadata)
  2. Ensure Default Value Usage in utils0.py:

    Modify the azimuth_ground_resolution function to handle missing keys more gracefully.

    # src/mintpy/utils/utils0.py
    
    def azimuth_ground_resolution(atr):
       Re = float(atr.get('EARTH_RADIUS', 6371000))  # Use default if key is missing
       # Rest of the function remains unchanged

How to Replicate the Bug

  1. Set up a conda environment and install ISCE2 using conda and MintPy using pip.
  2. Run the prep_isce.py script to generate metadata for ALOS2 data.
  3. Attempt to subset the data using MintPy with latitude and longitude information.
  4. Observe the KeyError for 'EARTH_RADIUS' in the error message.

Task

User Input/Request

Ticket Title: MintPy subset error on alosStack products

Description of the Problem

The user encountered an error when trying to subset ALOS2 data using MintPy. The error message indicated a missing 'EARTH_RADIUS' key in the metadata. The user ran ISCE2 and MintPy in the same environment, with ISCE2 installed via conda and MintPy via pip. The metadata file was generated using the prep_isce.py command, but it lacked the necessary 'EARTH_RADIUS' key, leading to the error.

Click here to create a Pull Request with the proposed solution

Files used for this task:

Changes on src/mintpy/cli/prep_isce.py # Analysis Report ## Overview The file `src/mintpy/cli/prep_isce.py` is a command-line interface script for preparing ISCE-2 metadata files for use with MintPy. It parses command-line arguments to specify observation files, metadata files, baseline directories, and geometry directories. The script then calls the `prep_isce` function from the `mintpy.prep_isce` module to process the inputs. ## Potential Cause of the Bug The issue reported involves a missing 'EARTH_RADIUS' key in the metadata, which is required by MintPy for processing. The script `prep_isce.py` is responsible for preparing metadata files, but there is no explicit handling or inclusion of 'EARTH_RADIUS' in the metadata preparation process within this script. ## Recommendations To address the issue, the script should be modified to ensure that 'EARTH_RADIUS' is included in the generated metadata file if it is not already present. This could involve: 1. **Checking for 'EARTH_RADIUS':** Before finalizing the metadata file, check if 'EARTH_RADIUS' is present. 2. **Setting a Default Value:** If 'EARTH_RADIUS' is missing, calculate or set a default value for it. The average Earth radius is approximately 6371 kilometers, which can be used as a default. 3. **Updating Metadata:** Ensure that the metadata file is updated with the 'EARTH_RADIUS' value before it is used by other parts of the MintPy pipeline. ## Next Steps - Implement the above recommendations in the `prep_isce.py` script. - Test the script to ensure that the 'EARTH_RADIUS' is correctly added to the metadata and that the error is resolved. - Coordinate with the team to ensure that any dependencies or related scripts are also updated if necessary. This analysis focuses solely on the `prep_isce.py` file. Further investigation may be needed in other parts of the codebase to ensure comprehensive resolution of the issue.
Changes on src/mintpy/utils/utils0.py # Analysis Report ## File: `src/mintpy/utils/utils0.py` ### Relevant Function: `azimuth_ground_resolution` The function `azimuth_ground_resolution` is responsible for calculating the azimuth resolution on the ground in meters from the metadata attributes. The error reported by the user is a `KeyError` for the missing `'EARTH_RADIUS'` key in the metadata dictionary (`atr`). This function attempts to retrieve the `'EARTH_RADIUS'` value from the metadata dictionary and defaults to a global constant `EARTH_RADIUS` if the key is not present. ### Potential Cause of the Bug 1. **Missing Key in Metadata**: The error indicates that the `'EARTH_RADIUS'` key is missing from the metadata dictionary (`atr`). The function does attempt to use a default value (`EARTH_RADIUS`) if the key is not found, but the error suggests that this fallback mechanism is not functioning as expected. 2. **Improper Handling of Missing Keys**: The function should handle the absence of the `'EARTH_RADIUS'` key more gracefully. It should ensure that the default value is used when the key is missing, which seems to be the intended behavior but is not working correctly. ### Recommendations 1. **Ensure Default Value Usage**: Modify the function to ensure that the default value for `EARTH_RADIUS` is used if the key is missing from the metadata dictionary. This can be done by using the `dict.get()` method, which already seems to be in place, but needs verification to ensure it works as intended. 2. **Error Handling**: Add error handling to provide a more informative error message if the default value cannot be used for some reason. This will help in diagnosing similar issues in the future. 3. **Logging**: Consider adding logging to track when the default value is used, which can be useful for debugging and understanding the flow of data through the function. ### Conclusion The function `azimuth_ground_resolution` should be reviewed to ensure that it correctly defaults to using the global `EARTH_RADIUS` constant when the `'EARTH_RADIUS'` key is missing from the metadata. This should resolve the `KeyError` reported by the user.