source: tags/Mars-V2.1/mastro/MAstro.cc

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