roughness.cc
1 #include <cmath>
2 #include <algorithm>
3 #include <cstdio>
4 #include <fstream>
5 #include <math.h>
6 #include <iostream>
7 #include <string>
8 #include <sstream>
9 
10 #include "vector.hh"
11 #include "position.hh"
12 #include "roughness.hh"
13 #include "Constants.h"
14 #include "TF2.h"
15 #include "TCanvas.h"
16 #include "signal.hh"
17 #include "Settings.h"
18 #include "Primaries.h"
19 #include "anita.hh"
20 #include "balloon.hh"
21 #include "earthmodel.hh"
22 #include "icemodel.hh"
23 #include "spline.h"
24 #include "ray.hh"
25 
26 #ifdef USE_HEALPIX
27 #include "healpix_base.h"
28 #include "pointing.h"
29 #endif
30 
31 Roughness::Roughness(Settings *settings1){
32 
33  rough_dir_str = std::getenv("ICEMC_SRC_DIR");
34 #ifdef USE_HEALPIX
35  order = 6; // order=6 corresponds to nside = 64
36  H = Healpix_Base(order, RING); // RING is an enum, and is the default used to make the maps with the microfacet python simulation
37 #endif
38 
39  roughscale_str = "0p10"; //just some defaults
40  roughnsims_str = "1000000";
41 
42  if (settings1->FIRN){
43  roughmaterial_str="firn";
44  NINDEX=NFIRN;
45  }
46  else{
47  roughmaterial_str="ice";
48  NINDEX=NICE;
49  }
50 };
51 
52 
53 void Roughness::SetRoughScale(double a){
54  std::ostringstream strs;
55  strs.precision(2);
56  strs.setf( std::ios::fixed, std:: ios::floatfield );
57  strs << a;
58  roughscale_str = strs.str();
59  roughscale_str[1] = 'p';
60 
61  if(roughscale_str=="0p00")
62  roughnsims_str = "1";
63  //std::cerr<<"Roughness srms: "<<roughscale_str<<std::endl;
64 };
65 
66 
67 std::string Roughness::incAngle_asString(double T0){
68  char s_f[10];
69  sprintf(s_f, "%.1f", T0);
70  std::string s = std::string() + s_f;
71  s.replace(s.find("."), 1, "p");
72  return s;
73 };
74 
75 #ifdef USE_HEALPIX
76 
77 // map from path to map to pixel to TParl, Tperp for each polarization
78 static std::map<std::string, std::map<int, std::pair<double,double> > >lower_cache_polperp;
79 //
80 static std::map<std::string, std::map<int, std::pair<double,double> > >lower_cache_polparl;
81 
82 
83 void Roughness::InterpolatePowerValue(double &tcoeff_perp_polperp, double &tcoeff_parl_polperp, double &tcoeff_perp_polparl, double &tcoeff_parl_polparl, double T0, double T, double A){
84  // tcoeff_* are the transmission coefficients which will be copied by address for use in icemc.cc's main function
85  // T0 [degrees] is the incident angle of the ray-in-ice with respect to the surface normal
86  // T [degrees] is the exiting angle from the surface towards the balloon, measured with respect to the surface normal
87  // A [degrees] is the azimuthal ground angle towards the balloon, measured with respect to the incidence plane (negative azimuth lies to the right, if facing parallel to the direction of incidence)
88 
89  // in the new implementation, we will use HEALPix look-up tables and average the values for the incidence angle tables that bracket the T0 value (floor and ceil)
90 
91  pointing ptg;
92  int pixel, thispixel_low;
93  // temporary values while reading file
94  double ptheta, pphi;
95  double Tparl_polperp, Tperp_polperp;
96  double Tparl_polparl, Tperp_polparl;
97  double T0_i;
98 
99  std::string header;
100  std::string base_rough_file_str="";
101  ifstream ifs;
102  std::string full_rough_file_lower;
103  std::string full_rough_file_upper;
104 
105  std::vector<double> X, Yperp_polparl, Yparl_polparl, Yperp_polperp, Yparl_polperp; // here X-> transmitted angle, Y-> T**2 entry
106 
107  tk::spline spl_parl_polparl, spl_perp_polparl, spl_parl_polperp, spl_perp_polperp;
108 
109  // Here is the header info from the table generation code:
110  // ## pixel azimuth hpx_theta P_polparl_parl P_polparl_perp P_polperp_parl P_polperp_perp
111 
112  for (double i=0; i<90; i+=.1){ // loop over incidence angle tables to read entries
113  base_rough_file_str = "/data/roughness_tables/"+roughmaterial_str+"/"+roughscale_str+"/combined_inc"+incAngle_asString((double)i)+"_nsims"+roughnsims_str+"_hp"+Form("%i",H.Nside())+"_beckmann.hpx";
114  full_rough_file_lower = rough_dir_str + base_rough_file_str;
115 
116  T0_i = asin(NINDEX * sin(i*PI/180.))*180./PI;
117 
118  if( !isnan(T0_i)){
119  ptg = pointing(T0_i*PI/180., A*PI/180.);
120  thispixel_low = H.ang2pix( ptg );
121  //std::cerr<<T<<" "<<thispixel_low<< std::endl;
122  //
123  if (!lower_cache_polperp.count(full_rough_file_lower)){
124  //std::cerr<<"Not in cache"<<std::endl;
125  ifs.open (full_rough_file_lower, std::ifstream::in);
126  //std::cerr<<ifs.good()<<std::endl;
127  if(ifs.good())
128  {
129  //would be more efficient to use std::emplace, but meh
130  //also, could use a std::array for inner part but that requires a b tmore logic
131  std::map<int, std::pair<double,double> > this_lower_polperp;
132  std::map<int, std::pair<double,double> > this_lower_polparl;
133  std::getline(ifs, header);
134  while (ifs.good()) {
135  ifs >> pixel >> pphi >> ptheta >> Tparl_polparl >> Tperp_polparl >> Tparl_polperp >> Tperp_polperp;
136  //std::cerr<<pphi<<" "<<ptheta<<" "<<Tparl<<" "<<Tperp<<std::endl;
137  if (Tparl_polparl < 0) Tparl_polparl = 0;
138  if (Tperp_polparl < 0) Tperp_polparl = 0;
139  if (Tparl_polperp < 0) Tparl_polperp = 0;
140  if (Tperp_polperp < 0) Tperp_polperp = 0;
141  //std::cerr<<Tparl<<" "<<Tperp<<std::endl;
142  this_lower_polparl[pixel]=std::pair<double,double>(Tparl_polparl,Tperp_polparl);
143  this_lower_polperp[pixel]=std::pair<double,double>(Tparl_polperp,Tperp_polperp);
144  }
145  lower_cache_polparl[full_rough_file_lower] = this_lower_polparl;
146  lower_cache_polperp[full_rough_file_lower] = this_lower_polperp;
147  }
148  ifs.close();
149  }//end !lower_cache.count()
150 
151  //std::cerr<<i<<" "<<lower_cache[full_rough_file_lower][thispixel_low].first<<" "<<lower_cache[full_rough_file_lower][thispixel_low].second<<std::endl;
152  X.push_back(T0_i);
153  Yparl_polparl.push_back( lower_cache_polparl[full_rough_file_lower][thispixel_low].first );
154  Yperp_polparl.push_back( lower_cache_polparl[full_rough_file_lower][thispixel_low].second );
155  Yparl_polperp.push_back( lower_cache_polperp[full_rough_file_lower][thispixel_low].first );
156  Yperp_polperp.push_back( lower_cache_polperp[full_rough_file_lower][thispixel_low].second );
157  }//end !isnan(T)
158  }//end for i loop
159 
160  X.push_back(90.);
161  Yparl_polparl.push_back( 0. );
162  Yperp_polparl.push_back( 0. );
163  Yparl_polperp.push_back( 0. );
164  Yperp_polperp.push_back( 0. );
165 
166  //std::cerr<<X.size()<<" "<<Yparl.size()<<" "<<Yperp.size()<<std::endl;
167 
168  if(X.size()>0){
169  spl_parl_polparl.set_points(X,Yparl_polparl);
170  spl_perp_polparl.set_points(X,Yperp_polparl);
171  spl_parl_polperp.set_points(X,Yparl_polperp);
172  spl_perp_polperp.set_points(X,Yperp_polperp);
173 
174  tcoeff_perp_polperp = spl_perp_polperp(T);
175  tcoeff_parl_polperp = spl_parl_polperp(T);
176  tcoeff_perp_polparl = spl_perp_polparl(T);
177  tcoeff_parl_polparl = spl_parl_polparl(T);
178  }
179 
180 };
181 #endif
182 
183 
184 
Reads in and stores input settings for the run.
Definition: Settings.h:37