SourcePopulation.cc 101 KB
Newer Older
Andrew Strong's avatar
Andrew Strong committed
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
using namespace std;
#include<iostream>
#include<fstream>
#include"SourcePopulation.h"
#include"EGRET_catalogue.h"
#include"CAT.h"
#include"Sources.h" //AWS20081215
#include"pointing.h"// healpix class//AWS20090108
#include "fitsio.h" //AWS20120521

#include<valarray> //AWS20090902

#include <gsl/gsl_rng.h>            //AWS20100824
#include <gsl/gsl_randist.h>        //AWS20100824
#include <gsl/gsl_sf.h>             //AWS20100827

#include"slalib.h"                  //AWS20120612

#include "healpix_map.h"            //AWS20170112   
#include "healpix_map_fitsio.h"     //AWS20170112
#include "pointing.h"               //AWS20170112

int poisson(double  x);
int poisson(unsigned int initialize_seed);
double power_law_sampler(double  x_min, double x_max, double g);
int power_law_sampler(unsigned int initialize_seed);

////////////////////////////////////////////////////////////

int  SourcePopulation::init()
{

  cout<< "SourcePopulation::init"<<endl;


  pi        =  acos(-1.);
  rtd       =  180./pi;  // radians to degrees
  dtr       =   pi/180.; // degrees to radians
  kpc_to_cm =  3.08e21;

  R0 = 8.5 ;// Sun-Galactic centre in kpc
 
  strcpy(title,"no title");

45
46
47
  IDL_plot_control =  1; //AWS20200623
  IDL_psym         = -6; //AWS20200623

48
49
50
  strcpy(galdef_ID, "sourcepop_needs_galdef_ID" ); //AWS20200701
  strcpy(psfile_tag,"sourcepop_needs_psfile_tag"); //AWS20200701

Andrew Strong's avatar
Andrew Strong committed
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

  //AWS20170112  read and test sensitivity map
  {     
   cout<<"  reading and testing Fermi sensitivity skymap"<<endl;
   string mapname;
          mapname = "../FITS/"+ string(Fermi_sensitivity_file);

    cout<<"  reading  healpix skymap Fermi sensitivity from file "<<mapname<<endl;
    int colnum=1, hdunum=2;
    read_Healpix_map_from_fits(mapname,healpix_skymap_Fermi_sensitivity,colnum,hdunum);
    int pix=100;
    cout<<"value="<<healpix_skymap_Fermi_sensitivity[pix]<<endl;
   
  
   
    for (double b=-90;b<  90; b+=10) 
    for (double l=0  ;l<=360; l+=10) 
    {
     double theta=(90-b)*dtr;
     double phi  =    l *dtr;
     pointing pointing_=pointing(theta,phi);
     cout<<" Fermi sensitivity  l="<< l<<" b="<<b<<" theta="<<theta<<" phi="<<phi;
     cout<<" interpolated value="<<healpix_skymap_Fermi_sensitivity.interpolated_value(pointing_)<<endl;
    }
  }

  return 0;
}



//////////////////////////////////////////////////////

