/* comp -lm -o /home/ridley/d.bin/pwdc process_wdc.c */ #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[]) { char ff_var[100],ff_units[100],ff_source[100],ff_type[100]; char ff_notes[1000], filein[100], fileout[100]; char line[100], stat[4], slat[8], slon[8], sdumb[8], master[100]; char comp, linein[402], sdata[7], *uname, sdate[10]; float data[4][1440], lat, lon; float dataout[4], ave[3], mag, dec, pi, h, d, fac; double uttime,basetime; int i,j,k,itime[6], cfile, nfiles, icomp, ihrs, order[3], hdz, loc; int yr, mo, da, error, unit, nvar, icompmax, done; int npt[3], input, strl, datef; long dumb, orow, pos, lpos; FILE *fpin, *fpd; pi = 3.1415; if (argc < 2) { printf("Enter filename to convert : "); gets(filein); nfiles = 1; } else { nfiles = argc - 1; strcpy(filein, argv[1]); } datef = 0; if (argc > 2) { for (i=2; i<=argc; i++) { if (strpos(argv[i-1],"-d")==0) { datef = 1; strcpy(sdate,strmid(argv[i-1],2,9)); } } } uname = spawn("/usr/bin/whoami"); strcpy(uname,strmid(uname,0,strlen(uname)-1)); strcpy(master,"/export/data1/ganglu/mags/tempfiles/mas.loc."); strcat(master,uname); if (datef) { strcat(master,"."); strcat(master,sdate); } fpin = fopen(master,"r"); fgets(master, 80, fpin); fgets(master, 80, fpin); fclose(fpin); strcpy(master,strmid(master,0,strlen(master)-1)); printf("Master is %s\n",master); cfile = 1; while (cfile <=nfiles) { error = 0; if (strpos(argv[cfile],"-d") != 0) { if (cfile > 1) strcpy(filein,argv[cfile]); printf("Working on file %s\n",filein); fpin = fopen(filein,"r"); orow = 0; hdz = 0; error = 0; icompmax = 4; fseek(fpin,0,SEEK_END); lpos = ftell(fpin); fseek(fpin,0,SEEK_SET); } else { error = 1; } while ((!feof(fpin)) && (!error)) { pos = ftell(fpin); if (lpos-pos <= 399) { icomp = icompmax; done = 1; } else { icomp = 0; done = 0; } for (i=0; i<1440; i++) { for (j=0; j<4; j++) { data[j][i] = 999999.0; } } while (icomp 0) { data[icomp][ihrs*60+i] = atof(sdata); } else { data[icomp][ihrs*60+i] = 0; } } ihrs++; } icomp++; } if (!done) { if (orow == 0) { input = 0; if (hdz) { fpd = fopen(master,"r"); if (fpd==NULL) printf("error opening file.\n"); done = 0; dec = 0.0; while (!done) { fgets(line, 99, fpd); if (feof(fpd)) done = 1; pos = strpos(line, stat); if (pos == 4) { done = 1; dec = atof(strmid(line,51,6)); } } printf("dec : %f\n",dec); fclose(fpd); for (i=0; i<3; i++) { ave[i] = 0.0; npt[i] = 0; k = order[i]; for (j=0; j<1440; j++) { if (data[k][j] < 99999.0) { ave[i] = ave[i] + data[k][j]; npt[i]++; } } if (npt[i] > 0) { ave[i] = ave[i] / (float) npt[i]; } } mag = sqrt(pow(ave[0],2)+pow(ave[1],2)+pow(ave[2],2)); if (mag < 3000.0) { input = 1; } else { if (fabs(dec*600.0 - ave[1]) > 20.0*600.0) { input = 2; } else { input = 3; } } dec = dec*pi/(600.0*180.0); } strcpy(ff_var,"time,"); strcat(ff_var,stat); if ((input == 3) || (hdz == 0)) { strcat(ff_var,"X,"); } else { strcat(ff_var,"H,"); } strcat(ff_var,stat); if ((input == 3) || (hdz == 0)) { strcat(ff_var,"Y,"); } else { strcat(ff_var,"E,"); } strcat(ff_var,stat); strcat(ff_var,"Z"); strcpy(ff_units, "s,nT,nT,nT"); strcpy(ff_source,"from file listed below,same,same,same"); strcpy(ff_type,"T,R,R,R"); strcpy(ff_notes,"Converted from file : "); strcat(ff_notes,filein); strcat(ff_notes,",latitude : "); strcat(ff_notes,slat); strcat(ff_notes,",longitude : "); strcat(ff_notes,slon); if (input == 1) { strcat(ff_notes,",Input Data is assumed to be nT"); } if (input == 2) { strcat(ff_notes,",Input H and Z absolute; D variational"); strcat(ff_notes,"; therefore data not changed"); } if (input == 3) { strcat(ff_notes,",Input D component was assumed to be 1/10 Min"); } nvar = 4; strcpy(fileout,mklower(stat)); strcat(fileout,c_int_str(itime[0],2)); strcat(fileout,c_int_str(itime[1],2)); strcat(fileout,c_int_str(itime[2],2)); error = mkhead(fileout,nvar,ff_var,ff_units,ff_source, ff_type,ff_notes); if (!error) unit = ffcreate(fileout); if (error || unit == -1) error = 1; } if (error) { printf("error in creating flat file. Can not continue.\n"); } else { if (input == 1) { for (i=0; i<1440; i++) { h = data[order[0]][i]; d = data[order[1]][i]; if ((h <99999.0) && (d < 99999.0)) { data[order[0]][i] = h; data[order[1]][i] = d; } } } if (input == 3) { for (i=0; i<1440; i++) { h = data[order[0]][i]; d = data[order[1]][i]; if ((h <99999.0) && (d < 99999.0)) { d = d*pi/(600.0*180.0); data[order[0]][i] = h*cos(d); data[order[1]][i] = h*sin(d); } } } printf("Working on date %s\n",c_r_to_s(basetime)); for (i=0; i<1440; i++) { for (k=0;k<3;k++) { if (data[order[k]][i] < 99999.0) { dataout[k] = data[order[k]][i]*fac; } else { dataout[k] = -1.0e32; } } uttime=basetime+ ((double) 3600.0*24.0)*((double) i)/((double) 1440); error = fdput(unit,orow,uttime,dataout); if (error) printf("Error in writing data to file.\n"); orow++; } } } else { error = 1; } } if (strpos(argv[cfile],"-d") != 0) { ffclose(unit); close(fpin); } cfile++; } }