-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscripts_UNCOVER_mosaics.py
1279 lines (958 loc) · 45.3 KB
/
scripts_UNCOVER_mosaics.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
##################################################################
# UNCOVER_mosaics_scripts/UNCOVER_mosaics_scripts.py #
# #
# Copyright 2022 Sedona Price <[email protected]> #
# Licensed under a 3-clause BSD style license - see LICENSE.rst #
##################################################################
import os, copy
import numpy as np
import matplotlib as mpl
try:
mpl.use('Agg')
except:
pass
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import matplotlib.patheffects as path_effects
from matplotlib.path import Path
from matplotlib.patches import PathPatch
from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, HPacker
from astropy.io import fits
from astropy.wcs import WCS
from astropy.visualization import make_lupton_rgb
talk_fonts_dict = {'font.family':' Arial, Helvetica Neue, Helvetica,sans-serif',
'mathtext.fontset': 'dejavusans',
'text.usetex': False ,
'axes.linewidth': 1,
'xtick.major.width': 1,
'xtick.minor.width': 1,
'ytick.major.width': 1,
'ytick.minor.width': 1}
talk_font_label_dict = {'fontweight': 550}
plt.rcParams.update(talk_fonts_dict)
def _get_dict_NIRCAM_primary(rgb_shape, noinsets=False):
if not noinsets:
# INCLUDES INSETS
pad_factor = 0.4
crop_y_fac = [0.0, 0.875]
crop_y_pix = [int(crop_y_fac[0]*rgb_shape[0]),
int(crop_y_fac[1]*rgb_shape[0])]
crop_x_fac = [0.0, 0.75]
crop_x_pix = [int(crop_x_fac[0]*rgb_shape[1]),
int(crop_x_fac[1]*rgb_shape[1])]
rgb_shape = ((crop_y_pix[1]-crop_y_pix[0]),
(crop_x_pix[1]-crop_x_pix[0]))
xpad = int(np.ceil(rgb_shape[1]*pad_factor))
rgb_big_shape = (rgb_shape[0], rgb_shape[1]+xpad, 3)
dict_NIRCAM_primary = {'pad_factor': pad_factor,
'crop_x_pix': crop_x_pix,
'crop_y_pix': crop_y_pix,
'filters_fontsize': 10,
'w_logo': 0.25,
'loc_logo': [-0.025/(1.+pad_factor) * 2., 0.025],
'annotate_field_label': 'Abell 2744',
'annotate_field_loc': (0.51,0.035),
'annotate_field_fontsize': 16,
'annotate_field_fontweight': 'bold',
'annotate_field_ha': 'left',
'annotate_field_va': 'bottom',
'ruler_loc': (0.2,0.025),
'ruler_vertical': False,
'filterlabel_loc': 'center left',
'filterlabel_bboxtoanchor': (0.025/(1.+pad_factor), 0.975),
'compassRA': 3.685,
'compassDEC': -30.41196,
'compass_len': 0.5,
'compass_units': 'arcmin',
}
dict_NIRCAM_primary['rgb_big_shape'] = rgb_big_shape
dict_NIRCAM_primary['clu_dicts'] = {
'Primary': {
'xpos': (1.-0.26)*rgb_big_shape[1] - 0.025*rgb_big_shape[0],
'ypos': 0.025*rgb_big_shape[0],
'width': 0.26 * rgb_big_shape[1],
'height': 0.26 * rgb_big_shape[1],
'label': 'primary cluster',
'label_loc': (0.5, 1.03),
'label_va': 'bottom',
'label_ha': 'center',
'inset_wedge': { 'direction': 'right',
'position_pix': [0.45*rgb_big_shape[1],
0.35*rgb_big_shape[0]],
'position_radec': [3.5890613224392545,-30.399657801823526],
},
'cutout': {'xlims_pix': [0.41*rgb_big_shape[1],
0.41*rgb_big_shape[1]+0.085*rgb_big_shape[1]] ,
'ylims_pix': [0.275*rgb_big_shape[0],
0.275*rgb_big_shape[0]+0.085*rgb_big_shape[1]],
'corners_radec': [[3.599700721107946,-30.40989946601362],
[3.5771050501813324,-30.390410732982055]], # LL / UR
},
},
'N': {
'xpos': 0.015*rgb_big_shape[1],
'ypos': 0.5*rgb_big_shape[0],
'width': 0.22 * rgb_big_shape[1],
'height': 0.22 * rgb_big_shape[1],
'label': None, #'N core',
'inset_wedge': { 'direction': 'left',
'position_pix': [0.49*rgb_big_shape[1],
0.67*rgb_big_shape[0]],
'position_radec': [3.578427776750986,-30.35596683807201],
},
'cutout': {'xlims_pix': [0.455*rgb_big_shape[1],
0.455*rgb_big_shape[1]+0.065*rgb_big_shape[1]] ,
'ylims_pix': [0.621*rgb_big_shape[0],
0.621*rgb_big_shape[0]+0.065*rgb_big_shape[1]],
'corners_radec': [[3.5877317917809206,-30.3626667037864],
[3.570465904230491,-30.347754496723287]], # LL / UR
},
},
'NW': {
'xpos': (1.-0.185)*rgb_big_shape[1] - 0.025*rgb_big_shape[0],
'ypos': (1.-0.025)*rgb_big_shape[0] - (0.185)*rgb_big_shape[1],
'width': 0.185 * rgb_big_shape[1],
'height': 0.185 * rgb_big_shape[1],
'label': None, #'NW core',
'inset_wedge': { 'direction': 'right',
'position_pix': [0.6*rgb_big_shape[1],
0.54*rgb_big_shape[0]],
'position_radec': [3.5491804906615614,-30.375710889030846],
},
'cutout': {'xlims_pix': [0.55*rgb_big_shape[1],
0.55*rgb_big_shape[1]+0.11*rgb_big_shape[1]] ,
'ylims_pix': [0.425*rgb_big_shape[0],
0.425*rgb_big_shape[0]+0.11*rgb_big_shape[1]],
'corners_radec': [[3.562472513062995,-30.389419870323387],
[3.5332341394003977,-30.36417771733208]], # LL / UR
},
},
}
else:
# NO INSETS
pad_factor = 0.
crop_y_fac = [0.,1.]
crop_y_pix = [int(crop_y_fac[0]*rgb_shape[0]),
int(crop_y_fac[1]*rgb_shape[0])]
crop_x_fac = [0., 0.875]
crop_x_pix = [int(crop_x_fac[0]*rgb_shape[1]),
int(crop_x_fac[1]*rgb_shape[1])]
rgb_shape = ((crop_y_pix[1]-crop_y_pix[0]),
(crop_x_pix[1]-crop_x_pix[0]))
xpad = int(np.ceil(rgb_shape[1]*pad_factor))
rgb_big_shape = (rgb_shape[0], rgb_shape[1]+xpad, 3)
dict_NIRCAM_primary = {'pad_factor': pad_factor,
'crop_x_pix': crop_x_pix,
'crop_y_pix': crop_y_pix,
'filters_fontsize': 10,
'w_logo': 0.25,
'loc_logo': [(1.-0.25*(rgb_big_shape[0]/rgb_big_shape[1]))-0.05, 0.025],
'annotate_field_label': 'Abell 2744',
'annotate_field_loc': (0.51,0.045),
'annotate_field_fontsize': 16,
'annotate_field_fontweight': 'bold',
'annotate_field_ha': 'left',
'annotate_field_va': 'bottom',
'ruler_loc': (0.95, 0.3),
'ruler_vertical': True,
'ruler_fontsize': 10.,
'filterlabel_loc': 'center left',
'filterlabel_bboxtoanchor': (0.025, 0.975),
'compassRA': 3.6262468,
'compassDEC': -30.327671,
'compass_len': 0.5,
'compass_units': 'arcmin',
'compass_fontsize': 9.5,
}
dict_NIRCAM_primary['rgb_big_shape'] = rgb_big_shape
dict_NIRCAM_primary['clu_dicts'] = None
return dict_NIRCAM_primary
def _get_dict_NIRISS_parallel(rgb_shape):
pad_factor = 0.1
crop_y_fac = [0.175, 0.875]
crop_y_pix = [int(crop_y_fac[0]*rgb_shape[0]),
int(crop_y_fac[1]*rgb_shape[0])]
crop_x_fac = [0.585, 1.]
crop_x_pix = [int(crop_x_fac[0]*rgb_shape[1]),
int(crop_x_fac[1]*rgb_shape[1])]
rgb_shape = ((crop_y_pix[1]-crop_y_pix[0]),
(crop_x_pix[1]-crop_x_pix[0]))
dict_NIRISS_parallel = {'pad_factor': pad_factor,
'crop_x_pix': crop_x_pix,
'crop_y_pix': crop_y_pix,
'filters_fontsize': 12,
'w_logo': 0.285,
'loc_logo': [0.04, 0.025],
'annotate_field_label': 'NIRISS Parallel',
'annotate_field_loc': (1.-0.035,0.085),
'annotate_field_ha': 'right',
'annotate_field_va': 'bottom',
'annotate_field_fontsize': 16,
'annotate_field_fontweight': 'bold',
'annotate_field_label2': 'Overlaps HFF',
'annotate_field_loc2': (1.-0.035,0.035),
'annotate_field_ha2': 'right',
'annotate_field_va2': 'bottom',
'annotate_field_fontsize2': 14,
'annotate_field_fontweight2': 'normal',
'annotate_field_fontstyle2': 'italic',
'ruler_loc': (0.005,0.12),
'ruler_vertical': False,
'ruler_fontsize': 10.5,
'filterlabel_loc': 'center right',
'filterlabel_bboxtoanchor': (1.-(0.025/(1.+pad_factor)), 0.975),
'compassRA': 3.412784,
'compassDEC': -30.353649,
'compass_len': 0.5,
'compass_units': 'arcmin',
}
xpad = int(np.ceil(rgb_shape[1]*pad_factor))
rgb_big_shape = (rgb_shape[0], rgb_shape[1]+xpad, 3)
dict_NIRISS_parallel['rgb_big_shape'] = rgb_big_shape
dict_NIRISS_parallel['clu_dicts'] = None
return dict_NIRISS_parallel
def _rebin(arr, new_2dshape):
shape = (new_2dshape[0], arr.shape[0] // new_2dshape[0],
new_2dshape[1], arr.shape[1] // new_2dshape[1])
return arr.reshape(shape).sum(-1).sum(-2)
def _get_pixscale(hdr):
if 'CD2_2' in hdr.keys():
pixscale = np.abs(hdr['CD2_2'])
elif 'PC2_2' in hdr.keys():
pixscale = np.abs(hdr['PC2_2'])
elif 'CDELT2' in hdr.keys():
pixscale = np.abs(hdr['CDELT2'])
else:
raise ValueError("Could not get pixel scale from header!")
pixscale *= 3600. # Convert from degrees to arcsec
return pixscale
def _pstamp_radec2pix(ra, dec, wcs, loc='center', npix=101):
if isinstance(loc, str):
if loc == 'center':
npixhalf = (npix-1)/2.
npix_off = [int(npixhalf), int(npixhalf)]
else:
raise ValueError
else:
# Assume a tuple in pstamp pixel coords: 0:npix-1 for x,y
npix_off = loc
x, y = wcs.wcs_world2pix(ra, dec, 1)
xint = x.astype(int); yint=y.astype(int)
if loc is None:
# No offsets:
xoff = float(x)
yoff = float(y)
else:
# Offset relative to the number of pixels in the pstamp
xoff = float(x) - (xint-npix_off[0])
yoff = float(y) - (yint-npix_off[1])
return xoff, yoff
# Parallel to make_lupton_rgb
def _make_linear_rgb(image_r, image_g, image_b, minimum=0, maximum=None,
filename=None):
if maximum is None:
maximum = np.max([image_r.max(), image_g.max(), image_b.max()])
try:
_ = len(maximum)
except:
maximum = 3*[maximum]
try:
_ = len(minimum)
except:
minimum = 3*[minimum]
rs = (image_r - minimum[0])/(maximum[0]-minimum[0])
rs[rs>1.] = 1.
rs[rs<0.] = 0.
gs = (image_g - minimum[1])/(maximum[1]-minimum[1])
gs[gs>1.] = 1.
gs[gs<0.] = 0.
bs = (image_b - minimum[2])/(maximum[2]-minimum[2])
bs[bs>1.] = 1.
bs[bs<0.] = 0.
rgb = np.dstack([rs,gs,bs])
if filename:
import matplotlib.image
matplotlib.image.imsave(filename, rgb, origin='lower')
return rgb
# Parallel to make_lupton_rgb
def _make_log_rgb(image_r, image_g, image_b,
minimum=0, maximum=None,
scalea=1500,
filename=None):
if maximum is None:
maximum = [image_r.max(), image_g.max(), image_b.max()]
try:
_ = len(maximum)
except:
maximum = 3*[maximum]
if minimum is None:
minimum = [image_r.min(), image_g.min(), image_b.min()]
try:
_ = len(minimum)
except:
minimum = 3*[minimum]
images_s = []
for i, img in enumerate([image_r, image_g, image_b]):
# First, clip the data values using minimum / maximum:
imc = (img - minimum[i])/(maximum[i]-minimum[i])
# Clear memory:
img = None
imc[imc>1.] = 1.
imc[imc<0.] = 0.
# Then apply scaling, following DS9 convention, as given in
# http://ds9.si.edu/doc/ref/how.html
ims = np.log10(scalea * imc + 1)/np.log10(scalea)
imc = None
# Clean up the very upper end > 1 values:
ims[ims>1.] = 1.
images_s.append(ims)
ims = None
rgb = np.dstack(images_s)
if filename:
import matplotlib.image
matplotlib.image.imsave(filename, rgb, origin='lower')
return rgb
def _plot_ruler_arcsec(ax, pixscale, len_arcsec=0.5, len_arcmin=None,
ruler_unit='arcsec', dscale=None, add_kpc=False,
vertical=False, show_ruler_label=True,
ruler_loc='lowerleft', color='white', ybase_offset=0.02,
delx=0.075, dely=0.075, delx_text=0.04, dely_text=0.04,
text_path_effects=None,
lw=2,fontsize=8):
####################################
# Show a ruler line:
xlim = ax.get_xlim()
ylim = ax.get_ylim()
len_line_angular = len_arcsec/(pixscale)
if ruler_unit.lower() == 'arcsec':
if len_arcsec % 1. == 0.:
string = r'{}"'.format(int(len_arcsec))
else:
intpart = str(len_arcsec).split('.')[0]
decpart = str(len_arcsec).split('.')[1]
string = r'{}."{}'.format(intpart, decpart)
if add_kpc & (dscale is not None):
string += r' = {:0.1f} kpc'.format(len_arcsec/(1.*dscale))
elif ruler_unit.lower() == 'kpc':
if len_arcsec/dscale % 1. == 0.:
string = r'{:0.0f} kpc'.format(int(len_arcsec/dscale))
else:
string = r'{:0.1f} kpc'.format(len_arcsec/dscale)
elif ruler_unit.lower() == 'arcmin':
if len_arcmin % 1. == 0.:
string = r"{}".format(int(len_arcmin)) + u'\u2032'
else:
intpart = str(len_arcmin).split('.')[0]
decpart = str(len_arcmin).split('.')[1]
string = r"{}'{}".format(intpart, decpart)
len_line_angular = len_arcmin/(pixscale) * 60.
if vertical:
if 'left' in ruler_loc:
x_base = xlim[0] + (xlim[1]-xlim[0])*delx
sign_y = 1.
x_text = x_base + (xlim[1]-xlim[0])*(delx_text)
ha = 'left'
elif 'right' in ruler_loc:
x_base = xlim[1] - (xlim[1]-xlim[0])*delx
sign_y = -1.
x_text = x_base - (xlim[1]-xlim[0])*(delx_text)
ha = 'right'
if 'upper' in ruler_loc:
y_base = ylim[1] - (ylim[1]-ylim[0])*(dely)
y_text = y_base
va = 'top'
elif 'lower' in ruler_loc:
y_base = ylim[0] + (ylim[1]-ylim[0])*(dely)
y_text = y_base
va = 'bottom'
else:
# Assume it's a position, in axes fractions:
x_base = xlim[0] + (xlim[1]-xlim[0])*ruler_loc[0]
y_base = ylim[0] + (ylim[1]-ylim[0])*(ruler_loc[1])
if ruler_loc[0] <= 0.5:
ha = 'left'
sign_x_text = 1.
else:
ha = 'right'
sign_x_text = -1.
if ruler_loc[1] <= 0.5:
va = 'bottom'
ytext_extra = 0.
sign_y = 1.
else:
va = 'top'
ytext_extra = len_line_angular
sign_y = -1.
x_text = x_base + (xlim[1]-xlim[0])*(delx_text)*sign_x_text
y_text = y_base + ytext_extra
ax.plot([x_base, x_base], [y_base+sign_y*len_line_angular, y_base],
c=color, ls='-',lw=lw, solid_capstyle='butt')
if show_ruler_label:
txt = ax.annotate(string, xy=(x_text, y_text), xycoords="data", xytext=(0,0),
color=color, textcoords="offset points", ha=ha, va=va,
fontsize=fontsize)
else:
if 'left' in ruler_loc:
x_base = xlim[0] + (xlim[1]-xlim[0])*delx
sign_x = 1.
ha = 'left'
elif 'right' in ruler_loc:
x_base = xlim[1] - (xlim[1]-xlim[0])*delx
sign_x = -1.
ha = 'right'
if 'upper' in ruler_loc:
y_base = ylim[1] - (ylim[1]-ylim[0])*(ybase_offset+dely)
y_text = y_base - (ylim[1]-ylim[0])*(dely_text)
va = 'top'
elif 'lower' in ruler_loc:
y_base = ylim[0] + (ylim[1]-ylim[0])*(ybase_offset+dely)
y_text = y_base + (ylim[1]-ylim[0])*(dely_text)
va = 'bottom'
else:
# Assume it's a position, in axes fractions:
x_base = xlim[0] + (xlim[1]-xlim[0])*ruler_loc[0]
y_base = ylim[0] + (ylim[1]-ylim[0])*(ruler_loc[1]+ybase_offset)
y_text = y_base + (xlim[1]-xlim[0])*(dely_text)
sign_x = 1.
if ruler_loc[0] <= 0.5:
ha = 'left'
else:
ha = 'right'
if ruler_loc[1] <= 0.5:
va = 'bottom'
else:
va = 'top'
ax.plot([x_base+sign_x*len_line_angular, x_base], [y_base, y_base],
c=color, ls='-',lw=lw, solid_capstyle='butt')
if show_ruler_label:
txt = ax.annotate(string, xy=(x_base, y_text), xycoords="data", xytext=(0,0),
color=color, textcoords="offset points", ha=ha, va=va,
fontsize=fontsize)
if (text_path_effects is not None) & show_ruler_label:
txt.set_path_effects(text_path_effects)
return ax
def _plot_crosshair(ax, ra, dec, wcs, pixscale, sep_arcsec=0.5, len_arcsec=0.5,
color='white', lw=1, npix=None):
####################################
# Show crosshairs: at top, right
if npix is None:
raise ValueError
xlim = ax.get_xlim()
ylim = ax.get_ylim()
len_crosshair_pix = len_arcsec/pixscale
sep_pix = sep_arcsec/pixscale
x_base, y_base = _pstamp_radec2pix(ra, dec, wcs, loc='center', npix=npix)
sign_x = 1. # right
sign_y = 1. # up
# Check if object center in plot area
cpad = sep_pix + len_crosshair_pix
if ((x_base < xlim[0]-cpad) | (x_base > xlim[1]+cpad) | \
(y_base < ylim[0]-cpad) | (y_base > ylim[1]+cpad)):
pass
else:
ax.plot([x_base, x_base],
[y_base+sign_y*sep_pix, y_base+sign_y*(sep_pix+len_crosshair_pix)],
c=color, ls='-', lw=lw, solid_capstyle='butt')
ax.plot([x_base+sign_x*sep_pix, x_base+sign_x*(sep_pix+len_crosshair_pix)],
[y_base, y_base],
c=color, ls='-', lw=lw, solid_capstyle='butt')
return ax
def _plot_compass_rose(ax, ra, dec, wcs, xpad=0, ypad=0,
len=1., units='arcsec',
fontsize=9.5, color='white',
text_path_effects=None):
if units == 'arcsec':
len_deg = len/3600.
elif units == 'arcmin':
len_deg = len/60.
elif units == 'degrees':
len_deg = len
else:
raise ValueError
text_offsetN = len_deg*0.25
text_offsetEx = len_deg*0.35
text_offsetEy = -len_deg*0.045
corner = _pstamp_radec2pix(ra, dec, wcs, loc=None)
Nend = _pstamp_radec2pix(ra, dec+len_deg, wcs, loc=None)
Eend = _pstamp_radec2pix(ra+len_deg, dec, wcs, loc=None)
Nlend = _pstamp_radec2pix(ra, dec+len_deg+text_offsetN, wcs, loc=None)
Elend = _pstamp_radec2pix(ra+len_deg+text_offsetEx, dec+text_offsetEy,
wcs, loc=None)
for end, lend, lbl in zip([Nend, Eend], [Nlend, Elend], ['N', 'E']):
ax.arrow(corner[0]+xpad, corner[1]+ypad,
end[0]-corner[0], end[1]-corner[1],
color=color, head_width=90., width = 1.)
if lbl == 'N':
va = 'bottom'
ha = 'center'
elif lbl == 'E':
va = 'center'
ha = 'right'
txt = ax.annotate(lbl, [lend[0]+xpad, lend[1]+ypad], va=va, ha=ha,
color=color, fontsize=fontsize, xycoords='data')
if text_path_effects is not None:
txt.set_path_effects(text_path_effects)
return ax
def load_data(fname_img, filt=None, upsample=False):
"""
Load image into a data dictionary.
There is an option to upsample the LW (or downsample the SW)
Parameters
----------
fname_img: str
FITS image filename
filt: str
Name of filter
upsample: bool, optional
Option to upsample the LW filters to match the SW filter pixelscale (if True)
Otherwise, downsample the SW filters to match the LW pixelscale.
Default: False
Returns
-------
data: dict
Dictionary containing the image, header, WCS, and filter.
"""
if filt is None:
raise ValueError("Must pass filter name with keyword arguement 'filt'!")
data = {}
# print("reading img: {}".format(fname_img))
# get images
hdu = fits.open(fname_img, memmap=True)
img = copy.deepcopy(hdu[0].data)
hdr = copy.deepcopy(hdu[0].header)
wcs = WCS(copy.deepcopy(hdr))
# Use PHOTFNU value to convert to Jy
# then multiply by 10^9 to get nJy
pfnu_filt_nJy = hdr['PHOTFNU'] * 1.e9
img *= pfnu_filt_nJy
pixscale = _get_pixscale(hdr)
if not upsample:
# Rebin to 0.04" platescale:
if int(np.ceil(pixscale*1000.)) == 20:
img_orig = copy.deepcopy(img)
# Preserve flux: just sum within rebinned pixels
img = _rebin(img_orig, (int(img_orig.shape[0]/2),
int(img_orig.shape[1]/2)))
hdr_orig = copy.deepcopy(hdr)
# Modify header:
for num in [1,2]:
hdr['NAXIS{}'.format(num)] = img.shape[0]
hdr['CRPIX{}'.format(num)] = (hdr_orig['CRPIX{}'.format(num)]+0.5)/2.
hdr['CD{}_{}'.format(num,num)] = hdr_orig['CD{}_{}'.format(num,num)]*2.
wcs = WCS(copy.deepcopy(hdr))
else:
# Upsample to 0.02 platescale:
if int(np.ceil(pixscale*1000.)) == 40:
# print("Upsampling!")
img_orig = copy.deepcopy(img)
hdr_orig = copy.deepcopy(hdr)
# Just block upsample values:
img = np.zeros((int(img_orig.shape[0]*2),
int(img_orig.shape[1]*2)))
for offsety in [0,1]:
for offsetx in [0,1]:
# Preserve flux: downscale
img[offsety:img.shape[0]-1+offsety:2,
offsetx:img.shape[1]-1+offsetx:2] = img_orig *0.25
# Modify header:
for num in [1,2]:
hdr['NAXIS{}'.format(num)] = img.shape[0]
hdr['CRPIX{}'.format(num)] = hdr_orig['CRPIX{}'.format(num)]*2. - 0.5
hdr['CD{}_{}'.format(num,num)] = hdr_orig['CD{}_{}'.format(num,num)]/2.
wcs = WCS(copy.deepcopy(hdr))
data['IMG'] = img
data['WCS'] = wcs
data['HDR'] = hdr
data['filt'] = filt
# Garbage collect:
img = None
wcs = None
hdr = None
hdu.close()
hdu = None
return data
def make_dataRGB(data_all=None, filters=None, colorprefix=['b','g','r']):
"""
Take an input dict of dicts and create summed images for R/G/B.
Parameters
----------
data_all: dict
Dictionary, containing dictionaries of data for multiple filters.
Each sub-dictionary (named with the filter name) -- as returned by load_data() --
has the following structure (eg, for 'F444W'):
data_all['F444W'] = {'IMG': <image>, 'WCS': <WCS>, 'HDR': <HDR>, 'filt': 'F444W'}
filters: array-like
Array (of length 3) holding the name of the filters for R/G/B.
For summing multiple filters, the name should
be a concatenation of the individual filter names, separated by '+'
eg, 'F356W+F410M+F444W'
colorprefix: array-like, optional
Array (of length 3) holding the order of how the filters array entries map to R/G/B.
By default, the ordering is Blue - Green - Red (short to long wavelength)
Default: ['b','g','r']
Returns
-------
data: dict
Dictionary containing the composite R/G/B images, headers, WCSs, and filters
"""
dataRGB = {}
for colprefix, filtsum in zip(colorprefix, filters):
filtlist = filtsum.split('+')
imsum = np.zeros(data_all[filtlist[-1]]['IMG'].shape)
for filt in filtlist:
imsum += data_all[filt]['IMG']
dataRGB['{}IMG'.format(colprefix)] = imsum
dataRGB['{}WCS'.format(colprefix)] = data_all[filtlist[-1]]['WCS']
dataRGB['{}HDR'.format(colprefix)] = data_all[filtlist[-1]]['HDR']
dataRGB['{}filt'.format(colprefix)] = filtsum
return dataRGB
def plot_full_mosaic_RGB(fileout=None, data=None,
dict_fieldopts=None,
imscale_type='log',
minimum=0, maximum=None, scalea=1500,
Q=8, stretch=2,
show_filters=True,
len_ruler_arcmin=1., show_wedge_outline=False,
plot_scale_inches=7., dpi=300):
"""
Script to plot UNCOVER NIRCAM & NIRISS mosaics, with settings about positioning / cropping
passed as part of a dict.
Parameters
----------
fileout: str or None
If specified, save figure to this file. If None, the figure will instead be displayed.
Default: None
data: dict
Dictionary containing information for the R, G, B images, as made by make_dataRGB.
For each of prefix = ['b', 'g', 'r'],
it contains 'IMG', 'WCS', 'HDR', 'filt' (prepended by the prefix), where
IMG contains the 2D image (or composite image), WCS is a astropy.wcs.WCS instance created from the
image HDR, and filt is the filter (or composite filter name)
dict_fieldopts: dict
Dictionary containing options for making the mosaic image.
For example, see _get_dict_NIRCAM_primary(shape), _get_dict_NIRISS_primary(shape),
imscale_type: str, optional
Type of scaling to apply to image. Options: 'log', 'linear', 'asinh'. Default: 'log'
minimum: float or array-like or None, optional
Minimum value for image scaling.
If float, applied to all 3 images (R/G/B).
If array-like, must have length 3, with the ordering [r_min, g_min, b_min].
If None, uses the minimum of each image.
Default: 0.
maximum: float or array-like or None, optional
Maximum value for image scaling (only used for 'linear' or 'log').
If float, applied to all 3 images (R/G/B).
If array-like, must have length 3, with the ordering [r_max, g_max, b_max].
If None, uses the maximum of each image.
Default: None
scalea: float, optional
Scale factor for 'log' scaling, following the DS9 scaling convention.
See http://ds9.si.edu/doc/ref/how.html
Default: 1500.
Q: float, optional
Factor for asinh scaling, using make_lupton_rgb. Default: 8.
stretch: float, optional
Factor for asinh stretch, using make_lupton_rgb. Default: 2.
show_filters: bool, optional
Option to show the color label with the filters. Default: True
len_ruler_arcmin: float, optional
Length of the scale bar, in arcmin. Default: 1.
show_wedge_outline: bool, optional
Option to show an outline around the inset wedges.
Can be helpful for iterating on wedge enpoint positioning. Default: False
plot_scale_inches: float, optional
Height of the resulting figure. Default: 7.
dpi: float, optional.
Resolution for the saved figure.
Excessively high dpi for very large mosaics can result in matplotlib problems.
Default: 300.
"""
if data is None:
errmsg = " Must pass 'data', \n"
errmsg += " a dict containing the loaded images RGB filter images! "
raise ValueError(errmsg)
if dict_fieldopts is None:
errmsg = " Must pass 'dict_fieldopts', \n"
errmsg += " a dict containing the settings for making a mosaic! "
raise ValueError(errmsg)
# Unpack plotting options per-field:
pad_factor = dict_fieldopts.get('pad_factor', 0.)
w_logo = dict_fieldopts.get('w_logo', 0.2)
loc_logo = dict_fieldopts.get('loc_logo', [0.,0.])
filters_fontsize = dict_fieldopts.get('filters_fontsize', 10)
annotate_field_label = dict_fieldopts.get('annotate_field_label', 'Field')
annotate_field_loc = dict_fieldopts.get('annotate_field_loc', [0.95,0.05])
annotate_field_ha = dict_fieldopts.get('annotate_field_ha', 'right')
annotate_field_va = dict_fieldopts.get('annotate_field_va', 'bottom')
annotate_field_fontsize = dict_fieldopts.get('annotate_field_fontsize', 14)
annotate_field_fontweight = dict_fieldopts.get('annotate_field_fontweight', 'normal')
ruler_loc = dict_fieldopts.get('ruler_loc', [0.1,0.1])
ruler_vertical = dict_fieldopts.get('ruler_vertical', False)
filterlabel_loc = dict_fieldopts.get('filterlabel_loc', 'center left')
filterlabel_bboxtoanchor = dict_fieldopts.get('filterlabel_bboxtoanchor', [0.05,0.95])
clu_dicts = dict_fieldopts.get('clu_dicts', None)
offset_factor = 0.5*pad_factor
nrows = ncols = 1
scale = plot_scale_inches
f, ax = plt.subplots(nrows, ncols)
f.subplots_adjust(top=1.0, bottom=0, right=1.0, left=0, hspace=0, wspace=0)
# Get npix from arcsec size:
pixscale = _get_pixscale(data['rHDR'])
if 'crop_x_pix' in dict_fieldopts.keys():
xpx = dict_fieldopts['crop_x_pix']
xoff_crop = xpx[0]
else:
xpx = [0,data['rIMG'].shape[1]]
xoff_crop = 0
if 'crop_y_pix' in dict_fieldopts.keys():
ypx = dict_fieldopts['crop_y_pix']
yoff_crop = ypx[0]
else:
ypx = [0,data['rIMG'].shape[0]]
yoff_crop = 0
if imscale_type == 'asinh':
rgb = make_lupton_rgb(data['rIMG'][ypx[0]:ypx[1],xpx[0]:xpx[1]],
data['gIMG'][ypx[0]:ypx[1],xpx[0]:xpx[1]],
data['bIMG'][ypx[0]:ypx[1],xpx[0]:xpx[1]],
Q=Q, stretch=stretch, minimum=minimum)
elif imscale_type == 'linear':
rgb = _make_linear_rgb(data['rIMG'][ypx[0]:ypx[1],xpx[0]:xpx[1]],
data['gIMG'][ypx[0]:ypx[1],xpx[0]:xpx[1]],
data['bIMG'][ypx[0]:ypx[1],xpx[0]:xpx[1]],
minimum=minimum, maximum=maximum)
elif imscale_type == 'log':
rgb = _make_log_rgb(data['rIMG'][ypx[0]:ypx[1],xpx[0]:xpx[1]],
data['gIMG'][ypx[0]:ypx[1],xpx[0]:xpx[1]],
data['bIMG'][ypx[0]:ypx[1],xpx[0]:xpx[1]],
minimum=minimum, maximum=maximum, scalea=scalea)
else:
raise ValueError("scale imscale_type='{}' unknown!'".format(imscale_type))
# Embed rgb in a BIGGER array:
xoff_bigarr = int(np.ceil(rgb.shape[1]*offset_factor))
xpad = int(np.ceil(rgb.shape[1]*pad_factor))
rgb_big = np.zeros((rgb.shape[0], rgb.shape[1]+xpad, rgb.shape[2]))
rgb_big[:, xoff_bigarr:rgb.shape[1]+xoff_bigarr,:] = rgb[:,:,:]
aspect_ratio = rgb_big.shape[1]*1./rgb_big.shape[0]
# Do a round about way to ensure no rounding offsets
# leading to missing pixels
npixtmp = scale*(aspect_ratio)*dpi
npixtmp_int = int(np.ceil(npixtmp))
aspect_ratio *= npixtmp_int/npixtmp
f.set_size_inches(ncols*scale*(aspect_ratio),nrows*scale)
# Free up memory:
rgb = None
ax.imshow(rgb_big, origin='lower', interpolation='none')