ssec / polar2grid

Tools for reading, remapping, and writing satellite instrument data.
http://www.ssec.wisc.edu/software/polar2grid/
GNU General Public License v3.0
71 stars 33 forks source link

P2G Version 3.0 resampling grid coverage error #696

Open kathys opened 2 weeks ago

kathys commented 2 weeks ago

Jay Cable reported that GINA was seeing P2G Version 3.0 failures on a "small fraction" of VIIRS passes when resampling to a locally defined grid. The implementation and log message:

./polar2grid_v_3_0/bin/polar2grid.sh -r viirs_sdr -w geotiff -p true_color -g alaska_300 --grid-configs ./grids/viirs_geotiff_grids.yml -f ./JPSS2.20240611.010250.dat.gz/ INFO : Sorting and reading input files... INFO : Loading product metadata from files... INFO : Loading swath geolocation into memory... INFO : Running day coverage filtering... INFO : Running night coverage filtering... INFO : Checking products for sufficient output grid coverage (grid: 'alaska_300')... WARNING : Resampling found -11830.07% of the output grid 'alaska_300' covered. Will skip producing this product: M03 WARNING : Resampling found -11830.07% of the output grid 'alaska_300' covered. Will skip producing this product: M04 WARNING : Resampling found -11830.07% of the output grid 'alaska_300' covered. Will skip producing this product: M05 WARNING : Resampling found -11830.07% of the output grid 'alaska_300' covered. Will skip producing this product: I01 WARNING : No products were found to overlap with 'alaska_300' grid. INFO : Computing products and saving data to writers... WARNING : No product files produced given available valid data and resampling settings. This can happen if the writer detects that no valid output will be written or the input data does not overlap with the target grid. INFO : SUCCESS

Jay staged the data and I downloaded it and was able to reproduce this issue. The same error occurred when using the latest P2G Version 3.1 version too. You can find the data here:

bumi:/data/users/kathys/test_data/jay_cable/images_v30

Jay noted that the command used to resample to this grid in P2G Version 2.3 did work for the non-crefl bands, by not for crefl (true color creation).

djhoese commented 1 week ago

Just jotting down some notes as I begin investigating this:

I'm able to get this to fail with just I01 (I'm sure any band would work) and the alaska_300 grid that Jay used. I looked at the lats and lons and the data doesn't cross the north pole, but the entire thing straddles the anti-meridian.

I also printed the individual polygon areas for resampling and got:

intersect_polygon.area()=-37.60554104342502 | source_polygon.area()=-56.296599419778815 | target_polygon.area()=0.3178814667042147

So something about the polygon for the source data is causing a negative area, but at the same time, the target polygon only has an area of 0.3179. That's very strange. More debugging to come...

djhoese commented 1 week ago

Ok so I did some more experimenting. This seems to primarily be tied to the lon/lats of the data although I can't figure out why beyond that it covers the antimeridian. It could be that it seems both the top and bottom of the swath cross the antimeridian while normally I feel like we have only one of those sides crossing. That's the only thing I can think of that would make this an occasional issue and not an almost-every time issue for GINA.

Interestingly, while the output grid crosses the anti-meridian that doesn't seem to be the problem. There are multiple issues/pull requests in pyresample trying to correct issues with boundaries/polygons crossing the antimeridian, fixing other issues, or just refactoring. These two are the big ones I think:

https://github.com/pytroll/pyresample/pull/546 https://github.com/pytroll/pyresample/pull/466

I did discover a rather simple workaround for now until those pyresample issues can be fixed/finished. @spruceboy this might be worth hacking into your system to see how it goes:

Subject: [PATCH] Workaround swath anti-meridian issues
---
Index: polar2grid/filters/_utils.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/polar2grid/filters/_utils.py b/polar2grid/filters/_utils.py
--- a/polar2grid/filters/_utils.py  (revision a074dffa51d25791e96809c4f75666974878931f)
+++ b/polar2grid/filters/_utils.py  (date 1718914397572)
@@ -53,6 +53,9 @@
         try:
             adp = area_def.boundary()
             adp.decimate(int(freq_fraction * area_def.shape[0]))
+            if adp.contour_poly.area() < 0:
+                # https://github.com/ssec/polar2grid/issues/696
+                raise ValueError("Failed to generate valid polygon for area. Polygon has a negative area.")
         except ValueError:
             if not isinstance(area_def, SwathDefinition):
                 logger.error("Unable to generate bounding geolocation polygon")

Basically if we see a negative area for a polygon that we're using for coverage information, fail, and load all the lon/lats to determine a more basic bounding box. I'm not convinced this will fix every error-case you were running into Jay, but it might fix some. :crossed_fingers: