-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAOPhot.py
3783 lines (3147 loc) · 177 KB
/
MAOPhot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
4# -*- coding: utf-8 -*-
"""
# # # ####### ######
## ## # # # # # # # # #### #####
# # # # # # # # # # # # # # #
# # # # # # # ###### ###### # # #
# # ####### # # # # # # # #
# # # # # # # # # # # #
# # # # ####### # # # #### #
# ### ###
## # # # #
# # # # # #
# # # # #
# ### # # ### # #
# ### # # ### # #
##### ### ### ### ###
Welcome to MAOPhot 1.0.0, a PSF Photometry tool using Astropy and Photutils.psf
1.0.0 Revision
- get_comparison_stars enhancement; when more than one match found
for a given comp star or vsx object, then newly matched star gets id
appended with ".1", or ".2", etc. (E.g., comp stars found: 120, 120.1,
120.2; vsx objects: Z Tau, Z Tau.1, Z Tau.2;) Only the comp stars listed
in Settings, and the Object Name listed in Settings is used. User can
changed those settings or change the matching radius. There is a
discussion about this in the documentation.
- Radio buttons "Display all objects" and "Display selected objects only"
allows user to display all detected objects or only ones listed in
the settings
- Check star in settings has priority over saved cvs file. User can change
check star and then re-run "Two Color Photometry".
- Added Non Iterative PSF Photometry option which uses class
PSFPhotometry
- check_star boolean in DataFrame is bool type throughout, not str
- Upgraded to Photutils 1.10.0 and Panda 2.1.4, etc.
- changed "Close" button in Settings window to "OK"
- report JD is DATE-OBS + EXPOSURE/2 (like AAVSO report)
- minor changes to Notes in report to reflect current AAVSO report
- added AMASS to AAVSO report
MAOPhot calculates stellar magnitudes from 2 dimensional digital photographs.
It produces an extended AAVSO (American Association of Variable Star Observers)
report which can be submitted to the AAVSO using the online tool WebObs
(http://www.aavso.org/webobs).
There are many photometry measuring programs available such as VPhot
(http://www.aavso.org/vphot) and AstroImageJ (University of Louisville). VPhot
uses the aperture photometry method.
MAOPhot uses the PSF Photometry method exclusively. PSF (point spread function)
modeling is well suited for measuring stellar magnitudes in crowded fields,
or the magnitude of a star that has a close companion, e.g., Z Tau.
(See https://www.aavso.org/lpv-double-trouble-campaign-0)
MAOPhot is written in Python. It uses many Python 'astropy'
(https://www.astropy.org/) libraries. The astropy package contains key
functionality and common tools for performing astronomy and astrophysics
with Python. Included in the package is Photutils.psf. See "PSF Photometry"
(https://photutils.readthedocs.io/en/stable/psf.html) which describes many of
the classes and methods used in MAOPhot
This program was derived from MetroPSF by Maxym Usatov.
It has been redesigned for AAVSO reporting only and includes, but not limited
to the following enhancements:
- Generation of Effective PSF model, and ability to create a ‘rejection list’
- option to use an Integrated Gaussian PRF (Pixel Response Function) as model
- PSF Photometry using an iterative algorithm to perform point spread function
photometry in crowded fields
- Photometry using an ensemble of comparison stars or a single comp star
- Generation of Two-Color Photometry (B, V), (V, R) or (V, I), and Single Image
Photometry reports in AAVSO extended format
- Use of telescope Transformation Coefficients (needed for Two Color Photometry)
- Image display shows comp star AAVSO label number and name of any found VSX
objects in image field
- Intermediate results are saved as .csv files
- User can optionally enter a AAVSO Chart ID when retrieving comparison star
data
- User can specify check star and list of comp stars to use
More about Single Image Photometry
Single Image Photometry does not utilize the Transformation
coefficients. Simple differential photometry is used.
Only a single comp star is used (which must be the case if the AAVSO
VPhot tool, 'Transform Applier' is to be used).
Var mag = Var IM - Comp IM + Comp (known) mag
Check star mag = Check star IM - Comp IM + Comp (known) mag
where IM is the instrumental magnitude
-2.5 * np.log10(self.results_tab_df["flux_fit"] / exptime)
self.results_tab_df["flux_fit"] represents the fitted flux for the
star (in that row)
More about Two Color Photometry
MAOPhot mimics VPhot's "Two Color Photometry" (Usually B and V)
See spreadsheet: ProcessingMaoImages_202281V1117Her.xlsx
It includes formulas to generate "two color photometry".
The method generate_aavso_report_2color() uses the same formula which
uses the telescope Transformation Coefficients.
The following coefficients are used and are added to the list of "pink"
fields. (Pink fields required for report generation)
Tbv
Tv_bv
Tb_bv
These are associated with a particular telescope, eg. MAO
Required data (Pink fields):
Tbv
Tv_bv
Tb_bv
"Select Comp Stars (AAVSO Label)"
"Use Check star (AAVSO Label)"
...
etc.
The following formulas are calculated for each comp star and
var = check star;
then calculated again for each comp star and var = "Object Name".
E.g. V1117 Her
Δv = vvar - vcomp
Δ(B-V) = Tbv * Δ(b-v)
Vvar = Δv + Tv_bv * Δ(B-V) + Vcomp
where:
Δv = IM of variable - IM comp
Vcomp = published V-magnitude of comp
Δ(B-V) = Tbv * difference between standard color of var and standard
color of comp
To calculate these formulas and then generate a report, two (2) sets
of results_tab_df in csv format must exist, one for B and one for V and
must have been derived from the B and V images of the Var under
analysis.
When generate_aavso_report_2color is called, by the menu item,
'Two Color Photometry->Two Color Photometry (B,V)', the user will
be asked to specify the 2 aformentioned csv files.
From these files/Panda databases, the formulas are calculated, and
results are displayed.
Error Estimation :
MAOPhot mimics VPhot when calculating error estimation.
From VPhot documentation:
In an ensemble solution with more than two comp stars,
the magnitude is estimated as the average of the individual
comp stars estimate [of the check star], and the error is taken as
the standard deviation of this sample.
If one or two comp stars are used, the error estimate is
based on the SNR of each measurement (the target measurement
and the comp stars measurements). The standard error of a
measurement is defined as 2.5 * np.log10(1 + 1 / SNR)
[The errors are added in quadruture.]
--
get_comparison_stars
Given the specified Check Star in field "Use Check Star (AAVSO label)"
and the list of comp stars in "Select Comp Stars (AAVSO Label)", these
stars are gotten from the AAVSO and each has an additional attribute,
is_check_star, added to the results_tab_df DB.
MAOPhot derived from original MetroPSF (C) Copyright 2021, Maxym Usatov
<maxim.usatov@bcsatellite.net> Refer to metropsf.pdf for license information.
This research made use of Photutils, an Astropy package for
detection and photometry of astronomical sources (Bradley et al. 20XX).
"""
#
# Constants
#
__version__ = "1.0.0"
__label_prefix__ = "comp " # prepended to comp stars label's; forces type to str
__empty_cell__ = "%" #this forces cell to be type string
from ast import Assert
from astropy.stats import SigmaClip
from astropy.stats import sigma_clipped_stats
from astropy.stats import gaussian_sigma_to_fwhm
from astropy.table import Table
from astropy.modeling.fitting import LevMarLSQFitter, SLSQPLSQFitter, SimplexLSQFitter
from astropy.nddata import NDData
from astropy.nddata import Cutout2D
from astropy.visualization import SqrtStretch, LogStretch, AsinhStretch, simple_norm
from astropy.coordinates import SkyCoord
from astropy.wcs import WCS
from astropy import units as u
from astropy.time import Time, TimeDelta
from astropy.io import fits
from photutils.background import Background2D
from photutils.background import MedianBackground
from photutils.background import MADStdBackgroundRMS
from photutils.background import LocalBackground
from photutils.detection import find_peaks
from photutils.psf import IterativePSFPhotometry, PSFPhotometry
from photutils.psf import IntegratedGaussianPRF, SourceGrouper
from photutils.detection import IRAFStarFinder
from photutils.psf import extract_stars
from photutils.psf import EPSFBuilder,EPSFFitter
from tkinter import filedialog as fd
from tkinter import ttk
import tkinter as tk
from mpl_toolkits.mplot3d import Axes3D
import math
from matplotlib import cm
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.ticker import ScalarFormatter
import matplotlib.pyplot as plt
import matplotlib
from astroquery.vizier import Vizier
from astroquery.astrometry_net import AstrometryNet
from tqdm import tqdm
from PIL import Image, ImageTk, ImageMath
import pandas as pd
import logging
import sys
import requests
import csv
import os.path
import numpy as np
import warnings
import datetime
from time import gmtime, strftime
warnings.filterwarnings("ignore")
matplotlib.use("TkAgg")
# Photometry
# No imports beyond this line.
def save_background_image(stretch_min, stretch_max, zoom_level, image_data):
global FITS_minimum
global FITS_maximum
global background_image
background_image = Image.fromarray(image_data)
width, height = background_image.size
new_size = (int(width * zoom_level), int(height * zoom_level))
background_image = background_image.resize(new_size, Image.LANCZOS)
background_image = ImageMath.eval("(a + " + str(stretch_min / 100 * FITS_maximum) +
") * 255 / " + str(stretch_max / 100 * FITS_maximum), a=background_image)
background_image = ImageMath.eval("convert(a, 'L')", a=background_image)
background_image.save('background.jpg')
def save_image(stretch_min, stretch_max, zoom_level, image_data, filename):
global FITS_minimum
global FITS_maximum
_image = Image.fromarray(image_data)
width, height = _image.size
new_size = (int(width * zoom_level), int(height * zoom_level))
_image = _image.resize(new_size, Image.LANCZOS)
_image = ImageMath.eval("(a + " + str(stretch_min / 100 * FITS_maximum) +
") * 255 / " +
str(stretch_max / 100 * FITS_maximum),
a=background_image)
_image = ImageMath.eval("convert(a, 'L')", a=background_image)
_image.save(filename)
def generate_FITS_thumbnail(stretch_min, stretch_max, zoom_level,
stretching_stringvar):
global generated_image
global image_data
global FITS_minimum
global FITS_maximum
converted_data = image_data.astype(float)
if stretching_stringvar == "Square Root":
stretch = SqrtStretch()
converted_data = (converted_data - np.min(converted_data)
) / np.ptp(converted_data)
converted_data = stretch(converted_data)
if stretching_stringvar == "Log":
stretch = LogStretch()
converted_data = (converted_data - np.min(converted_data)
) / np.ptp(converted_data)
converted_data = stretch(converted_data)
if stretching_stringvar == "Asinh":
stretch = AsinhStretch()
converted_data = (converted_data - np.min(converted_data)
) / np.ptp(converted_data)
converted_data = stretch(converted_data)
generated_image = Image.fromarray(converted_data)
FITS_maximum = np.max(converted_data)
width, height = generated_image.size
new_size = (int(width * zoom_level), int(height * zoom_level))
generated_image = generated_image.resize(new_size, Image.LANCZOS)
generated_image = ImageMath.eval("(a + " +
str(stretch_min / 100 * FITS_maximum) +
") * 255 / " +
str(stretch_max / 100 * FITS_maximum),
a=generated_image)
image_file = ""
image_data = []
class MyGUI:
zoom_level = 1
linreg_error = 0
zoom_step = 0.5
photometry_results_plotted = False
ePSF_samples_plotted = False
results_tab_df = pd.DataFrame()
image_bkg_value = 0
bkg2D = None #if fetched, the Background2D object
fit_shape = 21
error_raised = False
histogram_slider_low = 0
histogram_slider_high = 5
last_clicked_x = 0
last_clicked_y = 0
ensemble_size = 0
jd = 0
image_file = ""
photometry_circles = {}
valid_parameter_list = {}
ePSF_rejection_list = pd.DataFrame({'x':[],'y':[],"stale":[]})
epsf_model = None
stars_tbl = Table()
# Parameter declaration and init
fit_width_entry = None
max_ensemble_magnitude_entry = None
fwhm_entry = None
star_detection_threshold_entry = None
photometry_iterations_entry = None
sharplo_entry = None
bkg_filter_size_entry = None
matching_radius_entry = None
aavso_obscode_entry = None
telescope_entry = None
tbv_entry = None
tv_bv_entry = None
tb_bv_entry = None
tvr_entry = None
tv_vr_entry = None
tr_vr_entry = None
tvi_entry = None
tv_vi_entry = None
ti_vi_entry = None
catalog_stringvar = None
vizier_catalog_entry = None
fitter_stringvar = None
astrometrynet_entry = None
astrometrynet_key_entry = None
object_kref_entry = None
object_sel_comp_entry = None
object_name_entry = None
object_notes_entry = None
display_all_objects = None
#
# The TopLoevel window containing the settings
es_top = None
def console_msg(self, MAOPhot_message, level=logging.INFO):
# add a time stamp
message = datetime.datetime.now().strftime("%d %b %Y %H:%M:%S")
message += " " + MAOPhot_message
self.console.insert(tk.END, message+"\n")
self.console.see(tk.END)
self.window.update_idletasks()
self.our_logger.log(level=level, msg=MAOPhot_message)
"""
file = open("MAOPhotpsf.log", "a+", encoding="utf-8")
file.write(str(message) + "\n")
file.close()
"""
def display_image(self):
if len(image_data) > 0:
self.canvas.delete("all")
global generated_image
generate_FITS_thumbnail(self.histogram_slider_low,
self.histogram_slider_high,
self.zoom_level,
self.stretching_stringvar.get())
self.image = ImageTk.PhotoImage(generated_image)
self.canvas.create_image(0, 0, anchor=tk.NW, image=self.image)
self.canvas.config(scrollregion=self.canvas.bbox(tk.ALL))
self.canvas.bind("<Button-1>", self.mouse_click)
if self.ePSF_samples_plotted:
self.display_ePSF_samples()
self.plot_photometry()
def load_FITS(self, image_file):
global image_figure
global image_data
global image
global image_width
global image_height
global header
global FITS_minimum
global FITS_maximum
global generated_image
try:
self.console_msg("Loading FITS: " + image_file)
with fits.open(image_file) as image:
self.image_file = image_file
self.filename_label['text'] = "FITS: " + image_file
self.canvas.delete("all")
self.zoom_level = 1
self.photometry_results_plotted = False
self.ePSF_samples_plotted = False
#Load previous work if it exists
if os.path.isfile(self.image_file+".csv"):
self.results_tab_df = pd.read_csv(self.image_file + ".csv")
else:
self.results_tab_df = pd.DataFrame()
header = image[0].header
image_data = fits.getdata(image_file)
image_width = image_data.shape[1]
image_height = image_data.shape[0]
self.wcs_header = WCS(image[0].header)
FITS_minimum = np.min(image_data)
FITS_maximum = np.max(image_data)
self.console_msg("Width: " + str(image_width) +
" Height: " + str(image_height))
self.console_msg(
"FITS Minimum: " + str(FITS_minimum) + " Maximum: " +
str(FITS_maximum))
if 'filter' in header:
self.filter = str(header['filter'])
self.set_entry_text(self.filter_entry, self.filter)
self.console_msg("Filter: " + self.filter)
else:
self.console_msg(
"Filter name not in FITS header. Set filter manually.")
if 'airmass' in header:
self.airmass = str(header['airmass'])
self.set_entry_text(self.airmass_entry, self.airmass)
self.console_msg("Airmass: " + self.airmass)
else:
self.console_msg(
"Airmass not in FITS header. Airmass may be required for AAVSO report. Set Airmass manually.")
if 'exptime' in header:
exptime = header['exptime']
self.set_entry_text(self.exposure_entry, exptime)
self.console_msg("Exposure: " + str(exptime))
else:
self.console_msg(
"Exposure (EXPTIME) not in FITS header. Set exposure manually.")
self.jd = 0
if 'date-obs' in header:
try:
date_obs = Time(header['date-obs'])
self.jd = Time(date_obs, format='jd')
self.console_msg("DATE-OBS: " + str(self.jd.to_value('iso')) + " UTC; JD: " + str(self.jd))
self.set_entry_text(self.date_obs_entry, str(self.jd))
except Exception as e:
self.error_raised = True
exc_type, exc_obj, exc_tb = sys.exc_info()
self.console_msg("Exception at line no: " + str(exc_tb.tb_lineno) + " " + str(e), level=logging.ERROR)
pass
if 'jd' in header:
jd = header['jd']
self.console_msg(
"Julian date at the start of exposure (from JD): " + str(jd))
self.jd = jd
self.date_obs_entry.delete(0, tk.END)
self.date_obs_entry.insert(0, str(self.jd))
self.image_bkg_value = np.median(image_data)
self.console_msg("Median background level, ADU: "
+ str(self.image_bkg_value))
self.console_msg("Ready")
except Exception as e:
self.error_raised = True
exc_type, exc_obj, exc_tb = sys.exc_info()
self.console_msg("Exception at line no: " + str(exc_tb.tb_lineno)
+" "+str(e), level=logging.ERROR)
pass
def open_FITS_file(self):
global header
options = {}
options['defaultextension'] = '.fit'
options['filetypes'] = [('FIT', '.fit'),('FITS', '.fits'), ('FTS', '.fts')]
options['title'] = 'Open FIT image file...'
image_file = fd.askopenfilename(**options)
if len(image_file) > 0:
try:
self.load_FITS(image_file)
self.display_image()
self.clear_psf_label()
self.clear_epsf_plot()
except Exception as e:
self.error_raised = True
exc_type, exc_obj, exc_tb = sys.exc_info()
self.console_msg("Exception at line no: " + str(exc_tb.tb_lineno) +" "+str(e), level=logging.ERROR)
def save_FITS_file_as(self):
global image
global image_data
global header
options = {}
options['defaultextension'] = '.fit'
options['filetypes'] = [('FIT', '.fit'),('FITS', '.fits'), ('FTS', '.fts')]
options['title'] = 'Save FIT image file as...'
file_name = fd.asksaveasfile(**options)
try:
if len(str(file_name)) > 0:
self.console_msg("Saving FITS as " + str(file_name.name))
fits.writeto(file_name.name, image_data,
header, overwrite=True)
self.console_msg("Saved.")
except Exception as e:
self.error_raised = True
exc_type, exc_obj, exc_tb = sys.exc_info()
self.console_msg("Exception at line no: " + str(exc_tb.tb_lineno) +" "+str(e), level=logging.ERROR)
def save_FITS_file(self):
global image
global image_data
global header
file_name = self.image_file
try:
if len(str(file_name)) > 0:
self.console_msg("Saving FITS as " + str(file_name))
fits.writeto(file_name, image_data, header, overwrite=True)
self.console_msg("Saved.")
except Exception as e:
self.error_raised = True
exc_type, exc_obj, exc_tb = sys.exc_info()
self.console_msg("Exception at line no: " + str(exc_tb.tb_lineno) +" "+str(e), level=logging.ERROR)
def create_ePSF(self):
global header
self.console_msg("Initiating Effective PSF building...")
#make sure an image is loaded
if len(image_data) == 0:
self.console_msg("Cannot proceed; an image must be loaded first; use File->Open...")
return
try:
# Find local peaks in an image that are above a specified
# threshold value. Threshold determined
#
#determine the background, it will be subtrated from image_data, later
bkg_filter_size = int(self.bkg_filter_size_entry.get())
#make sure this is not an even number
if bkg_filter_size % 2 == 0:
bkg_filter_size += 1
sigma_clip = SigmaClip(sigma=3.0)
bkg_estimator = MedianBackground()
self.console_msg("Estimating background...")
self.bkg2D = Background2D(
image_data,
box_size=(self.fit_shape * 10, self.fit_shape * 10),
filter_size=(bkg_filter_size, bkg_filter_size),
edge_method='crop',
sigma_clip=sigma_clip,
bkg_estimator=bkg_estimator)
self.console_msg("Median Background2D level: "
+ str(self.bkg2D.background_median))
peaks_tbl = find_peaks(image_data, threshold=self.bkg2D.background_median * 3)
peaks_tbl_len = len(peaks_tbl)
self.console_msg("ePSF: found " + str(peaks_tbl_len) + " peaks.")
#mask out peaks near the boundary; use twice the aperature entry
#
##Calculate size of cutouts for EPSFBuilder
## make it 10 times the aperture entry
##same size used in display_ePSF_samples
self.fit_shape = int(self.fit_width_entry.get())
size = 10*self.fit_shape + 1
hsize = (size - 1)/2
x = peaks_tbl['x_peak']
y = peaks_tbl['y_peak']
_image = Image.fromarray(image_data)
width, height = _image.size
mask = ((x > hsize) & (x < (width -1 - hsize)) &
(y > hsize) & (y < (height -1 - hsize)))
#prelim_stars_tbl are inbound stars (not to close to the edge)
prelim_stars_tbl = Table()
prelim_stars_tbl['x'] = x[mask]
prelim_stars_tbl['y'] = y[mask]
prelim_stars_tbl['rejected'] = False #init
prelim_stars_tbl_len = len(prelim_stars_tbl)
self.console_msg("ePSF: found and removed " + str(peaks_tbl_len - prelim_stars_tbl_len) + " peaks on edge.")
self.console_msg("ePSF: " + str(prelim_stars_tbl_len) + " peaks remain.")
# now set 'rejected' to True for any stars that are proximate to
# another in the same list
for i in range(len(prelim_stars_tbl)):
for ii in range(len(prelim_stars_tbl)):
if ii == i:
continue
i_x = prelim_stars_tbl[i]['x']
i_y = prelim_stars_tbl[i]['y']
ii_x = prelim_stars_tbl[ii]['x']
ii_y = prelim_stars_tbl[ii]['y']
if math.dist([i_x, i_y], [ii_x, ii_y]) <= hsize:
#reject this because it is too close to that companion
prelim_stars_tbl[i]['rejected'] = True
prelim_stars_tbl[ii]['rejected'] = True
x = prelim_stars_tbl['x']
y = prelim_stars_tbl['y']
reject_this = prelim_stars_tbl['rejected']
mask = reject_this == False # only keep ones we don't reject
isolated_stars_tbl = Table()
isolated_stars_tbl['x'] = x[mask]
isolated_stars_tbl['y'] = y[mask]
isolated_stars_tbl['rejected'] = False #init
isolated_stars_tbl_len = len(isolated_stars_tbl)
self.console_msg("ePSF: found and removed " + str(prelim_stars_tbl_len - isolated_stars_tbl_len) + " close companions.")
self.console_msg("ePSF: " + str(isolated_stars_tbl_len) + " peaks remain.")
# now set 'rejected' to True for any stars that are proximate to a
# coordinate in ePSF_rejection_list
for psf_x, psf_y, psf_reject in isolated_stars_tbl:
isolated_stars_tbl.add_index('x')
for index, row in self.ePSF_rejection_list.iterrows():
reject_x = row['x']
reject_y = row['y']
if abs(reject_x - psf_x) <= hsize and abs(reject_y - psf_y) <= hsize:
#user does not want this one
isolated_stars_tbl.loc[psf_x]['rejected'] = True
break
x = isolated_stars_tbl['x']
y = isolated_stars_tbl['y']
reject_this = isolated_stars_tbl['rejected']
mask = reject_this == False # only keep ones we don't reject
self.stars_tbl = Table()
self.stars_tbl['x'] = x[mask]
self.stars_tbl['y'] = y[mask]
stars_tbl_len = len(self.stars_tbl)
self.console_msg("ePSF: found and removed " + str(isolated_stars_tbl_len - stars_tbl_len) + " peaks rejected by user.")
self.console_msg("ePSF: " + str(stars_tbl_len) + " peaks remain for EPSF Builder.")
# Subtract background from image_data
# bkg.background is a 2D ndarray of background image
clean_image = image_data-self.bkg2D.background
working_image = NDData(data=clean_image)
candidate_stars = extract_stars(working_image, self.stars_tbl, size=size)
epsf_builder = EPSFBuilder(oversampling=2, fitter=EPSFFitter(), maxiters=50)
self.console_msg("Starting ePSF Builder...(check console progress bar)")
self.epsf_model, fitted_stars = epsf_builder(candidate_stars)
model_length = len(self.epsf_model.data)
x = np.arange(0, model_length, 1)
y = np.arange(0, model_length, 1)
x, y = np.meshgrid(x, y)
#plot the psf model
self.psf_plot.clear()
self.psf_plot.plot_surface(x, y, self.epsf_model.data, cmap=cm.jet)
self.psf_plot_canvas.draw()
#plot ePSF samples
self.clear_epsf_plot()
self.display_ePSF_samples()
norm = simple_norm(self.epsf_model.data, 'log', percent=99.)
im = self.ePSF_plot.imshow(self.epsf_model.data, norm=norm, origin='lower', cmap='viridis')
self.ePSF_plot.set_title("Effective PSF")
self.fig_ePSF.colorbar(im, ax=self.ePSF_plot)
self.ePSF_plot_canvas.draw()
self.ePSF_samples_plotted = True
self.console_msg("Ready")
except Exception as e:
self.error_raised = True
exc_type, exc_obj, exc_tb = sys.exc_info()
self.console_msg("Exception at line no: " + str(exc_tb.tb_lineno) +" "+str(e), level=logging.ERROR)
def clear_ePSF(self):
global header
self.console_msg("Clearing ePSF model, Rejection List, plot...")
#drop all the rows but keep the 'x' and 'y' column
self.ePSF_rejection_list.drop(self.ePSF_rejection_list.index, inplace=True)
self.epsf_model = None #reset
self.stars_tbl = Table()
self.clear_psf_label()
self.clear_epsf_plot()
self.ePSF_samples_plotted = False
self.display_image()
self.console_msg("Ready")
return
def load_ePSF_rejection_list(self):
global header
try:
options = {}
options['defaultextension'] = '.csv'
options['filetypes'] = [('CSV', '.csv')]
options['title'] = 'Choose the ' + self.image_file + '-rejection.csv'
file_name = fd.askopenfilename(**options)
if len(str(file_name)) > 0 and os.path.isfile(str(file_name)):
self.console_msg("Loading Rejection list from: " + str(file_name))
self.ePSF_rejection_list = pd.read_csv(str(file_name))
self.ePSF_rejection_list["stale"] = True #reset
self.ePSF_samples_plotted = True
self.display_image()
else:
return
self.console_msg("Ready")
except Exception as e:
self.error_raised = True
exc_type, exc_obj, exc_tb = sys.exc_info()
self.console_msg("Exception at line no: " + str(exc_tb.tb_lineno) +" "+str(e), level=logging.ERROR)
return
def save_as_ePSF_rejection_list(self):
global header
try:
options = {}
options['defaultextension'] = '.csv'
options['filetypes'] = [('CSV', '.csv')]
options['title'] = 'Save Rejection List As... ' + self.image_file + '-rejection.csv'
options['initialfile'] = self.object_name_entry.get() + '-rejection.csv'
file_name = fd.asksaveasfile(**options)
if len(str(file_name)) > 0:
self.console_msg("Saving Rejection List as " + file_name.name)
dir_path = os.path.dirname(os.path.realpath(file_name.name)) + "\\"
self.ePSF_rejection_list.to_csv(file_name.name, index=False)
self.console_msg("Ready")
except Exception as e:
self.error_raised = True
exc_type, exc_obj, exc_tb = sys.exc_info()
self.console_msg("Exception at line no: " + str(exc_tb.tb_lineno) +" "+str(e), level=logging.ERROR)
return
def execute_noniterative_psf_photometry(self):
global header
self.console_msg(
"Initiating Non-iterative PSF photometry...")
if len(image_data) == 0:
self.console_msg("Cannot proceed; an image must be loaded first; use File->Open...")
return
try:
#determine the background, it will be subtrated from image_data, later
bkg_filter_size = int(self.bkg_filter_size_entry.get())
#make sure this is not an even number
if bkg_filter_size % 2 == 0:
bkg_filter_size += 1
self.fit_shape = int(self.fit_width_entry.get())
sigma_clip = SigmaClip(sigma=3.0)
bkg_estimator = MedianBackground()
self.console_msg("Estimating background...")
#Backgound2D used because background may vary over FOV
self.bkg2D = Background2D(
image_data,
box_size=(self.fit_shape * 10, self.fit_shape * 10),
filter_size=(bkg_filter_size, bkg_filter_size),
edge_method='crop',
sigma_clip=sigma_clip,
bkg_estimator=bkg_estimator)
self.console_msg("Median Background2D level: "
+ str(self.bkg2D.background_median))
# Subtract background from image_data
# bkg.background is a 2D ndarray of background image
clean_image = image_data-self.bkg2D.background
working_image = NDData(data=clean_image)
fwhm = float(self.fwhm_entry.get())
star_detection_threshold = float(
self.star_detection_threshold_entry.get())
sharplo = float(self.sharplo_entry.get())
star_find = IRAFStarFinder(threshold = star_detection_threshold,
fwhm = fwhm,
#minsep_fwhm = 1,
exclude_border = True,
roundhi = 3.0,
roundlo = -5.0,
sharplo = sharplo,
sharphi = 2.0
)
# the 2.5 in the following is from
# https://photutils.readthedocs.io/en/stable/grouping.html#getting-started
#grouper = SourceGrouper(2.5 * fwhm)
local_bkg = LocalBackground(inner_radius=fwhm*4, outer_radius=fwhm*8)
if self.fitter_stringvar.get() == "Sequential LS Programming":
self.console_msg(
"Setting fitter to Sequential Least Squares Programming")
selected_fitter = SLSQPLSQFitter()
elif self.fitter_stringvar.get() == "Simplex LS":
self.console_msg(
"Setting fitter to Simplex and Least Squares Statistic")
selected_fitter = SimplexLSQFitter()
else: # self.fitter_stringvar.get() == "Levenberg-Marquardt":
self.console_msg("Setting fitter to Levenberg-Marquardt")
selected_fitter = LevMarLSQFitter()
if self.epsf_model != None:
self.console_msg("Using derived Effective PSF Model")
psf_model = self.epsf_model
else:
#Use Gausian
sigma = 2.00 * fwhm / gaussian_sigma_to_fwhm
self.console_msg("Using Gaussian PRF for model; sigma = "+format(sigma, '.3f')+\
"; fwhm = "+format(fwhm, '.1f')+\
"; fitting width/height = "+str(self.fit_shape))
psf_model = IntegratedGaussianPRF(sigma)
photometry = PSFPhotometry(
psf_model = psf_model,
fit_shape = self.fit_shape,
finder = star_find,
grouper = None,
fitter = selected_fitter,
fitter_maxiters = 100,
localbkg_estimator = local_bkg,
aperture_radius=1.5*fwhm,
progress_bar=True
)
self.console_msg("Starting Photometry...(check console progress bar)")
result_tab = photometry(data=working_image)
if 'message' in selected_fitter.fit_info:
self.console_msg("Done. PSF fitter message(s): " + str(selected_fitter.fit_info['message']))
else:
self.console_msg("Done. PSF fitter; no message available")
self.results_tab_df = result_tab[result_tab.colnames[0:14]].to_pandas() # only need first 15 columns
self.results_tab_df["removed_from_ensemble"] = False
self.results_tab_df["date-obs"] = float(self.date_obs_entry.get())
if len(self.airmass_entry.get()) > 0:
self.results_tab_df["AMASS"] = float(self.airmass_entry.get())
else:
self.results_tab_df["AMASS"] = "na"
# Calculate instrumental magnitudes
# Following added for "True" inst mag used in AAVSO report
image_exposure_time = float(self.exposure_entry.get())
self.results_tab_df["inst_mag"] = -2.5 * np.log10(self.results_tab_df["flux_fit"] / image_exposure_time)
#record for later
self.results_tab_df["exposure"] = image_exposure_time
self.results_tab_df.to_csv(self.image_file + ".csv", index=False)
self.console_msg("Photometry saved to " + str(self.image_file + ".csv") + "; len = " + str(len(self.results_tab_df)))
self.plot_photometry()
except Exception as e:
self.error_raised = True
exc_type, exc_obj, exc_tb = sys.exc_info()
self.console_msg("Exception at line no: " + str(exc_tb.tb_lineno) +" "+str(e), level=logging.ERROR)
def execute_iterative_psf_photometry(self):
global header
self.console_msg(
"Initiating iterative PSF photometry...")
if len(image_data) == 0:
self.console_msg("Cannot proceed; an image must be loaded first; use File->Open...")
return
try:
#determine the background, it will be subtrated from image_data, later
bkg_filter_size = int(self.bkg_filter_size_entry.get())
#make sure this is not an even number
if bkg_filter_size % 2 == 0:
bkg_filter_size += 1
self.fit_shape = int(self.fit_width_entry.get())
sigma_clip = SigmaClip(sigma=3.0)
bkg_estimator = MedianBackground()
self.console_msg("Estimating background...")
#Backgound2D used because background may vary over FOV
self.bkg2D = Background2D(
image_data,
box_size=(self.fit_shape * 10, self.fit_shape * 10),
filter_size=(bkg_filter_size, bkg_filter_size),
edge_method='crop',
sigma_clip=sigma_clip,
bkg_estimator=bkg_estimator)
self.console_msg("Median Background2D level: "
+ str(self.bkg2D.background_median))
# Subtract background from image_data
# bkg.background is a 2D ndarray of background image
clean_image = image_data-self.bkg2D.background
working_image = NDData(data=clean_image)
fwhm = float(self.fwhm_entry.get())
star_detection_threshold = float(
self.star_detection_threshold_entry.get())
iterations = int(self.photometry_iterations_entry.get()) if self.photometry_iterations_entry.get().isnumeric() else None
sharplo = float(self.sharplo_entry.get())
#bkg_filter_size = int(self.bkg_filter_size_entry.get())
star_find = IRAFStarFinder(threshold = star_detection_threshold,
fwhm = fwhm,
minsep_fwhm = 1,
exclude_border = True,
roundhi = 3.0,
roundlo = -5.0,
sharplo = sharplo,
sharphi = 2.0
)
local_bkg = LocalBackground(inner_radius=fwhm*4, outer_radius=fwhm*8)
if self.fitter_stringvar.get() == "Sequential LS Programming":
self.console_msg(
"Setting fitter to Sequential Least Squares Programming")
selected_fitter = SLSQPLSQFitter()