int  SourcePopulation::print(int options) // default options=1 in header
{
  int i;

  *txt_stream<< "SourcePopulation::print, options = "<<options<<endl;

 if(options==1)
 {



  *txt_stream<<" ***** Source Population parameters *****"<<endl<<endl;
  *txt_stream<< "title : "<< title        <<endl;
  *txt_stream<<"luminosity_function_type="<<luminosity_function_type<<endl;
  if(luminosity_function_type==1)
  *txt_stream<<"delta function L="<<L<<endl;
  if(luminosity_function_type==2)
  *txt_stream<<"power-law L_min="<<L_min <<" L_max="<<L_max <<" alpha_L="<<alpha_L <<endl;

  *txt_stream<<"alpha_R="<<alpha_R<<endl;
  *txt_stream<<" beta_R="<< beta_R<<endl;
  *txt_stream<<"zscale ="<<zscale <<endl;
  *txt_stream<<"alpha_z="<<alpha_z<<endl;
  *txt_stream<<"density0="<<density0<<endl;
  *txt_stream<<"oversample="<<oversample<<endl;


  *txt_stream<<"spectral_model="<<spectral_model<<endl;
  *txt_stream<<"spectrum_g_0="<<spectrum_g_0<<endl;
  *txt_stream<<"spectrum_br0="<<spectrum_br0<<endl;
  *txt_stream<<"spectrum_g_1="<<spectrum_g_1<<endl;
  *txt_stream<<"spectrum_br1="<<spectrum_br1<<endl;
  *txt_stream<<"spectrum_g_2="<<spectrum_g_2<<endl;

  *txt_stream<<"spectrum_g_0_sigma="<<spectrum_g_0_sigma<<endl;
  *txt_stream<<"spectrum_br0_sigma="<<spectrum_br0_sigma<<endl;
  *txt_stream<<"spectrum_g_1_sigma="<<spectrum_g_1_sigma<<endl;
  *txt_stream<<"spectrum_br1_sigma="<<spectrum_br1_sigma<<endl;
  *txt_stream<<"spectrum_g_2_sigma="<<spectrum_g_2_sigma<<endl;

124
  *txt_stream<<"user-defined source flux detection limit="<<flux_detection_limit<<endl;
Andrew Strong's avatar
Andrew Strong committed
125
126
127
128
129
130
131
132
133
134
135
136

  *txt_stream<<"longitude range 1 "<<long_min1<<" to "<<long_max1<<endl;
  *txt_stream<<"longitude range 2 "<<long_min2<<" to "<<long_max2<<endl;
  *txt_stream<<" latitude range 1 "<< lat_min1<<" to "<< lat_max1<<endl;
  *txt_stream<<" latitude range 2 "<< lat_min2<<" to "<< lat_max2<<endl;

  *txt_stream<<"number of spectral energies ="<<n_E<<endl;
  *txt_stream<<"spectral energies:" ; for (i=0;i<n_E;i++) *txt_stream<<E[i]<<" "; *txt_stream<<endl;

  *txt_stream<<"healpix skymap order="<<healpix_skymap_order<<endl;

  *txt_stream<<"Fermi sensitivity file="<< Fermi_sensitivity_file<<endl; //AWS201701113
137
138
139

  *txt_stream<<"IDL_plot_control="<<IDL_plot_control<<endl; //AWS20200623
  *txt_stream<<"IDL_psym="        <<IDL_psym<<endl;         //AWS20200623
140
141
142
143
144

  *txt_stream<<"galdef_ID=" <<galdef_ID<<endl;              //AWS20200701
  *txt_stream<<"psfile_tag="<<psfile_tag<<endl;             //AWS20200701


145
146
 
}
Andrew Strong's avatar
Andrew Strong committed
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



 if(options==2)
 {
   *txt_stream<<"source sample listing"<<endl<<endl;

  for(int i=0;i<n_sample;i++)

	{
	  *txt_stream<<"source "<<i<<":  R=" <<sample_R[i]<< " z="<<sample_z[i]<<" theta="<<sample_theta[i]
                                      <<" l="<<sample_l[i] <<" b="<<sample_b[i]
		     <<" distance="<<sample_d[i]    <<" L="<<L <<" flux="<<sample_flux[i];

          if(spectral_model==2||spectral_model==3)
           *txt_stream<<" g_0="<<sample_spectrum_g_0[i]<< " br0="<<sample_spectrum_br0[i]
                      <<" g_1="<<sample_spectrum_g_1[i]<< " br1="<<sample_spectrum_br1[i]<<" g_2="<<sample_spectrum_g_2[i];

          *txt_stream << endl <<" flux spectrum= " ;              for (int iE=0;iE<n_E    ;iE++){*txt_stream<< sample_flux_spectra       [i][iE]<<" ";}
          *txt_stream << endl <<" binned flux spectrum= " ;       for (int iE=0;iE<n_E_bin;iE++){*txt_stream<< sample_binned_flux_spectra[i][iE]<<" ";}

          *txt_stream      <<endl<<endl;
	}
  }

 if(options==3)
 {
   *txt_stream<<"source sample listing"<<endl<<endl;
   *txt_stream<<"l    b   flux>100 MeV index break index break index"<<endl<<endl;

  for(int i=0;i<n_sample;i++)

	{
	  *txt_stream<<  sample_l[i] <<" "<<sample_b[i]	<<" "<<sample_flux[i];

          if(spectral_model==2||spectral_model==3)
           *txt_stream<<"     "<<sample_spectrum_g_0[i]<< "     "<<sample_spectrum_br0[i]
                      <<"     "<<sample_spectrum_g_1[i]<< "     "<<sample_spectrum_br1[i]<<"     "<<sample_spectrum_g_2[i];

          *txt_stream      <<endl      ;
	}
  }



  *txt_stream<<"SourcePopulation:: print complete"<<endl<<endl;

  return 0;
}
///////////////////////////////////////////////////
double  SourcePopulation::density(double R, double z)
{
  
  double density_;
  //  density_=density0*exp(-abs(z)/alpha_z)*exp(-(R-R0)/alpha_R);

  //zscale=alpha_z;
  //beta_R=1./alpha_R;
  density_=density0*exp(-abs(z)/zscale) * pow(R/R0,alpha_R) * exp(-beta_R*(R-R0)/R0); //AWS20051202 for consistency with galprop

  return density_;

  return 0;
}
///////////////////////////////////////////////////
int    SourcePopulation::gen_sample(unsigned  seed)
{
  *txt_stream<< "SourcePopulation::gen_sample"<<endl;
  cout       << "SourcePopulation::gen_sample"<<endl;

  // general purpose case for any distribution
  double Rmax, zmax, dR, dz;
  double R1,R2,R_,z1,z2,z_,sample_dd;
  double theta, dtheta;
  double sinl, cosl;

  int nR, nz, iR, iz;
  int m_sample,m_sample_total;
  int i,ii;
  double density_,number_total,number_delta ;
  double solid_angle,solid_angle_long,solid_angle_lat,solid_angle_long_lat;
  double l,b,b1,b2;
  int il,ib;


 

  Rmax= 20.;
  zmax= 0.3;
  dR=0.201; // avoid having R0 on grid
  dz=0.01;


  nR=int(  Rmax/dR);
  nz=int(2*zmax/dz);
  
 

  
  number_total=0.;

  for (iR=0;iR<nR;iR++)
  for (iz=0;iz<nz;iz++)
    {
      R1  =         iR*dR; R2=R1+dR;
      z1  = -zmax + iz*dz; z2=z1+dz;
      R_=(R1+R2)/2;
      z_=(z1+z2)/2;
      density_=density(R_,z_);
      density_ *= oversample;                       //AWS20060118
      number_delta=density_ *pi*(R2*R2 - R1*R1) *dz;
      number_total+=number_delta;
    }


  *txt_stream<<"total number of sources ="<<number_total<<endl;

  n_sample    =long(number_total);  
  sample_R    =new double[n_sample];
  sample_z    =new double[n_sample];
  sample_theta=new double[n_sample];
  sample_l    =new double[n_sample];
  sample_b    =new double[n_sample];
  sample_d    =new double[n_sample];
  sample_flux =new double[n_sample];

  sample_flux100  =new double[n_sample]; //AWS20120615
  sample_flux1000 =new double[n_sample]; //AWS20120615
  sample_flux10000=new double[n_sample]; //AWS20120615

  sample_flux_spectrum_E_ref_low  =new double[n_sample]; //AWS20111001
  sample_flux_spectrum_E_ref_high =new double[n_sample]; //AWS20111001

   sample_flux_spectra        = new double*[n_sample];                                                                          //AWS20100817
   sample_binned_flux_spectra = new double*[n_sample];   ;                                                                      //AWS20100817
   for (int i=0;i<n_sample;i++){ sample_flux_spectra[i]=new double[n_E];  sample_binned_flux_spectra[i]=new double[n_E_bin];   }//AWS20100817

   sample_spectrum_g_0        = new double[n_sample];                                                                           //AWS20100823
   sample_spectrum_br0        = new double[n_sample];                                                                           //AWS20100823
   sample_spectrum_g_1        = new double[n_sample];                                                                           //AWS20100823
   sample_spectrum_br1        = new double[n_sample];                                                                           //AWS20100823
   sample_spectrum_g_2        = new double[n_sample];                                                                           //AWS20100823

  // initialize gsl for Gaussian sampling
  // see example in gsl User Manual

  cout<<" initializing gsl random number generator"<<endl;
      const gsl_rng_type * T;
      gsl_rng * r;
         
       /* create a generator chosen by the environment variable GSL_RNG_TYPE */
     
       gsl_rng_env_setup();
       T = gsl_rng_default;
       r = gsl_rng_alloc (T);

  cout<<" initialized  gsl random number generator"<<endl;

   // case of no dispersion in spectra
  if(spectral_model==1)
  {
   for (int i=0;i<n_sample;i++)                                                                                                 //AWS20100823
   {
    sample_spectrum_g_0[i] = spectrum_g_0;
    sample_spectrum_br0[i] = spectrum_br0;
    sample_spectrum_g_1[i] = spectrum_g_1;
    sample_spectrum_br1[i] = spectrum_br1;
    sample_spectrum_g_2[i] = spectrum_g_2;
   }
  }

   // case of uncorrelated dispersion in spectra
  if(spectral_model==2)
  {
    
   for (int i=0;i<n_sample;i++)                                                                                                 //AWS20100823
   {
    sample_spectrum_g_0[i] = spectrum_g_0 +  gsl_ran_gaussian ( r,  spectrum_g_0_sigma);

                             
    sample_spectrum_br0[i] = spectrum_br0 * pow(10.,  gsl_ran_gaussian ( r,  spectrum_br0_sigma)); // sigma for break is log10

    sample_spectrum_g_1[i] = sample_spectrum_g_0[i] + spectrum_g_1 - spectrum_g_0;    // keep size of spectral break
    sample_spectrum_br1[i] = spectrum_br1;                                            // keep second break
    sample_spectrum_g_2[i] = sample_spectrum_g_0[i] + spectrum_g_2 - spectrum_g_0;    // keep size of second break

    if(verbose==1)
    {
    cout<<" sample_spectrum_g_0="<<sample_spectrum_g_0[i]<<" sample_spectrum_br0="<<sample_spectrum_br0[i];
    cout<<" sample_spectrum_g_1="<<sample_spectrum_g_1[i]<<" sample_spectrum_br1="<<sample_spectrum_br1[i];
    cout<<" sample_spectrum_g_2="<<sample_spectrum_g_2[i]<<endl;
    }
   }
  }

   // case of correlated index-break dispersion in spectra
  if(spectral_model==3)
  {
   
    double X,Y,correlation_angle;
    double pi=acos(-1.0);
    correlation_angle=19.9; // degrees, from Alice
    double s=sin(correlation_angle*pi/180.);
    double c=cos(correlation_angle*pi/180.);

 

   for (int i=0;i<n_sample;i++)                                                                                               
   {
    X =   gsl_ran_gaussian ( r,  spectrum_g_0_sigma); // offset in rotated system centred on mean
    Y =   gsl_ran_gaussian ( r,  spectrum_br0_sigma);

    sample_spectrum_g_0[i] =          spectrum_g_0  +       X * c - Y * s;
    sample_spectrum_br0[i] = pow(10., spectrum_br0  +       X * s + Y * c);

    sample_spectrum_g_1[i] = sample_spectrum_g_0[i] + spectrum_g_1 - spectrum_g_0;    // keep size of spectral break
    sample_spectrum_br1[i] = spectrum_br1;                                            // keep second break
    sample_spectrum_g_2[i] = sample_spectrum_g_0[i] + spectrum_g_2 - spectrum_g_0;    // keep size of second break

    if(verbose==1)
    {
    cout<<" sample_spectrum_g_0="<<sample_spectrum_g_0[i]<<" sample_spectrum_br0="<<sample_spectrum_br0[i];
    cout<<" sample_spectrum_g_1="<<sample_spectrum_g_1[i]<<" sample_spectrum_br1="<<sample_spectrum_br1[i];
    cout<<" sample_spectrum_g_2="<<sample_spectrum_g_2[i]<<endl;
    }
   }
  }


  i=0;
  theta=0.;
  dtheta=.1;
  
  // the following are redundant since rand uses same seed in each routine
  srand            (seed); // initialize random number generator
  //  poisson          (seed); // initialize Poisson generator
  //  power_law_sampler(seed); // initialize power-law sampler





  //----------------------------------------------------------------- generate sample

  number_total=0.;
  m_sample_total =0;
  sample_flux_min=1e30;
  sample_flux_max=0.  ;
  sample_d_min   =1e30;
  sample_d_max   =0.  ;

  sample_total_flux=0;
  sample_total_L   =0;

  for (iR=0;iR<nR;iR++)
  for (iz=0;iz<nz;iz++)
    {
      R1  =         iR*dR; R2=R1+dR;
      z1  = -zmax + iz*dz; z2=z1+dz;
      R_=(R1+R2)/2;
      z_=(z1+z2)/2;
      density_=density(R_,z_);    
      density_ *= oversample;                       //AWS20060118                   
      number_delta=density_ *pi*(R2*R2 - R1*R1) *dz;
      number_total+=number_delta;
      m_sample=poisson(number_delta);
      m_sample_total+=m_sample;

      if(verbose==1)
      *txt_stream<<" R_=" <<R_<< " z_="<<z_ <<" number_delta="<<number_delta
           <<" number_total="<<number_total
           << " m_sample="<<m_sample<<" m_sample_total="<<m_sample_total<<endl;

      //    for(ii=0;ii<number_delta-0.5;ii++,i++)

      for(ii=0;ii<m_sample        ;ii++,i++)
        if(i<n_sample)
	{
	  sample_R[i]=R_;
	  sample_z[i]=z_;
 
          theta=2.0*pi*double(rand())/RAND_MAX;                      // angle at GC
          sample_theta[i]=theta;
          sample_d[i]=sqrt(R_*R_ + R0*R0 - 2.0* R_ *R0 *cos(theta) + z_ * z_ ); // distance to source in kpc
          sample_dd  =sqrt(R_*R_ + R0*R0 - 2.0* R_ *R0 *cos(theta)           ); // distance to source in kpc projected onto plane

          sinl= R_ / sample_dd   * sin(theta) ;                              //   sine rule
          cosl=(R0*R0 + sample_dd*sample_dd -  R_*R_)/(2.0 * sample_dd * R0);// cosine rule
	  sample_l[i]=atan2(sinl,cosl)               *rtd        ; // -180/+180 deg

          sample_b[i]=atan (sample_z[i] / sample_dd )*rtd;

	  if(luminosity_function_type==2)L=power_law_sampler(L_min,L_max,alpha_L);
          sample_flux[i]=L/(4.0*pi* pow(sample_d[i]*kpc_to_cm,2));

          if(sample_d[i]   <   sample_d_min) sample_d_min   =sample_d   [i];
          if(sample_d[i]   >   sample_d_max) sample_d_max   =sample_d   [i];
          if(sample_flux[i]<sample_flux_min){sample_flux_min=sample_flux[i]; i_flux_min=i;}
          if(sample_flux[i]>sample_flux_max){sample_flux_max=sample_flux[i]; i_flux_max=i;}


          sample_total_flux+= sample_flux[i];
          sample_total_L   += L;

	  // assign spectra AWS20100817
	  //        double spectrum_norm=integratepowbr(1.0,spectrum_g_0,spectrum_g_1,spectrum_g_2, spectrum_br0,spectrum_br1,E_ref_low,E_ref_high);

	  double spectrum_norm=integratepowbr(1.0,sample_spectrum_g_0[i],sample_spectrum_g_1[i],sample_spectrum_g_2[i],
                                                  sample_spectrum_br0[i],sample_spectrum_br1[i],E_ref_low,E_ref_high);//AWS20100823

          // spectral energies
          for (int iE=0;iE<n_E;iE++)
          {
	    
            
          double spectrum_factor=shapepowbr(1.0,sample_spectrum_g_0[i],sample_spectrum_g_1[i],sample_spectrum_g_2[i],
                                                sample_spectrum_br0[i],sample_spectrum_br1[i],E[iE])
	                                                              //  *pow(E[iE], 2.0 )// NB need *E^2 for compatibility with galprop (l,b) skymaps, applied when required
	                                   / spectrum_norm; //AWS20100823


	  //          *txt_stream<<" E= "<<E[iE]<<" spectrum_norm="<<spectrum_norm<<" spectrum factor with breaks="<<spectrum_factor<<endl;
	  //	  cout<<" br0="<<sample_spectrum_br0[i]<<" "<<" E= "<<E[iE]<<" spectrum_norm="<<spectrum_norm<<" spectrum factor with breaks="<<spectrum_factor<<endl;

           sample_flux_spectra[i][iE]=sample_flux[i]*spectrum_factor;

           if(spectral_model==3)
               sample_flux_spectra[i][iE]
            = flux_cutoff_powerlaw( E[iE], sample_spectrum_g_0[i], sample_spectrum_br0[i], E_ref_low, E_ref_high, sample_flux[i]);//AWS20100830


          }


	 // reference energies AWS20110901

           if(spectral_model==1 || spectral_model==2)
	   {
            double spectrum_factor=shapepowbr(1.0,sample_spectrum_g_0[i],sample_spectrum_g_1[i],sample_spectrum_g_2[i],
                                                sample_spectrum_br0[i],sample_spectrum_br1[i],E_ref_low)
	                                   / spectrum_norm; 

	    sample_flux_spectrum_E_ref_low[i]=sample_flux[i]*spectrum_factor;

                   spectrum_factor=shapepowbr(1.0,sample_spectrum_g_0[i],sample_spectrum_g_1[i],sample_spectrum_g_2[i],
                                                sample_spectrum_br0[i],sample_spectrum_br1[i],E_ref_high)
	                                   / spectrum_norm; 

            sample_flux_spectrum_E_ref_high[i]=sample_flux[i]*spectrum_factor;



	    // >100 MeV, 
	    double E_bin_low_  = 1.e2;
	    double E_bin_high_ = 1.e6;

                   spectrum_factor=integratepowbr(1.0,sample_spectrum_g_0[i],sample_spectrum_g_1[i],sample_spectrum_g_2[i], 
                                                   sample_spectrum_br0[i],sample_spectrum_br1[i],E_bin_low_,E_bin_high_)
                                           /spectrum_norm; //AWS20120615


	     sample_flux100[i]=sample_flux[i]*spectrum_factor;                                           //AWS20120615

             //cout<<"sample_flux100="<<sample_flux100[i]      ;

	    // >1000 MeV, 
	           E_bin_low_  = 1.e3;
	           E_bin_high_ = 1.e6;

                   spectrum_factor=integratepowbr(1.0,sample_spectrum_g_0[i],sample_spectrum_g_1[i],sample_spectrum_g_2[i], 
                                                   sample_spectrum_br0[i],sample_spectrum_br1[i],E_bin_low_,E_bin_high_)
                                           /spectrum_norm; //AWS20120615


	     sample_flux1000[i]=sample_flux[i]*spectrum_factor;                                           //AWS20120615

             //cout<<" sample_flux1000="<<sample_flux1000[i];

	    // >10000 MeV, 
	           E_bin_low_  = 1.e4;
	           E_bin_high_ = 1.e6;

                   spectrum_factor=integratepowbr(1.0,sample_spectrum_g_0[i],sample_spectrum_g_1[i],sample_spectrum_g_2[i], 
                                                   sample_spectrum_br0[i],sample_spectrum_br1[i],E_bin_low_,E_bin_high_)
                                           /spectrum_norm; //AWS20120615


	     sample_flux10000[i]=sample_flux[i]*spectrum_factor;                                           //AWS20120615

             //cout<<" sample_flux10000="<<sample_flux10000[i]<<endl;




           }


           if(spectral_model==3)
	   {
            sample_flux_spectrum_E_ref_low[i]
             = flux_cutoff_powerlaw
                               ( E_ref_low,  sample_spectrum_g_0[i], sample_spectrum_br0[i], E_ref_low, E_ref_high, sample_flux[i]);

            sample_flux_spectrum_E_ref_high[i]
             = flux_cutoff_powerlaw
                               ( E_ref_high, sample_spectrum_g_0[i], sample_spectrum_br0[i], E_ref_low, E_ref_high, sample_flux[i]);
           }


         //  binned energy ranges

         for (int iE=0;iE<n_E_bin;iE++)
         {
    

         double spectrum_factor=integratepowbr(1.0,sample_spectrum_g_0[i],sample_spectrum_g_1[i],sample_spectrum_g_2[i], sample_spectrum_br0[i],sample_spectrum_br1[i],E_bin_low[iE],E_bin_high[iE])	   /spectrum_norm; //AWS20100823


          sample_binned_flux_spectra[i][iE]=sample_flux[i]*spectrum_factor;

           if(spectral_model==3)
	         sample_binned_flux_spectra[i][iE]
             = flux_cutoff_powerlaw( E_bin_low[iE] ,E_bin_high[iE], sample_spectrum_g_0[i], sample_spectrum_br0[i], E_ref_low, E_ref_high, sample_flux[i]);//AWS20100830


	  //      *txt_stream<<" E= "<<E_bin_low[iE]<<" - "<<E_bin_high[iE]<<" source binned spectrum factor="<<spectrum_factor<<endl;
          }

	      if(verbose==1)
	{
	  *txt_stream<<"i="<<i<<" R=" <<sample_R[i]<< " z="<<sample_z[i]<<" theta="<<sample_theta[i]
		      <<" L="<<L
			     <<" distance="<<sample_d[i]   <<" l="<<sample_l[i]    <<" b="<<sample_b[i]      <<" flux="<<sample_flux[i];
          *txt_stream  <<" flux spectrum= " ;              for (int iE=0;iE<n_E    ;iE++){*txt_stream<< sample_flux_spectra      [i][iE]<<" ";}
          *txt_stream  <<" binned flux spectrum= " ;       for (int iE=0;iE<n_E_bin;iE++){*txt_stream<< sample_binned_flux_spectra[i][iE]<<" ";}

          *txt_stream      <<endl;
	}
 



	}// if
    }// for


   *txt_stream<<"oversampling factor for sources     = "<<oversample<<" (following values include this factor ! ) " <<endl;     //AWS2010824
   *txt_stream<<"total number of sources from density= "<<number_total<<endl;
   *txt_stream<<"total number of sources simulated   = "<<m_sample_total<<endl;
   *txt_stream<<"total luminosity of sources = "<<sample_total_L<<endl;
   *txt_stream<<" sample distance min= "<<sample_d_min <<" max= "<<sample_d_max<<endl;                                           //AWS2010824
   *txt_stream<<" flux min= "<<sample_flux_min<<" flux max= "<<sample_flux_max<<endl;
   *txt_stream<<" l of min flux = "<< sample_l[i_flux_min]<<"    l of max flux = "<< sample_l[i_flux_max] <<endl;                //AWS2010824
   *txt_stream<<" b of min flux = "<< sample_b[i_flux_min]<<"    b of max flux = "<< sample_b[i_flux_max] <<endl;
   *txt_stream<<" sample  global         total flux             = "<<sample_total_flux<<endl;

   if(verbose==2){int options=2; print(options);}
   if(verbose==3){int options=3; print(options);}

   *txt_stream<<"SourcePopulation:: gen_sample  complete"<<endl<<endl;

   return 0;

}

