#include "stdio.h" #include "stdlib.h" #include "astring.h" #include "string.h" #include "math.h" #include "misc.h" #include "flatfile.h" int month_decode(char *smonth) { int month; month = 0; if (strstr(smonth,"JAN")) month = 1; if (strstr(smonth,"FEB")) month = 2; if (strstr(smonth,"MAR")) month = 3; if (strstr(smonth,"APR")) month = 4; if (strstr(smonth,"MAY")) month = 5; if (strstr(smonth,"JUN")) month = 6; if (strstr(smonth,"JUL")) month = 7; if (strstr(smonth,"AUG")) month = 8; if (strstr(smonth,"SEP")) month = 9; if (strstr(smonth,"OCT")) month = 10; if (strstr(smonth,"NOV")) month = 11; if (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[200], stat[4], comp[5], slat[10], slon[10]; char cshdz, csxyz, cshdz1; float dataout[4], missing, dt, data[4], pi, h, d; double uttime,basetime, utold; int error, unit, nvar, nfiles, cfile, i, itime[6], comps, m; long orow, nlines, row; FILE *fpin; char linein[100], sdata[10]; long strl, n, ind; int done, days, hour, min, order[3]; float temp; missing = -1.0e32; pi = 3.1415; 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) { days = 0; printf("There is an error with the input file name.\n"); } else { done = 0; while (!done) { for (hour=0;hour<24;hour++) { strcpy(linein," "); strl = strlen(linein); while ((strl < 45) && (!feof(fpin))) { fgets(linein, 99, fpin); strl = strlen(linein); } if (feof(fpin)) { done = 1; } else { if (hour==0) { strcpy(stat, strmid(linein,0,3)); if (strpos(stat,"PBQ") >= 0) strcpy(stat,"PDB"); if (strpos(stat,"RES") >= 0) strcpy(stat,"RBC"); if (strpos(stat,"GLN") >= 0) strcpy(stat,"GLL"); if (strpos(stat,"HRN") >= 0) strcpy(stat,"HOR"); change_station_name(stat); strcpy(comp, mkupper(strmid(linein,19,4))); itime[0] = atoi(strmid(linein,9,2)); itime[1] = month_decode(mkupper(strmid(linein,4,3))); itime[2] = atoi(strmid(linein,7,2)); itime[3] = 0; itime[4] = 0; itime[5] = 0; basetime = c_i_to_r(itime); 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)); printf("fileout : %s\n",fileout); strcpy(ff_var,"time,"); for(comps=0;comps<3;comps++) { m = 0; if (comps==0) { cshdz = 'H'; cshdz1 = 'H'; csxyz = 'X'; } if (comps==1) { cshdz = 'D'; cshdz1 = 'd'; csxyz = 'Y'; } if (comps==2) { cshdz = 'Z'; cshdz1 = 'Z'; csxyz = 'Z'; } for (i=0;i<4;i++) { if ((comp[i] == cshdz) || (comp[i] == csxyz) || (comp[i] == cshdz1)) { strcat(ff_var,stat); strcat(ff_var,strmid(comp,i,1)); if (comps < 2) strcat(ff_var,","); m = i; } } order[comps] = m; } 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; orow = 0; } if (!error) { for (min=0;min<30;min++) { for (i=0;i<3;i++) { fscanf(fpin,"%f",&temp); dataout[i] = temp/10.0; if (fabs(dataout[i]) > 99998.0) dataout[i] = missing; } fscanf(fpin,"%f",&temp); if (strpos(comp,"D") >= 0 || strpos(comp,"d") >= 0) { h = dataout[0]; d = dataout[1]; if (fabs(h) >=99999.0 ) dataout[1] = missing; if ((fabs(h) < 99999.0) && (fabs(d) < 99999.0)) { d = d*pi/(600.0*180.0); dataout[0] = h*cos(d); dataout[1] = h*sin(d); } } uttime=basetime+(double) orow*60.0; error = fdput(unit,orow,uttime,dataout); orow++; for (i=0;i<3;i++) { fscanf(fpin,"%f",&temp); dataout[i] = temp/10.0; if (fabs(dataout[i]) > 99998.0) dataout[i] = missing; } fscanf(fpin,"%f",&temp); if (strpos(comp,"D") >= 0 || strpos(comp,"d") >= 0) { h = dataout[0]; d = dataout[1]; if (fabs(h) >=99999.0 ) dataout[1] = missing; if ((fabs(h) <99999.0) && (fabs(d) < 99999.0)) { d = d*pi/(600.0*180.0); dataout[0] = h*cos(d); dataout[1] = h*sin(d); } } uttime=basetime+(double) orow*60.0; error = fdput(unit,orow,uttime,dataout); orow++; } } } } ffclose(unit); } close(fpin); } } }