|
114 | 114 | from ._uniform_grid import UniformGrid |
115 | 115 | from .typing_helpers import SingleCellCoaddApCorrMap |
116 | 116 |
|
117 | | -FILE_FORMAT_VERSION = "0.6" |
| 117 | +FILE_FORMAT_VERSION = "0.7" |
118 | 118 | """Version number for the file format as persisted, presented as a string of |
119 | 119 | the form M.m, where M is the major version, m is the minor version. |
120 | 120 | """ |
121 | 121 |
|
| 122 | +MAX_POLYGON_VERTICES = 6 |
| 123 | +"""Maximum number of vertices the overlap polygon region between a per-detector |
| 124 | +warp and the patch bounding box can have.""" |
| 125 | +# 3 vertices from the detector, 3 vertices from the patch bounding box. |
| 126 | + |
122 | 127 | logger = logging.getLogger(__name__) |
123 | 128 |
|
124 | 129 |
|
@@ -328,8 +333,12 @@ def readAsMultipleCellCoadd(self) -> MultipleCellCoadd: |
328 | 333 | day_obs=visit_dict[visit].day_obs, |
329 | 334 | physical_filter=visit_dict[visit].physical_filter, |
330 | 335 | ) |
| 336 | + if written_version >= version.parse("0.7"): |
| 337 | + num_vertices = row["num_vertices"] |
| 338 | + else: |
| 339 | + num_vertices = None # li[:None] returns the entire list. |
331 | 340 | visit_polygons[obs_id] = afwGeom.Polygon( |
332 | | - [Point2D(vertex) for vertex in row["polygon_vertices"]] |
| 341 | + [Point2D(vertex) for vertex in row["polygon_vertices"][:num_vertices]] |
333 | 342 | ) |
334 | 343 |
|
335 | 344 | for link_row in link_table: |
@@ -669,20 +678,40 @@ def writeMultipleCellCoaddAsFits( |
669 | 678 | format="I", |
670 | 679 | array=[obs_id.detector for obs_id in multiple_cell_coadd.common.visit_polygons], |
671 | 680 | ) |
| 681 | + number_of_vertices = [] |
672 | 682 | polygon_vertices_array = [] |
673 | | - for poly in multiple_cell_coadd.common.visit_polygons.values(): |
| 683 | + for obs_id, poly in multiple_cell_coadd.common.visit_polygons.items(): |
| 684 | + if num_vertices := len(poly.getVertices()) > MAX_POLYGON_VERTICES: |
| 685 | + logger.warning( |
| 686 | + "Visit %d, detector %d has a polygon with %d vertices. " |
| 687 | + "This geometry should be impossible for two intersecting " |
| 688 | + "convex quadrilaterals. Only the first %d will be stored in " |
| 689 | + "the FITS file.", |
| 690 | + obs_id.visit, |
| 691 | + obs_id.detector, |
| 692 | + num_vertices, |
| 693 | + MAX_POLYGON_VERTICES, |
| 694 | + ) |
| 695 | + number_of_vertices.append(num_vertices) |
674 | 696 | vertices = poly.getVertices() + poly.getVertices() |
675 | | - vertices = vertices[:6] |
| 697 | + vertices = vertices[:MAX_POLYGON_VERTICES] |
676 | 698 | polygon_vertices_array.append(np.array(vertices)) |
677 | 699 | polygon_column = fits.Column( |
678 | 700 | name="polygon_vertices", |
679 | | - format="12E", |
680 | | - dim="(2,6)", |
| 701 | + format=f"{2 * MAX_POLYGON_VERTICES}E", |
| 702 | + dim=f"(2,{MAX_POLYGON_VERTICES})", |
681 | 703 | array=polygon_vertices_array, |
682 | 704 | ) |
| 705 | + number_of_vertices_column = fits.Column( |
| 706 | + name="num_vertices", |
| 707 | + format="I", |
| 708 | + array=number_of_vertices, |
| 709 | + ) |
683 | 710 | visit_summary_hdu = fits.BinTableHDU.from_columns( |
684 | | - [visit_column, detector_column, polygon_column], name="VISIT_SUMMARY" |
| 711 | + [visit_column, detector_column, number_of_vertices_column, polygon_column], |
| 712 | + name="VISIT_SUMMARY", |
685 | 713 | ) |
| 714 | + visit_summary_hdu.header["POLYVERT"] = MAX_POLYGON_VERTICES |
686 | 715 |
|
687 | 716 | visit_recarray = np.rec.fromrecords( |
688 | 717 | recList=sorted(set(visit_records), key=lambda x: x[0]), # Sort by visit. |
|
0 commit comments