#include "stdio.h" #include "astring.h" #include "string.h" #include "stdlib.h" #include "amath.h" void main(int argc, char *argv[]) { FILE *fpin, *fpout; char filein[100], fileout[100]; char line[100], *savedlines, que[100], *start; float *sortvalue; int *order, nmag, col, i; int ifilef, ofilef, helpf, mlatf, mlongf, glatf, glongf, statf, flag; ifilef = 0; ofilef = 0; helpf = 0; mlatf = 0; mlongf = 0; glatf = 0; glongf = 0; statf = 0; if (argc >= 2) { for (i=2; i<=argc; i++) { if (strpos(argv[i-1],"-")==0) { flag = 0; if (strpos(argv[i-1],"l")>0) { mlatf = 1; flag=1; } if (strpos(argv[i-1],"o")>0) { mlongf = 1; flag=1; } if (strpos(argv[i-1],"g")>0) { glatf = 1; flag=1; } if (strpos(argv[i-1],"n")>0) { glongf = 1; flag=1; } if (strpos(argv[i-1],"s")>0) { statf = 1; flag=1; } if ((strpos(argv[i-1],"h")>0) || (!flag)) { helpf = 1; printf("usage : \n"); printf("sortpsi [-a] [-o] [-s] [flatfilein] [flatfileout]\n"); printf(" -l sort by magnetic latitude\n"); printf(" -o sort by magnetic longitude\n"); printf(" -g sort by geographic latitude\n"); printf(" -n sort by geographic longitude\n"); printf(" -s sort by station name\n"); } } else { if (!ifilef) { ifilef = 1; strcpy(filein,argv[i-1]); } else { ofilef = 1; strcpy(fileout,argv[i-1]); } } } } if (!helpf) { if (!ifilef) { printf("Enter master_psi file to sort : "); gets(filein); } if (!ofilef) { printf("Enter output psi name : "); gets(fileout); } if (mlatf+mlongf+glatf+glongf+statf == 0) { printf("Which column would you like to sort the PSI file by?\n"); printf("l - magnetic latitude\n"); printf("o - magnetic longitude\n"); printf("g - geographic latitude\n"); printf("n - geographic longitude\n"); printf("s - station identification\n"); gets(que); strcpy(que,mklower(que)); if (strpos(que,"l") == 0) mlatf = 1; if (strpos(que,"o") == 0) mlongf = 1; if (strpos(que,"g") == 0) glatf = 1; if (strpos(que,"n") == 0) glongf = 1; if (strpos(que,"s") == 0) statf = 1; if (glatf+glongf+mlatf+mlongf+statf == 0) { printf("I did not understand - therefor I chose "); printf("sorting by magnetic latitude.\n"); mlatf = 1; } } if (mlatf+mlongf+glatf+glongf+statf > 1) { if (mlatf) { mlongf = 0; glatf = 0; glongf = 0; statf = 0; } if (statf) { mlatf = 0; mlongf = 0; glatf = 0; glongf = 0; } if (glatf) { mlatf = 0; mlongf = 0; glongf = 0; statf = 0; } if (mlongf) { mlatf = 0; glatf = 0; glongf = 0; statf = 0; } printf("Too many flags selected!!\n"); } if (mlatf) printf(" ---- Sorting by magnetic latitude\n"); if (mlongf) printf(" ---- Sorting by magnetic longitude\n"); if (statf) printf(" ---- Sorting by station identification\n"); if (glatf) printf(" ---- Sorting by geographic latitude\n"); if (glongf) printf(" ---- Sorting by geographic longitude\n"); fpin = fopen(filein, "r"); fpout = fopen(fileout, "w"); if ((fpin != NULL) && (fpout != NULL)) { fgets(line,100,fpin); fprintf(fpout,"%s",line); nmag = atoi(strmid(line,0,3)); fgets(line,100,fpin); fprintf(fpout,"%s",line); sortvalue = calloc(sizeof(float),nmag); savedlines = calloc(sizeof(char),nmag*100); start = savedlines; for (i=0;i