gammasimtools 0.24.0__py3-none-any.whl → 0.25.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. {gammasimtools-0.24.0.dist-info → gammasimtools-0.25.0.dist-info}/METADATA +1 -1
  2. {gammasimtools-0.24.0.dist-info → gammasimtools-0.25.0.dist-info}/RECORD +58 -55
  3. {gammasimtools-0.24.0.dist-info → gammasimtools-0.25.0.dist-info}/entry_points.txt +1 -0
  4. simtools/_version.py +2 -2
  5. simtools/application_control.py +50 -0
  6. simtools/applications/derive_psf_parameters.py +5 -0
  7. simtools/applications/derive_pulse_shape_parameters.py +195 -0
  8. simtools/applications/plot_array_layout.py +63 -1
  9. simtools/applications/simulate_flasher.py +3 -2
  10. simtools/applications/simulate_pedestals.py +1 -1
  11. simtools/applications/simulate_prod.py +8 -23
  12. simtools/applications/simulate_prod_htcondor_generator.py +7 -0
  13. simtools/applications/submit_array_layouts.py +5 -3
  14. simtools/applications/validate_file_using_schema.py +49 -123
  15. simtools/configuration/commandline_parser.py +8 -6
  16. simtools/corsika/corsika_config.py +197 -87
  17. simtools/data_model/model_data_writer.py +14 -2
  18. simtools/data_model/schema.py +112 -5
  19. simtools/data_model/validate_data.py +82 -48
  20. simtools/db/db_model_upload.py +2 -1
  21. simtools/db/mongo_db.py +133 -42
  22. simtools/dependencies.py +5 -9
  23. simtools/io/eventio_handler.py +128 -0
  24. simtools/job_execution/htcondor_script_generator.py +0 -2
  25. simtools/layout/array_layout_utils.py +1 -1
  26. simtools/model/array_model.py +36 -5
  27. simtools/model/model_parameter.py +0 -1
  28. simtools/model/model_repository.py +18 -5
  29. simtools/ray_tracing/psf_analysis.py +11 -8
  30. simtools/ray_tracing/psf_parameter_optimisation.py +822 -679
  31. simtools/reporting/docs_read_parameters.py +69 -9
  32. simtools/runners/corsika_runner.py +12 -3
  33. simtools/runners/corsika_simtel_runner.py +6 -0
  34. simtools/runners/runner_services.py +17 -7
  35. simtools/runners/simtel_runner.py +12 -54
  36. simtools/schemas/model_parameters/flasher_pulse_exp_decay.schema.yml +2 -0
  37. simtools/schemas/model_parameters/flasher_pulse_shape.schema.yml +50 -0
  38. simtools/schemas/model_parameters/flasher_pulse_width.schema.yml +2 -0
  39. simtools/schemas/simulation_models_info.schema.yml +2 -0
  40. simtools/simtel/pulse_shapes.py +268 -0
  41. simtools/simtel/simtel_config_writer.py +82 -1
  42. simtools/simtel/simtel_io_event_writer.py +2 -2
  43. simtools/simtel/simulator_array.py +58 -12
  44. simtools/simtel/simulator_light_emission.py +45 -8
  45. simtools/simulator.py +361 -347
  46. simtools/testing/assertions.py +62 -6
  47. simtools/testing/configuration.py +1 -1
  48. simtools/testing/log_inspector.py +4 -1
  49. simtools/testing/sim_telarray_metadata.py +1 -1
  50. simtools/testing/validate_output.py +44 -9
  51. simtools/utils/names.py +2 -4
  52. simtools/version.py +37 -0
  53. simtools/visualization/legend_handlers.py +14 -4
  54. simtools/visualization/plot_array_layout.py +229 -33
  55. simtools/visualization/plot_mirrors.py +837 -0
  56. simtools/simtel/simtel_io_file_info.py +0 -62
  57. {gammasimtools-0.24.0.dist-info → gammasimtools-0.25.0.dist-info}/WHEEL +0 -0
  58. {gammasimtools-0.24.0.dist-info → gammasimtools-0.25.0.dist-info}/licenses/LICENSE +0 -0
  59. {gammasimtools-0.24.0.dist-info → gammasimtools-0.25.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,837 @@
1
+ #!/usr/bin/python3
2
+ """Functions for plotting mirror panel layout information."""
3
+
4
+ import logging
5
+ from pathlib import Path
6
+
7
+ import matplotlib.colors as mcolors
8
+ import matplotlib.patches as mpatches
9
+ import matplotlib.pyplot as plt
10
+ import numpy as np
11
+ from matplotlib.collections import PatchCollection
12
+
13
+ from simtools.io import io_handler
14
+ from simtools.model.mirrors import Mirrors
15
+ from simtools.model.telescope_model import TelescopeModel
16
+ from simtools.visualization import visualize
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+ PATCH_STYLE = {"alpha": 0.8, "edgecolor": "black", "facecolor": "dodgerblue"}
21
+ LABEL_STYLE = {"ha": "center", "va": "center", "fontsize": 10, "color": "white", "weight": "bold"}
22
+ STATS_BOX_STYLE = {"boxstyle": "round", "facecolor": "wheat", "alpha": 0.8}
23
+ OBSERVER_TEXT = "for an observer facing the mirrors"
24
+
25
+
26
+ def _rotate_coordinates_clockwise_90(x_pos, y_pos):
27
+ """
28
+ Rotate coordinates 90 degrees clockwise for observer-facing view.
29
+
30
+ Plots are rotated by 90 degrees clockwise to present the point of view of a
31
+ person standing on the camera platform.
32
+
33
+ Important: Any transformation applied to one mirror list must be applied consistently to
34
+ all (primary) mirror lists. Inconsistent transformations would result in incorrect mirror
35
+ configurations when running sim_telarray simulations.
36
+ """
37
+ return y_pos, -x_pos
38
+
39
+
40
+ def _detect_segmentation_type(data_file_path):
41
+ """
42
+ Detect the type of segmentation file (ring, shape, or standard).
43
+
44
+ Parameters
45
+ ----------
46
+ data_file_path : Path
47
+ Path to the segmentation data file
48
+
49
+ Returns
50
+ -------
51
+ str
52
+ One of "ring", "shape", or "standard"
53
+ """
54
+ with open(data_file_path, encoding="utf-8") as f:
55
+ for line in f:
56
+ line_lower = line.strip().lower()
57
+ if line_lower.startswith("#") or not line_lower:
58
+ continue
59
+ if line_lower.startswith("ring"):
60
+ return "ring"
61
+ if line_lower.startswith(("hex", "yhex")):
62
+ return "shape"
63
+ return "standard"
64
+
65
+
66
+ def plot(config, output_file, db_config=None):
67
+ """
68
+ Plot mirror panel layout based on configuration.
69
+
70
+ Parameters
71
+ ----------
72
+ config : dict
73
+ Configuration dictionary containing:
74
+ - parameter: str, parameter name (e.g., "mirror_list", "primary_mirror_segmentation")
75
+ - site: str, site name (e.g., "North", "South")
76
+ - telescope: str, telescope name (e.g., "LSTN-01")
77
+ - parameter_version: str, optional, parameter version
78
+ - model_version: str, optional, model version
79
+ - title: str, optional, plot title
80
+ output_file : str or Path
81
+ Path where to save the plot (without extension)
82
+ db_config : dict, optional
83
+ Database configuration dictionary
84
+
85
+ Returns
86
+ -------
87
+ None
88
+ The function saves the plot to the specified output file.
89
+ """
90
+ tel_model = TelescopeModel(
91
+ site=config["site"],
92
+ telescope_name=config["telescope"],
93
+ model_version=config.get("model_version"),
94
+ db_config=db_config,
95
+ ignore_software_version=True,
96
+ )
97
+
98
+ output_path = io_handler.IOHandler().get_output_directory()
99
+
100
+ parameter_name = config["parameter"]
101
+ parameter_value = tel_model.get_parameter_value(parameter_name)
102
+ tel_model.export_model_files(destination_path=output_path)
103
+
104
+ mirror_file = parameter_value
105
+ data_file_path = Path(output_path / mirror_file)
106
+
107
+ parameter_type = config["parameter"]
108
+
109
+ if parameter_type in ("primary_mirror_segmentation", "secondary_mirror_segmentation"):
110
+ segmentation_type = _detect_segmentation_type(data_file_path)
111
+
112
+ if segmentation_type == "ring":
113
+ fig = plot_mirror_ring_segmentation(
114
+ data_file_path=data_file_path,
115
+ telescope_model_name=config["telescope"],
116
+ parameter_type=parameter_type,
117
+ )
118
+ elif segmentation_type == "shape":
119
+ fig = plot_mirror_shape_segmentation(
120
+ data_file_path=data_file_path,
121
+ telescope_model_name=config["telescope"],
122
+ parameter_type=parameter_type,
123
+ )
124
+ else:
125
+ fig = plot_mirror_segmentation(
126
+ data_file_path=data_file_path,
127
+ telescope_model_name=config["telescope"],
128
+ parameter_type=parameter_type,
129
+ )
130
+ else:
131
+ mirrors = Mirrors(mirror_list_file=data_file_path)
132
+ fig = plot_mirror_layout(
133
+ mirrors=mirrors,
134
+ mirror_file_path=data_file_path,
135
+ telescope_model_name=config["telescope"],
136
+ )
137
+
138
+ visualize.save_figure(fig, output_file)
139
+ plt.close(fig)
140
+
141
+
142
+ def plot_mirror_layout(mirrors, mirror_file_path, telescope_model_name):
143
+ """
144
+ Plot the mirror panel layout from a Mirrors object.
145
+
146
+ Parameters
147
+ ----------
148
+ mirrors : Mirrors
149
+ Mirrors object containing mirror panel data including positions,
150
+ diameters, focal lengths, and shape types
151
+ mirror_file_path : Path or str
152
+ Path to the mirror list file
153
+ telescope_model_name : str
154
+ Name of the telescope model (e.g., "LSTN-01", "MSTN-01")
155
+
156
+ Returns
157
+ -------
158
+ matplotlib.figure.Figure
159
+ The generated figure object
160
+ """
161
+ logger.info(f"Plotting mirror layout for {telescope_model_name}")
162
+
163
+ fig, ax = plt.subplots(figsize=(10, 10))
164
+
165
+ x_pos = mirrors.mirror_table["mirror_x"].to("cm").value
166
+ y_pos = mirrors.mirror_table["mirror_y"].to("cm").value
167
+ x_pos, y_pos = _rotate_coordinates_clockwise_90(x_pos, y_pos)
168
+ diameter = mirrors.mirror_diameter.to("cm").value
169
+ shape_type = mirrors.shape_type
170
+ focal_lengths = mirrors.mirror_table["focal_length"].to("cm").value
171
+
172
+ mirror_ids = (
173
+ mirrors.mirror_table["mirror_panel_id"]
174
+ if "mirror_panel_id" in mirrors.mirror_table.colnames
175
+ else list(range(len(x_pos)))
176
+ )
177
+
178
+ # MST mirrors are numbered from 1 at the bottom to N at the top
179
+ if telescope_model_name and "MST" in telescope_model_name.upper():
180
+ n_mirrors = len(mirror_ids)
181
+ mirror_ids = [n_mirrors - mid for mid in mirror_ids]
182
+
183
+ patches, colors = _create_mirror_patches(x_pos, y_pos, diameter, shape_type, focal_lengths)
184
+
185
+ collection = PatchCollection(
186
+ patches,
187
+ cmap="viridis",
188
+ edgecolor="black",
189
+ linewidth=0.5,
190
+ )
191
+ collection.set_array(np.array(colors))
192
+ ax.add_collection(collection)
193
+
194
+ _add_mirror_labels(ax, x_pos, y_pos, mirror_ids, max_labels=20)
195
+
196
+ _configure_mirror_plot(ax, x_pos, y_pos)
197
+
198
+ cbar = plt.colorbar(collection, ax=ax, pad=0.02)
199
+ cbar.set_label("Focal length [cm]", fontsize=14)
200
+
201
+ _add_mirror_statistics(ax, mirrors, mirror_file_path, x_pos, y_pos, diameter)
202
+
203
+ return fig
204
+
205
+
206
+ def plot_mirror_segmentation(data_file_path, telescope_model_name, parameter_type):
207
+ """
208
+ Plot mirror segmentation layout from a segmentation file.
209
+
210
+ Parameters
211
+ ----------
212
+ data_file_path : Path or str
213
+ Path to the segmentation data file containing mirror segment positions,
214
+ diameters, and shape types in standard numeric format
215
+ telescope_model_name : str
216
+ Name of the telescope model (e.g., "LSTN-01", "MSTN-01")
217
+ parameter_type : str
218
+ Type of segmentation parameter (e.g., "primary_mirror_segmentation",
219
+ "secondary_mirror_segmentation")
220
+
221
+ Returns
222
+ -------
223
+ matplotlib.figure.Figure
224
+ The generated figure object
225
+ """
226
+ logger.info(f"Plotting {parameter_type} for {telescope_model_name}")
227
+
228
+ segmentation_data = _read_segmentation_file(data_file_path)
229
+
230
+ fig, ax = plt.subplots(figsize=(10, 10))
231
+
232
+ x_pos = segmentation_data["x"]
233
+ y_pos = segmentation_data["y"]
234
+ x_pos, y_pos = _rotate_coordinates_clockwise_90(x_pos, y_pos)
235
+ diameter = segmentation_data["diameter"]
236
+ shape_type = segmentation_data["shape_type"]
237
+ segment_ids = segmentation_data["segment_ids"]
238
+
239
+ patches, colors = _create_mirror_patches(x_pos, y_pos, diameter, shape_type, segment_ids)
240
+
241
+ collection = PatchCollection(
242
+ patches,
243
+ cmap="tab20",
244
+ edgecolor="black",
245
+ linewidth=0.8,
246
+ )
247
+ collection.set_array(np.array(colors))
248
+ ax.add_collection(collection)
249
+
250
+ _add_mirror_labels(ax, x_pos, y_pos, segment_ids, max_labels=30)
251
+
252
+ _configure_mirror_plot(ax, x_pos, y_pos)
253
+
254
+ cbar = plt.colorbar(collection, ax=ax, pad=0.02)
255
+ cbar.set_label("Segment ID", fontsize=14)
256
+
257
+ n_segments = len(set(segment_ids))
258
+ stats_text = (
259
+ f"Number of segments: {len(x_pos)}\n"
260
+ f"Number of segment groups: {n_segments}\n"
261
+ f"Segment diameter: {diameter:.1f} cm"
262
+ )
263
+
264
+ ax.text(
265
+ 0.02,
266
+ 0.98,
267
+ stats_text,
268
+ transform=ax.transAxes,
269
+ fontsize=11,
270
+ verticalalignment="top",
271
+ bbox=STATS_BOX_STYLE,
272
+ )
273
+
274
+ return fig
275
+
276
+
277
+ def _create_mirror_patches(x_pos, y_pos, diameter, shape_type, color_values):
278
+ """Create matplotlib patches for mirror panels or segments."""
279
+ patches = [
280
+ _create_single_mirror_patch(x, y, diameter, shape_type) for x, y in zip(x_pos, y_pos)
281
+ ]
282
+ return patches, list(color_values)
283
+
284
+
285
+ def _read_segmentation_file(data_file_path):
286
+ """Read mirror segmentation file and extract segment information."""
287
+ x_pos = []
288
+ y_pos = []
289
+ diameter = None
290
+ shape_type = None
291
+ segment_ids = []
292
+
293
+ with open(data_file_path, encoding="utf-8") as f:
294
+ for line in f:
295
+ line = line.strip()
296
+ if not line or line.startswith("#"):
297
+ continue
298
+
299
+ parts = line.split()
300
+ if len(parts) < 5:
301
+ continue
302
+
303
+ try:
304
+ x_pos.append(float(parts[0]))
305
+ y_pos.append(float(parts[1]))
306
+ except ValueError:
307
+ continue
308
+
309
+ diameter = _extract_diameter(parts, diameter)
310
+ shape_type = _extract_shape_type(parts, shape_type)
311
+ segment_ids.append(_extract_segment_id(parts, len(segment_ids)))
312
+
313
+ if len(x_pos) == 0:
314
+ logger.warning(f"No valid numeric data found in segmentation file: {data_file_path}")
315
+
316
+ return {
317
+ "x": np.array(x_pos),
318
+ "y": np.array(y_pos),
319
+ "diameter": diameter if diameter is not None else 150.0,
320
+ "shape_type": shape_type if shape_type is not None else 3,
321
+ "segment_ids": segment_ids,
322
+ }
323
+
324
+
325
+ def _extract_diameter(parts, current_diameter):
326
+ """Extract diameter from parts or return current value."""
327
+ return float(parts[2]) if current_diameter is None else current_diameter
328
+
329
+
330
+ def _extract_shape_type(parts, current_shape_type):
331
+ """Extract shape type from parts or return current value."""
332
+ return int(parts[4]) if current_shape_type is None else current_shape_type
333
+
334
+
335
+ def _extract_segment_id(parts, default_id):
336
+ """Extract segment ID from parts or return default."""
337
+ if len(parts) >= 8:
338
+ seg_id_str = parts[7].split("=")[-1] if "=" in parts[7] else parts[7]
339
+ return int("".join(filter(str.isdigit, seg_id_str)))
340
+ return default_id
341
+
342
+
343
+ def _create_single_mirror_patch(x, y, diameter, shape_type):
344
+ """Create a single matplotlib patch for a mirror panel (hexagonal only)."""
345
+ base_orientation = 0 if shape_type == 1 else np.pi / 2
346
+ # Rotate hexagon counter-clockwise to compensate for clockwise coordinate rotation
347
+ orientation = base_orientation - np.pi / 2
348
+
349
+ return mpatches.RegularPolygon(
350
+ (x, y),
351
+ numVertices=6,
352
+ radius=diameter / np.sqrt(3),
353
+ orientation=orientation,
354
+ )
355
+
356
+
357
+ def _add_mirror_labels(ax, x_pos, y_pos, mirror_ids, max_labels=20):
358
+ """Add mirror panel ID labels to the plot."""
359
+ mirror_data = sorted(zip(mirror_ids, x_pos, y_pos), key=lambda item: item[0])
360
+
361
+ for i, (mid, x, y) in enumerate(mirror_data):
362
+ if i < max_labels:
363
+ ax.text(
364
+ x,
365
+ y,
366
+ str(mid),
367
+ ha="center",
368
+ va="center",
369
+ fontsize=6,
370
+ color="white",
371
+ weight="bold",
372
+ )
373
+
374
+
375
+ def _configure_mirror_plot(ax, x_pos, y_pos):
376
+ """Add titles, labels, and limits."""
377
+ ax.set_aspect("equal")
378
+
379
+ if len(x_pos) == 0 or len(y_pos) == 0:
380
+ logger.warning("No valid mirror data found for plotting")
381
+ ax.set_xlim(-1000, 1000)
382
+ ax.set_ylim(-1000, 1000)
383
+ ax.text(0, 0, "No valid mirror data", ha="center", va="center", fontsize=14, color="red")
384
+ return
385
+
386
+ x_min, x_max = np.min(x_pos), np.max(x_pos)
387
+ y_min, y_max = np.min(y_pos), np.max(y_pos)
388
+
389
+ x_padding = (x_max - x_min) * 0.15
390
+ y_padding = (y_max - y_min) * 0.15
391
+
392
+ ax.set_xlim(x_min - x_padding, x_max + x_padding)
393
+ ax.set_ylim(y_min - y_padding, y_max + y_padding)
394
+
395
+ plt.xlabel("X position [cm]", fontsize=14)
396
+ plt.ylabel("Y position [cm]", fontsize=14)
397
+ plt.grid(True, alpha=0.3)
398
+ plt.tick_params(axis="both", which="major", labelsize=12)
399
+
400
+ ax.text(
401
+ 0.02,
402
+ 0.02,
403
+ OBSERVER_TEXT,
404
+ transform=ax.transAxes,
405
+ fontsize=10,
406
+ ha="left",
407
+ va="bottom",
408
+ style="italic",
409
+ color="black",
410
+ )
411
+
412
+
413
+ def _extract_float_after_keyword(line, keyword):
414
+ """Extract first float value after keyword in line."""
415
+ if keyword not in line:
416
+ return None
417
+ try:
418
+ part = line.split(keyword, 1)[1] if keyword == "=" else line.split(keyword)[-1]
419
+ return float(part.strip().split()[0])
420
+ except (ValueError, IndexError):
421
+ return None
422
+
423
+
424
+ def _read_mirror_file_metadata(mirror_file_path):
425
+ """Read metadata from mirror .dat file header (Rmax and total surface area)."""
426
+ metadata = {}
427
+ patterns = [
428
+ (("Total surface area:", "Total mirror surface area:"), ":", "total_surface_area"),
429
+ (("Rmax =", "Rmax="), "=", "rmax"),
430
+ (("mirrors are inside a radius of",), "of", "rmax"),
431
+ ]
432
+
433
+ try:
434
+ with open(mirror_file_path, encoding="utf-8") as f:
435
+ for line in f:
436
+ if not line.startswith("#"):
437
+ break
438
+ for triggers, keyword, key in patterns:
439
+ if key not in metadata and any(t in line for t in triggers):
440
+ if (val := _extract_float_after_keyword(line, keyword)) is not None:
441
+ metadata[key] = val
442
+ break
443
+ except OSError as e:
444
+ logger.warning(f"Could not read mirror file metadata: {e}")
445
+
446
+ return metadata
447
+
448
+
449
+ def _add_mirror_statistics(ax, mirrors, mirror_file_path, x_pos, y_pos, diameter):
450
+ """Add mirror statistics text to the plot."""
451
+ n_mirrors = mirrors.number_of_mirrors
452
+
453
+ metadata = _read_mirror_file_metadata(mirror_file_path)
454
+ max_radius = metadata.get("rmax")
455
+ total_area = metadata.get("total_surface_area")
456
+
457
+ # Calculate values if not available from file
458
+ if max_radius is None:
459
+ max_radius = np.sqrt(np.max(x_pos**2 + y_pos**2)) / 100.0
460
+
461
+ if total_area is None:
462
+ panel_area = 3 * np.sqrt(3) / 2 * (diameter / 200.0) ** 2
463
+ total_area = n_mirrors * panel_area
464
+
465
+ stats_text = (
466
+ f"Number of mirrors: {n_mirrors}\n"
467
+ f"Mirror diameter: {diameter:.1f} cm\n"
468
+ f"Max radius: {max_radius:.2f} m\n"
469
+ f"Total surface area: {total_area:.2f} $m^{2}$"
470
+ )
471
+
472
+ ax.text(
473
+ 0.02,
474
+ 0.98,
475
+ stats_text,
476
+ transform=ax.transAxes,
477
+ fontsize=11,
478
+ verticalalignment="top",
479
+ bbox=STATS_BOX_STYLE,
480
+ )
481
+
482
+
483
+ def _read_ring_segmentation_data(data_file_path):
484
+ """Read ring segmentation data from file."""
485
+ rings = []
486
+
487
+ with open(data_file_path, encoding="utf-8") as f:
488
+ for line in f:
489
+ if not line.startswith("#") and not line.startswith("%"):
490
+ if line.lower().startswith("ring"):
491
+ parts = line.split()
492
+ rings.append(
493
+ {
494
+ "nseg": int(parts[1].strip()),
495
+ "rmin": float(parts[2].strip()),
496
+ "rmax": float(parts[3].strip()),
497
+ "dphi": float(parts[4].strip()),
498
+ "phi0": float(parts[5].strip()),
499
+ }
500
+ )
501
+
502
+ return rings
503
+
504
+
505
+ def _plot_single_ring(ax, ring, cmap, color_index):
506
+ """Plot a single ring with its segments."""
507
+ rmin, rmax = ring["rmin"], ring["rmax"]
508
+ nseg, phi0 = ring["nseg"], ring["phi0"]
509
+ dphi = ring["dphi"]
510
+
511
+ # Angular gap between segments (in degrees) - represents the physical gaps
512
+ angular_gap = 0.3 # degrees
513
+
514
+ if nseg > 1:
515
+ dphi_rad = dphi * np.pi / 180
516
+ phi0_rad = phi0 * np.pi / 180
517
+ gap_rad = angular_gap * np.pi / 180
518
+
519
+ for i in range(nseg):
520
+ theta_i = i * dphi_rad + phi0_rad
521
+
522
+ # Fill segment with small gap on each side
523
+ n_theta = 100
524
+ theta_seg = np.linspace(theta_i + gap_rad, theta_i + dphi_rad - gap_rad, n_theta)
525
+
526
+ color_value = (color_index + i % 10) / 20.0 # Normalize to 0-1
527
+ color = cmap(color_value)
528
+ ax.fill_between(theta_seg, rmin, rmax, color=color, alpha=0.8)
529
+
530
+
531
+ def _add_ring_radius_label(ax, angle, radius, label_text):
532
+ """Add a radius label at the specified angle and radius."""
533
+ ax.text(
534
+ angle,
535
+ radius,
536
+ label_text,
537
+ ha="center",
538
+ va="center",
539
+ fontsize=9,
540
+ color="red",
541
+ weight="bold",
542
+ bbox={"boxstyle": "round,pad=0.3", "facecolor": "white", "alpha": 0.8},
543
+ )
544
+
545
+
546
+ def plot_mirror_ring_segmentation(data_file_path, telescope_model_name, parameter_type):
547
+ """
548
+ Plot mirror ring segmentation layout.
549
+
550
+ Parameters
551
+ ----------
552
+ data_file_path : Path or str
553
+ Path to the segmentation data file containing ring definitions with format:
554
+ ring <nseg> <rmin> <rmax> <dphi> <phi0>
555
+ telescope_model_name : str
556
+ Name of the telescope model (e.g., "LSTN-01", "MSTN-01")
557
+ parameter_type : str
558
+ Type of segmentation parameter (e.g., "primary_mirror_segmentation",
559
+ "secondary_mirror_segmentation")
560
+
561
+ Returns
562
+ -------
563
+ matplotlib.figure.Figure or None
564
+ The generated figure object, or None if no ring data found
565
+ """
566
+ logger.info(f"Plotting ring {parameter_type} for {telescope_model_name}")
567
+
568
+ rings = _read_ring_segmentation_data(data_file_path)
569
+
570
+ if not rings:
571
+ logger.warning(f"No ring data found in {data_file_path}")
572
+ return None
573
+
574
+ fig, ax = plt.subplots(subplot_kw={"projection": "polar"}, figsize=(10, 10))
575
+
576
+ cmap = mcolors.LinearSegmentedColormap.from_list("mirror_blue", ["#deebf7", "#3182bd"])
577
+
578
+ for i, ring in enumerate(rings):
579
+ _plot_single_ring(ax, ring, cmap, color_index=i * 10)
580
+
581
+ max_radius = max(ring["rmax"] for ring in rings)
582
+ label_padding = max_radius * 0.04
583
+ ax.set_ylim([0, max_radius + label_padding])
584
+ ax.set_yticklabels([])
585
+ ax.set_rgrids([])
586
+ ax.spines["polar"].set_visible(False)
587
+
588
+ label_angle = 30 * np.pi / 180
589
+
590
+ for ring in rings:
591
+ theta_full = np.linspace(0, 2 * np.pi, 360)
592
+ ax.plot(
593
+ theta_full,
594
+ np.repeat(ring["rmin"], len(theta_full)),
595
+ ":",
596
+ color="gray",
597
+ lw=0.8,
598
+ alpha=0.5,
599
+ )
600
+ ax.plot(
601
+ theta_full,
602
+ np.repeat(ring["rmax"], len(theta_full)),
603
+ ":",
604
+ color="gray",
605
+ lw=0.8,
606
+ alpha=0.5,
607
+ )
608
+
609
+ _add_ring_radius_label(ax, label_angle, ring["rmin"], f"{ring['rmin']:.3f}")
610
+ _add_ring_radius_label(ax, label_angle, ring["rmax"], f"{ring['rmax']:.3f}")
611
+
612
+ ax.text(
613
+ label_angle,
614
+ max_radius + label_padding * 2.5,
615
+ "[cm]",
616
+ ha="center",
617
+ va="center",
618
+ fontsize=10,
619
+ weight="bold",
620
+ color="red",
621
+ )
622
+
623
+ if len(rings) == 2:
624
+ stats_text = (
625
+ f"Inner ring segments: {rings[0]['nseg']}\nOuter ring segments: {rings[1]['nseg']}"
626
+ )
627
+ else:
628
+ stats_lines = [f"Ring {i + 1} segments: {ring['nseg']}" for i, ring in enumerate(rings)]
629
+ stats_text = "\n".join(stats_lines)
630
+
631
+ ax.text(
632
+ 0.02,
633
+ 0.98,
634
+ stats_text,
635
+ transform=ax.transAxes,
636
+ fontsize=11,
637
+ verticalalignment="top",
638
+ bbox=STATS_BOX_STYLE,
639
+ )
640
+
641
+ ax.text(
642
+ 0.02,
643
+ 0.02,
644
+ OBSERVER_TEXT,
645
+ transform=ax.transAxes,
646
+ fontsize=10,
647
+ ha="left",
648
+ va="bottom",
649
+ style="italic",
650
+ color="black",
651
+ )
652
+
653
+ plt.tight_layout()
654
+
655
+ return fig
656
+
657
+
658
+ def _parse_segment_id_line(line_stripped):
659
+ """Extract segment ID from a line if it contains segment ID information."""
660
+ try:
661
+ return int(line_stripped.split()[-1])
662
+ except (ValueError, IndexError):
663
+ return 0
664
+
665
+
666
+ def _is_skippable_line(line_stripped):
667
+ """Check if line should be skipped (empty or comment)."""
668
+ return not line_stripped or line_stripped.startswith(("#", "%"))
669
+
670
+
671
+ def _parse_shape_line(line_stripped, shape_segments, segment_ids, current_segment_id):
672
+ """Parse and append a single shape segmentation line."""
673
+ entries = line_stripped.split()
674
+
675
+ if any(line_stripped.lower().startswith(s) for s in ["hex", "yhex"]) and len(entries) >= 5:
676
+ shape_segments.append(
677
+ {
678
+ "shape": entries[0].lower(),
679
+ "x": float(entries[2]),
680
+ "y": float(entries[3]),
681
+ "diameter": float(entries[4]),
682
+ "rotation": float(entries[5]) if len(entries) > 5 else 0.0,
683
+ }
684
+ )
685
+ segment_ids.append(current_segment_id if current_segment_id > 0 else len(shape_segments))
686
+
687
+
688
+ def _read_shape_segmentation_file(data_file_path):
689
+ """
690
+ Read shape segmentation file.
691
+
692
+ Parameters
693
+ ----------
694
+ data_file_path : Path
695
+ Path to segmentation file
696
+
697
+ Returns
698
+ -------
699
+ tuple
700
+ (shape_segments, segment_ids)
701
+ """
702
+ shape_segments, segment_ids = [], []
703
+ current_segment_id = 0
704
+
705
+ with open(data_file_path, encoding="utf-8") as f:
706
+ for line in f:
707
+ line_stripped = line.strip()
708
+
709
+ if "segment id" in line_stripped.lower():
710
+ current_segment_id = _parse_segment_id_line(line_stripped)
711
+ elif not _is_skippable_line(line_stripped):
712
+ _parse_shape_line(line_stripped, shape_segments, segment_ids, current_segment_id)
713
+
714
+ return shape_segments, segment_ids
715
+
716
+
717
+ def _add_segment_label(ax, x, y, label):
718
+ """Add a label at the specified position."""
719
+ ax.text(x, y, str(label), **LABEL_STYLE)
720
+
721
+
722
+ def _create_shape_patches(ax, shape_segments, segment_ids):
723
+ """
724
+ Create patches for shape segments (hexagons).
725
+
726
+ Parameters
727
+ ----------
728
+ shape_segments : list
729
+ List of shape segment dictionaries
730
+ segment_ids : list
731
+ List of segment IDs
732
+ ax : matplotlib.axes.Axes
733
+ Axes to add text labels to
734
+
735
+ Returns
736
+ -------
737
+ tuple
738
+ (patches, maximum_radius)
739
+ """
740
+ patches, maximum_radius = [], 0
741
+
742
+ for i_seg, seg in enumerate(shape_segments):
743
+ x, y, diam, rot = seg["x"], seg["y"], seg["diameter"], seg["rotation"]
744
+ maximum_radius = max(maximum_radius, abs(x) + diam / 2, abs(y) + diam / 2)
745
+
746
+ patch = mpatches.RegularPolygon(
747
+ (x, y),
748
+ numVertices=6,
749
+ radius=diam / np.sqrt(3),
750
+ orientation=np.deg2rad(rot),
751
+ **PATCH_STYLE,
752
+ )
753
+
754
+ patches.append(patch)
755
+ label = segment_ids[i_seg] if i_seg < len(segment_ids) else i_seg + 1
756
+ _add_segment_label(ax, x, y, label)
757
+
758
+ return patches, maximum_radius
759
+
760
+
761
+ def plot_mirror_shape_segmentation(data_file_path, telescope_model_name, parameter_type):
762
+ """
763
+ Plot mirror shape segmentation layout.
764
+
765
+ Parameters
766
+ ----------
767
+ data_file_path : Path or str
768
+ Path to the segmentation data file containing explicit shape definitions
769
+ (hex, yhex) with positions, diameters, and rotations
770
+ telescope_model_name : str
771
+ Name of the telescope model (e.g., "LSTN-01", "MSTN-design")
772
+ parameter_type : str
773
+ Type of segmentation parameter (e.g., "primary_mirror_segmentation",
774
+ "secondary_mirror_segmentation")
775
+
776
+ Returns
777
+ -------
778
+ matplotlib.figure.Figure
779
+ The generated figure object
780
+ """
781
+ logger.info(f"Plotting shape {parameter_type} for {telescope_model_name}")
782
+
783
+ shape_segments, segment_ids = _read_shape_segmentation_file(data_file_path)
784
+
785
+ for seg in shape_segments:
786
+ seg["x"], seg["y"] = _rotate_coordinates_clockwise_90(seg["x"], seg["y"])
787
+ seg["rotation"] = seg["rotation"] - 90
788
+
789
+ fig, ax = plt.subplots(figsize=(10, 10))
790
+
791
+ # Create patches for shape segments
792
+ all_patches, maximum_radius = _create_shape_patches(ax, shape_segments, segment_ids)
793
+
794
+ collection = PatchCollection(all_patches, match_original=True)
795
+ ax.add_collection(collection)
796
+
797
+ ax.set_aspect("equal")
798
+ padding = maximum_radius * 0.1 if maximum_radius > 0 else 100
799
+ ax.set_xlim(-maximum_radius - padding, maximum_radius + padding)
800
+ ax.set_ylim(-maximum_radius - padding, maximum_radius + padding)
801
+
802
+ plt.xlabel("X position [cm]", fontsize=14)
803
+ plt.ylabel("Y position [cm]", fontsize=14)
804
+ plt.grid(True, alpha=0.3)
805
+ plt.tick_params(axis="both", which="major", labelsize=12)
806
+
807
+ ax.text(
808
+ 0.02,
809
+ 0.02,
810
+ OBSERVER_TEXT,
811
+ transform=ax.transAxes,
812
+ fontsize=10,
813
+ ha="left",
814
+ va="bottom",
815
+ style="italic",
816
+ color="black",
817
+ )
818
+
819
+ total_segments = len(shape_segments)
820
+ if segment_ids and total_segments > 0:
821
+ stats_text = f"Number of segments: {len(set(segment_ids))}"
822
+ elif total_segments > 0:
823
+ stats_text = f"Number of segments: {total_segments}"
824
+ else:
825
+ stats_text = "No segment data"
826
+
827
+ ax.text(
828
+ 0.02,
829
+ 0.98,
830
+ stats_text,
831
+ transform=ax.transAxes,
832
+ fontsize=11,
833
+ verticalalignment="top",
834
+ bbox=STATS_BOX_STYLE,
835
+ )
836
+
837
+ return fig