forked from RickKessler/SNANA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMWgaldust.c
4558 lines (3964 loc) · 138 KB
/
MWgaldust.c
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
/*
* MWgaldust_av is an interface to schlegel's dust_getval for
* use inside of snana. All the inputs are set to default values
* and the calling sequence sequence is internal rather than via the
* command line. The standard input is RA,DEC in degrees which are translated
* to galactic coordinates which is what dust_getval expects.
* Below is the original documentation of dust_getval, and all the
* needed routines from Schlegel are combined in one file below this
* simple interface subroutine.
* D.Cinabro 6 June 2007
*
* Usage: void MWgaldust(double RA,double DEC,double *avgal, double galebmv)
* RA,DEC are in degrees, avgal is the extnction in Sloan u,g,r,i,z filters,
* and galebmv is the Milky Way galaxy E(B-V) redening from the Schlegal map
*/
/******************************************************************************/
/*
* NAME:
* dust_getval
*
* PURPOSE:
* Read values from BH files or our dust maps.
*
* Either the coordinates "gall" and "galb" must be set, or these coordinates
* must exist in the file "infile". Output is written to standard output
* or the file "outfile".
*
* CALLING SEQUENCE:
* dust_getval gall galb map=map ipath=ipath interp=interp noloop=noloop \
* infile=infile outfile=outfile verbose=verbose
*
* OPTIONAL INPUTS:
* gall: Galactic longitude(s) in degrees
* galb: Galactic latitude(s) in degrees
* map: Set to one of the following (default is "Ebv"):
* I100: 100-micron map in MJy/Sr
* X : X-map, temperature-correction factor
* T : Temperature map in degrees Kelvin for n=2 emissivity
* Ebv : E(B-V) in magnitudes
* mask: Mask values
* infile: If set, then read "gall" and "galb" from this file
* outfile: If set, then write results to this file
* interp: Set this flag to "y" to return a linearly interpolated value
* from the 4 nearest pixels.
* This is disabled if map=='mask'.
* noloop: Set this flag to "y" to read entire image into memory
* rather than reading pixel values for one point at a time.
* This is a faster option for reading a large number of values,
* but requires reading up to a 64 MB image at a time into
* memory. (Actually, the smallest possible sub-image is read.)
* verbose: Set this flag to "y" for verbose output, printing pixel
* coordinates and map values
* ipath: Path name for dust maps; default to path set by the
* environment variable $DUST_DIR/maps, or to the current
* directory.
*
* EXAMPLES:
* Read the reddening value E(B-V) at Galactic (l,b)=(12,+34.5),
* interpolating from the nearest 4 pixels, and output to the screen:
* % dust_getval 12 34.5 interp=y
*
* Read the temperature map at positions listed in the file "dave.in",
* interpolating from the nearest 4 pixels, and output to file "dave.out".
* The path name for the temperature maps is "/u/schlegel/".
* % dust_getval map=T ipath=/u/schlegel/ interp=y \
* infile=dave.in outfile=dave.out
*
* DATA FILES FOR SFD MAPS:
* SFD_dust_4096_ngp.fits
* SFD_dust_4096_sgp.fits
* SFD_i100_4096_ngp.fits
* SFD_i100_4096_sgp.fits
* SFD_mask_4096_ngp.fits
* SFD_mask_4096_sgp.fits
* SFD_temp_ngp.fits
* SFD_temp_sgp.fits
* SFD_xmap_ngp.fits
* SFD_xmap_sgp.fits
*
* DATA FILES FOR BH MAPS:
* hinorth.dat
* hisouth.dat
* rednorth.dat
* redsouth.dat
*
* REVISION HISTORY:
* Written by D. Schlegel, 19 Jan 1998, Durham
* 5-AUG-1998 Modified by DJS to read a default path from an environment
* variable $DUST_DIR/map.
*
* Jan 28, 2007 D.Cinabro : add MWEBV = E(B-V) as additional output arg
*
*
* Oct 7, 2007 R.Kessler move contents of $DUST_DIR/maps to
* $SNDATA_ROOT/MWDUST/ and change local path
*
* Mar 02, 2012: uchar pLabel_temp[8] -> pLabel_temp[9]
* (bug found by S.Rodney)
*
* Feb 22, 2013: RK - update to compile with c++
*
* Sep 21 2013 RK - move GAL-related functions from sntools.c to here
*
* Oct 29 2013 RK - move slalib routines to sntools.c
*
* Jan 28 2020 RK - abort if WAVE>12000 and using Fitz99 color law
*
* Oct 9 2021 DB and DS - update Fitz/Odonell ratio and extend WAVE to 15000
*/
/**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <ctype.h>
#include "MWgaldust.h"
#include "sntools.h"
//#include "sntools_cosmology.h"
// #####################################################
//
// snana-GALextinct functions (moved from sntools)
//
// #####################################################
// *****************************
// mangled routines for fortran
double galextinct_(double *RV, double *AV, double *WAVE, int *OPT ) {
return GALextinct(*RV, *AV, *WAVE, *OPT);
}
void text_mwoption__(char *nameOpt, int *OPT, char *TEXT) {
text_MWoption(nameOpt,*OPT,TEXT);
}
void modify_mwebv_sfd__(int *OPT, double *RA, double *DECL,
double *MWEBV, double *MWEBV_ERR) {
modify_MWEBV_SFD(*OPT, *RA, *DECL, MWEBV, MWEBV_ERR) ;
}
// **********************************************
void text_MWoption(char *nameOpt, int OPT, char *TEXT) {
// Created Sep 19 2013
// Return corresponding *TEXT description of
// integer option OPT for
// *nameOpt = "MWCOLORLAW" or "COLORLAW" or "MWEBV" or "EBV"
// ABORT on invalid OPT.
char fnam[] = "text_MWoption" ;
// ------------------ BEGIN ------------------
sprintf(TEXT,"NULL");
// ----------------------------------------
if ( strcmp(nameOpt,"MWCOLORLAW") == 0 ||
strcmp(nameOpt,"COLORLAW" ) == 0 ) {
if ( OPT == OPT_MWCOLORLAW_OFF )
{ sprintf(TEXT,"No Extinction"); }
else if ( OPT == OPT_MWCOLORLAW_CCM89 )
{ sprintf(TEXT,"CCM89"); }
else if ( OPT == OPT_MWCOLORLAW_ODON94 )
{ sprintf(TEXT,"CCM89+ODonell94"); }
else if ( OPT == OPT_MWCOLORLAW_FITZ99 )
{ sprintf(TEXT,"Fitzpatrick99"); }
else {
sprintf(c1err,"Invalid OPT_MWCOLORLAW = %d", OPT);
sprintf(c2err,"Check OPT_MWCOLORAW_* in sntools.h");
errmsg(SEV_FATAL, 0, fnam, c1err, c2err);
}
}
// ----------------------------------------
else if ( strcmp(nameOpt,"MWEBV")== 0 ||
strcmp(nameOpt,"EBV" )== 0 ) {
if ( OPT == OPT_MWEBV_OFF )
{ sprintf(TEXT,"No Extinction"); }
else if ( OPT == OPT_MWEBV_FILE )
{ sprintf(TEXT,"FILE value (SIMLIB or data header)"); }
else if ( OPT == OPT_MWEBV_SFD98 )
{ sprintf(TEXT,"SFD98"); }
else if ( OPT == OPT_MWEBV_Sch11_PS2013 )
{ sprintf(TEXT,"Schlafly11+PS2013: 0.86*MWEBV(SFD98)" ); }
// { sprintf(TEXT,"Schlafly11+PS2013: -.06,-.14, for EBV<.1,EBV>.1"); }
else {
sprintf(c1err,"Invalid OPT_MWEBV = %d", OPT);
sprintf(c2err,"Check OPT_MWEBV_* in sntools.h");
errmsg(SEV_FATAL, 0, fnam, c1err, c2err);
}
}
// ----------------------------------------
else {
sprintf(c1err,"Invalid nameOpt = %s", nameOpt );
sprintf(c2err,"Valid nameOpt are COLORLAW and EBV");
errmsg(SEV_FATAL, 0, fnam, c1err, c2err);
}
} // end of text_MWoption
// **************************************
void modify_MWEBV_SFD(int OPT, double RA, double DECL,
double *MWEBV, double *MWEBV_ERR) {
// Created Sep 21 2013 by R.Kessler
// According to input integer option OPT, modify MWEBV & MWEBV_ERR.
// Note that input MWEBV arguments are the FILE values,
// and may be changed !
// Input RA and DEC may or may not be used depending on the option.
double MWEBV_INP, MWEBV_OUT=0.0, MWEBV_ERR_OUT=0.0, MWEBV_SFD98=0.0 ;
double dumXT[10] ;
// char fnam[] = "modify_MWEBV_SFD" ;
// ----------- BEGIN -----------
MWEBV_INP = *MWEBV ;
MWEBV_OUT = -999.0 ;
// check trival option with no Galactic extictio
if ( OPT == OPT_MWEBV_OFF ) {
MWEBV_OUT = MWEBV_ERR_OUT = 0.0 ;
goto LOAD_OUTPUT ;
}
// ----------------------------------------------------
// always compute MWEBV_SFD98 since many options need this.
if ( OPT >= OPT_MWEBV_SFD98 )
{ MWgaldust(RA, DECL, dumXT, &MWEBV_SFD98 ); }
else
{ MWEBV_SFD98 = -999. ; }
// ----------------------------------------------------
if ( OPT == OPT_MWEBV_FILE ) {
// already defined extinction from file -> use it
MWEBV_OUT = *MWEBV ; // do nothing
MWEBV_ERR_OUT = *MWEBV_ERR ;
}
else if ( OPT == OPT_MWEBV_SFD98 ) {
// force SFD98 regardless of input/FILE value
MWEBV_OUT = MWEBV_SFD98 ;
MWEBV_ERR_OUT = MWEBV_SFD98/6.0 ;
}
else if ( OPT == OPT_MWEBV_Sch11_PS2013 ) {
/* xxxxxxxxxxxxxx
// Based on D. Scolnic & E. Schlafly, private communication.
// See middle panel of Fig 8 in Schlafly 2011.
if ( MWEBV_SFD98 < 0.1 )
{ MWEBV_OUT = 0.94 * MWEBV_SFD98 ; } // low EBV corr
else
{ MWEBV_OUT = 0.86 * MWEBV_SFD98 ; } // high EBV corr
xxxxxxxxxxxxxxxxx */
MWEBV_OUT = 0.86 * MWEBV_SFD98 ; // Apr 13 2018: Dan suggests this
// MWEBV_ERR_OUT = 0.1 * MWEBV_OUT ; // error is 10% of EBV
MWEBV_ERR_OUT = 0.05 * MWEBV_OUT ; // Apr 13 2018
}
// load output
LOAD_OUTPUT:
*MWEBV = MWEBV_OUT ;
*MWEBV_ERR = MWEBV_ERR_OUT ;
} // end of modify_MWEBV_SFD
// **********************************************
double GALextinct(double RV, double AV, double WAVE, int OPT) {
/***
Input :
AV = V band (defined to be at 5495 Angstroms) extinction
RV = assumed A(V)/E(B-V) (e.g., = 3.1 in the LMC)
WAVE = wavelength in angstroms
OPT=89 => use original CCM89 :
Cardelli, Clayton, & Mathis (1989) extinction law.
OPT=94 => use update from O'Donell
OPT=99 => use Fitzpatrick 99 (PASP 111, 63) as implemented by
D.Scolnic with polynomial fit to ratio of F'99/O'94 vs. lambda.
O'94 is the opt=94 option and F'99 was computed from
http://idlastro.gsfc.nasa.gov/ftp/pro/astro/fm_unred.pro
Returns magnitudes of extinction.
Nov 1, 2006: Add option to use new/old NIR coefficients
(copied from Jha's MLCS code)
; c1 = [ 1. , 0.17699, -0.50447, -0.02427, 0.72085, $ ;Original
; 0.01979, -0.77530, 0.32999 ] ;coefficients
; c2 = [ 0., 1.41338, 2.28305, 1.07233, -5.38434, $ ;from CCM89
; -0.62251, 5.30260, -2.09002 ]
c1 = [ 1. , 0.104, -0.609, 0.701, 1.137, $ ;New coefficients
-1.718, -0.827, 1.647, -0.505 ] ;from O'Donnell
c2 = [ 0., 1.952, 2.908, -3.989, -7.985, $ ;(1994)
11.102, 5.491, -10.805, 3.347 ]
Sep 18 2013 RK
- add opt=99 option to use Fitzpatrick 99 update.
- reduce/remove pow calls to save CPU
- rename CCMextinct -> GALextinct
Aug 4 2019 RK
+ fix subtle bug by returning XT=0 only if AV=0, and not if AV<1E-9.
Recall that negative AV are used for warping spectra in kcor.c.
This bug caused all AV<0 to have same mag as AV=0.
***/
int i, DO94 ;
double XT, x, y, a, b, fa, fb, xpow, xx, xx2, xx3 ;
double y2, y3, y4, y5, y6, y7, y8 ;
char fnam[] = "GALextinct" ;
// ------------------- BEGIN --------------
XT = 0.0 ;
if ( AV == 0.0 ) { return XT ; }
// -----------------------------------------
DO94 = (OPT == 94 || OPT == 99 ) ;
x = 10000./WAVE; // inverse wavelength in microns
y = x - 1.82;
if (x >= 0.3 && x < 1.1) { // IR
xpow = pow(x,1.61) ;
a = 0.574 * xpow ;
b = -0.527 * xpow ;
}
else if (x >= 1.1 && x < 3.3) { // Optical/NIR
y2 = y * y ;
y3 = y2 * y ;
y4 = y2 * y2 ;
y5 = y3 * y2;
y6 = y3 * y3;
y7 = y4 * y3;
y8 = y4 * y4;
if ( DO94 ) {
a = 1. + 0.104*y - 0.609*y2 + 0.701*y3 + 1.137*y4
- 1.718*y5 - 0.827*y6 + 1.647*y7 -0.505*y8 ;
b = 1.952*y + 2.908*y2 -3.989*y3 - 7.985*y4
+ 11.102*y5 + 5.491*y6 - 10.805*y7 + 3.347*y8;
}
else {
a = 1. + 0.17699*y - 0.50447*y2 - 0.02427*y3
+ 0.72085*y4 + 0.01979*y5 - 0.77530*y6 + 0.32999*y7 ;
b = 1.41338*y + 2.28305*y2 + 1.07233*y3 - 5.38434*y4
- 0.62251*y5 + 5.30260*y6 - 2.09002*y7 ;
}
}
else if (x >= 3.3 && x < 8.0 ) { // UV
if (x >= 5.9) {
xx = x - 5.9 ;
xx2 = xx * xx ;
xx3 = xx2 * xx ;
fa = -0.04473*xx2 - 0.009779*xx3 ;
fb = 0.21300*xx2 + 0.120700*xx3 ;
} else {
fa = fb = 0.0;
}
xx = x - 4.67 ; xx2 = (xx*xx);
a = 1.752 - 0.316*x - 0.104/(xx2 + 0.341) + fa;
xx = x - 4.62 ; xx2 = (xx*xx);
b = -3.090 + 1.825*x + 1.206/(xx2 + 0.263) + fb;
}
else if (x >= 8.0 && x <= 10.0) { // Far-UV
xx = x - 8.0 ;
xx2 = xx * xx ;
xx3 = xx2 * xx ;
a = -1.073 - 0.628*xx + 0.137*xx2 - 0.070*xx3 ;
b = 13.670 + 4.257*xx - 0.420*xx2 + 0.374*xx3 ;
} else {
a = b = 0.0;
}
XT = AV*(a + b/RV);
// Sep 18 2013 RK/DS - Check option for Fitzptrack 99
#define NPOLY_FITZ99 11 //Dillon and Dan upped to 10, Oct 9 2021
if ( OPT == 99 ) {
double XTcor, wpow[NPOLY_FITZ99] ;
// xxx mark delete double F99_over_O94[NPOLY_FITZ99] = { // From D.Scolnic, Sep 18 2013
// 0.485382, 0.791117, -0.534349, 0.191105,
// -0.0380031, 0.00416853, -0.000235077, 5.31309e-06
//} ;
double F99_over_O94[NPOLY_FITZ99] = { // Dillon and Dan, Oct 9 2021
8.55929205e-02, 1.91547833e+00, -1.65101945e+00, 7.50611119e-01,
-2.00041118e-01, 3.30155576e-02, -3.46344458e-03, 2.30741420e-04,
-9.43018242e-06, 2.14917977e-07, -2.08276810e-09
};
if ( WAVE > WAVEMAX_FITZ99 ) {
sprintf(c1err,"Invalid WAVE=%.1f A for Fitzpatrick 99 color law.",
WAVE );
sprintf(c2err,"Avoid NIR (>%.1f), or update Fitz99 in NIR",
WAVEMAX_FITZ99 );
errmsg(SEV_FATAL, 0, fnam, c1err, c2err);
}
// compute powers of wavelength without using slow 'pow' function
wpow[0] = 1.0 ;
wpow[1] = WAVE/1000. ;
wpow[2] = wpow[1] * wpow[1] ;
wpow[3] = wpow[1] * wpow[2] ;
wpow[4] = wpow[2] * wpow[2] ;
wpow[5] = wpow[3] * wpow[2] ;
wpow[6] = wpow[3] * wpow[3] ;
wpow[7] = wpow[4] * wpow[3] ;
wpow[8] = wpow[4] * wpow[4] ;
wpow[9] = wpow[5] * wpow[4] ;
wpow[10] = wpow[5] * wpow[5] ;
XTcor = 0.0 ;
for(i=0; i < NPOLY_FITZ99; i++ )
{ XTcor += (wpow[i] * F99_over_O94[i]) ; }
XT *= XTcor ;
}
return XT ;
} // end of GALextinct
// ========== FUNCTION TO RETURN EBV(SFD) =================
void MWgaldust(
double RA // (I) RA
,double DEC // (I) DEC
,double *galxtinct // (O) u,g,r,i,z extinction
,double *galebmv // (O) E(B-V)
)
{
int imap;
int qInterp;
int qVerbose;
int qNoloop;
int nGal;
double tmpl;
double tmpb;
float * pGall = NULL;
float * pGalb = NULL;
float * pMapval;
double dustval;
/* Declarations for keyword input values */
char * pMapName;
char * pIPath = NULL ;
char pDefMap[] = "Ebv" ;
char pDefPath[200];
/* Declarations for data file names */
char pFileN[MAX_FILE_NAME_LEN];
char pFileS[MAX_FILE_NAME_LEN];
struct mapParms {
char * pName;
char * pFile1;
char * pFile2;
} ppMapAll[] = {
{ "Ebv" , "SFD_dust_4096_ngp.fits", "SFD_dust_4096_sgp.fits" },
{ "I100", "SFD_i100_4096_ngp.fits", "SFD_i100_4096_sgp.fits" },
{ "X" , "SFD_xmap_ngp.fits" , "SFD_xmap_sgp.fits" },
{ "T" , "SFD_temp_ngp.fits" , "SFD_temp_sgp.fits" },
{ "mask", "SFD_mask_4096_ngp.fits", "SFD_mask_4096_sgp.fits" }
};
double RV[5];
const int nmap = sizeof(ppMapAll) / sizeof(ppMapAll[0]);
/* Set defaults */
// xxx old sprintf(pDefPath, "%s/maps", getenv("DUST_DIR") );
sprintf(pDefPath, "%s/MWDUST", getenv("SNDATA_ROOT") );
pIPath = pDefPath ;
pMapName = pDefMap;
qInterp = 1; /* interpolation */
qVerbose = 0; /* not verbose */
qNoloop = 0; /* do not read entire image into memory */
RV[0] = 5.155; // u
RV[1] = 3.793; // g
RV[2] = 2.751; // r
RV[3] = 2.086; // i
RV[4] = 1.479; // z
// Translate from RA and DEC to galactic
slaEqgal(RA,DEC,&tmpl,&tmpb);
nGal = 1;
pGall = ccvector_build_(nGal);
pGalb = ccvector_build_(nGal);
pGall[0] = (float)tmpl;
pGalb[0] = (float)tmpb;
/* Determine the file names to use */
for (imap=0; imap < nmap; imap++) {
if (strcmp(pMapName,ppMapAll[imap].pName) == 0) {
sprintf(pFileN, "%s/%s", pIPath, ppMapAll[imap].pFile1);
sprintf(pFileS, "%s/%s", pIPath, ppMapAll[imap].pFile2);
}
}
/* Read values from FITS files in Lambert projection */
pMapval = lambert_getval(pFileN, pFileS, nGal, pGall, pGalb,
qInterp, qNoloop, qVerbose);
dustval = (double) *pMapval;
galxtinct[0] = RV[0]*dustval;
galxtinct[1] = RV[1]*dustval;
galxtinct[2] = RV[2]*dustval;
galxtinct[3] = RV[3]*dustval;
galxtinct[4] = RV[4]*dustval;
*galebmv = dustval;
return;
}
char Label_lam_nsgp[] = "LAM_NSGP";
char Label_lam_scal[] = "LAM_SCAL";
uchar * label_lam_nsgp = (uchar*)Label_lam_nsgp;
uchar * label_lam_scal = (uchar*)Label_lam_scal;
/******************************************************************************/
/* Fortran wrapper for reading Lambert FITS files */
void DECLARE(fort_lambert_getval)
(char * pFileN,
char * pFileS,
void * pNGal,
float * pGall,
float * pGalb,
void * pQInterp,
void * pQNoloop,
void * pQVerbose,
float * pOutput)
{
int iChar;
int qInterp;
int qNoloop;
int qVerbose;
long iGal;
long nGal;
float * pTemp;
/* Truncate the Fortran-passed strings with a null,
* in case they are padded with spaces */
for (iChar=0; iChar < 80; iChar++)
if (pFileN[iChar] == ' ') pFileN[iChar] = '\0';
for (iChar=0; iChar < 80; iChar++)
if (pFileS[iChar] == ' ') pFileS[iChar] = '\0';
/* Select the 4-byte words passed by a Fortran call */
if (sizeof(short) == 4) {
nGal = *((short *)pNGal);
qInterp = *((short *)pQInterp);
qNoloop = *((short *)pQNoloop);
qVerbose = *((short *)pQVerbose);
} else if (sizeof(int) == 4) {
nGal = *((int *)pNGal);
qInterp = *((int *)pQInterp);
qNoloop = *((int *)pQNoloop);
qVerbose = *((int *)pQVerbose);
} else if (sizeof(long) == 4) {
nGal = *((long *)pNGal);
qInterp = *((long *)pQInterp);
qNoloop = *((long *)pQNoloop);
qVerbose = *((long *)pQVerbose);
}
pTemp = lambert_getval(pFileN, pFileS, nGal, pGall, pGalb,
qInterp, qNoloop, qVerbose);
/* Copy results into Fortran-passed location for "pOutput",
* assuming that memory has already been allocated */
for (iGal=0; iGal < nGal; iGal++) pOutput[iGal] = pTemp[iGal];
}
/******************************************************************************/
/* Read one value at a time from NGP+SGP polar projections.
* Set qInterp=1 to interpolate, or =0 otherwise.
* Set qVerbose=1 to for verbose output, or =0 otherwise.
*/
float * lambert_getval
(char * pFileN,
char * pFileS,
long nGal,
float * pGall,
float * pGalb,
int qInterp,
int qNoloop,
int qVerbose)
{
int iloop;
int iGal;
int ii;
int jj;
int * pNS; /* 0 for NGP, 1 for SGP */
int nIndx;
int * pIndx;
int * pXPix;
int * pYPix;
int xPix;
int yPix;
int xsize;
DSIZE pStart[2];
DSIZE pEnd[2];
DSIZE nSubimg;
float * pSubimg;
float dx;
float dy;
float xr;
float yr;
float pWeight[4];
float mapval;
float * pOutput;
float * pDX = NULL;
float * pDY = NULL;
/* Variables for FITS files */
int qRead;
int numAxes;
DSIZE * pNaxis;
char * pFileIn = NULL;
HSIZE nHead;
uchar * pHead;
/* Allocate output data array */
pNS = ccivector_build_(nGal);
pOutput = ccvector_build_(nGal);
/* Decide if each point should be read from the NGP or SGP projection */
for (iGal=0; iGal < nGal; iGal++)
pNS[iGal] = (pGalb[iGal] >= 0.0) ? 0 : 1; /* ==0 for NGP, ==1 for SGP */
if (qNoloop == 0) { /* LOOP THROUGH ONE POINT AT A TIME */
/* Loop through first NGP then SGP */
for (iloop=0; iloop < 2; iloop++) {
qRead = 0;
/* Loop through each data point */
for (iGal=0; iGal < nGal; iGal++) {
if (pNS[iGal] == iloop) {
/* Read FITS header for this projection if not yet read */
if (qRead == 0) {
if (iloop == 0) pFileIn = pFileN; else pFileIn = pFileS;
fits_read_file_fits_header_only_(pFileIn, &nHead, &pHead);
qRead = 1;
}
if (qInterp == 0) { /* NEAREST PIXELS */
/* Determine the nearest pixel coordinates */
lambert_lb2pix(pGall[iGal], pGalb[iGal], nHead, pHead,
&xPix, &yPix);
pStart[0] = xPix;
pStart[1] = yPix;
/* Read one pixel value from data file */
fits_read_point_(pFileIn, nHead, pHead, pStart, &mapval);
pOutput[iGal] = mapval;
if (qVerbose != 0)
printf("%8.3f %7.3f %1d %8d %8d %12.5e\n",
pGall[iGal], pGalb[iGal], iloop, xPix, yPix, mapval);
} else { /* INTERPOLATE */
fits_compute_axes_(&nHead, &pHead, &numAxes, &pNaxis);
/* Determine the fractional pixel coordinates */
lambert_lb2fpix(pGall[iGal], pGalb[iGal], nHead, pHead,
&xr, &yr);
/* The following 4 lines introduced an erroneous 1/2-pixel shift
(DJS 18-Mar-1999).
xPix = (int)(xr-0.5);
yPix = (int)(yr-0.5);
dx = xPix - xr + 1.5;
dy = yPix - yr + 1.5;
*/
xPix = (int)(xr);
yPix = (int)(yr);
dx = xPix - xr + 1.0;
dy = yPix - yr + 1.0;
/* Force pixel values to fall within the image boundaries */
if (xPix < 0) { xPix = 0; dx = 1.0; }
if (yPix < 0) { yPix = 0; dy = 1.0; }
if (xPix >= pNaxis[0]-1) { xPix = pNaxis[0]-2; dx = 0.0; }
if (yPix >= pNaxis[1]-1) { yPix = pNaxis[1]-2; dy = 0.0; }
pStart[0] = xPix;
pStart[1] = yPix;
pEnd[0] = xPix + 1;
pEnd[1] = yPix + 1;
/* Create array of weights */
pWeight[0] = dx * dy ;
pWeight[1] = (1-dx) * dy ;
pWeight[2] = dx * (1-dy) ;
pWeight[3] = (1-dx) * (1-dy) ;
/* Read 2x2 array from data file */
fits_read_subimg_(pFileIn, nHead, pHead, pStart, pEnd,
&nSubimg, &pSubimg);
pOutput[iGal] = 0.0;
for (jj=0; jj < 4; jj++)
pOutput[iGal] += pWeight[jj] * pSubimg[jj];
fits_free_axes_(&numAxes, &pNaxis);
ccfree_((void **)&pSubimg);
if (qVerbose != 0)
printf("%8.3f %7.3f %1d %9.3f %9.3f %12.5e\n",
pGall[iGal], pGalb[iGal], iloop, xr, yr, pOutput[iGal]);
} /* -- END NEAREST PIXEL OR INTERPOLATE -- */
}
}
}
} else { /* READ FULL IMAGE */
pIndx = ccivector_build_(nGal);
pXPix = ccivector_build_(nGal);
pYPix = ccivector_build_(nGal);
if (qInterp != 0) {
pDX = ccvector_build_(nGal);
pDY = ccvector_build_(nGal);
}
/* Loop through first NGP then SGP */
for (iloop=0; iloop < 2; iloop++) {
/* Determine the indices of data points in this hemisphere */
nIndx = 0;
for (iGal=0; iGal < nGal; iGal++) {
if (pNS[iGal] == iloop) {
pIndx[nIndx] = iGal;
nIndx++;
}
}
/* Do not continue if no data points in this hemisphere */
if (nIndx > 0) {
/* Read FITS header for this projection */
if (iloop == 0) pFileIn = pFileN; else pFileIn = pFileS;
fits_read_file_fits_header_only_(pFileIn, &nHead, &pHead);
if (qInterp == 0) { /* NEAREST PIXELS */
/* Determine the nearest pixel coordinates */
for (ii=0; ii < nIndx; ii++) {
lambert_lb2pix(pGall[pIndx[ii]], pGalb[pIndx[ii]],
nHead, pHead, &pXPix[ii], &pYPix[ii]);
}
pStart[0] = ivector_minimum(nIndx, pXPix);
pEnd[0] = ivector_maximum(nIndx, pXPix);
pStart[1] = ivector_minimum(nIndx, pYPix);
pEnd[1] = ivector_maximum(nIndx, pYPix);
/* Read smallest subimage containing all points in this hemi */
fits_read_subimg_(pFileIn, nHead, pHead, pStart, pEnd,
&nSubimg, &pSubimg);
xsize = pEnd[0] - pStart[0] + 1;
/* Determine data values */
for (ii=0; ii < nIndx; ii++) {
pOutput[pIndx[ii]] = pSubimg[ pXPix[ii]-pStart[0] +
(pYPix[ii]-pStart[1]) * xsize ];
}
ccfree_((void **)&pSubimg);
} else { /* INTERPOLATE */
fits_compute_axes_(&nHead, &pHead, &numAxes, &pNaxis);
/* Determine the fractional pixel coordinates */
for (ii=0; ii < nIndx; ii++) {
lambert_lb2fpix(pGall[pIndx[ii]], pGalb[pIndx[ii]],
nHead, pHead, &xr, &yr);
/* The following 4 lines introduced an erroneous 1/2-pixel shift
(DJS 03-Mar-2004).
pXPix[ii] = (int)(xr-0.5);
pYPix[ii] = (int)(yr-0.5);
pDX[ii] = pXPix[ii] - xr + 1.5;
pDY[ii] = pYPix[ii] - yr + 1.5;
*/
pXPix[ii] = (int)(xr);
pYPix[ii] = (int)(yr);
pDX[ii] = pXPix[ii] - xr + 1.0;
pDY[ii] = pYPix[ii] - yr + 1.0;
/* Force pixel values to fall within the image boundaries */
if (pXPix[ii] < 0) { pXPix[ii] = 0; pDX[ii] = 1.0; }
if (pYPix[ii] < 0) { pYPix[ii] = 0; pDY[ii] = 1.0; }
if (pXPix[ii] >= pNaxis[0]-1)
{ pXPix[ii] = pNaxis[0]-2; pDX[ii] = 0.0; }
if (pYPix[ii] >= pNaxis[1]-1)
{ pYPix[ii] = pNaxis[1]-2; pDY[ii] = 0.0; }
}
pStart[0] = ivector_minimum(nIndx, pXPix);
pEnd[0] = ivector_maximum(nIndx, pXPix) + 1;
pStart[1] = ivector_minimum(nIndx, pYPix);
pEnd[1] = ivector_maximum(nIndx, pYPix) + 1;
/* Read smallest subimage containing all points in this hemi */
fits_read_subimg_(pFileIn, nHead, pHead, pStart, pEnd,
&nSubimg, &pSubimg);
xsize = pEnd[0] - pStart[0] + 1;
/* Determine data values */
for (ii=0; ii < nIndx; ii++) {
/* Create array of weights */
pWeight[0] = pDX[ii] * pDY[ii] ;
pWeight[1] = (1-pDX[ii]) * pDY[ii] ;
pWeight[2] = pDX[ii] * (1-pDY[ii]) ;
pWeight[3] = (1-pDX[ii]) * (1-pDY[ii]) ;
pOutput[pIndx[ii]] =
pWeight[0] * pSubimg[
pXPix[ii]-pStart[0] + (pYPix[ii]-pStart[1] )*xsize ]
+pWeight[1] * pSubimg[
pXPix[ii]-pStart[0]+1 + (pYPix[ii]-pStart[1] )*xsize ]
+pWeight[2] * pSubimg[
pXPix[ii]-pStart[0] + (pYPix[ii]-pStart[1]+1)*xsize ]
+pWeight[3] * pSubimg[
pXPix[ii]-pStart[0]+1 + (pYPix[ii]-pStart[1]+1)*xsize ] ;
}
fits_free_axes_(&numAxes, &pNaxis);
ccfree_((void **)&pSubimg);
} /* -- END NEAREST PIXEL OR INTERPOLATE -- */
}
}
ccivector_free_(pIndx);
ccivector_free_(pXPix);
ccivector_free_(pYPix);
if (qInterp != 0) {
ccvector_free_(pDX);
ccvector_free_(pDY);
}
}
/* Free the memory allocated for the FITS header
(Moved outside previous brace by Chris Stoughton 19-Jan-1999) */
fits_dispose_array_(&pHead);
/* Deallocate output data array */
ccivector_free_(pNS);
return pOutput;
}
/******************************************************************************/
/* Transform from galactic (l,b) coordinates to fractional (x,y) pixel location.
* Latitude runs clockwise from X-axis for NGP, counterclockwise for SGP.
* This function returns the ZERO-INDEXED pixel position.
* Updated 04-Mar-1999 to allow ZEA coordinate convention for the same
* projection.
*/
void lambert_lb2fpix
(float gall, /* Galactic longitude */
float galb, /* Galactic latitude */
HSIZE nHead,
uchar * pHead,
float * pX, /* X position in pixels from the center */
float * pY) /* Y position in pixels from the center */
{
int q1;
int q2;
int nsgp;
float scale;
float crval1;
float crval2;
float crpix1;
float crpix2;
float cdelt1;
float cdelt2;
float cd1_1;
float cd1_2;
float cd2_1;
float cd2_2;
float lonpole;
float xr;
float yr;
float theta;
float phi;
float Rtheta;
float denom;
static double dradeg = 180 / 3.1415926534;
char * pCtype1;
char * pCtype2;
fits_get_card_string_(&pCtype1, label_ctype1, &nHead, &pHead);
fits_get_card_string_(&pCtype2, label_ctype2, &nHead, &pHead);
fits_get_card_rval_(&crval1, label_crval1, &nHead, &pHead);
fits_get_card_rval_(&crval2, label_crval2, &nHead, &pHead);
fits_get_card_rval_(&crpix1, label_crpix1, &nHead, &pHead);
fits_get_card_rval_(&crpix2, label_crpix2, &nHead, &pHead);
if (strcmp(pCtype1, "LAMBERT--X") == 0 &&
strcmp(pCtype2, "LAMBERT--Y") == 0) {
fits_get_card_ival_(&nsgp, label_lam_nsgp, &nHead, &pHead);
fits_get_card_rval_(&scale, label_lam_scal, &nHead, &pHead);
lambert_lb2xy(gall, galb, nsgp, scale, &xr, &yr);
*pX = xr + crpix1 - crval1 - 1.0;
*pY = yr + crpix2 - crval2 - 1.0;
} else if (strcmp(pCtype1, "GLON-ZEA") == 0 &&
strcmp(pCtype2, "GLAT-ZEA") == 0) {
q1 = fits_get_card_rval_(&cdelt1, label_cdelt1, &nHead, &pHead);
q2 = fits_get_card_rval_(&cdelt2, label_cdelt2, &nHead, &pHead);
if (q1 == TRUE_MWDUST && q2 == TRUE_MWDUST) {
cd1_1 = cdelt1;
cd1_2 = 0.0;
cd2_1 = 0.0;
cd2_2 = cdelt2;
} else {
fits_get_card_rval_(&cd1_1, label_cd1_1, &nHead, &pHead);
fits_get_card_rval_(&cd1_2, label_cd1_2, &nHead, &pHead);
fits_get_card_rval_(&cd2_1, label_cd2_1, &nHead, &pHead);
fits_get_card_rval_(&cd2_2, label_cd2_2, &nHead, &pHead);
}
q1 = fits_get_card_rval_(&lonpole, label_lonpole, &nHead, &pHead);
if (q1 == FALSE_MWDUST) lonpole = 180.0; /* default value */
/* ROTATION */
/* Equn (4) - degenerate case */
if (crval2 > 89.9999) {
theta = galb;