source: branches/Corsika7500Compatibility/mastro/MAstro.cc@ 19536

Last change on this file since 19536 was 14859, checked in by tbretz, 12 years ago
Fixed a typo
File size: 32.9 KB
Line 
1/* ======================================================================== *\
2!
3! *
4! * This file is part of MARS, the MAGIC Analysis and Reconstruction
5! * Software. It is distributed to you in the hope that it can be a useful
6! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
7! * It is distributed WITHOUT ANY WARRANTY.
8! *
9! * Permission to use, copy, modify and distribute this software and its
10! * documentation for any purpose is hereby granted without fee,
11! * provided that the above copyright notice appear in all copies and
12! * that both that copyright notice and this permission notice appear
13! * in supporting documentation. It is provided "as is" without express
14! * or implied warranty.
15! *
16!
17!
18! Author(s): Thomas Bretz, 11/2003 <mailto:tbretz@astro.uni-wuerzburg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2008
21!
22!
23\* ======================================================================== */
24
25/////////////////////////////////////////////////////////////////////////////
26//
27// MAstro
28// ------
29//
30////////////////////////////////////////////////////////////////////////////
31#include "MAstro.h"
32
33#include <math.h> // fmod on darwin
34
35#include <iostream>
36
37#include <TArrayD.h> // TArrayD
38#include <TVector3.h> // TVector3
39
40#include "MTime.h" // MTime::GetGmst
41#include "MString.h"
42
43#include "MAstroCatalog.h" // FIXME: replace by MVector3!
44
45using namespace std;
46
47ClassImp(MAstro);
48
49const Double_t MAstro::kSynMonth = 29.53058868; // synodic month (new Moon to new Moon)
50const Double_t MAstro::kEpoch0 = 44240.37917; // First full moon after 1980/1/1
51
52Double_t MAstro::RadToHor()
53{
54 return 24/TMath::TwoPi();
55}
56
57Double_t MAstro::HorToRad()
58{
59 return TMath::TwoPi()/24;
60}
61
62Double_t MAstro::Trunc(Double_t val)
63{
64 // dint(A) - truncate to nearest whole number towards zero (double)
65 return val<0 ? TMath::Ceil(val) : TMath::Floor(val);
66}
67
68Double_t MAstro::Round(Double_t val)
69{
70 // dnint(A) - round to nearest whole number (double)
71 return val<0 ? TMath::Ceil(val-0.5) : TMath::Floor(val+0.5);
72}
73
74Double_t MAstro::Hms2Sec(Int_t deg, UInt_t min, Double_t sec, Char_t sgn)
75{
76 const Double_t rc = TMath::Sign((60.0 * (60.0 * (Double_t)TMath::Abs(deg) + (Double_t)min) + sec), (Double_t)deg);
77 return sgn=='-' ? -rc : rc;
78}
79
80Double_t MAstro::Dms2Rad(Int_t deg, UInt_t min, Double_t sec, Char_t sgn)
81{
82 // pi/(180*3600): arcseconds to radians
83 //#define DAS2R 4.8481368110953599358991410235794797595635330237270e-6
84 return Hms2Sec(deg, min, sec, sgn)*TMath::Pi()/(180*3600)/**DAS2R*/;
85}
86
87Double_t MAstro::Hms2Rad(Int_t hor, UInt_t min, Double_t sec, Char_t sgn)
88{
89 // pi/(12*3600): seconds of time to radians
90//#define DS2R 7.2722052166430399038487115353692196393452995355905e-5
91 return Hms2Sec(hor, min, sec, sgn)*TMath::Pi()/(12*3600)/**DS2R*/;
92}
93
94Double_t MAstro::Dms2Deg(Int_t deg, UInt_t min, Double_t sec, Char_t sgn)
95{
96 return Hms2Sec(deg, min, sec, sgn)/3600.;
97}
98
99Double_t MAstro::Hms2Deg(Int_t hor, UInt_t min, Double_t sec, Char_t sgn)
100{
101 return Hms2Sec(hor, min, sec, sgn)/240.;
102}
103
104Double_t MAstro::Dms2Hor(Int_t deg, UInt_t min, Double_t sec, Char_t sgn)
105{
106 return Hms2Sec(deg, min, sec, sgn)/54000.;
107}
108
109Double_t MAstro::Hms2Hor(Int_t hor, UInt_t min, Double_t sec, Char_t sgn)
110{
111 return Hms2Sec(hor, min, sec, sgn)/3600.;
112}
113
114void MAstro::Day2Hms(Double_t day, Char_t &sgn, UShort_t &hor, UShort_t &min, UShort_t &sec)
115{
116 /* Handle sign */
117 sgn = day<0?'-':'+';
118
119 /* Round interval and express in smallest units required */
120 Double_t a = Round(86400. * TMath::Abs(day)); // Days to seconds
121
122 /* Separate into fields */
123 const Double_t ah = Trunc(a/3600.);
124 a -= ah * 3600.;
125 const Double_t am = Trunc(a/60.);
126 a -= am * 60.;
127 const Double_t as = Trunc(a);
128
129 /* Return results */
130 hor = (UShort_t)ah;
131 min = (UShort_t)am;
132 sec = (UShort_t)as;
133}
134
135void MAstro::Rad2Hms(Double_t rad, Char_t &sgn, UShort_t &deg, UShort_t &min, UShort_t &sec)
136{
137 Day2Hms(rad/(TMath::Pi()*2), sgn, deg, min, sec);
138}
139
140void MAstro::Rad2Dms(Double_t rad, Char_t &sgn, UShort_t &deg, UShort_t &min, UShort_t &sec)
141{
142 Rad2Hms(rad*15, sgn, deg, min, sec);
143}
144
145void MAstro::Deg2Dms(Double_t d, Char_t &sgn, UShort_t &deg, UShort_t &min, UShort_t &sec)
146{
147 Day2Hms(d/24, sgn, deg, min, sec);
148}
149
150void MAstro::Deg2Hms(Double_t d, Char_t &sgn, UShort_t &deg, UShort_t &min, UShort_t &sec)
151{
152 Day2Hms(d/360, sgn, deg, min, sec);
153}
154
155void MAstro::Hor2Dms(Double_t h, Char_t &sgn, UShort_t &deg, UShort_t &min, UShort_t &sec)
156{
157 Day2Hms(h*15/24, sgn, deg, min, sec);
158}
159
160void MAstro::Hor2Hms(Double_t h, Char_t &sgn, UShort_t &deg, UShort_t &min, UShort_t &sec)
161{
162 Day2Hms(h/24, sgn, deg, min, sec);
163}
164
165void MAstro::Day2Hm(Double_t day, Char_t &sgn, UShort_t &hor, Double_t &min)
166{
167 /* Handle sign */
168 sgn = day<0?'-':'+';
169
170 /* Round interval and express in smallest units required */
171 Double_t a = Round(86400. * TMath::Abs(day)); // Days to seconds
172
173 /* Separate into fields */
174 const Double_t ah = Trunc(a/3600.);
175 a -= ah * 3600.;
176
177 /* Return results */
178 hor = (UShort_t)ah;
179 min = a/60.;
180}
181
182void MAstro::Rad2Hm(Double_t rad, Char_t &sgn, UShort_t &deg, Double_t &min)
183{
184 Day2Hm(rad/(TMath::Pi()*2), sgn, deg, min);
185}
186
187void MAstro::Rad2Dm(Double_t rad, Char_t &sgn, UShort_t &deg, Double_t &min)
188{
189 Rad2Hm(rad*15, sgn, deg, min);
190}
191
192void MAstro::Deg2Dm(Double_t d, Char_t &sgn, UShort_t &deg, Double_t &min)
193{
194 Day2Hm(d/24, sgn, deg, min);
195}
196
197void MAstro::Deg2Hm(Double_t d, Char_t &sgn, UShort_t &deg, Double_t &min)
198{
199 Rad2Hm(d/360, sgn, deg, min);
200}
201
202void MAstro::Hor2Dm(Double_t h, Char_t &sgn, UShort_t &deg, Double_t &min)
203{
204 Day2Hm(h*15/24, sgn, deg, min);
205}
206
207void MAstro::Hor2Hm(Double_t h, Char_t &sgn, UShort_t &deg, Double_t &min)
208{
209 Day2Hm(h/24, sgn, deg, min);
210}
211
212TString MAstro::GetStringDeg(Double_t deg, const char *fmt)
213{
214 Char_t sgn;
215 UShort_t d, m, s;
216 Deg2Dms(deg, sgn, d, m, s);
217
218 return MString::Format(fmt, sgn, d, m ,s);
219}
220
221TString MAstro::GetStringHor(Double_t deg, const char *fmt)
222{
223 Char_t sgn;
224 UShort_t h, m, s;
225 Hor2Hms(deg, sgn, h, m, s);
226
227 return MString::Format(fmt, sgn, h, m ,s);
228}
229
230// --------------------------------------------------------------------------
231//
232// Interpretes a string ' - 12 30 00.0' or '+ 12 30 00.0'
233// as floating point value -12.5 or 12.5. If interpretation is
234// successfull kTRUE is returned, otherwise kFALSE. ret is not
235// touched if interpretation was not successfull. The successfull
236// interpreted part is removed from the TString.
237//
238Bool_t MAstro::String2Angle(TString &str, Double_t &ret)
239{
240 Char_t sgn;
241 Int_t d, len;
242 UInt_t m;
243 Float_t s;
244
245 // Skip whitespaces before %c and after %f
246 int n=sscanf(str.Data(), " %c %d %d %f %n", &sgn, &d, &m, &s, &len);
247
248 if (n!=4 || (sgn!='+' && sgn!='-'))
249 return kFALSE;
250
251 str.Remove(0, len);
252
253 ret = Dms2Deg(d, m, s, sgn);
254 return kTRUE;
255}
256
257// --------------------------------------------------------------------------
258//
259// Interpretes a string '-12:30:00.0', '12:30:00.0' or '+12:30:00.0'
260// as floating point value -12.5, 12.5 or 12.5. If interpretation is
261// successfull kTRUE is returned, otherwise kFALSE. ret is not
262// touched if interpretation was not successfull.
263//
264Bool_t MAstro::Coordinate2Angle(const TString &str, Double_t &ret)
265{
266 Char_t sgn = str[0]=='-' ? '-' : '+';
267 Int_t d;
268 UInt_t m;
269 Float_t s;
270
271 const int n=sscanf(str[0]=='+'||str[0]=='-' ? str.Data()+1 : str.Data(), "%d:%d:%f", &d, &m, &s);
272
273 if (n!=3)
274 return kFALSE;
275
276 ret = Dms2Deg(d, m, s, sgn);
277 return kTRUE;
278}
279
280// --------------------------------------------------------------------------
281//
282// Returns val=-12.5 as string '-12:30:00'
283//
284TString MAstro::Angle2Coordinate(Double_t val)
285{
286 Char_t sgn;
287 UShort_t d,m,s;
288
289 Deg2Dms(val, sgn, d, m, s);
290
291 return MString::Format("%c%02d:%02d:%02d", sgn, d, m, s);
292}
293
294// --------------------------------------------------------------------------
295//
296// Return year y, month m and day d corresponding to Mjd.
297//
298void MAstro::Mjd2Ymd(UInt_t mjd, UShort_t &y, Byte_t &m, Byte_t &d)
299{
300 // Express day in Gregorian calendar
301 const ULong_t jd = mjd + 2400001;
302 const ULong_t n4 = 4*(jd+((6*((4*jd-17918)/146097))/4+1)/2-37);
303 const ULong_t nd10 = 10*(((n4-237)%1461)/4)+5;
304
305 y = n4/1461L-4712;
306 m = ((nd10/306+2)%12)+1;
307 d = (nd10%306)/10+1;
308}
309
310// --------------------------------------------------------------------------
311//
312// Return Mjd corresponding to year y, month m and day d.
313//
314Int_t MAstro::Ymd2Mjd(UShort_t y, Byte_t m, Byte_t d)
315{
316 // Month lengths in days
317 static int months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
318
319 // Validate month
320 if (m<1 || m>12)
321 return -1;
322
323 // Allow for leap year
324 months[1] = (y%4==0 && (y%100!=0 || y%400==0)) ? 29 : 28;
325
326 // Validate day
327 if (d<1 || d>months[m-1])
328 return -1;
329
330 // Precalculate some values
331 const Byte_t lm = 12-m;
332 const ULong_t lm10 = 4712 + y - lm/10;
333
334 // Perform the conversion
335 return 1461L*lm10/4 + (306*((m+9)%12)+5)/10 - (3*((lm10+188)/100))/4 + d - 2399904;
336}
337
338// --------------------------------------------------------------------------
339//
340// Convert a mjd to a number yymmdd. The century is just cut away, e.g.
341// 54393 --> 71020 (2007/10/20)
342// 50741 --> 971020 (1997/10/20)
343// 17868 --> 71020 (1907/10/20)
344//
345UInt_t MAstro::Mjd2Yymmdd(UInt_t mjd)
346{
347 UShort_t y;
348 Byte_t m, d;
349 Mjd2Ymd(mjd, y, m, d);
350
351 return d + m*100 + (y%100)*10000;
352}
353
354// --------------------------------------------------------------------------
355//
356// Convert a yymmdd number to mjd. The century is defined as 2000 for
357// yy<70, 1900 otherwise.
358// 71020 --> 54393 (2007/10/20)
359// 971020 --> 50741 (1997/10/20)
360//
361UInt_t MAstro::Yymmdd2Mjd(UInt_t yymmdd)
362{
363 const Byte_t dd = yymmdd%100;
364 const Byte_t mm = (yymmdd/100)%100;
365 const UShort_t yy = (yymmdd/10000)%100;
366
367 return Ymd2Mjd(yy + (yy<70 ? 2000 : 1900), mm, dd);
368}
369
370// --------------------------------------------------------------------------
371//
372// theta0, phi0 [rad]: polar angle/zenith distance, azimuth of 1st object
373// theta1, phi1 [rad]: polar angle/zenith distance, azimuth of 2nd object
374// AngularDistance [rad]: Angular distance between two objects
375//
376Double_t MAstro::AngularDistance(Double_t theta0, Double_t phi0, Double_t theta1, Double_t phi1)
377{
378 // v1 = theta0/phi0 --> theta0 / 0
379 // v2 = theta1/phi1 --> theta1 / phi1-phi0
380 // acos(alpha) = v1*v2/|v1||v2| |v1|*|v2|=1
381 const Double_t sin2 = TMath::Sin(theta0)*TMath::Sin(theta1);
382 const Double_t cos2 = TMath::Cos(theta0)*TMath::Cos(theta1);
383
384 return TMath::ACos(sin2*TMath::Cos(phi1-phi0) + cos2);
385 /*
386 TVector3 v0;
387 v0.SetMagThetaPhi(1, theta0, phi0);
388
389 TVector3 v1;
390 v1.SetMagThetaPhi(1, theta0, phi0);
391
392 return v0.Angle(v1);
393 */
394}
395
396// --------------------------------------------------------------------------
397//
398// Calls MTime::GetGmst() Better use MTime::GetGmst() directly
399//
400Double_t MAstro::UT2GMST(Double_t ut1)
401{
402 return MTime(ut1).GetGmst();
403}
404
405// --------------------------------------------------------------------------
406//
407// RotationAngle
408//
409// calculates the angle for the rotation of the sky coordinate system
410// with respect to the local coordinate system. This is identical
411// to the rotation angle of the sky image in the camera.
412//
413// sinl [rad]: sine of observers latitude
414// cosl [rad]: cosine of observers latitude
415// theta [rad]: polar angle/zenith distance
416// phi [rad]: rotation angle/azimuth
417//
418// Return sin/cos component of angle
419//
420// The convention is such, that the rotation angle is -pi/pi if
421// right ascension and local rotation angle are counted in the
422// same direction, 0 if counted in the opposite direction.
423//
424// (In other words: The rotation angle is 0 when the source culminates)
425//
426// Using vectors it can be done like:
427// TVector3 v, p;
428// v.SetMagThetaPhi(1, theta, phi);
429// p.SetMagThetaPhi(1, TMath::Pi()/2-latitude, 0);
430// v = v.Cross(l));
431// v.RotateZ(-phi);
432// v.Rotate(-theta)
433// rho = TMath::ATan2(v(2), v(1));
434//
435// For more information see TDAS 00-11, eqs. (18) and (20)
436//
437void MAstro::RotationAngle(Double_t sinl, Double_t cosl, Double_t theta, Double_t phi, Double_t &sin, Double_t &cos)
438{
439 const Double_t sint = TMath::Sin(theta);
440 const Double_t cost = TMath::Cos(theta);
441
442 const Double_t snlt = sinl*sint;
443 const Double_t cslt = cosl*cost;
444
445 const Double_t sinp = TMath::Sin(phi);
446 const Double_t cosp = TMath::Cos(phi);
447
448 const Double_t v1 = sint*sinp;
449 const Double_t v2 = cslt - snlt*cosp;
450
451 const Double_t denom = TMath::Sqrt(v1*v1 + v2*v2);
452
453 sin = cosl*sinp / denom; // y-component
454 cos = (snlt-cslt*cosp) / denom; // x-component
455}
456
457// --------------------------------------------------------------------------
458//
459// RotationAngle
460//
461// calculates the angle for the rotation of the sky coordinate system
462// with respect to the local coordinate system. This is identical
463// to the rotation angle of the sky image in the camera.
464//
465// sinl [rad]: sine of observers latitude
466// cosl [rad]: cosine of observers latitude
467// theta [rad]: polar angle/zenith distance
468// phi [rad]: rotation angle/azimuth
469//
470// Return angle [rad] in the range -pi, pi
471//
472// The convention is such, that the rotation angle is -pi/pi if
473// right ascension and local rotation angle are counted in the
474// same direction, 0 if counted in the opposite direction.
475//
476// (In other words: The rotation angle is 0 when the source culminates)
477//
478// Using vectors it can be done like:
479// TVector3 v, p;
480// v.SetMagThetaPhi(1, theta, phi);
481// p.SetMagThetaPhi(1, TMath::Pi()/2-latitude, 0);
482// v = v.Cross(l));
483// v.RotateZ(-phi);
484// v.Rotate(-theta)
485// rho = TMath::ATan2(v(2), v(1));
486//
487// For more information see TDAS 00-11, eqs. (18) and (20)
488//
489Double_t MAstro::RotationAngle(Double_t sinl, Double_t cosl, Double_t theta, Double_t phi)
490{
491 const Double_t snlt = sinl*TMath::Sin(theta);
492 const Double_t cslt = cosl*TMath::Cos(theta);
493
494 const Double_t sinp = TMath::Sin(phi);
495 const Double_t cosp = TMath::Cos(phi);
496
497 return TMath::ATan2(cosl*sinp, snlt-cslt*cosp);
498}
499
500// --------------------------------------------------------------------------
501//
502// Estimates the time at which a source culminates.
503//
504// ra: right ascension [rad]
505// elong: observers longitude [rad]
506// mjd: modified julian date (utc)
507//
508// return time in [-12;12]
509//
510Double_t MAstro::EstimateCulminationTime(Double_t mjd, Double_t elong, Double_t ra)
511{
512 // startime at 1.1.2000 for greenwich 0h
513 const Double_t gmt0 = 6.664520;
514
515 // difference of startime for greenwich for two calendar days [h]
516 const Double_t d0 = 0.06570982224;
517
518 // mjd of greenwich 1.1.2000 0h
519 const Double_t mjd0 = 51544;
520
521 // mjd today
522 const Double_t mjd1 = TMath::Floor(mjd);
523
524 // scale between star-time and sun-time
525 const Double_t scale = 1;//1.00273790926;
526
527 const Double_t UT = (ra-elong)*RadToHor() - (gmt0 + d0 * (mjd1-mjd0))/scale;
528
529 return fmod(2412 + UT, 24) - 12;
530}
531
532// --------------------------------------------------------------------------
533//
534// Kepler - solve the equation of Kepler
535//
536Double_t MAstro::Kepler(Double_t m, Double_t ecc)
537{
538 m *= TMath::DegToRad();
539
540 Double_t delta = 0;
541 Double_t e = m;
542 do {
543 delta = e - ecc * sin(e) - m;
544 e -= delta / (1 - ecc * cos(e));
545 } while (fabs(delta) > 1e-6);
546
547 return e;
548}
549
550// --------------------------------------------------------------------------
551//
552// GetMoonPhase - calculate phase of moon as a fraction:
553// Returns -1 if calculation failed
554//
555Double_t MAstro::GetMoonPhase(Double_t mjd)
556{
557 /****** Calculation of the Sun's position. ******/
558
559 // date within epoch
560 const Double_t epoch = 44238; // 1980 January 0.0
561 const Double_t day = mjd - epoch;
562 if (day<0)
563 {
564 cout << "MAstro::GetMoonPhase - Day before Jan 1980" << endl;
565 return -1;
566 }
567
568 // mean anomaly of the Sun
569 const Double_t n = fmod(day*360/365.2422, 360);
570
571 const Double_t elonge = 278.833540; // ecliptic longitude of the Sun at epoch 1980.0
572 const Double_t elongp = 282.596403; // ecliptic longitude of the Sun at perigee
573
574 // convert from perigee co-ordinates to epoch 1980.0
575 const Double_t m = fmod(n + elonge - elongp + 360, 360);
576
577 // solve equation of Kepler
578 const Double_t eccent = 0.016718; // eccentricity of Earth's orbit
579 const Double_t k = Kepler(m, eccent);
580 const Double_t ec0 = sqrt((1 + eccent) / (1 - eccent)) * tan(k / 2);
581 // true anomaly
582 const Double_t ec = 2 * atan(ec0) * TMath::RadToDeg();
583
584 // Sun's geocentric ecliptic longitude
585 const Double_t lambdasun = fmod(ec + elongp + 720, 360);
586
587
588 /****** Calculation of the Moon's position. ******/
589
590 // Moon's mean longitude.
591 const Double_t mmlong = 64.975464; // moon's mean lonigitude at the epoch
592 const Double_t ml = fmod(13.1763966*day + mmlong + 360, 360);
593 // Moon's mean anomaly.
594 const Double_t mmlongp = 349.383063; // mean longitude of the perigee at the epoch
595 const Double_t mm = fmod(ml - 0.1114041*day - mmlongp + 720, 360);
596 // Evection.
597 const Double_t ev = 1.2739 * sin((2 * (ml - lambdasun) - mm)*TMath::DegToRad());
598 // Annual equation.
599 const Double_t sinm = TMath::Sin(m*TMath::DegToRad());
600 const Double_t ae = 0.1858 * sinm;
601 // Correction term.
602 const Double_t a3 = 0.37 * sinm;
603 // Corrected anomaly.
604 const Double_t mmp = (mm + ev - ae - a3)*TMath::DegToRad();
605 // Correction for the equation of the centre.
606 const Double_t mec = 6.2886 * sin(mmp);
607 // Another correction term.
608 const Double_t a4 = 0.214 * sin(2 * mmp);
609 // Corrected longitude.
610 const Double_t lp = ml + ev + mec - ae + a4;
611 // Variation.
612 const Double_t v = 0.6583 * sin(2 * (lp - lambdasun)*TMath::DegToRad());
613 // True longitude.
614 const Double_t lpp = lp + v;
615 // Age of the Moon in degrees.
616 const Double_t age = (lpp - lambdasun)*TMath::DegToRad();
617
618 // Calculation of the phase of the Moon.
619 return (1 - TMath::Cos(age)) / 2;
620}
621
622// --------------------------------------------------------------------------
623//
624// Calculate the Period to which the time belongs to. The Period is defined
625// as the number of synodic months ellapsed since the first full moon
626// after Jan 1st 1980 (which was @ MJD=44240.37917)
627//
628Double_t MAstro::GetMoonPeriod(Double_t mjd)
629{
630 const Double_t et = mjd-kEpoch0; // Elapsed time
631 return et/kSynMonth;
632}
633
634// --------------------------------------------------------------------------
635//
636// Convert a moon period back to a mjd
637//
638// See also
639// MAstro::GetMoonPeriod
640//
641Double_t MAstro::GetMoonPeriodMjd(Double_t p)
642{
643 return p*kSynMonth+kEpoch0;
644}
645
646// --------------------------------------------------------------------------
647//
648// To get the moon period as defined for MAGIC observation we take the
649// nearest integer mjd, eg:
650// 53257.8 --> 53258
651// 53258.3 --> 53258
652// Which is the time between 12h and 11:59h of the following day. To
653// this day-period we assign the moon-period at midnight. To get
654// the MAGIC definition we now substract 284.
655//
656// For MAGIC observation period do eg:
657// GetMagicPeriod(53257.91042)
658// or
659// MTime t;
660// t.SetMjd(53257.91042);
661// GetMagicPeriod(t.GetMjd());
662// or
663// MTime t;
664// t.Set(2004, 1, 1, 12, 32, 11);
665// GetMagicPeriod(t.GetMjd());
666//
667// To get a floating point magic period use
668// GetMoonPeriod(mjd)-284
669//
670Int_t MAstro::GetMagicPeriod(Double_t mjd)
671{
672 const Double_t mmjd = (Double_t)TMath::Nint(mjd);
673 const Double_t period = GetMoonPeriod(mmjd);
674
675 return (Int_t)TMath::Floor(period)-284;
676}
677
678// --------------------------------------------------------------------------
679//
680// Get the start time (12h noon) of the MAGIC period p.
681//
682// See also
683// MAstro::GetMagicPeriod
684//
685Double_t MAstro::GetMagicPeriodStart(Int_t p)
686{
687 return TMath::Floor(GetMoonPeriodMjd(p+284))+0.5;
688}
689
690// --------------------------------------------------------------------------
691//
692// Returns right ascension and declination [rad] of the sun at the
693// given mjd (ra, dec).
694//
695// returns the mean longitude [rad].
696//
697// from http://xoomer.alice.it/vtomezzo/sunriset/formulas/index.html
698//
699Double_t MAstro::GetSunRaDec(Double_t mjd, Double_t &ra, Double_t &dec)
700{
701 const Double_t T = (mjd-51544.5)/36525;// + (h-12)/24.0;
702
703 const Double_t T2 = T<0 ? -T*T : T*T;
704 const Double_t T3 = T*T*T;
705
706 // Find the ecliptic longitude of the Sun
707
708 // Geometric mean longitude of the Sun
709 const Double_t L = 280.46646 + 36000.76983*T + 0.0003032*T2;
710
711 // mean anomaly of the Sun
712 Double_t g = 357.52911 + 35999.05029*T - 0.0001537*T2;
713 g *= TMath::DegToRad();
714
715 // Longitude of the moon's ascending node
716 Double_t omega = 125.04452 - 1934.136261*T + 0.0020708*T2 + T3/450000;
717 omega *= TMath::DegToRad();
718
719 const Double_t coso = cos(omega);
720 const Double_t sino = sin(omega);
721
722 // Equation of the center
723 const Double_t C = (1.914602 - 0.004817*T - 0.000014*T2)*sin(g) +
724 (0.019993 - 0.000101*T)*sin(2*g) + 0.000289*sin(3*g);
725
726 // True longitude of the sun
727 const Double_t tlong = L + C;
728
729 // Apperent longitude of the Sun (ecliptic)
730 Double_t lambda = tlong - 0.00569 - 0.00478*sino;
731 lambda *= TMath::DegToRad();
732
733 // Obliquity of the ecliptic
734 Double_t obliq = 23.4392911 - 0.01300416667*T - 0.00000016389*T2 + 0.00000050361*T3 + 0.00255625*coso;
735 obliq *= TMath::DegToRad();
736
737 // Find the RA and DEC of the Sun
738 const Double_t sinl = sin(lambda);
739
740 ra = atan2(cos(obliq) * sinl, cos(lambda));
741 dec = asin(sin(obliq) * sinl);
742
743 return L*TMath::DegToRad();
744}
745
746// --------------------------------------------------------------------------
747//
748// Returns right ascension and declination [rad] of the moon at the
749// given mjd (ra, dec).
750//
751void MAstro::GetMoonRaDec(Double_t mjd, Double_t &ra, Double_t &dec)
752{
753 // Mean Moon orbit elements as of 1990.0
754 const Double_t l0 = 318.351648 * TMath::DegToRad();
755 const Double_t P0 = 36.340410 * TMath::DegToRad();
756 const Double_t N0 = 318.510107 * TMath::DegToRad();
757 const Double_t i = 5.145396 * TMath::DegToRad();
758
759 Double_t sunra, sundec, g;
760 {
761 const Double_t T = (mjd-51544.5)/36525;// + (h-12)/24.0;
762 const Double_t T2 = T<0 ? -T*T : T*T;
763
764 GetSunRaDec(mjd, sunra, sundec);
765
766 // mean anomaly of the Sun
767 g = 357.52911 + 35999.05029*T - 0.0001537*T2;
768 g *= TMath::DegToRad();
769 }
770
771 const Double_t sing = sin(g)*TMath::DegToRad();
772
773 const Double_t D = (mjd-47891) * TMath::DegToRad();
774 const Double_t l = 13.1763966*D + l0;
775 const Double_t MMoon = l -0.1114041*D - P0; // Moon's mean anomaly M
776 const Double_t N = N0 -0.0529539*D; // Moon's mean ascending node longitude
777
778 const Double_t C = l-sunra;
779 const Double_t Ev = 1.2739 * sin(2*C-MMoon) * TMath::DegToRad();
780 const Double_t Ae = 0.1858 * sing;
781 const Double_t A3 = 0.37 * sing;
782 const Double_t MMoon2 = MMoon+Ev-Ae-A3; // corrected Moon anomaly
783
784 const Double_t Ec = 6.2886 * sin(MMoon2) * TMath::DegToRad(); // equation of centre
785 const Double_t A4 = 0.214 * sin(2*MMoon2)* TMath::DegToRad();
786 const Double_t l2 = l+Ev+Ec-Ae+A4; // corrected Moon's longitude
787
788 const Double_t V = 0.6583 * sin(2*(l2-sunra)) * TMath::DegToRad();
789 const Double_t l3 = l2+V; // true orbital longitude;
790
791 const Double_t N2 = N -0.16*sing;
792
793 ra = fmod( N2 + atan2( sin(l3-N2)*cos(i), cos(l3-N2) ), TMath::TwoPi() );
794 dec = asin(sin(l3-N2)*sin(i) );
795}
796
797// --------------------------------------------------------------------------
798//
799// Return Euqation of time in hours for given mjd
800//
801Double_t MAstro::GetEquationOfTime(Double_t mjd)
802{
803 Double_t ra, dec;
804 const Double_t L = fmod(GetSunRaDec(mjd, ra, dec), TMath::TwoPi());
805
806 if (L-ra>TMath::Pi())
807 ra += TMath::TwoPi();
808
809 return 24*(L - ra)/TMath::TwoPi();
810}
811
812// --------------------------------------------------------------------------
813//
814// Returns noon time (the time of the highest altitude of the sun)
815// at the given mjd and at the given observers longitude [deg]
816//
817// The maximum altitude reached at noon time is
818// altmax = 90.0 + dec - latit;
819// if (dec > latit)
820// altmax = 90.0 + latit - dec;
821// dec=Declination of the sun
822//
823Double_t MAstro::GetNoonTime(Double_t mjd, Double_t longit)
824{
825 const Double_t equation = GetEquationOfTime(TMath::Floor(mjd));
826 return 12. + equation - longit/15;
827}
828
829// --------------------------------------------------------------------------
830//
831// Returns the time (in hours) between noon (the sun culmination)
832// and the sun being at height alt[deg] (90=zenith, 0=horizont)
833//
834// civil twilight: 0deg to -6deg
835// nautical twilight: -6deg to -12deg
836// astronom twilight: -12deg to -18deg
837//
838// latit is the observers latitude in rad
839//
840// returns -1 in case the sun doesn't reach this altitude.
841// (eg. alt=0: Polarnight or -day)
842//
843// To get the sun rise/set:
844// double timediff = MAstro::GetTimeFromNoonToAlt(mjd, latit*TMath::DegToRad(), par[0]);
845// double noon = MAstro::GetNoonTime(mjd, longit);
846// double N = TMath::Floor(mjd)+noon/24.;
847// double risetime = N-timediff/24.;
848// double settime = N+timediff/24.;
849//
850Double_t MAstro::GetTimeFromNoonToAlt(Double_t mjd, Double_t latit, Double_t alt)
851{
852 Double_t ra, dec;
853 GetSunRaDec(mjd, ra, dec);
854
855 const Double_t h = alt*TMath::DegToRad();
856
857 const Double_t arg = (sin(h) - sin(latit)*sin(dec))/(cos(latit)*cos(dec));
858
859 return TMath::Abs(arg)>1 ? -1 : 12*acos(arg)/TMath::Pi();
860}
861
862// --------------------------------------------------------------------------
863//
864// Returns the time of the sunrise/set calculated before and after
865// the noon of floor(mjd) (TO BE IMPROVED)
866//
867// Being longit and latit the longitude and latitude of the observer
868// in deg and alt the hight above or below the horizont in deg.
869//
870// civil twilight: 0deg to -6deg
871// nautical twilight: -6deg to -12deg
872// astronom twilight: -12deg to -18deg
873//
874// A TArrayD(2) is returned with the the mjd of the sunrise in
875// TArray[0] and the mjd of the sunset in TArrayD[1].
876//
877TArrayD MAstro::GetSunRiseSet(Double_t mjd, Double_t longit, Double_t latit, Double_t alt)
878{
879 const Double_t timediff = MAstro::GetTimeFromNoonToAlt(mjd, latit*TMath::DegToRad(), alt);
880 const Double_t noon = MAstro::GetNoonTime(mjd, longit);
881
882 const Double_t N = TMath::Floor(mjd)+noon/24.;
883
884 const Double_t rise = timediff<0 ? N-0.5 : N-timediff/24.;
885 const Double_t set = timediff<0 ? N+0.5 : N+timediff/24.;
886
887 TArrayD rc(2);
888 rc[0] = rise;
889 rc[1] = set;
890 return rc;
891}
892
893// --------------------------------------------------------------------------
894//
895// Returns the distance in x,y between two polar-vectors (eg. Alt/Az, Ra/Dec)
896// projected on aplain in a distance dist. For Magic this this the distance
897// of the camera plain (1700mm) dist also determins the unit in which
898// the TVector2 is returned.
899//
900// v0 is the reference vector (eg. the vector to the center of the camera)
901// v1 is the vector to which we determin the distance on the plain
902//
903// (see also MStarCamTrans::Loc0LocToCam())
904//
905TVector2 MAstro::GetDistOnPlain(const TVector3 &v0, TVector3 v1, Double_t dist)
906{
907 v1.RotateZ(-v0.Phi());
908 v1.RotateY(-v0.Theta());
909 v1.RotateZ(-TMath::Pi()/2); // exchange x and y
910 v1 *= dist/v1.Z();
911
912 return v1.XYvector(); //TVector2(v1.Y(), -v1.X());//v1.XYvector();
913}
914
915// --------------------------------------------------------------------------
916//
917// Calculate the absolute misspointing from the nominal zenith angle nomzd
918// and the deviations in zd (devzd) and az (devaz).
919// All values given in deg, the return value, too.
920//
921Double_t MAstro::GetDevAbs(Double_t nomzd, Double_t meszd, Double_t devaz)
922{
923 nomzd *= TMath::DegToRad();
924 meszd *= TMath::DegToRad();
925 devaz *= TMath::DegToRad();
926
927 const double x = sin(meszd) * sin(nomzd) * cos(devaz);
928 const double y = cos(meszd) * cos(nomzd);
929
930 return acos(x + y) * TMath::RadToDeg();
931}
932
933// --------------------------------------------------------------------------
934//
935// Returned is the offset (number of days) which must be added to
936// March 1st of the given year, eg:
937//
938// Int_t offset = GetDayOfEaster(2004);
939//
940// MTime t;
941// t.Set(year, 3, 1);
942// t.SetMjd(t.GetMjd()+offset);
943//
944// cout << t << endl;
945//
946// If the date coudn't be calculated -1 is returned.
947//
948// The minimum value returned is 21 corresponding to March 22.
949// The maximum value returned is 55 corresponding to April 25.
950//
951// --------------------------------------------------------------------------
952//
953// Gauss'sche Formel zur Berechnung des Osterdatums
954// Wann wird Ostern gefeiert? Wie erfährt man das Osterdatum für ein
955// bestimmtes Jahr, ohne in einen Kalender zu schauen?
956//
957// Ostern ist ein "bewegliches" Fest. Es wird am ersten Sonntag nach dem
958// ersten Frühlingsvollmond gefeiert. Damit ist der 22. März der früheste
959// Termin, der 25. April der letzte, auf den Ostern fallen kann. Von
960// diesem Termin hängen auch die Feste Christi Himmelfahrt, das 40 Tage
961// nach Ostern, und Pfingsten, das 50 Tage nach Ostern gefeiert wird, ab.
962//
963// Von Carl Friedrich Gauß (Mathematiker, Astronom und Physiker;
964// 1777-1855) stammt ein Algorithmus, der es erlaubt ohne Kenntnis des
965// Mondkalenders die Daten der Osterfeste für die Jahre 1700 bis 2199 zu
966// bestimmen.
967//
968// Gib eine Jahreszahl zwischen 1700 und 2199 ein:
969//
970// Und so funktioniert der Algorithmus:
971//
972// Es sei:
973//
974// J die Jahreszahl
975// a der Divisionsrest von J/19
976// b der Divisionsrest von J/4
977// c der Divisionsrest von J/7
978// d der Divisionsrest von (19*a + M)/30
979// e der Divisionsrest von (2*b + 4*c + 6*d + N)/7
980//
981// wobei M und N folgende Werte annehmen:
982//
983// für die Jahre M N
984// 1583-1599 22 2
985// 1600-1699 22 2
986// 1700-1799 23 3
987// 1800-1899 23 4
988// 1900-1999 24 5
989// 2000-2099 24 5
990// 2100-2199 24 6
991// 2200-2299 25 0
992// 2300-2399 26 1
993// 2400-2499 25 1
994//
995// Dann fällt Ostern auf den
996// (22 + d + e)ten März
997//
998// oder den
999// (d + e - 9)ten April
1000//
1001// Beachte:
1002// Anstelle des 26. Aprils ist immer der 19. April zu setzen,
1003// anstelle des 25. Aprils immer dann der 18. April, wenn d=28 und a>10.
1004//
1005// Literatur:
1006// Schüler-Rechenduden
1007// Bibliographisches Institut
1008// Mannheim, 1966
1009//
1010// --------------------------------------------------------------------------
1011//
1012// Der Ostersonntag ist ein sog. unregelmäßiger Feiertag. Alle anderen
1013// unregelmäßigen Feiertage eines Jahres leiten sich von diesem Tag ab:
1014//
1015// * Aschermittwoch ist 46 Tage vor Ostern.
1016// * Pfingsten ist 49 Tage nach Ostern.
1017// * Christi Himmelfahrt ist 10 Tage vor Pfingsten.
1018// * Fronleichnam ist 11 Tage nach Pfingsten.
1019//
1020// Man muß also nur den Ostersonntag ermitteln, um alle anderen
1021// unregelmäßigen Feiertage zu berechnen. Doch wie geht das?
1022//
1023// Dazu etwas Geschichte:
1024//
1025// Das 1. Kirchenkonzil im Jahre 325 hat festgelegt:
1026//
1027// * Ostern ist stets am ersten Sonntag nach dem ersten Vollmond des
1028// Frühlings.
1029// * Stichtag ist der 21. März, die "Frühlings-Tagundnachtgleiche".
1030//
1031// Am 15.10.1582 wurde von Papst Gregor XIII. der bis dahin gültige
1032// Julianische Kalender reformiert. Der noch heute gültige Gregorianische
1033// Kalender legt dabei folgendes fest:
1034//
1035// Ein Jahr hat 365 Tage und ein Schaltjahr wird eingefügt, wenn das Jahr
1036// durch 4 oder durch 400, aber nicht durch 100 teilbar ist. Hieraus
1037// ergeben sich die zwei notwendigen Konstanten, um den Ostersonntag zu
1038// berechnen:
1039//
1040// 1. Die Jahreslänge von und bis zum Zeitpunkt der
1041// Frühlings-Tagundnachtgleiche: 365,2422 mittlere Sonnentage
1042// 2. Ein Mondmonat: 29,5306 mittlere Sonnentage
1043//
1044// Mit der "Osterformel", von Carl Friedrich Gauß (1777-1855) im Jahre 1800
1045// entwickelt, läßt sich der Ostersonntag für jedes Jahr von 1583 bis 8202
1046// berechnen.
1047//
1048// Der früheste mögliche Ostertermin ist der 22. März. (Wenn der Vollmond
1049// auf den 21. März fällt und der 22. März ein Sonntag ist.)
1050//
1051// Der späteste mögliche Ostertermin ist der 25. April. (Wenn der Vollmond
1052// auf den 21. März fällt und der 21. März ein Sonntag ist.)
1053//
1054Int_t MAstro::GetEasterOffset(UShort_t year)
1055{
1056 if (year<1583 || year>2499)
1057 {
1058 cout << "MAstro::GetDayOfEaster - Year " << year << " not between 1700 and 2199" << endl;
1059 return -1;
1060 }
1061
1062 Int_t M=0;
1063 Int_t N=0;
1064 switch (year/100)
1065 {
1066 case 15:
1067 case 16: M=22; N=2; break;
1068 case 17: M=23; N=3; break;
1069 case 18: M=23; N=4; break;
1070 case 19:
1071 case 20: M=24; N=5; break;
1072 case 21: M=24; N=6; break;
1073 case 22: M=25; N=0; break;
1074 case 23: M=26; N=1; break;
1075 case 24: M=25; N=1; break;
1076 }
1077
1078 const Int_t a = year%19;
1079 const Int_t b = year%4;
1080 const Int_t c = year%7;
1081 const Int_t d = (19*a + M)%30;
1082 const Int_t e = (2*b + 4*c + 6*d + N)%7;
1083
1084 if (e==6 && d==28 && a>10)
1085 return 48;
1086
1087 if (d+e==35)
1088 return 49;
1089
1090 return d + e + 21;
1091}
Note: See TracBrowser for help on using the repository browser.