ConversionUtil.cc
Go to the documentation of this file.
1 
7 #include <utl/config.h>
8 #include "ConversionUtil.h"
9 #include "ErrorPropagation.h"
10 
11 #include <utl/AugerException.h>
12 #include <utl/TimeStamp.h>
13 #include <utl/UTCDateTime.h>
14 #include <utl/ModifiedJulianDate.h>
15 #include <utl/NumericalErrorPropagation.h>
16 #include <utl/CorrelationMatrix.h>
17 
18 #include <evt/Event.h>
19 
20 #include <TVector3.h>
21 
22 #include <RecEvent.h>
23 #include <RecShower.h>
24 
25 #include <iostream>
26 #include <string>
27 #include <cmath>
28 #include <vector>
29 
30 using namespace std;
31 using namespace otoa;
32 using namespace utl;
33 
34 
35 namespace otoa {
36 
37  unsigned int
39  {
40  const UTCDateTime t = timest;
41  return (t.GetYear() % 100)*10000 + t.GetMonth()*100 + t.GetDay();
42  }
43 
44 
45  unsigned int
47  {
48  const UTCDateTime t = timest;
49  return t.GetHour()*10000 + t.GetMinute()*100 + t.GetSecond();
50  }
51 
52 
53  double
55  {
56  const double kFirstFullMoon = 1795.157467; // moon cycle at 2004/01/07
57  const double kMoonCycle = 29.53; // duration of one moon cycle in days
58  return ModifiedJulianDate(timest) / kMoonCycle - kFirstFullMoon;
59  }
60 
61 
62  double
64  {
65  // a la CDAS
66  const UTCDateTime p = ts;
67  const TimeStamp time0 = UTCDateTime(p.GetYear(), p.GetMonth(), p.GetDay()).GetTimeStamp();
68  const double julianDay0 = ModifiedJulianDate(time0) + 2400000.5;
69  const double tt = (julianDay0 - 2415020.0) / 36525.0;
70  const double dgmst0 = (0.000001075*tt + 100.0021359)*tt + 0.276919398;
71  const double gmst0 = (dgmst0 - int(dgmst0)) * 24.0;
72  return gmst0 + (p.GetHour() + p.GetMinute()/60. + p.GetSecond()/3600.) * 366.25 / 365.25;
73  }
74 
75 
76  double
77  CalculateNutationCorrection(const double julianDay)
78  {
79  // a la CDAS
80  const double tt = (julianDay - 2415020.0) / 36525.; // time (Julius century) from 1900
81 
82  const double llt = (0.000303*tt + 36000.7689)*tt + 279.6967;
83  const double ldt = (-0.001133*tt + 481267.8831)*tt + 270.4342;
84  const double mmt = (-0.000150*tt + 35999.0498)*tt + 358.4758;
85  const double mdt = (0.009192*tt + 477198.8491)*tt + 296.1046;
86  const double omegat = (0.002078*tt - 1934.1420)*tt + 259.1833;
87 
88  const double ll = fmod(llt, 360.); // mean ecliptic longtitude of the sun (deg)
89  const double ld = fmod(ldt, 360.); // mean ecliptic longtitude of the moon(deg)
90  const double mm = fmod(mmt, 360.); // mean anomaly of the sun (deg)
91  const double md = fmod(mdt, 360.); // mean anomaly of the moon (deg)
92 
93  double omega = fmod(omegat, 360.); // ecliptic lg. of ascending node of moon
94  if (omega < 0)
95  omega += 360;
96  /* not used in the correction
97  const double d_psi_t = -(17.2327 + 0.01737 * tt) * sin(deg*(omega))
98  - (1.2729 + 0.00013 * tt) * sin(deg*(2.0 * ll))
99  + 0.2088 * sin(deg*(2.0 * omega))
100  - 0.2037 * sin(deg*(2.0 * ld))
101  + (0.1261 - 0.00031 * tt) * sin(deg*(mm))
102  + 0.0675 * sin(deg*(md))
103  - (0.0497 - 0.00012 * tt) * sin(deg*(2.0 * ll + mm))
104  - 0.0342 * sin(deg*(2.0 * ld - omega))
105  - 0.0261 * sin(deg*(2.0 * ld + mm))
106  + 0.0214 * sin(deg*(2.0 * ll - mm))
107  - 0.0149 * sin(deg*(2.0 * (ll - ld) + md))
108  + 0.0124 * sin(deg*(2.0 * ll - omega))
109  + 0.0114 * sin(deg*(2.0 * ld - md));
110  */
111  const double depsit =
112  (9.2100 + 0.00091*tt) * cos(omega*deg)
113  + (0.5522 - 0.00029*tt) * cos(2*ll*deg)
114  - 0.0904 * cos(2*omega*deg)
115  + 0.0884 * cos(2*ld*deg)
116  + 0.0216 * cos((2*ll + mm)*deg)
117  + 0.0183 * cos((2*ld - omega)*deg)
118  + 0.0113 * cos((2*ld + md)*deg)
119  - 0.0093 * cos((2*ll - mm)*deg)
120  - 0.0006 * cos((2*ll - omega)*deg);
121 
122  //vector<double> result;
123  //result.push_back(d_psi_t / 3600.0); //sol.d_psi
124  //result.push_back( d_epsi_t / 3600.0); //sol.d_epsi
125 
126  /* central equation of the sun, not used in the correction
127  const double cc = (1.919460 - 0.004789 * tt - 0.000014 * tt * tt) * sin(deg*(mm))
128  + (0.020094 - 0.000100 * tt) * sin(deg*(2.0 * mm))
129  + 0.000293 * sin(deg*(3.0 * mm));*/
130 
131  //result.push_back(ll + cc); //sol.lambda_s
132 
133  const double epsi =
134  ((0.000000503*tt - 0.00000164)*tt - 0.0130125)*tt + 23.452294 +
135  0.00256*cos(omega*deg); //sol.epsi
136  //result.push_back(epsi*deg); //sol.epsi
137  double nuthour = depsit / 3600 * cos(epsi*deg);
138  return nuthour;
139  }
140 
141 
142  void
143  LinearFit(const vector<double>& x,
144  const vector<double>& y,
145  const vector<double>& ey,
146  double& a0,
147  double& a1,
148  double& chi2)
149  {
150  double s1 = 0;
151  double sx = 0;
152  double sxx = 0;
153  double sy = 0;
154  double sxy = 0;
155  double syy = 0;
156 
157  for (unsigned int i = 0; i < x.size(); ++i) {
158  if (ey[i] > 0) {
159  const double wi = 1 / (ey[i]*ey[i]);
160  const double wX = wi * x[i];
161  const double wY = wi * y[i];
162  s1 += wi;
163  sx += wX;
164  sxx += wX * x[i];
165  sy += wY;
166  sxy += wX * y[i];
167  syy += wY * y[i];
168  }
169  }
170 
171  const double d = s1*sxx - sx*sx;
172 
173  a0 = (sxx*sy - sx * sxy) / d;
174  a1 = (-sx*sy + s1 * sxy) / d;
175 
176  chi2 = syy - a0*sy - a1*sxy;
177  }
178 
179 
180  void
181  FillCelestialCoordinates(RecShower& recShower)
182  {
183  const TVector3& coreUTM = recShower.GetCoreUTMCS();
184  TimeStamp coreTime(recShower.GetCoreTimeSecond(), recShower.GetCoreTimeNanoSecond());
185 
186  const double julianDay = ModifiedJulianDate(coreTime) + 2400000.5;
187  const double gmst = TimeStamp2GMST(coreTime);
188 
189  // error on julian day and gmst negligible, relative error << 1e-5
190  // 1e-5 if our clocks are wrong by second (time conversion GPS <-> UTC can cause this but very unlikely)
191  // 1e-12 if we are wrong by some 100 nanoseconds (order of time offset between SD/FD)
192  vector<Parameter> locParameters;
193  locParameters.push_back(Parameter(julianDay, 0.));
194  locParameters.push_back(Parameter(gmst, 0.));
195  locParameters.push_back(Parameter(coreUTM.X(), recShower.GetCoreEastingError()));
196  locParameters.push_back(Parameter(coreUTM.Y(), recShower.GetCoreNorthingError()));
197  locParameters.push_back(Parameter(coreUTM.Z(), 0.)); // zero by definition
198  locParameters.push_back(Parameter(recShower.GetZenith(), recShower.GetZenithError()));
199  locParameters.push_back(Parameter(recShower.GetAzimuth(), recShower.GetAzimuthError()));
200 
201  CorrelationMatrix locCorrelations(7);
202  // we only have these, others are set to zero automatically
203  locCorrelations.Set(2, 3, recShower.GetCoreNorthingEastingCorrelation());
204  locCorrelations.Set(5, 6, recShower.GetZenithAzimuthCorrelation());
205 
207  NumericalErrorPropagation pEqu(calcEqu, locParameters, locCorrelations);
208 
209  const double ra = pEqu.GetFunctionResult()[0];
210  const double dec = pEqu.GetFunctionResult()[1];
211 
212  // safeguards for NaNs
213  const double raErr = std::isnan(pEqu.GetPropagatedErrors()[0]) ?
214  ra : pEqu.GetPropagatedErrors()[0];
215  const double decErr = std::isnan(pEqu.GetPropagatedErrors()[1]) ?
216  dec : pEqu.GetPropagatedErrors()[1];
217  const double raDecCorr = std::isnan(pEqu.GetPropagatedCorrelations().Get(0, 1)) ?
218  0 : pEqu.GetPropagatedCorrelations().Get(0, 1);
219 
220  recShower.SetRightAscension(ra);
221  recShower.SetRightAscensionError(raErr);
222  recShower.SetDeclination(dec);
223  recShower.SetDeclinationError(decErr);
224  recShower.SetRDCorrelation(raDecCorr);
225 
226  // -------- galactic coordinates
228  NumericalErrorPropagation pGal(calcGal, locParameters, locCorrelations);
229 
230  const double galLong = pGal.GetFunctionResult()[0];
231  const double galLat = pGal.GetFunctionResult()[1];
232 
233  // saveguards for NaNs
234  const double galLongErr = std::isnan(pGal.GetPropagatedErrors()[0]) ?
235  galLong : pGal.GetPropagatedErrors()[0];
236  const double galLatErr = std::isnan(pGal.GetPropagatedErrors()[1]) ?
237  galLat : pGal.GetPropagatedErrors()[1];
238  const double galLatLongCorr = std::isnan(pGal.GetPropagatedCorrelations().Get(0, 1)) ?
239  0 : pGal.GetPropagatedCorrelations().Get(0, 1);
240 
241  recShower.SetGalacticLatitude(galLat);
242  recShower.SetGalacticLongitude(galLong);
243  recShower.SetGalLatError(galLatErr);
244  recShower.SetGalLongError(galLongErr);
245  recShower.SetLongLatCorrelation(galLatLongCorr);
246  }
247 
248 }
double ModifiedJulianDate(const time_t unixSecond)
constexpr double mm
Definition: AugerUnits.h:113
int GetHour() const
Definition: UTCDateTime.h:54
const std::vector< double > & GetPropagatedErrors() const
unsigned int TimeStamp2HHMMSS(const utl::TimeStamp &timest)
Convert a TimeStamp into an integer representing the time as HHMMSS.
int GetYear() const
Definition: UTCDate.h:44
int GetMinute() const
Definition: UTCDateTime.h:56
A TimeStamp holds GPS second and nanosecond for some event.
Definition: TimeStamp.h:110
double CalculateNutationCorrection(const double julianDay)
Used by the calculation of the equatorial coordinates. TODO: Move such calculations to utl or use lib...
constexpr double deg
Definition: AugerUnits.h:140
void Set(const int iPar, const int jPar, const double corr)
int GetSecond() const
Definition: UTCDateTime.h:58
const CorrelationMatrix & GetPropagatedCorrelations() const
unsigned int TimeStamp2YYMMDD(const utl::TimeStamp &timest)
Convert a TimeStamp into an integer representing the date as YYMMDD.
double Get(const int iPar, const int jPar) const
int GetMonth() const
Definition: UTCDate.h:46
int GetDay() const
Definition: UTCDate.h:48
double TimeStamp2GMST(const utl::TimeStamp &ts)
Convert a TimeStamp to GMST.
void FillCelestialCoordinates(RecShower &recShower)
void LinearFit(const vector< double > &x, const vector< double > &y, const vector< double > &ey, double &a0, double &a1, double &chi2)
Do a linear fit and return coefficients and chi2.
const std::vector< double > & GetFunctionResult() const
double TimeStamp2MoonCycle(const utl::TimeStamp &timest)
Convert a TimeStamp into a fractional mooncycle since 2004/01/07.

, generated on Tue Sep 26 2023.