setiastrosuitepro 1.6.4__py3-none-any.whl → 1.7.1.post2__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.

Potentially problematic release.


This version of setiastrosuitepro might be problematic. Click here for more details.

Files changed (132) hide show
  1. setiastro/images/TextureClarity.svg +56 -0
  2. setiastro/images/abeicon.svg +16 -0
  3. setiastro/images/acv_icon.png +0 -0
  4. setiastro/images/colorwheel.svg +97 -0
  5. setiastro/images/cosmic.svg +40 -0
  6. setiastro/images/cosmicsat.svg +24 -0
  7. setiastro/images/first_quarter.png +0 -0
  8. setiastro/images/full_moon.png +0 -0
  9. setiastro/images/graxpert.svg +19 -0
  10. setiastro/images/last_quarter.png +0 -0
  11. setiastro/images/linearfit.svg +32 -0
  12. setiastro/images/narrowbandnormalization.png +0 -0
  13. setiastro/images/new_moon.png +0 -0
  14. setiastro/images/pixelmath.svg +42 -0
  15. setiastro/images/planetarystacker.png +0 -0
  16. setiastro/images/waning_crescent_1.png +0 -0
  17. setiastro/images/waning_crescent_2.png +0 -0
  18. setiastro/images/waning_crescent_3.png +0 -0
  19. setiastro/images/waning_crescent_4.png +0 -0
  20. setiastro/images/waning_crescent_5.png +0 -0
  21. setiastro/images/waning_gibbous_1.png +0 -0
  22. setiastro/images/waning_gibbous_2.png +0 -0
  23. setiastro/images/waning_gibbous_3.png +0 -0
  24. setiastro/images/waning_gibbous_4.png +0 -0
  25. setiastro/images/waning_gibbous_5.png +0 -0
  26. setiastro/images/waxing_crescent_1.png +0 -0
  27. setiastro/images/waxing_crescent_2.png +0 -0
  28. setiastro/images/waxing_crescent_3.png +0 -0
  29. setiastro/images/waxing_crescent_4.png +0 -0
  30. setiastro/images/waxing_crescent_5.png +0 -0
  31. setiastro/images/waxing_gibbous_1.png +0 -0
  32. setiastro/images/waxing_gibbous_2.png +0 -0
  33. setiastro/images/waxing_gibbous_3.png +0 -0
  34. setiastro/images/waxing_gibbous_4.png +0 -0
  35. setiastro/images/waxing_gibbous_5.png +0 -0
  36. setiastro/qml/ResourceMonitor.qml +84 -82
  37. setiastro/saspro/__main__.py +20 -1
  38. setiastro/saspro/_generated/build_info.py +2 -2
  39. setiastro/saspro/abe.py +37 -4
  40. setiastro/saspro/aberration_ai.py +364 -33
  41. setiastro/saspro/aberration_ai_preset.py +29 -3
  42. setiastro/saspro/acv_exporter.py +379 -0
  43. setiastro/saspro/add_stars.py +33 -6
  44. setiastro/saspro/astrospike_python.py +45 -3
  45. setiastro/saspro/backgroundneutral.py +108 -40
  46. setiastro/saspro/blemish_blaster.py +4 -1
  47. setiastro/saspro/blink_comparator_pro.py +150 -55
  48. setiastro/saspro/clahe.py +4 -1
  49. setiastro/saspro/continuum_subtract.py +4 -1
  50. setiastro/saspro/convo.py +13 -7
  51. setiastro/saspro/cosmicclarity.py +129 -18
  52. setiastro/saspro/crop_dialog_pro.py +123 -7
  53. setiastro/saspro/curve_editor_pro.py +181 -64
  54. setiastro/saspro/curves_preset.py +249 -47
  55. setiastro/saspro/doc_manager.py +245 -15
  56. setiastro/saspro/exoplanet_detector.py +120 -28
  57. setiastro/saspro/frequency_separation.py +1158 -204
  58. setiastro/saspro/ghs_dialog_pro.py +81 -16
  59. setiastro/saspro/graxpert.py +1 -0
  60. setiastro/saspro/gui/main_window.py +706 -264
  61. setiastro/saspro/gui/mixins/dock_mixin.py +245 -24
  62. setiastro/saspro/gui/mixins/file_mixin.py +35 -16
  63. setiastro/saspro/gui/mixins/menu_mixin.py +35 -1
  64. setiastro/saspro/gui/mixins/theme_mixin.py +160 -14
  65. setiastro/saspro/gui/mixins/toolbar_mixin.py +499 -24
  66. setiastro/saspro/gui/mixins/update_mixin.py +138 -36
  67. setiastro/saspro/gui/mixins/view_mixin.py +42 -0
  68. setiastro/saspro/halobgon.py +4 -0
  69. setiastro/saspro/histogram.py +184 -8
  70. setiastro/saspro/image_combine.py +4 -0
  71. setiastro/saspro/image_peeker_pro.py +4 -0
  72. setiastro/saspro/imageops/narrowband_normalization.py +816 -0
  73. setiastro/saspro/imageops/serloader.py +1345 -0
  74. setiastro/saspro/imageops/starbasedwhitebalance.py +23 -52
  75. setiastro/saspro/imageops/stretch.py +582 -62
  76. setiastro/saspro/isophote.py +4 -0
  77. setiastro/saspro/layers.py +13 -9
  78. setiastro/saspro/layers_dock.py +183 -3
  79. setiastro/saspro/legacy/image_manager.py +154 -20
  80. setiastro/saspro/legacy/numba_utils.py +68 -48
  81. setiastro/saspro/legacy/xisf.py +240 -98
  82. setiastro/saspro/live_stacking.py +203 -82
  83. setiastro/saspro/luminancerecombine.py +228 -27
  84. setiastro/saspro/mask_creation.py +174 -15
  85. setiastro/saspro/mfdeconv.py +113 -35
  86. setiastro/saspro/mfdeconvcudnn.py +119 -70
  87. setiastro/saspro/mfdeconvsport.py +112 -35
  88. setiastro/saspro/morphology.py +4 -0
  89. setiastro/saspro/multiscale_decomp.py +81 -29
  90. setiastro/saspro/narrowband_normalization.py +1618 -0
  91. setiastro/saspro/numba_utils.py +72 -57
  92. setiastro/saspro/ops/commands.py +18 -18
  93. setiastro/saspro/ops/script_editor.py +10 -2
  94. setiastro/saspro/ops/scripts.py +122 -0
  95. setiastro/saspro/perfect_palette_picker.py +37 -3
  96. setiastro/saspro/plate_solver.py +84 -49
  97. setiastro/saspro/psf_viewer.py +119 -37
  98. setiastro/saspro/remove_green.py +1 -1
  99. setiastro/saspro/resources.py +73 -0
  100. setiastro/saspro/rgbalign.py +460 -12
  101. setiastro/saspro/selective_color.py +4 -1
  102. setiastro/saspro/ser_stack_config.py +82 -0
  103. setiastro/saspro/ser_stacker.py +2321 -0
  104. setiastro/saspro/ser_stacker_dialog.py +1838 -0
  105. setiastro/saspro/ser_tracking.py +206 -0
  106. setiastro/saspro/serviewer.py +1625 -0
  107. setiastro/saspro/sfcc.py +662 -216
  108. setiastro/saspro/shortcuts.py +171 -33
  109. setiastro/saspro/signature_insert.py +692 -33
  110. setiastro/saspro/stacking_suite.py +1347 -485
  111. setiastro/saspro/star_alignment.py +247 -123
  112. setiastro/saspro/star_spikes.py +4 -0
  113. setiastro/saspro/star_stretch.py +38 -3
  114. setiastro/saspro/stat_stretch.py +892 -129
  115. setiastro/saspro/subwindow.py +787 -363
  116. setiastro/saspro/supernovaasteroidhunter.py +1 -1
  117. setiastro/saspro/texture_clarity.py +593 -0
  118. setiastro/saspro/wavescale_hdr.py +4 -1
  119. setiastro/saspro/wavescalede.py +4 -1
  120. setiastro/saspro/whitebalance.py +84 -12
  121. setiastro/saspro/widgets/common_utilities.py +28 -21
  122. setiastro/saspro/widgets/resource_monitor.py +209 -111
  123. setiastro/saspro/widgets/spinboxes.py +10 -13
  124. setiastro/saspro/wimi.py +27 -656
  125. setiastro/saspro/wims.py +13 -3
  126. setiastro/saspro/xisf.py +101 -11
  127. {setiastrosuitepro-1.6.4.dist-info → setiastrosuitepro-1.7.1.post2.dist-info}/METADATA +4 -2
  128. {setiastrosuitepro-1.6.4.dist-info → setiastrosuitepro-1.7.1.post2.dist-info}/RECORD +132 -87
  129. {setiastrosuitepro-1.6.4.dist-info → setiastrosuitepro-1.7.1.post2.dist-info}/WHEEL +0 -0
  130. {setiastrosuitepro-1.6.4.dist-info → setiastrosuitepro-1.7.1.post2.dist-info}/entry_points.txt +0 -0
  131. {setiastrosuitepro-1.6.4.dist-info → setiastrosuitepro-1.7.1.post2.dist-info}/licenses/LICENSE +0 -0
  132. {setiastrosuitepro-1.6.4.dist-info → setiastrosuitepro-1.7.1.post2.dist-info}/licenses/license.txt +0 -0
