source: tags/Mars-V0.10.2/mastro/MAstro.cc

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