geodesy_main.c

Go to the documentation of this file.
00001 /**
00002 \file    geodesy_main.c
00003 \brief   'c' main program to use the function library geodesy.h/.c
00004 \author  Glenn D. MacGougan (GDM)
00005 \date    2007-11-29
00006 \since   2007-04-20
00007 
00008 \b "LICENSE INFORMATION" \n
00009 Copyright (c) 2006, refer to 'author' doxygen tags \n
00010 All rights reserved. \n
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided the following conditions are met: \n
00014 
00015 - Redistributions of source code must retain the above copyright
00016   notice, this list of conditions and the following disclaimer. \n
00017 - Redistributions in binary form must reproduce the above copyright
00018   notice, this list of conditions and the following disclaimer in the
00019   documentation and/or other materials provided with the distribution. \n
00020 - The name(s) of the contributor(s) may not be used to endorse or promote 
00021   products derived from this software without specific prior written 
00022   permission. \n
00023 
00024 THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
00025 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
00026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027 DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
00028 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
00030 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
00031 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
00032 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00033 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
00034  SUCH DAMAGE.
00035 */
00036 
00037 #include <stdio.h>
00038 #include <stdlib.h>
00039 #include "geodesy.h"
00040 #include "constants.h"
00041 
00042 /// \brief  A  main() for a geodesy application.
00043 int main( int argc, char* argv[] )
00044 {
00045   const int min_nr_args = 3;
00046   int ellipse=0;
00047   int fn_choice=0;
00048   GEODESY_enumReferenceEllipse  referenceEllipse = GEODESY_REFERENCE_ELLIPSE_WGS84;
00049   BOOL result = FALSE;
00050 
00051   if( argc < min_nr_args )
00052   {
00053     printf("\nUSAGE\n");
00054     printf("geodesy <ellipse> <function> <additional arguments for function ... >\n");
00055     printf("\nellipse:\n");
00056     printf("0,   WGS84\n");
00057     printf("1,   Airy 1830\n");
00058     printf("2,   Modified Airy\n");
00059     printf("3,   Australian National\n");
00060     printf("4,   Bessel 1841\n");
00061     printf("5,   Clarke 1866\n");
00062     printf("6,   Clarke 1880\n");
00063     printf("7,   Everest(India 1830)\n");
00064     printf("8,   Everest(Brunei & E.Malaysia)\n");
00065     printf("9,   Everest(W.Malaysia & Singapore)\n");
00066     printf("10,  Geodetic Reference System 1980\n");
00067     printf("11,  Helmert 1906\n");
00068     printf("12,  Hough 1960\n");
00069     printf("13,  International 1924\n");
00070     printf("14,  South American 1969\n");
00071     printf("15,  World Geodetic System 1972\n");
00072 
00073     printf("\nfunction and additional arguments:\n");
00074     printf("0, GEODESY_GetReferenceEllipseParameters\n");
00075     printf("   requires no additions arguments\n");
00076     printf("1, GEODESY_ConvertGeodeticCurvilinearToEarthFixedCartesianCoordinates\n");
00077     printf("   latitude[deg], longitude[deg], height[m]\n");
00078     printf("2, GEODESY_ConvertEarthFixedCartesianToGeodeticCurvilinearCoordinates\n");
00079     printf("   userX[m], userY[m], userZ[m]\n");
00080     printf("3, GEODESY_ComputeNorthingEastingVertical\n");
00081     printf("   ref_lat[deg], ref_lon[deg] ref_hgt[m], lat[deg], lon[deg], hgt[m]\n");
00082     printf("4  ComputeAzimuthAndElevationAnglesBetweenToPointsInTheEarthFixedFrame\n" );
00083     printf("   fromX[m], fromY[m], fromZ[m], toX[m], toY[m], toZ[m]\n" );
00084     printf("5, GEODESY_ComputePositionDifference\n");
00085     printf("   ref_lat[deg], ref_lon[deg] ref_hgt[m], lat[deg], lon[deg], hgt[m]\n");
00086 
00087     return 0;
00088   }
00089 
00090   ellipse = atoi(argv[1]);
00091   if( ellipse > 15 )
00092   { 
00093     printf("Invalid ellipse argument\n");
00094     return -1;
00095   }
00096   referenceEllipse = (GEODESY_enumReferenceEllipse)ellipse;
00097 
00098   fn_choice = atoi(argv[2]);
00099   if( fn_choice < 0 || fn_choice > 5 )
00100   { 
00101     printf("Invalid function argument\n");
00102     return -1;
00103   }
00104 
00105 
00106   // Check the number of required arguments
00107   switch(fn_choice)
00108   {
00109     case 0: if( argc != 3 ){ printf("Invalid function arguments\n"); return -1; } break;
00110     case 1: if( argc != 6 ){ printf("Invalid function arguments\n"); return -1; } break;
00111     case 2: if( argc != 6 ){ printf("Invalid function arguments\n"); return -1; } break;
00112     case 3: if( argc != 9 ){ printf("Invalid function arguments\n"); return -1; } break;
00113     case 4: if( argc != 9 ){ printf("Invalid function arguments\n"); return -1; } break;
00114     case 5: if( argc != 6 ){ printf("Invalid function arguments\n"); return -1; } break;
00115     default: break;
00116   }
00117 
00118   switch(fn_choice)
00119   {
00120     case 0: 
00121     {
00122       double a;      //!< semi-major axis of the reference ellipse                     [m]
00123       double b;      //!< semi-minor axis of the reference ellipse (b = a - a*f_inv)   [m] 
00124       double f_inv;  //!< inverse of the flattening of the reference ellipse           []
00125       double e2;     //!< eccentricity of the reference ellipse (e2 = (a*a-b*b)/(a*a)) [] 
00126 
00127       result = GEODESY_GetReferenceEllipseParameters( referenceEllipse, //!< reference ellipse enumerated    []
00128         &a,      //!< semi-major axis of the reference ellipse                     [m]
00129         &b,      //!< semi-minor axis of the reference ellipse (b = a - a*f_inv)   [m] 
00130         &f_inv,  //!< inverse of the flattening of the reference ellipse           []
00131         &e2      //!< eccentricity of the reference ellipse (e2 = (a*a-b*b)/(a*a)) [] 
00132       );
00133       if( result == FALSE )
00134       {
00135         printf("GEODESY_GetReferenceEllipseParameters returned FALSE\n");
00136         return -1;
00137       }
00138 
00139       printf( "GEODESY_GetReferenceEllipseParameters\n" );
00140       printf( "ellipse = %s\n", GEODESY_REFERENCE_ELLIPSE_STRING_DESCRIPTION[ellipse] );
00141       printf( "a       [m] = %20.3f\n", a );
00142       printf( "b       [m] = %20.9f\n", b );
00143       printf( "f_inv   []  = %20.11f\n", f_inv );
00144       printf( "e2      []  = %.15f\n\n", e2 );
00145       break;
00146     }
00147     case 1: 
00148     {
00149       double latitude;   //!< geodetic latitude                [rad]
00150       double longitude;  //!< geodetic longitude               [rad]
00151       double height;     //!< geodetic height                  [m]
00152       double x;          //!< earth fixed cartesian coordinate [m]
00153       double y;          //!< earth fixed cartesian coordinate [m]
00154       double z;          //!< earth fixed cartesian coordinate [m]
00155 
00156       latitude  = atof( argv[3] )*DEG2RAD;
00157       longitude = atof( argv[4] )*DEG2RAD;
00158       height    = atof( argv[5] );
00159 
00160       result = GEODESY_ConvertGeodeticCurvilinearToEarthFixedCartesianCoordinates(
00161         referenceEllipse,  //!< reference ellipse enumerated []
00162         latitude,   //!< geodetic latitude                [rad]
00163         longitude,  //!< geodetic longitude               [rad]
00164         height,     //!< geodetic height                  [m]
00165         &x,               //!< earth fixed cartesian coordinate [m]
00166         &y,               //!< earth fixed cartesian coordinate [m]
00167         &z                //!< earth fixed cartesian coordinate [m]
00168       );
00169       if( result == FALSE )
00170       {
00171         printf("GEODESY_ConvertGeodeticCurvilinearToEarthFixedCartesianCoordinates returned FALSE\n");
00172         return -1;
00173       }
00174       printf( "GEODESY_ConvertGeodeticCurvilinearToEarthFixedCartesianCoordinates\n" );
00175       printf( "ellipse   = %s\n", GEODESY_REFERENCE_ELLIPSE_STRING_DESCRIPTION[ellipse] );
00176       printf( "latitude  [deg] = %.12lf\n", latitude*RAD2DEG );
00177       printf( "longitude [deg] = %.12lf\n", longitude*RAD2DEG );
00178       printf( "height    [m]   = %.4lf\n", height );
00179       printf( "x         [m]   = %.4lf\n", x );
00180       printf( "y         [m]   = %.4lf\n", y );
00181       printf( "z         [m]   = %.4lf\n\n", z );
00182       break;
00183     }
00184     case 2:
00185     {
00186       double latitude;   //!< geodetic latitude                [rad]
00187       double longitude;  //!< geodetic longitude               [rad]
00188       double height;     //!< geodetic height                  [m]
00189       double x;          //!< earth fixed cartesian coordinate [m]
00190       double y;          //!< earth fixed cartesian coordinate [m]
00191       double z;          //!< earth fixed cartesian coordinate [m]
00192 
00193       x = atof( argv[3] );
00194       y = atof( argv[4] );
00195       z = atof( argv[5] );
00196 
00197       result = GEODESY_ConvertEarthFixedCartesianToGeodeticCurvilinearCoordinates(
00198         referenceEllipse,  //!< reference ellipse enumerated []     
00199         x,              // earth fixed cartesian coordinate [m]
00200         y,              // earth fixed cartesian coordinate [m]
00201         z,              // earth fixed cartesian coordinate [m]  
00202         &latitude,      // geodetic latitude                [rad]
00203         &longitude,     // geodetic longitude               [rad]
00204         &height         // geodetic height                  [m]
00205       );
00206       if( result == FALSE )
00207       {
00208         printf("GEODESY_ConvertEarthFixedCartesianToGeodeticCurvilinearCoordinates returned FALSE\n");
00209         return -1;
00210       }
00211 
00212       printf( "GEODESY_ConvertEarthFixedCartesianToGeodeticCurvilinearCoordinates\n" );
00213       printf( "ellipse   = %s\n", GEODESY_REFERENCE_ELLIPSE_STRING_DESCRIPTION[ellipse] );
00214       printf( "latitude  [deg] = %.12lf\n", latitude*RAD2DEG );
00215       printf( "longitude [deg] = %.12lf\n", longitude*RAD2DEG );
00216       printf( "height    [m]   = %.4lf\n", height );
00217       printf( "x         [m]   = %.4lf\n", x );
00218       printf( "y         [m]   = %.4lf\n", y );
00219       printf( "z         [m]   = %.4lf\n\n", z );
00220       break;
00221     }
00222     case 3:
00223     {
00224       double referenceLatitude;  //!< datum geodetic latitude  [rad]
00225       double referenceLongitude; //!< datum geodetic longitude [rad]
00226       double referenceHeight;    //!< datum geodetic height    [m]
00227       double latitude;           //!< geodetic latitude        [rad]
00228       double longitude;          //!< geodetic longitude       [rad]
00229       double height;             //!< geodetic height          [m]
00230       double northing;           //!< local geodetic northing  [m]
00231       double easting;            //!< local geodetic easting   [m]
00232       double vertical;           //!< local geodetic vertical  [m]
00233       
00234       referenceLatitude = atof(argv[3])*DEG2RAD;
00235       referenceLongitude= atof(argv[4])*DEG2RAD;
00236       referenceHeight   = atof(argv[5]);
00237       
00238       latitude = atof(argv[6])*DEG2RAD;
00239       longitude= atof(argv[7])*DEG2RAD;
00240       height   = atof(argv[8]);
00241   
00242       result = GEODESY_ComputeNorthingEastingVertical(
00243         referenceEllipse,  //!< reference ellipse enumerated []
00244         referenceLatitude,  //!< datum geodetic latitude  [rad]
00245         referenceLongitude, //!< datum geodetic longitude [rad]
00246         referenceHeight,    //!< datum geodetic height    [m]
00247         latitude,           //!< geodetic latitude        [rad]
00248         longitude,          //!< geodetic longitude       [rad]
00249         height,             //!< geodetic height          [m]
00250         &northing,                //!< local geodetic northing  [m]
00251         &easting,                 //!< local geodetic easting   [m]
00252         &vertical                 //!< local geodetic vertical  [m]
00253         );
00254       if( result == FALSE )
00255       {
00256         printf("GEODESY_ComputeNorthingEastingVertical returned FALSE\n");
00257         return -1;
00258       }
00259 
00260       printf( "GEODESY_ComputeNorthingEastingVertical\n" );
00261       printf( "ellipse   = %s\n", GEODESY_REFERENCE_ELLIPSE_STRING_DESCRIPTION[ellipse] );
00262       printf( "reference_latitude  [deg] = %.12lf\n",  referenceLatitude*RAD2DEG );
00263       printf( "reference_longitude [deg] = %.12lf\n",  referenceLongitude*RAD2DEG );
00264       printf( "reference_height    [m]   = %.4lf\n",   referenceHeight );
00265       printf( "latitude            [deg] = %.12lf\n",  latitude*RAD2DEG );
00266       printf( "longitude           [deg] = %.12lf\n",  longitude*RAD2DEG );
00267       printf( "height              [m]   = %.4lf\n",   height );
00268       printf( "northing            [m]   = %.4lf\n",   northing );
00269       printf( "easting             [m]   = %.4lf\n",   easting );
00270       printf( "vertical            [m]   = %.4lf\n\n", vertical );
00271       break;
00272     }
00273     case 4:
00274     {
00275       double elevation=0;  //!< elevation angle [rad]
00276       double azimuth=0;    //!< azimuth angle   [rad]
00277       double fromX = atof( argv[3] );
00278       double fromY = atof( argv[4] );
00279       double fromZ = atof( argv[5] );
00280       double toX = atof( argv[6] );
00281       double toY = atof( argv[7] );
00282       double toZ = atof( argv[8] );
00283 
00284       result = GEODESY_ComputeAzimuthAndElevationAnglesBetweenToPointsInTheEarthFixedFrame(
00285         referenceEllipse,  //!< reference ellipse enumerated []
00286         fromX, //!< earth centered earth fixed vector from point X component [m]
00287         fromY, //!< earth centered earth fixed vector from point Y component [m]
00288         fromZ, //!< earth centered earth fixed vector from point Z component [m]
00289         toX,   //!< earth centered earth fixed vector to point X component   [m]
00290         toY,   //!< earth centered earth fixed vector to point Y component   [m]
00291         toZ,   //!< earth centered earth fixed vector to point Z component   [m]
00292         &elevation,  //!< elevation angle [rad]
00293         &azimuth     //!< azimuth angle   [rad]
00294       );
00295       if( result == FALSE )
00296       {
00297         printf("GEODESY_ComputeAzimuthAndElevationAnglesBetweenToPointsInTheEarthFixedFrame returned FALSE\n");
00298         return -1;
00299       }
00300       printf( "GEODESY_ComputeAzimuthAndElevationAnglesBetweenToPointsInTheEarthFixedFrame\n" );
00301       printf( "ellipse         = %s\n", GEODESY_REFERENCE_ELLIPSE_STRING_DESCRIPTION[ellipse] );
00302       printf( "fromX     [m]   = %.4lf\n",  fromX );
00303       printf( "fromY     [m]   = %.4lf\n",  fromY );
00304       printf( "fromZ     [m]   = %.4lf\n",  fromZ );
00305       printf( "toX       [m]   = %.4lf\n",  toX );
00306       printf( "toY       [m]   = %.4lf\n",  toY );
00307       printf( "toZ       [m]   = %.4lf\n",  toZ );
00308       printf( "azimuth   [deg] = %.12lf\n",  azimuth*RAD2DEG );
00309       printf( "elevation [deg] = %.12lf\n",  elevation*RAD2DEG );
00310 
00311       break;
00312     }    
00313     case 5:
00314     {
00315       double referenceLatitude;  //!< datum geodetic latitude  [rad]
00316       double referenceLongitude; //!< datum geodetic longitude [rad]
00317       double referenceHeight;    //!< datum geodetic height    [m]
00318       double latitude;           //!< geodetic latitude        [rad]
00319       double longitude;          //!< geodetic longitude       [rad]
00320       double height;             //!< geodetic height          [m]
00321       double northing;           //!< local geodetic northing  [m]
00322       double easting;            //!< local geodetic easting   [m]
00323       double vertical;           //!< local geodetic vertical  [m]
00324       
00325       referenceLatitude = atof(argv[3])*DEG2RAD;
00326       referenceLongitude= atof(argv[4])*DEG2RAD;
00327       referenceHeight   = atof(argv[5]);
00328       
00329       latitude = atof(argv[6])*DEG2RAD;
00330       longitude= atof(argv[7])*DEG2RAD;
00331       height   = atof(argv[8]);
00332   
00333       result = GEODESY_ComputeNorthingEastingVertical(
00334         referenceEllipse,   // reference ellipse enumerated []
00335         referenceLatitude,  // datum geodetic latitude  [rad]
00336         referenceLongitude, // datum geodetic longitude [rad]
00337         referenceHeight,    // datum geodetic height    [m]
00338         latitude,           // geodetic latitude        [rad]
00339         longitude,          // geodetic longitude       [rad]
00340         height,             // geodetic height          [m]
00341         &northing,          // local geodetic northing  [m]
00342         &easting,           // local geodetic easting   [m]
00343         &vertical           // local geodetic vertical  [m]
00344         );
00345       if( result == FALSE )
00346       {
00347         printf("GEODESY_ComputeNorthingEastingVertical returned FALSE\n");
00348         return -1;
00349       }
00350 
00351       printf( "GEODESY_ComputePositionDifference\n" );
00352       printf( "ellipse   = %s\n", GEODESY_REFERENCE_ELLIPSE_STRING_DESCRIPTION[ellipse] );
00353       printf( "reference_latitude  [deg] = %.12lf\n",  referenceLatitude*RAD2DEG );
00354       printf( "reference_longitude [deg] = %.12lf\n",  referenceLongitude*RAD2DEG );
00355       printf( "reference_height    [m]   = %.4lf\n",   referenceHeight );
00356       printf( "latitude            [deg] = %.12lf\n",  latitude*RAD2DEG );
00357       printf( "longitude           [deg] = %.12lf\n",  longitude*RAD2DEG );
00358       printf( "height              [m]   = %.4lf\n",   height );
00359       printf( "northing            [m]   = %.4lf\n",   northing );
00360       printf( "easting             [m]   = %.4lf\n",   easting );
00361       printf( "vertical            [m]   = %.4lf\n\n", vertical );
00362       break;
00363     }
00364     default:
00365     {
00366       break;
00367     }
00368   }
00369 
00370   return 0;
00371 }