77
88# CDD
99# Created: 5/6/22
10- # Updated: 5/9/22
10+ # Updated: 7/8/24
1111
1212import numpy as np
1313import matplotlib .pyplot as plt
1414import pandas as pd
15+ import os as os
16+ import glob as glob
1517
1618#learning how to detect stars using astropy
1719from astropy .stats import sigma_clipped_stats
2426from astropy .stats import SigmaClip
2527from astropy .io import fits
2628
29+
2730import time as time
2831
2932import simmer .contrast as sim_con_curve
3033
31- #Test file
32- startname = '/Users/courtney/Documents/data/shaneAO/data-'
33- datestr = '2019-07-19'
34- midname = '-AO-Courtney.Dressing/reduced_'
35- starname = 'T294301883'
36- filt = 'J'
37- endname = '/final_im.fits'
38-
39- filename = startname + datestr + midname + datestr + '/' + starname + '/' + filt + endname
40- outdir = startname + datestr + midname + datestr + '/' + starname + '/' + filt + '/'
41-
42- def analyze (filename = filename , maxiter = 10 , postol = 1 , fwhmtol = 0.5 , inst = 'ShARCS' , outdir = '' , verbose = True ):
34+ def analyze (filename = 'filename' , maxiter = 10 , postol = 1 , fwhmtol = 0.5 , inst = 'ShARCS' , outdir = '' , verbose = True ):
4335
4436 #set defaults
4537 if inst == 'PHARO' :
@@ -51,66 +43,78 @@ def analyze(filename=filename, maxiter = 10, postol=1, fwhmtol = 0.5, inst = 'Sh
5143 #open the image and load the data
5244 im = fits .getdata (filename , ext = 0 )
5345
46+
5447 #Find target source using rough guesses about image properties
5548 original_sources = find_sources (im )
5649
5750 #Find brightest peak
5851 xcen , ycen = find_center (original_sources , xcen_guess = im .shape [0 ]/ 2. , ycen_guess = im .shape [1 ]/ 2. , verbose = verbose )
5952
53+ print ('found center: ' , xcen , ycen )
54+
6055 #Determine FWHM using that center
6156 fwhm = find_FWHM (im , [xcen ,ycen ])
6257 if verbose == True :
6358 print ('Estimated FWHM: ' , fwhm )
6459
65- #Iterate until coordinates and FWHM agree to within tolerance
66- #or until maximum number of iterations is reached
67- posdiff = 5
68- fwhmdiff = 2
69- niter = 1
70- while np .logical_and (niter < maxiter , np .logical_or (posdiff > postol , fwhmdiff > fwhmtol )):
71- if verbose == True :
72- print ('Beginning iteration ' , niter )
73-
74- #Find sources again
75- updated_sources = find_sources (im , fwhm = fwhm )
76- if verbose == True :
77- print ('Updated sources' )
78- print (updated_sources )
79-
80- #Find brightest peak again using updated list of stars
81- updated_xcen , updated_ycen = find_center (updated_sources , xcen_guess = xcen , ycen_guess = ycen , verbose = verbose )
82-
83- #Determine FWHM using that center
84- updated_fwhm = find_FWHM (im , [updated_xcen ,updated_ycen ])
85- if verbose == True :
86- print ('Estimated FWHM: ' , updated_fwhm )
87-
88- #Compute differences
89- posdiff = np .sqrt ((updated_xcen - xcen )** 2. + (updated_ycen - ycen )** 2. )
90- fwhmdiff = np .sqrt ((updated_fwhm - fwhm )** 2. )
91- if verbose == True :
92- print ('Current posdiff: ' , posdiff )
93- print ('Current fwhmdiff: ' , fwhmdiff )
94-
95- #Update reference values
96- xcen = updated_xcen
97- ycen = updated_ycen
98- fwhm = updated_fwhm
99- niter += 1
100-
101- #Give up if needed
102- if updated_sources is None :
103- print ('Source identification failed.' )
104- return im , xcen , ycen , np .NaN , None , None
105- # im, xcen, ycen, fwhm, updated_sources, contrast_curve
106-
107- #Save FWHM, center position to file
108- d = {'filename' : filename , 'xcen' : xcen , 'ycen' : ycen , 'fwhm' : fwhm }
109- key_details = pd .DataFrame (data = d ,index = [0 ])
110- key_details .to_csv (outdir + 'key_details.csv' , index = False )
111-
112- #Save sources to file
113- updated_sources .to_csv (outdir + 'detected_stars.csv' , index = False )
60+ #put in for J band TIC0233633993 05/30/21 (image not used because reobserved in June in better conditions)
61+ maxfwhm = 100.
62+ if fwhm > maxfwhm :
63+ fwhm = np .min ([fwhm ,maxfwhm ])
64+ print ('using maximum FWHM: ' , maxfwhm )
65+
66+ #Default is to skip search for companion stars
67+ updated_sources = 0
68+ if 5 < 2 :
69+ #Iterate until coordinates and FWHM agree to within tolerance
70+ #or until maximum number of iterations is reached
71+ posdiff = 5
72+ fwhmdiff = 2
73+ niter = 1
74+ while np .logical_and (niter < maxiter , np .logical_or (posdiff > postol , fwhmdiff > fwhmtol )):
75+ if verbose == True :
76+ print ('Beginning iteration ' , niter )
77+
78+ #Find sources again
79+ updated_sources = find_sources (im , fwhm = fwhm )
80+ if verbose == True :
81+ print ('Updated sources' )
82+ print (updated_sources )
83+
84+ #Find brightest peak again using updated list of stars
85+ updated_xcen , updated_ycen = find_center (updated_sources , xcen_guess = xcen , ycen_guess = ycen , verbose = verbose )
86+
87+ #Determine FWHM using that center
88+ updated_fwhm = find_FWHM (im , [updated_xcen ,updated_ycen ])
89+ if verbose == True :
90+ print ('Estimated FWHM: ' , updated_fwhm )
91+
92+ #Compute differences
93+ posdiff = np .sqrt ((updated_xcen - xcen )** 2. + (updated_ycen - ycen )** 2. )
94+ fwhmdiff = np .sqrt ((updated_fwhm - fwhm )** 2. )
95+ if verbose == True :
96+ print ('Current posdiff: ' , posdiff )
97+ print ('Current fwhmdiff: ' , fwhmdiff )
98+
99+ #Update reference values
100+ xcen = updated_xcen
101+ ycen = updated_ycen
102+ fwhm = updated_fwhm
103+ niter += 1
104+
105+ #Give up if needed
106+ if updated_sources is None :
107+ print ('Source identification failed.' )
108+ return im , xcen , ycen , np .NaN , None , None
109+ # im, xcen, ycen, fwhm, updated_sources, contrast_curve
110+
111+ #Save FWHM, center position to file
112+ d = {'filename' : filename , 'xcen' : xcen , 'ycen' : ycen , 'fwhm' : fwhm }
113+ key_details = pd .DataFrame (data = d ,index = [0 ])
114+ key_details .to_csv (outdir + 'key_details.csv' , index = False )
115+
116+ #Save sources to file
117+ updated_sources .to_csv (outdir + 'detected_stars.csv' , index = False )
114118
115119 #Make contrast curve (both regular & highres)
116120 for hh in np .arange (2 ):
@@ -124,6 +128,8 @@ def analyze(filename=filename, maxiter = 10, postol=1, fwhmtol = 0.5, inst = 'Sh
124128
125129 contrast_curve = sim_con_curve .contrast_curve_main (im , fwhm , inst , position = [xcen , ycen ], highres = highres )
126130 contrast_curve .to_csv (outdir + 'contrast_curve' + highrestag + '.csv' ,index = False )
131+ print ('saved contrast curve to: ' , outdir + 'contrast_curve' + highrestag + '.csv' )
132+ print ('saved contrast curve plot to :' , outdir + 'contrast_curve' + highrestag + '.png' )
127133
128134 #Make a plot
129135 plt .errorbar (contrast_curve .arcsec , contrast_curve .dmag , contrast_curve .dmrms )
@@ -215,3 +221,35 @@ def find_center(df, xcen_guess = 0, ycen_guess = 0, verbose=False):
215221 xcen = xcen_guess
216222 ycen = ycen_guess
217223 return xcen , ycen
224+
225+ #Test with a single file
226+ if 3 < 2 :
227+ startname = '/Users/courtney/Documents/data/shaneAO/data-'
228+ datestr = '2021-03-29'
229+ midname = '-AO-Courtney.Dressing/reduced_'
230+ starname = 'TIC0148883384'
231+ filt = 'J'
232+ endname = '/final_im.fits'
233+
234+ filename = startname + datestr + midname + datestr + '/' + starname + '/' + filt + endname
235+ # outdir = startname + datestr + midname + datestr + '/'+starname+'/'+filt+'/'
236+ print ('filename: ' , filename )
237+ flist = [filename ]
238+
239+ #Analyze ALL images
240+ if 3 < 4 :
241+ #Get list of final images
242+ namestr = '/Users/courtney/Documents/data/shaneAO/*/reduced*/*/*/final_im.fits'
243+ flist = glob .glob (namestr )
244+
245+ print ('Files: ' , len (flist ))
246+
247+ for ff in np .arange (len (flist )):
248+ filename = flist [ff ]
249+ print (flist [ff ])
250+ print (filename )
251+ outdir = os .path .dirname (filename )+ '/'
252+ print (ff , ': ' , outdir )
253+
254+ print ('working on : ' , filename )
255+ im , xcen , ycen , fwhm , updated_sources , contrast_curve = analyze (filename , outdir = outdir )
0 commit comments