setiastro/saspro/wimi.py CHANGED
@@ -6524,509 +6524,6 @@ class WIMIDialog(QDialog):
6524
6524
  print(f"[MinorBodies] objects inside cone: {kept}")
6525
6525
  return results
6526
6526
 
6527
- def _get_astap_exe(self) -> str:
6528
- s = self._settings()
6529
- # preferred key (what SettingsDialog writes)
6530
- p = s.value("paths/astap", "", type=str)
6531
- if p:
6532
- return p
6533
- # migrate legacy key if present
6534
- legacy = s.value("astap/exe_path", "", type=str)
6535
- if legacy:
6536
- s.setValue("paths/astap", legacy)
6537
- s.remove("astap/exe_path")
6538
- s.sync()
6539
- return legacy
6540
- return ""
6541
-
6542
- def _set_astap_exe(self, path: str) -> None:
6543
- s = self._settings()
6544
- s.setValue("paths/astap", path)
6545
- s.sync()
6546
-
6547
- def plate_solve_image(self):
6548
- """
6549
- Attempts to plate-solve the loaded image using ASTAP,
6550
- first trying a seeded solve (RA, SPD, scale, binning),
6551
- then falling back to a blind solve if anything is missing.
6552
- On success, updates self.header and self.wcs.
6553
- """
6554
- if not hasattr(self, 'image_path') or not self.image_path:
6555
- return
6556
-
6557
- # 1) Ensure ASTAP path
6558
- astap_exe = self._get_astap_exe()
6559
- if not astap_exe or not os.path.exists(astap_exe):
6560
- # last-resort browse if nothing in settings (keeps existing behavior)
6561
- filt = "Executables (*.exe);;All Files (*)" if sys.platform.startswith("win") else "Executables (*)"
6562
- new_path, _ = QFileDialog.getOpenFileName(self, "Select ASTAP Executable", "", filt)
6563
- if not new_path:
6564
- return
6565
- astap_exe = new_path
6566
- self._set_astap_exe(astap_exe)
6567
-
6568
- # 2) Write out the normalized FITS for ASTAP
6569
- normalized = self.stretch_image(self.image_data.astype(np.float32))
6570
- try:
6571
- tmp_path = self.save_temp_fits_image(normalized, self.image_path)
6572
- except Exception as e:
6573
- QMessageBox.critical(self, "Plate Solve", f"Error saving temp FITS: {e}")
6574
- return
6575
-
6576
- # 3) Seed arguments from header
6577
- raw_hdr = None
6578
- if isinstance(self.original_header, fits.Header):
6579
- raw_hdr = self.original_header
6580
- elif self.image_path.lower().endswith(('.fits','.fit')):
6581
- with fits.open(self.image_path, memmap=False) as hdul:
6582
- raw_hdr = hdul[0].header
6583
-
6584
- seed_args = []
6585
- if isinstance(raw_hdr, fits.Header):
6586
- # debug-dump
6587
- print("🔍 Raw header contents:")
6588
- for k,v in raw_hdr.items():
6589
- print(f" {k} = {v}")
6590
-
6591
- try:
6592
- # RA→hours, SPD
6593
- ra_deg = float(raw_hdr["CRVAL1"])
6594
- dec_deg= float(raw_hdr["CRVAL2"])
6595
- ra_h = ra_deg / 15.0
6596
- spd = dec_deg + 90.0
6597
-
6598
- # plate scale from CD matrix (°/px→″/px)
6599
- cd1 = float(raw_hdr.get("CD1_1", raw_hdr.get("CDELT1",0)))
6600
- cd2 = float(raw_hdr.get("CD2_1", raw_hdr.get("CDELT2",0)))
6601
- scale = np.hypot(cd1, cd2) * 3600.0
6602
-
6603
- # apply XBINNING/YBINNING
6604
- bx = int(raw_hdr.get("XBINNING", 1))
6605
- by = int(raw_hdr.get("YBINNING", bx))
6606
- if bx != by:
6607
- print(f"⚠️ Unequal binning: {bx}×{by}, averaging.")
6608
- binf = (bx+by)/2.0
6609
- scale *= binf
6610
-
6611
- seed_args = [
6612
- "-ra", f"{ra_h:.6f}",
6613
- "-spd", f"{spd:.6f}",
6614
- "-scale", f"{scale:.3f}"
6615
- ]
6616
- print(f"🔸 Seeding ASTAP: RA={ra_h:.6f}h, SPD={spd:.6f}°, scale={scale:.3f}\"/px (×{binf} bin)")
6617
- except Exception as e:
6618
- print("⚠️ Failed to build seed args, will do blind solve:", e)
6619
-
6620
- # 4) Build ASTAP args
6621
- if seed_args:
6622
- args = ["-f", tmp_path] + seed_args + ["-wcs", "-sip"]
6623
- else:
6624
- args = ["-f", tmp_path, "-r", "179", "-fov", "0", "-z", "0", "-wcs", "-sip"]
6625
-
6626
- print("▶️ Running ASTAP with arguments:", args)
6627
-
6628
- # create and launch the process
6629
- process = QProcess(self)
6630
- process.start(astap_exe, args)
6631
- if not process.waitForStarted(5000):
6632
- #QMessageBox.critical(self, "Plate Solve", "Failed to start ASTAP process.")
6633
- os.remove(tmp_path)
6634
-
6635
- return None
6636
- if not process.waitForFinished(300000):
6637
- #QMessageBox.critical(self, "Plate Solve", "ASTAP process timed out.")
6638
- os.remove(tmp_path)
6639
- return None
6640
-
6641
- exit_code = process.exitCode()
6642
- stdout = process.readAllStandardOutput().data().decode()
6643
- stderr = process.readAllStandardError().data().decode()
6644
- print("ASTAP exit code:", exit_code)
6645
- print("ASTAP STDOUT:\n", stdout)
6646
- print("ASTAP STDERR:\n", stderr)
6647
-
6648
- if exit_code != 0:
6649
- os.remove(tmp_path)
6650
- #QMessageBox.warning(self, "Plate Solve", "ASTAP failed. Falling back to blind solve.")
6651
-
6652
- return None
6653
-
6654
- # --- Retrieve the initial solved header from the temporary FITS file ---
6655
- try:
6656
- with fits.open(tmp_path, memmap=False) as hdul:
6657
- solved_header = dict(hdul[0].header)
6658
- for key in ["COMMENT", "HISTORY", "END"]:
6659
- solved_header.pop(key, None)
6660
- print("Initial solved header retrieved from temporary FITS file:")
6661
- for key, value in solved_header.items():
6662
- print(f"{key} = {value}")
6663
- except Exception as e:
6664
- QMessageBox.critical(self, "Plate Solve", f"Error reading solved header: {e}")
6665
- os.remove(tmp_path)
6666
-
6667
- return None
6668
-
6669
- # --- Check for a .wcs file and merge its header if present ---
6670
- wcs_path = os.path.splitext(tmp_path)[0] + ".wcs"
6671
- if os.path.exists(wcs_path):
6672
- try:
6673
- wcs_header = {}
6674
- with open(wcs_path, "r") as f:
6675
- text = f.read()
6676
- # Matches a FITS header keyword and its value (with an optional comment).
6677
- pattern = r"(\w+)\s*=\s*('?[^/']*'?)[\s/]"
6678
- for match in re.finditer(pattern, text):
6679
- key = match.group(1).strip().upper()
6680
- val = match.group(2).strip()
6681
- if val.startswith("'") and val.endswith("'"):
6682
- val = val[1:-1].strip()
6683
- wcs_header[key] = val
6684
- wcs_header.pop("END", None)
6685
- print("WCS header retrieved from .wcs file:")
6686
- for key, value in wcs_header.items():
6687
- print(f"{key} = {value}")
6688
- # Merge the parsed WCS header into the solved header.
6689
- solved_header.update(wcs_header)
6690
- except Exception as e:
6691
- print("Error reading .wcs file:", e)
6692
- else:
6693
- print("No .wcs file found; using header from temporary FITS.")
6694
-
6695
- # --- If loaded from a slot, merge the original file path from slot metadata ---
6696
- if getattr(self, "_from_slot", False) and hasattr(self, "_slot_meta"):
6697
- if "file_path" not in solved_header and "file_path" in self._slot_meta:
6698
- solved_header["file_path"] = self._slot_meta["file_path"]
6699
- print("Merged file_path from slot metadata into solved header.")
6700
-
6701
- # --- Add any missing required WCS keywords ---
6702
- required_keys = {
6703
- "CTYPE1": "RA---TAN",
6704
- "CTYPE2": "DEC--TAN",
6705
- "RADECSYS": "ICRS",
6706
- "WCSAXES": 2,
6707
- # CRVAL1, CRVAL2, CRPIX1, CRPIX2 are ideally provided by ASTAP.
6708
- }
6709
- for key, default in required_keys.items():
6710
- if key not in solved_header:
6711
- solved_header[key] = default
6712
- print(f"Added missing key {key} with default value {default}.")
6713
-
6714
- # --- Convert keys that are expected to be numeric from strings to numbers ---
6715
- expected_numeric_keys = {
6716
- "CRPIX1", "CRPIX2", "CRVAL1", "CRVAL2", "CROTA1", "CROTA2",
6717
- "CDELT1", "CDELT2", "CD1_1", "CD1_2", "CD2_1", "CD2_2", "WCSAXES"
6718
- }
6719
- for key in expected_numeric_keys:
6720
- if key in solved_header:
6721
- try:
6722
- # For keys that should be integers, you can use int(float(...)) if necessary.
6723
- solved_header[key] = float(solved_header[key])
6724
- except ValueError:
6725
- print(f"Warning: Could not convert {key} value '{solved_header[key]}' to float.")
6726
-
6727
- # --- Ensure integer keywords are stored as integers ---
6728
- for key in ["WCSAXES", "NAXIS", "NAXIS1", "NAXIS2", "NAXIS3"]:
6729
- if key in solved_header:
6730
- try:
6731
- solved_header[key] = int(float(solved_header[key]))
6732
- except ValueError:
6733
- print(f"Warning: Could not convert {key} value '{solved_header[key]}' to int.")
6734
-
6735
-
6736
- os.remove(tmp_path)
6737
- print("ASTAP plate solving successful. Final solved header:")
6738
- for key, value in solved_header.items():
6739
- print(f"{key} = {value}")
6740
-
6741
- # --------------------------------------------------------------------
6742
- # 1) Make sure A_ORDER/B_ORDER exist in pairs:
6743
- if "B_ORDER" in solved_header and "A_ORDER" not in solved_header:
6744
- solved_header["A_ORDER"] = solved_header["B_ORDER"]
6745
- if "A_ORDER" in solved_header and "B_ORDER" not in solved_header:
6746
- solved_header["B_ORDER"] = solved_header["A_ORDER"]
6747
-
6748
- # 2) Convert SIP‐order keywords to ints:
6749
- for key in ("A_ORDER","B_ORDER","AP_ORDER","BP_ORDER"):
6750
- if key in solved_header:
6751
- solved_header[key] = int(float(solved_header[key]))
6752
-
6753
- # 3) Convert every SIP coefficient to float:
6754
- for k in list(solved_header):
6755
- if re.match(r"^(?:A|B|AP|BP)_[0-9]+_[0-9]+$", k):
6756
- solved_header[k] = float(solved_header[k])
6757
-
6758
- # --------------------------------------------------------------------
6759
- # 4) Now rebuild your FITS header from the dict, preserving ordering:
6760
- new_hdr = fits.Header()
6761
- for key, val in solved_header.items():
6762
- # skip any stray non‑FITS metadata
6763
- if key == "file_path":
6764
- continue
6765
- new_hdr[key] = val
6766
-
6767
- # 5) Finally swap in the new header and re-init WCS (with SIP!)
6768
- self.header = new_hdr
6769
- try:
6770
- self.apply_wcs_header(self.header)
6771
- self.status_label.setText("Status: ASTAP solve succeeded.")
6772
- except Exception as e:
6773
- QMessageBox.critical(self, "Plate Solve", f"Error initializing WCS from solved header:\n{e}")
6774
- return
6775
-
6776
- return solved_header
6777
-
6778
-
6779
- def save_temp_fits_image(self, normalized_image, image_path: str):
6780
- """
6781
- Save the normalized_image as a FITS file to a temporary file.
6782
-
6783
- If the original image is FITS, this method retrieves the stored metadata
6784
- from the ImageManager and passes it directly to save_image().
6785
- If not, it generates a minimal header.
6786
-
6787
- Returns the path to the temporary FITS file.
6788
- """
6789
- # Always save as FITS.
6790
- selected_format = "fits"
6791
- bit_depth = "32-bit floating point"
6792
- is_mono = (normalized_image.ndim == 2 or
6793
- (normalized_image.ndim == 3 and normalized_image.shape[2] == 1))
6794
-
6795
- # If the original image is FITS, try to get its stored metadata.
6796
- original_header = None
6797
- if image_path.lower().endswith((".fits", ".fit")):
6798
- if self.parent() and hasattr(self.parent(), "image_manager"):
6799
- # Use the metadata from the current slot.
6800
- _, meta = self.parent().image_manager.get_current_image_and_metadata()
6801
- # Assume that meta already contains a proper 'original_header'
6802
- # (or the entire meta is the header).
6803
- original_header = meta.get("original_header", None)
6804
- # If nothing is stored, fall back to creating a minimal header.
6805
- if original_header is None:
6806
- print("No stored FITS header found; creating a minimal header.")
6807
- original_header = self.create_minimal_fits_header(normalized_image, is_mono)
6808
- else:
6809
- # For non-FITS images, generate a minimal header.
6810
- original_header = self.create_minimal_fits_header(normalized_image, is_mono)
6811
-
6812
- # Create a temporary filename.
6813
- tmp_file = tempfile.NamedTemporaryFile(suffix=".fits", delete=False)
6814
- tmp_path = tmp_file.name
6815
- tmp_file.close()
6816
-
6817
- try:
6818
- # Call your global save_image() exactly as in AstroEditingSuite.
6819
- save_image(
6820
- img_array=normalized_image,
6821
- filename=tmp_path,
6822
- original_format=selected_format,
6823
- bit_depth=bit_depth,
6824
- original_header=original_header,
6825
- is_mono=is_mono
6826
- # (image_meta and file_meta can be omitted if not needed)
6827
- )
6828
- print(f"Temporary normalized FITS saved to: {tmp_path}")
6829
- except Exception as e:
6830
- print("Error saving temporary FITS file using save_image():", e)
6831
- raise e
6832
- return tmp_path
6833
-
6834
- def create_minimal_fits_header(self, img_array, is_mono=False):
6835
- """
6836
- Creates a minimal FITS header when the original header is missing.
6837
- """
6838
-
6839
- header = Header()
6840
- header['SIMPLE'] = (True, 'Standard FITS file')
6841
- header['BITPIX'] = -32 # 32-bit floating-point data
6842
- header['NAXIS'] = 2 if is_mono else 3
6843
- header['NAXIS1'] = img_array.shape[2] if img_array.ndim == 3 and not is_mono else img_array.shape[1] # Image width
6844
- header['NAXIS2'] = img_array.shape[1] if img_array.ndim == 3 and not is_mono else img_array.shape[0] # Image height
6845
- if not is_mono:
6846
- header['NAXIS3'] = img_array.shape[0] if img_array.ndim == 3 else 1 # Number of color channels
6847
- header['BZERO'] = 0.0 # No offset
6848
- header['BSCALE'] = 1.0 # No scaling
6849
- header.add_comment("Minimal FITS header generated by AstroEditingSuite.")
6850
-
6851
- return header
6852
-
6853
- def stretch_image(self, image):
6854
- """
6855
- Perform an unlinked linear stretch on the image.
6856
- Each channel is stretched independently by subtracting its own minimum,
6857
- recording its own median, and applying the stretch formula.
6858
- Returns the stretched image in [0,1].
6859
- """
6860
- was_single_channel = False # Flag to check if image was single-channel
6861
-
6862
- # If the image is 2D or has one channel, convert to 3-channel
6863
- if image.ndim == 2 or (image.ndim == 3 and image.shape[2] == 1):
6864
- was_single_channel = True
6865
- image = np.stack([image] * 3, axis=-1)
6866
-
6867
- image = np.asarray(image, dtype=np.float32)
6868
- stretched_image = image.copy() # Need copy for in-place modification
6869
- self.stretch_original_mins = []
6870
- self.stretch_original_medians = []
6871
- target_median = 0.02
6872
-
6873
- for c in range(3):
6874
- channel_min = np.min(stretched_image[..., c])
6875
- self.stretch_original_mins.append(channel_min)
6876
- stretched_image[..., c] -= channel_min
6877
- channel_median = np.median(stretched_image[..., c])
6878
- self.stretch_original_medians.append(channel_median)
6879
- if channel_median != 0:
6880
- numerator = (channel_median - 1) * target_median * stretched_image[..., c]
6881
- denominator = (
6882
- channel_median * (target_median + stretched_image[..., c] - 1)
6883
- - target_median * stretched_image[..., c]
6884
- )
6885
- denominator = np.where(denominator == 0, 1e-6, denominator)
6886
- stretched_image[..., c] = numerator / denominator
6887
- else:
6888
- print(f"Channel {c} - Median is zero. Skipping stretch.")
6889
-
6890
- stretched_image = np.clip(stretched_image, 0.0, 1.0)
6891
- self.was_single_channel = was_single_channel
6892
- return stretched_image
6893
-
6894
- def unstretch_image(self, image):
6895
- """
6896
- Undo the unlinked linear stretch using stored parameters.
6897
- Returns the unstretched image.
6898
- """
6899
- original_mins = self.stretch_original_mins
6900
- original_medians = self.stretch_original_medians
6901
- was_single_channel = self.was_single_channel
6902
-
6903
- image = np.asarray(image, dtype=np.float32)
6904
-
6905
- if image.ndim == 2:
6906
- channel_median = np.median(image)
6907
- original_median = original_medians[0]
6908
- original_min = original_mins[0]
6909
- if channel_median != 0 and original_median != 0:
6910
- numerator = (channel_median - 1) * original_median * image
6911
- denominator = channel_median * (original_median + image - 1) - original_median * image
6912
- denominator = np.where(denominator == 0, 1e-6, denominator)
6913
- image = numerator / denominator
6914
- else:
6915
- print("Channel median or original median is zero. Skipping unstretch.")
6916
- image += original_min
6917
- image = np.clip(image, 0, 1)
6918
- return image
6919
-
6920
- for c in range(3):
6921
- channel_median = np.median(image[..., c])
6922
- original_median = original_medians[c]
6923
- original_min = original_mins[c]
6924
- if channel_median != 0 and original_median != 0:
6925
- numerator = (channel_median - 1) * original_median * image[..., c]
6926
- denominator = (
6927
- channel_median * (original_median + image[..., c] - 1)
6928
- - original_median * image[..., c]
6929
- )
6930
- denominator = np.where(denominator == 0, 1e-6, denominator)
6931
- image[..., c] = numerator / denominator
6932
- else:
6933
- print(f"Channel {c} - Median or original median is zero. Skipping unstretch.")
6934
- image[..., c] += original_min
6935
-
6936
- image = np.clip(image, 0, 1)
6937
- if was_single_channel and image.ndim == 3:
6938
- image = np.mean(image, axis=2, keepdims=True)
6939
- return image
6940
-
6941
- def retrieve_and_apply_wcs(self, job_id):
6942
- """Download the wcs.fits file from Astrometry.net, extract WCS header data, and apply it."""
6943
- try:
6944
- wcs_url = f"https://nova.astrometry.net/wcs_file/{job_id}"
6945
- wcs_filepath = "wcs.fits"
6946
- max_retries = 10
6947
- delay = 10 # seconds
6948
-
6949
- for attempt in range(max_retries):
6950
- response = requests.get(wcs_url, stream=True)
6951
- response.raise_for_status()
6952
-
6953
- with open(wcs_filepath, 'wb') as f:
6954
- for chunk in response.iter_content(chunk_size=8192):
6955
- f.write(chunk)
6956
-
6957
- try:
6958
- with fits.open(wcs_filepath, ignore_missing_simple=True, ignore_missing_end=True) as hdul:
6959
- wcs_header = hdul[0].header
6960
- print("WCS header successfully retrieved.")
6961
- # 🔁 use common path
6962
- self.apply_wcs_header(wcs_header)
6963
- return wcs_header
6964
- except Exception as e:
6965
- print(f"Attempt {attempt + 1}: Failed to process WCS file - possibly HTML instead of FITS. Retrying in {delay} seconds...")
6966
- print(f"Error: {e}")
6967
- time.sleep(delay)
6968
-
6969
- print("Failed to download a valid WCS FITS file after multiple attempts.")
6970
- return None
6971
-
6972
- except requests.exceptions.RequestException as e:
6973
- print(f"Error downloading WCS file: {e}")
6974
- except Exception as e:
6975
- print(f"Error processing WCS file: {e}")
6976
-
6977
- return None
6978
-
6979
-
6980
-
6981
- def apply_wcs_header(self, wcs_header):
6982
- """
6983
- Apply a solved WCS header. Sets self.wcs, self.pixscale (arcsec/pix),
6984
- self.orientation, and updates the orientation label.
6985
- """
6986
- # 1) Initialize the WCS object
6987
- self.wcs = WCS(wcs_header, naxis=2, relax=True)
6988
-
6989
- # 2) Derive pixel scale (arcsec/pixel)
6990
- if 'CDELT1' in wcs_header:
6991
- # CDELT1 is degrees/pixel
6992
- self.pixscale = abs(float(wcs_header['CDELT1'])) * 3600.0
6993
- elif 'CD1_1' in wcs_header and 'CD2_2' in wcs_header:
6994
- # approximate from CD matrix determinant
6995
- det = (wcs_header['CD1_1'] * wcs_header['CD2_2']
6996
- - wcs_header['CD1_2'] * wcs_header['CD2_1'])
6997
- pixscale_deg = math.sqrt(abs(det))
6998
- self.pixscale = pixscale_deg * 3600.0
6999
- else:
7000
- self.pixscale = None
7001
- print("Warning: could not derive pixscale from header.")
7002
-
7003
- # 3) Extract orientation (CROTA2 if present)
7004
- if 'CROTA2' in wcs_header:
7005
- self.orientation = float(wcs_header['CROTA2'])
7006
- else:
7007
- # fallback to your custom function
7008
- self.orientation = calculate_orientation(wcs_header)
7009
-
7010
- # 4) Update the GUI label
7011
- if self.orientation is not None:
7012
- self.orientation_label.setText(f"Orientation: {self.orientation:.2f}°")
7013
- else:
7014
- self.orientation_label.setText("Orientation: N/A")
7015
-
7016
- print(f" -> pixscale = {self.pixscale} arcsec/pixel")
7017
- print(f" -> orientation = {self.orientation}°")
7018
- try:
7019
- cr1 = wcs_header.get('CRVAL1')
7020
- cr2 = wcs_header.get('CRVAL2')
7021
- if cr1 is not None and cr2 is not None:
7022
- self.center_ra = float(cr1)
7023
- self.center_dec = float(cr2)
7024
- print(f" -> center RA/Dec = {self.center_ra:.6f}, {self.center_dec:.6f}")
7025
- except Exception:
7026
- print("Warning: could not extract CRVAL1/CRVAL2")
7027
-
7028
-
7029
-
7030
6527
  def calculate_pixel_from_ra_dec(self, ra, dec):
