-
Notifications
You must be signed in to change notification settings - Fork 3
/
depth-comparison.py
1098 lines (912 loc) · 34.9 KB
/
depth-comparison.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
from __future__ import print_function
import os
import sys
import datetime
from astrometry.util.fits import *
from astrometry.util.plotutils import *
from astrometry.util.starutil_numpy import mjdtodate
import pylab as plt
import numpy as np
from legacypipe.survey import *
import tractor
#target_mjd = 57445.5
#mjd_diff = 0.5
#mjd_diff = 7
ps = PlotSequence('bot')
#decam = False
decam = True
mzls = not decam
if decam:
from decam import DecamNominalCalibration
nom = DecamNominalCalibration()
else:
from mosaic import MosaicNominalCalibration
nom = MosaicNominalCalibration()
botfn = 'bot-matched.fits'
if not os.path.exists(botfn):
obsfn = 'obsbot.fits'
if not os.path.exists(obsfn):
cmd = 'python copilot.py --fits %s' % obsfn
print('Running:', cmd)
os.system(cmd)
bot = fits_table(obsfn)
date = np.array(map(mjdtodate, bot.mjd_obs))
# plt.clf()
# for i,band in enumerate('grz'):
# I = np.flatnonzero(np.array([b.strip() == band for b in bot.band]))
# plt.plot(date[I], np.zeros(len(I))+i, '.', color=dict(z='m').get(band,band))
# plt.xlabel('Date')
# plt.ylabel('band')
# xt,xl = plt.xticks()
# print('Ticks', xt)
# print('Labels', xl)
# plt.xticks(xt, ['']*len(xt), rotation=90)
# ml,mh = np.min(bot.mjd_obs[bot.mjd_obs > 0])-10, np.max(bot.mjd_obs)+10
# xl,xh = mjdtodate(ml), mjdtodate(mh)
# plt.xlim(xl,xh)
# plt.twiny()
# plt.xlim(ml,mh)
# plt.xlabel('MJD')
# plt.ylim(-0.5, 2.5)
# ps.savefig()
bot.cut(bot.mjd_obs != 0)
#bot.cut(np.abs(bot.mjd_obs - target_mjd) < mjd_diff)
#print(len(bot), 'images for MJD')
bot.cut(np.argsort(bot.mjd_obs))
# for x in zip(bot.mjd_obs, bot.filename, bot.expnum)[:20]:
# print(x)
botccds = set(zip(bot.expnum, bot.extension))
print(len(botccds), 'unique CCDs in copilot database')
survey = LegacySurveyData()
#ccds = survey.get_ccds_readonly()
ccds = survey.get_annotated_ccds()
print('Annotated CCDs:', len(ccds))
# HACK
#ccds.cut(np.abs(ccds.mjd_obs - target_mjd) < mjd_diff)
#print('Cut to', len(ccds), 'CCDs near target MJD')
ccds.cut(ccds.ccdzpt < 99.)
print('With good zeropoints:', len(ccds))
if mzls:
# Raw MzLS images are per-AMP, not per-CCD, so the names don't
# match up ("im4" vs "ccd1"). Instead just map by expnum, and
# record all matches
ccdmap = {}
for i,expnum in enumerate(ccds.expnum):
try:
ccdmap[expnum].append(i)
except KeyError:
ccdmap[expnum] = [i]
else:
ccdmap = dict([((expnum,ext.strip()),i) for i,(expnum,ext) in enumerate(zip(ccds.expnum, ccds.ccdname))])
print('Annotated CCDs map:', ccdmap.items()[:10], '...')
imatched = []
jmatched = []
alljmatched = []
for i,(expnum,ccdname,obstype) in enumerate(
zip(bot.expnum, bot.extension, bot.obstype)):
ccdname = ccdname.strip()
obstype = obstype.strip()
if obstype in ['zero', 'dome flat', 'focus', 'dark']:
continue
iccd = None
try:
if mzls:
iccds = ccdmap[expnum]
alljmatched.append(iccds)
# Arbitrarily keep just the first one...
iccd = iccds[0]
else:
iccd = ccdmap[(expnum, ccdname)]
alljmatched.append(iccd)
except KeyError:
# Check for mistaken expnums...
tryexpnum = None
if expnum > 3000000 and 'mos%i.fits' % expnum in bot.filename[i]:
# mos3101289 -- actually expnum 101289.
tryexpnum = expnum - 3000000
elif expnum > 300000 and expnum < 400000 and 'mos%i.fits' % expnum in bot.filename[i]:
# mos399505 -- actually expnum 99505
tryexpnum = expnum - 300000
if tryexpnum is not None:
try:
iccds = ccdmap[tryexpnum]
alljmatched.append(iccds)
iccd = iccds[0]
print('Tried', tryexpnum, 'rather than', expnum, 'and found a match!')
except KeyError:
print('Also tried expnum', tryexpnum, 'but no luck')
if iccd is None:
print('Did not match bot entry: expnum', expnum, 'ccdname', ccdname, 'obstype "%s", object "%s"' % (obstype, bot.object[i].strip()))
print(' filename', bot.filename[i].strip())
continue
imatched.append(i)
jmatched.append(iccd)
jmatched = np.array(jmatched)
matched = ccds[jmatched]
alljmatched = np.hstack(alljmatched)
unmatched = np.ones(len(ccds), bool)
unmatched[alljmatched] = False
unmatched = np.flatnonzero(unmatched)
unmatched = ccds[unmatched]
print(len(unmatched), 'from CCDs file unmatched')
unmatched.writeto('ccds-unmatched.fits')
print('Found', len(matched), 'CCDs in survey CCDs file')
imatched = np.array(imatched)
print('Matched to', len(imatched), 'from bot file')
bot.cut(imatched)
bot.add_columns_from(matched)
bot.writeto(botfn)
else:
bot = fits_table(botfn)
print('Filters:', np.unique(bot.band))
print('Exptimes:', np.unique(bot.exptime))
print('Transparencies:', np.unique(bot.transparency)[:10])
bot.cut(bot.exptime > 30.)
# HACK
bot.cut(bot.transparency > 0.5)
print('CUT to', len(bot), 'images')
allbot = bot
# FROM OBSBOT:
r_half = 0.45 #arcsec
pixsc = 0.262 # pixscale
def Neff(seeing):
# magic 2.35: convert seeing FWHM into sigmas in arcsec.
return (4. * np.pi * (seeing / 2.35)**2 +
8.91 * r_half**2 +
pixsc**2/12.)
def Neff2(seeing):
# magic 2.35: convert seeing FWHM into sigmas in arcsec.
pfact = 1.15
return ((4. * np.pi * (seeing / 2.35)**2)**(1./pfact) +
(8.91 * r_half**2)**(1./pfact)) ** pfact
# Compute galaxy norm for a 1.3" Gaussian PSF, given FWHM seeing in arcsec
# and pixel scale in arcsec/pixel.
def get_galnorm(seeing, pixsc):
from tractor.patch import ModelMask
S = 32
W,H = S*2+1, S*2+1
psf_sigma = seeing / 2.35 / pixsc
tim = tractor.Image(data=np.zeros((H,W)), inverr=np.ones((H,W)),
psf=tractor.NCircularGaussianPSF([psf_sigma], [1.]),
wcs=tractor.NullWCS(pixscale=pixsc))
gal = SimpleGalaxy(tractor.PixPos(S,S), tractor.Flux(1.))
mm = ModelMask(0, 0, W, H)
galmod = gal.getModelPatch(tim, modelMask=mm).patch
galmod = np.maximum(0, galmod)
galmod /= galmod.sum()
return np.sqrt(np.sum(galmod**2))
pixsc = nom.pixscale
fidseeing = 1.3
if False:
# Does averaging multiple CCDs per exposure reduce the scatter?
for band in np.unique(bot.band):
band = band.strip()
bot = allbot[np.array([b.strip() == band for b in allbot.band])]
print(len(bot), 'in', band, 'band')
from collections import Counter
c = Counter(bot.expnum)
keep_expnum = set()
for k,v in c.most_common():
print('Exposure number', k, 'appears', v, 'times')
if v <= 1:
break
keep_expnum.add(k)
bot.cut(np.nonzero([expnum in keep_expnum
for expnum in bot.expnum])[0])
print('Cut to', len(bot), 'with > 1 CCD per exposure')
bad = np.flatnonzero((bot.photometric == False))
print(len(bad), 'rows are non-photometric')
tt = 'Obsbot vs Pipeline depth: %s band' % band
fid = nom.fiducial_exptime(band)
extinction = bot.ebv * fid.A_co
plt.clf()
notbad = np.flatnonzero((bot.photometric == True))
equivtime = (bot.exptime / bot.expfactor)
extdepth = bot.galdepth - extinction
# 2-coverage target (90% fill)
#target_depth = dict(g=24.0, r=23.4, z=22.5)[band]
# -> 1-coverage depth (- ~0.37 mag)
#target_depth -= 2.5*np.log10(np.sqrt(2.))
target_depth = fid.single_exposure_depth
p1 = plt.plot(equivtime[notbad], extdepth[notbad], 'b.', alpha=0.5)
(xl,xh,yl,yh) = plt.axis()
xl = 10.
meantime = []
meandepth = []
for expnum in keep_expnum:
I = np.flatnonzero(bot.expnum == expnum)
print(len(I), 'CCDs for expnum', expnum)
print('Exp factors:', bot.expfactor[I])
meanf = np.mean(bot.expfactor[I])
i = I[0]
meantime.append(bot.exptime[i] / meanf)
meandepth.append(np.mean(bot.galdepth[I] - extinction[I]))
# plot fit line (x = f(y), just to confuse you...)
# 1 mag = factor of 6.3 in exposure time
slope = (10.**(1./2.5))**2
diff = np.median(np.log(equivtime[notbad]) / np.log(slope) -
extdepth[notbad])
yy = np.linspace(20, 25, 100)
xx = slope**(yy + diff)
plt.plot(xx, yy, 'k-', alpha=0.3)
p2 = plt.plot(xx, yy+0.05, 'k--', alpha=0.3)
plt.plot(xx, yy-0.05, 'k--', alpha=0.3)
plt.ylabel('Pipeline galdepth (unextincted) (mag)')
plt.xlabel('Bot-predicted equivalent exposure time (s)')
plt.axhline(target_depth, color='b', alpha=0.3)
plt.axvline(fid.exptime, color='b', alpha=0.3)
plt.xscale('log')
xt = [10, 20, 50, 100, 200, 300, 400, 500]
plt.xticks(xt, ['%i'%t for t in xt])
plt.axis([xl,xh,yl,yh])
plt.title(tt)
ps.savefig()
plt.clf()
plt.plot(meantime, meandepth, 'bo')
plt.plot(xx, yy, 'k-', alpha=0.3)
p2 = plt.plot(xx, yy+0.05, 'k--', alpha=0.3)
plt.plot(xx, yy-0.05, 'k--', alpha=0.3)
plt.ylabel('Pipeline galdepth (unextincted) (mag)')
plt.xlabel('Bot-predicted equivalent exposure time (s)')
plt.axhline(target_depth, color='b', alpha=0.3)
plt.axvline(fid.exptime, color='b', alpha=0.3)
plt.xscale('log')
xt = [10, 20, 50, 100, 200, 300, 400, 500]
plt.xticks(xt, ['%i'%t for t in xt])
plt.axis([xl,xh,yl,yh])
plt.title(tt)
ps.savefig()
# for expnum,mnt,mnd in zip(keep_expnum, meantime, meandepth):
# I = np.flatnonzero(bot.expnum == expnum)
# plt.plot([mnt + np.zeros(len(I)), equivtime[I]],
# [mnd + np.zeros(len(I)), extdepth[I]], 'b.-')
#
# plt.axis([xl,xh,yl,yh])
# ps.savefig()
sys.exit(0)
bot = allbot
gauss_galnorm_nom = get_galnorm(fidseeing, pixsc)
# correct for ~17% difference between Gaussian galnorm and real PSF
galnorm_nom = gauss_galnorm_nom / 1.17
galneff_nom = Neff(fidseeing)
see = np.arange(0.7, 2.01, 0.1)
gauss_galnorms = np.array([get_galnorm(s, pixsc) for s in see])
galneffs = Neff(see)
galneff2_nom = Neff2(fidseeing)
galneffs2 = Neff2(see)
plt.clf()
p1 = plt.plot(see, (1. / gauss_galnorms**2) / (1. / gauss_galnorm_nom**2),
'bo-')
p2 = plt.plot(see, galneffs / galneff_nom, 'g--')
p2 = plt.plot(see, galneffs2 / galneff2_nom, 'go-')
plt.legend((p1[0],p2[0]),('Galnorm (Gaussian PSF)', 'Gal Neff'), loc='upper left')
plt.xlabel('Seeing (arcsec)')
plt.ylabel('Exposure factor')
ps.savefig()
# HACK
bot.band = np.array([b[0] for b in bot.band])
botbands = np.unique(bot.band)
print('Bot bands:', botbands)
for band in botbands:
band = band.strip()
bot = allbot[np.array([b.strip() == band for b in allbot.band])]
print(len(bot), 'in', band, 'band')
bad = np.flatnonzero((bot.photometric == False))
print(len(bad), 'rows are non-photometric')
notbad = np.flatnonzero(bot.photometric)
tt = 'Obsbot vs Pipeline depth: %s band' % band
fid = nom.fiducial_exptime(band)
extinction = bot.ebv * fid.A_co
zp0 = nom.zeropoint(band)
psfnorm_seeing = bot.pixscale_mean * 2.35 * (1. / (bot.psfnorm_mean * 2. * np.sqrt(np.pi)))
factor = np.median(bot.seeing / psfnorm_seeing)
xx = np.array([0, 5])
plt.clf()
plt.plot(psfnorm_seeing, bot.seeing, 'b.')
plt.plot(psfnorm_seeing[bad], bot.seeing[bad], 'r.')
plt.xlabel('Pipeline PSF norm -> seeing')
plt.ylabel('Bot seeing')
ax = plt.axis()
plt.plot(xx, xx*factor, 'k-', alpha=0.3)
p2 = plt.plot(xx, xx*factor*0.95, 'k--', alpha=0.3)
plt.plot(xx, xx*factor*1.05, 'k--', alpha=0.3)
plt.axis(ax)
plt.title(tt)
plt.legend([p2[0]], ['+- 5%'], loc='lower right')
ps.savefig()
# galnorm_seeing = bot.pixscale_mean * 2.35 * 1. / (bot.galnorm_mean * 2. * np.sqrt(np.pi))
#
# A = np.zeros((len(bot),2))
# A[:,0] = 1.
# A[:,1] = galnorm_seeing
# b = np.linalg.lstsq(A, bot.seeing)[0]
# print('Lstsq:', b)
# offset = b[0]
# slope = b[1]
# xx = np.array([0, 5])
#
# plt.clf()
# #plt.plot(bot.galnorm_mean, bot.seeing, 'b.')
# plt.plot(galnorm_seeing, bot.seeing, 'b.')
# plt.xlabel('Pipeline galaxy norm -> seeing')
# plt.ylabel('Bot seeing')
# ax = plt.axis()
# p = plt.plot(xx, offset + xx*slope, 'k-', alpha=0.3)
# plt.plot(xx, offset + xx*slope * 0.9, 'k--', alpha=0.3)
# plt.plot(xx, offset + xx*slope * 1.1, 'k--', alpha=0.3)
# plt.legend([p[0]], ['offset %0.2f, slope %0.3f' % (offset, slope)],
# loc='lower right')
# plt.axis(ax)
# plt.title(tt)
# ps.savefig()
galneff = Neff(bot.seeing)
plotx = 1. / (bot.galnorm_mean)**2 * pixsc**2
factor = np.median(galneff / plotx)
xx = np.array([0, 1000])
plt.clf()
plt.plot(plotx, galneff, 'b.')
plt.plot(plotx[bad], galneff[bad], 'r.')
plt.xlabel('Pipeline Neff = 1 / galaxy norm^2 (arcsec^2)')
plt.ylabel('Bot galaxy Neff (arcsec^2)')
plt.title(tt)
ax = plt.axis()
p1 = plt.plot(xx, xx*factor, 'k-', alpha=0.3)
plt.plot(xx, xx*factor*0.9, 'k--', alpha=0.3)
p2 = plt.plot(xx, xx*factor*1.1, 'k--', alpha=0.3)
plt.plot(xx, xx, 'r-', alpha=0.3)
plt.axhline(galneff_nom, color='b', alpha=0.3)
plt.axis(ax)
plt.legend([p1[0], p2[0]], ['slope %0.3f' % (factor), '+- 10%'],
loc='lower right')
ps.savefig()
# What does the Neff imply about depth factor?
galnorm_factor = (1. / bot.galnorm_mean**2) / (1. / galnorm_nom**2)
print('Nominal galnorm: Gaussian', gauss_galnorm_nom, 'corrected', galnorm_nom)
print('Median galnorm:', np.median(bot.galnorm_mean))
galneff_factor = galneff / galneff_nom
print('Nominal Neff:', galneff_nom)
print('Median Neff:', np.median(galneff))
Iok = np.flatnonzero((bot.galdepth > 0) * np.isfinite(bot.galdepth) *
(bot.gaussgaldepth > 0) * np.isfinite(bot.gaussgaldepth))
diff = np.median(bot.galdepth[Iok] - bot.gaussgaldepth[Iok])
print('Median difference between galdepth and Gaussgaldepth:', diff)
# plt.clf()
# plt.plot(bot.gaussgaldepth, bot.galdepth, 'b.')
# plt.xlabel('Gaussian galdepth')
# plt.ylabel('Galdepth')
# ax = plt.axis()
# plt.plot(xx, xx, 'r-', alpha=0.3)
# plt.plot(xx, xx+diff, 'k-', alpha=0.3)
# plt.axis(ax)
# plt.title(tt)
# ps.savefig()
# Duh, Gaussian Galnorm is just a factor ~ 17% larger than Galnorm.
# galnorm_x = 1. / 10.**((bot.galdepth / -2.5) + 9)
# gaussgalnorm_x = 1. / 10.**((bot.gaussgaldepth / -2.5) + 9)
# factor = 10. ** (0.17 / 2.5)
# plt.clf()
# plt.plot(galnorm_x, gaussgalnorm_x, 'b.')
# plt.xlabel('galnorm')
# plt.ylabel('Gauss galnorm')
# plt.title(tt)
# ax = plt.axis()
# plt.plot(xx, xx, 'r-', alpha=0.3)
# plt.plot(xx, xx*factor, 'b-', alpha=0.3)
# plt.axis(ax)
# ps.savefig()
factor = np.median(galneff_factor / galnorm_factor)
xx = np.array([0, 10])
scatter = np.std(galneff_factor / (galnorm_factor * factor))
print('PSF Scatter:', scatter)
plt.clf()
p1 = plt.plot(galnorm_factor, galneff_factor, 'b.')
plt.plot(galnorm_factor[bad], galneff_factor[bad], 'r.')
plt.xlabel('Pipeline exposure factor from galnorm')
plt.ylabel('Bot exposure factor from Neff')
plt.title(tt)
ax = plt.axis()
p = plt.plot(xx, xx*factor, 'k-', alpha=0.3)
p2 = plt.plot(xx, xx*factor*0.9, 'k--', alpha=0.3)
plt.plot(xx, xx*factor*1.1, 'k--', alpha=0.3)
plt.plot(xx, xx, 'r-', alpha=0.3)
plt.legend([p[0],p2[0],p1[0]], ['slope %0.3f' % (factor), '+- 10%',
'scatter %.1f %%' % (100.*scatter)],
loc='lower right')
plt.axis(ax)
ps.savefig()
if mzls:
bot.avsky *= bot.exptime
skyflux = 10.**((bot.sky - zp0) / -2.5) * bot.exptime * pixsc**2
# What is this?
##okvals = notbad[np.flatnonzero((bot.avsky < skyflux)[notbad])]
okvals = notbad
xx = np.array([0, 30000])
A = np.zeros((len(okvals),2))
A[:,0] = 1.
A[:,1] = bot.avsky[okvals]
b = np.linalg.lstsq(A, skyflux[okvals])[0]
print('Lstsq:', b)
offset = b[0]
slope = b[1]
# plt.clf()
# plt.plot(bot.exptime, bot.avsky, 'b.')
# plt.xlabel('Exptime (s)')
# plt.ylabel('CP avsky')
# ps.savefig()
#
# plt.clf()
# plt.plot(bot.exptime, skyflux, 'b.')
# plt.xlabel('Exptime (s)')
# plt.ylabel('Bot sky flux')
# ps.savefig()
out = np.flatnonzero(bot.avsky > skyflux)
print('AVSKY > SKYFLUX: expnums', bot.expnum[out])
plt.clf()
plt.plot(bot.avsky, skyflux, 'b.')
plt.plot(bot.avsky[bad], skyflux[bad], 'r.')
plt.plot(bot.avsky[out], skyflux[out], 'g.')
plt.xlabel('CP avsky')
plt.ylabel('Bot sky flux')
ax = plt.axis()
p = plt.plot(xx, offset + xx*slope, 'k-', alpha=0.3)
p2 = plt.plot(xx, offset + xx*slope*0.9, 'k--', alpha=0.3)
plt.plot(xx, offset + xx*slope*1.1, 'k--', alpha=0.3)
plt.axis(ax)
plt.title(tt)
plt.legend([p[0],p2[0]], ['offset %0.2f, slope %0.3f' % (offset, slope),
'+- 10%'],
loc='lower right')
ps.savefig()
udates = np.unique(bot.date_obs)
print('Unique dates:', udates)
ccdskymags = []
skys = []
umjds = []
slopes = []
gains = []
bot_zpts = []
zpts = []
ccdzpts = []
bot_trans = []
ccdtrans = []
for idate,d in enumerate(udates):
I = np.flatnonzero(bot.date_obs == d)
A = np.zeros((len(I),2))
A[:,0] = 1.
A[:,1] = bot.avsky[I]
b = np.linalg.lstsq(A, skyflux[I])[0]
offset = b[0]
slope = b[1]
slopes.append(slope)
gains.append(np.median(bot.arawgain[I]))
ccdskymags.append(np.median(bot.ccdskymag[I]))
skys.append(np.median(bot.sky[I]))
umjds.append(np.median(bot.mjd_obs[I]))
bot_zpts.append(np.median(bot.zeropoint[I]))
zpts.append(np.median(bot.zpt[I]))
ccdzpts.append(np.median(bot.ccdzpt[I]))
bot_trans.append(np.median(bot.transparency[I]))
ccdtrans.append(np.median(bot.ccdtransp[I]))
if idate >= 1:
continue
plt.clf()
plt.plot(bot.avsky[I], skyflux[I], 'b.')
plt.xlabel('CP avsky')
plt.ylabel('Bot sky flux')
plt.title('Date: ' + d)
ax = plt.axis()
p = plt.plot(xx, offset + xx*slope, 'k-', alpha=0.3)
p2 = plt.plot(xx, offset + xx*slope*0.9, 'k--', alpha=0.3)
plt.plot(xx, offset + xx*slope*1.1, 'k--', alpha=0.3)
plt.axis(ax)
plt.legend([p[0],p2[0]], ['offset %0.2f, slope %0.3f' % (offset, slope),
'+- 10%'],
loc='lower right')
ps.savefig()
plt.clf()
plt.subplots_adjust(bottom=0.25)
dd = [datetime.date(*[int(w) for w in d.split('-')])
for d in udates]
plt.plot(dd, slopes, 'b.')
plt.plot(dd, gains, 'r.')
plt.xticks(rotation=90)
plt.xlabel('Obs date')
plt.ylabel('Slope')
ps.savefig()
alldd = [datetime.date(*[int(w) for w in d.split('-')])
for d in bot.date_obs]
plt.clf()
plt.plot(dd, bot_zpts, 'b.', label='Bot zeropoint')
plt.plot(dd, ccdzpts, 'gx', label='Pipeline ccdzpt')
plt.plot(dd, zpts, 'r.', label='Pipeline zpt')
#plt.plot(alldd, bot.ccdzpt, 'r.', alpha=0.1)
plt.xticks(rotation=90)
plt.legend()
plt.axhline(26.2, color='k', lw=2, alpha=0.5)
#plt.axhline(26.2+0.6, color='k', lw=2, alpha=0.5, ls='--')
#plt.axhline(26.2-0.6, color='k', lw=2, alpha=0.5, ls='--')
plt.xlabel('Obs date')
plt.ylabel('Zeropoint')
ps.savefig()
plt.clf()
plt.plot(dd, bot_trans, 'b.', label='Bot transparency')
plt.plot(dd, ccdtrans, 'r.', label='Pipeline ccdtransp')
plt.xticks(rotation=90)
plt.legend()
plt.xlabel('Obs date')
plt.ylabel('Transparency')
ps.savefig()
# plt.clf()
# plt.plot(dd, ccdskymags, 'b.', label='ccdskymag')
# plt.plot(dd, skys, 'r.', label='bot sky')
# plt.xticks(rotation=90)
# plt.xlabel('Obs date')
# plt.ylabel('Sky estimate')
# plt.legend()
# ps.savefig()
#
# ccdskymags = np.array(ccdskymags)
# skys = np.array(skys)
# off = np.median(ccdskymags - skys)
#
# plt.clf()
# plt.plot(dd, ccdskymags, 'b.', label='ccdskymag')
# plt.ylim(18,20)
# plt.xticks(rotation=90)
# plt.xlabel('Obs date')
# plt.ylabel('Sky estimate')
# plt.legend()
# ps.savefig()
# plt.clf()
# plt.plot(dd, skys + off, 'r.', label='bot sky')
# plt.ylim(18,20)
# plt.xticks(rotation=90)
# plt.xlabel('Obs date')
# plt.ylabel('Sky estimate')
# plt.legend()
# ps.savefig()
#
#
# #from astrometry.util.starutil_numpy import *
# #umjds = np.array([datetomjd(d) for d in dd])
# umjds = np.array(umjds)
#
# plt.clf()
# plt.plot(umjds % 28.0, ccdskymags, 'b.', label='ccdskymag')
# plt.plot(umjds % 28.0, skys + off, 'r.', label='bot sky')
# plt.ylim(18,20)
# plt.xticks(rotation=90)
# plt.xlabel('Obs date % 28')
# plt.ylabel('Sky estimate')
# plt.legend()
# ps.savefig()
#
#
# plt.clf()
# plt.plot(bot.mjd_obs, bot.avsky / bot.exptime, 'b.')
# plt.ylabel('avsky')
# plt.xlabel('mjd')
# ps.savefig()
#
# plt.clf()
# plt.plot(bot.mjd_obs, bot.sky, 'b.')
# plt.ylabel('sky')
# plt.xlabel('mjd')
# ps.savefig()
#
# plt.clf()
# plt.plot(bot.mjd_obs, bot.ccdskymag, 'b.')
# plt.ylabel('ccdskymag')
# plt.xlabel('mjd')
# ps.savefig()
#
# plt.clf()
# plt.plot(bot.mjd_obs, bot.ccdskycounts, 'b.')
# plt.ylabel('ccdskycounts')
# plt.xlabel('mjd')
# ps.savefig()
#
# plt.clf()
# plt.plot(bot.sky, bot.ccdskymag, 'b.')
# plt.xlabel('sky')
# plt.ylabel('ccdskymag')
# ps.savefig()
#
# plt.clf()
# plt.plot(bot.ccdskycounts, bot.ccdskymag, 'b.')
# plt.xlabel('ccdskycounts')
# plt.ylabel('ccdskymag')
# ps.savefig()
#
# plt.clf()
# plt.plot(bot.mjd_obs, bot.ccdskymag - bot.sky, 'b.')
# plt.xlabel('mjd')
# plt.ylabel('ccdskymag - sky')
# ps.savefig()
# Convert skyflux into a sig1 estimate
# in Poisson process, mean = variance; total sky counts are distributed
# like that.
# ignore gain
skyvar = skyflux
skysig1 = np.sqrt(skyvar)
# use (bot) zeropoint to scale to nanomaggy units
zpscale = NanoMaggies.zeropointToScale(bot.zeropoint)
skysig1 /= zpscale
# extra factor of time from the zeropoint...
skysig1 /= bot.exptime
factor = np.median(skysig1 / bot.sig1)
xx = np.array([0, 1])
plt.clf()
#plt.plot(bot.sig1[notbad], skysig1[notbad], 'b.', alpha=0.25)
plt.scatter(bot.sig1[notbad], skysig1[notbad], c=bot.mjd_obs[notbad], alpha=0.25, edgecolor='none')
ax = plt.axis()
[xmn,xmx,ymn,ymx] = ax
plt.plot(np.clip(bot.sig1[bad], xmn,xmx), np.clip(skysig1[bad], ymn,ymx), 'r.')
plt.axis(ax)
plt.xlabel('Pipeline sig1')
plt.ylabel('Bot sig1 = f(sky, zpt)')
#ax = plt.axis()
p = plt.plot(xx, xx*factor, 'k-', alpha=0.3)
p2 = plt.plot(xx, (xx*factor)*0.95, 'k--', alpha=0.3)
plt.plot(xx, (xx*factor)*1.05, 'k--', alpha=0.3)
plt.plot(xx, xx, 'r-', alpha=0.3)
plt.axis(ax)
plt.legend([p[0],p2[0]], ['slope %0.3f' % (factor), '+- 5%'],
loc='lower right')
plt.title(tt)
ps.savefig()
# plt.clf()
# plt.plot(bot.exptime, bot.sig1, 'b.')
# plt.xlabel('exptime')
# plt.ylabel('sig1')
# plt.ylim(0,np.percentile(bot.sig1, 98))
# ps.savefig()
# sig1 (from CCDs table)
# is in units of nanomaggies
zpscale = NanoMaggies.zeropointToScale(bot.zeropoint)
expfactor_sig1 = (bot.sig1 * zpscale)**2
expfactor_sig1 *= bot.exptime / fid.exptime
print('Median expfactor_sig1:', np.median(expfactor_sig1))
#expfactor_sig1 /= np.median(expfactor_sig1)
print('Bot.sky:', bot.sky)
# Sky estimate (from bot)
# This is the expfactor scaling
expfactor_sky = 10.**(-0.4 * (bot.sky - fid.skybright))
factor = np.median(expfactor_sky / expfactor_sig1)
scatter = np.std(expfactor_sky / (expfactor_sig1 * factor))
print('Sky Scatter:', scatter)
plt.clf()
#p1 = plt.plot(expfactor_sig1[notbad], expfactor_sky[notbad], 'b.')
plt.scatter(expfactor_sig1[notbad], expfactor_sky[notbad],
c=bot.mjd_obs[notbad], edgecolor='none', alpha=0.25,
label='scatter %.1f %%' % (100.*scatter))
ax = plt.axis()
[xmn,xmx,ymn,ymx] = ax
plt.plot(np.clip(expfactor_sig1[bad], xmn,xmx),
np.clip(expfactor_sky[bad], ymn,ymx), 'r.')
plt.axis(ax)
plt.xlabel('Pipeline exposure factor from sig1 (and zpt; arb. scale)')
plt.ylabel('Bot exposure factor from sky')
plt.title(tt)
ax = plt.axis()
xx = np.array([0, 10])
p = plt.plot(xx, xx*factor, 'k-', alpha=0.3)
p2 = plt.plot(xx, (xx*factor)*0.9, 'k--', alpha=0.3,
label='+- 10%')
plt.plot(xx, (xx*factor)*1.1, 'k--', alpha=0.3)
plt.legend(loc='lower right')
plt.axis(ax)
ps.savefig()
diff = np.median(bot.zeropoint - bot.ccdzpt)
xx = np.array([20,30])
# plt.clf()
# plt.plot(bot.exptime, bot.ccdzpt, 'b.')
# plt.xlabel('Exptime (s)')
# plt.ylabel('Pipeline zeropoint')
# ps.savefig()
# plt.clf()
# plt.plot(bot.exptime, bot.zeropoint, 'b.')
# plt.xlabel('Exptime (s)')
# plt.ylabel('Bot zeropoint')
# ps.savefig()
plt.clf()
#plt.plot(bot.ccdzpt, bot.zeropoint, 'b.')
plt.scatter(bot.ccdzpt, bot.zeropoint, c=bot.mjd_obs, alpha=0.25, edgecolor='none')
plt.plot(bot.ccdzpt[bad], bot.zeropoint[bad], 'r.')
plt.xlabel('Pipeline zeropoint')
plt.ylabel('Bot zeropoint')
ax = plt.axis()
plt.plot(xx, xx + diff, 'k-', alpha=0.3)
p2 = plt.plot(xx, xx + diff+0.05, 'k--', alpha=0.3)
plt.plot(xx, xx + diff-0.05, 'k--', alpha=0.3)
plt.legend([p2[0]], ['+- 0.05 mag'], loc='lower right')
plt.axis(ax)
plt.title(tt)
ps.savefig()
bot_trans = 10.**(-0.4 * (zp0 - bot.zeropoint))
expfactor_bot_zpt = 1./bot_trans**2
p_trans = 10.**(-0.4 * (np.median(bot.ccdzpt) - bot.ccdzpt))
expfactor_p_zpt = 1./p_trans**2
factor = np.median(expfactor_bot_zpt / expfactor_p_zpt)
xx = np.array([0, 10])
scatter = np.std(expfactor_bot_zpt / (expfactor_p_zpt * factor))
print('Zpt Scatter:', scatter)
plt.clf()
p1 = plt.plot(expfactor_p_zpt, expfactor_bot_zpt, 'b.')
plt.plot(expfactor_p_zpt[bad], expfactor_bot_zpt[bad], 'r.')
plt.xlabel('Pipeline exposure factor from zpt (arb. scale)')
plt.ylabel('Bot exposure factor from zeropoint')
plt.title(tt)
ax = plt.axis()
p = plt.plot(xx, xx*factor, 'k-', alpha=0.3)
p2 = plt.plot(xx, (xx*factor)*0.9, 'k--', alpha=0.3)
plt.plot(xx, (xx*factor)*1.1, 'k--', alpha=0.3)
plt.legend([p2[0], p1[0]], ['+- 10%', 'scatter %.1f %%' % (100.*scatter)],
loc='lower right')
plt.axis(ax)
ps.savefig()
# unextinct both so that the plot range tells us achieved depth
# in the quantity we care about
fid = nom.fiducial_exptime(band)
extinction = bot.ebv * fid.A_co
# 2-coverage target (90% fill)
target_depth = dict(g=24.0, r=23.4, z=22.5)[band]
# -> 1-coverage depth (- ~0.37 mag)
target_depth -= 2.5*np.log10(np.sqrt(2.))
# Using pipeline galdepth estimate, compute expfactor.
depthfactor = 10.**(-0.4 * (bot.galdepth - target_depth))
# airmass factor and transparency factor are already included
# in the zeropoint, hence in depthfactor.
expfactor = (depthfactor**2 *
10.**(0.8 * fid.A_co * bot.ebv))
# this is on top of the exposure time of this image / nominal
expfactor *= bot.exptime / fid.exptime
factor = np.median(bot.expfactor / expfactor)
xx = np.array([0, 10])
print('Galdepth values:', bot.galdepth)
print(bot.galdepth.min(), 'to', bot.galdepth.max())
scatter = np.std(bot.expfactor / (expfactor * factor))
print('Overall Scatter:', scatter)
goodvals = notbad[np.flatnonzero(expfactor[notbad] < 1e16)]
plt.clf()
#p1 = plt.plot(expfactor[notbad], bot.expfactor[notbad], 'b.')
#p1 = plt.plot(expfactor[goodvals], bot.expfactor[goodvals], 'b.')
plt.scatter(expfactor[goodvals], bot.expfactor[goodvals], c=bot.mjd_obs[goodvals], edgecolor='none', alpha=0.25, label='scatter %.1f %%' % (100.*scatter))
ax = plt.axis()
[xmn,xmx,ymn,ymx] = ax
plt.plot(np.clip(expfactor[bad], xmn,xmx),
np.clip(bot.expfactor[bad], ymn,ymx), 'r.')
plt.axis(ax)
plt.xlabel('Pipeline expfactor from galdepth')
plt.ylabel('Bot expfactor')
plt.plot(xx, xx*factor, 'k-', alpha=0.3, label='slope %0.3f' % (factor))
plt.plot(xx, (xx*factor)*0.9, 'k--', alpha=0.3, label='+- 10%')
plt.plot(xx, (xx*factor)*1.1, 'k--', alpha=0.3)
plt.plot(xx, xx, 'r-', alpha=0.3)
plt.legend(loc='lower right')
plt.axis(ax)
plt.title(tt)
ps.savefig()
galnorm = 1. / np.sqrt(galneff)
galsig1 = skysig1 / galnorm
galdepth = -2.5 * (np.log10(5. * galsig1) - 9)
diff = np.median(galdepth - bot.galdepth)
xx = np.array([20, 25])
gooddepth = notbad[bot.galdepth[notbad] > 10]
plt.clf()
# plt.plot((bot.galdepth - extinction)[gooddepth], (galdepth - extinction)[gooddepth], 'b.')
plt.scatter((bot.galdepth - extinction)[gooddepth], (galdepth - extinction)[gooddepth], c=bot.mjd_obs[gooddepth], edgecolor='none', alpha=0.25)
ax = plt.axis()
plt.plot((bot.galdepth - extinction)[bad], (galdepth - extinction)[bad], 'r.')
plt.xlabel('Pipeline galdepth (unextincted)')
plt.ylabel('Bot galdepth (unextincted)')
plt.axvline(target_depth, color='b', alpha=0.3)
#ax = [max(ax[0],20), ax[1], ax[2], ax[3]]
plt.plot(xx, xx+diff, 'k-', alpha=0.3)
p2 = plt.plot(xx, xx+diff+0.05, 'k--', alpha=0.3)
plt.plot(xx, xx+diff-0.05, 'k--', alpha=0.3)
plt.axis(ax)
plt.legend([p2[0]], ['+- 0.05 mag'], loc='lower right')
plt.title(tt)
ps.savefig()
II = []
q = np.percentile(bot.mjd_obs, [0, 25, 50, 75, 100])
for qlo,qhi in zip(q, q[1:]):
II.append(np.flatnonzero((bot.mjd_obs >= qlo) * (bot.mjd_obs <= qhi)))
for I in II:
plt.clf()
plt.scatter((bot.galdepth - extinction)[I], (galdepth - extinction)[I], c=bot.mjd_obs[I], edgecolor='none', alpha=0.25,
vmin=q[0], vmax=q[-1])
plt.xlabel('Pipeline galdepth (unextincted)')
plt.ylabel('Bot galdepth (unextincted)')
plt.axvline(target_depth, color='b', alpha=0.3)
plt.plot(xx, xx+diff, 'k-', alpha=0.3)
p2 = plt.plot(xx, xx+diff+0.05, 'k--', alpha=0.3)
plt.plot(xx, xx+diff-0.05, 'k--', alpha=0.3)
plt.axis(ax)
plt.legend([p2[0]], ['+- 0.05 mag'], loc='lower right')
plt.title(tt)
ps.savefig()
equivtime = (bot.exptime / bot.expfactor)
extdepth = bot.galdepth - extinction
plt.clf()
#notbad = np.flatnonzero((bot.photometric == True))
#p1 = plt.plot(equivtime[notbad], extdepth[notbad], 'b.', alpha=0.5)