home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.eri.u-tokyo.ac.jp
/
2014.03.ftp.eri.u-tokyo.ac.jp.zip
/
ftp.eri.u-tokyo.ac.jp
/
pub
/
seisv
/
src
/
3.02
/
select.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-11-10
|
19KB
|
661 lines
/* SeisView alone/client Copyright (C) 1992,1999 K.Koketsu
<history>
99-11-09 Y2K issue for filename convention.
99-01-08 Check the success of gethostbyname().
98-11-05 Remote auxiliary files and fix a bug in retrv_server().
98-11-03 Include panel definitions and remove CR.
98-11-02 Measure the elapse time of retrieval (optional).
98-10-16 retrv() is again separated from `select.c'.
98-10-13 Fix in 2.02 patchlevel a appears again.
98-02-25 Abandon DOS, but take Windows 95/NT.
96-10-08 Fix the bug in retrv_server() (2.02 patchlevel a).
96-05-28 Fix the bug in making filenames after b10.
94-01-10 bcopy --> memcpy. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/timeb.h>
#include <time.h>
#include "seis.h"
#ifdef WIN32
extern HWND hwnd;
#endif
extern char *iseg[10]; /* work segments for selected events */
extern unsigned nseg; /* number of working segments */
extern short nmax, nmax2; /* maximum numbers of selected events */
extern char fc$[11]; /* string typed by user */
extern int itot; /* total # selected events */
extern short isort; /* flag for sorting */
extern char z$[11], w$[81]; /* work string */
extern char drv$[65], drvo$[65]; /* parameters for disk,prn */
/***************************** variables for menu ****************************/
extern char em$[51]; /* error message on menu */
extern char mc$[42][9], md$[65]; /* field strings */
extern short isr; /* initial cursor position */
/**************************** variables for retrieval ************************/
char myhost[101];
char f2$[6];
char y1$[5], m1$[3], d1$[3], h1$[3], n1$[3], s1$[5];
char y2$[5], m2$[3], d2$[3], h2$[3], n2$[3], s2$[5];
char dst$[11];
char so1$[4], som1$[3], so2$[4], som2$[3];
char sa1$[3], sam1$[3], sa2$[3], sam2$[3];
char sew1$[2], sew2$[2], sns1$[2], sns2$[2];
char dp1$[6], dp2$[6], mg1$[5], mg2$[5];
char ynh$[2], ynm$[2];
extern char to1$[4], tom1$[3], to2$[4], tom2$[3];
extern char ta1$[3], tam1$[3], ta2$[3], tam2$[3];
extern char tew1$[2], tew2$[2], tns1$[2], tns2$[2];
/******************** parameters for retrieving kernel **********************/
unsigned imd1,ihm1,isc1, imd2,ihm2,isc2; /* month+day, hour+min, sec */
long lo1, lo2; /* longitude */
long b180 = 180*256L, b360 = 360*256L;
short lom1,lom2, la1,lam1,la2,lam2; /* lonmin, lat,latmin */
unsigned ih1, ih2; /* depth */
int m1, m2; /* magnitude */
unsigned iyy1,im1,id1, iyy2,im2,id2; /* years from and to */
unsigned jh1,jm1, jh2,jm2; /* times from and to */
double hh1,hh2, rm1,rm2; /* depth, magnitude */
int getn_server();
/*************************** SELECT ********************** (Select events.) */
int select_seis() /* Avoid conflict with the standard UNIX select(). */
{
int i, rc;
unsigned iy, ra;
char g$[24];
int jyy1, jyy2;
#ifdef SPEED
char *timeline;
struct timeb timebuffer;
#ifdef WIN32
char tl0[80], tl1[80];
#endif
#endif
strcpy(mc$[ 0],f2$);
strcpy(mc$[ 1],y1$); strcpy(mc$[ 2],m1$); strcpy(mc$[ 3],d1$);
strcpy(mc$[ 4],h1$); strcpy(mc$[ 5],n1$); strcpy(mc$[ 6],s1$);
strcpy(mc$[ 7],y2$); strcpy(mc$[ 8],m2$); strcpy(mc$[ 9],d2$);
strcpy(mc$[10],h2$); strcpy(mc$[11],n2$); strcpy(mc$[12],s2$);
strcpy(md$,dst$);
strcpy(mc$[14],so1$); strcpy(mc$[15],som1$); strcpy(mc$[16],sew1$);
strcpy(mc$[17],so2$); strcpy(mc$[18],som2$); strcpy(mc$[19],sew2$);
strcpy(mc$[20],sa1$); strcpy(mc$[21],sam1$); strcpy(mc$[22],sns1$);
strcpy(mc$[23],sa2$); strcpy(mc$[24],sam2$); strcpy(mc$[25],sns2$);
strcpy(mc$[26],dp1$); strcpy(mc$[27],dp2$);
strcpy(mc$[28],mg1$); strcpy(mc$[29],mg2$);
strcpy(mc$[30],ynh$); strcpy(mc$[31],ynm$);
isr = 0;
j31:
standend();
rc = panel( SELECT );
if(rc==3 || rc==6) return( 0 );
isr = 0;
strcpy(f2$,mc$[0]);
strcpy(y1$,mc$[1]); strcpy(y2$,mc$[ 7]);
strcpy(m1$,mc$[2]); strcpy(m2$,mc$[ 8]);
strcpy(d1$,mc$[3]); strcpy(d2$,mc$[ 9]);
strcpy(h1$,mc$[4]); strcpy(h2$,mc$[10]);
strcpy(n1$,mc$[5]); strcpy(n2$,mc$[11]);
strcpy(s1$,mc$[6]); strcpy(s2$,mc$[12]);
strxcpy(dst$,md$);
strcpy(so1$,mc$[14]); strcpy(som1$,mc$[15]); strcpy(sew1$,mc$[16]);
strcpy(so2$,mc$[17]); strcpy(som2$,mc$[18]); strcpy(sew2$,mc$[19]);
strcpy(sa1$,mc$[20]); strcpy(sam1$,mc$[21]); strcpy(sns1$,mc$[22]);
strcpy(sa2$,mc$[23]); strcpy(sam2$,mc$[24]); strcpy(sns2$,mc$[25]);
strcpy(dp1$,mc$[26]); strcpy(dp2$ ,mc$[27]);
strcpy(mg1$,mc$[28]); strcpy(mg2$ ,mc$[29]);
strcpy(ynh$,mc$[30]); strcpy(ynm$ ,mc$[31]);
if(strchr(f2$,' ')) *(strchr(f2$,' ')) = 0;
iyy1 = atoi(y1$); iyy2 = atoi(y2$);
if(iyy1>iyy2 || (strstr(f2$,"ISC") && iyy1<1964)) {
isr = 1;
strcpy(em$, "Invalid date!");
goto j31;
}
im1 = atoi(m1$); im2 = atoi(m2$);
id1 = atoi(d1$); id2 = atoi(d2$);
jh1 = atoi(h1$); jh2 = atoi(h2$);
jm1 = atoi(n1$); jm2 = atoi(n2$);
isc1 = (unsigned)(atof(s1$)*10.);
isc2 = (unsigned)(atof(s2$)*10.);
imd1 = im1*256 + id1; imd2 = im2*256 + id2;
ihm1 = jh1*256 + jm1; ihm2 = jh2*256 + jm2;
if( *dst$ ) {
if(! dist(dst$,0)) { isr = 13; goto j31; }
}
lo1 = atoi(so1$ ); lo2 = atoi(so2$ );
la1 = atoi(sa1$ ); la2 = atoi(sa2$ );
lom1 = atoi(som1$); lom2 = atoi(som2$);
lam1 = atoi(sam1$); lam2 = atoi(sam2$);
jtoa((int)lo1,so1$,-3); jtoa((int)lo2,so2$,-3);
jtoa(la1 ,sa1$ ,-2); jtoa(la2 ,sa2$ ,-2);
jtoa(lom1,som1$,-2); jtoa(lom2,som2$,-2);
jtoa(lam1,sam1$,-2); jtoa(lam2,sam2$,-2);
if(lom1 > 59) isr = 17;
if(lom2 > 59) isr = 20;
if(lam1 > 59) isr = 23;
if(lam2 > 59) isr = 26;
if(isr > 1) {
strcpy(em$, "Minute values should be less than 60!");
goto j31;
}
_normal_font(); wreverse();
move(14, 5); clrtoeol();
addstr(" Now retrieving ...");
itot = 0; isort = 0;
lo1 = lo1*256 + lom1;
lo2 = lo2*256 + lom2;
la1 = la1*256 + lam1;
la2 = la2*256 + lam2;
if(*sew1$ == 'W') lo1 = - lo1;
if(*sew2$ == 'W') lo2 = - lo2;
if(*sns1$ == 'S') la1 = - la1;
if(*sns2$ == 'S') la2 = - la2;
if(la1 > la2) {
strcpy(em$,"The second latitude should be in the north of the first!");
goto j31;
}
if(lo1 >= lo2) lo2 = lo2 + b360;
hh1 = atof(dp1$); hh2 = atof(dp2$);
ih1 = (unsigned)(hh1*10 + 0.01);
ih2 = (unsigned)(hh2*10 + 0.01);
rm1 = atof(mg1$); rm2 = atof(mg2$);
if(rm1 >= 0.) m1 = (int)(rm1*10 + 0.01);
else m1 = (int)(rm1*10 - 0.01);
if(rm2 >= 0.) m2 = (int)(rm2*10 + 0.01);
else m2 = (int)(rm2*10 - 0.01);
if(*drv$ != '@') goto jALONE;
if(! open_server(drv$+1)) {
sprintf(em$, "`%s' cannot be accessed to!", drv$+1);
goto j31;
}
if(!strcmp(f2$,"RIKA") || !strcmp(f2$,"UTSU")) {
if(! retrv_server(f2$,&itot,0)) goto j46;
close_server();
}
else {
if(! strcmp(f2$,"JPN")) {
jyy1 = iyy1; jyy2 = iyy2;
if(iyy1 < 1885) {
if(! retrv_server("RIKA",&itot,0)) goto j46;
else { close_server(); open_server(drv$+1); }
}
if(iyy1 < 1926) {
if(iyy2 > 1925) iyy2 = 1925;
if(! retrv_server("UTSU",&itot,0)) goto j46;
else { close_server(); open_server(drv$+1); }
iyy2 = jyy2;
}
if(iyy2 > 1925) { if(iyy1 < 1926) iyy1 = 1926; }
else goto j56;
}
for(iy=iyy1; iy<=iyy2; iy++) {
if(iy > iyy1) open_server(drv$ + 1);
if(! strcmp(f2$,"JPN")) strcpy(g$, "JMA");
else strcpy(g$, f2$);
if(! (i = retrv_server(g$, &itot, iy))) goto j46;
close_server();
if(i < 0) break;
}
}
j56:
if(! strcmp(f2$,"JPN")) { iyy1 = jyy1; iyy2 = jyy2; }
goto j41;
j46:
close_server();
goto j31;
jALONE:
if(!strcmp(f2$,"RIKA")) {
if(! rvrika()) goto j31;
}
else if(!strcmp(f2$,"UTSU")) {
if(! rvutsu(0)) goto j31;
}
else {
if(!strcmp(f2$,"JPN")) {
if(iyy1 < 1885) {
if(! rvrika()) goto j31;
sprintf(w$, "RIKA -- %5d", itot);
mvaddstr(14, 25, w$);
xflush();
}
if(iyy1 < 1926) { /* Note RIKA+UTSU < 2000. */
if(iyy2 < 1926) i = 0; else i = 1;
if(! rvutsu(i)) goto j31;
sprintf(w$, "UTSU -- %5d", itot);
mvaddstr(14, 25, w$);
xflush();
}
}
if(iyy1 < 1926) iyy1 = 1926;
#ifdef SPEED
ftime( &timebuffer );
timeline = ctime( &(timebuffer.time) );
#ifdef WIN32
sprintf(tl0, "%.19s.%hu %s", timeline, timebuffer.millitm,
&timeline[20]);
#else
printf("%.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20]);
#endif
#endif
for(iy=iyy1; iy<=iyy2; iy++) {
if(!strcmp(f2$,"JPN")) strcpy(g$, "JMA"); else strcpy(g$, f2$);
sprintf(w$, "%-5.5s (%4d) -- %5d", g$, iy, itot);
mvaddstr(14, 25, w$);
xflush();
/* Y2K for file name convention */
/*sprintf(g$+strlen(g$), "-%d.B", iy-1900);*/
if (iy < 2000) sprintf(g$+strlen(g$), "-%d.B", iy-1900);
else if(iy < 2010) sprintf(g$+strlen(g$),"00%d.B", iy-2000);
else sprintf(g$+strlen(g$), "0%d.B", iy-2000);
j29: if(! bload(iseg[nseg-1],0,g$)) goto j31;
ra = 0;
for(;;) {
ra = retrv(ra, itot, nseg-1, 0);
if(ra == 0xffff) break;
else if(ra == 0xfffe) {
itoa(2, g$+strlen(g$), 10);
goto j29;
}
else if(ra > 0x7fff) {
/* itoa(0xffff-ra+1, g$+strlen(g$)-1, 10); 96-05-28 */
itoa(0xffff-ra+1, strchr(g$,'.')+2, 10);
goto j29;
}
else {
if(++itot == nmax) break;
ra++;
}
}
if(itot == nmax) break;
}
#ifdef SPEED
ftime( &timebuffer );
timeline = ctime( &(timebuffer.time) );
#ifdef WIN32
sprintf(tl1, "%.19s.%hu %s", timeline, timebuffer.millitm,
&timeline[20]);
strcat(tl0, tl1);
MessageBox(hwnd, tl0, "debug", MB_OK);
#else
printf("%.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20]);
#endif
#endif
}
j41:
standend();
if(itot == nmax) return( 2 );
else return( 0 );
}
/********* DIST ************ (Translate a district name to lat/lon.) */
dist(a$, flg)
char *a$; int flg;
{
char *iwseg;
unsigned rcod, off;
char s[11], ew1[2], ew2[2], ns1[2], ns2[2];
char do1[4], do2[4], da1[3], da2[3];
iwseg = iseg[nseg-1]; off = 0;
if(!(rcod = rbload(iwseg, off, "regions"))) return( 0 );
while(sscanf(iwseg+off, "%10s %s %s %s %s %s %s %s %s",
s, do1, ew1, do2, ew2, da1, ns1, da2, ns2) == 9) {
if( strstr(a$,s) ) goto jdist;
if((off = (unsigned)strchr(iwseg+off,'\n')) == 0) break;
if((off = off + 1 - (unsigned)iwseg) > rcod) break;
}
strcpy(em$, "Invalid region name!");
return( 0 );
jdist:
if(flg == 0) {
strcpy(so1$, do1); strcpy(so2$, do2);
strcpy(sa1$, da1); strcpy(sa2$, da2);
strcpy(som1$,"00"); strcpy(som2$,"00");
strcpy(sam1$,"00"); strcpy(sam2$,"00");
strcpy(sew1$, ew1); strcpy(sew2$, ew2);
strcpy(sns1$, ns1); strcpy(sns2$, ns2);
}
else {
strcpy(to1$, do1); strcpy(to2$, do2);
strcpy(ta1$, da1); strcpy(ta2$, da2);
strcpy(tom1$,"00"); strcpy(tom2$,"00");
strcpy(tam1$,"00"); strcpy(tam2$,"00");
strcpy(tew1$, ew1); strcpy(tew2$, ew2);
strcpy(tns1$, ns1); strcpy(tns2$, ns2);
}
return( 1 );
}
rvutsu( flg )
int flg;
{
unsigned ra;
if(! bload(iseg[nseg-1],0,"UTSU.B")) return( 0 );
j29:
ra = 0;
for(;;) {
ra = retrv(ra, itot, nseg-1, 0);
if(ra == 0xffff) return( 1 );
else if(ra > 0x7fff) {
if(flg == 1) return( 1 );
if(! bload(iseg[nseg-1],0,"UTSU.B2")) return( 0 );
goto j29;
}
else {
if(++itot == nmax) return( 2 );
ra++;
}
}
}
rvrika()
{
unsigned ra;
if(! bload(iseg[nseg-1],0,"RIKA.B")) return( 0 );
ra = 0;
for(;;) {
ra = retrv(ra, itot, nseg-1, 0);
if(ra == 0xffff) return( 1 );
else { itot++; ra++; }
}
}
/* common retrieving kernel */
#include "retrv.c"
int rbload(sg, off, fname) /* extended bload() */
char *sg, *fname;
unsigned off;
{
char t[17];
unsigned rcod, nn;
if(*drvo$ == '@') {
if(! open_server(drvo$+1)) {
sprintf(em$, "`%s' cannot be accessed to!", drvo$+1);
return( 0 );
}
gethostname(myhost, 100);
send_server( myhost );
send_server( ":" ); send_server( fname );
getn_server(t, 16);
if(! *t) {
sprintf(em$, "File `%s' does not exist on `%s'!", t+1, drvo$+1);
return( 0 );
}
rcod = 0;
for(;;) {
getn_server(t, 6);
sscanf(t, "%d", &nn);
if(nn == 0) break;
getn_server(sg+off+rcod, nn);
rcod += nn;
}
}
else {
if(!(rcod = bload(sg,off,fname))) return( 0 );
}
return( rcod );
}
#define SEISD (516 + 5000)
#ifdef WIN32
#include <io.h>
#include <winsock.h>
SOCKET sock;
const char on = 1;
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
int sock;
int on = 1;
#endif
open_server( hname )
char *hname;
{
struct hostent *addr;
struct sockaddr_in server;
/* listen connection */
server.sin_family = AF_INET;
server.sin_port = htons( SEISD );
addr = gethostbyname( hname );
if(addr == NULL) return( 0 ); /* 1999-01-08 */
memcpy(&server.sin_addr, addr->h_addr, addr->h_length);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) return( 0 );
if (connect(sock, &server, sizeof(server)) < 0) {
close( sock );
return( 0 );
}
setsockopt(sock, SOL_SOCKET, SO_LINGER, &on, on);
return( 1 );
}
void close_server() { close( sock ); }
int getn_server(s, n)
char *s; int n;
{
#ifdef WIN32
return( recv(sock, s, n, 0) );
#else
return( read(sock, s, n) );
#endif
}
void putn_server(s, n)
char *s; int n;
{
#ifdef WIN32
send(sock, s, n, 0);
#else
write(sock, s, n);
#endif
}
void send_server( t )
char *t;
{
char s[16];
int i, n;
n = strlen( t );
for(i=0; i<16; i++) {
if(i >= n) s[i] = ' ';
else s[i] = t[i];
}
putn_server(s, 16);
}
unsigned short
read_server( s )
unsigned char *s;
{
getn_server(s, 16);
return(s[0] + (s[1]<<8));
}
void submit_server(s, iy)
char *s;
int iy;
{
char t[5];
gethostname(myhost, 100);
send_server( myhost );
send_server( s );
sprintf(t, "%4d", iy); send_server( t );
send_server( itoa(iyy1,w$,10) );
send_server(m1$); send_server(d1$);
send_server(h1$); send_server(n1$); send_server(s1$);
send_server( itoa(iyy2,w$,10) );
send_server(m2$); send_server(d2$);
send_server(h2$); send_server(n2$); send_server(s2$);
send_server(so1$); strcpy(w$, som1$);
if(*sew1$ == 'W') strcat(w$, sew1$); send_server(w$);
send_server(so2$); strcpy(w$, som2$);
if(*sew2$ == 'W') strcat(w$, sew2$); send_server(w$);
send_server(sa1$); strcpy(w$, sam1$);
if(*sns1$ == 'S') strcat(w$, sns1$); send_server(w$);
send_server(sa2$); strcpy(w$, sam2$);
if(*sns2$ == 'S') strcat(w$, sns2$); send_server(w$);
send_server(dp1$); send_server(dp2$);
send_server(mg1$); send_server(mg2$);
send_server(ynh$); send_server(ynm$);
}
retrv_server(s, itot, iy)
char *s;
int *itot, iy;
{ /* <return code>
1: OK, 0: network error, -1: over nmax */
char t[17];
int n, ismany;
unsigned pag, nn, m, of, num, eof;
if(iy == 0) sprintf(w$, "%-5.5s -- %5d", s, *itot);
else sprintf(w$, "%-5.5s (%4d) -- %5d", s, iy, *itot);
mvaddstr(14, 25, w$); xflush();
submit_server(s, iy);
eof = 0;
ismany = 0;
while(eof != 0xffff) {
getn_server(t, 16);
if(! *t) {
sprintf(em$, "File `%s' does not exist on `%s'!", t+1, drv$+1);
return( 0 );
}
else send_server( "1" );
getn_server(t, 5);
t[5] = 0;
n = atoi( t );
of = 0;
nn = (unsigned)n * 16 + 2;
while(nn > 0) {
m = (unsigned)getn_server(iseg[nseg-1]+of, nn);
if(m > 0) { nn -= m; of += m; }
}
eof = (unsigned char)iseg[nseg-1][of+nn-1];
eof = (eof << 8) + (unsigned char)iseg[nseg-1][of+nn-2];
if(*itot+n > nmax) {
n = nmax - *itot;
ismany = 1;
}
if(n > 0) {
pag = (*itot) / 4095;
num = (*itot) % 4095;
nn = n * 16;
if(num+n > 4095) {
of = num*16 + 4;
m = (4095-num) * 16;
memcpy(iseg[pag]+of, iseg[nseg-1], m);
/* These are wrong! (1998-11-05)
*(iseg[pag]+of+nn ) = (unsigned char)0xfe;
*(iseg[pag]+of+nn+1) = (unsigned char)0xff; */
iseg[pag][0xfff4] = (unsigned char)0xfe;
iseg[pag][0xfff5] = (unsigned char)0xff;
pag++;
num = 0;
nn -= m;
}
else m = 0;
of = num*16 + 4;
memcpy(iseg[pag]+of, iseg[nseg-1]+m, nn);
*itot += n;
*(iseg[pag]+of+nn ) = (unsigned char)0xff;
*(iseg[pag]+of+nn+1) = (unsigned char)0xff;
}
}
if( ismany ) return( -1 );
return( 1 );
}
char select_pan[] = "\
Request : SELECT \
Change parameters in () as you like and press Enter. \
0. Data Source ( ) \
1. Date/Time From ( + / / ) ( : : ) \
To ( + / / ) ( : : ) \
2. Region ( ) \
or Longitude -deg,min- ( )( )( ) --> ( )( )( ) \
Latitude -deg,min- ( )( )( ) --> ( )( )( ) \
|Longitudes are measured eastward.| \
3. Depth -km- ( ) --> ( ) \
4. Magnitude ( ) --> ( ) \
5. You like to include events whose following item is undetermined? < Y/N > \
Depth ( ) \
Magnitude ( ) \
26, 2, 5 \
28, 3, 4 \
35, 3, 2 \
40, 3, 2 \
51, 3, 2 \
56, 3, 2 \
61, 3, 4 \
28, 4, 4 \
35, 4, 2 \
40, 4, 2 \
51, 4, 2 \
56, 4, 2 \
61, 4, 4 \
26, 5, 10 \
26, 6, 3 \
33, 6, 2 \
39, 6, 1 \
49, 6, 3 \
56, 6, 2 \
62, 6, 1 \
26, 7, 2 \
32, 7, 2 \
38, 7, 1 \
49, 7, 2 \
55, 7, 2 \
61, 7, 1 \
26, 9, 5 \
49, 9, 5 \
26, 10, 4 \
49, 10, 4 \
26, 12, 1 \
26, 13, 1 ";