#include "stdio.h" #include "stdlib.h" #include "astring.h" #include "string.h" #include "math.h" #include "misc.h" #include "flatfile.h" void main(int argc, char *argv[]) { FILE *fpout, *fpd, *fpin; char filein[100], fileout[100], filedec[100]; char *vn, *vu, *vs, *vt, no[1000], line[80]; char stime[24],etime[24], stat[4], sdate[25], *uname; double uttime[1]; int inunit, outunit, ncol[1], csize[1], error, decfile, found, done, sloc; int strl; long srow, erow, nrow[1], orow, irow; float *data, direction, dec, hx, dy, pi, missing; pi = 3.1415926; missing = -1.0e32; if (argc < 2) { printf("Enter flat file to copy : "); gets(filein); } else { strcpy(filein,argv[1]); } if (argc < 3) { printf("Enter output file name : "); gets(fileout); } else { strcpy(fileout,argv[2]); } printf("Enter start date of time period (dd-mon-yy) : "); gets(sdate); uname = spawn("/usr/bin/whoami"); strcpy(uname,strmid(uname,0,strlen(uname)-1)); strcpy(filedec,"/export/data1/ganglu/mags/tempfiles/mas.loc."); strcat(filedec,uname); strcat(filedec,"."); strcat(filedec,sdate); fpin = fopen(filedec,"r"); fgets(filedec, 80, fpin); fclose(fpin); strcpy(filedec,strmid(filedec,0,strlen(filedec)-1)); strl = strlen(filedec); if (strl > 0) { fpd = fopen(filedec,"r"); if (fpd != NULL) { decfile = 1; } else { printf("%s not found. Will prompt for declination angles\n",filedec); decfile = 0; } } else { decfile = 0; } inunit = ffopen(filein); vn = calloc(sizeof(char),ff_ncol[inunit]*16+2); vu = calloc(sizeof(char),ff_ncol[inunit]*16+2); vs = calloc(sizeof(char),ff_ncol[inunit]*31+2); vt = calloc(sizeof(char),ff_ncol[inunit]*2+2); data = calloc(sizeof(float),ff_ncol[inunit]); error = rdhead(inunit,vn,vu,vs,vt,no, stime,etime,nrow,ncol,csize); if ((int) vn[18] == (int) 'H') { printf("Converting HDZ to XYZ\n"); strcat(no,",Converted to XYZ coordinates from file : "); direction = 1.0; vn[18] = 'X'; vn[18+15] = 'Y'; } else { printf("Converting XYZ to HDZ\n"); strcat(no,",Converted to HDZ coordinates from file : "); direction = -1.0; vn[18] = 'H'; vn[18+15] = 'D'; } strcat(no,filein); strcpy(stat,strmid(vn,15,3)); found = 0; if (decfile) { fseek(fpd,0,SEEK_SET); done = 0; while (!done) { fgets(line, 79, fpd);; if (feof(fpd)) done = 1; sloc = strloc(line, stat); if ((sloc > 0) && (sloc < 2)) { dec = atof(strmid(line,67,7))*pi/180.0; printf("stat: %s - declination: %f\n",stat,dec*180.0/pi); found = 1; done = 1; } } } if (!found) { printf("Declination not found.\n"); printf("Enter declination for station %s in degrees : ",stat); scanf("%f",&dec); dec = dec*pi/180.0; } dec = direction*dec; printf("declination : %f\n",dec*180.0/pi); strcat(no,",Magnetometers rotated through and angle of : "); strcat(no,c_int_str((int) (dec*180.0/pi),2)); error = mkhead(fileout,ncol[0],vn,vu,vs,vt,no); outunit = ffcreate(fileout); if (outunit != -1) { srow = 0; erow = nrow[0]-1; orow = 0; for (irow=srow; irow<=erow; irow++) { error = fdget(inunit,irow,uttime,data); hx = data[0]; dy = data[1]; if ((hx != missing) && (dy != missing)) { data[0] = hx*cos(dec)-dy*sin(dec); data[1] = hx*sin(dec)+dy*cos(dec); } else { data[0] = missing; data[1] = missing; } error = fdput(outunit,orow,uttime[0],data); orow++; } ffclose(inunit); ffclose(outunit); } }