home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / gmt_os2.zip / src / project.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-24  |  23.5 KB  |  733 lines

  1. /*--------------------------------------------------------------------
  2.  *    The GMT-system:    @(#)project.c    2.10  24 Jul 1995
  3.  *
  4.  *    Copyright (c) 1991-1995 by P. Wessel and W. H. F. Smith
  5.  *    See README file for copying and redistribution conditions.
  6.  *--------------------------------------------------------------------*/
  7. /*
  8.  * project.c
  9.  * reads (x,y,[z]) data and writes some combination of (x,y,z,p,q,u,v),
  10.  * where p,q is the distance along,across the track of the projection of (x,y),
  11.  * and u,v are the un-transformed (x,y) coordinates of the projected position.
  12.  * Can also create (x,y) along track.
  13.   
  14.    Author:     Walter H. F. Smith
  15.    Date:    19 April, 1988.
  16.    Modified:    4 December 1988, to be more flexible.
  17.    Complete rebuild 22 June, 1989 to use vector products and do more things.
  18.    version 2.0
  19.  
  20. */
  21.  
  22. #include "gmt.h"
  23.  
  24. int    compare_distances();
  25. int    do_flat_earth();
  26. int    solve_right_spherical_triangle();
  27. int    sphere_azim_dist();
  28. void    oblique_setup();
  29. void    oblique_transform();
  30. void    make_euler_matrix();
  31. void    matrix_3v();
  32. void    matrix_2v();
  33. void    sphere_project_setup();
  34. void    flat_project_setup();
  35.  
  36. struct DATA {
  37.     double    a[7];
  38. }    *p_data;
  39.  
  40. int    output_choice[7];
  41.  
  42. main (argc, argv)
  43. int argc;
  44. char **argv;
  45. {    
  46.     int    i, j, k, n_definitions, n_outputs, n_used, n_read, ix, iy, n_alloc = GMT_CHUNK;
  47.     int    nc, ne, np, nl, nw;
  48.  
  49.     double    xx, yy, zz, x_a, y_a, x_b, y_b, x_p, y_p, d_to_km, cos_theta, sin_theta;
  50.     double    theta, d_inc = 0.0, d_along, xy[2];
  51.     
  52.     double    azimuth = 0.0, l_min = 0.0, l_max = 0.0, w_min = 0.0, w_max = 0.0;
  53.     double    a[3], b[3], x[3], xt[3], pole[3], center[3], e[9];
  54.  
  55.     BOOLEAN    check_length, check_width, convert_units, dateline, error, find_new_point, flat_earth;
  56.     BOOLEAN generate, greenwich, origin_set, pole_set, rads, sort_output, stay_within, two_points;
  57.     
  58.     FILE *fp = NULL;
  59.     
  60.     char    modifier, record_str[100], heading[7][100], format1[20], format2[20], txt_a[32], txt_b[32];
  61.     
  62.     argc = gmt_begin (argc, argv);
  63.  
  64.     check_length = check_width = dateline = error = find_new_point = flat_earth = generate = greenwich = FALSE;
  65.     pole_set = sort_output = stay_within = two_points = convert_units = FALSE;
  66.     rads = TRUE;
  67.     x_a = x_b = x_p = y_a = y_b = y_p = 0.0;
  68.     n_definitions = 0;
  69.     n_outputs = 0;
  70.     j = 1;
  71.     for (i = 0; i < 7; i++) output_choice[i] = 0;
  72.     sprintf (format1, "%s\t\0", gmtdefs.d_format);
  73.     sprintf (format2, "%s\n\0", gmtdefs.d_format);
  74.  
  75.     for (i = 1; i < argc; i++) {
  76.         if (argv[i][0] == '-') {
  77.             switch (argv[i][1]) {
  78.             
  79.         
  80.                 /* Common parameters */
  81.             
  82.                 case 'H':
  83.                 case 'V':
  84.                 case ':':
  85.                 case '\0':
  86.                     error += get_common_args (argv[i], 0, 0, 0, 0);
  87.                     break;
  88.                 
  89.                 /* Supplemental parameters */
  90.                 
  91.                 case 'F':
  92.                     for (j = 2, k = 0; argv[i][j]; j++, k++) {
  93.                         switch (argv[i][j]) {
  94.                             case 'x':
  95.                                 output_choice[k] = 0;
  96.                                 break;
  97.                             case 'y':
  98.                                 output_choice[k] = 1;
  99.                                 break;
  100.                             case 'z':
  101.                                 output_choice[k] = 2;
  102.                                 break;
  103.                             case 'p':
  104.                                 output_choice[k] = 3;
  105.                                 break;
  106.                             case 'q':
  107.                                 output_choice[k] = 4;
  108.                                 break;
  109.                             case 'r':
  110.                                 output_choice[k] = 5;
  111.                                 find_new_point = TRUE;
  112.                                 break;
  113.                             case 's':
  114.                                 output_choice[k] = 6;
  115.                                 find_new_point = TRUE;
  116.                                 break;
  117.                             default:
  118.                                 fprintf (stderr, "%s: GMT SYNTAX ERROR -F option:  Unrecognized choice %c\n", gmt_program, argv[i][j]);
  119.                                 error = TRUE;
  120.                         }
  121.                         n_outputs++;
  122.                     }
  123.                     break;
  124.                 case 'A':
  125.                     azimuth = atof(&argv[i][2]);
  126.                     n_definitions++;
  127.                     break;
  128.                 case 'C':
  129.                     nc = sscanf(&argv[i][2], "%[^/]/%s", txt_a, txt_b);
  130.                     x_a = ddmmss_to_degree (txt_a);
  131.                     y_a = ddmmss_to_degree (txt_b);
  132.                     origin_set = TRUE;
  133.                     break;
  134.                 case 'D':
  135.                     modifier = argv[i][2];
  136.                     if (modifier == 'D' || modifier == 'd') {
  137.                         dateline = TRUE;
  138.                     }
  139.                     else if (modifier == 'g' || modifier == 'G') {
  140.                         greenwich = TRUE;
  141.                     }
  142.                     else if (modifier) {
  143.                         fprintf (stderr, "%s: GMT SYNTAX ERROR -D option:  Unrecognized modifier %c\n", gmt_program, modifier);
  144.                         error = TRUE;
  145.                     }
  146.                     break;
  147.                 case 'E':
  148.                     ne = sscanf(&argv[i][2], "%[^/]/%s", txt_a, txt_b);
  149.                     x_b = ddmmss_to_degree (txt_a);
  150.                     y_b = ddmmss_to_degree (txt_b);
  151.                     two_points = TRUE;
  152.                     n_definitions++;
  153.                     break;
  154.                 case 'G':
  155.                     generate = TRUE;
  156.                     d_inc = atof(&argv[i][2]);
  157.                     break;
  158.                 case 'L':
  159.                     check_length = TRUE;
  160.                     modifier = argv[i][2]; 
  161.                     if (modifier == 'W' || modifier == 'w') {
  162.                         stay_within = TRUE;
  163.                     }
  164.                     else {
  165.                         nl = sscanf(&argv[i][2], "%lf/%lf", &l_min, &l_max);
  166.                     }
  167.                     break;
  168.                 case 'M':
  169.                     convert_units = TRUE;
  170.                     break;
  171.                 case 'N':
  172.                     flat_earth = TRUE;
  173.                     break;
  174.                 case 'S':
  175.                     sort_output = TRUE;
  176.                     break;
  177.                 case 'T':
  178.                     np = sscanf(&argv[i][2], "%[^/]/%s", txt_a, txt_b);
  179.                     x_p = ddmmss_to_degree (txt_a);
  180.                     y_p = ddmmss_to_degree (txt_b);
  181.                     pole_set = TRUE;
  182.                     n_definitions++;
  183.                     break;
  184.                 case 'W':
  185.                     nw = sscanf(&argv[i][2], "%lf/%lf", &w_min, &w_max);
  186.                     check_width = TRUE;
  187.                     break;
  188.                 default:
  189.                     error = TRUE;
  190.                     gmt_default_error (argv[i][1]);
  191.                     break;
  192.             }
  193.         }
  194.         else {
  195.             if ((fp = fopen(argv[i],"r")) == NULL) {
  196.                 fprintf (stderr, "Cannot open file %s\n", argv[i]);
  197.                 exit(-1);
  198.             }
  199.         }
  200.     }
  201.     
  202.     if (argc == 1 || gmt_quick) {
  203.         fprintf (stderr, "project %s - project data onto line or great circle, generate track, or translate coordiantes\n\n", GMT_VERSION);
  204.         fprintf(stderr,"usage:    project [file] -C<ox>/<oy> -F<flags> [-A<azimuth>] [-D<d_or_g>]\n");
  205.         fprintf(stderr,"\t[-E<bx>/<by>] [-G<dist>] [-H] [-L[w][<l_min>/<l_max>]] [-M] [-N] [-S]\n");
  206.         fprintf(stderr,"\t[-T<px>/<py>] [-V] [-W<w_min>/<w_max>] [-:]\n\n");
  207.         
  208.         if (gmt_quick) exit (-1);
  209.  
  210.         fprintf(stderr,"\tproject will read stdin or file, and does not want input if -G option.\n");
  211.         fprintf(stderr,"\tThe projection may be defined in (only) one of three ways:\n");
  212.         fprintf(stderr,"\t  (1) by a center -C and an azimuth -A,\n");
  213.         fprintf(stderr,"\t  (2) by a center -C and end point of the path -E,\n");
  214.         fprintf(stderr,"\t  (3) by a center -C and a roTation pole position -T.\n");
  215.         fprintf(stderr,"\t  In a spherical projection [default], all cases place the central meridian\n");
  216.         fprintf(stderr,"\t  of the transformed coordinates (p,q) through -C (p = 0 at -C).  The equator\n");
  217.         fprintf(stderr,"\t  of the (p,q) system (line q = 0) passes through -C and makes an angle\n");
  218.         fprintf(stderr,"\t  <azimuth> with North (case 1), or passes through -E (case 2), or is\n");
  219.         fprintf(stderr,"\t  determined by the pole -T (case 3).  In (3), point -C need not be on equator.\n");
  220.         fprintf(stderr,"\t  In a cartesian [-F option] projection, p = q = 0 at -O in all cases;\n");
  221.         fprintf(stderr,"\t  (1) and (2) orient the p axis, while (3) orients the q axis.\n\n");
  222.         fprintf(stderr,"\t-flags: Indicate what output you want as one or more of xyzpqrs in any order;\n");
  223.         fprintf(stderr,"\t  where x,y,[z] refer to input data locations and optional values,\n");
  224.         fprintf(stderr,"\t  p,q are the coordinates of x,y in the projection's coordinate system,\n");
  225.         fprintf(stderr,"\t  r,s is the projected position of x,y (taking q = 0) in the (x,y) coordinate system.\n");
  226.         fprintf(stderr,"\t  p,q may be scaled from degrees into kilometers by the -M option.  See -L, -M, -W\n\n");
  227.         fprintf(stderr,"\t-C<ox>/<oy> sets the location of the center.\n");
  228.         fprintf (stderr, "\n\tOPTIONS:\n");
  229.         fprintf(stderr,"\t-A<azimuth> sets the option (1) Azimuth, (degrees CW from North).\n");
  230.         fprintf(stderr,"\t-D will force the location of the Discontinuity in the r coordinate;\n");
  231.         fprintf(stderr,"\t  -Dd (dateline) means [-180 < r < 180], -Dg (greenwich) means [0 < r < 360].\n");
  232.         fprintf(stderr,"\t  The default does not check; in spherical case this usually results in [-180,180].\n");
  233.         fprintf(stderr,"\t-E<bx>/<by> sets the option (2) location of end point E.\n");
  234.         fprintf(stderr,"\t-G means Generate (r,s,p) points along profile every <dist> units. (No input data used.)\n");
  235.         fprintf(stderr,"\t   If E given, will generate from A to E; else must give -L<l_min>/<l_max> for length.\n");
  236.         explain_option ('H');
  237.         fprintf(stderr,"\t-L Check the Length along the projected track and use only certain points.\n");
  238.         fprintf(stderr,"\t  -Lw will use only those points Within the span from A to E (Must have set -E).\n");
  239.         fprintf(stderr,"\t  -L<l_min>/<l_max> will only use points whose p is [l_min <= p <= l_max].\n");
  240.         fprintf(stderr,"\t  Default uses all points.  Note p = 0 at A and increases toward E in azim direction.\n");
  241.         fprintf(stderr,"\t-M means convert to Map units, so x,y,r,s are degrees,\n");
  242.         fprintf(stderr,"\t  while p,q,dist,l_min,l_max,w_min,w_max are km.\n");
  243.         fprintf(stderr,"\t  If not set, then p,q,dist,l_min,l_max,w_min,w_max are assumed to be in same units as x,y,r,s.\n");
  244.         fprintf(stderr,"\t-N means Flat_earth; a cartesian projection is made.  Default is spherical.\n");
  245.         fprintf(stderr,"\t-S means the output should be Sorted into increasing p value.\n");
  246.         fprintf(stderr,"\t-T<px>/<py> sets the option (3) location of the roTation pole to the projection.\n");
  247.         explain_option ('V');
  248.         fprintf(stderr,"\t-W Check the width across the projected track and use only certain points.\n");
  249.         fprintf(stderr,"\t  This will use only those points whose q is [w_min <= q <= w_max].\n");
  250.         fprintf(stderr,"\t  Note that q is positive to your LEFT as you walk from A toward B in azim direction.\n");
  251.         explain_option (':');
  252.         fprintf(stderr,"\tWarning:  project is CASE SENSITIVE.  Enter flags in lower case, other options in UPPER CASE.\n");
  253.         exit(-1);
  254.     }
  255.     
  256.     if ( !(origin_set && nc == 2) ) {
  257.         fprintf (stderr, "%s: GMT SYNTAX ERROR -C option.  Correct syntax: -C<lon0>/<lat0>\n", gmt_program);
  258.         error++;
  259.     }
  260.     if (two_points && ne != 2) {
  261.         fprintf (stderr, "%s: GMT SYNTAX ERROR -E option.  Correct syntax: -E<lon1>/<lat1>\n", gmt_program);
  262.         error++;
  263.     }
  264.     if (pole_set && np != 2) {
  265.         fprintf (stderr, "%s: GMT SYNTAX ERROR -T option.  Correct syntax: -T<lonp>/<latp>\n", gmt_program);
  266.         error++;
  267.     }
  268.     if (check_length && !stay_within && nl != 2) {
  269.         fprintf (stderr, "%s: GMT SYNTAX ERROR -L option.  Correct syntax: -L[w | <min>/<max>]\n", gmt_program);
  270.         error++;
  271.     }
  272.     if (check_width && (nw != 2 || w_min >= w_max)) {
  273.         fprintf (stderr, "%s: GMT SYNTAX ERROR -L option.  Correct syntax: -L[w | <min>/<max>]\n", gmt_program);
  274.         error++;
  275.     }
  276.     if (azimuth < 0.0 || azimuth >= 360.0) {
  277.         fprintf (stderr, "%s: GMT SYNTAX ERROR -A option.  Must specify azimuth in 0-360 degree range\n", gmt_program);
  278.         error++;
  279.     }
  280.     if ( n_definitions != 1) {
  281.         fprintf (stderr, "%s: GMT SYNTAX ERROR: Specify only one of -A, -E, and -T\n", gmt_program);
  282.         error++;
  283.     }
  284.     if ( two_points && (x_a == x_b) && (y_a == y_b) ) {
  285.         fprintf (stderr, "%s: GMT SYNTAX ERROR -E option: Second point must differ from origin!\n", gmt_program);
  286.         error++;
  287.     }
  288.     if ( generate && l_min == l_max && !(two_points)) {    /* We don't know how long to generate  */
  289.         fprintf (stderr, "%s: GMT SYNTAX ERROR -G option: Must also specify -Lmin/max or use -E instead\n", gmt_program);
  290.         error++;
  291.     }
  292.     if ( generate && d_inc <= 0.0) {    /* No increment given  */
  293.         fprintf (stderr, "%s: GMT SYNTAX ERROR -G option: Must specify a positive increment\n", gmt_program);
  294.         error++;
  295.     }
  296.     if (stay_within && !(two_points) ) {    /* Same problem.  */
  297.         fprintf (stderr, "%s: GMT SYNTAX ERROR -L option: Must specify -Lmin/max or use -E instead\n", gmt_program);
  298.         error++;
  299.     }
  300.     if (n_outputs > 7) {
  301.         fprintf (stderr, "%s: GMT SYNTAX ERROR -F option: Too many output columns selected (%d)\n", gmt_program, n_outputs);
  302.         error++;
  303.     }
  304.     if (n_outputs <= 0 && !(generate) ) {
  305.         fprintf (stderr, "%s: GMT SYNTAX ERROR: Must specify desired output with -F\n", gmt_program);
  306.         error++;
  307.     }
  308.  
  309.     if (error) exit (-1);
  310.  
  311.     
  312.     p_data = (struct DATA *) memory (CNULL, n_alloc, sizeof (struct DATA), "project");
  313.     
  314.     d_to_km = 0.001 * 2.0 * M_PI * gmtdefs.ellipse[N_ELLIPSOIDS-1].eq_radius / 360.0;
  315.  
  316.     if (generate && two_points && (l_min == l_max) ) stay_within = TRUE;    /* Default generate from A to B  */
  317.  
  318.     /* Set up rotation matrix e for flat earth, or pole and center for spherical; get l_min, l_max if stay_within  */
  319.  
  320.     if (flat_earth) {
  321.         flat_project_setup(y_a, x_a, y_b, x_b, y_p, x_p, &azimuth, e, two_points, pole_set);
  322.         /* Azimuth is now changed to cartesian theta in radians */
  323.         if (stay_within) {
  324.             l_min = 0.0;
  325.             xx = x_b - x_a;
  326.             yy = y_b - y_a;
  327.             l_max = d_sqrt(xx*xx + yy*yy);
  328.             if (convert_units) l_max *= d_to_km;
  329.         }
  330.     }
  331.     else {
  332.         if (pole_set) {
  333.             oblique_setup(y_p, x_p, pole, y_a, x_a, center, pole_set, rads);
  334.         }
  335.         else {
  336.             sphere_project_setup(y_a, x_a, a, y_b, x_b, b, &azimuth, pole, center, two_points, rads);
  337.         }
  338.         /* Azimuth is now changed to radians  */
  339.         if (stay_within) {
  340.             l_min = 0.0;
  341.             l_max = dot3v(a,b);
  342.             l_max = d_acos(l_max) * R2D;
  343.             if (convert_units) l_max *= d_to_km;
  344.         }
  345.     }
  346.  
  347.     /* Now things are initialized.  We will work in degrees for awhile, so we convert things:  */
  348.  
  349.     if (convert_units) {
  350.         d_inc /= d_to_km;
  351.         l_min /= d_to_km;
  352.         l_max /= d_to_km;
  353.         w_min /= d_to_km;
  354.         w_max /= d_to_km;
  355.     }
  356.  
  357.     /*  Now we are ready to work  */
  358.  
  359.     n_used = 0;
  360.     n_read = 0;
  361.  
  362.     if (generate) {
  363.         n_outputs = 3;
  364.         output_choice[0] = 5;
  365.         output_choice[1] = 6;
  366.         output_choice[2] = 3;
  367.         d_along = l_min;
  368.         while (d_along < l_max) {
  369.             p_data[n_used].a[3] = d_along;
  370.             n_used++;
  371.             d_along = l_min + n_used * d_inc;
  372.             if (n_used == (n_alloc-1)) {
  373.                 n_alloc += GMT_CHUNK;
  374.                 p_data = (struct DATA *) memory ((char *)p_data, n_alloc, sizeof (struct DATA), "project");
  375.             }
  376.         }
  377.         p_data[n_used].a[3] = l_max;
  378.         n_used ++;
  379.         find_new_point = TRUE;
  380.     }
  381.     
  382.     else {
  383.         if (fp == NULL) fp = stdin;
  384.  
  385.         if (gmtdefs.io_header) {
  386.             fgets(record_str, 100, fp);
  387.             sscanf(record_str, "%s %s %s", heading[0], heading[1], heading[2]);
  388.             if (! (heading[2]) ) strcpy(heading[2],"z");
  389.             strcpy(heading[3],"p");
  390.             strcpy(heading[4],"q");
  391.             strcpy(heading[5],"r");
  392.             strcpy(heading[6],"s");
  393.             for (i = 1; i < gmtdefs.n_header_recs; i++) fgets(record_str, 100, fp);
  394.         }
  395.  
  396.         zz = 0.0;
  397.         ix = (gmtdefs.xy_toggle) ? 1 : 0;    iy = 1 - ix;        /* Set up which columns have x and y */
  398.         while ( (fgets(record_str, 100, fp) != NULL) ) {
  399.             sscanf (record_str, "%lf %lf %lf", &xy[ix], &xy[iy], &zz);
  400.             xx = xy[0];    yy = xy[1];
  401.     
  402.             n_read ++;
  403.             
  404.             if (flat_earth) {
  405.                 x[0] = xx - x_a;
  406.                 x[1] = yy - y_a;
  407.                 matrix_2v (e,x,xt);
  408.             }
  409.             else {
  410.                 oblique_transform(yy, xx, &xt[1], &xt[0], pole, center, rads);
  411.             }
  412.             if(check_length) {
  413.                 if (xt[0] < l_min) continue;
  414.                 if (xt[0] > l_max) continue;
  415.             }
  416.             if (check_width) {
  417.                 if (xt[1] < w_min) continue;
  418.                 if (xt[1] > w_max) continue;
  419.             }
  420.             p_data[n_used].a[0] = xx;
  421.             p_data[n_used].a[1] = yy;
  422.             p_data[n_used].a[2] = zz;
  423.             p_data[n_used].a[3] = xt[0];
  424.             p_data[n_used].a[4] = xt[1];
  425.             n_used++;
  426.             if (n_used == n_alloc) {
  427.                 n_alloc += GMT_CHUNK;
  428.                 p_data = (struct DATA *) memory ((char *)p_data, n_alloc, sizeof (struct DATA), "project");
  429.             }
  430.         }
  431.             
  432.  
  433.         if (fp != stdin) fclose(fp);
  434.     
  435.         if (sort_output) qsort((char *)p_data, n_used, sizeof (struct DATA), compare_distances);
  436.     }
  437.  
  438. /*    Get here when all data are loaded with p,q and p is in increasing order if desired.  */
  439.  
  440.     if (find_new_point) {    /* We need to find r,s  */
  441.     
  442.         if (flat_earth) {
  443.             cos_theta = cos(azimuth);
  444.             sin_theta = sin(azimuth);
  445.             for (i = 0; i < n_used; i++) {
  446.                 p_data[i].a[5] = x_a + p_data[i].a[3] * cos_theta;
  447.                 p_data[i].a[6] = y_a + p_data[i].a[3] * sin_theta;
  448.                 while (greenwich && p_data[i].a[5] < 0.0) p_data[i].a[5] += 360.0;
  449.                 while (dateline && p_data[i].a[5] > 180.0) p_data[i].a[5] -= 360.0;
  450.             }
  451.         }
  452.         else {
  453.             xx = x_a;
  454.             yy = y_a;
  455.             geo_to_cart(&yy, &xx, x, rads);
  456.             for (i = 0; i < n_used; i++) {
  457.                 theta = p_data[i].a[3];
  458.                 make_euler_matrix(pole, e, &theta, rads);
  459.                 matrix_3v(e,x,xt);
  460.                 cart_to_geo(&yy, &xx, xt, rads);
  461.                 p_data[i].a[5] = xx;
  462.                 p_data[i].a[6] = yy;
  463.                 while (greenwich && p_data[i].a[5] < 0.0) p_data[i].a[5] += 360.0;
  464.                 while (dateline && p_data[i].a[5] > 180.0) p_data[i].a[5] -= 360.0;
  465.             }
  466.         }
  467.     }
  468.     
  469. /* At this stage, all values are still in degrees.  */
  470.  
  471.     if (convert_units) {
  472.         for (i = 0; i < n_used; i++) {
  473.             p_data[i].a[3] *= d_to_km;
  474.             p_data[i].a[4] *= d_to_km;
  475.         }
  476.     }
  477.  
  478. /* Now output  */
  479.  
  480.     if (gmtdefs.io_header) {
  481.         if (generate) {
  482.             printf("lon\tlat\tdist\n");
  483.         }
  484.         else {
  485.             for (j = 0; j < n_outputs-1; j++) {
  486.                 printf("%s\t", heading[output_choice[j]]);
  487.             }
  488.             printf("%s\n",heading[output_choice[j]]);
  489.         }
  490.     }
  491.     
  492.     for (i = 0; i < n_used; i++) {
  493.         for (j = 0; j < n_outputs-1; j++) printf(format1, p_data[i].a[output_choice[j]]);
  494.         printf(format2, p_data[i].a[output_choice[j]]);
  495.     }
  496.  
  497.     if (gmtdefs.verbose) fprintf(stderr, "N read, N used: %d %d\n", n_read, n_used);
  498.     
  499.     free ((char *)p_data);
  500.     
  501.     gmt_end (argc, argv);
  502. }
  503.  
  504. int    compare_distances(point_1, point_2)
  505.  
  506.     struct DATA    *point_1, *point_2;
  507. {
  508.     double    d_1, d_2;
  509.  
  510.     d_1 = point_1->a[3];
  511.     d_2 = point_2->a[3];
  512.  
  513.     if (d_1 < d_2)
  514.         return (-1);
  515.     if (d_1 > d_2)
  516.         return (1);
  517.     else
  518.         return (0);
  519. }
  520.  
  521. void    oblique_setup(plat, plon, p, clat, clon, c, c_given, rads)
  522. double    plat, plon, *p, clat, clon, *c;
  523. int    c_given, rads;
  524. {
  525.     /* routine sets up a unit 3-vector p, the pole of an 
  526.        oblique projection, given plat, plon, the position 
  527.        of this pole in the usual coordinate frame.
  528.        c_given = TRUE means that clat, clon are to be used
  529.        as the usual coordinates of a point through which the
  530.        user wants the central meridian of the oblique
  531.        projection to go.  If such a point is not given, then
  532.        the central meridian will go through p and the usual
  533.        N pole.  In either case, a unit 3-vector c is created
  534.        which is the directed normal to the plane of the central
  535.        meridian, pointing in the positive normal (east) sense.
  536.        rads = TRUE if we need to convert plat, plon, clat, clon
  537.        from degrees to radians.  */
  538.     
  539.     double    s[3];  /* s points to the south pole  */
  540.  
  541.     s[0] = s[1] = 0.0;
  542.     s[2] = -1.0;
  543.  
  544.     geo_to_cart(&plat, &plon, p, rads);
  545.     
  546.     if (c_given) {    /* s points to user's clat, clon  */
  547.         geo_to_cart(&clat, &clon, s, rads);
  548.     }
  549.     cross3v(p, s, c);
  550.     normalize3v(c);
  551. }
  552.  
  553. void    oblique_transform(xlat, xlon, x_t_lat, x_t_lon, p, c, rads)
  554. double    xlat, xlon, *x_t_lat, *x_t_lon, *p, *c;
  555. int    rads;
  556. {
  557.     /* routine takes the point x at conventional (xlat, xlon) and
  558.        computes the transformed coordinates (x_t_lat, x_t_lon) in
  559.        an oblique reference frame specified by the unit 3-vectors
  560.        p (the pole) and c (the directed normal to the oblique
  561.        central meridian).  p and c have been computed earlier by
  562.        the routine oblique_setup().  rads = TRUE if lats and lons
  563.        are in degrees.  */
  564.  
  565.     double    x[3], p_cross_x[3], temp1, temp2;
  566.     
  567.     geo_to_cart(&xlat, &xlon, x, rads);
  568.  
  569.     temp1 = dot3v(x,p);
  570.     *x_t_lat = d_asin(temp1);
  571.  
  572.     cross3v(p,x,p_cross_x);
  573.     normalize3v(p_cross_x);
  574.     
  575.     temp1 = dot3v(p_cross_x, c);
  576.     temp2 = dot3v(x, c);
  577.     *x_t_lon = copysign( d_acos(temp1), temp2);
  578.  
  579.     if (rads) {
  580.         *x_t_lat *= R2D;
  581.         *x_t_lon *= R2D;
  582.     }
  583. }
  584.  
  585. void    make_euler_matrix(p, e, theta, rads)
  586. double    *p, *e, *theta;
  587. int    rads;
  588. {
  589.     /* Routine to fill an euler matrix e with the elements
  590.        needed to rotate a 3-vector about the pole p through
  591.        an angle theta.  p is a unit 3-vector.  If rads = TRUE,
  592.        we have to convert theta from degrees into radians before
  593.        we use it.  */
  594.     
  595.     double    cos_theta, sin_theta, one_minus_cos_theta;
  596.     double    pxsin, pysin, pzsin, temp;
  597.     
  598.     if (rads) {
  599.         *theta *= D2R;
  600.     }
  601.     cos_theta = cos(*theta);
  602.     sin_theta = sin(*theta);
  603.     one_minus_cos_theta = 1.0 - cos_theta;
  604.     
  605.     pxsin = p[0] * sin_theta;
  606.     pysin = p[1] * sin_theta;
  607.     pzsin = p[2] * sin_theta;
  608.     
  609.     temp = p[0] * one_minus_cos_theta;
  610.     e[0] = temp * p[0] + cos_theta;
  611.     e[1] = temp * p[1] - pzsin;
  612.     e[2] = temp * p[2] + pysin;
  613.     
  614.     temp = p[1] * one_minus_cos_theta;
  615.     e[3] = temp * p[0] + pzsin;
  616.     e[4] = temp * p[1] + cos_theta;
  617.     e[5] = temp * p[2] - pxsin;
  618.  
  619.     temp = p[2] * one_minus_cos_theta;
  620.     e[6] = temp * p[0] - pysin;
  621.     e[7] = temp * p[1] + pxsin;
  622.     e[8] = temp * p[2] + cos_theta;
  623. }
  624.  
  625. void    matrix_3v(a,x,b)
  626. double    *a, *x, *b;
  627. {
  628.     /* routine to find b, where Ax = b, A is a 3 by 3 square matrix,
  629.        and x and b are 3-vectors.  A is stored row wise, that is:
  630.        
  631.        A = { a11, a12, a13, a21, a22, a23, a31, a32, a33 }  */
  632.     
  633.     b[0] = x[0]*a[0] + x[1]*a[1] + x[2]*a[2];
  634.     b[1] = x[0]*a[3] + x[1]*a[4] + x[2]*a[5];
  635.     b[2] = x[0]*a[6] + x[1]*a[7] + x[2]*a[8];
  636. }
  637.  
  638. void    matrix_2v(a,x,b)
  639. double    *a, *x, *b;
  640. {
  641.     /* routine to find b, where Ax = b, A is a 2 by 2 square matrix,
  642.        and x and b are 2-vectors.  A is stored row wise, that is:
  643.        
  644.        A = { a11, a12, a21, a22 }  */
  645.     
  646.     b[0] = x[0]*a[0] + x[1]*a[1];
  647.     b[1] = x[0]*a[2] + x[1]*a[3];
  648. }
  649.  
  650. void    sphere_project_setup(alat, alon, a, blat, blon, b, azim, p, c, two_pts, rads)
  651. double    alat, alon, *a, blat, blon, *b, *azim, *p, *c;
  652. int    two_pts, rads;
  653. {
  654.     /* routine to initialize a pole vector, p, and a central meridian 
  655.        normal vector, c, for use in projecting points onto a great circle.
  656.        
  657.        The great circle is specified in either one of two ways:
  658.        if (two_pts), then the user has given two points, a and b,
  659.        which specify the great circle (directed from a to b);
  660.        if !(two_pts), then the user has given one point, a, and an azimuth,
  661.        azim, clockwise from north, which defines the projection.
  662.  
  663.        The strategy is to use the oblique_transform operations above,
  664.        in such a way that the great circle of the projection is the
  665.        equator of an oblique transform, and the central meridian goes
  666.        through a.  Then the transformed longitude gives the distance
  667.        along the projection circle, and the transformed latitude gives
  668.        the distance normal to the projection circle.
  669.  
  670.        If (two_pts), then p = normalized(a X b).  If not, we temporarily
  671.        create p_temp = normalized(a X n), where n is the north pole.
  672.        p_temp is then rotated about a through the angle azim to give p.
  673.        After p is found, then c = normalized(p X a).
  674.     */
  675.  
  676.     double    e[9];    /* Euler roatation matrix, if needed  */
  677.     double neg_azim;
  678.  
  679.     /* First find p vector  */
  680.  
  681.     if (two_pts) {
  682.         geo_to_cart(&alat, &alon, a, rads);
  683.         geo_to_cart(&blat, &blon, b, rads);
  684.         cross3v(a, b, p);
  685.         normalize3v(p);
  686.     }
  687.     else {
  688.         geo_to_cart(&alat, &alon, a, rads);
  689.         b[0] = b[1] = 0.0;    /* set b to north pole  */
  690.         b[2] = 1.0;
  691.         cross3v(a, b, c);    /* use c for p_temp  */
  692.         normalize3v(c);
  693. /*        make_euler_matrix(a, e, azim, rads);    */
  694.         neg_azim = -(*azim);
  695.         make_euler_matrix(a, e, &neg_azim, rads);
  696.         if (rads) *azim *= D2R;
  697.         matrix_3v(e, c, p);    /* c (p_temp) rotates to p  */
  698.     }
  699.  
  700.     /* Now set c vector  */
  701.     
  702.     cross3v(p, a, c);
  703.     normalize3v(c);
  704. }
  705.  
  706. void    flat_project_setup(alat, alon, blat, blon, plat, plon, azim, e, two_pts, pole_set)
  707. double    alat, alon, blat, blon, plat, plon, *azim, *e;
  708. int    two_pts;
  709. BOOLEAN pole_set;
  710.  
  711. {
  712.     /* Sets up stuff for rotation of cartesian 2-vectors, analogous
  713.        to the spherical three vector stuff above.  Also change azim
  714.        to the cartesian theta, counterclockwise from the x axis.  */        
  715.  
  716.     if (two_pts) {
  717.         *azim = d_atan2((blat - alat), (blon - alon));
  718.     }
  719.     else if (pole_set) {
  720.         *azim = d_atan2((plat - alat), (plon - alon)) - 0.5 * M_PI;
  721.     }
  722.     else {
  723.         *azim = D2R * (90.0 - *azim);
  724.     }
  725.     
  726.     e[0] = e[3] = cos(*azim);
  727.     e[1] = sin(*azim);
  728.     e[3] = -e[1];
  729. }
  730.  
  731.  
  732.  
  733.