/////////////////////////////////////////////////////////

int SourcePopulation::analyse_sample()
{
  int i,ii;
  double density_,number_total,number_delta ;
  double solid_angle,solid_angle_long,solid_angle_lat,solid_angle_long_lat;
  double l,b,b1,b2;
  int il,ib;
  double spectrum_factor;
  double spectrum_norm;
  double long_min1a,long_min2a,long_max1a,long_max2a;

  pointing apointing; // healpix coordinates of a pixel //AWS20090109
  int ipix;           // healpix pixel index            //AWS20090109

  int skymap_method = 2; // 1=scale total intensity (original method), 2 = from source spectra (new method) //AWS20100818

  *txt_stream<< " >> SourcePopulation::analyse_sample"<<endl;
  *txt_stream<< "title :"<< title        <<endl;

   //----------------------------------------------------------  generate differential logN-logS
   lnS_min =-20.0001; // to make printing better
   lnS_max =-00;
   dlnS    =.20;

  n_dlnN_dlnS=  int((lnS_max-lnS_min)/dlnS);
    dlnN_dlnS        =new double [ n_dlnN_dlnS];
    dlnN_dlnS_deg    =new double [ n_dlnN_dlnS];
    dlnN_dlnS_int    =new double [ n_dlnN_dlnS];
    dlnN_dlnS_int_deg=new double [ n_dlnN_dlnS];
    FS               =new double [ n_dlnN_dlnS];
    FS_int           =new double [ n_dlnN_dlnS];

   dlnN_dlnS_soplimit        =new double [ n_dlnN_dlnS]; //AWS20170123
   dlnN_dlnS_sublimit        =new double [ n_dlnN_dlnS]; //AWS20170123
   dlnN_dlnS_int_soplimit    =new double [ n_dlnN_dlnS]; //AWS20170123
   dlnN_dlnS_int_sublimit    =new double [ n_dlnN_dlnS]; //AWS20170123
   FS_soplimit               =new double [ n_dlnN_dlnS]; //AWS20170123
   FS_sublimit               =new double [ n_dlnN_dlnS]; //AWS20170123
   FS_int_soplimit           =new double [ n_dlnN_dlnS]; //AWS20170123
   FS_int_sublimit           =new double [ n_dlnN_dlnS]; //AWS20170123

  sample_l_min=new double[ n_dlnN_dlnS];
  sample_l_max=new double[ n_dlnN_dlnS];
  sample_l_av =new double[ n_dlnN_dlnS];
  sample_b_min=new double[ n_dlnN_dlnS];
  sample_b_max=new double[ n_dlnN_dlnS];
  sample_b_av =new double[ n_dlnN_dlnS];

  longprof_nlong             =   int(360/longprof_dlong);
  longprof_intensity         =new double[longprof_nlong];
  longprof_intensity_sublimit=new double[longprof_nlong];
  longprof_sourcecnt         =new double[longprof_nlong];
  longprof_sourcecnt_sublimit=new double[longprof_nlong];

   latprof_nlat               =   int(180/latprof_dlat );
   latprof_intensity         =new double[ latprof_nlat ];
   latprof_intensity_sublimit=new double[ latprof_nlat ];
   latprof_sourcecnt         =new double[ latprof_nlat ];
   latprof_sourcecnt_sublimit=new double[ latprof_nlat ];

  for (ii=0;ii< n_dlnN_dlnS;ii++)
  {
   dlnN_dlnS      [ii]=0;
   dlnN_dlnS_deg  [ii]=0;

   FS             [ii]=0;

   dlnN_dlnS_soplimit[ii]=0; //AWS20170123
   dlnN_dlnS_sublimit[ii]=0; //AWS20170123

   sample_l_min[ii]= 1e30;
   sample_b_min[ii]= 1e30;
   sample_l_max[ii]=-1e30;
   sample_b_max[ii]=-1e30;

   sample_l_av[ii]=0;
   sample_b_av[ii]=0;
  }

  for (ii=0;ii<longprof_nlong;ii++)
  {
   longprof_intensity         [ii]=0.;
   longprof_intensity_sublimit[ii]=0.;
   longprof_sourcecnt         [ii]=0.;
   longprof_sourcecnt         [ii]=0.;
   }


  for (ii=0;ii< latprof_nlat ;ii++)
  {
    latprof_intensity         [ii]=0.;
    latprof_intensity_sublimit[ii]=0.;
    latprof_sourcecnt         [ii]=0.;
    latprof_sourcecnt         [ii]=0.;
   }


  sample_selected_total_flux         =0.;
  sample_selected_total_flux_sublimit=0.;
  sample_selected_total_flux_soplimit=0.;

  sample_selected_sourcecnt          =0.;
  sample_selected_sourcecnt_sublimit =0.;
  sample_selected_sourcecnt_soplimit =0.;

  sample_selected_d_min              = 1e30;
  sample_selected_d_min_sublimit     = 1e30;
  sample_selected_d_min_soplimit     = 1e30;

  sample_selected_d_max              =-1e30;
  sample_selected_d_max_sublimit     =-1e30;
  sample_selected_d_max_soplimit     =-1e30;


  sample_selected_d_av               = 0.;         
  sample_selected_d_av_sublimit      = 0.;
  sample_selected_d_av_soplimit      = 0.;

  //----------------------------------------------------------------------------------
  // set limits in range -180<l<+180

  // but this is problematic, instead convert to 0-360 and compare with original ranges  AWS20120522

  long_min1a=long_min1;
  long_min2a=long_min2;
  long_max1a=long_max1;
  long_max2a=long_max2;

  if(long_min1a  >= +180.){long_min1a -=360.; long_max1a -=360.;}
  if(long_min2a  >= +180.){long_min2a -=360.; long_max2a -=360.;}

  // more robust in marginal cases like 0.0-180.0, 180.0-360.0 but not correct    AWS20120521
  //  if(long_max1a  >= +180.){long_min1a -=360.; long_max1a -=360.;}           //AWS20120521
  //  if(long_max2a  >= +180.){long_min2a -=360.; long_max2a -=360.;}           //AWS20120521

   for (i=0;i<n_sample;i++)
     {

       ii=int( (log10(sample_flux[i])-lnS_min)/dlnS  );

       if(verbose==3)
        *txt_stream<<"before selection :sample i="<<i<<" l="<<sample_l[i]<<" sample_flux[i]="<<sample_flux[i]
         << " ii="<<ii
	 <<" long_min1 ="<<long_min1 <<" long_max1 ="<<long_max1 
	 <<" long_min1a="<<long_min1a<<" long_max1a="<<long_max1a
	 <<" long_min2 ="<<long_min2 <<" long_max2 ="<<long_max2 
	 <<" long_min2a="<<long_min2a<<" long_max2a="<<long_max2a
         <<endl;

       if(ii>=0 && ii<n_dlnN_dlnS)
       {
         //          -180 <     sample_l     <180
         //             0 <long_min,long_max <360


	 /*
	 if(  (    ( sample_l[i]  >= long_min1a     && sample_l[i]  <= long_max1a  )  
		 ||( sample_l[i]  >= long_min2a     && sample_l[i]  <= long_max2a  ) )
	 */

	 // put l in range 0-360
         double sample_l_0360= sample_l[i]; if ( sample_l_0360 < 0.0)  sample_l_0360+= 360.0; //AWS20120522 

	 if(  (    ( sample_l_0360 >= long_min1  && sample_l_0360<= long_max1   )             //AWS20120522 
		 ||( sample_l_0360 >= long_min2  && sample_l_0360<= long_max2   ) )           //AWS20120522 
               &&
              (    ( sample_b[i]   >=  lat_min1  && sample_b[i]  <=  lat_max1  )  
		 ||( sample_b[i   ]>=  lat_min2  && sample_b[i]  <=  lat_max2  ) )
           )

       {

	 //AWS20170116  replace flux_detection by value in sensitivity map
         {
          double theta=(90-sample_b[i])*dtr;
          double phi  =    sample_l[i] *dtr;
          pointing pointing_=pointing(theta,phi);
          cout<<"source statistics sample i="<<i<<" Fermi sensitivity  l="<<sample_l[i] <<" b="<<sample_b[i]
              <<" theta="<<theta<<" phi="<<phi;
	  //          cout<<" interpolated value="<<healpix_skymap_Fermi_sensitivity.interpolated_value(pointing_)<<endl;
794
795
          flux_detection_limit_sensitivity_file = healpix_skymap_Fermi_sensitivity.interpolated_value(pointing_); //AWS20200713 was flux_detection_limit
          cout<<" flux detection limit_sensitivity_file="<<flux_detection_limit_sensitivity_file<<" sample flux="<<sample_flux[i]<<endl;
Andrew Strong's avatar
Andrew Strong committed
796
797
798
799
800
         }

        dlnN_dlnS  [ii]++;
        FS         [ii]+= sample_flux[i];

801
        if(sample_flux[i]<flux_detection_limit_sensitivity_file)    //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
802
803
804
805
806
	{                                          //AWS20170123
         dlnN_dlnS_sublimit[ii]++;                 //AWS20170123
                FS_sublimit[ii]+= sample_flux[i];  //AWS20170123
	}                                          //AWS20170123

807
        if(sample_flux[i]>flux_detection_limit_sensitivity_file)    //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
	{                                          //AWS20170123
         dlnN_dlnS_soplimit[ii]++;                 //AWS20170123
                FS_soplimit[ii]+= sample_flux[i]; //AWS20170123
	}                                          //AWS20170123


	sample_l_av[ii]+=abs(sample_l[i]);
	sample_b_av[ii]+=abs(sample_b[i]);
	
        if (abs(sample_l[i])< sample_l_min[ii]) sample_l_min[ii]= abs(sample_l[i]);
        if (abs(sample_b[i])< sample_b_min[ii]) sample_b_min[ii]= abs(sample_b[i]);
        if (abs(sample_l[i])> sample_l_max[ii]) sample_l_max[ii]= abs(sample_l[i]);
        if (abs(sample_b[i])> sample_b_max[ii]) sample_b_max[ii]= abs(sample_b[i]);



         sample_selected_sourcecnt++;
         sample_selected_total_flux         +=sample_flux[i];
         sample_selected_d_av               +=sample_d   [i];
         if (sample_d[i] < sample_selected_d_min) sample_selected_d_min= sample_d[i];
         if (sample_d[i] > sample_selected_d_max) sample_selected_d_max= sample_d[i];



832
	 if(sample_flux[i]<flux_detection_limit_sensitivity_file) //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
833
834
835
836
837
838
839
840
841
	{
         sample_selected_sourcecnt_sublimit++;
         sample_selected_total_flux_sublimit+=sample_flux[i];
         sample_selected_d_av_sublimit      +=sample_d   [i];

         if (sample_d[i] < sample_selected_d_min_sublimit) sample_selected_d_min_sublimit= sample_d[i];
         if (sample_d[i] > sample_selected_d_max_sublimit) sample_selected_d_max_sublimit= sample_d[i];
	}

842
	 if(sample_flux[i]>flux_detection_limit_sensitivity_file)  //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
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
	{
         sample_selected_sourcecnt_soplimit++;
         sample_selected_total_flux_soplimit+=sample_flux[i];
         sample_selected_d_av_soplimit      +=sample_d   [i];

         if (sample_d[i] < sample_selected_d_min_soplimit) sample_selected_d_min_soplimit= sample_d[i];
         if (sample_d[i] > sample_selected_d_max_soplimit) sample_selected_d_max_soplimit= sample_d[i];
	}

      if(verbose==3)
	    	  *txt_stream<<"selected sample i="<<i<<" R=" <<sample_R[i]<< " z="<<sample_z[i]<<" theta="<<sample_theta[i]
		      <<" L="<<L
                      <<" distance="<<sample_d[i]   <<" l="<<sample_l[i]    <<" b="<<sample_b[i]      <<" flux="<<sample_flux[i]                  <<endl;

        } // if      

	// longitude profile
	 //      if(sample_b[i]>=longprof_b_min && sample_b[i]<=longprof_b_max)

        if         (( sample_b[i]>=  lat_min1     && sample_b[i]<=  lat_max1  )  
		 ||(  sample_b[i]>=  lat_min2     && sample_b[i]<=  lat_max2  ) )
	  {
	    ii=int((sample_l[i]+180.)/longprof_dlong);
	    if(ii >= 0 && ii < longprof_nlong) 
	    {
             longprof_intensity         [ii] += sample_flux[i];

870
             if(sample_flux[i]<flux_detection_limit_sensitivity_file) //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
871
872
873
874
             longprof_intensity_sublimit[ii] += sample_flux[i];

             longprof_sourcecnt         [ii] ++               ;

875
             if(sample_flux[i]<flux_detection_limit_sensitivity_file) //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
             longprof_sourcecnt_sublimit[ii] ++               ;

	    }//if ii
	  }//if sample_b
	
      

	//  latitude profile
	//  if(sample_l[i]>= latprof_l_min && sample_l[i]<= latprof_l_max)

	 if   (    ( sample_l[i]  >= long_min1a     && sample_l[i]  <= long_max1a  )  
		 ||( sample_l[i]  >= long_min2a     && sample_l[i]  <= long_max2a  ) )
	  {
	    ii=int((sample_b[i]+ 90.)/ latprof_dlat );
	    if(ii >= 0 && ii <  latprof_nlat ) 
	    {
              latprof_intensity         [ii] += sample_flux[i];

894
	      if(sample_flux[i]<flux_detection_limit_sensitivity_file) //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
895
896
897
898
              latprof_intensity_sublimit[ii] += sample_flux[i];

              latprof_sourcecnt         [ii] ++               ;

899
	      if(sample_flux[i]<flux_detection_limit_sensitivity_file) //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
              latprof_sourcecnt_sublimit[ii] ++               ;

	    }//if ii
	  }//if sample_l
	
       }//if


     }// i 

   if                                        (sample_selected_sourcecnt >0)
   sample_selected_d_av             /=        sample_selected_sourcecnt         ;
   if                                        (sample_selected_sourcecnt_sublimit >0)
   sample_selected_d_av_sublimit    /=        sample_selected_sourcecnt_sublimit;
   if                                        (sample_selected_sourcecnt_soplimit >0)
   sample_selected_d_av_soplimit    /=        sample_selected_sourcecnt_soplimit;



   //--------------------------------------------------------------------------------

   dlnN_dlnS_total_N=0.;
   dlnN_dlnS_total_S=0.;


  for (ii=0;ii< n_dlnN_dlnS;ii++)
  { 
    if( dlnN_dlnS  [ii] > 0)
    {
     sample_l_av[ii] /= dlnN_dlnS  [ii];
     sample_b_av[ii] /= dlnN_dlnS  [ii];
    }

    solid_angle=  (long_max1-long_min1)*(sin(lat_max1*dtr)-sin(lat_min1*dtr))*rtd
                + (long_max2-long_min2)*(sin(lat_max1*dtr)-sin(lat_min1*dtr))*rtd
                + (long_max1-long_min1)*(sin(lat_max2*dtr)-sin(lat_min2*dtr))*rtd
                + (long_max2-long_min2)*(sin(lat_max2*dtr)-sin(lat_min2*dtr))*rtd;

    dlnN_dlnS_deg[ii]= dlnN_dlnS[ii] / solid_angle;

    dlnN_dlnS_total_N += dlnN_dlnS  [ii];
    dlnN_dlnS_total_S += dlnN_dlnS  [ii] * (pow(10,lnS_min+(ii)*dlnS) + pow(10,lnS_min+(ii+1)*dlnS))/2.0 ;

  }

  // integral N(S)
  for (ii=0;ii< n_dlnN_dlnS;ii++)
  {
    dlnN_dlnS_int    [ii]=0;
    dlnN_dlnS_int_deg[ii]=0;
    FS_int           [ii]=0;

    dlnN_dlnS_int_sublimit    [ii]=0;                         //AWS20170123
           FS_int_sublimit    [ii]=0;                         //AWS20170123

    dlnN_dlnS_int_soplimit    [ii]=0;                         //AWS20170123
           FS_int_soplimit    [ii]=0;                         //AWS20170123

    for(i=ii;i< n_dlnN_dlnS;i++)
    {    
     dlnN_dlnS_int    [ii]+= dlnN_dlnS      [i];
     dlnN_dlnS_int_deg[ii]+= dlnN_dlnS_deg  [i];
     FS_int           [ii]+= FS             [i];

     dlnN_dlnS_int_sublimit    [ii]+= dlnN_dlnS_sublimit [i]; //AWS20170123
            FS_int_sublimit    [ii]+=    FS_int_sublimit [i]; //AWS20170123

     dlnN_dlnS_int_soplimit    [ii]+= dlnN_dlnS_soplimit [i]; //AWS20170123
            FS_int_soplimit    [ii]+=    FS_int_soplimit [i]; //AWS20170123

    }
  }


  //  solid_angle_long=longprof_dlong*(longprof_b_max-longprof_b_min)/(rtd*rtd);

  solid_angle_long=longprof_dlong*dtr * (sin(lat_max1*dtr)-sin(lat_min1*dtr))
                  +longprof_dlong*dtr * (sin(lat_max2*dtr)-sin(lat_min2*dtr));
  *txt_stream<<"longitude profile solid_angle     ="<<solid_angle_long<<" sr"<<endl;

  //  solid_angle_lat = latprof_dlat *( latprof_l_max- latprof_l_min)/(rtd*rtd);
  // *txt_stream<<" latitude profile solid_angle     ="<<solid_angle_lat <<" sr"<<endl;


  for (ii=0;ii<longprof_nlong;ii++)
  {
   longprof_intensity         [ii]/=solid_angle_long;
   longprof_intensity_sublimit[ii]/=solid_angle_long;
  }

  for (ii=0;ii< latprof_nlat ;ii++)
  {
   b=-90.+ (ii+.5)* latprof_dlat;
   // solid_angle_lat = latprof_dlat *( latprof_l_max- latprof_l_min)/(rtd*rtd)*cos(b*dtr);
    b1=-90.+ ii* latprof_dlat;
    b2= b1 +     latprof_dlat;
    solid_angle_lat = (long_max1-long_min1) *dtr  *(sin(b2*dtr) - sin(b1*dtr))
                    + (long_max2-long_min2) *dtr  *(sin(b2*dtr) - sin(b1*dtr));
    latprof_intensity         [ii]/=solid_angle_lat ;
    latprof_intensity_sublimit[ii]/=solid_angle_lat ;
  }



  //-------------------------------------------------------------------- generate skymaps

  // (l,b) maps
   skymap_sourcecnt         .init(longprof_nlong, latprof_nlat,1);
   skymap_sourcecnt_sublimit.init(longprof_nlong, latprof_nlat,1);
   skymap_sourcecnt_soplimit.init(longprof_nlong, latprof_nlat,1);

   skymap_intensity_spectrum         .init(longprof_nlong, latprof_nlat,n_E);
   skymap_intensity_spectrum_sublimit.init(longprof_nlong, latprof_nlat,n_E);
   skymap_intensity_spectrum_soplimit.init(longprof_nlong, latprof_nlat,n_E);

   skymap_intensity         .init(longprof_nlong, latprof_nlat,n_E_bin);
   skymap_intensity_sublimit.init(longprof_nlong, latprof_nlat,n_E_bin);
   skymap_intensity_soplimit.init(longprof_nlong, latprof_nlat,n_E_bin);

   // healpix maps
   healpix_skymap_sourcecnt                    = Skymap<double> (healpix_skymap_order, 1);       //AWS20090107
   healpix_skymap_sourcecnt_sublimit           = Skymap<double> (healpix_skymap_order, 1);       //AWS20090107
   healpix_skymap_sourcecnt_soplimit           = Skymap<double> (healpix_skymap_order, 1);       //AWS20090107


   healpix_skymap_intensity_spectrum           = Skymap<double> (healpix_skymap_order, n_E);     //AWS20090107
   healpix_skymap_intensity_spectrum_sublimit  = Skymap<double> (healpix_skymap_order, n_E);     //AWS20090107
   healpix_skymap_intensity_spectrum_soplimit  = Skymap<double> (healpix_skymap_order, n_E);     //AWS20090107

   healpix_skymap_intensity                    = Skymap<double> (healpix_skymap_order, n_E_bin); //AWS20090107
   healpix_skymap_intensity_sublimit           = Skymap<double> (healpix_skymap_order, n_E_bin); //AWS20090107
   healpix_skymap_intensity_soplimit           = Skymap<double> (healpix_skymap_order, n_E_bin); //AWS20090107

   healpix_skymap_sourcecnt           = 0.;
   healpix_skymap_sourcecnt_sublimit  = 0.;
   healpix_skymap_sourcecnt_soplimit  = 0.;

   healpix_skymap_intensity           = 0.;
   healpix_skymap_intensity_sublimit  = 0.;
   healpix_skymap_intensity_soplimit  = 0.;

   healpix_skymap_intensity_spectrum           = 0.; //AWS20100818
   healpix_skymap_intensity_spectrum_sublimit  = 0.; //AWS20100818
   healpix_skymap_intensity_spectrum_soplimit  = 0.; //AWS20100818


   //   cout<<"testing ang2pix:";
   //   apointing = pointing(0.5,0.1);
   //   cout<<healpix_skymap_sourcecnt.ang2pix(apointing)<<endl; // since Skymap is derived from Healpix_Base


   //---------------------------------------------------------------------------------------------------------


   cout<<" generating skymaps of sources"<<endl;

   for (i=0;i<n_sample;i++)
   {

	 //AWS20170116  replace flux_detection by value in sensitivity map
         {
          double theta=(90-sample_b[i])*dtr;
          double phi  =    sample_l[i] *dtr;
          pointing pointing_=pointing(theta,phi);
          cout<<"skymap generation source sample i="<<i<<" Fermi sensitivity  l="<<sample_l[i] <<" b="<<sample_b[i]
              <<" theta="<<theta<<" phi="<<phi;
	  //          cout<<" interpolated value="<<healpix_skymap_Fermi_sensitivity.interpolated_value(pointing_)<<endl;
1067
1068
          flux_detection_limit_sensitivity_file = healpix_skymap_Fermi_sensitivity.interpolated_value(pointing_); //AWS20200713
          cout<<" flux detection limit_sensitivity_file="<<flux_detection_limit_sensitivity_file<<" sample flux="<<sample_flux[i]<<endl;
Andrew Strong's avatar
Andrew Strong committed
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
         }

       ii=int( (log10(sample_flux[i])-lnS_min)/dlnS  );
       //   *txt_stream<<"sample_flux[i]="<<sample_flux[i]<< " ii="<<ii<<endl;
       if(ii>=0 && ii<n_dlnN_dlnS)
       {
	 // for skymap use -180/+180 degrees as in galplot internally
            if(sample_l[i]>=0.0 )
	    il=int((sample_l[i]     )/longprof_dlong);
            if(sample_l[i]< 0.0 )
	    il=int((sample_l[i]+360.)/longprof_dlong);

	    ib=int((sample_b[i]+ 90.)/ latprof_dlat );

	    if(  il >= 0 && il < longprof_nlong  
	      && ib >= 0 && ib <  latprof_nlat )
	    {
             skymap_intensity.d2[il][ib].s[0]       += sample_flux[i];

1088
             if(sample_flux[i]<flux_detection_limit_sensitivity_file) //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
1089
1090
             skymap_intensity_sublimit.d2[il][ib].s[0]       += sample_flux[i];

1091
             if(sample_flux[i]>flux_detection_limit_sensitivity_file) //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
1092
1093
1094
1095
             skymap_intensity_soplimit.d2[il][ib].s[0]       += sample_flux[i];

             skymap_sourcecnt.d2[il][ib].s[0]       ++               ;

1096
             if(sample_flux[i]<flux_detection_limit_sensitivity_file) //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
1097
1098
             skymap_sourcecnt_sublimit.d2[il][ib].s[0]       ++               ;

1099
             if(sample_flux[i]>flux_detection_limit_sensitivity_file) //AWS20200713
Andrew Strong's avatar
Andrew Strong committed
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
             skymap_sourcecnt_soplimit.d2[il][ib].s[0]       ++               ;


	    }//if il ib


	    // healpix                                                                                          //AWS20090108

	    apointing=pointing( (90.-sample_b[i])*dtr , sample_l[i]*dtr   ); // input is theta, phi => 90-b, l
            ipix=healpix_skymap_sourcecnt.ang2pix(apointing);
	    //           cout<<"SourcePopulation healpix maps: l="<<sample_l[i]<<" b="<<sample_b[i]<<" ipix="<<ipix<<endl;

                                                      healpix_skymap_sourcecnt         [ipix][0]++;
1113
1114
            if(sample_flux[i]<flux_detection_limit_sensitivity_file)   healpix_skymap_sourcecnt_sublimit[ipix][0]++;  
            if(sample_flux[i]>flux_detection_limit_sensitivity_file)   healpix_skymap_sourcecnt_soplimit[ipix][0]++;
Andrew Strong's avatar
Andrew Strong committed
1115
1116
1117
1118
1119


           if(skymap_method==1)//AWS20100820
	   {
	                                              healpix_skymap_intensity         [ipix][0]+= sample_flux[i];
1120
1121
            if(sample_flux[i]<flux_detection_limit_sensitivity_file)   healpix_skymap_intensity_sublimit[ipix][0]+= sample_flux[i];
            if(sample_flux[i]>flux_detection_limit_sensitivity_file)   healpix_skymap_intensity_soplimit[ipix][0]+= sample_flux[i];
Andrew Strong's avatar
Andrew Strong committed
1122
1123
1124
1125
1126
1127
1128
1129
	   }

	    // using source spectra directly 
            if(skymap_method==2)//AWS20100818
	    {
	      for(int iE=0;iE<n_E;iE++)
	      {
	                                                 healpix_skymap_intensity_spectrum         [ipix][iE]+= sample_flux_spectra[i][iE];
1130
1131
               if(sample_flux[i]<flux_detection_limit_sensitivity_file)   healpix_skymap_intensity_spectrum_sublimit[ipix][iE]+= sample_flux_spectra[i][iE];
               if(sample_flux[i]>flux_detection_limit_sensitivity_file)   healpix_skymap_intensity_spectrum_soplimit[ipix][iE]+= sample_flux_spectra[i][iE];
Andrew Strong's avatar
Andrew Strong committed
1132
1133
1134
1135
1136
	      }

	      for(int iE=0;iE<n_E_bin;iE++)
	      {
	                                                 healpix_skymap_intensity                  [ipix][iE]+= sample_binned_flux_spectra[i][iE];
1137
1138
               if(sample_flux[i]<flux_detection_limit_sensitivity_file)   healpix_skymap_intensity_sublimit         [ipix][iE]+= sample_binned_flux_spectra[i][iE];
               if(sample_flux[i]>flux_detection_limit_sensitivity_file)   healpix_skymap_intensity_soplimit         [ipix][iE]+= sample_binned_flux_spectra[i][iE];
Andrew Strong's avatar
Andrew Strong committed
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
	      }

	    }


       }//if ii

       //     cout<<" processed source #"<<i<<endl;


     }// for i


   // check sums of l,b and healpix maps                                                                                            //AWS20090901
   *txt_stream<<" completed adding sources to skymaps"<<endl;                                                                       //AWS20090901
   double skymap_intensity_total         =0.;    for(il=0;il< longprof_nlong;il++)  for(ib=0;ib<  latprof_nlat ;ib++)skymap_intensity_total         +=    skymap_intensity            .d2[il][ib].s[0];
   double skymap_intensity_sublimit_total=0.;    for(il=0;il< longprof_nlong;il++)  for(ib=0;ib<  latprof_nlat ;ib++)skymap_intensity_sublimit_total+=    skymap_intensity_sublimit   .d2[il][ib].s[0];
   double skymap_intensity_soplimit_total=0.;    for(il=0;il< longprof_nlong;il++)  for(ib=0;ib<  latprof_nlat ;ib++)skymap_intensity_soplimit_total+=    skymap_intensity_soplimit   .d2[il][ib].s[0];


   *txt_stream<<"sum  before applying solid angle         skymap_intensity           "<<        skymap_intensity_total         <<endl;//AWS20090901
   *txt_stream<<"sum  before applying solid angle         skymap_intensity_sublimit  "<<        skymap_intensity_sublimit_total<<endl;//AWS20090901
   *txt_stream<<"sum  before applying solid angle         skymap_intensity_soplimit  "<<        skymap_intensity_soplimit_total<<endl;//AWS20090901

   *txt_stream<<"sum  before applying solid angle healpix_skymap_intensity           "<<healpix_skymap_intensity         .sum()<<endl;//AWS20090901
   *txt_stream<<"sum  before applying solid angle healpix_skymap_intensity_sublimit  "<<healpix_skymap_intensity_sublimit.sum()<<endl;//AWS20090901
   *txt_stream<<"sum  before applying solid angle healpix_skymap_intensity_soplimit  "<<healpix_skymap_intensity_soplimit.sum()<<endl;//AWS20090901




   for(il=0;il< longprof_nlong;il++)
   for(ib=0;ib<  latprof_nlat ;ib++)
   {
    b=-90.+ (ib+.5)* latprof_dlat;
    b1=-90.+ ib* latprof_dlat;
    b2= b1 +     latprof_dlat;
    //solid_angle_long_lat = longprof_dlong * latprof_dlat /(rtd*rtd) * cos(b*dtr);*txt_stream<< solid_angle_long_lat;
    solid_angle_long_lat = longprof_dlong / rtd *(sin(b2*dtr) - sin(b1*dtr));
    //*txt_stream<<" "<< solid_angle_long_lat<<endl;
    skymap_intensity         .d2[il][ib].s[0] /=solid_angle_long_lat;
    skymap_intensity_sublimit.d2[il][ib].s[0] /=solid_angle_long_lat;
    skymap_intensity_soplimit.d2[il][ib].s[0] /=solid_angle_long_lat;
   }

   healpix_skymap_intensity          /= healpix_skymap_intensity.solidAngle();  //AWS20090108
   healpix_skymap_intensity_sublimit /= healpix_skymap_intensity.solidAngle();  //AWS20090108
   healpix_skymap_intensity_soplimit /= healpix_skymap_intensity.solidAngle();  //AWS20090108

   healpix_skymap_intensity_spectrum          /= healpix_skymap_intensity.solidAngle();  //AWS20100818
   healpix_skymap_intensity_spectrum_sublimit /= healpix_skymap_intensity.solidAngle();  //AWS20100818
   healpix_skymap_intensity_spectrum_soplimit /= healpix_skymap_intensity.solidAngle();  //AWS20100818



   //   cout<<" completed multiplying  skymaps by solid angle"<<endl;

   if(skymap_method==1) //AWS20100818
     {

   spectrum_norm=integratepowbr(1.0,spectrum_g_0,spectrum_g_1,spectrum_g_2, spectrum_br0,spectrum_br1,E_ref_low,E_ref_high);

   // === skymaps at spectral energies

   for (ii=0;ii<n_E;ii++)
   {
     //   spectrum_factor=pow(E[ii]/E[0],-spectrum_g + 2 ) // *E^2 for compatibility with galprop skymaps
     //     spectrum_factor=pow(E[ii],-spectrum_g + 2 ) // *E^2 for compatibility with galprop skymaps
     //               * (spectrum_g -1)/(pow(E_ref_low    ,-spectrum_g+1)-pow(E_ref_high    ,-spectrum_g+1));

     //    *txt_stream<<" E= "<<E[ii]<<" spectrum factor without breaks="<<spectrum_factor<<endl;

     

      spectrum_factor=shapepowbr(1.0,spectrum_g_0,spectrum_g_1,spectrum_g_2, spectrum_br0,spectrum_br1,E[ii])
                        *pow(E[ii], 2.0 )// *E^2 for compatibility with galprop (l,b) skymaps
               	        / spectrum_norm;

    *txt_stream<<" E= "<<E[ii]<<" spectrum factor with breaks="<<spectrum_factor<<endl;

    for(il=0;il< longprof_nlong;il++)
    for(ib=0;ib<  latprof_nlat ;ib++)
    {

       skymap_intensity_spectrum          .d2[il][ib].s[ii] = skymap_intensity          .d2[il][ib].s[0] * spectrum_factor;
       skymap_intensity_spectrum_sublimit .d2[il][ib].s[ii] = skymap_intensity_sublimit .d2[il][ib].s[0] * spectrum_factor;
       skymap_intensity_spectrum_soplimit .d2[il][ib].s[ii] = skymap_intensity_soplimit .d2[il][ib].s[0] * spectrum_factor;


    } //for il ib


    //  cout<<" completed multiplying  l,b  spectral skymaps by spectral factor"<<endl;

    // healpix  //AWS20090108
    // healpix without *E^2, compatible with galprop healpix skymaps
      spectrum_factor=shapepowbr(1.0,spectrum_g_0,spectrum_g_1,spectrum_g_2, spectrum_br0,spectrum_br1,E[ii])
               	        / spectrum_norm;

    for (ipix=0;ipix<healpix_skymap_intensity_spectrum.Npix();ipix++)
    {
     healpix_skymap_intensity_spectrum         [ipix][ii] = healpix_skymap_intensity         [ipix][0] * spectrum_factor;
     healpix_skymap_intensity_spectrum_sublimit[ipix][ii] = healpix_skymap_intensity_sublimit[ipix][0] * spectrum_factor;
     healpix_skymap_intensity_spectrum_soplimit[ipix][ii] = healpix_skymap_intensity_soplimit[ipix][0] * spectrum_factor;
    }


   } //for ii

  }//skymap_method==1

   //  cout<<" completed multiplying  healpix spectral skymaps by spectral factor"<<endl;



   // all-sky average intensity
   valarray<double> healpix_skymap_intensity_spectrum_sublimit_av(n_E)   ;
   valarray<double> healpix_skymap_intensity_spectrum_soplimit_av(n_E)   ;
                    healpix_skymap_intensity_spectrum_sublimit_av=0.;
                    healpix_skymap_intensity_spectrum_soplimit_av=0.;

   for(ii=0;ii<n_E;ii++) 
   {
    for (ipix=0;ipix<healpix_skymap_intensity_spectrum.Npix();ipix++)
     {
      healpix_skymap_intensity_spectrum_sublimit_av[ii]+=healpix_skymap_intensity_spectrum_sublimit[ipix][ii];
      healpix_skymap_intensity_spectrum_soplimit_av[ii]+=healpix_skymap_intensity_spectrum_soplimit[ipix][ii];
     }
      healpix_skymap_intensity_spectrum_sublimit_av[ii]/= (healpix_skymap_intensity.Npix()*oversample); 
      healpix_skymap_intensity_spectrum_soplimit_av[ii]/= (healpix_skymap_intensity.Npix()*oversample); 
   }

  if(FALSE)//AWS20110826
  {
   for(ii=0;ii<n_E;ii++) *txt_stream<<"healpix all sky average intensity spectrum sublimit at energy "<<E[ii]<<" : "<<  healpix_skymap_intensity_spectrum_sublimit_av[ii]<<" "<<endl;
   for(ii=0;ii<n_E;ii++) *txt_stream<<"healpix all sky average intensity spectrum soplimit at energy "<<E[ii]<<" : "<<  healpix_skymap_intensity_spectrum_soplimit_av[ii]<<" "<<endl;
  } 



   // === skymaps in binned energy ranges


   if(skymap_method==1)
     {
   spectrum_norm=integratepowbr(1.0,spectrum_g_0,spectrum_g_1,spectrum_g_2, spectrum_br0,spectrum_br1,E_ref_low,E_ref_high);//AWS20100818

   //   for (ii=0;ii<n_E_bin;ii++)
   for (ii=1;ii<n_E_bin;ii++) //AWS20100820 to avoid overwriting first energy range at start
   {
     //   spectrum_factor=(pow(E_bin_low[ii],-spectrum_g+1)-pow(E_bin_high[ii],-spectrum_g+1))
     //                   /(pow(E_ref_low    ,-spectrum_g+1)-pow(E_ref_high    ,-spectrum_g+1));
     //    *txt_stream<<" E= "<<E_bin_low[ii]<<" - "<<E_bin_high[ii]<<" spectrum factor="<<spectrum_factor<<endl;

    spectrum_factor=integratepowbr(1.0,spectrum_g_0,spectrum_g_1,spectrum_g_2, spectrum_br0,spectrum_br1,E_bin_low[ii],E_bin_high[ii]) //AWS20060123
                   /spectrum_norm;
    *txt_stream<<" E= "<<E_bin_low[ii]<<" - "<<E_bin_high[ii]<<" spectrum factor="<<spectrum_factor<<endl;

    for(il=0;il< longprof_nlong;il++)
    for(ib=0;ib<  latprof_nlat ;ib++)
    {

       skymap_intensity          .d2[il][ib].s[ii] = skymap_intensity          .d2[il][ib].s[0];
       skymap_intensity_sublimit .d2[il][ib].s[ii] = skymap_intensity_sublimit .d2[il][ib].s[0];;
       skymap_intensity_soplimit .d2[il][ib].s[ii] = skymap_intensity_soplimit .d2[il][ib].s[0];;

       skymap_intensity          .d2[il][ib].s[ii] *= spectrum_factor;
       skymap_intensity_sublimit .d2[il][ib].s[ii] *= spectrum_factor;
       skymap_intensity_soplimit .d2[il][ib].s[ii] *= spectrum_factor;

    } //for il ib

    //  cout<<" completed multiplying  l,b  binned energy range skymaps by spectral factor"<<endl;

    // healpix  
    for (ipix=0;ipix<healpix_skymap_intensity_spectrum.Npix();ipix++)                                             //AWS20090108
    {
     healpix_skymap_intensity         [ipix][ii] = healpix_skymap_intensity          [ipix][0] * spectrum_factor; //AWS20090108
     healpix_skymap_intensity_sublimit[ipix][ii] = healpix_skymap_intensity_sublimit [ipix][0] * spectrum_factor; //AWS20090108
     healpix_skymap_intensity_soplimit[ipix][ii] = healpix_skymap_intensity_soplimit [ipix][0] * spectrum_factor; //AWS20090108
    }


   } //for ii


   ii=0; //AWS20100820 assign the first bin correctly now

    spectrum_factor=integratepowbr(1.0,spectrum_g_0,spectrum_g_1,spectrum_g_2, spectrum_br0,spectrum_br1,E_bin_low[ii],E_bin_high[ii]) //AWS20060123
                   /spectrum_norm;
    *txt_stream<<" E= "<<E_bin_low[ii]<<" - "<<E_bin_high[ii]<<" spectrum factor="<<spectrum_factor<<endl;

    for(il=0;il< longprof_nlong;il++)
    for(ib=0;ib<  latprof_nlat ;ib++)
    {

       skymap_intensity          .d2[il][ib].s[ii] = skymap_intensity          .d2[il][ib].s[0];
       skymap_intensity_sublimit .d2[il][ib].s[ii] = skymap_intensity_sublimit .d2[il][ib].s[0];;
       skymap_intensity_soplimit .d2[il][ib].s[ii] = skymap_intensity_soplimit .d2[il][ib].s[0];;

       skymap_intensity          .d2[il][ib].s[ii] *= spectrum_factor;
       skymap_intensity_sublimit .d2[il][ib].s[ii] *= spectrum_factor;
       skymap_intensity_soplimit .d2[il][ib].s[ii] *= spectrum_factor;

    } //for il ib

    //  cout<<" completed multiplying  l,b  binned energy range skymaps by spectral factor"<<endl;

    // healpix  
    for (ipix=0;ipix<healpix_skymap_intensity_spectrum.Npix();ipix++)                                             //AWS20090108
    {
     healpix_skymap_intensity         [ipix][ii] = healpix_skymap_intensity          [ipix][0] * spectrum_factor; //AWS20090108
     healpix_skymap_intensity_sublimit[ipix][ii] = healpix_skymap_intensity_sublimit [ipix][0] * spectrum_factor; //AWS20090108
     healpix_skymap_intensity_soplimit[ipix][ii] = healpix_skymap_intensity_soplimit [ipix][0] * spectrum_factor; //AWS20090108
    }


  }//skymap_method==1



   //  cout<<" completed multiplying  healpix  binned energy range skymaps by spectral factor"<<endl;

  //-------------------------------------------------------------------- correct all results for oversampling    //AWS20060118
    for (ii=0;ii< n_dlnN_dlnS;ii++)
    {
     dlnN_dlnS        [ii]/= oversample;
     dlnN_dlnS_int    [ii]/= oversample;
     dlnN_dlnS_deg    [ii]/= oversample;
     dlnN_dlnS_int_deg[ii]/= oversample;
     FS               [ii]/= oversample;
     FS_int           [ii]/= oversample;

         dlnN_dlnS_sublimit [ii]/= oversample; //AWS20170123
     dlnN_dlnS_int_sublimit [ii]/= oversample; //AWS20170123
                FS_sublimit [ii]/= oversample; //AWS20170123
            FS_int_sublimit [ii]/= oversample; //AWS20170123

         dlnN_dlnS_soplimit [ii]/= oversample; //AWS20170123
     dlnN_dlnS_int_soplimit [ii]/= oversample; //AWS20170123
                FS_soplimit [ii]/= oversample; //AWS20170123
            FS_int_soplimit [ii]/= oversample; //AWS20170123


    }

     skymap_intensity_spectrum          /= oversample;
     skymap_intensity_spectrum_sublimit /= oversample;
     skymap_intensity_spectrum_soplimit /= oversample;

     skymap_intensity                   /= oversample;
     skymap_intensity_sublimit          /= oversample;
     skymap_intensity_soplimit          /= oversample;
    
     dlnN_dlnS_total_N                  /= oversample;
     dlnN_dlnS_total_S                  /= oversample;

     sample_selected_sourcecnt          /= oversample;
     sample_selected_sourcecnt_soplimit /= oversample;
     sample_selected_sourcecnt_sublimit /= oversample;
     sample_selected_total_flux         /= oversample;
     sample_selected_total_flux_soplimit/= oversample;
     sample_selected_total_flux_sublimit/= oversample;

     //  cout<<" completed dividing  l,b skymaps by oversample"<<endl;

     // healpix
     healpix_skymap_intensity_spectrum          /= oversample;
     healpix_skymap_intensity_spectrum_sublimit /= oversample;
     healpix_skymap_intensity_spectrum_soplimit /= oversample;

     healpix_skymap_intensity                   /= oversample;
     healpix_skymap_intensity_sublimit          /= oversample;
     healpix_skymap_intensity_soplimit          /= oversample;


     //  cout<<" completed dividing  healpix   skymaps by oversample"<<endl;


     // assign energy grid for healpix skymaps 
     valarray<double> skymap_energy_spectrum_grid(n_E);
     for (int iE=0;iE<n_E;iE++){skymap_energy_spectrum_grid[iE]=E[iE];}
     healpix_skymap_intensity_spectrum         .setSpectra(skymap_energy_spectrum_grid);
     healpix_skymap_intensity_spectrum_sublimit.setSpectra(skymap_energy_spectrum_grid);
     healpix_skymap_intensity_spectrum_soplimit.setSpectra(skymap_energy_spectrum_grid);

     valarray<double> skymap_energy_bins_E_low (n_E_bin);
     valarray<double> skymap_energy_bins_E_high(n_E_bin);
     for (int iE_bin=0;iE_bin<n_E_bin;iE_bin++){skymap_energy_bins_E_low [iE_bin]=E_bin_low [iE_bin];}
     for (int iE_bin=0;iE_bin<n_E_bin;iE_bin++){skymap_energy_bins_E_high[iE_bin]=E_bin_high[iE_bin];}

     healpix_skymap_intensity         .setSpectra(skymap_energy_bins_E_low, skymap_energy_bins_E_high);
     healpix_skymap_intensity_sublimit.setSpectra(skymap_energy_bins_E_low, skymap_energy_bins_E_high);
     healpix_skymap_intensity_soplimit.setSpectra(skymap_energy_bins_E_low, skymap_energy_bins_E_high);




     // write to FITS files
     int write_to_fits =1;//0; //AWS20140930 AWS20150612
     if( write_to_fits==1) //AWS20140930

     //         if(TRUE)//AWS20110902 try again AWS20140930
     // if(FALSE)//AWS20110817 was causing CCfitsError

 {
  cout<<" writing population synthesis skymaps to healpix files"<<endl;

  string skymap_filename;             //AWS20140131
  string fits_directory = "../FITS/"; //AWS20140131  not yet specified in class, so define as in Configure.cc for now

  if(skymap_method==1)
    {
     healpix_skymap_intensity         .write("../FITS/galplot_healpix_skymap_intensity_method_1.fits");
     healpix_skymap_intensity_sublimit.write("../FITS/galplot_healpix_skymap_intensity_sublimit_method_1.fits");
     healpix_skymap_intensity_soplimit.write("../FITS/galplot_healpix_skymap_intensity_soplimit_method_1.fits");
 
     healpix_skymap_intensity_spectrum         .write("../FITS/galplot_healpix_skymap_intensity_spectrum_method_1.fits");
     healpix_skymap_intensity_spectrum_sublimit.write("../FITS/galplot_healpix_skymap_intensity_spectrum_sublimit_method_1.fits");
     healpix_skymap_intensity_spectrum_soplimit.write("../FITS/galplot_healpix_skymap_intensity_spectrum_soplimit_method_1.fits");
    }

  if(skymap_method==2)
    {
     cout<<" writing population synthesis method 2 intensity skymaps to healpix files"<<endl;

     

     skymap_filename = fits_directory + string(title) + "_healpix_skymap_intensity_method_2.fits";                  //AWS20140131
                                                          healpix_skymap_intensity         .write(skymap_filename); //AWS20140131


     skymap_filename = fits_directory + string(title) + "_healpix_skymap_intensity_sublimit_method_2.fits";         //AWS20140131
                                                          healpix_skymap_intensity_sublimit.write(skymap_filename); //AWS20140131


     skymap_filename = fits_directory + string(title) + "_healpix_skymap_intensity_soplimit_method_2.fits";         //AWS20140131
                                                          healpix_skymap_intensity_soplimit.write(skymap_filename); //AWS20140131

							  /*                                                          AWS20140131
     healpix_skymap_intensity         .write("../FITS/galplot_healpix_skymap_intensity_method_2.fits");
     healpix_skymap_intensity_sublimit.write("../FITS/galplot_healpix_skymap_intensity_sublimit_method_2.fits");
     healpix_skymap_intensity_soplimit.write("../FITS/galplot_healpix_skymap_intensity_soplimit_method_2.fits");
							  */
     


     cout<<" writing population synthesis intensity spectrum  method 2 skymaps to healpix files"<<endl;

    
     skymap_filename = fits_directory + string(title) + "_healpix_skymap_intensity_spectrum_method_2.fits";                  //AWS20140131
                                                          healpix_skymap_intensity_spectrum         .write(skymap_filename); //AWS20140131


     skymap_filename = fits_directory + string(title) + "_healpix_skymap_intensity_spectrum_sublimit_method_2.fits";         //AWS20140131
                                                          healpix_skymap_intensity_spectrum_sublimit.write(skymap_filename); //AWS20140131


     skymap_filename = fits_directory + string(title) + "_healpix_skymap_intensity_spectrum_soplimit_method_2.fits";         //AWS20140131
                                                          healpix_skymap_intensity_spectrum_soplimit.write(skymap_filename); //AWS20140131

							  /*                                                                   AWS20140131
     healpix_skymap_intensity_spectrum         .write("../FITS/galplot_healpix_skymap_intensity_spectrum_method_2.fits");
     healpix_skymap_intensity_spectrum_sublimit.write("../FITS/galplot_healpix_skymap_intensity_spectrum_sublimit_method_2.fits");
     healpix_skymap_intensity_spectrum_soplimit.write("../FITS/galplot_healpix_skymap_intensity_spectrum_soplimit_method_2.fi

							  */

    }

     cout<<" writing population synthesis source count skymaps to healpix files"<<endl;


     skymap_filename = fits_directory + string(title) + "_healpix_skymap_sourcecnt.fits";                                      //AWS20140131
                                                          healpix_skymap_sourcecnt.write(skymap_filename);                     //AWS20140131

							 /*
     healpix_skymap_sourcecnt         .write("../FITS/galplot_healpix_skymap_sourcecnt.fits");
							 */

 } // if write_to_fits

         cout<<" printing results to file"<<endl;

  //--------------------------------------------------------------------  print results

   *txt_stream<<"source counts for region "<<long_min1 <<" < l < "<<long_max1 <<",  "<<long_min2 <<" < l < "<<long_max2<<" /  "
                                    << lat_min1 <<" < b < "<< lat_max1 <<",  "<< lat_min2 <<" < b < "<< lat_max2<<endl;
   *txt_stream<<"converted to      region "<<long_min1a<<" < l < "<<long_max1a<<",  "<<long_min2a<<" < l < "<<long_max2a<<endl;
                                
1529
   cout<<"writing summary table to results txt for "<< title<<endl;         //AWS20200622
Andrew Strong's avatar
Andrew Strong committed
1530

1531
   *txt_stream<<"summary table of N(S) for "<< title <<endl;                //AWS20200622
Andrew Strong's avatar
Andrew Strong committed
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555

   for (ii=0;ii< n_dlnN_dlnS;ii++)
   {
       if( dlnN_dlnS[ii]>0)
       *txt_stream <<"ii="<<ii
            <<" lnS="<<       lnS_min+ii*dlnS  <<"/"<<       lnS_min+(ii+1)*dlnS
	    <<" S="  <<pow(10,lnS_min+ii*dlnS) <<"/"<<pow(10,lnS_min+(ii+1)*dlnS)
            <<" N(S)= "<<  dlnN_dlnS    [ii]
            <<" N(>S)= "<< dlnN_dlnS_int[ii]
 //         <<" logN=  " << log10(dlnN_dlnS[ii]+1e-10)
  	    <<" N(S) deg^-2 ="  <<   dlnN_dlnS_deg    [ii]
  	    <<" N(>S) deg^-2 ="  <<  dlnN_dlnS_int_deg[ii]
            <<" F(S) = "<<         FS    [ii]
            <<" F(>S)= "<<         FS_int[ii]
            <<" l_av="<<   sample_l_av [ii]                
            <<" b_av="<<   sample_b_av [ii]  
            <<" l_min="<<  sample_l_min[ii]                
            <<" l_max="         <<  sample_l_max[ii]  
            <<" b_min="<<  sample_b_min[ii]                
            <<" b_max="         <<  sample_b_max[ii]  

            <<endl;
    }

1556
1557
1558
   // output for external plotting routines                                 //AWS20200622

   *txt_stream<<"format for external plotting routines"<<endl;              //AWS20200622
1559
1560
1561

   // idl

1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
   *txt_stream<<"S = ["; 
   for (ii=0;ii< n_dlnN_dlnS;ii++)
       { *txt_stream<<pow(10, (lnS_min+ii*dlnS + lnS_min+(ii+1)*dlnS)/2. );
           if(ii<n_dlnN_dlnS-1)    *txt_stream<<",";
       }
   *txt_stream<<"]";
   *txt_stream<<";  idl format";
   *txt_stream<<endl;

  
   *txt_stream<<"NS = ["; 
   for (ii=0;ii< n_dlnN_dlnS;ii++)
       { *txt_stream<<dlnN_dlnS[ii];
           if(ii<n_dlnN_dlnS-1)    *txt_stream<<",";
       }
   *txt_stream<<"]";
   *txt_stream<<";  idl format";
   *txt_stream<<endl;

1581

1582
1583
1584
1585

   //   *txt_stream<<"device,file='"<<title<<".ps'          ;idl"<<endl;

   if(IDL_plot_control==1)  // start plot
1586
1587
   {
    *txt_stream<<"set_plot,'ps'                         ;idl"<<endl;
1588
1589
    *txt_stream<<"device,file='"<<"sourcepop_NS"<<"_"<<galdef_ID<<"_"<<psfile_tag<<".ps' ;idl"  <<endl;
    *txt_stream<<              "gv sourcepop_NS"<<"_"<<galdef_ID<<"_"<<psfile_tag<<".ps &    "  <<endl;
1590
1591
1592
    *txt_stream<<"!p.font=0                             ;idl"<<endl;
   }
  
1593

1594
   if(IDL_plot_control==1) // start plot
1595
     *txt_stream<<"plot,S,NS,/xlog,/ylog,xrange=[1e-13,1e-5],yrange=[0.1,1e4], xstyle=1,ystyle=1, psym="<<IDL_psym<<", xtitle='S,  cm!e-2!n s!e-1!n',ytitle='N(S)', charsize=1.0,title='"<<galdef_ID<<"_"<<psfile_tag<<"',/nodata ; idl"<<endl;
1596

1597
1598


1599

1600
   if(IDL_plot_control==1||IDL_plot_control==2) // overplot on start plot
1601
     *txt_stream<<"oplot,S,NS,psym="<<IDL_psym<<"   ; idl"<<endl;
1602

1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631

   // python format

  *txt_stream<<"title='" <<title<< "' # python format"<<endl;


  *txt_stream<<"S = ["; 
   for (ii=0;ii< n_dlnN_dlnS;ii++)
       { *txt_stream<<pow(10, (lnS_min+ii*dlnS + lnS_min+(ii+1)*dlnS)/2. );
           if(ii<n_dlnN_dlnS-1)    *txt_stream<<",";
       }
   *txt_stream<<"]";
   *txt_stream<<" #  python format";
   *txt_stream<<endl;

  
   *txt_stream<<"NS = ["; 
   for (ii=0;ii< n_dlnN_dlnS;ii++)
       { *txt_stream<<dlnN_dlnS[ii];
           if(ii<n_dlnN_dlnS-1)    *txt_stream<<",";
       }
   *txt_stream<<"]";
   *txt_stream<<" #  python format";
   *txt_stream<<endl;

   // end of format for external plotting routines



Andrew Strong's avatar
Andrew Strong committed
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
   *txt_stream<<"(oversampling corrected) dlnN_dlnS:  total N ="<< dlnN_dlnS_total_N  <<"           total S ="<< dlnN_dlnS_total_S<< endl;

   *txt_stream<<" sample region corrected for oversampling factor ="<<oversample                         <<endl;
   *txt_stream<<" sample region selected total sources            ="<<sample_selected_sourcecnt          <<endl;
   *txt_stream<<" sample region selected total sources above limit="<<sample_selected_sourcecnt_soplimit <<endl;
   *txt_stream<<" sample region selected total sources below limit="<<sample_selected_sourcecnt_sublimit <<endl;
   *txt_stream<<" sample region selected total flux               ="<<sample_selected_total_flux         <<endl;
   *txt_stream<<" sample region selected total flux   above limit ="<<sample_selected_total_flux_soplimit<<endl;
   *txt_stream<<" sample region selected total flux   below limit ="<<sample_selected_total_flux_sublimit<<endl;
   *txt_stream<<" sample region selected average distance         ="<<sample_selected_d_av               <<endl;
   *txt_stream<<" sample region selected average dist below limit ="<<sample_selected_d_av_sublimit      <<endl;
   *txt_stream<<" sample region selected average dist above limit ="<<sample_selected_d_av_soplimit      <<endl;
   *txt_stream<<" sample region selected min     distance         ="<<sample_selected_d_min              <<endl;
   *txt_stream<<" sample region selected min     dist below limit ="<<sample_selected_d_min_sublimit     <<endl;
   *txt_stream<<" sample region selected min     dist above limit ="<<sample_selected_d_min_soplimit     <<endl;
   *txt_stream<<" sample region selected max     distance         ="<<sample_selected_d_max              <<endl;
   *txt_stream<<" sample region selected max     dist below limit ="<<sample_selected_d_max_sublimit     <<endl;
   *txt_stream<<" sample region selected max     dist above limit ="<<sample_selected_d_max_soplimit     <<endl;

   if(FALSE)//AWS20110826
   {
   *txt_stream<<"longitude profile: " << lat_min1<<" < b < "<< lat_max1<<",  "<< lat_min2<<" < b < "<< lat_max2<<endl;

  for (ii=0;ii<longprof_nlong;ii++)
  {
   l=-180.+ (ii+.5)*longprof_dlong;
   *txt_stream<<"l="<<l
       <<" intensity="         << longprof_intensity         [ii]
       <<" below limit="       << longprof_intensity_sublimit[ii]
       <<" source count="      << longprof_sourcecnt         [ii]
       <<" below limit="       << longprof_sourcecnt_sublimit[ii]
       <<" above limit="       << longprof_sourcecnt         [ii]-longprof_sourcecnt_sublimit[ii]
       <<" / "                 <<(longprof_sourcecnt         [ii]-longprof_sourcecnt_sublimit[ii])/(solid_angle_long*rtd*rtd) <<" deg-2"
       <<endl;
  }


  *txt_stream<<" latitude profile: "<<long_min1 <<" < l < "<<long_max1 <<",  "<<long_min2 <<" < l < "<<long_max2<<endl;
  *txt_stream<<" converted to      "<<long_min1a<<" < l < "<<long_max1a<<",  "<<long_min2a<<" < l < "<<long_max2a<<endl;

  for (ii=0;ii< latprof_nlat ;ii++)
  {
   b=-90.+ (ii+.5)* latprof_dlat;
   //   solid_angle_lat = latprof_dlat *( latprof_l_max- latprof_l_min)/(rtd*rtd)/pow(cos(b*dtr),2);
    b1=-90.+ ii* latprof_dlat;
    b2= b1 +     latprof_dlat;
    solid_angle_lat = (long_max1-long_min1) *dtr  *(sin(b2*dtr) - sin(b1*dtr))
                    + (long_max2-long_min2) *dtr  *(sin(b2*dtr) - sin(b1*dtr));
   *txt_stream<<"b="<<b
       <<" intensity="         <<  latprof_intensity         [ii]
       <<" below limit="       <<  latprof_intensity_sublimit[ii]
       <<" source count="      <<  latprof_sourcecnt         [ii]
       <<" below limit="       <<  latprof_sourcecnt_sublimit[ii]
       <<" above limit="       <<  latprof_sourcecnt         [ii]- latprof_sourcecnt_sublimit[ii]
       <<" / "                 <<( latprof_sourcecnt         [ii]- latprof_sourcecnt_sublimit[ii])
                                  /(solid_angle_lat*rtd*rtd) <<" deg-2"
       <<endl;
  }
 }//if(FALSE)

  if(verbose==2)
  {
  *txt_stream<<endl;

  *txt_stream<<"skymap: intensity spectrum"<<endl;
  skymap_intensity_spectrum.print();
  *txt_stream<<"skymap: intensity spectrum   below  detection limit"<<endl;
  skymap_intensity_spectrum_sublimit.print();
  *txt_stream<<"skymap: intensity spectrum   above  detection limit"<<endl;
  skymap_intensity_spectrum_soplimit.print();

  *txt_stream<<"skymap: intensity"<<endl;
  skymap_intensity.print();
  *txt_stream<<"skymap: intensity    below detection limit"<<endl;
  skymap_intensity_sublimit.print();
  *txt_stream<<"skymap: intensity    above detection limit"<<endl;
  skymap_intensity_soplimit.print();

  *txt_stream<<"skymap: source count"<<endl;
  skymap_sourcecnt.print();
  *txt_stream<<"skymap:  source count below detection limit"<<endl;
  skymap_sourcecnt_sublimit.print();
  *txt_stream<<"skymap: source count  above detection limit"<<endl;
  skymap_sourcecnt_soplimit.print();
  }


  *txt_stream<< " << SourcePopulation:analyse_sample complete"<<endl<<endl;

  cout       << " << SourcePopulation:analyse_sample"<<endl;

  return 0;
}

