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 / sort.c < prev    next >
C/C++ Source or Header  |  1999-11-10  |  7KB  |  172 lines

  1. /*  SeisView alone/client    Copyright (C) 1992,1998  K.Koketsu
  2.              <history>
  3.          98-11-03  Include panel definitions and remove CR.
  4.          98-10-01  Fix the misdeclaration of `isr'.
  5.          93-12-07  Original version.                */
  6.  
  7. #include "seis.h"
  8.  
  9. extern char     *iseg[10];            /* work segments for  selected events  */
  10. extern unsigned nseg;                 /* number of working segments          */
  11. extern short   nmax, nmax2;           /* maximum numbers of selected events  */
  12. extern int   itot;                               /* total # selected events  */
  13. extern short isort;                              /* flag for sorting         */
  14.  
  15. /***************************** variables for menu ****************************/
  16. extern char   em$[51];                            /* error message on menu   */
  17. extern char   mc$[42][9];                         /* field strings           */
  18. extern short  isr;                                /* initial cursor position */
  19.  
  20. /*************************** SORT ****************** (Sort selected events.) */
  21. int  ll[2048], lr[2048];
  22. void swap();
  23. int  fnitem();
  24.  
  25. int sort()
  26. {
  27.     int    ip, iq, ir;
  28.     int    i, j, k, l, m, n;
  29.     double tt, tu;
  30.  
  31.     if(itot == 0)  return( 3 );
  32.     if(itot > nmax2)  return( 4 );
  33.     isr = 0;
  34.     strcpy(mc$[0], "MAG ");  strcpy(mc$[1], "D");
  35.     strcpy(mc$[2], "    ");  strcpy(mc$[3], " ");
  36. j40:
  37.     if(panel(SORT) == 3)  return( 0 );
  38.  
  39.     if(!strcmp(mc$[0],"TIME") && isort==0) {
  40.         if(*mc$[1] == 'D') {
  41.         isort = 1;
  42.         for(i=0; i<itot/2; i++)  swap(i, itot-1-i);
  43.     }
  44.     goto j60;
  45.     }
  46.     if((ip=fnitem(mc$[0])) > 10) {
  47.     strcpy(em$, "The first item is invalid!");
  48.     goto j40;
  49.     }
  50.  
  51.       wblink();                       /* 1st sorting (quick sort) */
  52.       move(11, 6);
  53.       printw("Be patient. Now sorting to %s ...", mc$[0]);
  54.  
  55.       if(*mc$[1] == 'D') iq = 2;  else iq = 1;
  56.       if(!strcmp(mc$[0],"TIME") && iq==1) isort = 0;  else isort = 1;
  57.       k = 1;  l = 1;  m = itot;
  58. j160: i = l;  j = m;  n = (l+m)/2;
  59.       tt = fnarg(n, ip);
  60. j170: if(iq == 2) goto j182;
  61. j172: if(fnarg(i,ip) < tt) { i++; goto j172; }
  62. j174: if(fnarg(j,ip) > tt) { j--; goto j174; }
  63.       goto j200;
  64. j182: if(fnarg(i,ip) > tt) { i++; goto j182; }
  65. j184: if(fnarg(j,ip) < tt) { j--; goto j184; }
  66. j200: if(i >= j) goto j210;
  67.       swap(i-1, j-1);
  68.  
  69.       i++;  j--;  goto j170;
  70. j210: if(i == j) { i++; j--; }
  71.       if(l >= j) goto j240;
  72.       if(i < m) { ll[k] = l; lr[k] = j; k++; l=i; goto j160; }
  73.       m = j;  goto j160;
  74. j240: if(i < m) { l = i;  goto j160; }
  75.       k--;
  76.       if(k >= 0) { l = ll[k]; m = lr[k]; goto j160; }
  77.  
  78. j60:  if(!strcmp(mc$[2],"    "))  goto j50;
  79.  
  80.       if((ir=fnitem(mc$[2])) > 10) {
  81.       strcpy(em$, "The second item is invalid!");
  82.       goto j40;
  83.       }
  84.  
  85.       wblink();                      /* 2nd sorting (bubble sort) */
  86.       move(11, 6);
  87.       printw("Be patient. Now sorting to %s ...", mc$[2]);
  88.  
  89.       if(*mc$[3] == 'D') iq = 2;  else iq = 1;
  90.       isort = 1;
  91.       i = 1;  tt = fnarg(i,ip);
  92. j290: j = i+1;  if(j >= itot) goto j50;
  93. j300: tu = fnarg(j,ip);
  94.       if(tu == tt) { if(++j < itot) goto j300; }
  95.       if(j == i+1)  goto j310;
  96.       if(j == itot)  j++;
  97.       for(k=j-1; k>=i+1; k--) {
  98.       for(l=i+1; l<=k; l++) {
  99.           if((iq==1 && fnarg(l-1,ir)>fnarg(l,ir)) ||
  100.          (iq==2 && fnarg(l-1,ir)<fnarg(l,ir)))  swap(l-2, l-1);
  101.       }
  102.       }
  103. j310: i = j;  tt = tu;  goto j290;
  104. j50:  standend();
  105.       return( 0 );
  106. }
  107.  
  108. int fnitem( a$ )
  109. char   *a$;
  110. {
  111.     if( strstr(a$,"TIME") )  return( 0 );
  112.     else if( strstr(a$,"LON") )  return( 1 );
  113.     else if( strstr(a$,"LAT") )  return( 2 );
  114.     else if( strstr(a$,"DEP") )  return( 3 );
  115.     else if( strstr(a$,"MAG") )  return( 4 );
  116.     return( 99 );
  117. }
  118.  
  119. void swap(P, Q)
  120. unsigned  P, Q;
  121. {
  122.     unsigned TP, SP, TQ, SQ;
  123.     char     IBF[16];
  124.  
  125.     SP = P/4095;  TP = P - SP*4095;
  126.     SQ = Q/4095;  TQ = Q - SQ*4095;
  127.     memcpy(IBF, iseg[SP]+TP*16+4, 16);
  128.     memcpy(iseg[SP]+TP*16+4, iseg[SQ]+TQ*16+4, 16);
  129.     memcpy(iseg[SQ]+TQ*16+4, IBF, 16);
  130. }
  131.  
  132. /************************ Panel Definitions **********************/
  133. char sort_pan[] = "\
  134.     Request : SORT                                                           \
  135.     Specify items and sorting order in () as you like and press Enter.       \
  136.                                                                              \
  137.                        Item   Order      Sortable items       Valid order    \
  138.                  1st (      ) (   )   | TIME   LON    LAT     A:Ascending    \
  139.                  2nd (      ) (   )   | DEP    MAG            D:Descending   \
  140.                                                                              \
  141.      Keep the second item blank if you do not need it.                       \
  142.     (The second sorting will be done by means of bubble sort,                \
  143.      and so that should take a long time.)                                   \
  144.                                                                              \
  145. 23,  4,  4 \
  146. 32,  4,  1 \
  147. 23,  5,  4 \
  148. 32,  5,  1 ";
  149.  
  150. char help_pan[] = "\
  151. 'Region' always has higher priority than 'Latitude' and 'Longitude'.         \
  152. If you like to specify an area by coordinates, 'Region' should be made blank.\
  153.                                                                              \
  154. <Regions> (See the file 'regions'. You may edit this.)                       \
  155. Geographical Area: WORLD,PACIFIC,ATLANTIC,INDIAN,NWPAC(NW Pacific),AUSTRALIA,\
  156.    AFRICA,NAMERICA,CAMERICA,SAMERICA,USSR,CHINA,ASEAN,INDIA,ARAB,EUROPE,JAPAN\
  157. Japanese district:                                                           \
  158.    HOKKAIDO,TOHOKU,KANTO,IZU,TOKAI,CHUBU,KINKI,NANKAI,CHUGOKU,SHIKOKU,KYUSHU \
  159.                                                                              \
  160. <Data Sources> ISC,ISCNE: International Seismological Centre                 \
  161.                PDE      : USGS (Preliminary Determination of Earthquakes)    \
  162.                JMA      : Japan Meteorological Agency                        \
  163.                UTSU     : Dr Utsu's Catalog                                  \
  164.                RIKA     : Rika Nenpyo                                        \
  165.                JPN      : JMA + UTSU + RIKA                                  \
  166.                JUNEC    : Japanese National Universities                     \
  167.                                                                              \
  168. <Map Data>     EARTH: World with plate boundaries                            \
  169.                WORLD: fine World by ISC                                      \
  170.                NWPAC: North-western Pacific by Dr Yoshii                     \
  171.                JAPAN: fine Japan by Ms Kotake                                ";
  172.