-
Notifications
You must be signed in to change notification settings - Fork 2
/
odi_helpers.py
1324 lines (1164 loc) · 49.3 KB
/
odi_helpers.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
import sys, os, glob, string
import numpy as np
import astropy as ast
import matplotlib.pyplot as plt
from pyraf import iraf
from tqdm import tqdm
import odi_config as odi
def deg_to_sex(ra, dec):
"""
Convert an Ra and Dec position in decimal degrees to hexadecimal
Parameters
----------
ra : float
Ra in decimal degrees
dec : float
Dec in decimal degrees
Returns
-------
ra : str
Ra in hexadecimal HH:MM:SS
dec : str
Dec in hexadecimal DD:MM:SS
"""
from astropy import units as u
from astropy.coordinates import Angle
rad = Angle(ra * u.deg)
decd = Angle(dec * u.deg)
ra = rad.to_string(unit=u.hour, sep=':')
dec = decd.to_string(unit=u.deg, sep=':')
return ra, dec
def get_targ_ra_dec(img, ota):
"""
Get and return the Ra and Dec of an OTA based on the ``RA`` and ``DEC``
header cards.
Parameters
----------
img : str
Name of image
ota : str
Name of ota
Returns
-------
ra : float
Ra in decimal degrees
dec : str
Dec in decimal degrees
"""
from astropy.io import fits
hdulist = fits.open(img.f)
hdu = hdulist[0]
# hdulist.info()
# print hdu.header
ra = hdu.header['RA']
dec = hdu.header['DEC']
return ra, dec
hdulist.close()
def list_wcs_coords(img, ota, gapmask, inst,output='radec.coo', gmaglim=20., stars_only=True, offline = False, source = 'sdss'):
"""
Create the files needed to fix the WCS solution on a given ota. This
function will create lists of SDSS, 2MASS, or Gaia sources depending on the
options selected by the user. If this function is run in the ``offline``
mode, the source catalogs will be taken from the files produced by
:py:func:`offlinecats`
Parameters
----------
img : str
Name of image
ota : str
Name of OTA
gapmask : numpy array
A numpy array of the gap location on the ota. This can be produced by
the function :py:func:`get_gaps.get_gaps`. The gap mask is used to
filter out stars that fall in or near gaps on the ota.
int : str
Version of ODI used, ``podi`` or ``5odi``
output : str
Desired name of the output catalog
gmaglim : float
Magnitude limit in the g band for sources that will be included in the
catalogs. This might need to be adjusted according to your data. If it
is too bright, there might not be enough sources to produces a good
WCS solution.
stars_only : bool
When using SDSS sources this only includes sources flagged as stars
offline : bool
When ``True`` this function will use the catalogs produced by
:py:func:`offlinecats`. If ``False`` this function will query the
online ``SDSS`` catalog for sources.
source : str
Name of desired source catalog. Must be either ``sdss``,``twomass``, or
``gaia``.
Note
----
This functions produces three files for each ota in the ``coords``
directory with the following naming scheme:
1. ``img.nofits()+'.'+ota+'.radec.coo'``
2. ``img.nofits()+'.'+ota+'.radec.coo.px'``
3. ``img.nofits()+'.'+ota+'.sdssxy'``
"""
if offline == False:
xdim, ydim = odi.get_sdss_coords(img, ota, inst,output=odi.coordspath+img.nofits()+'.'+ota+'.sdss')
ras,decs,psfMag_u,psfMagErr_u,psfMag_g,psfMagErr_g,psfMag_r,psfMagErr_r,psfMag_i,psfMagErr_i,psfMag_z,psfMagErr_z = np.loadtxt(odi.coordspath+img.nofits()+'.'+ota+'.sdss',usecols=(0,1,2,3,4,5,6,7,8,9,10,11), unpack=True, delimiter=',', skiprows=2)
probPSF = np.loadtxt(odi.coordspath+img.nofits()+'.'+ota+'.sdss', usecols=(12,), dtype=int, unpack=True, delimiter=',', skiprows=2)
coords2 = list(zip(ras[np.where((psfMag_g<gmaglim) & (probPSF==1))],decs[np.where((psfMag_g<gmaglim) & (probPSF==1))]))
if offline == True and source == 'sdss':
sdss_cat = odi.sdsspath+'offline_'+ota+'.'+img.base()+'.sdss'
tqdm.write('Using Ra and Dec from:', sdss_cat,'for fixwcs')
ras,decs,psfMag_u,psfMagErr_u,psfMag_g,psfMagErr_g,psfMag_r,psfMagErr_r,psfMag_i,psfMagErr_i,psfMag_z,psfMagErr_z = np.loadtxt(sdss_cat,usecols=(0,1,2,3,4,5,6,7,8,9,10,11), unpack=True, delimiter=',', skiprows=1)
coords2 = list(zip(ras[np.where(psfMag_g<gmaglim)],decs[np.where(psfMag_g<gmaglim)]))
if offline == True and source == 'twomass':
twomass_cat = odi.twomasspath+'offline_'+ota+'.'+img.base()+'.mass'
ras,decs = np.loadtxt(twomass_cat,usecols=(2,3), unpack=True, delimiter=',', skiprows=1)
# Just creating dummy variables so that the file formats remain the same for other functions
psfMag_u = np.ones(len(ras))
psfMagErr_u = np.ones(len(ras))
psfMag_g = np.ones(len(ras))
psfMagErr_g = np.ones(len(ras))
psfMag_r = np.ones(len(ras))
psfMagErr_r = np.ones(len(ras))
psfMag_i = np.ones(len(ras))
psfMagErr_i = np.ones(len(ras))
psfMag_z = np.ones(len(ras))
psfMagErr_z = np.ones(len(ras))
coords2 = list(zip(ras,decs))
if source == 'gaia':
gaia_cat = odi.gaiapath+'offline_'+ota+'.'+img.base()+'.gaia'
ras,decs = np.loadtxt(gaia_cat,usecols=(0,1), unpack=True, delimiter=',', skiprows=1)
# Just creating dummy variables so that the file formats remain the same
# for other functions
psfMag_u = np.ones(len(ras))
psfMagErr_u = np.ones(len(ras))
psfMag_g = np.ones(len(ras))
psfMagErr_g = np.ones(len(ras))
psfMag_r = np.ones(len(ras))
psfMagErr_r = np.ones(len(ras))
psfMag_i = np.ones(len(ras))
psfMagErr_i = np.ones(len(ras))
psfMag_z = np.ones(len(ras))
psfMagErr_z = np.ones(len(ras))
coords2 = list(zip(ras,decs))
hdulist = odi.fits.open(img.f)
hdu = odi.tan_header_fix(hdulist[ota])
if offline == True:
xdim = hdu.header['NAXIS1']
ydim = hdu.header['NAXIS2']
w = odi.WCS(hdu.header)
pixcrd2 = w.wcs_world2pix(coords2, 1)
pixid = []
with open(odi.coordspath+output,'w+') as f:
with open(odi.coordspath+output+'.pix', 'w+') as fp:
with open(odi.coordspath+img.nofits()+'.'+ota+'.sdssxy', 'w+') as fxy:
for i,c in enumerate(coords2):
if 20.0 <= pixcrd2[i,0] < xdim-100.0 and 20.0 <= pixcrd2[i,1] < ydim-100.0:
# make an image cutout of the gap mask
x, y = int(round(pixcrd2[i,0])), int(round(pixcrd2[i,1]))
cutout = gapmask[y-30:y+30,x-30:x+30]
# print cutout
if not (cutout.astype(bool)).any():
pixid.append(i)
r, d = odi.deg_to_sex(c[0], c[1])
print(r, d, psfMag_g[i], file=f)
print(pixcrd2[i,0], pixcrd2[i,1], i, 'm', file=fp)
print(pixcrd2[i,0], pixcrd2[i,1], ras[i],decs[i],psfMag_u[i],psfMagErr_u[i],psfMag_g[i],psfMagErr_g[i],psfMag_r[i],psfMagErr_r[i],psfMag_i[i],psfMagErr_i[i],psfMag_z[i],psfMagErr_z[i], file=fxy)
pixid = np.array(pixid)
pixcrd3 = pixcrd2[pixid]
hdulist.close()
return pixcrd3
def fix_wcs(img, ota, coords='radec.coo', iters=3):
"""
Try to improve the WCS solution of an OTA using the IRAF task ``msccmatch``.
This function will use the ``img.nofits()+'.'+ota+'.radec.coo'`` file produced
by :py:func:`list_wcs_coords`.
Parameters
----------
img : str
Name of image
ota : str
Name of OTA
coords : str
Name of coordinate file
iter : int
Number of desired iterations of ``msccmatch``. It is still being
tested, but one might be all that is necessary, especially if using the
Gaia catalog.
Note
----
This function is set up to use the files in the ``illcor`` directory. The
following are the parameters used by ``msccmatch``.
- verbose='yes'
- usebpm='no'
- nsearch=250
- search=30
- rsearch=0.2
- cfrac=.5
- csig=0.1
- nfit=5
- rms=1.0
- maxshif=5.0
- fitgeom="general"
- update='yes'
- interac='yes'
- fit='no',
- accept='yes'
- Stdout=1
"""
image = odi.illcorpath+'illcor_'+ota+'.'+img.stem()
iraf.mscred(_doprint=0)
iraf.unlearn(iraf.mscred.msccmatch)
for i in range(iters):
fix = iraf.msccmatch(input=image,
coords=odi.coordspath+coords,
usebpm='no',
verbose='yes',
nsearch=250,
search=30,
rsearch=0.2,
cfrac=.5,
csig=0.1,
nfit=5,
rms=1.0,
maxshif=5.0,
fitgeom="general",
update='yes',
interac='yes',
fit='no',
accept='yes',
Stdout=1)
tqdm.write('fixing WCS for',img.f+'['+ota+'], iter ='+repr(i))
tqdm.write(fix[-6])
tqdm.write(fix[-5])
tqdm.write(fix[-4])
tqdm.write(fix[-3])
tqdm.write(fix[-2])
def fix_wcs_full(img, coords='radec.coo', iters=1):
"""
Try to improve the WCS solution of a final stacked image.
Parameters
----------
img : str
Name of image
coords : str
Name of coordinate file
iter : int
Number of desired iterations of ``msccmatch``. It is still being
tested, but one might be all that is necessary, especially if using the
Gaia catalog.
Note
----
This function is set up to use the files in the ``illcor`` directory. The
following are the parameters used by ``msccmatch``.
- verbose='yes'
- usebpm='no'
- nsearch=250
- search=30
- rsearch=0.2
- cfrac=.5
- csig=0.1
- nfit=5
- rms=1.0
- maxshif=5.0
- fitgeom="general"
- update='yes'
- interac='yes'
- fit='no',
- accept='yes'
- Stdout=1
"""
iraf.mscred(_doprint=0)
iraf.unlearn(iraf.mscred.msccmatch)
# otaext = {'33':'[1]','34':'[2]','44':'[3]','43':'[4]','42':'[5]','32':'[6]','22':'[7]','23':'[8]','24':'[9]'}
for i in range(iters):
fix = iraf.msccmatch(input=img,
coords=coords,
usebpm='no',
verbose='yes',
nsearch=250,
search=30,
rsearch=0.2,
cfrac=.5,
csig=0.1,
nfit=5,
rms=1.0,
maxshif=5.0,
fitgeom="general",
update='yes',
interac='yes',
fit='no',
accept='yes',
Stdout=1)
tqdm.write('fixing WCS for',img.f+', iter ='+repr(i))
tqdm.write(fix[-6])
tqdm.write(fix[-5])
tqdm.write(fix[-4])
tqdm.write(fix[-3])
tqdm.write(fix[-2])
def repair_bad_wcs(img, ota, refimg, refota):
tqdm.write('repairing bad wcs solution for',img.f+'['+ota+']...')
# get good CD matrix values from the reference image
# refimg = refimg.f+'['+refota+']'
refhdu = odi.fits.open(refimg)
pvlist = refhdu[refota].header['PV*']
for pv in pvlist:
tpv = 'T'+pv
refhdu[refota].header.rename_keyword(pv, tpv, force=False)
w_ref = odi.WCS(refhdu[refota].header)
tqdm.write(w_ref.wcs.cd, w_ref.wcs.crpix, w_ref.wcs.crval)
# get the bad WCS info so we can do some checking
# image = img.f+'['+ota+']'
hdu = odi.fits.open(img.f)
pvlist = hdu[refota].header['PV*']
for pv in pvlist:
tpv = 'T'+pv
hdu[refota].header.rename_keyword(pv, tpv, force=False)
w = odi.WCS(hdu[ota].header)
tqdm.write(w.wcs.cd, w.wcs.crpix, w.wcs.crval)
def repair_wcs_keywords(img):
hdulist = odi.fits.open(img.f, mode='update')
existing_radesys = hdulist[0].header['RADESYS']
tqdm.write(img.f)
tqdm.write('--> Existing RADESYS value:', existing_radesys)
correct_radesys = existing_radesys.strip("'").strip()
tqdm.write('--> Correct RADESYS value:', correct_radesys)
hdulist[0].header["RADESYS"] = correct_radesys
# print 'fixing CTYPES in OTA headers'
for k in tqdm(img.otas):
existing_ctype1 = hdulist[k].header['CTYPE1']
existing_ctype2 = hdulist[k].header['CTYPE2']
correct_ctype1 = existing_ctype1.replace('TAN','TPV')
correct_ctype2 = existing_ctype2.replace('TAN','TPV')
hdulist[k].header['CTYPE1'] = correct_ctype1
hdulist[k].header['CTYPE2'] = correct_ctype2
hdulist.flush()
hdulist.close()
def getfwhm_ota(img, ota, gaia=False, radius=4.0, buff=7.0, width=5.0):
"""
Get a fwhm estimate for a single OTA using the SDSS catalog stars and
IRAF imexam (SLOW, but works). Adapted from Kathy Rohde's getfwhm script
(this implementation is simpler in practice). The radius, buff, and width
parameters are for the pyraf task rimexam. This fwhm measure comes from
a gaussian fittype.
The positions of the SDSS starts are pulled from a ``coords`` file. This
module automatically fetches the ``coords`` file for the ``img`` and ``ota``
being processed from the appropriate directory.
In addition to a median fwhm measurement this module will also
produce an ouputfile where the positions and fwhm of each source are stored.
This ``output`` file is used in other modules in the ``odi-tools`` software.
The name of this ``output`` file is generated based on the ``img`` and
``ota``.
Parameters
-----------
img : str
String containing name of the image currently in use
ota : str
Name of ota extension to be used (e.g. OTA33.SCI)
Returns
--------
gfwhm : float
Median fwhm measure of sources found in the ota field.
Examples
--------
>>> img = 'img1.fits'
>>> ota = 'OTA33.SCI'
>>> gfwhm = getfwhm_ota(img,ota)
"""
# coords= img.nofits()+'.'+ota+'.sdssxy'
image = odi.reprojpath+'reproj_'+ota+'.'+img.stem()
if gaia:
coords = odi.coordspath+'reproj_'+ota+'.'+img.base()+'.gaiaxy'
else:
coords = odi.coordspath+'reproj_'+ota+'.'+img.base()+'.sdssxy'
tqdm.write('Measuring GFWHM in {:s} \nusing coordinates from {:s}'.format(image, coords))
outputfile = odi.coordspath+img.nofits()+'.'+ota+'.fwhm.log'
iraf.tv.rimexam.setParam('radius',radius)
iraf.tv.rimexam.setParam('buffer',buff)
iraf.tv.rimexam.setParam('width',width)
iraf.tv.rimexam.setParam('rplot',20.)
iraf.tv.rimexam.setParam('center','yes')
# fit a gaussian, rather than a moffat profile (it's more robust for faint sources)
iraf.tv.rimexam.setParam('fittype','gaussian')
iraf.tv.rimexam.setParam('iterati',1)
if not os.path.isfile(outputfile):
iraf.tv.imexamine(image, frame=10, logfile = outputfile, keeplog = 'yes', defkey = "a", nframes=0, imagecur = coords,use_display='no', StdoutG='/dev/null',mode='h')
outputfile_clean = open(outputfile.replace('.log','_clean.log'),"w")
for line in open(outputfile,"r"):
if not 'INDEF' in line:
outputfile_clean.write(line)
if 'INDEF' in line:
outputfile_clean.write(line.replace('INDEF','999'))
outputfile_clean.close()
os.rename(outputfile.replace('.log','_clean.log'),outputfile)
gfwhm = np.loadtxt(outputfile, usecols=(10,), unpack=True)
# hdulist = ast.io.fits.open(image)
# seeing = hdulist[0].header['FWHMSTAR']
# gfwhm = seeing/0.11
medfwhm = np.median(gfwhm[np.where(gfwhm < 900.0)])
tqdm.write('median gwfhm in ota {:s}: {:5.2f} pixels'.format(ota, medfwhm))# (determined via QR)'
return np.median(gfwhm[np.where(gfwhm < 900.0)])
def getfwhm_full(img, radius=4.0, buff=7.0, width=5.0):
"""
Get a fwhm estimate for a stacked image using the SDSS catalog stars and
IRAF imexam (SLOW, but works). Adapted from Kathy Rohde's getfwhm script
(this implementation is simpler in practice). The radius, buff, and width
parameters are for the pyraf task rimexam. This fwhm measure comes from
a gaussian fittype.
The positions of the SDSS starts are pulled from a ``coords`` file. This
module automatically fetches the ``coords`` file for the ``img`` and ``ota``
being processed from the appropriate directory.
In addition to a median fwhm measurement this module will also
produce an ouputfile where the positions and fwhm of each source are stored.
This ``output`` file is used in other modules in the ``odi-tools`` software.
The name of this ``output`` file is generated based on the ``img``.
Parameters
-----------
img : str
String containing name of the image currently in use
Returns
--------
gfwhm : float
Median fwhm measure of sources found in the ota field.
Examples
--------
>>> img = 'stack1.fits'
>>> gfwhm = getfwhm_full(img)
"""
coords = img.nofits()+'.sdssxy'
outputfile = img.nofits()+'.fwhm.log'
iraf.tv.rimexam.setParam('radius',radius)
iraf.tv.rimexam.setParam('buffer',buff)
iraf.tv.rimexam.setParam('width',width)
iraf.tv.rimexam.setParam('rplot',20.)
iraf.tv.rimexam.setParam('center','yes')
# fit a gaussian, rather than a moffat profile (it's more robust for faint sources)
iraf.tv.rimexam.setParam('fittype','gaussian')
iraf.tv.rimexam.setParam('iterati',1)
if not os.path.isfile(outputfile):
iraf.tv.imexamine(img, frame=10, logfile = outputfile, keeplog = 'yes', defkey = "a", nframes=0, imagecur = coords, wcs = "logical", use_display='no', StdoutG='/dev/null',mode='h')
outputfile_clean = open(outputfile.replace('.log','_clean.log'),"w")
for line in open(outputfile,"r"):
if not 'INDEF' in line:
outputfile_clean.write(line)
if 'INDEF' in line:
outputfile_clean.write(line.replace('INDEF','999'))
outputfile_clean.close()
os.rename(outputfile.replace('.log','_clean.log'),outputfile)
#
# # unfortunately we have to toss the first measured fwhm value from the median because of the file format
# # gfwhm = np.genfromtxt(outputfile, usecols=(3,), skip_header=4, skip_footer=3, unpack=True)
gfwhm = np.loadtxt(outputfile, usecols=(10,), unpack=True)
# hdulist = ast.io.fits.open(image)
# seeing = hdulist[0].header['FWHMSTAR']
# gfwhm = seeing/0.11
tqdm.write('median gwfhm in ',img.f+': ',np.median(gfwhm),'pixels')# (determined via QR)'
return np.median(gfwhm)
def imcombine_lists(images_, filters, guide_otas):
"""
Create a list files for OTAs, sorted by filter, that will be later
combined to make a dark sky flat. Does not include guide OTAs in dark sky flat!
Parameters
----------
images : list
List of images
filters : list
List of filters present in the images list
guide_otas : list
List of guide OTA ids
Note
----
Nothing is returned by this function. A file will be created for each OTA
following this naming scheme: ``OTA##.SCI.filter.lis``. For example:
``OTA22.SCI.odi_g.lis``. Within each of these files will be a list of
images to combine.
"""
for filter in filters:
for key in tqdm(odi.OTA_dictionary, desc='Building dark sky flat lists:', ncols=0):
ota = odi.OTA_dictionary[key]
list_name = open(ota+'.'+filter+'.lis',"w")
for img in images_:
fullid = ota+'.'+img.stem()
raw_img = 'raw_'+ota+'.'+img.stem()
if filter in fullid:
if fullid not in guide_otas:
print(odi.rawpath+raw_img, file=list_name)
list_name.close()
return
def reproject_ota(img, ota, rad, decd, wcsref):
"""
Use the IRAF task ``mscimage`` in the ``mscred`` package to reproject
an OTA to a reference tangent plane with constant pixel scale.
Parameters
----------
img : str
Name of image being processed
ota : str
Name of current ``ota`` being processed in ``img``
rad : float
Reference Ra position for reprojection
decd : float
Reference Ra position for reprojection
wcfreg : str
Name of image and ota to be used as the reference image for ``mscimage``
Note
----
Nothing is returned by this function but the reprojected ota is saved to the
``repreopath``. The pipeline is setup to use OTA33 of
the first image in the images list as the reference image for this function.
Here is how the ``mscimage`` IRAF parameters are set:
- iraf.mscred.mscimage.format='image'
- iraf.mscred.mscimage.pixmask='yes'
- iraf.mscred.mscimage.verbose='yes'
- iraf.mscred.mscimage.wcssour='image'
- iraf.mscred.mscimage.ref=wcsref
- iraf.mscred.mscimage.ra=rad
- iraf.mscred.mscimage.dec=decd
- iraf.mscred.mscimage.scale=0.11
- iraf.mscred.mscimage.rotation=0.0
- iraf.mscred.mscfimage.blank=-999
- iraf.mscred.mscimage.interpo='poly5'
- iraf.mscred.mscimage.minterp='poly5'
- iraf.mscred.mscimage.nxbl=4096
- iraf.mscred.mscimage.nybl=4096
- iraf.mscred.mscimage.fluxcon='yes'
- iraf.mscred.mscimage(image,imout)
"""
from pyraf import iraf
image = odi.illcorpath+'illcor_'+ota+'.'+img.stem()
imout = odi.reprojpath+'reproj_'+ota+'.'+img.stem()
iraf.mscred(_doprint=0)
iraf.clobber='no'
iraf.unlearn(iraf.mscred.mscimage)
iraf.mscred.mscimage.format='image'
iraf.mscred.mscimage.pixmask='yes'
iraf.mscred.mscimage.verbose='yes'
iraf.mscred.mscimage.wcssour='image'
iraf.mscred.mscimage.ref=wcsref
iraf.mscred.mscimage.ra=rad
iraf.mscred.mscimage.dec=decd
iraf.mscred.mscimage.scale=0.11
iraf.mscred.mscimage.rotation=0.0
iraf.mscred.mscimage.blank=-999
iraf.mscred.mscimage.interpo='poly5'
iraf.mscred.mscimage.minterp='poly5'
iraf.mscred.mscimage.nxbl=4096
iraf.mscred.mscimage.nybl=4096
iraf.mscred.mscimage.fluxcon='yes'
iraf.mscred.mscimage(image,imout)
return
def bkg_boxes(hdu,nboxes,length,sources):
"""
Function to calculate the sigma clipped statistics of a number of randomly
generated boxes over an ota.
Parameters
----------
hdu : fits object
Hdulist that as been opened by astropy.fits.io
nboxes : int
Number of random boxes to generate over the ota
length : int
Length of side of box in pixels
sources : bool
If ``True`` any sources detected in a given box will be masked before
calculating the background statistics
Returns
-------
bg_stats : numpy array
Array containing the background stats of all of the boxes
bg_median : float
Median background level of the boxes
med_std : float
Median standard deviation of the background level in each box
std_std : float
Standard deviation of the standard deviations in each box
centers : list
Pixel centers of each box
"""
image = hdu.data
#Get length of image in each axis
naxis1 = hdu.header['NAXIS1']
naxis2 = hdu.header['NAXIS2']
#generate the centers of n random boxes.
box_centers = np.random.randint(length,np.min([naxis1-length,naxis2-length]),size=(nboxes,2))
#divide length by 2
# another numpy error on wopr (can't convert to integer), so don't worry about integer arithmetic
side = length/2
bg_stats = []
centers = []
for center in range(len(box_centers)):
x1 = int(box_centers[center][0]-side)
x2 = int(box_centers[center][0]+side)
y1 = int(box_centers[center][1]-side)
y2 = int(box_centers[center][1]+side)
#Check to ensure that box is within image
if (x1 > side and x2 < naxis1-1.5*side) and (y1 > side and y2 < naxis2-1.5*side):
centers.append(box_centers[center])
"""
The centers that are within the image bounds are returned
in case you need to examine the regions used.
"""
box = image[x1:x2,y1:y2]
if np.isnan(box).any() == False and (box >= 0).all() == True:
"""
Only boxes with non-negative values are kept.
This should help deal with cell gaps
The sigma and iter values might need some tuning.
"""
mean, median, std = odi.sigma_clipped_stats(box, sigma=3.0)
if std >= 2.0*np.sqrt(median):
pass
else:
if sources == False:
bg_stats.append((mean, median, std))
if sources == True:
threshold = median + (std * 2.)
segm_img = odi.detect_sources(box, threshold, npixels=20)
if segm_img is not None: # if the box doesn't have any sources, then just take the sigma clipped numbers
# print(box, threshold, segm_img)
mask = segm_img.data.astype(np.bool)# turn segm_img into a mask
selem = np.ones((10, 10)) # dilate using a 25x25 box
mask2 = odi.binary_dilation(mask, selem)
#new_mask = mask_first_pass + mask2
new_mask = mask2
mean_mask, median_mask, std_mask = odi.sigma_clipped_stats(box, sigma=3.0, mask=new_mask)
else :
mean_mask, median_mask, std_mask = mean, median, std
bg_stats.append((mean_mask, median_mask, std_mask))
bg_stats = np.reshape(np.array(bg_stats),(len(bg_stats),3))
centers = np.reshape(np.array(centers),(len(centers),2))
#Calculate median std of Background
med_std = np.median(bg_stats[:,2])
#calculate standard deviation of the std values
std_std = np.std(bg_stats[:,2])
#median
bg_median = np.median(bg_stats[:,1])
#Locate the box that had the largest std
#Array will be returned for plotting if wanted
# max_std = np.argmax(bg_stats[:,2])
# max_center = centers[max_std]
# max_box = image[max_center[0]-side:max_center[0]+side,max_center[1]-side:max_center[1]+side]
# max_box is currently not needed.
max_box = 1.0
return bg_stats,bg_median,med_std,std_std,centers,max_box
def bgsub_ota(img, ota, apply=False):
"""
Subtract a background level from an OTA.
Parameters
----------
img : str
Name of image being processed
ota : str
Name of current ``ota`` being processed in ``img``
apply : bool
If ``True`` the background level is calculated and subtracted. If
``False``, the background level is calculated by not subtracted from
the current ``ota``.
Returns
-------
bg_mean : float
Mean background level
bg_median : float
Meidan background level
bg_std : float
Standard deviation of background
Note
----
This function calls ``odi.mask_ota`` to calculate the background statistics
"""
from pyraf import iraf
image = odi.reprojpath+'reproj_'+ota+'.'+img.stem()
imout = odi.bgsubpath+'bgsub_'+ota+'.'+img.stem()
bg_mean, bg_median, bg_std = odi.mask_ota(img, ota, reproj=True)
tqdm.write('subtracting {:7.2f} from {:s}'.format(bg_median, image))
if apply and not os.path.isfile(imout):
# print bg_mean, bg_median, bg_std
iraf.unlearn(iraf.imutil.imarith)
iraf.imutil.imarith.setParam('operand1',image)
iraf.imutil.imarith.setParam('op','-')
iraf.imutil.imarith.setParam('operand2',bg_median)
iraf.imutil.imarith.setParam('result',imout)
iraf.imutil.imarith.setParam('verbose','no')
iraf.imutil.imarith(mode='h')
return bg_mean, bg_median, bg_std
def getfwhm_new():
pass
def stack_otas(ota):
"""
(Currently not used)
Stack the OTAs at the end of the ``odi_process.py`` using the ``IRAF`` total
``imcombine``. Here are the the settings used by ``imcombine``:
- combine='average'
- reject='none'
- offsets='wcs'
- masktype='goodvalue'
- maskval=0
- blank=-999
- scale='none'
- zero='none'
- lthresh=-900
- hthresh=60000
- logfile=ota+'_stack.log'
Parameters
----------
ota : str
Name of current ``ota`` being processed
"""
from pyraf import iraf
iraf.immatch.imcombine(odi.scaledpath+'scaled_'+ota+'*.fits', odi.otastackpath+ota+'_stack.fits', combine='average', reject='none', offsets='wcs', masktype='goodvalue', maskval=0, blank=-999, scale='none', zero='none', lthresh=-900, hthresh=60000, logfile=ota+'_stack.log')
def deep_obj_mask(img, ota, apply=False):
"""
Currently not used
"""
from astropy.io import fits
from astropy.stats import sigma_clipped_stats
image = odi.scaledpath+'scaled_'+ota+'.'+img.stem()
ota_mask = 'objmask_'+ota+'.'+str(img.dither())+'.fits'
hdulist = fits.open(image)
hdu_ota = hdulist[0]
# maskhdu = fits.open(bppath+ota_mask)
gapshdu = fits.open(odi.bppath+'reproj_mask_'+ota+'.'+img.stem())
total_mask = gapshdu[0].data
#maskhdu[0].data +
nx, ny = hdu_ota.data.shape
mean1, median1, std1 = sigma_clipped_stats(hdu_ota.data[0:ny/2,0:nx/2], mask=total_mask[0:ny/2,0:nx/2], sigma=3.0, iters=3)
mean2, median2, std2 = sigma_clipped_stats(hdu_ota.data[0:ny/2,nx/2:nx], mask=total_mask[0:ny/2,nx/2:nx], sigma=3.0, iters=3)
mean3, median3, std3 = sigma_clipped_stats(hdu_ota.data[ny/2:ny,0:nx/2], mask=total_mask[ny/2:ny,0:nx/2], sigma=3.0, iters=3)
mean4, median4, std4 = sigma_clipped_stats(hdu_ota.data[ny/2:ny,nx/2:nx], mask=total_mask[ny/2:ny,nx/2:nx], sigma=3.0, iters=3)
mean = [mean1, mean2, mean3, mean4]
median = [median1, median2, median3, median4]
std = [std1, std2, std3, std4]
# plt.imshow(hdu_ota.data, origin='lower', cmap='Greys_r', vmin=-10., vmax=500.)
# plt.imshow(total_mask, cmap=random_cmap(), alpha=0.5)
# plt.show()
return mean, median, std
def find_new_bg(refimg, filter):
"""
Calculate a new background level to be added to the OTAs before
stacking
Parameters
----------
refimg : str
Reference image for background calculation
filter : str
Filter of the reference image
Returns
-------
sky_med : float
Median background level
"""
fwhm, zp_med, zp_std, bg_mean, bg_median, bg_std = np.loadtxt('derived_props.txt',usecols=(4,5,6,7,8,9),unpack=True)
imgnum, ota_d, filt_d, guide_d = np.loadtxt('derived_props.txt',usecols=(0,1,2,3),unpack=True,dtype=str)
imgs = []
for i,s in enumerate(imgnum):
idn = s[0]
imgs.append(idn)
img = np.array(imgs)
keep = np.where((img == refimg.dither()) & (filt_d==filter))
sky_med = np.median(bg_median[keep].astype(float))
sky_mean = np.median(bg_mean[keep].astype(float))
sky_std = np.median(bg_std[keep].astype(float))
tqdm.write('calculated sky median, mean, std to re-add: {:.3f} {:.3f} {:.3f}'.format(sky_med, sky_mean, sky_std))
return sky_med, sky_mean, sky_std
def is_guide_ota(img, ota):
"""
Determines whether the specified image OTA was used for guiding.
Parameters
----------
img : str
Name of image being processed
ota : str
Name of current ``ota`` being processed in ``img``
Returns
-------
guide : boolean
True if guide OTA, False if not
"""
from astropy.io import fits
from photutils.segmentation import detect_sources, source_properties
guide = False
check_ota = 'raw/raw_'+ota+'.'+img.stem()
hdu = fits.open(check_ota)
data = hdu[0].data
segm = detect_sources(data, 1.0, 50000)
props = source_properties(data, segm)
corners = []
for p in props:
cutout = p.data_cutout
yc, xc = int(p.cutout_centroid[0].value), int(p.cutout_centroid[1].value)
bgcent = cutout[yc,xc]
bgcorn = np.array([cutout[0,0], cutout[0,-1], cutout[-1,0], cutout[-1,-1]])
# print(bgcent, bgcorn)
bgrat = bgcorn.max()/bgcent
corners.append(bgrat)
med_ratio = np.median(corners)
if med_ratio > 10:
guide = True
return guide
def make_stack_list(images, object, filter, inst):
"""
Makes a list of images to be stacked using ``stack_images()``. This list
does not include the guiding OTAs as determined by ``derived_props.txt``.
Parameters
----------
object : str
Name of object in field
filter : str
Filter name of images being stacked
Note
----
Produces a file with the following naming scheme ``object+'_'+filter+'_stack.list'``
"""
# fwhm_d, zp_med, zp_std, bg_mean, bg_median, bg_std = np.loadtxt('derived_props.txt',usecols=(4,5,6,7,8,9),unpack=True)
# imgnum, ota_d, filt_d, guide_d = np.loadtxt('derived_props.txt',usecols=(0,1,2,3),unpack=True,dtype=str)
# guide = (guide_d == 'True') # turn the text booleans into a boolean array
guide_otas = np.loadtxt('guide_otas.txt',usecols=(0,),unpack=True,dtype=str)
scaled = []
for img in images: # generate a list of the scaled images
for key in odi.OTA_dictionary:
ota = odi.OTA_dictionary[key]
fullid = ota+'.'+img.stem()
if fullid not in guide_otas:
scaled.append(odi.scaledpath+'scaled_'+ota+'.'+img.stem())
if not os.path.isfile(object.replace(' ','_')+'_'+filter+'_stack.list'):
with open(object.replace(' ','_')+'_'+filter+'_stack.list','w+') as stack_file:
for j, im in enumerate(scaled):
print(im, file=stack_file)
def stack_images(images, stackname, refimg):
"""
Stack the images that are in the list produced by ``make_stack_list`` using
the ``IRAF`` task ``imcombine``. The following are the parameters used by
``imcombine``.
- combine='average'
- reject='none'
- offsets='wcs'
- masktype='goodvalue'
- maskval=0
- blank=-999
- scale='none'
- zero='none'
- lthresh=-900
- hthresh=60000
- logfile=ota+'_stack.log'
Parameters
----------
stackname : str
Name given to final stacked images
refimg: str
Name of reference image used in background calculation
"""
from astropy.io import fits
from pyraf import iraf
tqdm.write(refimg.f)
fitsref = fits.open(refimg.f)
hduref = fitsref[0]
objname = stackname.replace(' ','_') #hduref.header['object'].replace(' ','_')
filter_name = hduref.header['filter']
ref_airmass = hduref.header['airmass']
sky_med, sky_mean, sky_std = odi.find_new_bg(refimg, filter_name)
odi.make_stack_list(images, objname, filter_name, refimg.inst)
# sky_med = hduref.header['skybg']
output = objname+'_'+filter_name+'.fits'
output_bpm = objname+'_'+filter_name+'_bpm.pl'
tqdm.write('@'+objname+'_'+filter_name+'_stack.list')
if not os.path.isfile(output):
iraf.unlearn(iraf.immatch.imcombine, iraf.imutil.imarith)
iraf.immatch.imcombine('@'+objname+'_'+filter_name+'_stack.list', 'temp', combine='average', reject='none', offsets='wcs', masktype='goodvalue', maskval=0, blank=-999, scale='none', zero='none', lthresh=-900, hthresh=60000)
# iraf.imutil.imarith.setParam('operand1','temp')
# iraf.imutil.imarith.setParam('op','+')
# iraf.imutil.imarith.setParam('operand2',sky_med)
# iraf.imutil.imarith.setParam('result',output)
# iraf.imutil.imarith.setParam('verbose','yes')
# iraf.imutil.imarith(mode='h')
# flip the image so it's N-up E-left
# first get the image dimensions from the header
fitsstack = fits.open('temp.fits')
xdim = fitsstack[0].header['NAXIS1']
ydim = fitsstack[0].header['NAXIS2']
iraf.imcopy('temp.fits['+repr(xdim)+':1,1:'+repr(ydim)+']', 'temp_flip.fits')
iraf.imutil.imexpr('(a != -999) ? a + b : -999',output,'temp_flip.fits',sky_med)
iraf.imutil.imexpr('a < 0',output_bpm, output)
iraf.imutil.imdelete('temp, temp_flip', verify='no')
iraf.unlearn(iraf.imutil.hedit)
iraf.imutil.hedit.setParam('images',output)
iraf.imutil.hedit.setParam('fields','BPM')
iraf.imutil.hedit.setParam('value',output_bpm)
iraf.imutil.hedit.setParam('add','yes')
iraf.imutil.hedit.setParam('addonly','no')