/* comp -lm -o /home/ridley/d.bin/pdhz_wdc 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; 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,"/galileo/d/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)); 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 = 3; 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) { done = 1; } else { done = 0; } for (i=0; i<1440; i++) { for (j=0; j<4; j++) { data[j][i] = 999999.0; } } for (ihrs=0;ihrs<24;ihrs++) { for (icomp=0;icomp<3;icomp++) { strcpy(linein," "); fgets(linein, 402, fpin); if (ihrs == 0) { if (icomp == 0) { dumb = (long) atof(strmid(linein,0,6)); dumb = 90000 - dumb; strcpy(sdumb,c_long_str(dumb,6)); strcpy(slat,strmid(sdumb,0,3)); strcat(slat,"."); strcat(slat,strmid(sdumb,3,3)); lat = 90.0-((float) dumb)/1000.0; dumb = (long) atof(strmid(linein,6,6)); strcpy(sdumb,c_long_str(dumb,6)); strcpy(slon,strmid(sdumb,0,3)); strcat(slon,"."); strcat(slon,strmid(sdumb,3,3)); lon = ((float) dumb)/1000.0; yr = atoi(strmid(linein,12,2)); mo = atoi(strmid(linein,14,2)); da = atoi(strmid(linein,16,2)); itime[0] = yr; itime[1] = mo; itime[2] = da; itime[3] = 0; itime[4] = 0; itime[5] = 0; basetime = c_i_to_r(itime); } comp = linein[18]; if (comp == (int) 'H') { order[0] = icomp; hdz = 1; } if (comp == (int) 'X') { order[0] = icomp; } if (comp == (int) 'D') { order[1] = icomp; hdz = 1; } if (comp == (int) 'E') { order[1] = icomp; hdz = 1; } if (comp == (int) 'Y') { order[1] = icomp; } if (comp == (int) 'Z') { order[2] = icompmax-1; icomp = icompmax-1; } if (icomp == 0) { strcpy(stat, strmid(linein,21,3)); change_station_name(stat); } } for (i=0; i<60; i++) { strcpy(sdata, strmid(linein,34+i*6,6)); k = 0; for (j=0;j<6;j++) { if (isdigit(sdata[j])) k++; } if (k > 0) { data[icomp][ihrs*60+i] = atof(sdata); } else { data[icomp][ihrs*60+i] = 0; } } /* printf("reading hour icomp: %li %3li\n",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]; } 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++; } }