/*

	cl /AL filter.c



	filter track  1 /c 100

	filters 'track.raw' and makes 'tracka.raw' with no vector longer
	than 100 points.

*/
#include <stdio.h>
#include <math.h>
#include <string.h>

#define NUM_TYPE 5
#define DEG_RAD 0.01745329251				/* converts degrees to radians */
#define MAX_VAL 10000
#define MAX_DIST 10000.00

FILE *Fpin,*Fpout,*FpPrn;
double huge Lat[MAX_VAL],huge Lon[MAX_VAL];
struct filetype
{
	char *id;
	double scale;
}F[NUM_TYPE]=
{
	{"0",100.0},
	{"A",10.0},
	{"B",1.0},
	{"C",0.1},
	{"D",0.01}
};
int Type=0;
int CutLong=0;

main(argc,argv)

int argc;
char *argv[];

{
	int i,j,k,n,il,m;
	char string1[100],string2[100],filename[40],file[40],string0[100];
	int num,segs=0,len,numread,print=0,end_of_file=0;
	double dist,mindist,coslat,sum=0.0,used=0.0;

	for(i=1;i<argc;i++)
	{
		if(argv[i][0]=='/'&&argv[i][1]=='p')
			print=1;
		if(i<argc-1&&argv[i][0]=='/'&&argv[i][1]=='c')
		{
			sscanf(argv[i+1],"%d",&CutLong);
			printf("Cutting at %d points\n",CutLong);
		}
	}

	FpPrn=fopen("prn","wt");

	if(argc<2)
	{
		printf("Give input file name.  (type will be made '____.raw'\n");
		scanf("%s",file);
	}
	else
		strcpy(file,argv[1]);
	strcpy(filename,file);
	strcat(filename,".raw");
	Fpin=fopen(filename,"rt");
	if(!Fpin)
	{
		printf("Could not open file '%s'.\n",filename);
		if(print==1)
			fprintf(FpPrn,"Could not open file '%s'.\n",filename);
		exit(0);
	}
	printf("\n\nChoose minimum distance in km.\n\n");
	for(i=0;i<NUM_TYPE;i++)
		printf("%d -- %8.5lf  (%s)\n",i+1,F[i].scale,F[i].id);
	if(argc<3)
		num=getch()-'1';
	else
		num=argv[2][0]-'1';
	if(num<0||num>=NUM_TYPE)
		exit(0);
	strcpy(filename,file);
	filename[7]='\0';
	strcat(filename,F[num].id);
	strcat(filename,".raw");
	Fpout=fopen(filename,"wt");
	if(!Fpout)
	{
		printf("Could not open '%s'/n",filename);
		if(print==1)
			fprintf(FpPrn,"Could not open '%s'/n",filename);
	}
	else
	{
		printf("Opened '%s'\n",filename);
		if(print==1)
			fprintf(FpPrn,"Opened '%s'\n",filename);
	}
	mindist=F[num].scale/111.0;
	mindist*=mindist;
	while(fscanf(Fpin,"%s",string0)==1)
	{
		sscanf(string0,"%d",&n);
		segs+=1;
		i=0;
		if(string0[strlen(string0)-1]!='/')
		{
			do
			{
				numread=fscanf(Fpin,"%s%s",string1,string2);
				if(numread<2)
				{	
					end_of_file=1;
					printf("Hit unexpected end of file!\n");
					if(print==1)
					fprintf(FpPrn,"Hit unexpected end of file!\n");
				}
				len=strlen(string2);
				if(len>100)
				{
					printf("String too long.\n");
					if(print==1)
						fprintf(FpPrn,"String too long.\n");
					exit(0);
				}
				if(i>=MAX_VAL)
				{
					printf("Too many entries in segment %d\n",segs);
					printf("1st Lat = %lf  Lon = %lf\n",Lat[0],Lon[0]);
					if(print==1)
					{
						printf("Too many entries in segment %d\n",segs);
						printf("1st Lat = %lf  Lon = %lf\n",Lat[0],Lon[0]);
					}
					exit(0);
				}
				sscanf(string1,"%lf",Lat+i);
				sscanf(string2,"%lf",Lon+i);
				i+=1;
			}while(string2[strlen(string2)-1]!='/'&&end_of_file==0);
			num=i;
			sum+=num;
			printf("Segment %3d (attrib %3d) contains %3d elements. ",segs,n,num);
			fprintf(Fpout,"%5d\n%10.6lf %11.6lf",n,Lat[0],Lon[0]);
			j=1;
			il=0;
			coslat=cos(Lat[0]*DEG_RAD);
			coslat*=coslat;
			m=1;
			for(i=1;i<num-1;i++)
			{
				dist=(Lat[i]-Lat[il])*(Lat[i]-Lat[il])+
						(Lon[i]-Lon[il])*(Lon[i]-Lon[il])/coslat;
				if(dist>mindist&&dist<MAX_DIST)
				{
					il=i;
					if(m%3==0)
						fprintf(Fpout,"\n");
					else
						fprintf(Fpout,"  ");
					fprintf(Fpout,"%10.6lf %11.6lf",Lat[i],Lon[i]);
					j+=1;
					m+=1;
					if(CutLong>0&&(num-i)>25&&m%CutLong==0)
					{	
						printf("cutting at %d entries\n",j);
						fprintf(Fpout,"//\n%5d\n%10.6lf %11.6lf",n,Lat[i],Lon[i]);
						m=1;
					}
				}
			}
			if(m%3==0)
				fprintf(Fpout,"\n");
			else
				fprintf(Fpout,"  ");
			fprintf(Fpout,"%10.6lf %11.6lf//\n",Lat[num-1],Lon[num-1]);
			printf("%3d after filter.\n",j+1);
			used+=j+1;
		}
	}
	if(string2[strlen(string2)-1]!='/')
		fprintf(Fpout,"//\n\n");
	else
		fprintf(Fpout,"\n\n");
	printf("%d segments:  ",segs);
	printf("Filtered file is %0.2lf percent of original.\n",used/sum*100.0);
	if(print==1)
	{
		fprintf(FpPrn,"%s",filename);
		fprintf(FpPrn,"%d segments:  ",segs);
		fprintf(FpPrn,"Filtered file is %0.2lf percent of original.\n",
			used/sum*100.0);
	}
	fclose(FpPrn);
	fclose(Fpout);
	fclose(Fpin);
}



                                       