7031
6528
  """Convert RA/Dec to pixel coordinates using the WCS data."""
7032
6529
  if not hasattr(self, "wcs") or self.wcs is None:
@@ -7059,117 +6556,6 @@ class WIMIDialog(QDialog):
7059
6556
 
7060
6557
  return int(round(x_val)), int(round(y_val))
7061
6558
 
7062
- def login_to_astrometry(self, api_key):
7063
- try:
7064
- response = requests.post(
7065
- ASTROMETRY_API_URL + "login",
7066
- data={'request-json': json.dumps({"apikey": api_key})}
7067
- )
7068
- response_data = response.json()
7069
- if response_data.get("status") == "success":
7070
- return response_data["session"]
7071
- else:
7072
- raise ValueError("Login failed: " + response_data.get("error", "Unknown error"))
7073
- except Exception as e:
7074
- raise Exception("Login to Astrometry.net failed: " + str(e))
7075
-
7076
-
7077
- def upload_image_to_astrometry(self, image_path, session_key):
7078
- try:
7079
- # Check if the file is XISF format
7080
- file_extension = os.path.splitext(image_path)[-1].lower()
7081
- if file_extension == ".xisf":
7082
- # Load the XISF image
7083
- xisf = XISF(image_path)
7084
- im_data = xisf.read_image(0)
7085
-
7086
- # Convert to a temporary TIFF file for upload
7087
- temp_image_path = os.path.splitext(image_path)[0] + "_converted.tif"
7088
- if im_data.dtype == np.float32 or im_data.dtype == np.float64:
7089
- im_data = np.clip(im_data, 0, 1) * 65535
7090
- im_data = im_data.astype(np.uint16)
7091
-
7092
- # Save as TIFF
7093
- if im_data.shape[-1] == 1: # Grayscale
7094
- tiff.imwrite(temp_image_path, np.squeeze(im_data, axis=-1))
7095
- else: # RGB
7096
- tiff.imwrite(temp_image_path, im_data)
7097
-
7098
- print(f"Converted XISF file to TIFF at {temp_image_path} for upload.")
7099
- image_path = temp_image_path # Use the converted file for upload
7100
-
7101
- # Upload the image file
7102
- with open(image_path, 'rb') as image_file:
7103
- files = {'file': image_file}
7104
- data = {
7105
- 'request-json': json.dumps({
7106
- "publicly_visible": "y",
7107
- "allow_modifications": "d",
7108
- "session": session_key,
7109
- "allow_commercial_use": "d"
7110
- })
7111
- }
7112
- response = requests.post(ASTROMETRY_API_URL + "upload", files=files, data=data)
7113
- response_data = response.json()
7114
- if response_data.get("status") == "success":
7115
- return response_data["subid"]
7116
- else:
7117
- raise ValueError("Image upload failed: " + response_data.get("error", "Unknown error"))
7118
-
7119
- except Exception as e:
7120
- raise Exception("Image upload to Astrometry.net failed: " + str(e))
7121
-
7122
- finally:
7123
- # Clean up temporary file if created
7124
- if file_extension == ".xisf" and os.path.exists(temp_image_path):
7125
- os.remove(temp_image_path)
7126
- print(f"Temporary TIFF file {temp_image_path} deleted after upload.")
7127
-
7128
-
7129
-
7130
- def poll_submission_status(self, subid):
7131
- """Poll Astrometry.net to retrieve the job ID once the submission is processed."""
7132
- max_retries = 90 # Adjust as necessary
7133
- retries = 0
7134
- while retries < max_retries:
7135
- try:
7136
- response = requests.get(ASTROMETRY_API_URL + f"submissions/{subid}")
7137
- response_data = response.json()
7138
- jobs = response_data.get("jobs", [])
7139
- if jobs and jobs[0] is not None:
7140
- return jobs[0]
7141
- else:
7142
- print(f"Polling attempt {retries + 1}: Job not ready yet.")
7143
- except Exception as e:
7144
- print(f"Error while polling submission status: {e}")
7145
-
7146
- retries += 1
7147
- time.sleep(10) # Wait 10 seconds between retries
7148
-
7149
- return None
7150
-
7151
- def poll_calibration_data(self, job_id):
7152
- """Poll Astrometry.net to retrieve the calibration data once it's available."""
7153
- max_retries = 90 # Retry for up to 15 minutes (90 * 10 seconds)
7154
- retries = 0
7155
- while retries < max_retries:
7156
- try:
7157
- response = requests.get(ASTROMETRY_API_URL + f"jobs/{job_id}/calibration/")
7158
- response_data = response.json()
7159
- if response_data and 'ra' in response_data and 'dec' in response_data:
7160
- print("Calibration data retrieved:", response_data)
7161
- return response_data # Calibration data is complete
7162
- else:
7163
- print(f"Calibration data not available yet (Attempt {retries + 1})")
7164
- except Exception as e:
7165
- print(f"Error retrieving calibration data: {e}")
7166
-
7167
- retries += 1
7168
- time.sleep(10) # Wait 10 seconds between retries
7169
-
7170
- return None
7171
-
7172
-
7173
6559
  #If originally a fits file update the header