/////////////////////////////////////////////////////////
 int SourcePopulation::gen_sample_EGRET_catalogue(int options)
{

  int i_sample,i_source;
  double R_,z_,theta_,d_;
  EGRET_catalogue egret_catalogue;

  
  egret_catalogue .read(0);
  egret_catalogue. select(options);
  egret_catalogue .print(*txt_stream);

  
  *txt_stream<<"total number of EGRET sources ="<<egret_catalogue .n_source<<endl;

  n_sample    =long(egret_catalogue.n_source);  
  sample_R    =new double[n_sample];
  sample_z    =new double[n_sample];
  sample_theta=new double[n_sample];
  sample_l    =new double[n_sample];
  sample_b    =new double[n_sample];
  sample_d    =new double[n_sample];
  sample_flux =new double[n_sample];

   sample_flux_spectra        = new double*[n_sample];                                                                          //AWS20100818
   sample_binned_flux_spectra = new double*[n_sample];   ;                                                                      //AWS20100818
   for (int i=0;i<n_sample;i++){ sample_flux_spectra[i]=new double[n_E];  sample_binned_flux_spectra[i]=new double[n_E_bin];   }//AWS20100818

  // values from catalogue
  i_sample=0;
  for (i_source=0;i_source<n_sample;i_source++)
    {
      if(egret_catalogue.selected[i_source]==1)
	{
        sample_l[i_sample]= egret_catalogue.l[i_source];
     if(sample_l[i_sample] >180.)
        sample_l[i_sample]-=360.;           // SourcePopulation uses -180<l<+180
     
     sample_b   [i_sample]= egret_catalogue.b[i_source];
     sample_flux[i_sample]= egret_catalogue.F[i_source]*1e-8; // unit of EGRET catalogue fluxes = 1e-8  AWS20051206
    
     if(verbose==4)
       *txt_stream<<"selected EGRET source "<< egret_catalogue.name[i_source]<<"  l="<< sample_l[i_sample]<<" b="<< sample_b[i_sample]<<" flux="<< sample_flux[i_sample]<<endl;

     i_sample++;
	}
    }

  n_sample=i_sample;

  oversample=1.0; // AWS200601018

  // fictitious values to complete SourcePopulation
  R_    = 5.;
  z_    = 0.;
 theta_ = 0.;
 d_     = 5.;



  for (i_sample=0;i_sample<n_sample;i_sample++)
    {
     sample_R    [i_sample]= R_;
     sample_z    [i_sample]= z_;
     sample_theta[i_sample]= theta_;
     sample_d    [i_sample]= d_;
    }


  

  return 0;
}
/////////////////////////////////////////////////////////
 int SourcePopulation::gen_sample_SPI_catalogue(char *directory, char* filename, int options)
{

  int i_sample,i_source;
  double R_,z_,theta_,d_;
  CAT spi_catalogue;

  
  spi_catalogue .read(directory,filename);
  
  //  spi_catalogue .print(*txt_stream);
  spi_catalogue .print();

  
  *txt_stream<<"total number of SPI sources ="<<spi_catalogue .n_sources<<endl;

  n_sample    =long(spi_catalogue.n_sources);  
  sample_R    =new double[n_sample];
  sample_z    =new double[n_sample];
  sample_theta=new double[n_sample];
  sample_l    =new double[n_sample];
  sample_b    =new double[n_sample];
  sample_d    =new double[n_sample];
  sample_flux =new double[n_sample];

   sample_flux_spectra        = new double*[n_sample];                                                                          //AWS20100818
   sample_binned_flux_spectra = new double*[n_sample];   ;                                                                      //AWS20100818
   for (int i=0;i<n_sample;i++){ sample_flux_spectra[i]=new double[n_E];  sample_binned_flux_spectra[i]=new double[n_E_bin];   }//AWS20100818

  // values from catalogue
  i_sample=0;
  for (i_source=0;i_source<n_sample;i_source++)
    {
   
        sample_l[i_sample]= spi_catalogue.longitude[i_source];
     if(sample_l[i_sample] >180.)
        sample_l[i_sample]-=360.;           // SourcePopulation uses -180<l<+180
     
     sample_b   [i_sample]= spi_catalogue.latitude[i_source];
     sample_flux[i_sample]= spi_catalogue.flux    [i_source];
    
     if(verbose==4)
       *txt_stream<<"selected SPI source "<< spi_catalogue.name[i_source]<<"  l="<< sample_l[i_sample]<<" b="<< sample_b[i_sample]<<" flux="<< sample_flux[i_sample]<<endl;

     i_sample++;
	
    }

  n_sample=i_sample;

  oversample=1.0; // AWS200601018

  // fictitious values to complete SourcePopulation
  R_    = 5.;
  z_    = 0.;
 theta_ = 0.;
 d_     = 5.;

  for (i_sample=0;i_sample<n_sample;i_sample++)
    {
     sample_R    [i_sample]= R_;
     sample_z    [i_sample]= z_;
     sample_theta[i_sample]= theta_;
     sample_d    [i_sample]= d_;
    }


  

  return 0;
}

