#include "stdio.h" #include "flatfile.h" #include "string.h" #include "astring.h" #include "stdlib.h" #include "amag.h" #include "math.h" #include "misc.h" int month_decode(char *smonth) { int month; month = 0; if (strstr(smonth,"JAN") || strstr(smonth,"Jan")) month = 1; if (strstr(smonth,"FEB") || strstr(smonth,"Feb")) month = 2; if (strstr(smonth,"MAR") || strstr(smonth,"Mar")) month = 3; if (strstr(smonth,"APR") || strstr(smonth,"Apr")) month = 4; if (strstr(smonth,"MAY") || strstr(smonth,"May")) month = 5; if (strstr(smonth,"JUN") || strstr(smonth,"Jun")) month = 6; if (strstr(smonth,"JUL") || strstr(smonth,"Jul")) month = 7; if (strstr(smonth,"AUG") || strstr(smonth,"Aug")) month = 8; if (strstr(smonth,"SEP") || strstr(smonth,"Sep")) month = 9; if (strstr(smonth,"OCT") || strstr(smonth,"Oct")) month = 10; if (strstr(smonth,"NOV") || strstr(smonth,"Nov")) month = 11; if (strstr(smonth,"DEC") || strstr(smonth,"Dec")) month = 12; return month; } 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]; float dataout[4], missing, dt; double uttime,basetime, utold, dumb1, dumb2, dumb3; int yr, mo, da, error, unit, nvar, nfiles, cfile,i, itime[6]; long orow, pos, lpos; FILE *fpin; missing = -1.0e32; 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"); printf("reading file : %s\n",filein); orow = 0; error = 0; for (i=0; i<40; i++) fgets(line,79,fpin); strcpy(stat,strmid(line,17,3)); change_station_name(stat); for (i=0; i<5; i++) fgets(line,79,fpin); printf("%s\n",strmid(line,0,50)); itime[0] = atoi(strmid(line,28,2)); itime[1] = month_decode(mkupper(strmid(line,22,3))); itime[2] = atoi(strmid(line,19,2)); itime[3] = 0; itime[4] = 0; itime[5] = 0; /* printf("YY MM DD :%2li%2li%2li\n",itime[0],itime[1],itime[2]); */ basetime = c_i_to_r(itime); c_r_to_i(itime, basetime); 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)); strcpy(ff_var,"time,"); strcat(ff_var,stat); strcat(ff_var,"X,"); strcat(ff_var,stat); strcat(ff_var,"Y,"); 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; for (i=0; i<2; i++) fgets(line,79,fpin); utold = 0.0; while ((!feof(fpin)) && (!error)) { fgets(line,79,fpin); itime[0] = atoi(strmid(line,10,2)); itime[1] = month_decode(mkupper(strmid(line,4,3))); itime[2] = atoi(strmid(line,1,2)); itime[3] = 0; itime[4] = 0; itime[5] = 0; basetime = c_i_to_r(itime); uttime = basetime + atof(strmid(line,13,2))*3600. + atof(strmid(line,16,2))*60.; if (uttime > 0.) { if (uttime-basetime < 24.0*3600.0 && uttime > utold) { dataout[0] = atof(strmid(line,18,10)); dataout[1] = atof(strmid(line,30,10)); dataout[2] = atof(strmid(line,42,10)); if (dataout[0] == 99999.0) dataout[0] = missing; if (dataout[1] == 99999.0) dataout[1] = missing; if (dataout[2] == 99999.0) dataout[2] = missing; error = fdput(unit,orow,uttime,dataout); utold = uttime; orow++; } } else { error = 1; } } ffclose(unit); } close(fpin); }