source: Sophya/trunk/SophyaExt/XAstroPack/xastropack.cc@ 1672

Last change on this file since 1672 was 1628, checked in by cmv, 24 years ago

ajout documentation cmv 7/8/01

File size: 13.6 KB
RevLine 
[1456]1#include <math.h>
2#include <stdio.h>
3#include "xastropack.h"
4
[1628]5/*!
6 \defgroup XAstroPack XAstroPack module
7 This module contains simple programs to perform various
8 astronomical computation (based on the libastro of Xephem).
9
10 \verbatim
[1456]11// TEMPS: modified Julian date (mjd) (number of days elapsed since 1900 jan 0.5)
12// jour [1,31] (dy)
13// mois [1,12] (mn)
14// annee (yr)
[1515]15// universal time [0,24[ (utc)
16// Greenwich mean siderial [0,24[ (gst)
17// Greenwich mean siderial at 0h UT [0,24[ (gst0)
[1456]18// EQUATORIALE: ascension droite en heures [0,24[ (ra)
19// declinaison en degres [-90,90] (dec)
20// angle horaire en heures [-12,12] (-12=12) (ha) tsid=ha+ra
21// GALACTIQUE: longitude en degres [0,360[ (glng)
22// latitude en degres [-90,90] (glat)
[1515]23// HORIZONTAL: azimuth en degres [0,360[ (az)
24// (angle round to the east from north+)
25// altitude en degres [-90,90] (alt)
26// ECLIPTIQUE: lontitude ecliptique en degres [0,360[ (eclng)
27// (angle round counter clockwise from the vernal equinoxe)
28// latitude ecliptique en degres [-90,90] (eclat)
29// GEOGRAPHIE: longitude en degres ]-180,180] (geolng)
30// (angle + vers l'ouest, - vers l'est)
31// latitude en degres [-90,90] (north>0) (geolat)
[1628]32 \endverbatim
33*/
[1456]34
[1628]35/*! \ingroup XAstroPack
36\brief Compute true Julian day from MJD
37*/
[1465]38double TrueJDfrMJD(double mjd)
[1456]39{
40 return mjd + MJD0;
41}
42
[1628]43/*! \ingroup XAstroPack
44\brief Compute MJD from true Julian day
45*/
[1465]46double MJDfrTrueJD(double jd)
47{
48 return jd - MJD0;
49}
50
[1628]51/*! \ingroup XAstroPack
52\brief Compute MJD from date
53*/
[1456]54double MJDfrDate(double dy,int mn,int yr)
55{
56 double mjd;
57 cal_mjd(mn,dy,yr,&mjd);
58 return mjd;
59}
60
[1628]61/*! \ingroup XAstroPack
62\brief Compute date from MJD
63*/
[1456]64void DatefrMJD(double mjd,double *dy,int *mn,int *yr)
65{
66 mjd_cal(mjd,mn,dy,yr);
67}
68
[1628]69/*! \ingroup XAstroPack
70\brief Given a mjd, return the year as a double.
71*/
[1456]72double YearfrMJD(double mjd)
73{
74 double yr;
75 mjd_year(mjd,&yr);
76 return yr;
77}
78
[1628]79/*! \ingroup XAstroPack
80\brief Given a decimal year, return mjd
81*/
[1456]82double MJDfrYear(double yr)
83{
84 double mjd;
85 year_mjd(yr,&mjd);
86 return mjd;
87}
88
[1628]89/*! \ingroup XAstroPack
90\brief Given a mjd, return the year and number of days since 00:00 Jan 1
91\warning: if mjd = 2 January -> number of days = 1
92*/
[1456]93void YDfrMJD(double mjd,double *dy,int *yr)
94{
95 mjd_dayno(mjd,yr,dy);
96}
97
[1628]98/*! \ingroup XAstroPack
99\brief Give GST from UTC
100\verbatim
101 Given a modified julian date, mjd, and a universally coordinated time, utc,
102 return greenwich mean siderial time, *gst.
103 N.B. mjd must be at the beginning of the day.
104\endverbatim
105*/
[1456]106double GSTfrUTC(double mjd0,double utc)
107{
108 double gst;
109 utc_gst(mjd0,utc,&gst) ;
110 return gst;
111}
112
[1628]113/*! \ingroup XAstroPack
114\brief Give UTC from GST
115\verbatim
116 Given a modified julian date, mjd, and a greenwich mean siderial time, gst,
117 return universally coordinated time, *utc.
118 N.B. mjd must be at the beginning of the day.
119\endverbatim
120*/
[1456]121double UTCfrGST(double mjd0,double gst)
122{
123 double utc;
124 gst_utc(mjd0,gst,&utc);
125 return utc;
126}
127
[1628]128/*! \ingroup XAstroPack
129\brief gmst0() - return Greenwich Mean Sidereal Time at 0h UT
130\param mjd = date at 0h UT in julian days since MJD0
131*/
[1456]132double GST0(double mjd0)
133/* Copie depuis le code de Xephem car pas prototype */
134{
135 double T, x;
136 T = ((int)(mjd0 - 0.5) + 0.5 - J2000)/36525.0;
137 x = 24110.54841 +
138 (8640184.812866 + (0.093104 - 6.2e-6 * T) * T) * T;
139 x /= 3600.0;
140 range(&x, 24.0);
141 return (x);
142}
143
[1628]144/*! \ingroup XAstroPack
145\brief Compute precession between 2 dates.
146*/
[1456]147void Precess(double mjd1,double mjd2,double ra1,double dec1,double *ra2,double *dec2)
148{
149 ra1 *= PI/12.; // radians
150 dec1 *= PI/180.; // radians
151 precess(mjd1,mjd2,&ra1,&dec1);
152 *ra2 = ra1*12./PI; if(*ra2>24.) *ra2 -= 24.; if(*ra2==24.) *ra2 = 0.;
153 *dec2 = dec1*180./PI;
154}
155
[1628]156/*! \ingroup XAstroPack
157\brief Given apparent altitude find airmass.
158*/
[1456]159double AirmassfrAlt(double alt)
160{
161 double x;
162 alt *= PI/180.; // radians
163 airmass(alt,&x);
164 return x;
165}
166
[1628]167/*! \ingroup XAstroPack
168\brief Give the hour angle from sideral time and right ascencion
169*/
[1456]170double HafrRaTS(double gst,double ra)
171{
172 double ha = gst - ra;
173 // Attention au probleme de la discontinuite 0h <==> 24h
174 // ts=1 ra=23 ; (ts-ra)=-22 <-12 --> ha = +2 = +24 + (ts-ra)
175 // ts=23 ra=1 ; (ts-ra)=+22 >+12 --> ha = -2 = -24 + (ts-ra)
176 if(ha==-12.) ha = 12.; if(ha<-12.) ha += 24.; if(ha>12.) ha -= 24.;
177 return ha;
178}
179
[1628]180/*! \ingroup XAstroPack
181\brief Give a time in h:mn:s from a decimal hour
182\verbatim
[1456]183// INPUT: hd
[1465]184// OUTPUT: h mn s (h,mn,s >=< 0)
185// REMARQUE: si hd<0 alors h<0 ET mn<0 ET s<0
186// EX: 12.51 -> h=12 mn=30 s=10 ;
187// -12.51 -> h=-12 mn=-30 s=-10 ;
[1628]188\endverbatim
189*/
190void HMSfrHdec(double hd,int *h,int *mn,double *s)
[1456]191{
192 int sgn=1;
193 if(hd<0.) {sgn=-1; hd*=-1.;}
194 *h = int(hd);
195 *mn = int((hd-(double)(*h))*60.);
196 *s = (hd - (double)(*h) - (double)(*mn)/60.)*3600.;
197 // pb precision
198 if(*s<0.) *s = 0.;
199 if(*s>60. || *s==60.) {*s-=60.; *mn+=1;} // s=double attention comparaison
200 if(*mn<0) *mn = 0;
201 if(*mn>=60) {*mn-=60; *h+=1;}
[1465]202 *h *= sgn; *mn *= sgn; *s *= (double)sgn;
[1456]203}
204
[1628]205/*! \ingroup XAstroPack
206\brief Give a decimal hour from a time in h:mn:s
207\verbatim
[1465]208// INPUT: h , mn , s (h,mn,s >=< 0)
209// RETURN: en heures decimales
210// REMARQUE: pour avoir hd=-12.51 <- h=-12 mn=-30 s=-10
[1628]211\endverbatim
212*/
213double HdecfrHMS(int h,int mn,double s)
[1456]214{
[1465]215 return ((double)h + (double)mn/60. + s/3600.);
[1456]216}
217
[1628]218/*! \ingroup XAstroPack
219\brief Give a time string from a time in h:mn:s
220\verbatim
[1465]221// INPUT: h , mn , s (h,mn,s >=< 0)
[1456]222// RETURN: string h:mn:s
[1628]223\endverbatim
224*/
225string ToStringHMS(int h,int mn,double s)
[1456]226{
[1465]227 double hd = HdecfrHMS(h,mn,s); // put in range
228 HMSfrHdec(hd,&h,&mn,&s);
[1456]229 char str[128];
[1465]230 if(hd<0.)
231 sprintf(str,"-%d:%d:%.3f",-h,-mn,-s);
232 else
233 sprintf(str,"%d:%d:%.3f",h,mn,s);
[1456]234 string dum = str;
235 return dum;
236}
237
[1628]238/*! \ingroup XAstroPack
239\brief Give a time string from a decimal hour
240*/
[1456]241string ToStringHdec(double hd)
242{
243 int h,mn; double s;
[1465]244 HMSfrHdec(hd,&h,&mn,&s);
[1456]245 return ToStringHMS(h,mn,s);
246}
247
[1628]248/*! \ingroup XAstroPack
249\brief Convert equatorial coordinates into galactic coordinates
250*/
[1456]251void EqtoGal(double mjd,double ra,double dec, double *glng,double *glat)
252// Coordonnees equatoriales -> Coordonnees galactiques
253{
254 ra *= PI/12.; // radians
255 dec *= PI/180.; // radians
256 eq_gal(mjd,ra,dec,glat,glng);
257 // Vraiment bizarre, sur Linux-g++ glng>=360 ne comprend pas glng==360 ! (CMV)
258 *glng *= 180./PI; if(*glng>360.) *glng -= 360.; if(*glng==360.) *glng = 0.;
259 *glat *= 180./PI;
260}
261
[1628]262/*! \ingroup XAstroPack
263\brief Convert galactic coordinates into equatorial coordinates
264*/
[1456]265void GaltoEq(double mjd,double glng,double glat,double *ra,double *dec)
266// Coordonnees galactiques -> Coordonnees equatoriales
267{
268 glng *= PI/180.; // radians
269 glat *= PI/180.; // radians
270 gal_eq (mjd,glat,glng,ra,dec);
271 *ra *= 12./PI; if(*ra>24.) *ra -= 24.; if(*ra==24.) *ra = 0.;
272 *dec *= 180./PI;
273}
274
[1628]275/*! \ingroup XAstroPack
276\brief Convert equatorial coordinates into horizontal coordinates
277*/
[1456]278void EqtoHor(double geolat,double ha,double dec,double *az,double *alt)
279// Coordonnees equatoriales -> Coordonnees horizontales
280{
281 geolat *= PI/180.;
282 ha *= PI/12.; // radians
283 dec *= PI/180.; // radians
284 hadec_aa (geolat,ha,dec,alt,az);
285 *alt *= 180./PI;
286 *az *= 180./PI; if(*az>360.) *az -= 360.; if(*az==360.) *az = 0.;
287}
288
[1628]289/*! \ingroup XAstroPack
290 Convert horizontal coordinates into equatorial coordinates
291*/
[1456]292void HortoEq(double geolat,double az,double alt,double *ha,double *dec)
293// Coordonnees horizontales -> Coordonnees equatoriales
294{
295 geolat *= PI/180.;
296 alt *= PI/180.; // radians
297 az *= PI/180.; // radians
298 aa_hadec (geolat,alt,az,ha,dec);
299 *ha *= 12./PI;
300 if(*ha==-12.) *ha = 12.; if(*ha<-12.) *ha += 24.; if(*ha>12.) *ha -= 24.;
301 *dec *= 180./PI;
302}
303
[1628]304/*! \ingroup XAstroPack
305\brief Convert equatorial coordinates into ecliptic coordinates
306*/
[1456]307// Attention, j'ai modifie eq_ecl.c pour proteger NaN
308// dans ecleq_aux :
309// *q = (sy*ceps)-(cy*seps*sx*sw);
310// if(*q<-1.) *q = -PI/2.; else if(*q>1.) *q = PI/2.; else *q = asin(*q);
311void EqtoEcl(double mjd,double ra,double dec,double *eclng,double *eclat)
312// Coordonnees equatoriales -> Coordonnees ecliptiques
313{
314 ra *= PI/12.; // radians
315 dec *= PI/180.; // radians
316 eq_ecl(mjd,ra,dec,eclat,eclng);
317 *eclng *= 180./PI; if(*eclng>360.) *eclng -= 360.; if(*eclng==360.) *eclng = 0.;
318 *eclat *= 180./PI;
319}
320
[1628]321/*! \ingroup XAstroPack
322\brief Convert ecliptic coordinates into equatorial coordinates
323*/
[1456]324void EcltoEq(double mjd,double eclng,double eclat,double *ra,double *dec)
325// Coordonnees ecliptiques -> Coordonnees equatoriales
326{
327 eclat *= PI/180.; // radians
328 eclng *= PI/180.; // radians
329 ecl_eq(mjd,eclat,eclng,ra,dec);
330 *ra *= 12./PI; if(*ra>24.) *ra -= 24.; if(*ra==24.) *ra = 0.;
331 *dec *= 180./PI;
332}
333
[1628]334/*! \ingroup XAstroPack
335\brief Give Sun position
336\verbatim
337 given the modified JD, mjd, return the true geocentric ecliptic longitude
338 of the sun for the mean equinox of the date, *lsn, in radians, the
339 sun-earth distance, *rsn, in AU, and the latitude *bsn, in radians
340 (since this is always <= 1.2 arcseconds, in can be neglected by
341 calling with bsn = NULL).
342\endverbatim
343*/
[1456]344void SunPos(double mjd,double *eclsn,double *ecbsn)
345{
346 double rsn;
347 sunpos(mjd,eclsn,&rsn,ecbsn);
348 *eclsn *= 180./PI; if(*eclsn>360.) *eclsn -= 360.; if(*eclsn==360.) *eclsn = 0.;
349 *ecbsn *= 180./PI;
350}
351
[1628]352/*! \ingroup XAstroPack
353\brief Give Moon position
354\verbatim
355 given the mjd, find the geocentric ecliptic longitude, lam, and latitude,
356 bet, and geocentric distance, rho in a.u. for the moon. also return
357 the sun's mean anomaly, *msp, and the moon's mean anomaly, *mdp.
358 (for the mean equinox)
359\endverbatim
360*/
[1456]361void MoonPos(double mjd,double *eclmn,double *ecbmn)
362{
363 double rho,msp,mdp;
364 moon(mjd,eclmn,ecbmn,&rho,&msp,&mdp);
365 *eclmn *= 180./PI; if(*eclmn>360.) *eclmn -= 360.; if(*eclmn==360.) *eclmn = 0.;
366 *ecbmn *= 180./PI;
367}
368
[1628]369/*! \ingroup XAstroPack
370\brief Give planet position
371\verbatim
372 * given a modified Julian date, mjd, and a planet, p, find:
[1456]373 * lpd0: heliocentric longitude,
374 * psi0: heliocentric latitude,
375 * rp0: distance from the sun to the planet,
376 * rho0: distance from the Earth to the planet,
377 * none corrected for light time, ie, they are the true values for the
378 * given instant.
379 * lam: geocentric ecliptic longitude,
380 * bet: geocentric ecliptic latitude,
381 * each corrected for light time, ie, they are the apparent values as
382 * seen from the center of the Earth for the given instant.
383 * dia: angular diameter in arcsec at 1 AU,
384 * mag: visual magnitude when 1 AU from sun and earth at 0 phase angle.
[1628]385 * (for the mean equinox)
386 \endverbatim
387*/
388void PlanetPos(double mjd,int numplan,double *ecl,double *ecb,double *diamang)
[1456]389{
390 double lpd0,psi0,rp0,rho0,mag;
391 plans(mjd,numplan,&lpd0,&psi0,&rp0,&rho0,ecl,ecb,diamang,&mag);
392 *ecl *= 180./PI; if(*ecl>360.) *ecl -= 360.; if(*ecl==360.) *ecl = 0.;
393 *ecb *= 180./PI;
394}
395
[1628]396/*! \ingroup XAstroPack
397\brief Give Jupiter position
398*/
[1456]399void JupiterPos(double mjd,double *ecl,double *ecb,double *diamang)
400{
401 PlanetPos(mjd,JUPITER,ecl,ecb,diamang);
402}
403
[1628]404/*! \ingroup XAstroPack
405\brief Give Saturn position
406*/
[1456]407void SaturnPos(double mjd,double *ecl,double *ecb,double *diamang)
408{
409 PlanetPos(mjd,SATURN,ecl,ecb,diamang);
410}
[1515]411
[1628]412/*! \ingroup XAstroPack
413\brief Given a coordinate type "typ", convert to standard for astropack
414\verbatim
415// Return : 0 = OK
416// 1 = Unknown type of coordinates
417// 2 = bad range for coord1
418// 4 = bad range for coord2
419// 6 = bad range for coord1 et coord2
420\endverbatim
421*/
[1515]422int CoordConvertToStd(TypAstroCoord typ,double& coord1,double& coord2)
423{
424 int rc = 0;
425
426 // ---- Equatoriales alpha,delta
427 // - standard = [0,24[ , [-90,90]
428 if(typ&TypCoordEq) {
429 if(typ&TypCoordDD) {
430 coord1 = coord1 / 180. * 12.;
431 } else if(typ&TypCoordRR) {
432 coord1 = coord1 / PI * 12.;
433 coord2 = coord2 / PI * 180.;
434 }
435 if(coord1==24.) coord1 = 0.;
436 if(coord1<0. || coord1>=24.) rc+= 2;
437 if(coord2<-90. || coord2>90. ) rc+= 4;
438
439 // ---- Galactiques gLong, gLat
440 // ---- Horizontales azimuth,altitude
441 // ---- Ecliptiques EclLong,EclLat
442 // - standard = [0,360[ , [-90,90]
443 } else if( typ&TypCoordGal || typ&TypCoordHor || typ&TypCoordEcl) {
444 if(typ&TypCoordHD) {
445 coord1 = coord1 / 12. * 180.;
446 } else if(typ&TypCoordRR) {
447 coord1 = coord1 / PI * 180.;
448 coord2 = coord2 / PI * 180.;
449 }
450 if(coord1==360.) coord1 = 0.;
451 if(coord1<0. || coord1>=360.) rc+= 2;
452 if(coord2<-90. || coord2>90. ) rc+= 4;
453
454 } else { // Coordonnees non-connues
455 rc= 1;
456 }
457
458 return rc;
459}
Note: See TracBrowser for help on using the repository browser.