/////////////////////////////////////////////////////////

int SourcePopulation::gen_sample_Fermi_catalogue(char *directory, char *filename,int options) //AWS20081215
{

  // generate a population of sources from an input Fermi catalogue

  int i_sample,i_source;
  double R_,z_,theta_,d_;
 

  Sources sources;
  
  
  cout<<"SourcePopulation::gen_sample_Fermi_catalogue"<<endl;  
  //  cout<<"reading Fermi catalogue from "<<directory<<" "<< filename<<endl;
  string Fermi_cat_file=string(directory)+string(filename);
  cout<<"reading Fermi catalogue from "<<Fermi_cat_file<<endl;
 
  sources.read(Fermi_cat_file);
  cout<<"total number of Fermi sources ="<<sources.size()<<endl;

  if(options==  100)cout<<"Using Fermi source flux >   100 MeV"<<endl;
  if(options== 1000)cout<<"Using Fermi source flux >  1000 MeV"<<endl;
  if(options==10000)cout<<"Using Fermi source flux > 10000 MeV"<<endl;//AWS20120530

  n_sample    =long(sources.size());  
  sample_R    =new double[n_sample];
  sample_z    =new double[n_sample];
  sample_theta=new double[n_sample];
  sample_l    =new double[n_sample];
  sample_b    =new double[n_sample];
  sample_d    =new double[n_sample];
  sample_flux =new double[n_sample];

   sample_flux_spectra        = new double*[n_sample];                                                                          //AWS20100818
   sample_binned_flux_spectra = new double*[n_sample];   ;                                                                      //AWS20100818
   for (int i=0;i<n_sample;i++){ sample_flux_spectra[i]=new double[n_E];  sample_binned_flux_spectra[i]=new double[n_E_bin];   }//AWS20100818

  // values from catalogue
  i_sample=0;
  for (i_source=0;i_source<n_sample;i_source++)
    {
      cout<<"Fermi Catalogue: l="<<sources[i_source].l<<" b="<<sources[i_source].b
          <<" flux100="               <<sources[i_source].flux100
          <<" flux1000="              <<sources[i_source].flux1000  
	  <<" flux10000="             <<sources[i_source].flux10000    //AWS20120530  
          <<" flux_density=prefactor="<<sources[i_source].prefactor    //AWS20140927
          <<" spectral index="        <<sources[i_source].index        //AWS20140927
          <<" pivot energy="          <<sources[i_source].pivot        //AWS20140927
          <<endl;//AWS20110806

        sample_l[i_sample]= sources[i_source].l;
     if(sample_l[i_sample] >180.)
        sample_l[i_sample]-=360.;           // SourcePopulation uses -180<l<+180


	sample_b[i_sample]= sources[i_source].b;

     if(options==  100)                                    //AWS20110808
     sample_flux[i_sample]= sources[i_source].flux100;

     if(options== 1000)                                    //AWS20110808
     sample_flux[i_sample]= sources[i_source].flux1000;    //AWS20110808

     if(options==10000)                                    //AWS20120530
     sample_flux[i_sample]= sources[i_source].flux10000;   //AWS20120530

      i_sample++;
    }



  n_sample=i_sample;

  oversample=1.0; // AWS200601018

  // fictitious values to complete SourcePopulation
  R_    = 5.;
  z_    = 0.;
 theta_ = 0.;
 d_     = 5.;

  

  for (i_sample=0;i_sample<n_sample;i_sample++)
    {
     sample_R    [i_sample]= R_;
     sample_z    [i_sample]= z_;
     sample_theta[i_sample]= theta_;
     sample_d    [i_sample]= d_;
    }
  
  
  
  

  return 0;
}