7174
6560
  def update_fits_with_wcs(self, filepath, calibration_data):
7175
6561
  if not filepath.lower().endswith(('.fits', '.fit')):
@@ -7391,23 +6777,43 @@ class WIMIDialog(QDialog):
7391
6777
  CIRCLE('ICRS', {ra_center}, {dec_center}, {radius_deg})
7392
6778
  ) = 1
7393
6779
  """
6780
+
6781
+ result = None
6782
+ last_err = None
6783
+
7394
6784
  for attempt in range(5):
7395
6785
  try:
7396
6786
  result = Simbad.query_tap(query)
6787
+ last_err = None
7397
6788
  break
7398
6789
  except Exception as e:
6790
+ last_err = e
7399
6791
  if attempt < 4:
7400
- time.sleep(1)
6792
+ time.sleep(1) # or QThread.msleep(1000) if you want less UI freeze
7401
6793
  else:
7402
- QMessageBox.critical(
6794
+ # After 5 attempts total, stop with a helpful message
6795
+ err_txt = str(last_err) if last_err is not None else "Unknown error"
6796
+ QMessageBox.warning(
7403
6797
  self,
7404
- "Query Failed",
7405
- f"Try again later:\n{e}"
7406
- )
6798
+ "SIMBAD network error",
6799
+ (
6800
+ "We couldn't reach SIMBAD due to a network or service error.\n\n"
6801
+ "We tried 5 times and then stopped.\n\n"
6802
+ "What to try:\n"
6803
+ " • Check your internet connection / VPN / firewall\n"
6804
+ " • Verify SIMBAD is reachable: https://simbad.cds.unistra.fr/simbad/\n"
6805
+ " • Then try again\n\n"
6806
+ f"Last error:\n{err_txt}"
6807
+ )
6808
+ )
6809
+ return
7407
6810
 
7408
6811
  if result is None or len(result) == 0:
7409
- QMessageBox.information(self, "No Results",
7410
- "No objects found in the specified area.")
6812
+ QMessageBox.information(
6813
+ self,
6814
+ "No Results",
6815
+ "No objects found in the specified area."
6816
+ )
7411
6817
  return
7412
6818
 
7413
6819
  # ——— 3a) list of all “star” & binary/variable OTYPE codes ———
@@ -7912,41 +7318,6 @@ class WIMIDialog(QDialog):
7912
7318
  self.status_label.setText(f"Status: Blind solve failed — {res}")
7913
7319
 
7914
7320
 
7915
- def extract_wcs_data(file_path):
7916
- try:
7917
- # Open the FITS file with minimal validation to ignore potential errors in non-essential parts
7918
- with fits.open(file_path, ignore_missing_simple=True, ignore_missing_end=True) as hdul:
7919
- header = hdul[0].header
7920
-
7921
- # Extract essential WCS parameters
7922
- wcs_params = {}
7923
- keys_to_extract = [
7924
- 'WCSAXES', 'CTYPE1', 'CTYPE2', 'EQUINOX', 'LONPOLE', 'LATPOLE',
7925
- 'CRVAL1', 'CRVAL2', 'CRPIX1', 'CRPIX2', 'CUNIT1', 'CUNIT2',
7926
- 'CD1_1', 'CD1_2', 'CD2_1', 'CD2_2', 'A_ORDER', 'A_0_0', 'A_0_1',
7927
- 'A_0_2', 'A_1_0', 'A_1_1', 'A_2_0', 'B_ORDER', 'B_0_0', 'B_0_1',
7928
- 'B_0_2', 'B_1_0', 'B_1_1', 'B_2_0', 'AP_ORDER', 'AP_0_0', 'AP_0_1',
7929
- 'AP_0_2', 'AP_1_0', 'AP_1_1', 'AP_2_0', 'BP_ORDER', 'BP_0_0',
7930
- 'BP_0_1', 'BP_0_2', 'BP_1_0', 'BP_1_1', 'BP_2_0'
7931
- ]
7932
- for key in keys_to_extract:
7933
- if key in header:
7934
- wcs_params[key] = header[key]
7935
-
7936
- # Manually create a minimal header with WCS information
7937
- wcs_header = fits.Header()
7938
- for key, value in wcs_params.items():
7939
- wcs_header[key] = value
7940
-
7941
- # Initialize WCS with this custom header
7942
- wcs = WCS(wcs_header)
7943
- print("WCS successfully initialized with minimal header.")
7944
- return wcs
7945
-
7946
- except Exception as e:
7947
- print(f"Error processing WCS file: {e}")
7948
- return None
7949
-
7950
7321
  # Function to calculate comoving radial distance (in Gly)
7951
7322
  def calculate_comoving_distance(z):
7952
7323
  z = abs(z)