-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMANGA_bench.py
executable file
·1258 lines (1046 loc) · 48.3 KB
/
MANGA_bench.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
#! /usr/bin/env python
import sys
def loader():
'''Import all of the necessary modules.
This function exists so if the user gives bad arguments to a MANGA call
they don't have to wait for all these imports before an error is raised.
'''
global ConfigParser, np, os, matplotlib, plt, Circle, AG, rc, PDF,\
PatchCollection, iraf, pyfits, ADE, time, glob, datetime, pickle,\
mmp
print 'Loading module...'
import numpy as np
import os
import matplotlib
if os.popen('echo $DISPLAY').readline() == 'localhost:10.0\n':
print '\tDeactivating display...'
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from mpl_toolkits.axes_grid1 import AxesGrid as AG
from matplotlib import rc
from matplotlib.backends.backend_pdf import PdfPages as PDF
from matplotlib.collections import PatchCollection
print '\tsyncing mesh...'
from pyraf import iraf
import pyfits
print '\tcalculating splines...'
import ADEUtils as ADE
import time
print '\tinitializing goodness...'
import glob
from datetime import datetime
import ConfigParser
import pickle
import MANGAmap as mmp
print 'load complete!\n'
debug = False
def FReD(direct_image, fiber_image, num_ap, pot, filt, dir_cut,\
EXTEN=0, OUTPUT=0, FL=50, FR=4.2, FP='99,99',FO='-99'):
'''
Description:
FReD is primary reduction tool for the FRD Bench. It takes in two
images, one for the direct beam and one for the fiber beam, and
produces outputs suitable for FRD analysis. The main tasks that
FReD performs are:
1) Producing curves of growth that show the enclosed energy
as a function of radius
2) Correcting both beam's data for effects that make the
direct beam non-ideal
3) Computing effective f-ratios for each beam. The effective
f-ratio is the f-ratio an ideal lens would have if its enclosed
energy was EE(r) at r.
Input:
direct_image - str
The name of the FITS file containing the direct beam data
fiber_image - stf
The name of the FITS file containing the fiber beam data
num_ap - Int
The number of apertures to use in creating the curve of growth
EXTEN - Int
The FITS extension where the primary data is storred. This
must be the same for both the direct and fiber beams
OUTPUT - str
The name of the output file. FReD produce no output besides
this file, so if you don't set it then nothing will happen.
FL - Float
The focal length of the lens used (L2)
FR - Float
The focal ratio (f-number) of the lens used
Output:
The only output produced by FReD is the output file specified by the
OUTPUT keyword. This file is a bunch of vectors suitable for plotting
by an external package, like supermongo. See the output file header
for more information on the file's contents.
Version History:
1.0 - 1.12.2011
1.1 - 1.13.2011 - Changed from appetize to annulize_sb followed
by a cumsum.
1.2 - 1.17.2011 - Changed to a corrected version of annulize (not
annulize_sb). The correction accounts for
incomplete data at large radii without having
to use surface brightness.
1.3 - 10.2011 - Added FRD metrics
'''
version = 1.3
# Shit's gonna get real real later on with variable names
# just remember that d_ is for variables relating to the direct beam
# and f_ is for variables relating to the fiber beam.
#
# Comments preceded by '#' relate to variable naming conventions
direct_HDU = pyfits.open(direct_image)[EXTEN]
fiber_HDU = pyfits.open(fiber_image)[EXTEN]
d_exptime = direct_HDU.header['EXPTIME']
f_exptime = fiber_HDU.header['EXPTIME']
direct = direct_HDU.data
fiber = fiber_HDU.data
direct[np.where(direct < dir_cut)] = 0.0
if pot:
# direct_time_str = direct_HDU.header['TIME-OBS']
# fiber_time_str = fiber_HDU.header['TIME-OBS']
# direct_time = np.float(direct_time_str[6:])\
# + np.float(direct_time_str[3:5])*60.\
# + np.float(direct_time_str[0:2])*3600.
# fiber_time = np.float(fiber_time_str[6:])\
# + np.float(fiber_time_str[3:5])*60.\
# + np.float(fiber_time_str[0:2])*3600.
# fcorrect = pot.get_correction(fiber_time,filt)
# dcorrect = pot.get_correction(direct_time,filt)
direct_start_time = direct_HDU.header['STARTIME']
direct_end_time = direct_HDU.header['ENDTIME']
fiber_start_time = fiber_HDU.header['STARTIME']
fiber_end_time = fiber_HDU.header['ENDTIME']
# filt = 'V'
pot.set_ref(filt,direct_start_time,direct_end_time)
dcorrect = pot.get_correction2(direct_start_time,direct_end_time,filt)
fcorrect = pot.get_correction2(fiber_start_time,fiber_end_time,filt)
print ' Direct throughput correction is '+str(dcorrect)
print ' Fiber throughput correction is '+str(fcorrect)
direct *= dcorrect
fiber *= fcorrect
if debug:
pot.plot(filt,[direct_start_time,direct_end_time,fiber_start_time,fiber_end_time])
raw_input(' Tput plots...')
if debug: print ' Annulizing...'
d_rvec, d_sb, d_sberr = ADE.fast_annulize(direct,num_ap)
f_rvec, f_sb, f_sberr = ADE.fast_annulize(fiber,num_ap)
if debug:
plt.clf()
fig = plt.figure(0)
ax1 = fig.add_subplot(221)
ax1.plot(d_rvec,d_sb)
ax1.plot(f_rvec,f_sb)
ax1.set_xlabel('Radius [px]')
ax1.set_ylabel('Counts [ADU]')
fig.show()
raw_input(' hit enter or something')
'''Turn pixels into an physical length. The SBIG STL-1001E has 24 micron
square pixels'''
d_rvec *= 0.024 #-> mm
f_rvec *= 0.024
if debug: print ' Cumsumming...'
d_flux = np.cumsum(d_sb)
f_flux = np.cumsum(f_sb)
d_ferr = (np.cumsum(d_sberr**2))**0.5
f_ferr = (np.cumsum(f_sberr**2))**0.5
if debug:
ax2 = fig.add_subplot(222)
ax2.plot(d_rvec, d_flux)
ax2.plot(f_rvec, f_flux)
ax2.set_xlabel('Radius [mm]')
ax2.set_ylabel('Cumulative flux [ADU]')
fig.show()
raw_input(' Cumsum')
'''Now we normalize the fluxes so we are talking about EE, the enclosed
energy'''
if debug: print ' EEing...'
d_max = np.max(d_flux)
f_max = np.max(f_flux)
d_EE = d_flux/d_max
f_EE = f_flux/f_max
'''we'll use f_EE_nd for plotting later'''
f_EE_nd = f_flux/d_max
# d_fmaxerr = d_ferr[np.where(d_flux == d_max)[0]]
# f_fmaxerr = f_ferr[np.where(f_flux == f_max)[0]]
# d_EEerr = ((d_ferr/d_max)**2 + (d_fmaxerr*d_flux/(d_max**2))**2)**0.5
# f_EEerr = ((f_ferr/f_max)**2 + (f_fmaxerr*f_flux/(f_max**2))**2)**0.5
'''Now we need to use the difference between an ideal beam and the direct
beam (which should be ideal) to create a correction that we can apply
to both the direct and fiber data'''
# _correction will correspond to corrections that will be applied to
# get corrected values and _c will correspond to values that have
# been corrected
f_r_correction = np.zeros(f_EE.size,dtype=float)
d_r_c = np.zeros(d_EE.size,dtype=float)
if debug: print ' Correcting...'
j=0
for k in range(f_r_correction.size):
# Naming conventions here match what is in my notebook on pages
# 47 through 51
'''First the direct beam'''
d_r_c[k] = (d_EE[k]*(FL/(2*FR))**2)**0.5
'''Now the fiber beam'''
f_r_i = (f_EE[k]*(FL/(2*FR))**2)**0.5
'''find the closest d_EE value that is less than f_EE[k]'''
while d_EE[j] < f_EE[k]: j += 1
'''interpolate'''
m = (d_EE[j] - d_EE[j-1])/(d_rvec[j] - d_rvec[j-1])
r_d = (f_EE[k] - d_EE[j-1])/m + d_rvec[j-1]
'''f_dr2 is f_dr**2'''
f_dr2 = r_d**2 - f_r_i**2
f_r_correction[k] = f_dr2
# if debug: print (f_rvec[k]**2 - f_r_correction[k])**0.5, f_rvec[k]**2, f_r_correction[k]
'''We do this to fix some weirdness that might happen at really large
radii. It's more of a visual appeal thing than anything else'''
if (np.abs(f_rvec[k]**2 - f_r_correction[k]))**0.5 <\
(np.abs(f_rvec[k-1]**2 - f_r_correction[k-1]))**0.5 \
or (f_rvec[k]**2 - f_r_correction[k]) < 0:
# if debug: print ' here'
f_r_correction[k] = f_r_correction[k-1]
'''Actually perform the correction on the fiber data'''
f_r_c = (f_rvec**2 - f_r_correction)**0.5
d_rerr = (np.abs(d_r_c - d_rvec))**0.5
f_rerr = (np.abs(f_r_c - f_rvec))**0.5
# if debug: print (np.abs(f_r_c - f_rvec))**0.5
################
if debug: print ' Computing...'
'''For the various plots we want to make we need to have the f-numbers
which is pretty easy b/c it only depends on radius'''
# N stands for f-number, a la my notes
d_N = FL/(2*d_rvec)
f_N = FL/(2*f_rvec)
d_N_c = FL/(2*d_r_c)
f_N_c = FL/(2*f_r_c)
'''We also need the EFFECTIVE f-number, which is the what the f-number
of the lens in an ideal system would be if the enclosed energy was
EE[k] at r[k]'''
#_e is for an effective quantity
d_N_e = d_N*(d_EE)**0.5
f_N_e = f_N*(f_EE)**0.5
d_N_e_c = d_N_c*(d_EE)**0.5
f_N_e_c = f_N_c*(f_EE)**0.5
##################
'''Before we go any further we will compute some metrics of throughput.
For this we'll first need to put fluxes in units of counts/time so that
different exposure times do not affect the result'''
d_ADUrate = d_flux/d_exptime
f_ADUrate = f_flux/f_exptime
'''We will compute the relative ADU/time at various percentages of the
total'''
tput_100 = float(f_ADUrate.max() / d_ADUrate.max())
testtput = f_ADUrate[-1] / d_ADUrate[-1]
print "Tput_100 is {}\nTest is {}".format(tput_100,testtput)
rf5 = FL/10.
rf4 = FL/8.
rf32 = FL/6.4
rf3 = FL/6.
rf47 = FL/9.4
# rf42 = FL/8.4
# rf4 = FL/8.
# rf38 = FL/7.6
# rf3 = FL/6.
rf_input = FL/(2.*FR)
if rf4 > f_r_c.max():
print "Max fiber radius reached at f/4. ({} > {})".format(rf4, f_r_c.max())
# d_ADU_input = np.interp(rf_input,d_rvec,d_ADUrate)
d_ADU_input = d_ADUrate.max()
# tput_f5 = np.interp(rf5,f_r_c,f_ADUrate)/d_ADUf5
# tput_f4 = np.interp(rf4,f_r_c,f_ADUrate)/d_ADUf4
# tput_f5b = np.interp(rf5,f_r_c,f_EE)
# tput_f4b = np.interp(rf4,f_r_c,f_EE)
f_ADUf5 = np.interp(rf5,f_rvec,f_ADUrate)
f_ADUf4 = np.interp(rf4,f_rvec,f_ADUrate)
f_ADUf3 = np.interp(rf3,f_rvec,f_ADUrate)
f_ADUf32 = np.interp(rf32,f_rvec,f_ADUrate)
f_ADUf47 = np.interp(rf47,f_rvec,f_ADUrate)
tput_f5 = f_ADUf5/d_ADU_input
tput_f4 = f_ADUf4/d_ADU_input
tput_f3 = f_ADUf3/d_ADU_input
tput_f32 = f_ADUf32/d_ADU_input
tput_f47 = f_ADUf47/d_ADU_input
# f_ADUf42 = np.interp(rf42,f_rvec,f_ADUrate)
# f_ADUf4 = np.interp(rf4,f_rvec,f_ADUrate)
# f_ADUf38 = np.interp(rf38,f_rvec,f_ADUrate)
# f_ADUf3 = np.interp(rf3,f_rvec,f_ADUrate)
# tput_f42 = f_ADUf42/d_ADU_input
# tput_f4 = f_ADUf4/d_ADU_input
# tput_f38 = f_ADUf38/d_ADU_input
# tput_f3 = f_ADUf3/d_ADU_input
tput_f5b = np.interp(rf5,f_rvec,f_EE)
tput_f4b = np.interp(rf4,f_rvec,f_EE)
r_ideal = FL/(2*FR)
r_ideal_test = d_r_c[np.where(f_ADUrate == f_ADUrate.max())[0]]
metric = np.interp(r_ideal, f_r_c, f_EE)
metric80 = FL/(2*np.interp(0.82,f_EE,f_r_c))
metric90 = FL/(2*np.interp(0.9,f_EE,f_r_c))
if debug:
try:
direct_V = pot.get_voltage(direct_start_time,direct_end_time,filt)
fiber_V = pot.get_voltage(fiber_start_time,fiber_end_time,filt)
except KeyError:
direct_V,drawV = (1.0,1.0)
fiber_V,frawV = (1.0,1.0)
ax3 = fig.add_subplot(223)
ax3.plot(f_rvec,f_ADUrate,'g')
ax3.plot(d_rvec,d_ADUrate,'b')
ax3.set_xlabel('Radius [mm]')
ax3.set_ylabel('Count rate [ADU/s]')
ax3.axvline(ls=':',x=rf32,color='b')
ax3.axvline(ls='--',x=rf32,color='g')
ax3.axvline(ls=':',x=rf4,color='k')
ax3.axvline(ls=':',x=rf5,color='k')
ax3.axhline(ls='--',y=sloanf,color='g')
ax3.axhline(ls=':',y=sloand,color='b')
infostr = "\tFiber ratio at f/3.2 ({:}mm): {:8.4E}".format(rf32,sloanf/fiber_V)+\
"\n\tDirect ratio at f/3.2 ({}mm): {:8.4E}".format(rf32,sloand/direct_V)+\
"\n\tFiber counts/s at (f/4, f/5): ({:8.4E}, {:8.4E})".format(f_ADUf4,f_ADUf5)+\
"\n\tDirect counts/s at (f/4, f/5): ({:8.4E}, {:8.4E})".format(d_ADUf4,d_ADUf5)+\
"\n\tFiber voltages are : {:4.4f}".format(fiber_V)+\
"\n\tDirect voltages are : {:4.4f}".format(direct_V)+\
"\n\tSloan metric is {}".format(sloan_m)+\
"\n\tFull direct is: {:8.4E}".format(float(d_ADUrate.max()))+\
"\n\tMax d_r_c value is: {:4.2f}".format(d_r_c.max())+\
"\n\tr_ideal is: {:4.2f}".format(r_ideal)+\
"\n\tr_max is: {}".format(r_ideal_test)
ax3.text(1.1,0.4,infostr,transform=ax3.transAxes,ha='left',va='center')
print infostr
fig.suptitle('{} - {}'.format(os.popen('pwd').readlines()[0],datetime.now().isoformat(' ')))
fig.show()
raw_input(" ADUrate")
##################
'''Grab some information about the data for the header'''
try:
filt = pyfits.open(direct_image)[EXTEN].header['FILTER']
except KeyError:
filt = 'NA'
try:
fiber = pyfits.open(direct_image)[EXTEN].header['OBSERVER']
except KeyError:
fiber = 'NA'
try:
polish = pyfits.open(direct_image)[EXTEN].header['TELESCOP']
except KeyError:
polish = 'NA'
if OUTPUT:
f = open(OUTPUT,'w')
f.write('# Generated by FReD v.'+str(version)+'\n'
+'# Output writen on: '+datetime.now().isoformat(' ')+'\n'
+'# Input file (direct beam): '+direct_image+'\n'
+'# Input file (fiber beam): '+fiber_image+'\n'
+'# Number of apertures: '+str(num_ap)+'\n'
+'# Focal length and beam speed: '+str(FL)+'mm '+str(FR)+'\n'
+'# Filter: '+filt+'\n'
+'# Fiber input position: '+FP+'\n'
+'# Fiber output position: '+FO+'\n'
+'# Harness: '+polish+'\n'
+'# Total Throughput: '+str(tput_100)+'\n'
+'# Direct image cutoff: '+str(dir_cut)+'\n'
+'#\n'
+'# d_r = aperture radius of direct beam (mm)\n'
+'# f_r = aperture radius of fiber beam (mm)\n'
+'# d_N = f-ratio of direct beam\n'
+'# f_N = f-ratio of fiber beam\n'
+'# d_r_c = corrected direct beam radius (mm)\n'
+'# f_r_c = corrected fiber beam radius (mm)\n'
+'# d_N_c = corrected direct f-ratio\n'
+'# f_N_c = corrected fiber f-ratio\n'
+'# d_EE = normalized enclosed energy of direct beam\n'
+'# f_EE = normalized enclosed energy of fiber beam\n'
+'# d_N_e = effective f-ratio for direct beam\n'
+'# f_N_e = effective f-ratio for fiber beam\n'
+'# d_N_e_c = effective f-ratio for corrected direct beam\n'
+'# f_N_e_c = effective f-ratio for corrected fiber beam\n'
+'#\n'
+'# d_r f_r d_N f_N d_r_c'
+' f_r_c d_N_c f_N_c d_EE f_EE'
+' d_N_e f_N_e d_N_e_c f_N_e_c\n'
+'# 1 2 3 4 5'
+' 6 7 8 9 10'
+' 11 12 13 14\n')
for i in range(min(d_rvec.size,f_rvec.size)):
np.array([d_rvec[i],
f_rvec[i],
d_N[i],
f_N[i],
d_r_c[i],
f_r_c[i],
d_N_c[i],
f_N_c[i],
d_EE[i],
f_EE[i],
d_N_e[i],
f_N_e[i],
d_N_e_c[i],
f_N_e_c[i]]).tofile(f,sep=' ',format='%9.3E')
f.write('\n')
f.close()
return ((metric90,metric80,tput_100,tput_f5,tput_f47,
tput_f4,tput_f32,tput_f3,tput_f5b,tput_f4b),(d_N,d_N_c,f_N,f_N_c,d_EE,f_EE,f_EE_nd))
def soba(nood,num_ap,dir_cut,exten,pot,mfile):
'''
Description:
soba takes a reduced Noodle object and runs it through FReD for
analysis. The Noodle must have been reduced or the soba will not
taste good! All inputs are handled by main as soba is not intended
to be run alone.
Inputs:
nood - The reduced Noodle object
num_ap - The number of apertures to use when reducing the data
exten - The FITS extension that holds the data
Output:
soba runs FReD on every filter/f-ratio combination found in nood.
As a result, the output is a bunch of .dat files produced by FReD.
'''
print '\nAnalyzing data...'
f = open(mfile,'wb')
f.write('# Output writen on: '+datetime.now().isoformat(' ')+'\n'
+'# Reduction directory: '+os.getcwd()+'\n'
+'# INI file: '+sys.argv[1]+'\n'
+'#\n'
+'# {:10}= '.format('Fiber_pos')+'fiber input position\n'
+'# {:10}= '.format('Out_pos')+'fiber output position\n'
+'# {:10}= '.format('filt')+'filter\n'
+'# {:10}= '.format('N90')+'fiber f/# at EE90\n'
+'# {:10}= '.format('N82')+'fiber f/# at EE82\n'
+'# {:10}= '.format('tput')+'total throughput\n'
+'# {:10}= '.format('tput5')+'fiber within f/5 / direct within input beam (uncorrected)\n'
+'# {:10}= '.format('tput47')+'throughput at f/4.7 (uncorrected)\n'
+'# {:10}= '.format('tput4')+'throughput at f/4 (uncorrected)\n'
+'# {:10}= '.format('tput32')+'throughput at f/3.2 (uncorrected)\n'
+'# {:10}= '.format('tput3')+'throughput at f/3 (uncorrected)\n'
+'# {:10}= '.format('EE5')+'fiber EE at f/5\n'
+'# {:10}= '.format('EE4')+'fiber EE at f/4\n'
+'#\n'
+str('#{0:>10}{1:>10}{2:>9}{3:>9}{4:>9}{5:>9}{6:>9}{7:>9}'
+'{8:>9}{9:>9}{10:>9}{11:>9}{12:>9}\n')\
.format('Fiber_pos','Out_pos','filt','N90','N82','tput','tput5',\
'tput47','tput4','tput32','tput3','EE5','EE4')
+str('#{0:>10}{1:>10}{2:>9}{3:>9}{4:>9}{5:>9}{6:>9}{7:>9}'
+'{8:>9}{9:>9}{10:>9}{11:>9}{12:>9}\n')\
.format(1,2,3,4,5,6,7,8,9,10,11,12,13))
hdulist = []
for fiber_pos in np.sort(nood.keys()):
print 'Fiber ('+str(fiber_pos)+')'
try: outpos = nood[fiber_pos]['outpos']
except KeyError: outpos = 'NA'
print 'Output position is ('+outpos+')'
focal_length = nood[fiber_pos]['focal_length']
focal_ratio = nood[fiber_pos]['focal_ratio']
diameter = nood[fiber_pos]['diameter']
L2_focal_length = nood[fiber_pos]['L2f']
# focal_length = 53.2988
print 'Input focal length = {} mm'.format(L2_focal_length)
print 'Aperture stop diameter = {} mm'.format(diameter)
print 'Fiber fed at f/{:3.1f}'.format(focal_ratio)
print 'Camera focal length = '+str(focal_length)+' mm'
for filt in nood[fiber_pos]['data'].keys():
direct_name = nood[fiber_pos]['data'][filt]['direct']['final']
fiber_name = nood[fiber_pos]['data'][filt]['fiber']['final']
name = fiber_name[:fiber_name.rfind('f.')]
print ' Filter: '+str(filt)
print ' Direct image is '+direct_name
print ' Fiber image is '+fiber_name
(metric,plot_data) = FReD(direct_name,fiber_name,num_ap,pot,filt,
dir_cut,EXTEN=exten,FL=focal_length,
FR=focal_ratio,OUTPUT=name+'.dat',
FP=fiber_pos,FO=outpos)
hdu = pyfits.ImageHDU(np.array(plot_data))
hdu.header.update('FIBERPOS',fiber_pos)
hdu.header.update('OUTPOS',outpos)
hdu.header.update('SLOAN',metric[3])
hdulist.append(hdu)
f.write('{0:>11}{1:>10}{2:>9}'.format(fiber_pos,outpos,filt))
for m in metric: f.write('{:9.4f}'.format(m))
f.write('\n')
'''now generate the FRD plots using supermongo'''
sm = open('tmp_'+name+'.sm','wb')
sm.write('verbose 0\n'
+'macro read manga_FRD.sm\n'
+'device postencap_color '+name+'.ps\n'
+'manga_FRD "'+name+'.dat"\ndevice nodevice\n')
sm.close()
os.system('sm < tmp_'+name+'.sm')
os.system('convert -density 200 '+name+'.ps -quality 92 '+name+'.jpg')
os.system('rm tmp_'+name+'.sm')
try:
pyfits.HDUList([pyfits.PrimaryHDU(None)]+hdulist).writeto('plotdata.fits')
except IOError:
if raw_input("Overwrite file 'plotdata.fits'?: (Y/n)").lower() in ['y','']:
os.system('rm plotdata.fits')
pyfits.HDUList([pyfits.PrimaryHDU(None)]+hdulist).writeto('plotdata.fits')
# plot_helper('plotdata.fits','allfibers.pdf','')
# os.system('convert -density 200 allfibers.pdf -quality 92 allfibers.jpg')
f.close()
return
def main():
options = ConfigParser.ConfigParser()
options.read(sys.argv[1])
print "Reading settings from "+sys.argv[1]
resume = options.getboolean('Options','resume')
if resume:
noodfile = options.get('Options','noodle_file')
print 'Loading previous data run from '+noodfile
N = pickle.load(open(noodfile,'rb'))
else:
N = Noodle(options)
N.build_run()
noodsave = options.get('Options','noodle_save')
print 'Saving data run to '+noodsave
pickle.dump(N,open(noodsave,'wb'))
num_ap = options.getint('Options','num_ap')
dir_cut = options.getint('Options','direct_cutoff')
gogo = options.getboolean('Options','gogo')
mfile = options.get('Options','metric_file')
hname = options.get('Options','html_name')
html = options.getboolean('Options','html_go')
global debug
debug = options.getboolean('Options','debug')
if gogo:
doT = options.get('Data','Tput_file')
if doT.lower() == 'false': T = False
else: T = thePot(doT)
os.system('cp /d/monk/eigenbrot/MANGA/manga_FRD.sm .')
soba(N.ratios,num_ap,dir_cut,N.exten,T,mfile)
ring_helper(mfile)
plot_helper('plotdata.fits',
'allfibers.pdf',
hname+'\n'+datetime.now().isoformat(' '))
os.system('convert -density 200 allfibers.pdf -quality 92 allfibers.jpg')
# soba(N,num_ap,dir_cut,0,T,mfile) #use if running an ABABA run
if html:
print 'Producing web product...'
webit(hname)
print 'Reduction completed'
return
class Noodle:
''' Noodle is a data structure that keeps track of all the different
data files associated with the full-beam bench. It also can reduce
these files to produce images ready for analysis by FReD.
'''
def __init__(self, config):
'''The data is intiallized with a .ini file that is passed
to Noodle throuh main(). All initialization does is read in the
relevant parameters and create the empty data structures.
'''
self.fiber_dir = config.get('Data','fiber_directory')
self.direct_dir = config.get('Data','direct_directory')
self.dark_dir = config.get('Data','dark_directory')
self.exten = config.getint('Data','fits_exten')
self.clean = config.getboolean('Options','cleanup')
self.darkcombine = config.get('Options','darkcombine')
self.datacombine = config.get('Options','datacombine')
self.datareject = config.get('Options','datareject')
self.rejectpar = config.get('Options','rejectpar')
self.mapping_scheme = config.get('Options','inoutmap')
self.darks = {}
self.fiber = {}
self.direct = {}
'''I'll keep calling the top-level distinction a ratio because it will
avoid any confusion that might arise by calling it fibers'''
self.ratios = {}
self.mapdb = self.genmap(self.mapping_scheme)
def build_run(self):
'''build_run is the highest level method for Noodle and really the
only one you would need to call directly. It assembles the raw data
and reduces it. It also calls a lot of helper functions.
'''
print '\nBuilding data run...'
self.get_darks()
print ' dark-subtracting fiber images...'
self.reduce_data(self.fiber_dir,self.fiber)
print ' dark-subtracting direct images...'
self.fill_dict(self.direct_dir,self.direct)
self.sub_darks(self.direct)
self.direct_to_ratios(self.direct)
self.clean_up(self.ratios)
print ' combining images...'
self.combine()
self.uphead(self.mapdb)
if self.clean: os.system('rm *_ds.fits')
def genmap(self,scheme):
'''All this does is return the correct input-output mapping. It doesn't
actually compute anything'''
try:
return mmp.master[scheme]
except KeyError:
print "Warning: mapping scheme not in database"
return False
def reduce_data(self,directory,dictionary):
self.fill_dict(directory,dictionary)
self.sub_darks(dictionary)
self.add_to_ratios(dictionary)
def fill_dict(self,directory,dictionary):
data_list = glob.glob(directory+'/*FIT')
for data in data_list:
exptime = pyfits.open(data)[self.exten].header['EXPTIME']
if exptime not in dictionary.keys():
dictionary[exptime] = {'raw':[],'ds':[]}
dictionary[exptime]['raw'].append(data)
def get_direct(self):
direct_list = glob.glob(self.direct_dir+'/*')
for data in direct_list:
head = pyfits.open(data)[self.exten].header
fiber_pos = head['ORIGIN']
filt = head['FILTER']
exptime = head['EXPTIME']
if fiber_pos not in self.direct.keys():
self.direct[fiber_pos] = {}
if filt not in self.direct[fiber_pos].keys():
self.direct[fiber_pos][filt] = {'raw':{},'combined':None}
if data.find('Combined') >= 0:
self.direct[fiber_pos][filt]['combined'] = data
elif data.find('Combined') == -1:
if exptime not in self.direct[fiber_pos][filt]['raw'].keys():
self.direct[fiber_pos][filt]['raw'][exptime] =\
{'raw':[],'ds':[]}
self.direct[fiber_pos][filt]['raw'][exptime]['raw']\
.append(data)
def get_darks(self):
print ' finding darks...'
dark_list = glob.glob(self.dark_dir+'/*.[Ff][Ii][Tt]*')
for dark in dark_list:
exptime = pyfits.open(dark)[self.exten].header['EXPTIME']
if exptime not in self.darks.keys():
self.darks[exptime] = {'raw':[],'combined':None}
if dark.find('Combined') >= 0:
self.darks[exptime]['combined'] = dark
else:
self.darks[exptime]['raw'].append(dark)
def gen_dark(self,exptime):
iraf.imcombine.reject = 'none'
iraf.imcombine.combine = self.darkcombine
name = self.dark_dir+'/Combined_'+str(exptime)+'_DARK.fits'
iraf.imcombine(','.join(self.darks[exptime]['raw']),name)
self.darks[exptime]['combined'] = name
def sub_darks(self,data):
for exp in data.keys():
inputlist = data[exp]['raw']
outputlist = [s[s.rfind('/')+1:s.find('.FIT')]+\
'_ds.fits'\
for s in inputlist]
if self.darks[exp]['combined'] == None: self.gen_dark(exp)
if len(inputlist) > 10:
while len(inputlist) > 0:
inputstring = ','.join(inputlist[:10])
outputstring = ','.join(outputlist[:10])
iraf.nhedit(inputstring,'FIBERPOS','"(ORIGIN)"','Fiber input position',add=True,addonly=True)
iraf.imarith(inputstring,'-',\
self.darks[exp]['combined'],\
outputstring)
data[exp]['ds'] += outputlist[:10]
inputlist = inputlist[10:]
outputlist = outputlist[10:]
else:
iraf.nhedit(','.join(inputlist),'FIBERPOS','"(ORIGIN)"','Fiber input position',add=True,addonly=True)
iraf.imarith(','.join(inputlist),\
'-',\
self.darks[exp]['combined'],\
','.join([s[s.rfind('/')+1:s.find('.FIT')]+\
'_ds.fits' for s in inputlist]))
data[exp]['ds'] += outputlist
def add_to_ratios(self, data):
for exp in data.keys():
for ds in data[exp]['ds']:
head = pyfits.open(ds)[self.exten].header
fiber_pos = head['FIBERPOS']
L2_focal_length = float(head['APTAREA'])
diameter = float(head['APTDIA'])
focal_ratio = L2_focal_length/diameter
filt = head['FILTER']
ftype = head['OBSERVER']
timestr = head['TIME-OBS']
obstime = np.float(timestr[6:])\
+ np.float(timestr[3:5])*60.\
+ np.float(timestr[0:2])*3600.
if fiber_pos not in self.ratios.keys():
self.ratios[fiber_pos] =\
{'data':{},'focal_ratio':focal_ratio, 'diameter':diameter, 'L2f':L2_focal_length}
if filt not in self.ratios[fiber_pos]['data'].keys():
self.ratios[fiber_pos]['data'][filt] = {}
if ftype not in self.ratios[fiber_pos]['data'][filt].keys():
self.ratios[fiber_pos]['data'][filt][ftype] =\
{'raw':[],'obstimes':[],'exptime':exp,'final':None}
self.ratios[fiber_pos]['data'][filt][ftype]['raw'].append(ds)
self.ratios[fiber_pos]['data'][filt][ftype]['obstimes'].append(obstime)
def direct_to_ratios(self, data):
for exp in data.keys():
for ds in data[exp]['ds']:
head = pyfits.open(ds)[self.exten].header
L3_focal_length = head['FOCALLEN']
apt_diameter = head['APTDIA']
filt = head['FILTER']
ftype = head['OBSERVER']
timestr = head['TIME-OBS']
obstime = np.float(timestr[6:])\
+ np.float(timestr[3:5])*60.\
+ np.float(timestr[0:2])*3600.
for pos in self.ratios.keys():
self.ratios[pos]['focal_length'] = L3_focal_length
try:
self.ratios[pos]['data'][filt][ftype]['raw'].append(ds)
self.ratios[pos]['data'][filt][ftype]['obstimes'].append(obstime)
except KeyError:
self.ratios[pos]['data'][filt][ftype] =\
{'raw':[ds],'obstimes':[obstime],'exptime':exp,'final':None}
def clean_up(self,data):
final_direct = []
for fratio in data.keys():
for filt in data[fratio]['data'].keys():
if 'fiber' not in data[fratio]['data'][filt].keys():
del data[fratio]['data'][filt]
def combine(self):
iraf.imcombine.combine = self.datacombine
iraf.imcombine.reject = self.datareject
iraf.imcombine.lsigma = self.rejectpar
for fiber_pos in self.ratios.keys():
for filt in self.ratios[fiber_pos]['data'].keys():
for ftype in self.ratios[fiber_pos]['data'][filt].keys():
name = self.ratios[fiber_pos]['data'][filt][ftype]['raw'][0]
name = name[:name.rfind('.0')]+'.fits'
mintime = min(self.ratios[fiber_pos]['data'][filt][ftype]['obstimes'])
maxtime = max(self.ratios[fiber_pos]['data'][filt][ftype]['obstimes'])
maxtime += float(self.ratios[fiber_pos]['data'][filt][ftype]['exptime'])
iraf.imcombine(\
','.join(self.ratios\
[fiber_pos]['data'][filt][ftype]['raw']),\
name)
self.ratios[fiber_pos]['data'][filt][ftype]['final'] = name
iraf.nhedit(name,'STARTIME',mintime,'Start time of first combined image',addonly=True)
iraf.nhedit(name,'ENDTIME',maxtime,'End time of last combined image',addonly=True)
def uphead(self,mapping):
if mapping == False:
print "Warning: Input to output mapping not loaded correctly.\n"+\
"FITS headers will NOT be updated"
return
for fiber_pos in self.ratios.keys():
for filt in self.ratios[fiber_pos]['data'].keys():
outpos = mapping[fiber_pos]
iraf.nhedit(self.ratios[fiber_pos]['data'][filt]['fiber']['final'],\
"OUTPOS",outpos,'Fiber output position',\
add=True,addonly=False,after='FIBERPOS')
self.ratios[fiber_pos]['outpos'] = outpos
class thePot:
def __init__(self, t_file):
self.ref_levels = {}
## t_file = config.get('Data','Tput_file')
## self.order = config.getint('Data','Tput_fit_order')
try: self.times, self.levels, self.errs = np.loadtxt(t_file,usecols=(0,1,2),
unpack=True,
converters={0: self.format_time})
except ValueError:
self.times, self.levels = np.loadtxt(t_file,usecols=(0,1),unpack=True,
converters={0: self.format_time})
self.filters = np.loadtxt(t_file,usecols=(-1,),dtype=np.str)
def set_ref(self, filt, time1, time2):
if time2 > self.times.max() or time1 < self.times.min():
self.ref_levels[filt] = {'level': 1.0, 'times': [time1,time2]}
return
tidx = np.where((self.times >= time1) & (self.times <= time2))
avg_level = np.mean(self.levels[tidx])
self.ref_levels[filt] = {'level': avg_level, 'times': [time1,time2]}
# self.plot(filt,times=[time1,time2])
def get_correction2(self, time1, time2, filt):
if time2 > self.times.max() or time1 < self.times.min():
print "Asking for time outside of range"
return 1.0
tidx = np.where((self.times >= time1) & (self.times <= time2))
avg_level = np.mean(self.levels[tidx])
return self.ref_levels[filt]['level']/avg_level
def get_voltage(self,time1,time2,filt,std=False):
idx = np.where((self.times >= time1) & (self.times <= time2))
rawlevel = np.mean(self.levels[idx])
if std:
return rawlevel, np.std(self.levels[idx])
else: return rawlevel
def format_time(self,string):
h = np.float(string[0:2])*3600.
m = np.float(string[3:5])*60.
s = np.float(string[6:])
return h+m+s
def plot(self,filt,times=False):
fig = plt.figure(0)
plt.clf()
ax1 = fig.add_subplot(111)
idx = np.where(self.filters == filt)
ax1.plot(self.times[idx],self.levels[idx],'.')#,t,l,'-')
try: ax1.plot(self.ref_levels[filt]['times'],\
[self.ref_levels[filt]['level'] for i in range(2)],linestyle='',marker='s')
except KeyError: pass
ax1.set_ylabel('$V$')
ax1.set_xlabel('time [s]')
fig.suptitle(os.popen('pwd').readlines()[0]+datetime.now().isoformat(' '))
if times:
for time in times:
ax1.axvline(ls=':',x=time,color='r')
fig.show()
return
def webit(name):
path = '/d/www/eigenbrot/MANGA/'+name
os.system('mkdir '+path)
os.system('cp *.dat '+path)
os.system('cp *.txt '+path)
os.system('cp *.ps '+path)
os.system('cp *.jpg '+path)
os.system('cp *.pdf '+path)
ht = open(path+'/index.html','wb')
ht.write('<HTML><TITLE>FRD Bench data output '+name+'</TITLE>\n'
+'<body bgcolor="EEEEEE" link="#333FF" vlink="#5500BB">\n'
+'<h1>Data products for '+name+'\n'
+'<h2>Web summary generated on '+datetime.now().isoformat(' ')
+'</h2></h1>\n<hr noshade>\n')
dat_list = glob.glob(path+'/*.txt')+glob.glob(path+'/*.dat')
for dat in dat_list:
link = dat[dat.rfind('/')+1:]
ht.write('<a href='+link+' type="text/plain">'+link+'</a><br>\n')
ht.write('<h3>FRD plots</h3>Click images for jpg\n')
ht.write('<h4>All-fiber grid</h4>\n'
+'<a href=allfibers.pdf>pdf</a> <a href=allfibers.jpg>jpg</a><br>\n'
+'<a href=allfibers.jpg><img src=allfibers.jpg height=300 width=300></a>\n')
plt_list = [i[i.rfind('/')+1:i.rfind('.')] for i in glob.glob(path+'/*.ps')]
ht.write('<h4>Individual fibers</h4>\n<table>\n<tr>')
i = 1
for plot in plt_list:
ht.write('<td>\n'+plot+':<br>\n')
ht.write('<a href='+plot+'.ps>ps</a> <a href='+plot+'.jpg>jpg</a><br>\n')
ht.write('<a href='+plot+'.jpg><img src='+plot+'.jpg height=300 width=232></a>\n</td>\n')
if i % 5 == 0: ht.write('</tr>\n<tr>')
i += 1
ht.write('</tr></table>\n</body>\n</HTML>')
ht.close()
old_m = open('/d/www/eigenbrot/MANGA/index.html')
lines = old_m.readlines()
old_m.close()
lines[3] = '<h2>Last updated '+datetime.now().isoformat(' ')+'</h2></h1>\n'
m = open('/d/www/eigenbrot/MANGA/index.html','wb')
m.writelines([l for l in lines[:-1]])
m.write('<a href='+name+'>'+name+'</a><br>\n</body></HTML>')
m.close()
def plot_helper(datafile,filename,title):
pp = PDF(filename)
rc('text', usetex=False)
rc('font', family='serif')
rc('font', size=9.0)
rc('axes', linewidth=0.4)
fiber_plot_mapping = dict([('0,-2',11),
('1,-2',10),
('2,-2',9),
('-1,-1',12),
('0,-1',3),
('1,-1',2),
('2,-1',8),
('-2,0',13),
('-1,0',4),
('0,0',1),
('1,0',7),