////////////////////////////////////////////////////////////////////////////////////////////

 int  SourcePopulation::gen_Fermi_catalogue_from_sample(char *directory, char *filename, int options)//AWS20110829

{
  // generate a Fermi catalogue from the population synthesis sample

  Sources         sources; // a catalogue of sources
  Sources::Source source ; // one source, struture defined in Sources



  cout<<">> SourcePopulation::gen_Fermi_catalogue_from_sample"<<endl;
  cout<<" options="<<options<<endl;

  for (int i=1;i<n_sample;i++)
  {
   char source_number_char[20];
   string source_number; sprintf(source_number_char,"%d",i); 
   source.name="popsyn"+string(source_number_char);
   source.l = sample_l[i];
   source.b = sample_b[i];
  

  source.flux100  =sample_flux100  [i]; //AWS20120615
  source.flux1000 =sample_flux1000 [i]; //AWS20120615
  source.flux10000=sample_flux10000[i]; //AWS20120615

  source.fitType=Sources::strToFitType("FIXED");

  if(spectral_model==1)                                        //AWS20120615
  source. spType=Sources::strToFitType("BROKENPOWERLAW");

  if(spectral_model==2)                                        //AWS20120615
  source. spType=Sources::strToFitType("BROKENPOWERLAW");      //AWS20120615

  if(spectral_model==3)                                        //AWS20110901
  source. spType=Sources::strToFitType("POWERLAWEXPCUTOFF");   //AWS20110901


  source.spParameters.resize(5);                               //AWS20120614 was (2)
  source.spParameters[0]=sample_spectrum_g_0[i];               //AWS20110901
  source.spParameters[1]=sample_spectrum_br0[i];               //AWS20110901
  source.spParameters[2]=sample_spectrum_g_1[i];               //AWS20120614
  source.spParameters[3]=sample_spectrum_br1[i];               //AWS20120614
  source.spParameters[4]=sample_spectrum_g_2[i];               //AWS20120614

  source.spParametersUnc.resize(5);                            //AWS20120614 was (2)
  source.spParametersUnc[0]=0.0;
  source.spParametersUnc[1]=0.0;
  source.spParametersUnc[2]=0.0;                               //AWS20120614
  source.spParametersUnc[3]=0.0;                               //AWS20120614
  source.spParametersUnc[4]=0.0;                               //AWS20120614

  source.index=sample_spectrum_g_0[i];                         //AWS20110901
  source.pivot=                         E_ref_low;             //AWS20110901
  source.prefactor=sample_flux_spectrum_E_ref_low[i];          //AWS20110901 since Source outputs this as flux_density OK

  double pi=acos(-1.); double dtr=pi/180.;                                                       //AWS20120612
  slaGaleq(source.l*dtr, source.b*dtr, &source.ra, &source.dec);                                 //AWS20120612

  source.ra/=dtr; source.dec/=dtr;                                                               //AWS20120612

  // cout<<"from slaGaleq: l="<<source.l<<" b="<<source.b<<" ra="<<source.ra<<" dec="<<source.dec<<endl;

  

  sources.add_source(source);

  }

  cout<<" number of sample sources in catalogue for output = "<<sources.size()<<endl;

  //  string Fermi_cat_sample_file=string(directory)+string(filename);

  string Fermi_cat_sample_file=string(filename); //AWS20110830

 int write_Fermi_catalogue =1; //0; //AWS20140930 AWS20150612
 if( write_Fermi_catalogue==1) //AWS20140930
 {
  cout<<"output Fermi catalogue from sample to file  "<<Fermi_cat_sample_file<<endl;
  sources.write(Fermi_cat_sample_file);
 }
  cout<<"<< SourcePopulation::gen_Fermi_catalogue_from_sample"<<endl;
  return 0;
}

///////////////////////////////////////////////////////////////////////////////////

int SourcePopulation::read_sample_MSPs (char *directory, char *filename, int options)//AWS20120521
{
  cout<<">> SourcePopulation::read_sample_MSPs"<<endl;
  int debug=0;

  double flux_factor=0.1;// convert from >100 MeV to >1 GeV, provisional fix

 fitsfile *fptr;
 int status,colnum;
 int hdunum,hdutype,total_hdus;
 long nrows;
 char colname[100];
 
 string MSPs_sample_file;
 MSPs_sample_file=string(directory)+string(filename);

 cout<<"reading from "<<MSPs_sample_file<<endl;
 status=0;
 fits_open_file(&fptr, MSPs_sample_file.c_str