#include "stdio.h" #include "astring.h" #include "string.h" #include "stdlib.h" #include "misc.h" #include "flatfile.h" #include "amag.h" /* SAMNET DATA FORMAT ------------------- Each half-hour block consists of 61 records - 1 header record followed by 60 data records. Header record ------------- FORMAT (A3,1X,I2,1X,2I2,1X,3I2,2X,I1) Description Position (bytes) Length (bytes) Site I.D. 1-3 3 Year 5-6 2 Month 8-9 2 Day 10-11 2 Hour 13-14 2 Minute 15-16 2 Second 17-18 2 Sampling Period 21 1 Data Record ------------ FORMAT (18I4,A1,I3,I4) Description Position (bytes) Length (bytes) Data (Z,D,H) 1-72 18*4 Site I.D. 73 1 Day 74-76 3 Record I.D. 77-80 4 So, there are six 3-channel data values in each record stored in the order Z, D, H. The data values are in the range 0-4095 in units of 0.25 nT. */ void main(int argc, char *argv[]) { char filein[100], fileout[100]; FILE *fpin, *fpd; char ff_var[100],ff_units[100],ff_source[100],ff_type[100]; char ff_notes[300], line[100], stat[4], *uname; float data[3][86400], pi=3.1415926; double uttime, basetime; float outdata[10], dt; int yr, mo, da, itime[6], dumb, error, unit, nvar; int ihr, i, j, k, nfiles, cfile, found, done, sloc, strl; long ipos, flpos, fpos, orow; if (argc < 2) { printf("Enter filename to convert : "); gets(filein); nfiles = 1; } else { nfiles = argc - 1; strcpy(filein, argv[1]); } for (cfile=1;cfile<=nfiles;cfile++) { if (cfile > 1) strcpy(filein,argv[cfile]); fpin = fopen(filein,"r"); if (fpin == NULL) { printf("error in opening file %s.\n",filein); } else { fseek(fpin,0,SEEK_END); flpos = ftell(fpin); fseek(fpin,0,SEEK_SET); fpos = ftell(fpin); ipos = 0; while (flpos-fpos > 0) { if (fpos == 0) { fgets(line, 99, fpin); } if (ipos==0) { strcpy(stat,strmid(line,0,3)); change_station_name(stat); yr = atoi(strmid(line,4,2)); mo = atoi(strmid(line,7,2)); da = atoi(strmid(line,9,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); strcpy(fileout,mklower(stat)); strcat(fileout,strmid(line,4,2)); strcat(fileout,strmid(line,7,2)); strcat(fileout,strmid(line,9,2)); strl = strlen(fileout); for (i=0;i 72) { j=72; } for (i=0; i0) { for (i=0; i<6; i++) { for (j=0; j<3; j++) { data[j][ipos] = (atof(strmid(line,i*12+j*4,4)))*0.25; } if ((data[0][ipos] == 0) && (data[1][ipos] == 0) && (data[2][ipos] == 0)) { for (j=0; j<3; j++) data[j][ipos]=1024.0; } ipos++; } } } fpos = ftell(fpin); } close(fpin); if (ipos != (long) (86400.0/dt)) { printf("Error!\n"); printf("Number of points read in : %ld\n",ipos); printf("dt : %f - implies %ld points.\n",dt,(long) (86400.0/dt)); } printf("writing file %s\n",fileout); strcpy(ff_var,"time,"); strcat(ff_var,stat); strcat(ff_var,"H,"); strcat(ff_var,stat); 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); nvar = 4; 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 { /* order of variables is Z D H */ for (orow=0; orow