Skip to content

Fix a bug in projecting water to cryosphere radargrid#253

Open
oberonia78 wants to merge 3 commits intoisce-framework:developfrom
oberonia78:cryo_water_iono
Open

Fix a bug in projecting water to cryosphere radargrid#253
oberonia78 wants to merge 3 commits intoisce-framework:developfrom
oberonia78:cryo_water_iono

Conversation

@oberonia78
Copy link
Copy Markdown
Contributor

This PR fixes three issues related to the use of water masks in ionospheric processing:

  1. When applying the water mask for ionosphere estimation on the main and side bands, the process failed because Frequency A was hard-coded. This PR updates the logic to dynamically select the appropriate projection based on whether the Frequency B radar grid is used.

  2. The processing also failed when using water masks in EPSG:3413 or EPSG:3031, because the code incorrectly assumed an EPSG:4326 projection. This PR fixes the issue by properly handling the input projection and performing the necessary coordinate transformation.

  3. insar.py deletes the intermediate files but some of them is required for water projection. This PR moves the steps deleting the intermediate files.

@hfattahi hfattahi added this to the R05.01.4 milestone Apr 12, 2026
try:
dst_srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
raster_srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
except Exception:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

raise error?

Comment on lines +335 to +340
else:
tx_to_bbox = osr.CoordinateTransformation(raster_srs, dst_srs)

# transform two neighboring source points to estimate target resolution
p00 = tx_to_bbox.TransformPoint(x0, y0)[:2]
p10 = tx_to_bbox.TransformPoint(x0 + dx, y0)[:2]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revise from here to line 350

Copy link
Copy Markdown
Contributor

@hfattahi hfattahi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @oberonia78 . Please double check how come project_map_to_radar works between 4326 and UTM and not between polar stereo and 4326! Few more comments below as we discussed in PR review.

raise RuntimeError("Could not determine raster EPSG from projection.")
raster_epsg = int(raster_epsg)

xmin, ymin, xmax, ymax = map(float, bbox)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a type?

Suggested change
xmin, ymin, xmax, ymax = map(float, bbox)
xmin, ymin, xmax, ymax = map(bbox)

Comment on lines +265 to +268
try:
raster_srs.AutoIdentifyEPSG()
except Exception:
pass
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this helping with?

Comment on lines +280 to +284
try:
dst_srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
raster_srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
except Exception:
pass
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the order of axis is important for us. If that is the case then you should not pass but raise an exception.

Comment on lines +298 to +299
xmin_src, xmax_src = min(xs), max(xs)
ymin_src, ymax_src = min(ys), max(ys)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work for crossing dateline? Please see the stage_dem.py where we do similar transformations and I think we were more careful crossing dateline.

Comment on lines +346 to +350
# fallback in case estimate becomes zero or numerically unstable
if not np.isfinite(est_dx) or est_dx <= 0:
est_dx = max((xmax - xmin) / 1000.0, 1e-6)
if not np.isfinite(est_dy) or est_dy <= 0:
est_dy = max((ymax - ymin) / 1000.0, 1e-6)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not make sense. What is going here?

Comment on lines +329 to +344
# Use source pixel size magnitude to define output resolution in target CRS.
# This keeps behavior simple and avoids very strange defaults from Warp.
# For your case (bbox_epsg=4326), output grid becomes lon/lat.
if bbox_epsg == raster_epsg:
out_xres = abs(dx)
out_yres = abs(dy)
else:
tx_to_bbox = osr.CoordinateTransformation(raster_srs, dst_srs)

# transform two neighboring source points to estimate target resolution
p00 = tx_to_bbox.TransformPoint(x0, y0)[:2]
p10 = tx_to_bbox.TransformPoint(x0 + dx, y0)[:2]
p01 = tx_to_bbox.TransformPoint(x0, y0 + dy)[:2]

est_dx = abs(p10[0] - p00[0])
est_dy = abs(p01[1] - p00[1])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please double check and clean up.

Comment on lines +1226 to +1228
if "water" in mask_type and filter_bool:
water_mask_path = cfg[
"dynamic_ancillary_file_group"]["water_mask_file"]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the water_mask_path is None? please do the check

Comment on lines +375 to +385
warped_band = warped_ds.GetRasterBand(1)
if warped_band is None:
raise RuntimeError("Failed to access warped raster band.")

arr = warped_band.ReadAsArray()
if arr is None:
raise RuntimeError("Failed to read warped raster window.")

warped_gt = warped_ds.GetGeoTransform()
if warped_gt is None:
raise RuntimeError("Warped raster geotransform is missing.")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to raising the RuntimeError, please use journal to log those error.

@hfattahi hfattahi removed this from the R05.01.4 milestone Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants