Main Page   Namespace List   Compound List   File List   Compound Members   File Members  

astroutil.cxx

Go to the documentation of this file.
00001 #include <cmath>
00002 #include "astroutil.h"
00003 
00004 
00005 double GetJD(int An,int Me,int Gio,double utc)
00006 {
00007         int A,B,D;
00008         double J_D;
00009         long int C;
00010 
00011         if (Me > 2);
00012         else {
00013                 An = An - 1;
00014                 Me = Me + 12;
00015                 }
00016         A = (An / 100); B = 2 - A + (A / 4);
00017         C = (long int)(365.25 * An); if (An < 0) C = C - 1;
00018         D = (int)(30.6001 * (Me + 1));
00019         J_D = B + C + D + Gio + 1720994.5+ utc / 24.;
00020         return J_D;
00021 }   /* Giorno_Giuliano */
00022 
00023 double          GetGMST(double J_D)//double Ora_Un_Dec)
00024 {
00025         double M,T,T1,Tempo_Siderale_0,Tempo_Siderale_Ora,Tempo_Siderale_Loc;
00026         double Ora_Un_Dec=modf(J_D-0.5,&M)*24;  J_D-=Ora_Un_Dec/24; 
00027         T = ((J_D) - J2000) / 36525.;
00028         T1 = (24110.54841 + 8640184.812866 * T + 0.0093103 * T * T)/86400.0;
00029         Tempo_Siderale_0 = modf(T1,&M) * 24.;
00030         Tempo_Siderale_Ora = Tempo_Siderale_0 + Ora_Un_Dec * 1.00273790935;
00031         if (Tempo_Siderale_Ora < 0.) Tempo_Siderale_Ora = Tempo_Siderale_Ora + 24.;
00032         if (Tempo_Siderale_Ora >= 24.) Tempo_Siderale_Ora = Tempo_Siderale_Ora - 24.;
00033         Tempo_Siderale_Loc = Tempo_Siderale_Ora*15.;
00034         return Tempo_Siderale_Loc;
00035 }   /* Calcolo_Tempo_Siderale */
00036 
00037 
00038 double Kepler(double MeanAnomaly,double Eccentricity)
00039 {
00040 double E;              /* Eccentric Anomaly                    */
00041 double Error;
00042 double TrueAnomaly;
00043  
00044     E = MeanAnomaly;    /* Initial guess */
00045     do
00046         {
00047         Error = (E - Eccentricity*sin(E) - MeanAnomaly)
00048                 / (1. - Eccentricity*cos(E));
00049         E -= Error;
00050         }
00051    while (fabs(Error) >= 0.000001);
00052  
00053     if (fabs(E-PI) < 0.000001)
00054         TrueAnomaly = PI;
00055       else
00056         TrueAnomaly = 2.*atan(sqrt((1.+Eccentricity)/(1.-Eccentricity))
00057                                 *tan(E/2.));
00058     if (TrueAnomaly < 0)
00059         TrueAnomaly += PI2;
00060  
00061     return TrueAnomaly;
00062 }
00063 
00064 
00065 
00066 
00067 
00068 void matrix_transpose(double a[3][3], double b[3][3])
00069 {
00070    int i, j;
00071    double temp[3][3];
00072 
00073    /* This loop transposes the matrix into a temporary matrix */
00074    for(i=0; i<3; i++) {
00075       for(j=0; j<3; j++) {
00076          temp[j][i] = a[i][j];
00077       }/* end for j */
00078    }/* end for i */
00079 
00080    /* And then we assign the temporary matrix to the destination matrix */
00081    /* This step is necessary in case the original and destination matrix
00082       are the same variable */
00083    for(i=0; i<3; i++) {
00084       for(j=0; j<3; j++) {
00085          b[j][i] = temp[j][i];
00086       }/* end for j */
00087    }/* end for i */
00088 
00089 }/* end matrix_transpose() */
00090 
00091 void matrix_multiply(double a[3][3], double b[3][3], double c[3][3])
00092 {
00093    int i, j, k;
00094    double sum;
00095 
00096    for(i=0; i<3; i++) {
00097       for(k=0; k<3; k++) {
00098         sum = 0;
00099         for(j=0; j<3; j++) {
00100            sum += a[i][j] * b[j][k];
00101         }/* end for j */
00102         c[i][k] = sum;
00103       }/* end for k */
00104    }/* end for i */
00105 }/* end matrix_multiply() */
00106 
00107 void calc_unit_vector(double a[3], double b[3])
00108 {
00109    double sqr_root;
00110    sqr_root = sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
00111    b[0] = a[0] / sqr_root;
00112    b[1] = a[1] / sqr_root;
00113    b[2] = a[2] / sqr_root;
00114 }/* end calc_unit_vector() */
00115 
00116 void vector_cross_product(double a[3], double b[3], double c[3])
00117 {
00118    c[0] = a[1]*b[2] - a[2]*b[1];
00119    c[1] = a[2]*b[0] - a[0]*b[2];
00120    c[2] = a[0]*b[1] - a[1]*b[0];
00121 }/* end vector_cross_product() */
00122 
00123 void vector_matrix_multiply(const double a[3], double b[3][3], double c[3])
00124 {
00125    int i;
00126    for (i=0;i<=2;i++) {
00127       c[i] = a[0]*b[0][i] + a[1]*b[1][i] + a[2]*b[2][i];
00128    }/* end for */
00129 }/* end vector_matrix_multiply() */
00130 
00131 double modulo(double a, double b)
00132 {
00133 
00134    if ((a - floor(a/b)*b) >= 0.0) return(a - floor(a/b)*b);
00135    else return(a - floor(a/b)*b + b);
00136 }/* fine modulo() */
00137 
00138 
00139 
00140 
00141 void eqtogal(double ra, double dec,double *l,double *b)
00142 {
00143 
00144         double Xe,Ye,Ze;
00145         
00146         double RotM[3][3] = {{-0.0548755604, 0.4941094279, -0.8676661490},{ -0.8734370902, -0.4448296300, -0.1980763734}, {-0.4838350155, 0.7469822445, 0.4559837762}};
00147         double Pos[3];
00148         double Gal[3];
00149 
00150         Pos[0]=Xe=cos(dec*D2R)*cos(ra*D2R);
00151         Pos[1]=Ye=cos(dec*D2R)*sin(ra*D2R);
00152         Pos[2]=Ze=sin(dec*D2R);
00153   
00154         vector_matrix_multiply(Pos, RotM, Gal);
00155 
00156         *l=atan2(Gal[1],Gal[0])*R2D;
00157         *b=atan(Gal[2]/sqrt(SQR(Gal[0])+SQR(Gal[1])))*R2D;
00158 
00159 }
00160 
00161 void galtoeq(double l,double b, double *ra, double *dec)
00162 {
00163 
00164         double Xe,Ye,Ze;
00165         
00166         double RotM[3][3] = {{-0.0548755604, 0.4941094279, -0.8676661490},{ -0.8734370902, -0.4448296300, -0.1980763734}, {-0.4838350155, 0.7469822445, 0.4559837762}};
00167         double Pos[3];
00168         double Gal[3];
00169         double Tra[3][3];
00170         matrix_transpose(RotM,Tra);
00171         Pos[0]=Xe=cos(b*D2R)*cos(l*D2R);
00172         Pos[1]=Ye=cos(b*D2R)*sin(l*D2R);
00173         Pos[2]=Ze=sin(b*D2R);
00174   
00175         vector_matrix_multiply(Pos, Tra, Gal);
00176 
00177         *ra=atan2(Gal[1],Gal[0])*R2D;
00178         *dec=atan(Gal[2]/sqrt(SQR(Gal[0])+SQR(Gal[1])))*R2D;
00179         if(*ra<0.)*ra+=360;
00180 
00181 }
00182 
00183 
00184 void range (double *s, double m)
00185 {
00186         while((*s)>=m) (*s)-=m;
00187         while((*s)<0) (*s)+=m;
00188 }
00189 
00190 /* transformation from spherical to cartesian coordinates */
00191 void sphcart (SPHER s, CARTES *c)
00192 {
00193         double rcb = s.r * cos(s.b);
00194 
00195         c->x = rcb * cos(s.l);
00196         c->y = rcb * sin(s.l);
00197         c->z = s.r * sin(s.b);
00198 }
00199 
00200 /* transformation from cartesian to spherical coordinates */
00201 void car2sph (CARTES c,SPHER *s)        
00202 {
00203         double rho = c.x*c.x + c.y*c.y;
00204 
00205         if (rho > 1e-35) {      /* standard case: off axis */
00206             s->l = atan2(c.y, c.x);
00207             range (&(s->l), 2*PI2);
00208             s->b = atan2(c.z, sqrt(rho));
00209             s->r = sqrt(rho + c.z*c.z);
00210         } else {                /* degenerate case; avoid math error */
00211             s->l = 0.0;
00212             if (c.z == 0.0)
00213                         s->b = 0.0;
00214             else
00215                         s->b = (c.z > 0.0) ? PI2/2. : -PI2/2.;
00216             s->r = fabs(c.z);
00217         }
00218 }
00219 
00220 double modulo_vet(double a[3])
00221 {
00222         return sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]);
00223 }
00224 
00225 void vector_dot_product(double a[3],double b[3],double *mod,double *angle)
00226 {
00227         *mod = a[0]*b[0]+a[1]*b[1]+a[2]*b[2];
00228         *angle=R2D*acos(*mod/(modulo_vet(a)*modulo_vet(b)));
00229 
00230 }
00231 
00232 int vector2radec (double *pos, double *ra, double *dec)
00233 {
00234    double xyproj;
00235 
00236    xyproj = sqrt (pow (pos[0], 2.0) + pow (pos[1], 2.0));
00237    if ((xyproj == 0.0) && (pos[2] == 0))
00238    {
00239       *ra = 0.0;
00240       *dec = 0.0;
00241       return 1;
00242    }
00243     else if (xyproj == 0.0)
00244    {
00245       *ra = 0.0;
00246       if (pos[2] < 0.0)
00247       *dec = -90.0;
00248        else
00249          *dec = 90.0;
00250       return 2;
00251    }
00252     else
00253    {
00254       *ra = atan2 (pos[1], pos[0]) * SECS2RADS / 54000.0;
00255       *dec = atan2 (pos[2], xyproj) * SECS2RADS / 3600.0;
00256 
00257       if (*ra < 0.0)
00258          *ra += 24.0;
00259    }
00260    return 0;
00261 }
00262 
00263 
00264 void Sfer2xyz (double ra, double dec,double *vector)
00265 {
00266 
00267    vector[0] = cos (dec*D2R) * cos ( ra*D2R);
00268    vector[1] = cos (dec*D2R) * sin ( ra*D2R);
00269    vector[2] = sin (dec*D2R);
00270 
00271    return;
00272 }
00273 
00274 int InsideSAA(double lon, double lat)
00275 {
00276         double perim[7][2] = {{-88.,-30.},{-88.,-12.},{-55.,-0.1},{-32.,-0.1},{-7,-12},
00277     {50.,-25},{50.,-30.}};
00278         double longmin=-88;
00279         double longmax=50.;
00280         double latmin=-30;
00281         double latmax=-0.1;
00282         double diffx[7],diffy[7],diffd[7];
00283         if((lon>=longmin)&&(lon<=longmax)&&(lat>=latmin)&&(lat<=latmax)){
00284         for (int i=0;i<6;i++){
00285                         diffx[i]=perim[i+1][0]-perim[i][0];
00286                         diffy[i]=perim[i+1][1]-perim[i][1];
00287                         //cout<<i<<"  "<<diffx[i]<<endl;
00288         }
00289         diffx[6]=perim[0][0]-perim[6][0];
00290         diffy[6]=perim[0][1]-perim[6][1];
00291 
00292         for (i=0;i<7;i++){
00293                         diffd[i]=sqrt(SQR(diffx[i])+SQR(diffy[i]));
00294                         //cout<<diffd[i]<<endl;
00295         }
00296         double xnew[7],ynew[7];
00297         int inside[7];
00298         int xtemp[7],txtemp=0,tins=0;
00299         for (i=0;i<7;i++){
00300                         xnew[i]=((lon-perim[i][0])*diffx[i]+(lat-perim[i][1])*diffy[i])/diffd[i];
00301                         ynew[i]=(lat-perim[i][1])*diffx[i]-(lon-perim[i][0])*diffy[i];
00302                         xtemp[i]=((xnew[i] >=0.)&&(xnew[i]<=diffd[i]));
00303                         inside[i]=(xtemp[i] == 1) && (ynew[i]<0.);
00304                         txtemp+=xtemp[i];
00305                         tins+=inside[i];
00306                         //cout<<diffd[i]<<endl;
00307         }
00308         int insaa=(txtemp==tins)&&(txtemp >0);
00309         //cout<<insaa<<" "<<txtemp<<"  "<<tins<<endl;
00310 
00311         return insaa;
00312         }
00313         else
00314                 return 0;
00315 }
00316 
00317 
00318 void GetRockMat(const double pp[3],const double rock,const int orb,double ppr[3])
00319 {
00320            //rock down for even orbits, up for odd orbits
00321     double rock_sign = -1. + 2.*(orb - 2*(orb/2));
00322         double sinrock = sin(rock_sign*rock*D2R);
00323     double cosrock = cos(rock_sign*rock*D2R);
00324         double rockmat[3][3];
00325         double cost = pp[0]/sqrt(pp[0]*pp[0]+pp[1]*pp[1]);
00326     double sint = -pp[1]/sqrt(pp[0]*pp[0]+pp[1]*pp[1]);
00327     rockmat[0][0]=cosrock*cost*cost+sint*sint;
00328         rockmat[1][0]=(1.-cosrock)*cost*sint;
00329         rockmat[2][0]=-sinrock*cost;
00330     rockmat[0][1]=(1.-cosrock)*cost*sint;
00331         rockmat[1][1]=cosrock*sint*sint+cost*cost;
00332         rockmat[2][1]=sinrock*sint;
00333     rockmat[0][2]=sinrock*cost;
00334         rockmat[1][2]=-sinrock*sint;
00335         rockmat[2][2]=cosrock;
00336         
00337 
00338     ppr[0]=rockmat[0][0]*pp[0]+rockmat[0][1]*pp[1]+rockmat[0][2]*pp[2];
00339     ppr[1]=rockmat[1][0]*pp[0]+rockmat[1][1]*pp[1]+rockmat[1][2]*pp[2];
00340         ppr[2]=rockmat[2][0]*pp[0]+rockmat[2][1]*pp[1]+rockmat[2][2]*pp[2];
00341 }

Generated on Wed Aug 14 10:09:35 2002 for astro by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001