-
Notifications
You must be signed in to change notification settings - Fork 0
/
temper_fits_routines.i
1436 lines (1070 loc) · 39.3 KB
/
temper_fits_routines.i
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
# include "ieee.i"
# include "lmfit.i"
NaN = array(float,1) ; ieee_set, NaN,2 ; NaN = NaN(1) ;
_wcs_bin_dir = "~/Software/wcstools-3.8.7/bin/" ;
//*************************************************************************
func define_opacity(wl,P) {
// Define opacity law in m^2.kg^-1 of gas
extern _kappa, _kappa0, _l0, _fact ;
// Draine at 0.03 produit kappa = 0.2 cm^2/g de poussiere = 2e-4 em m^2.kg^-1 of gas
_kappa0 = 0.1 * 1e-3 ; _l0 = 1.2e-3 ;
// 1m^2/kg = 10 cm^2/g
//for consistency with temper_fits.i and Philippe's code.
_kappa0 = 0.1 * 1e-1; // 1 cm^2/g = 0.1 m^2/kg
write, "Setting nu0 to 1000Ghz, kappa0 to ",_kappa0 * 10," (cm^2.g^-1)" ;
nu0 = 1000e9 ; // Hz ;
_l0 = SI.c / nu0 ;
P.kappa0 = _kappa0 ; P.nu0 = nu0 ;
_kappa = _kappa0 * (wl/_l0)^-2 ;
_fact = _kappa * SI.Msun / ((P.distance*SI.pc)^2) ;
}
//*************************************************************************
func zero_to_NaN(img) {
// On mets les NaN a 0 d'abord
ou = where(ieee_test(img)) ;
if (numberof(ou)) {
img(ou) = 0. ;
}
// On fait le test sur 0
ou = where(abs(img)<1e-300) ;
NaN = array(float,1) ; ieee_set, NaN,2 ; NaN = NaN(1) ;
if (numberof(ou)) {
img(ou) = NaN ;
}
}
//*************************************************************************
func NaN_to_zero(img) {
// Warning : not a function
ou = where(ieee_test(img)) ;
if (numberof(ou)) {
img(ou) = 0. ;
}
}
//*************************************************************************
func get_beam(conf,scan_speed) {
/* DOCUMENT beam(conf,scan_speed)
Returns instrument beam size in arcsec
SEE ALSO:
*/
beam_size = [] ;
if (conf.instrument=="PACS" || conf.instrument=="SPIRE") {
if (abs(scan_speed - 10) < 1e-3) {
if (abs(conf.lamb - 70) < 1e-3) beam_size = 5.4 ;
if (abs(conf.lamb - 100) < 1e-3) beam_size = 6.7 ;
if (abs(conf.lamb - 160) < 1e-3) beam_size = 11.2 ;
}
if (abs(scan_speed - 20) < 1e-3) {
if (abs(conf.lamb - 70) < 1e-3) beam_size = 5.6 ;
if (abs(conf.lamb - 100) < 1e-3) beam_size = 6.8 ;
if (abs(conf.lamb - 160) < 1e-3) beam_size = 11.4 ;
}
if (abs(scan_speed - 60) < 1e-3) {
if (abs(conf.lamb - 70) < 1e-3) beam_size = 8.4 ;
if (abs(conf.lamb - 100) < 1e-3) beam_size = 9.4 ;
if (abs(conf.lamb - 160) < 1e-3) beam_size = 13.5 ;
}
if (abs(conf.lamb - 250) < 1e-3) beam_size = 18.1 ;
if (abs(conf.lamb - 350) < 1e-3) beam_size = 25.2 ;
if (abs(conf.lamb - 500) < 1e-3) beam_size = 36.9 ;
if (is_void(beam_size)) {
"ERROR: cannot obtain beam_size. Exiting." ;
error ;
}
} else if (conf.instrument=="SABOCA" || conf.instrument=="LABOCA") {
// Reads the fits file and pixel size
image = cfitsio_open(conf.file, fh) ;
// Beam size (circular for APEX)
beam_size = cfitsio_get(fh,"BMAJ") * 3600.; // en arcsec
if (is_void(beam_size)) {
"ERROR: cannot obtain beam_size. Exiting." ;
error ;
}
}
return beam_size ;
}
//*************************************************************************
func read_Herschel(filename,instrument,wavelength,scan_speed,&pix_size,&beam_size,&conversion_factor,&med) {
/* DOCUMENT read_Herschel(filename,instrument,wavelength,scan_speed,&pix_size,&beam_size,&conversion_factor,&med)
Reads a fits file filename containing Herschel data
Convert the flux to MJy/sr assuming PACS data is in Jy/pixel and SPIRE data in Jy/beam if the info is not
instrument = "PACS" or "SPIRE"
Returns:
-- Herschel beam size in arcsec
-- pixel size in arcsec
SEE ALSO:
*/
// Obtain beam_size as a function of wavelength and scan_speed
beam_size = [] ;
if (abs(scan_speed - 10) < 1e-6) {
if (abs(wavelength - 70e-6) < 1e-6) beam_size = 5.4 ;
if (abs(wavelength - 100e-6) < 1e-6) beam_size = 6.7 ;
if (abs(wavelength - 160e-6) < 1e-6) beam_size = 11.2 ;
}
if (abs(scan_speed - 20) < 1e-6) {
if (abs(wavelength - 70e-6) < 1e-6) beam_size = 5.6 ;
if (abs(wavelength - 100e-6) < 1e-6) beam_size = 6.8 ;
if (abs(wavelength - 160e-6) < 1e-6) beam_size = 11.4 ;
}
if (abs(scan_speed - 60) < 1e-6) {
if (abs(wavelength - 70e-6) < 1e-6) beam_size = 8.4 ;
if (abs(wavelength - 100e-6) < 1e-6) beam_size = 9.4 ;
if (abs(wavelength - 160e-6) < 1e-6) beam_size = 13.5 ;
}
if (abs(wavelength - 250e-6) < 1e-6) beam_size = 18.1 ;
if (abs(wavelength - 350e-6) < 1e-6) beam_size = 25.2 ;
if (abs(wavelength - 500e-6) < 1e-6) beam_size = 36.9 ;
if (is_void(beam_size)) {
"ERROR: cannot obtain beam_size. Exiting." ;
error ;
}
// Reads the fits file and pixel size
fh = cfitsio_open(filename) ;
image = cfitsio_read_image(fh) ;
if (is_void(image)) {
"ERROR : can't read fits file. Exiting" ;
"Is it a single HDU file ?" ;
error ;
}
dims = dimsof(image) ;
if ((dimsof(image))(1) > 2) image = image(,,1) ; // only considering the 1st plane
//if(is_void(image)) {
// fits_goto_hdu, fh, 2;
// fits_list, fh ;
// image = fits_read_array(fh) ;
// if (is_void(image)) {
// "ERROR : can't read fits file. Exiting" ;
// error ;
// }
//}
if (typeof(image(1)) == "double") image = float(image) ;
CDELT1 = cfitsio_get(fh,"CDELT1") ;
if (is_void(CDELT1)) CDELT1 = cfitsio_get(fh,"CD1_1") ;
pix_size = abs(CDELT1) * 3600.; // en arcsec
write, "Number of pixels", dimsof(image)(2), "x", dimsof(image)(3) ;
write, "pixel_scale=", pix_size,"arcsec" ;
// Conversion en MJy/sr
BUNIT = cfitsio_get(fh,"BUNIT") ;
if (is_void(BUNIT)) BUNIT = cfitsio_get(fh,"QTTY____") ;
if (BUNIT == "MJy/sr") {
write, "Data in MJy/sr, no conversion needed" ;
conversion_factor = 1.0 ;
} else {
if (instrument == "PACS") {
// Jy/pixel --> MJy/sr
write, "Assuming PACS data is in Jy/pixel" ;
write, "Converting Jy/pixel to MJy/sr";
conversion_factor = 1.0 / (1e6 * (pix_size * SI.as)^2) ;
} else if (instrument == "SPIRE") {
// Jy/beam --> MJy/sr
// 1.13309 * thetaA^2 is the integral of a Gaussian beam
// where thetaA is the HPBW (Half-Power Beamwidth)
write, "Assuming SPIRE data is in Jy/beam" ;
write, "Converting Jy/beam to MJy/sr";
conversion_factor = 1.0 / (1e6 * (beam_size * SI.as)^2) * pi/4./log(2.) ;
}
}
write, "conversion factor = ", conversion_factor ;
cfitsio_close, fh ;
image *= float(conversion_factor) ;
ou = where(!ieee_test(image)) ;
// remove median for speed
write, "Computing image median ..." ;
//med = median(image(ou)) ;
write, "Skipping median of image ..." ;
med = 0
write, "measured median [MJy/sr]", med ;
return image ;
}
//*************************************************************************
func read_APEX(filename,instrument,&pix_size,&beam_size,&conversion_factor) {
/* DOCUMENT read_APEX(filename,instrument,&pix_size,&beam_size,&conversion_factor)
Reads a fits file filename containing APEX (SABOCA or LABOCA) data
Convert the flux to Mjy/sr assuming PACS data is in Jy/beam
instrument = "SABOCA" or "LABOCA"
Returns:
-- beam size in arcsec
-- pixel size in arcsec
SEE ALSO:
*/
// Reads the fits file and pixel size
image = cfitsioRead(filename, fh) ;
if (is_void(image)) {
"ERROR : can't read fits file. Exiting" ;
"Is it a single HDU file ?" ;
error ;
}
dims = dimsof(image) ;
if ((dimsof(image))(1) > 2) image = image(,,1) ; // only considering the 1st plane
if (typeof(image(1)) == "double") image = float(image) ;
CDELT1 = cfitsio_get(fh,"CDELT1") ;
if (is_void(CDELT1)) CDELT1 = cfitsio_get(fh,"CD1_1") ;
pix_size = abs(CDELT1) * 3600.; // en arcsec
write, "Number of pixels", dimsof(image)(2), "x", dimsof(image)(3) ;
write, "pixel_scale=", pix_size,"arcsec" ;
// Beam size (circular for APEX)
beam_size = cfitsio_get(fh,"BMAJ") * 3600.; // en arcsec
if (is_void(beam_size)) {
"ERROR: cannot obtain beam_size. Exiting." ;
error ;
}
write, "beam_size=", beam_size,"arcsec" ;
// Conversion en MJy/sr
// Jy/beam --> MJy/sr
// 1.13309 * thetaA^2 is the integral of a Gaussian beam
// where thetaA is the HPBW (Half-Power Beamwidth)
BUNIT = cfitsio_get(fh,"BUNIT") ;
if (BUNIT == "MJy/sr") {
write, "Data in MJy/sr, no conversion needed" ;
factor = 1.0 ;
} else if (BUNIT == "Jy/beam") {
write, "data is in Jy/beam" ;
write, "Converting Jy/beam to MJy/sr";
factor = 1.0 / (1e6 * (beam_size * SI.as)^2) * pi/4./log(2.) ;
conversion_factor = factor ;
} else {
"ERROR: unknown BUNIT. Exiting." ;
write, "BUNIT=", BUNIT ;
error ;
}
write, "conversion factor = ", conversion_factor ;
return image * float(factor);
}
//*************************************************************************
func fit_sed(flux,sigma,upper,wl,&T,&dens,&chi2,&T0,&dens0, &T_error,&dens_error_factor, logfit=,plot=,stdev=,correl=) {
/* DOCUMENT
- Fit a map by a black body * power-law opacity law
- Perform the Bayesian analysis of the different parameters
Units:
- wavelengths in m
Si le flux en W.m^-2.Hz^-1, ie 1e26 Jy et _fact en m^2.kg^-1 * Msun / pc^2,
dens0 est renvoye en Msun
SEE ALSO:
*/
extern _kappa, _kappa0, _l0, _fact ;
extern tab_flux_ratio, tab_T ;
//flux = fact * 10.^1 * bb_nu(30.,wl) * (1+0.1*random(5)) ; // TEST
//flux = [-0.00148352,-0.00120954,0.34486,0.502697,0.55466]
if (is_void(logfit)) logfit=0 ;
if (is_void(plot)) plot=0 ;
if (is_void(stdev)) stdev=0 ;
if (is_void(correl)) correl=0 ;
flux0 = flux ;
sigma0 = sigma ;
ou_upper = where(upper) ;
ou_data = where(!upper) ; //points qui ne sont des upper limits
if (plot) {
fma ; lxy, 1, 1 ;
if (numberof(ou_data)) plp, flux0(ou_data), wl(ou_data) * 1e6, symbol = 6, dy=sigma0(ou_data) ;
if (numberof(ou_upper)) plp, 3*sigma0(ou_upper), wl(ou_upper) * 1e6, symbol = 7 ;
limits ; relimits_log ;
}
// Using color if we only have 2 data points
if ( (numberof(flux) == 2) && (numberof(ou_data) == 2)) {
flux_ratio = flux(2)/flux(1) ;
T = interp(tab_T, tab_flux_ratio, flux_ratio) ;
dens = flux(2) / (_fact(2) * bb_nu(T,wl(2))) ;
// Using proper fit if more than 3 data points
} else if (numberof(ou_data) >= 3) { // do the fitting if we have at least 1 point which is not an upper limit
// initial guess:
// - estimate the temperature from the wavelength where the SED peaks
// - compute the density from the last point given the estimated temperature
T0 = 25. * 100e-6/wl(flux(mxx)) ; // Loi de Wien
l = ou_data(0) ;
dens0 = log10( flux(l) / (_fact(l) * bb_nu(T0,wl(l))) ) ;
a = [T0,dens0] ;
if (logfit==1) {
if (anyof(flux(ou_data)) <= 0.) {
T = -99 ;
dens = -99 ;
chi2 = -99 ;
write, "ERROR: negative flux in SED" ;
return [] ;
}
// Flux en log
sigma(ou_data) = sigma(ou_data)/flux(ou_data) ; // erreur en relatif
flux(ou_data) = log(flux(ou_data)) ;
// Poids
W = sigma * 0. ;
W(ou_data) = 1./sigma(ou_data)^2 ;
// Upper limits
if (numberof(ou_upper)) W(ou_upper) = 0. ;
// fit
result= lmfit(log_black_body, wl, a, flux, W, deriv=1, stdev=stdev, monte_carlo=500, correl=correl) ;
} else if (logfit==2) {
if (anyof(flux(ou_data)) <= 0.) {
T = -99 ;
dens = -99 ;
chi2 = -99 ;
write, "ERROR: negative flux in SED" ;
return [] ;
}
_is_upper_limit = array(0,numberof(flux)) ;
// Flux en log
sigma(ou_data) = sigma(ou_data)/flux(ou_data) ; // erreur en relatif
flux(ou_data) = log(flux(ou_data)) ;
// Poids
W = sigma * 0. ;
W(ou_data) = 1./sigma(ou_data)^2 ;
// Upper limits
if (numberof(ou_upper)) {
W(ou_upper) = 1./sigma(ou_upper)^2 ;
flux(ou_upper) = 0.0 ;
_is_upper_limit(ou_upper) = 1 ;
}
X = [wl,_is_upper_limit] ;
// fit
result= lmfit(log_black_body_upper, X, a, flux, W, deriv=1, stdev=stdev, monte_carlo=0, correl=correl) ;
} else {
// Poids
W = 1./sigma^2 ;
// Upper limits
if (numberof(ou_upper)) {
flux(ou_upper) = 0.0 ;
}
// fit
result = lmfit(black_body2, wl, a, flux, W, deriv=1, stdev=stdev, monte_carlo=0, correl=correl) ;
}
if (plot) {
//*** pltr, swrite(i)+swrite(j), 0, 90 ;
wl2 = spanl(50.,1000.,100) * 1e-6 ;
plg, _kappa0 * (wl2/_l0)^-2 * SI.Msun / ((distance*SI.pc)^2) * bb_nu(a(1),wl2) * 10.^a(2), wl2* 1e6, color="red" ;
//plg, _kappa0 * (wl2/_l0)^-2 * SI.Msun / ((distance*SI.pc)^2) * bb_nu(T0,wl2) * 10.^dens0, wl2* 1e6, color="blue", type=2 ;
//plp, _kappa * SI.Msun / ((distance*SI.pc)^2) * bb_nu(a(1),wl) * 10.^a(2), wl* 1e6, color="red" ;
//*** pltr, swrite(a(1))+swrite(10^a(2)*_density_factor), 0, 70 ;
}
// Save results
T = a(1) ;
dens = 10.^a(2) ;
chi2 = result.chi2_last ;
dens0 = 10^dens0 ;
T_error = (*result.stdev)(1) ;
dens_error_factor = 10.^((*result.correl)(2)) ;
//write, "T", T, T_error ;
} else {
T = NaN ;
dens = NaN ;
chi2 = NaN ;
T_error = NaN ;
dens_error_factor = NaN ;
}
}
//*************************************************************************
func black_body(x,a,&grad,deriv=) {
// a(1) is temperature
// 10^a(2) is density or mass
extern _fact, cst_th ;
wl= x ;
T = a(1) ;
if (a(1) < 0) {
zero = x * 0. ;
grad = [zero, zero] +1e-3;
return zero ;
}
hc_lkT = cst_th/(T*wl) ;
fact_exp = exp(min(hc_lkT,700.) ) ;
if ((min(fact_exp) -1.0) > 1e-20) {
BB = 2.*SI.h*SI.c * max(1./ ((fact_exp -1.) * wl^3), 1e-200) ; // same as bb_nu
} else {
BB = 1e50 ;
}
BB = 10.^min(a(2),100) * _fact * BB ;
// derivee par rapport a T, teste Ok numeriquement + maxima
if ((min(fact_exp) -1.0) > 1e-20) {
dBB_dT = BB * hc_lkT / T * (fact_exp / (fact_exp -1.));
} else {
dBB_dT = 1e50 ;
}
if (deriv) {
grad = [dBB_dT, BB * log(10.)] + 1e-300;
}
return BB ;
}
//*************************************************************************
func black_body2(x,a,&grad,deriv=) {
// a(1) is temperature
// 10^a(2) is density or mass
extern _fact, cst_th ;
wl= x ;
T = a(1) ;
if (a(1) < 0) {
zero = x * 0. ;
grad = [zero, zero] +1e-3;
return zero ;
}
hc_lkT = cst_th/(T*wl) ;
fact_exp = exp(min(hc_lkT,700.) ) ;
if ((min(fact_exp) -1.0) > 1e-20) {
BB = 2.*SI.h*SI.c * max(1./ ((fact_exp -1.) * wl^3), 1e-200) ; // same as bb_nu
} else {
BB = 1e50 ;
}
BB = 1.0 - exp(-10.^min(a(2),100) * _fact) * BB ;
// derivee par rapport a T, teste Ok numeriquement + maxima
if ((min(fact_exp) -1.0) > 1e-20) {
dBB_dT = BB * hc_lkT / T * (fact_exp / (fact_exp -1.)) ;
} else {
dBB_dT = 1e50 ;
}
if (deriv) {
grad = [dBB_dT, BB * log(10.)] + 1e-300;
}
return BB ;
}
//*************************************************************************
func log_black_body(x,a,&grad,deriv=) {
// a(1) is temperature
// a(2) is density
extern _fact, cst_th ;
wl= x ;
T = a(1) ;
if (a(1) < 0) {
zero = x * 0. ;
grad = [zero, zero] +1e-3;
return zero ;
}
hc_lkT = cst_th/(T*wl) ;
fact_exp = min(exp(min(hc_lkT,700.) ), 1e100) ;
if ((min(fact_exp) -1.0) > 1e-20) {
BB = 2.*SI.h*SI.c * max(1./ ((fact_exp -1.) * wl^3), 1e-200) ; // same as bb_nu
} else {
BB = 1e50 ;
}
log_BB = log(10.^min(a(2),100) * _fact * BB) ;
// derivee par rapport a T, teste Ok numeriquement + maxima
if ((min(fact_exp) -1.0) > 1e-20) {
dBB_dT = hc_lkT / T * fact_exp / (fact_exp -1.) ;
} else {
dBB_dT = 1e50 ;
}
if (deriv) {
grad = [dBB_dT, log(10.)] + 1e-100;
}
return log_BB ;
}
//*************************************************************************
func log_black_body_upper(x,a,&grad,deriv=) {
// a(1) is temperature
// a(2) is density
extern _fact, cst_th;
wl = x(,1) ;
_is_upper_limit = x(,2) ;
T = a(1) ;
ou_upper = where(_is_upper_limit) ;
ou_data = where(!_is_upper_limit) ;
log_BB = dBB_dT = array(0.,numberof(wl)) ;
if (a(1) < 0) {
zero = x * 0. ;
grad = [zero, zero] +1e-3;
return zero ;
}
hc_lkT = cst_th/(T*wl) ;
fact_exp = min(exp(min(hc_lkT,700.) ), 1e100) ;
if ((min(fact_exp) -1.0) > 1e-20) {
BB = 2.*SI.h*SI.c * max(1./ ((fact_exp -1.) * wl^3), 1e-200) ; // same as bb_nu
} else {
BB = 1e50 ;
}
BB = 10.^min(a(2),100) * _fact * BB ;
log_BB(ou_data) = log(BB(ou_data)) ;
if (numberof(ou_upper)) log_BB(ou_upper) = BB(ou_upper) ; // On ne renvoie pas le log
// derivee par rapport a T, teste Ok numeriquement + maxima
if ((min(fact_exp) -1.0) > 1e-20) {
dBB_dT(ou_data) = hc_lkT(ou_data) / T * fact_exp(ou_data) / (fact_exp(ou_data) -1.) ;
if (numberof(ou_upper)) {
dBB_dT(ou_upper) = BB(ou_upper) * hc_lkT(ou_upper) / T * fact_exp(ou_upper) / (fact_exp(ou_upper) -1.) ;
}
} else {
dBB_dT = 1e50 ;
}
if (deriv) {
grad = [dBB_dT, log(10.)] + 1e-100;
}
return log_BB ;
}
//*************************************************************************
func xy2sky_file(filename,coord_filename) {
/* DOCUMENT xy2sky_file(filename,coord_filename)
Write an ASCII file with the coordinates of the pixels in degrees
SEE ALSO:
*/
extern _wcs_bin_dir ;
fh = cfitsio_open(filename) ;
naxis = cfitsio_get(fh,"NAXIS") ;
nx = cfitsio_get(fh,"NAXIS1") ;
ny = cfitsio_get(fh,"NAXIS2") ;
cfitsio_close, fh ;
/*
if (naxis != 2) {
"Error: Fits file is not an image" ;
"Exiting" ;
return ;
}
*/
f=open("pixel_list.tmp","w") ;
ix = indgen(nx)(,-:1:ny) ;
iy = indgen(ny)(-:1:nx,:) ;
write, f, ix, iy ;
close, f ;
// Extraction des coordonnees en degrees
if (strchar(strchar(filename)(-2:0))=="gz") {
filename2 = "tmp.fits" ;
system, "gunzip -c "+filename+" > "+filename2
} else {
filename2 = filename ;
}
system, _wcs_bin_dir+"/xy2sky -j "+filename2+" @pixel_list.tmp | awk -F \" \" '{print $1 \" \" $2}' > "+coord_filename ;
system, "rm -rf pixel_list.tmp tmp.fits" ;
return ;
}
//*************************************************************************
func remap_fits(filename,nx,ny,coord_filename,image=) {
/* DOCUMENT
Remap a fits file on the coordinates provided in a ASCII file
The dims of the new coordinates is nx * ny
Image KEYWORD is if you want to replace the image in the fits file
must have the same dims as the fits file
SEE ALSO:
*/
extern _wcs_bin_dir ;
// Verification image
if (is_void(image)) {
image=cfitsioRead(filename) ;
} else {
fh = cfitsio_open(filename);
nim_x = cfitsio_get(fh, (key = "NAXIS1")) ;
nim_y = cfitsio_get(fh, (key = "NAXIS2")) ;
cfitsio_close, fh ;
N=dimsof(image) ;
if ( (nim_x != N(2)) || (nim_y != N(3)) ) {
"*** Error in remap_fits: incorrect number of pixel in provided image" ;
write, filename, " size", nim_x, nim_y ;
write, N(2), N(3) ;
error ;
return [] ;
}
}
// Calcul des indices pixels
system, "rm -rf pixel_list.tmp" ;
if (strchar(strchar(filename)(-2:0))=="gz") {
filename2 = "tmp.fits" ;
system, "gunzip -c "+filename+" > "+filename2
} else {
filename2 = filename ;
}
system, _wcs_bin_dir+"/sky2xy -j "+filename2+" @"+coord_filename+" | awk -F \" \" '{print $5 \" \" $6}' > pixel_list.tmp" ;
system, "rm -rf tmp.fits" ;
// Extraction des flux des pixels
pixels=OpenASCII("pixel_list.tmp",valtype="float",prompt=0) ;
N_pixels = numberof(pixels) ;
//write, N_pixels, "were found"
if (N_pixels != nx*ny) {
"*** Error in remap_fits: incorrect number of pixel in coord map" ;
write, N_pixels ;
write, nx, ny, "->", nx*ny ;
error ;
return [] ;
}
interp = bilinear(image,pixels.X1,pixels.X2,minus_one=0,outside=0) ;
//ou = where(interp < -1e10) ;
//if (numberof(ou)) interp(ou) = NaN ;
im = array(float,nx,ny) ;
im(*) = interp(*) ;
return im ;
}
//------------------------------------------------------------
func pixel_sed(win=,color=,pixel=) {
// TODO : 9/11/2010 : bug le fit lineaire n'est pas le meme que dans le routine proncipale
// pixels 329,358 et 328,358 pour m16
extern map, map_sigma, map_upper, wl, distance, Temperature, density, chi2 ;
if (is_void(color)) color="black" ;
if (is_void(win)) win=window() ;
if (is_void(pixel)) {
window, win ;
m = mouse(1) ;
i = int(m(1)) ;
j = int(m(2)) ;
if (i==0 && (j==0)) {
write, "Pixel error" ;
return ;
}
} else {
i = pixel(1) ;
j = pixel(2) ;
}
write, "Pixel", i, j ;
// define flux and error in the pixel
flux = map(i,j,) ;
sigma = map_sigma(i,j,) ;
upper = map_upper(i,j,)
write, "lambda (micron) Flux (Jy) Error (Jy)" ;
write, wl*1e6, flux, sigma ;
window, win+1; fma ; lxy, 1, 1 ;
ou_data = where(!upper) ;
ou_upper = where(upper) ;
W = array(0.,numberof(flux)) ;
if (numberof(ou_data)) plp, flux(ou_data), wl(ou_data) * 1e6, symbol = 6, dy =sigma(ou_data) ;
if (numberof(ou_upper)) plp, 3*sigma(ou_upper), wl(ou_upper) * 1e6, symbol = 7 ;
limits ; relimits_log ;
if (numberof(ou_data)) {
T = Temperature(i,j) ;
dens = density(i,j) ;
write, "Map parameters: T=", T, "dens=", dens, "chi2=", chi2(i,j) ;
wl2 = spanl(50.,1000.,100) * 1e-6 ;
plg, _kappa0 * (wl2/_l0)^-2 * SI.Msun / ((distance*SI.pc)^2) * bb_nu(T,wl2) * dens / _density_factor, wl2* 1e6, color=color ;
/*
// do the fitting (linear)
if (numberof(ou_data) > 2) {
W(ou_data) = 1.0/ sigma(ou_data)^2 ;
// initial guess:
// compute the density from the last point given a chosen temperature
T0 = 10. ;//
l = ou_data(0) ;
dens0 = log10( flux(l) / (_fact(l) * bb_nu(T0,wl(l))) ) ;
a = [T0,dens0] ;
result= lmfit(black_body, wl, a, flux, W, deriv=1, stdev=0, monte_carlo=0, correl=0) ;
plg, _kappa0 * (wl2/_l0)^-2 * SI.Msun / ((distance*SI.pc)^2) * bb_nu(a(1),wl2) * 10^a(2), wl2* 1e6, color="green", type=2 ;
write, "linear fit (green) T=", a(1), "dens=", 10^a(2) * _density_factor ;
}
// do the fitting (log)
if (numberof(ou_data) > 2) {
// initial guess:
// compute the density from the last point given a chosen temperature
T0 = 10. ;//
l = ou_data(0) ;
dens0 = log10( flux(l) / (_fact(l) * bb_nu(T0,wl(l))) ) ;
a = [T0,dens0] ;
W(ou_data) = 1.0/ sigma(ou_data)^2 ;
flux(ou_data) = log(flux(ou_data)) ;
W = array(0.,numberof(flux)) ;
W(ou_data) = 1.0 ;
result= lmfit(log_black_body, wl, a, flux, W, deriv=0, stdev=0, monte_carlo=0, correl=0) ;
plg, _kappa0 * (wl2/_l0)^-2 * SI.Msun / ((distance*SI.pc)^2) * bb_nu(a(1),wl2) * 10^a(2), wl2* 1e6, color="blue", type=2 ;
write, "log fit (blue) T=", a(1), "dens=", 10^a(2) * _density_factor ;
}
*/
window, win ;
return ;
}
}
//------------------------------------------------------------
func log_normal(x,a) {
mu = a(1) ;
sigma = a(2) ;
fact = a(3) ;
y = fact/(x*sigma*sqrt(2*pi)) * exp( -(log(x) - mu)^2/(2*sigma^2) ) ;
return y ;
}
//------------------------------------------------------------
func read_scale_offset(file) {
f = open(file) ;
rdline, f, 17 ;
instru = filter = unite = norm = array(string,8) ;
wav = scale = offset = beam_surf_sr = beam_surf_arcsec2 = ratio = array(float,8) ;
read, f, instru, filter, wav, unite, norm, scale, offset, beam_surf_sr, beam_surf_arcsec2, ratio ;
close, f ;
// PACS and SPIRE fluxes
select = [1,3,4,5,6] ;
select = [] ;
offset = offset(select) ;
scale = scale(select) ;
/*
// Test to find out what is ratio
ratio = ratio(select) ;
beam_surf_sr = beam_surf_sr(select) ;
beam_surf_arcsec2 = beam_surf_arcsec2(select) ;
// write, beam_surf_sr / beam_surf_arcsec2, "" ;
write, scale * ratio * beam_surf_sr * 1e6, "" ; // this is equal to 1
*/
return [offset,scale] ;
}
//------------------------------------------------------------
func read_scale_offset2(file,wl) {
n_lines = int() ;
f = popen("wc -l "+file,0) ;
read, f, n_lines ;
close, f ;
f = open(file) ;
rdline, f, 17 ;
instru = filter = unite = norm = array(string,n_lines-17) ;
wav = scale = offset = beam_surf_sr = beam_surf_arcsec2 = ratio = array(float,n_lines-17) ;
read, f, instru, filter, wav, unite, norm, scale, offset, beam_surf_sr, beam_surf_arcsec2, ratio ;
close, f ;
Offset = Scale = array(float,numberof(wl)) ;
for (i=1 ; i<=numberof(wl) ; i++) {
ou = where(abs(wav - wl(i)*1e6) < 1e-3) ;
if (numberof(ou) != 1) {
write, "ERROR : scale and offset are not available (or multiple) for this wavelength", wl(i) ;
error ;
} else {
Offset(i) = offset(ou) ;
Scale(i) = scale(ou) ;
}
}
return [Offset,Scale] ;
}
//------------------------------------------------------------
func read_median_offset(file) {
f = open(file) ;
rdline, f, 19 ;
instru = filter = unite = norm = array(string,5) ;
wav = scale = offset = beam_surf_sr = beam_surf_arcsec2 = ratio = scale_error = offset_error = rchi2 = medianp = medianh = array(float,5) ;
read, f, instru, filter, wav, unite, norm, scale, offset, beam_surf_sr, beam_surf_arcsec2, ratio, scale_error, offset_error, rchi2, medianp, medianh ;
close, f ;
return [medianp,medianh] ;
}
//------------------------------------------------------------
func read_median_offset2(file,wl) {
n_lines = int() ;
f = popen("wc -l "+file,0) ;
read, f, n_lines ;
close, f ;
f = open(file) ;
rdline, f, 19 ;
instru = filter = unite = norm = array(string,n_lines-19) ;
wav = scale = offset = beam_surf_sr = beam_surf_arcsec2 = ratio = scale_error = offset_error = rchi2 = medianp = medianh = array(float,n_lines-19) ;
read, f, instru, filter, wav, unite, norm, scale, offset, beam_surf_sr, beam_surf_arcsec2, ratio, scale_error, offset_error, rchi2, medianp, medianh ;
close, f ;
Medianp = Medianh = array(float,numberof(wl)) ;
for (i=1 ; i<=numberof(wl) ; i++) {
ou = where(abs(wav - wl(i)*1e6) < 1e-3) ;
if (numberof(ou) != 1) {
write, "ERROR : scale and offset are not available (or multiple) for this wavelength", wl(i) ;
error ;
} else {
Medianp(i) = medianp(ou) ;
Medianh(i) = medianh(ou) ;
}
}
return [Medianp,Medianh] ;
}
//------------------------------------------------------------
func make_PDF(data,xtitle=,ytitle=,log=,color=,mask=,N_bins=,plot_error=,type=,width=,cumul=) {
/* DOCUMENT make_PDF(data,xtitle=,ytitle=,log=,color=,mask=,N_bins=,plot_error=,type=,width=,cumul=)
SEE ALSO:
*/
if (is_void(N_bins)) N_bins=200 ;
if (is_void(log)) log=0 ;
if (is_void(plot_error)) plot_error=0 ;
if (is_void(color)) color="black" ;
if (is_void(type)) type=1 ;