source: SH_SHM/trunk/util/find_next_loc.c @ 92

Revision 16, 3.5 KB checked in by marcus, 15 years ago (diff)

r1 | svn | 2007-12-13 11:10:29 +0100 (Do, 13 Dez 2007) | 2 lines

Initial import

Line 
1
2/* file find_next_loc.c
3 *      ===============
4 *
5 * version 1, 24-Jun-2003
6 *
7 * Finds next location to a given location out of a list
8 * K. Stammler, 24-Jun-2003
9 *
10 */
11
12
13/*
14 *
15 *  SeismicHandler, seismic analysis software
16 *  Copyright (C) 1996,  Klaus Stammler, Federal Institute for Geosciences
17 *                                       and Natural Resources (BGR), Germany
18 *
19 *  This program is free software; you can redistribute it and/or modify
20 *  it under the terms of the GNU General Public License as published by
21 *  the Free Software Foundation; either version 2 of the License, or
22 *  (at your option) any later version.
23 *
24 *  This program is distributed in the hope that it will be useful,
25 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
26 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 *  GNU General Public License for more details.
28 *
29 *  You should have received a copy of the GNU General Public License
30 *  along with this program; if not, write to the Free Software
31 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
32 *
33 */
34
35
36#include <stdio.h>
37#include <string.h>
38#include "basecnst.h"
39#include "sysbase.h"
40#include "utusrdef.h"
41#include "glusrdef.h"
42#include "earthloc.h"
43#include "ptusrdef.h"
44
45
46
47/* global variable */
48char shd_errors[BC_FILELTH+1];
49
50
51
52int main( int argc, char *argv[] )
53{
54        /* local variables */
55        double  lat1, lon1;             /* first location */
56        double  lat2, lon2;             /* second location */
57        double  distance, azim, bazim;  /* output values */
58        float   fdist, fbazim;          /* float output */
59        float   flat1, flon1, flat2, flon2;  /* float scratch values */
60        char    fname[cBcFileLth+1];    /* name of location list */
61        FILE    *fp;                    /* pointer to input file */
62        char    line[cBcLineLth+1];     /* current line of file */
63        STATUS  status;                 /* return status */
64        BOOLEAN sphere;                 /* sphere computation */
65        float   mindist, minlat, minlon;/* values of closest location */
66
67        /* executable code */
68
69        /* read parameters from command line */
70        if  (argc != 4)  {
71                printf( "Usage: %s <lat> <lon> <loclist>\n", argv[0] );
72                return 0;
73        } /*endif*/
74
75        if  (sscanf(argv[1],"%f",&flat1) != 1)  {
76                fprintf( stderr, "%s: error readinf <lat>\n", argv[0] );
77                return 1;
78        } /*endif*/
79        if  (sscanf(argv[2],"%f",&flon1) != 1)  {
80                fprintf( stderr, "%s: error readinf <lon>\n", argv[0] );
81                return 1;
82        } /*endif*/
83        strcpy( fname, argv[3] );
84        lat1 = flat1;
85        lon1 = flon1;
86
87        /* open input file and loop lines */
88        fp = fopen( fname, "r" );
89        if  (fp == NULL)  {
90                fprintf( stderr, "%s: cannot open input file %s\n", argv[0], fname );
91                return 1;
92        } /*endif*/
93
94        sphere = TRUE;
95        minlat = minlon = 0.0;
96        mindist = 1.0e6;
97
98        while (!feof(fp))  {
99
100                if  (fgets(line,cBcLineLth,fp) == NULL)  break;
101                if  (*line == '!')  continue;
102                if  (*line == '\n')  continue;
103                if  (sscanf(line,"%f %f",&flat2,&flon2) != 2)  {
104                        fprintf( stderr, "%s: read error on file %s, line\n%s\n",
105                                argv[0], fname, line );
106                        fclose( fp );
107                        return 1;
108                } /*endif*/
109
110                if  (sphere)  {
111                        mb_spherediff( flat2, flon2, flat1, flon1, &fdist, &fbazim );
112                } else {
113                        lat2 = flat2;
114                        lon2 = flon2;
115                        mb_locdiff( lat2, lon2, lat1, lon1, &distance, &azim, &bazim );
116                        fdist = distance;
117                } /*endif*/
118
119                if  (fdist < mindist)  {
120                        mindist = fdist;
121                        minlon = flon2;
122                        minlat = flat2;
123                } /*endif*/
124
125        } /*endwhile*/
126
127        fclose( fp );
128
129        printf( "%7.3f deg  %5.1f km  loc: %7.3f %7.3f\n", mindist, mindist*111.19,
130                minlat, minlon );
131
132        return 0;
133
134} /* end of main */
Note: See TracBrowser for help on using the repository browser.