-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelMake_v2.py
More file actions
1013 lines (884 loc) · 36.5 KB
/
ModelMake_v2.py
File metadata and controls
1013 lines (884 loc) · 36.5 KB
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
#!/home/abrahams/epd_free-7.3-2-rh5-x86/bin/python
import sys, os
import gt_apps as gaps
import numpy as np
from astLib import astCoords
import pyfits
from astLib import astWCS
import healpy as hp
import scipy.optimize
from scipy import ndimage
#
# 5August2014: Fix fx_src for the lightcurve!!! The model is not created:
# To fix, just copy mdl2_all.xml and in LikeRun freeze the
# sources. Easy enough.
#
#
#
#
# Must be have changed into analysis directory!
#
# Here we make the model. We need to sift through the catalog and
# choose strong sources that are close to the center of the RoI.
# Then, we need to determine the spectral shape before writing then
# xml file. This routine also needs to create the spatial templates
# for HI, CO (from Planck), and dust.
#
# For many of these functions, we'll need to import a bunch of things
# that are present in the "SelectLoad.py" section.
## ##############################################
# weighted sum of nearest pixels
def Interpol(im, x, y):
""" Weighted sum of nearest pixels given a fractional pixel position. """
dx = x%1.0
dy = y%1.0
x1 = int(np.floor(x))
y1 = int(np.floor(y))
weight = np.array([(1-dx)*(1-dy),dx*(1-dy),(1-dx)*dy,dx*dy])
val = np.zeros( 4 )
val[0] = im[x,y]
val[1] = im[(x+1),y]
val[2] = im[x,(y+1)]
val[3] = im[(x+1),(y+1)]
return sum(val*weight)
## ##############################################
# Get the pixel value for the LAB survey, needs galactic longitude
# and the number of pixels on that axis.
def find_lon_hi(l, nl):
if (l < 180):
lon = 2*(180-l)
else:
lon = 2*(540-l)
# previously else: lon = nl - (l-180)*2
return lon
## ##############################################
def GenLon(Input):
""" Longitude array for background maps """
rad= ((Input['nxpix'][1]*Input['binsz'][1])/2.0) + (6./2.0)
lu = (Input['glon'][1] + rad)%360
ld = (Input['glon'][1] - rad)%360
df = int(lu/Input['binsz'][1])
sz = int(2*rad/Input['binsz'][1])
if ( (lu - ld) < 0 ):
df = (lu - 0.)/Input['binsz'][1]
l = np.append( np.linspace(lu,0.,df),np.linspace((360-Input['binsz'][1]),ld,int(sz-df)))
else:
l = np.linspace(lu,ld,int(2*rad/Input['binsz'][1]))
return l
## ##############################################
def GenLat(Input):
""" Latitude array for background maps """
rad= ((Input['nypix'][1]*Input['binsz'][1])/2.0) + (6./2.0)
bu = (Input['glat'][1] + rad)
bd = (Input['glat'][1] - rad)
# return np.linspace(bd,bu,int(Input['nxpix'][1]+ (10./Input['binsz'][1])))
return np.linspace(bd,bu,int(2*rad/Input['binsz'][1]))
## ##############################################
def HI_map(Inputs, fl,dist):
""" Make HI background map using LAB survey, integrated assuming
optically thick emission: N(HI) = -log( 1 - (Tb/(Ts - Tbg)) ).
Optically thin emission corresponds to Ts --> infinity
Tb = antenna temperature (data)
Ts = spin temperature
Tbg= background temperature, assumed CMB temperature
(eventually use 1.4 GHz data, but small difference
at high Galactic latitudes)
dist= 1 is near, 2 is far.
"""
import pyfits
from astLib import astCoords
if ( dist == 1 ):
hi = pyfits.open(fl['HI%s'%(fl['opacity'])])
elif ( dist == 2 ):
hi = pyfits.open(fl['HI%s_gt'%(fl['opacity'])])
else:
print "Error -- no HI maps there."
hi[0].data = np.transpose( hi[0].data )
head = pyfits.getheader('CMAP.fits')
cmap_w = astWCS.WCS(head, mode="pyfits")
l = GenLon(Inputs)
b = GenLat(Inputs)
nl = head['NAXIS1']
# cycle through coordinates, find pixel values and place it into array
image = np.zeros( (l.size,b.size) )
for i in np.arange( l.size ):
for j in np.arange( b.size ):
new_l = find_lon_hi(l[i],nl)
new_b = 2*(90+b[j])
image[i,j] = Interpol(hi[0].data, new_l, new_b)
radx = ((Inputs['nxpix'][1]*Inputs['binsz'][1])/2.0) + (6./2.0)
rady = ((Inputs['nypix'][1]*Inputs['binsz'][1])/2.0) + (6./2.0)
pxcen = radx/Inputs['binsz'][1]
pycen = rady/Inputs['binsz'][1]
ima = pyfits.PrimaryHDU(np.transpose(image))
ima.header['CTYPE1'] = ('GLON-CAR')
ima.header['CRPIX1'] = ( pxcen, 'Reference Pixel')
ima.header['CRVAL1'] = (Inputs['glon'][1], 'Galactic longitude at reference pixel')
ima.header['CDELT1'] = (-Inputs['binsz'][1],'x-axis increment per pixel')
ima.header['CUNIT1'] = ('deg','Physical Units for x-axis')
ima.header['CTYPE2'] = ('GLAT-CAR')
ima.header['CRPIX2'] = ( pycen, 'Reference pixel')
ima.header['CRVAL2'] = (Inputs['glat'][1],'Galactic latitude at reference pixel')
ima.header['CDELT2'] = (Inputs['binsz'][1], 'y-axis increment per pixel')
ima.header['CUNIT2'] = ('deg','Physical units for y-axis')
ima.header['Equinox'] = (2000.,'Equinox of coordinates')
image[ image < 0.0 ] = 0.0
# we need to normalize the map (making sure degrees are radians)
if ( dist == 1 ):
ima.writeto(fl['HImp_%snon'%fl['opacity']])
hi = pyfits.open(fl['HImp_%snon'%fl['opacity']])
elif ( dist == 2 ):
ima.writeto(fl['HImp_%snon_gt'%fl['opacity']])
hi = pyfits.open(fl['HImp_%snon_gt'%fl['opacity']])
hi[0].data[ hi[0].data < 0.0 ] = 0.0
norm = hi[0].data.sum() * (np.pi/180.)**2 * (Inputs['binsz'][1]**2)
hi[0].data /= norm
if ( dist == 1 ):
hi.writeto(fl['HImp_%s'%fl['opacity']])
elif ( dist == 2 ):
hi.writeto(fl['HImp_%s_gt'%fl['opacity']])
hi = None
ima= None
image = None
cmap_w = None
head = None
del hi, ima, image, cmap_w, head
## ##############################################
def CO_map(Inputs, fl):
""" Make CO background map from Planck CO - type 2 map. HealPy is used
simply to find the index corresponding to the location of interest.
We use the CO(J=1->0) line (1st column of the HealPix data). """
import healpy as hp
mmm = open('COmake.txt','a')
mmm.write('Starting CO\n')
co = pyfits.open(fl['COplanck'])
conv = (np.pi/180.)
head = pyfits.getheader('CMAP.fits')
l = GenLon(Inputs)
b = GenLat(Inputs)
mmm.write("Opened and generated lat/lon\n")
mapp = np.zeros( (int(l.size),int(b.size)) )
error = np.zeros( (int(l.size),int(b.size)) )
mmm.write("Starting map create\n")
for i in np.arange( int(l.size) ):
for j in np.arange( int(b.size) ):
index = hp.ang2pix(2048,(np.pi/2.)-(conv*b[j]),(conv*l[i]),nest=True)
mapp[i,j] = co[1].data[index][0]
error[i,j] = co[1].data[index][1]
mmm.write('Finished map\n')
co = None
del co
radx = ((Inputs['nxpix'][1]*Inputs['binsz'][1])/2.0) + (6./2.0)
rady = ((Inputs['nypix'][1]*Inputs['binsz'][1])/2.0) + (6./2.0)
pxcen = radx/Inputs['binsz'][1]
pycen = rady/Inputs['binsz'][1]
# ADD IN THIS CORRECTION FACTOR, FOUND IN http://arxiv.org/abs/1501.03606
# :: Divide by 1.16 to remove 13CO contamination
mmm.write('Writing CO(J=1-0) map\n')
maps = pyfits.PrimaryHDU( np.transpose(mapp)/1.16 )
maps.header['CTYPE1'] = ('GLON-CAR')
maps.header['CRPIX1'] = ( pxcen, 'Reference Pixel')
maps.header['CRVAL1'] = (Inputs['glon'][1], 'Galactic longitude at reference pixel')
maps.header['CDELT1'] = (-Inputs['binsz'][1],'x-axis increment per pixel')
maps.header['CUNIT1'] = ('deg','Physical Units for x-axis')
maps.header['CTYPE2'] = ('GLAT-CAR')
maps.header['CRPIX2'] = ( pycen, 'Reference pixel')
maps.header['CRVAL2'] = (Inputs['glat'][1],'Galactic latitude at reference pixel')
maps.header['CDELT2'] = (Inputs['binsz'][1], 'y-axis increment per pixel')
maps.header['CUNIT2'] = ('deg','Physical units for y-axis')
maps.header['Equinox'] = (2000.,'Equinox of coordinates')
maps.writeto(fl['COall'])
mmm.write('Writing error map\n')
err = pyfits.PrimaryHDU( np.transpose(error) )
err.header = maps.header
err.writeto(fl['COer'])
COThresh(Inputs,fl)
CO_Cldkill(Inputs,fl)
if ( (np.absolute(Inputs['glon'][1]) < 15) ):
# Make flat template if necessary
co = pyfits.open(fl['COall'])
co[0].data = np.ones( co[0].data.shape )
co[0].data /= (co[0].data.sum() * (Inputs['binsz'][1]**2)*(np.pi/180.)**2)
co.writeto('../FlatTemp_norm.fits')
mapp = None
error = None
co = None
maps = None
head = None
del mapp, error, co, maps, head
mmm.write('Done CO maps\n')
mmm.close()
print "Done CO maps"
## ##############################################
def COThresh(Inputs,fl):
""" We open the CO map and it's error map from Planck and
set about thresholding and normalizing the map for use.
Right now, it is set to a 2-sigma threshold.
"""
co = pyfits.open(fl['COall'])
err= pyfits.open(fl['COer'])
co[0].data[ co[0].data < 2*err[0].data ] = 0.0
co[0].data[ co[0].data < 0 ] = 0.0
co.writeto(fl['COmpnon'])
norm = co[0].data.sum() * (np.pi/180.)**2 * (Inputs['binsz'][1]**2)
co[0].data /= norm
co.writeto(fl['COmp'])
co = None
err = None
norm = None
del co, err, norm
return "bwoop."
###########################################################################
def CO_Cldkill(Inputs,fl):
""" Take CO map and remove cloud of interest (center of ROI).
"""
l = Inputs['glon'][1]
b = Inputs['glat'][1]
co = pyfits.open(fl['COmpnon'])
xcen = co[0].header['CRPIX1']
ycen = co[0].header['CRPIX2']
co[0].data = np.transpose( co[0].data )
ny = co[0].header['NAXIS2']
nx = co[0].header['NAXIS1']
comsk = np.zeros( (ny,nx) )
comsk[ co[0].data > 0 ] = 1
co[0].data *= comsk
cldmsk = np.zeros( (ny,nx) )
cldmsk[xcen,ycen] = 1
while True:
diff_temp = comsk - cldmsk
cldmsk = ndimage.binary_dilation(cldmsk)
cldmsk *= comsk
diff = comsk - cldmsk
if ( (diff - diff_temp).sum() == 0 ):
break
co[0].data -= cldmsk*co[0].data
co[0].data = np.transpose(co[0].data)
co.writeto(fl['COcldmsk'])
co = None
comsk = None
cldmsk = None
diff = None
diff_temp = None
del co, comsk, cldmsk, diff, diff_temp
return "Removed cloud from CO map."
####################################################################
#def fun_ebv(var):
# ebv = pyfits.open('../EBV_pl.fits')
# tau = pyfits.open('../Dust_.fits')
# return ((ebv[0].data[ebv[0].data<0.2] - var*tau[0].data[ebv[0].data<0.2])**2).sum()
## ##############################################
def Avmap_make(Inputs,fl):
""" Make color excess map from SFD98. Using older, non-HealPix
version. Download of SFD website. Use multiply the E(B-V)
by 3.1 to get Av (assuming Galactic average value of Rv).
01 Dec 2014: using Planck-derived E(B-V)
"""
# Used for SFD map
# if (Inputs['glat'][1] > 0):
# mp = pyfits.open(fl['ndust'])
# else:
# mp = pyfits.open(fl['sdust'])
mp = pyfits.open(fl['Dust_allsky'])
l = GenLon(Inputs)
b = GenLat(Inputs)
Av = np.zeros( (l.size,b.size) )
Av_= np.zeros( (l.size,b.size) )
conv = (np.pi/180.)
# ebv = np.zeros( (l.size,b.size) )
for i in np.arange( int(l.size) ):
for j in np.arange( int(b.size) ):
index = hp.ang2pix(2048,(np.pi/2.)-(conv*b[j]),(conv*l[i]),nest=True)
Av[i,j]= 3.1*mp[1].data[index][0] # tau353
Av_[i,j]=3.1*mp[1].data[index][1] # tau353 error
# ebv[i,j]=3.1*mp[1].data[index][2] # E(B-V)
## Av[i,j] = 3.55*nicest[1].data[int(index)/1024][0][int(index)%1024]
## For the strange ordering . . .
# The above is A_V(tau353)
# Ok, now, least squares fit: tau353 to E(B-V): fit to low color excess?? Haven't done yet
# as of September 1, 2015
radx = ((Inputs['nxpix'][1]*Inputs['binsz'][1])/2.0) + (6./2.0)
rady = ((Inputs['nypix'][1]*Inputs['binsz'][1])/2.0) + (6./2.0)
pxcen = radx/Inputs['binsz'][1]
pycen = rady/Inputs['binsz'][1]
# notice the 3.1 when writing the fits file: need Av, not E(B-V)
# maps = pyfits.PrimaryHDU( 3.1*np.transpose(Av) )
maps = pyfits.PrimaryHDU( 1.49e4*np.transpose(Av) )
maps.header['CTYPE1'] = ('GLON-CAR')
maps.header['CRPIX1'] = ( pxcen, 'Reference Pixel')
maps.header['CRVAL1'] = (Inputs['glon'][1], 'Galactic longitude at reference pixel')
maps.header['CDELT1'] = (-Inputs['binsz'][1],'x-axis increment per pixel')
maps.header['CUNIT1'] = ('deg','Physical Units for x-axis')
maps.header['CTYPE2'] = ('GLAT-CAR')
maps.header['CRPIX2'] = ( pycen, 'Reference pixel')
maps.header['CRVAL2'] = (Inputs['glat'][1],'Galactic latitude at reference pixel')
maps.header['CDELT2'] = (Inputs['binsz'][1], 'y-axis increment per pixel')
maps.header['CUNIT2'] = ('deg','Physical units for y-axis')
maps.header['Equinox'] = (2000.,'Equinox of coordinates')
maps.writeto(fl['Dust'])
#
mapr = pyfits.PrimaryHDU(1.49e4*np.transpose(Av_))
mapr.header = maps.header
mapr.writeto('../Dust_err.fits')
mp = None
Av = None
Av_= None
maps = None
del mp, Av, Av_, maps
## ##############################################
def Dust_resid(Inputs,fl):
""" Make the standard dark gas template, E(B-V)res -- the color
excess which is not explained by HI nor CO. Find the least-
squares fit of HI+CO to the Av map. Remove it. This (generally
positive) residual supposedly accounts for all gas/dust not
traced by HI or CO. """
var = np.array([5e21,1e20])
if (fl['opacity'].lower() == 'thick'):
(R,q) = scipy.optimize.fmin(fun_thick,var)
elif (fl['opacity'].lower() == 'thin'):
(R,q) = scipy.optimize.fmin(fun_thin,var)
h = open('Properties.txt','a')
h.write(' R = %s \n q = %s \n'%(R,q) )
h.close()
dust = pyfits.open(fl['Dust'])
hi1 = pyfits.open(fl['HImp_%snon'%fl['opacity']])
hi2 = pyfits.open(fl['HImp_%snon_gt'%fl['opacity']])
co = pyfits.open(fl['COmpnon'])
co_msk=pyfits.open(fl['COcldmsk'])
Eres = dust[0].data - ( (1.0/(R)) * ((hi1[0].data + hi2[0].data) +
(q*co[0].data)) )
Eres[ Eres < 0.0 ] = 0.0
resmap = pyfits.PrimaryHDU(Eres)
resmap.header = hi1[0].header
resmap.writeto(fl['Ebv_%snon'%fl['opacity']])
norm = Eres.sum() * (np.pi/180)**2 * (Inputs['binsz'][1]**2)
# Now for the Av,res map without the CO-emitting area
co[0].data -= co_msk[0].data
Eres[ co[0].data > 0 ] = 0
resmap = pyfits.PrimaryHDU(Eres)
resmap.header = hi1[0].header
resmap.writeto(fl['Ebvcldmsk'])
Eres /= norm
resmap = pyfits.PrimaryHDU(Eres)
resmap.header = hi1[0].header
resmap.writeto(fl['Ebv_%s'%fl['opacity']])
dust = None
hi1 = None
hi2 = None
co = None
co_msk = None
Eres = None
resmap = None
del dust, hi1, hi2, co, co_msk, Eres, resmap
## ##############################################
def fun_thin(var):
""" Used to make the E(B-V)residual map: optimize this linear
combination of E(B-V), N(HI), and W_CO to find the color excess
that can't be explained by HI or CO. """
dust = pyfits.open('../Dust.fits')
hi1 = pyfits.open('../HI_thin.fits')
hi2 = pyfits.open('../HI_thin_gt.fits')
co = pyfits.open('../CO_temp.fits')
Eres = dust[0].data - ( (1.0/(var[0])) * ((hi1[0].data + hi2[0].data) +
(var[1]*co[0].data)) )
dust = None
hi1 = None
hi2 = None
co = None
del dust, hi1, hi2, co
return (Eres**2).sum()
## ##############################################
def fun_thick(var):
""" Used to make the E(B-V)residual map: optimize this linear
combination of E(B-V), N(HI), and W_CO to find the color excess
that can't be explained by HI or CO. """
dust = pyfits.open('../Dust.fits')
hi1 = pyfits.open('../HI_thick.fits')
hi2 = pyfits.open('../HI_thick_gt.fits')
co = pyfits.open('../CO_temp.fits')
Eres = dust[0].data - ( (1.0/(var[0])) * ((hi1[0].data + hi2[0].data) +
(var[1]*co[0].data)) )
dust = None
hi1 = None
hi2 = None
co = None
del dust, hi1, hi2, co
return (Eres**2).sum()
## ##############################################
def djs_angpos(xval):
""" taken from Schlegel dust map routines Put angles into 0 <= angle < 360"""
if (xval > 0 or xval == 0):
return ( xval%360 )
else:
return ( (xval%360)+360 )
## ##############################################
def Diffuse_std(Inputs,fl,dat,fix):
""" Modify model file to include the standard diffuse sources,
ones that will not change: HI, inverse Compton, and
isotropic emission. HI assumes a broken power law 2
spectrum. Unlike some other Fermi papers, now (Nov.20, 2014)
the isotropic component is left free. """
# HI scale was 1e-8, now 1e-27
# dat.append('<source name="HI" type="DiffuseSource">\n')
# dat.append('\t<spectrum type="PowerLaw2">\n')
# dat.append('\t\t<parameter free="%s" max="1e5" min="1e-6" name="Integral" scale="1e-27" value="1"/>\n'%int(fix))
# dat.append('\t\t<parameter free="%s" max="1" min="-4" name="Index" scale="1.0" value="-2"/>\n'%int(fix))
# dat.append('\t\t<parameter free="0" max="200000.0" min="20.0" name="LowerLimit" scale="1.0" value="%s"/>\n'% Inputs['emin'][1])
# dat.append('\t\t<parameter free="0" max="200000.0" min="20.0" name="UpperLimit" scale="1.0" value="%s"/>\n'% Inputs['emax'][1])
# dat.append('\t</spectrum>\n')
# dat.append('\t<spatialModel file="%s" type="SpatialMap">\n'%(fl['HImp_%snon'%fl['opacity']]))
# dat.append('\t\t<parameter free="0" max="1e3" min="1e-3" name="Prefactor" scale="1.0" value="1.0"/>\n')
# dat.append('\t</spatialModel>\n')
# dat.append('</source>\n')
dat.append('<source name="HI" type="DiffuseSource">\n')
dat.append('<!-- point source units are cm^-2 s^-1 MeV^-1 -->\n')
if ( fl['gas_spec'].lower() == 'BPL2' ):
dat.append('\t<spectrum type="BrokenPowerLaw2">\n')
dat.append('\t\t<parameter free="1" max="1000.0" min="0.001" name="Integral" scale="1e-27" value="1.0"/>\n')
dat.append('\t\t<parameter free="0" max="-1.0" min="-5.0" name="Index1" scale="1.0" value="-1.8"/>\n')
dat.append('\t\t<parameter free="1" max="-1.0" min="-5.0" name="Index2" scale="1.0" value="-2.8"/>\n')
dat.append('\t\t<parameter free="1" max="5000.0" min="500.0" name="BreakValue" scale="1.0" value="1200.0"/>\n')
dat.append('\t\t<parameter free="0" max="1000000.0" min="20.0" name="LowerLimit" scale="1.0" value="%s"/>\n'% Inputs['emin'][1])
dat.append('\t\t<parameter free="0" max="1000000.0" min="20.0" name="UpperLimit" scale="1.0" value="%s"/>\n'% Inputs['emax'][1])
elif ( fl['gas_spec'].lower() == 'spec' ):
dat.append('\t<spectrum file="/home/abrahams/HICO_survey/SourceSearch/Analysis/LIS_cas.txt" type="FileFunction">\n')
dat.append('\t\t<parameter free="1" max="10" min="0.01" name="Normalization" scale="1" value="1" />\n')
dat.append('\t</spectrum>\n')
dat.append('\t<spatialModel file="%s" type="SpatialMap">\n'%(fl['HImp_%snon'%fl['opacity']]))
dat.append('\t\t<parameter free="0" max="1e3" min="1e-3" name="Prefactor" scale="1.0" value="1.0"/>\n')
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
#### HI far #### HI_far scale
dat.append('<source name="HI_far" type="DiffuseSource">\n')
if ( fl['gas_spec'].lower() == 'BPL2' ):
dat.append('\t<spectrum type="PowerLaw2">\n')
dat.append('\t\t<parameter free="1" max="1e5" min="1e-6" name="Integral" scale="1e-27" value="1"/>\n')
dat.append('\t\t<parameter free="1" max="1" min="-4" name="Index" scale="1.0" value="-2"/>\n')
dat.append('\t\t<parameter free="0" max="1000000.0" min="20.0" name="LowerLimit" scale="1.0" value="%s"/>\n'% Inputs['emin'][1])
dat.append('\t\t<parameter free="0" max="1000000.0" min="20.0" name="UpperLimit" scale="1.0" value="%s"/>\n'% Inputs['emax'][1])
elif ( fl['gas_spec'].lower() == 'spec' ):
dat.append('\t<spectrum file="/home/abrahams/HICO_survey/SourceSearch/Analysis/LIS_cas.txt" type="FileFunction">\n')
dat.append('\t\t<parameter free="1" max="10" min="0.01" name="Normalization" scale="1" value="1" />\n')
dat.append('\t</spectrum>\n')
dat.append('\t<spatialModel file="%s" type="SpatialMap">\n'%(fl['HImp_%snon_gt'%fl['opacity']]))
dat.append('\t\t<parameter free="0" max="1e3" min="1e-3" name="Prefactor" scale="1.0" value="1.0"/>\n')
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
#### IC ####
dat.append('<source name="IC" type="DiffuseSource">\n')
dat.append('\t<spectrum type="ConstantValue">\n')
dat.append('\t\t<parameter free="1" max="100.0" min="1e-6" name="Value" scale="1.0" value="1.0"/>\n')
dat.append('\t</spectrum>\n')
dat.append('\t<spatialModel file="%s" type="MapCubeFunction">\n'% fl['IC'])
dat.append('\t\t<parameter free="0" max="1e3" min="1e-3" name="Normalization" scale="1.0" value="1.0"/>\n')
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
#### iso ####
dat.append('<source name="iso_P8R2_SOURCE_V6_v06" type="DiffuseSource">\n')
dat.append('\t<spectrum file="%s" type="FileFunction">\n'% fl['iso'])
dat.append('\t\t<parameter free="%s" max="10" min="1e-2" name="Normalization" scale="1" value="1"/>\n'%int(fix))
dat.append('\t</spectrum>\n')
dat.append('\t<spatialModel type="ConstantValue">\n')
dat.append('\t\t<parameter free="0" max="10.0" min="0.0" name="Value" scale="1.0" value="1.0"/>\n')
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
return dat
## ##############################################
def Diffuse_iso(Inputs,fl,dat,fix):
""" Modify model file to include the isotropic emission. """
#### iso ####
dat.append('<source name="iso_P8R2_SOURCE_V6_v06" type="DiffuseSource">\n')
dat.append('\t<spectrum file="%s" type="FileFunction">\n'% fl['iso'])
dat.append('\t\t<parameter free="%s" max="10" min="1e-2" name="Normalization" scale="1" value="1"/>\n'%int(fix))
dat.append('\t</spectrum>\n')
dat.append('\t<spatialModel type="ConstantValue">\n')
dat.append('\t\t<parameter free="0" max="10.0" min="0.0" name="Value" scale="1.0" value="1.0"/>\n')
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
return dat
## ##############################################
def Diffuse_CO(Inputs,fl,dat,fix,cld):
""" Modify model file to include diffuse emission from CO
Inputs: standard analysis inputs
fl : file names for analysis
dat : model xml string list """
# CO scale was 1e-8, now 1e-7
dat.append('<source name="CO" type="DiffuseSource">\n')
if ( fl['gas_spec'].lower() == 'spec' ):
dat.append('\t<spectrum file="/home/abrahams/HICO_survey/SourceSearch/Analysis/qCO_cas.txt" type="FileFunction">\n')
dat.append('\t\t<parameter free="1" max="10" min="0.01" name="Normalization" scale="1" value="1" />\n')
dat.append('\t</spectrum>\n')
else:
dat.append('\t<spectrum type="PowerLaw2">\n')
dat.append('\t\t<parameter free="%s" max="1e5" min="1e-6" name="Integral" scale="1e-6" value="1"/>\n'%int(fix))
dat.append('\t\t<parameter free="%s" max="1" min="-4" name="Index" scale="1.0" value="-2"/>\n'%int(fix))
dat.append('\t\t<parameter free="0" max="1000000.0" min="20.0" name="LowerLimit" scale="1.0" value="%s"/>\n'% Inputs['emin'][1])
dat.append('\t\t<parameter free="0" max="1000000.0" min="20.0" name="UpperLimit" scale="1.0" value="%s"/>\n'% Inputs['emax'][1])
dat.append('\t</spectrum>\n')
if ( cld == True ):
dat.append('\t<spatialModel file="%s" type="SpatialMap">\n'%(fl['COmpnon']))
elif ( cld == False ):
dat.append('\t<spatialModel file="%s" type="SpatialMap">\n'%(fl['COmcldmsk']))
dat.append('\t\t<parameter free="0" max="1e3" min="1e-3" name="Prefactor" scale="1.0" value="1.0"/>\n')
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
return dat
## ##############################################
def Diffuse_DG(Inputs,fl,dat,fix,cld):
""" Modify model file to include diffuse emission from dark gas
Inputs: standard analysis inputs
fl : file names for analysis
dat : model xml string list """
# DG scale was 1e-8, now 1e-5
dat.append('<source name="E(B-V)" type="DiffuseSource">\n')
if ( fl['gas_spec'].lower() == 'spec' ):
dat.append('\t<spectrum file="/home/abrahams/HICO_survey/SourceSearch/Analysis/qdg_cas.txt" type="FileFunction">\n')
dat.append('\t\t<parameter free="1" max="10" min="0.01" name="Normalization" scale="1" value="1" />\n')
dat.append('\t</spectrum>\n')
else:
dat.append('\t<spectrum type="PowerLaw2">\n')
dat.append('\t\t<parameter free="%s" max="1e5" min="1e-6" name="Integral" scale="1e-5" value="1"/>\n'%int(fix))
dat.append('\t\t<parameter free="%s" max="1" min="-4" name="Index" scale="1.0" value="-2"/>\n'%int(fix))
dat.append('\t\t<parameter free="0" max="1000000.0" min="20.0" name="LowerLimit" scale="1.0" value="%s"/>\n'% Inputs['emin'][1])
dat.append('\t\t<parameter free="0" max="1000000.0" min="20.0" name="UpperLimit" scale="1.0" value="%s"/>\n'% Inputs['emax'][1])
dat.append('\t</spectrum>\n')
if ( cld ):
dat.append('\t<spatialModel file="%s" type="SpatialMap">\n'%(fl['Ebv_%snon'%fl['opacity']]))
else:
dat.append('\t<spatialModel file="%s" type="SpatialMap">\n'%(fl['Ebvcldmsk']))
dat.append('\t\t<parameter free="0" max="1e3" min="1e-3" name="Prefactor" scale="1.0" value="1.0"/>\n')
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
return dat
## ##############################################
def Diffuse_ptsrc(Inputs,fl,dat,glon,glat,cnt):
""" Modify model file to include additional point source
Inputs: standard analysis inputs
fl : file names for analysis
dat : model xml string list
glon : galactic longitude of source
glat : galactic latitude of source"""
(RA,dec) = astCoords.convertCoords("GALACTIC","J2000",glon,glat,2000)
dat.append('<source name="Add_%s" type="PointSource">\n'%cnt)
dat.append('\t<spectrum type="PowerLaw2">\n')
dat.append('\t\t<parameter free="1" max="100000" min="1e-06" name="Integral" scale="1e-9" value="7" />\n')
dat.append('\t\t<parameter free="1" max="1" min="-4" name="Index" scale="1" value="-2.2" />\n')
dat.append('\t\t<parameter free="0" max="1000000" min="20" name="LowerLimit" scale="1" value="%s" />\n'%Inputs['emin'][1])
dat.append('\t\t<parameter free="0" max="1000000" min="20" name="UpperLimit" scale="1" value="%s" />\n'%Inputs['emax'][1])
dat.append('\t</spectrum>\n')
dat.append('\t<spatialModel type="SkyDirFunction">\n')
dat.append('\t\t<parameter free="0" max="360" min="-360" name="RA" scale="1" value="%s" />\n'%RA)
dat.append('\t\t<parameter free="0" max="90" min="-90" name="DEC" scale="1" value="%s" />\n'%dec)
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
return dat
## ##############################################
def Mdl_Make(Inputs,fl,directory):
""" Make a model XML file from "make3FGLxml" and then modifying it to include
the HI, CO, Av_residual maps as backgrounds
Input: input data, ROI position, energy range, etc.
fl : list of file names from SelectLoad.FileNames
key : all/NoCODG/Ptsrc -- make model with or without CO/dark gas template
or with a point source replacing the CO and dark gas templates."""
import numpy as np
import os
mdd = fl['mode'].lower()
if ( fl['MODEL_%s'%mdd] in os.listdir('.') ):
print "Model already made. what?"
return "Model already made. what?"
if ( fl['mode'] == 'orig' ):
return "No need for now\n\n\n bwoop."
if ( fl['mode'].lower() == 'all' ):
try:
f = open('MODEL.xml','r')
except:
print "No model!"
else:
try:
f = open('/home/abrahams/HICO_survey/SourceSearch/l%sb%s/ebin250_10000/'%(int(Inputs['glon'][1]),int(Inputs['glat'][1]))+fl['outmdl2_all'],'r')
print "Yes NewMinuit fit."
print "In %s"%os.listdir('.')
except:
try:
f = open(fl['outmdl1_all'],'r')
print "Pooping, no %s"%fl['outmdl1_all']
except:
f = open('MODEL.xml','r')
print "Nothing fit, nothing to see here."
dat = f.readlines()
f.close()
# i = 0
# while (i < 19 ):
# del dat[-1]
# i += 1
del dat[-1]
for i in np.arange( len(dat) ):
try:
if ( ('gll_iem' in dat[i]) ):
del dat[i:i+10]
break
except:
pass
for i in np.arange( len(dat) ):
try:
if ( 'iso_P8R2' in dat[i] ):
del dat[i:i+8]
break
except:
pass
# Added Dec.2, 2015:
# Give proper LMC template
# ind = np.where( np.array(dat) == '<source name="LMC" type="DiffuseSource">\n' )
smcdir = '/home/abrahams/Tools-Old/Tools-April2012/ScienceTools-v9r27p1-fssc-20120410-i686-pc-linux-gnu-libc2.5/i686-pc-linux-gnu-libc2.5/refdata/fermi/genericSources/Templates/'
for i in range( len(dat) ):
if ( 'LMC.fits' in dat[i] ):
smcnm = smcdir + 'LMC.fits'
dat[i] = '\t<spatialModel file="%s" type="SpatialMap">\n'%(smcnm)
elif ( 'SMC.fits' in dat[i] ):
smcnm = smcdir + 'SMC.fits'
dat[i] = '\t<spatialModel file="%s" type="SpatialMap">\n'%(smcnm)
# Now to the separate cases
if ( mdd == 'all' ):
dat = Diffuse_std(Inputs,fl,dat,1)
dat = Diffuse_CO(Inputs,fl,dat,1,True)
dat = Diffuse_DG(Inputs,fl,dat,1,True)
elif ( mdd == "nocodg" ):
try:
os.mkdir( '%s'%mdd )
except:
pass
dat = Diffuse_iso(Inputs,fl,dat,1)
for i in np.arange( len(dat) ):
if ('name="CO"' in dat[i]):
if ( 'type="PowerLaw2"' in dat[i+1] ):
del dat[i:i+11]
break
elif ('type="BrokenPowerLaw2"' in dat[i+1]):
del dat[i:i+13]
break
elif ( 'type="FileFunction"' in dat[i+1]):
del dat[i:i+8]
break
for i in np.arange( len(dat) ):
if ('E(B-V)' in dat[i]):
if ('type="PowerLaw2"' in dat[i+1]):
del dat[i:i+11]
break
elif ('type="BrokenPowerLaw2"' in dat[i+1]):
del dat[i:i+13]
break
elif ( 'type="FileFunction"' in dat[i+1]):
del dat[i:i+8]
break
# dat = Diffuse_std(Inputs,fl,dat,1)
elif ( mdd == "ptsrc" ):
try:
os.mkdir( '%s'%mdd )
except:
pass
# (glon,glat) = COPtsrc_coords(Inputs,fl)
# dat = Diffuse_std(Inputs,fl,dat,1)
glon = Inputs['glon'][1]
glat = Inputs['glat'][1]
dat = Diffuse_ptsrc(Inputs,fl,dat,glon,glat,0)
dat = Diffuse_iso(Inputs,fl,dat,1)
for i in np.arange( len(dat) ):
if ('CO' in dat[i]):
if ( 'type="PowerLaw2"' in dat[i+1] ):
del dat[i:i+11]
break
elif ('type="BrokenPowerLaw2"' in dat[i+1]):
del dat[i:i+13]
break
elif ( 'type="FileFunction"' in dat[i+1]):
del dat[i:i+8]
break
for i in np.arange( len(dat) ):
if ('E(B-V)' in dat[i]):
if ( 'type="PowerLaw2"' in dat[i+1] ):
del dat[i:i+11]
break
elif ('type="BrokenPowerLaw2"' in dat[i+1]):
del dat[i:i+13]
break
elif ( 'type="FileFunction"' in dat[i+1]):
del dat[i:i+8]
break
elif( mdd == 'nodg' ):
try:
os.mkdir( '%s'%mdd )
except:
pass
# dat = Diffuse_std(Inputs,fl,dat,1)
# dat = Diffuse_CO(Inputs,fl,dat,1)
dat = Diffuse_iso(Inputs,fl,dat,1)
for i in np.arange( len(dat) ):
if ('E(B-V)' in dat[i]):
if ( 'type="PowerLaw2"' in dat[i+1] ):
del dat[i:i+11]
break
elif ('type="BrokenPowerLaw2"' in dat[i+1]):
del dat[i:i+13]
break
elif ( 'type="FileFunction"' in dat[i+1]):
del dat[i:i+8]
break
elif( mdd == 'noco' ):
try:
os.mkdir( '%s'%mdd )
except:
pass
dat = Diffuse_iso(Inputs,fl,dat,1)
# dat = Diffuse_std(Inputs,fl,dat,1)
# dat = Diffuse_DG(Inputs,fl,dat,1)
for i in np.arange( len(dat) ):
if ('CO' in dat[i]):
if ( 'type="PowerLaw2"' in dat[i+1] ):
del dat[i:i+11]
break
elif ('type="BrokenPowerLaw2"' in dat[i+1]):
del dat[i:i+13]
break
elif ( 'type="FileFunction"' in dat[i+1]):
del dat[i:i+8]
break
elif ( mdd == "all_plus" ):
try:
os.mkdir( '%s'%mdd )
except:
pass
# (glon,glat) = COPtsrc_coords(Inputs,fl)
# dat = Diffuse_std(Inputs,fl,dat,1)
# dat = Diffuse_CO(Inputs,fl,dat,1)
# dat = Diffuse_DG(Inputs,fl,dat,1)
glon = Inputs['glon'][1]
glat = Inputs['glat'][1]
# dat = Diffuse_std(Inputs,fl,dat,1)
# dat = Diffuse_CO(Inputs,fl,dat,1,True)
# dat = Diffuse_DG(Inputs,fl,dat,1,True)
dat = Diffuse_ptsrc(Inputs,fl,dat,glon,glat,0)
dat = Diffuse_iso(Inputs,fl,dat,0)
elif ( (mdd == 'nocld') ):
## Was failing because "directory already exists"
try:
os.mkdir( '%s'%mdd )
except:
pass
dat = Diffuse_iso(Inputs,fl,dat,1)
for i in np.arange( len(dat) ):
if ( 'name="CO"' in dat[i] ):
while ('</source>' not in dat[i]):
i += 1
if ( 'type="SpatialMap"' in dat[i] ):
dat[i] = '\t<spatialModel file="%s" type="SpatialMap">\n'%fl['COcldmsk']
elif ( 'name="E(B-V)"' in dat[i] ):
while ('</source>' not in dat[i]):
i += 1
if ( 'type="SpatialMap"' in dat[i] ):
dat[i] = '\t<spatialModel file="%s" type="SpatialMap">\n'%fl['Ebvcldmsk']
elif ( (mdd =='fx_src') ):
try:
os.mkdir( '%s'%mdd )
except:
pass
dat = Diffuse_iso(Inputs,fl,dat,0)
# dat = Diffuse_std(Inputs,fl,dat,0)
# dat = Diffuse_CO(Inputs,fl,dat,0)
# dat = Diffuse_DG(Inputs,fl,dat,0)
# os.system('cp mdl2_all.xml %s/%s'%(mdd,fl['MODEL_power_law']))
elif ( mdd == 'power_law' ):
os.mkdir('%s'%mdd)
# os.system('cp mdl2_all.xml %s/%s'%(mdd,fl['MODEL_power_law']))
return 0
if ( (np.absolute(Inputs['glon'][1]) < 15) and (mdd == 'all')):
dat.append('<source name="Flat" type="DiffuseSource">\n')
dat.append('\t<spectrum file="/home/abrahams/HICO_survey/SourceSearch/Analysis/fermi_bubbles_spectra.txt" type="FileFunction">\n')
dat.append('\t\t<parameter free="1" max="10" min="0.01" name="Normalization" scale="1" value="1" />\n')
dat.append('\t</spectrum>\n')
dat.append('\t<spectrum type="PowerLaw2">\n')
dat.append('\t\t<parameter free="%s" max="1e5" min="1e-6" name="Integral" scale="1e-7" value="1"/>\n'%int(1))
dat.append('\t\t<parameter free="%s" max="1" min="-4" name="Index" scale="1.0" value="-2"/>\n'%int(1))
dat.append('\t\t<parameter free="0" max="200000.0" min="20.0" name="LowerLimit" scale="1.0" value="%s"/>\n'% Inputs['emin'][1])
dat.append('\t\t<parameter free="0" max="200000.0" min="20.0" name="UpperLimit" scale="1.0" value="%s"/>\n'% Inputs['emax'][1])
dat.append('\t</spectrum>\n')
dat.append('\t<spatialModel file="%s" type="SpatialMap">\n'%('/home/abrahams/HICO_survey/SourceSearch/l%sb%s/FlatTemp_norm.fits'%(int(Inputs['glon'][1]),int(Inputs['glat'][1]))))
dat.append('\t\t<parameter free="0" max="1e3" min="1e-3" name="Prefactor" scale="1.0" value="1.0"/>\n')
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
dat.append('</source_library>')
g = open(fl['MODEL_%s'%mdd],'w')
g.writelines(dat)
g.close()
return 0
## ##############################################
def Mdl_Gas(Inputs,fl,MDL):
""" Make a model file of just the individual gas components
Input: input data, ROI position, energy range, etc.
fl : list of file names from SelectLoad.FileNames
MDL : tell me which component I am looking at"""
if ( fl['mode'] == 'orig' ):
return "No need for now\n\n\n bwoop."
dat = ['<?xml version="1.0" standalone="no"?>']
dat.append('<source_library title="source library">')
# Now to the separate cases: CO+DG, no CO+DG, no CO+DG with point source #
if ( MDL.lower() == 'hi' ):
dat.append('<source name="HI" type="DiffuseSource">\n')
dat.append('\t<spectrum type="PowerLaw2">\n')
dat.append('\t\t<parameter free="1" max="1e5" min="1e-6" name="Integral" scale="2e-7" value="1"/>\n')
dat.append('\t\t<parameter free="1" max="1" min="-4" name="Index" scale="1.0" value="-2"/>\n')
dat.append('\t\t<parameter free="0" max="200000.0" min="20.0" name="LowerLimit" scale="1.0" value="%s"/>\n'% Inputs['emin'][1])
dat.append('\t\t<parameter free="0" max="200000.0" min="20.0" name="UpperLimit" scale="1.0" value="%s"/>\n'% Inputs['emax'][1])
dat.append('\t</spectrum>\n')
dat.append('\t<spatialModel file="%s" type="SpatialMap">\n'%(fl['HImp_%s'%fl['opacity']]))
dat.append('\t\t<parameter free="0" max="1e3" min="1e-3" name="Prefactor" scale="1.0" value="1.0"/>\n')
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
elif( MDL.lower() == 'hi_far' ):
dat.append('<source name="HI_far" type="DiffuseSource">\n')
dat.append('\t<spectrum type="PowerLaw2">\n')
dat.append('\t\t<parameter free="1" max="1e5" min="1e-6" name="Integral" scale="2e-8" value="1"/>\n')
dat.append('\t\t<parameter free="1" max="1" min="-4" name="Index" scale="1.0" value="-2"/>\n')
dat.append('\t\t<parameter free="0" max="200000.0" min="20.0" name="LowerLimit" scale="1.0" value="%s"/>\n'% Inputs['emin'][1])
dat.append('\t\t<parameter free="0" max="200000.0" min="20.0" name="UpperLimit" scale="1.0" value="%s"/>\n'% Inputs['emax'][1])
dat.append('\t</spectrum>\n')
dat.append('\t<spatialModel file="%s" type="SpatialMap">\n'%(fl['HImp_%s_gt'%fl['opacity']]))
dat.append('\t\t<parameter free="0" max="1e3" min="1e-3" name="Prefactor" scale="1.0" value="1.0"/>\n')
dat.append('\t</spatialModel>\n')
dat.append('</source>\n')
elif( MDL.lower() == 'co' ):
dat = Diffuse_CO(Inputs,fl,dat)
elif( MDL.lower() == 'dg' ):
dat = Diffuse_DG(Inputs,fl,dat)
dat.append('</source_library>')
g = open(fl['MODEL_%s'%MDL.lower()],'w')
g.writelines(dat)
g.close()
return 0
## ##############################################
def COPtsrc_coords(Inputs,fl):
""" We want to replace the CO/DG templates with point sources.
Wherever a large clump of CO is found, replace with a
point source. Return coordinates of location of CO max
close to the center of the ROI.
We smooth the CO map with a Gaussian filter to average over
noise and then threshold. """
im = pyfits.open(fl['COmp'])
im[0].data = np.transpose( im[0].data )
im2= ndimage.gaussian_filter(im[0].data, sigma=4)
dist = np.zeros( (im[0].header['CRPIX2'],im[0].header['CRPIX1']) )
for i in np.arange( im[0].header['CRPIX1']):
np.sqrt( (i - im[0].header['CRPIX1'])**2 +
(np.arange(im[0].header['CRPIX2']) - im[0].header['CRPIX2'])**2 )
im2[dist > 50] = 0 # Kill CO outside 5 degrees or so
max_ = np.where( im2 == np.max(im2) )
wcs = astWCS.WCS(im[0].header, mode="pyfits")