#include "stdio.h" #include "stdlib.h" #include "astring.h" #include "string.h" #include "flatfile.h" #include "math.h" #include "amag.h" void main(int argc, char *argv[]) { char ff_var[100],ff_units[100],ff_source[100],ff_type[100]; char ff_notes[300], filein[100], fileout[100]; char line[80], stat[4], slat[40][8], slon[40][8], sdumb[8], fstat[4]; char comp, linein[1500], sdata[7]; char statname[40][4]; float dataout[4], dt,pi,h,d; float datax[40][8640],datay[40][8640],dataz[40][8640]; float lats[40], lons[40]; double uttime, basetime, times[8640]; long i, j, k, l, numstats, n, firsttime, ntimes; int itime[6], cfile, nfiles, c[100], strl; int yr, mo, da, error, unit, nvar, nstat, done, ntick; long dumb, orow, pos, lpos; FILE *fpin; pi = 3.14159; ntick = 60; 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]); strcpy(fileout, filein); printf("Working on file %s\n",filein); fpin = fopen(filein,"r"); error = 0; done = 0; fseek(fpin,0,SEEK_END); lpos = ftell(fpin); fseek(fpin,0,SEEK_SET); printf(" Scanning file to determine how many stations there are.\n"); numstats = 0; done = 0; while ((!feof(fpin)) && (!done)) { pos = ftell(fpin); if (lpos-pos <= 0) done = 1; strcpy(linein," "); strl = strlen(linein); while (strl < 1400) { fgets(linein, 1441, fpin); strl = strlen(linein); pos = ftell(fpin); if (lpos-pos <= 0) { done = 1; strl = 1500; } } strcpy(stat, strmid(linein,12,3)); change_station_name(stat); for (n=0;n= 0) strcpy(stat,"RAA"); c[numstats] = atoi(strmid(linein,72,1)); strcpy(slat[numstats],strmid(linein,15,3)); strcat(slat[numstats],"."); strcat(slat[numstats],strmid(linein,18,2)); strcpy(slon[numstats],strmid(linein,20,3)); strcat(slon[numstats],"."); strcat(slon[numstats],strmid(linein,23,2)); if (numstats == 0) { itime[0] = atoi(strmid(linein,50,2)); itime[1] = atoi(strmid(linein,52,2)); itime[2] = atoi(strmid(linein,54,2)); itime[3] = atoi(strmid(linein,56,2)); itime[4] = atoi(strmid(linein,58,2)); itime[5] = 0; dt = atof(strmid(linein,60,2)); basetime = c_i_to_r(itime); } numstats++; } } printf(" dt is %f....\n",dt); printf(" There are %d stations in this file.\n",numstats); printf(" Basetime for this file is : %s\n",c_r_to_s(basetime)); if (dt < 5.0) { printf(" dt is %f. Code assumes that the data is 1 min values...\n",dt); exit(1); } fseek(fpin,0,SEEK_SET); i = 0; done = 0; while ((!feof(fpin)) && (!done)) { for (j=0;j 144) { ntimes=144; } for (j=0;j 4) { strcat(ff_notes,",Unknown coordinates (labeled as ABZ)."); } nvar = 4; printf(" Creating flatfile %s\n",fileout); 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; orow = 0; for (i=0;i 140) { printf("Working on station %s and time %s\n",statname[j],c_r_to_s(uttime));} if (datax[j][i*ntick+l] > 60000.0) { dataout[0] = -1.0e+32; } else { if (c[j] != 2) { dataout[0] = datax[j][i*ntick+l]; } else { h = datax[j][i*ntick+l]; } } if (datay[j][i*ntick+l] > 60000.0) { dataout[1] = -1.0e+32; } else { if (c[j] != 2) { dataout[1] = datay[j][i*ntick+l]; } else { d = datax[j][i*ntick+l]/600.0/180.0; } } if (dataz[j][i*ntick+l] > 60000.0) { dataout[2] = -1.0e+32; } else { dataout[2] = dataz[j][i*ntick+l]; } if ((c[j] == 2) && (h != -1.0e32)) { dataout[0] = h*sin(d); dataout[1] = h*cos(d); } error = fdput(unit,orow,uttime,dataout); if (error) { printf("Error in writing data to file %s.\n",fileout); l = ntick; i = ntimes; } else orow++; } } ffclose(unit); } /* if ((strloc(stat,fstat) >= 0) && (i > 0)) { done = 1; } else { i++; } } nstat = i; if (!error) { fseek(fpin,0,SEEK_SET); pos = ftell(fpin); i = 0; while ((pos < lpos) && (!error)) { strcpy(linein," "); strl = strlen(linein); while (strl < 1400) { fgets(linein, 1441, fpin); strl = strlen(linein); pos = ftell(fpin); if (lpos-pos <= 0) { done = 1; strl = 1500; } } j = i/nstat; k = i%nstat; for (l=0; l