/*
	linefix.c

	lf1.bat = cl /AL linefix.c vmaputil sketmast mmserial vimage3 graphlib

	reads a map file and allows you to edit '<name>.bin' file.

	Output goes into '<name>.raw' so rename old one or you will 
	lose it.  You can also convert the raw file before exiting.

*/
#include <stdio.h>
#include <string.h>
#include "font.h"

/* getfile stuff */

#define MAX_NUM 300
#define S_SIZE 40
#define MAX_X 640

	extern char GF_String[MAX_NUM][S_SIZE];

/* display stuff */

#define NUM_TYPE 25
#define NUM_SIZE 25
#define NUM_PSIZE 3
#define NUM_FILL  5
#define NUM_SYM  25
#define MAX_COLORS 16
#define B_LINES 48
#define STR_LEN 100

	unsigned char MT_Buff[12][MAX_X],Mbuff[B_LINES][MAX_X],Buffer[MAX_X];
	int FontSize=2;
	char VideoType='X';
	int ScreenXs,ScreenYs;
	int BottomHigh=96;
	int FullScreenXs,FullScreenYs;
	struct board
	{
		int row,col,color;
	}B_Id;
	struct   Color 
	{ 
		unsigned char  r, g, b; 
	}Lut[256];

/* VGA color stuff */

#define BLACK      0
#define D_GREY     1
#define M_GREY     2
#define L_GREY     3
#define WHITE      4
#define RED        5
#define ORANGE     6
#define YELLOW     7
#define GREEN      8
#define CYAN       9
#define BLUE      10
#define PURPLE    11
#define MAGENTA   12
#define BROWN     13
#define D_BLUE    14
#define BUFF      15

	struct Color Vcolor[16]=
	{
		{   0,  0,  0},
		{  46, 46, 46},
		{ 128,128,128},
		{ 191,191,191},
		{ 255,255,255},
		{ 255,  0,  0},
		{ 255,128,  0},
		{ 191,191,  0},
		{   0,255,  0},
		{   0,191,191},
		{  40, 40,255},
		{ 128,  0,255},
		{ 191,  0,191},
		{  96, 28, 14},
		{   0, 20, 60},
		{ 105, 90, 75}
	};

/* map stuff */

#define MIN_PIX  0.000
#define SYM_SIZE 11

	char T_Menu_L[NUM_TYPE+3][2][50];
	int Sym_Size=SYM_SIZE;
	struct symbol_name
	{
		char name[50],filename[50];
		float max_pix,min_pix;
		int color;
		char on;
		char Symbol[SYM_SIZE][SYM_SIZE];
	}PlaceFile[NUM_SYM];
	char String[STR_LEN];
	struct map
	{
		int KeyPlotted;
		char Fill_Name[256][20];
		int Fill_In;
		int Fill_After;
		int Fill_Color[256][2];
		int Max_Fill;
		int Fill_Color_RGB[256][3];
		float Fill_Color_IHS[256][3];
		int Fill_Order[256];
		int Comp_Color[256];
		int Num_Fill;
		float FI_min[NUM_FILL],FI_max[NUM_FILL];
		char Fill_In_Name[NUM_FILL][STR_LEN];
		int White,Num_Color;
		int screen_xs,screen_ys;
		int Num_Type,Out;
		float Pixsize,Clat,Clon,Space;
		int Mback,Text,High,Back,Grat;
		int GratOn;
		int LatLon,Black;
		char Color_Name[MAX_COLORS][30];
		int Color_Val[MAX_COLORS][3];
		int Type;
		int Ns[NUM_TYPE];
		int Mm[NUM_TYPE];
		int NumSymbol;
		int Dummy1;
		int Dummy2;
		char Group_Name[NUM_TYPE][30];
		struct line_name
		{
			char filename[54];
			float max_pix,min_pix;
			int color;
		}MapFile[NUM_TYPE][NUM_SIZE];
		struct place_name
		{
			char *name,*filename;
			float max_pix,min_pix;
			int color;
		}PlaceFile[3][3];	/* size = 18*3*3 = 162 bytes */
		struct Color lut[256];
	}M;
	unsigned char getpt();
	struct map_input
	{
		char *name,type;
		double indat[6];
	}miv;
	struct map_screen
	{
		int xc,yc,xs,ys;
	}map_pos;
	float MaxLat,MinLat,MaxLon,MinLon;
	
/* other stuff */

#define ZOOM_NUM  100
#define MAX_SEG 10000
#define MAX_EDIT  500
#define MAX_PTS  1000

	int Rxc,Ryc,Repeat;		/* multiple screens if Repeat>1 */
	int Mouse=0;
	int NumSec;		/*  number of active lines */
	int NumSeg;		/*  number of lines in 'linefix.tmp */
	int E[MAX_SEG],TmpNum;
	long TmpOffset[MAX_EDIT];
	struct line
	{
		int numpt;
		float mmll[4],latlon[MAX_PTS][2];
		long attrib;
	};
	struct line L1,L2;
	char Cut[MAX_PTS];
	float ZoomData[ZOOM_NUM][3];
	int Z=0;
	int Edit,BackF[625],NumBackF;
	FILE *Fptmp,*Fpout;
	int NumChange=0;	/* number of lines changed */	
	char Cr=0xd;
	int Cursor=1;
	int Shift=0;
	double DelLat=0.0,DelLon=0.0;
	int Digit=0;		/* 1 if Sketch Master (GTCO) 12 x 18 tablet attached */
	char Null[10];

char Zoom();
char m_getpad();
int prune(int *cx,int *cy,int *cxs,int *cys,int *cspeed);

main(argc,argv)

int argc;
char *argv[];

{
	int i,j,k;
	char ans;
	char cfile[100],string[100],name[100];
	int val,num;

	cfile[0]='\0';
	if(argc>1)
	{
		for(k=1;k<argc;k++)
		{
			if(argv[k][0]=='/'&&argv[k][1]=='d')
				Digit=1;
			if(argv[k][0]=='/'&&argv[k][1]=='a')
			{
				strcpy(cfile,argv[k+1]);
				for(k=0;k<strlen(cfile);k++)
					if(cfile[k]=='.')
						cfile[k]='\0';
				strcat(cfile,".map");
			}
		}
	}
	if(Digit==1)
		init_board(1,Null);
	if(cfile[0]==0)
	{
		if(get_file_name("*.map",cfile)<0)
		{
			printf("No maps chosen.\n\n");
			exit(0);
		}
	}
	printf("     You must first choose a line\n");
	printf("to edit.  Move the bar to the line\n");
	printf("you wish to edit and hit 'Enter'.\n");
	printf("\n");
	printf("     Next you may choose any number\n");
	printf("of background lines.  These lines\n");
	printf("will be plotted in grey on the\n");
	printf("screen for reference but cannot be\n");
	printf("edited.  You are not required to\n");
	printf("choose any background lines.\n");
	printf("\n");
	printf("	When you have chosen all of the\n");
	printf("lines you wish to work with hit\n");
	printf("'Esc' to begin editing.  Available\n");
	printf("options will be printed at the\n");
	printf("bottom of the screen with immediate\n");
	printf("instructions in a red field.\n");
	printf("\n");
	printf("     Use 'x' to exit the program. \n");
	printf("If any changes have been made and\n");
	printf("saved you will be asked if you wish\n");
	printf("to save them permanently.\n");
	printf("Working '%s'\n\n",cfile);
	val=read_control(cfile);
	if(val<0)
	{
		if(val==-1)
			printf("Could not open control file '%s'\n\n",cfile);
		if(val==-2)
			printf("Could not find 'begin_color in file '%s'\n\n",cfile);
		if(val==-3)
			printf("Could not find 'begin_vector in file '%s'\n\n",cfile);
		if(val==-4)
			printf("Too many vectors in a group in file '%s'\n\n",cfile);
		if(val==-5)
			printf("Bad color name in file '%s'\n\n",cfile);
		if(val==-6)
			printf
	("Missing or bad pixel size, central lat and lon or graticule spacing\n");
			exit(0);
	}
	k=0;
	for(i=0;i<M.Num_Type;i++)
	{
		for(j=0;j<M.Ns[i];j++)
			strcpy(GF_String[k++],M.MapFile[i][j].filename);
	}
	Fptmp=fopen("linefix.tmp","w+b");
	if(!Fptmp)
	{
		printf("Could not open temp file to write.\n\n");
		exit(0);
	}
	Fpout=fopen("linefix.out","wb");
	if(!Fpout)
	{
		printf("Could not open scratch file to write.\n\n");
		exit(0);
	}
	WriteStringAtxy(40,0,39,(char)112," Choose Line to Edit");
	Edit=scroll_list(40,1,40,21,GF_String,k,string);
	if(Edit<0)
		exit(0);
	printf("\n\nEditing '%s'\n\n",string);
	ClearBox(40,79,0,0,112);
	WriteStringAtxy(40,0,39,(char)112," Choose a Background line");
	do
	{
		ClearBox(40,79,0,0,112);
		WriteStringAtxy(40,0,39,(char)112," Choose a Background line");
		val=scroll_list(40,1,40,21,GF_String,k,string);
		if(val>=0)
		{
			BackF[NumBackF++]=val;
			printf("Background %3d  '%s'\n",NumBackF,string);
		}
	}while(val>=0);
	printf("\n\nEditing ------- '%s'\n",GF_String[Edit]);
	for(i=0;i<NumBackF;i++)
		printf("Background %3d  '%s'\n",i+1,GF_String[BackF[i]]);
	edit();
	if(NumChange>0)	/* changes were made to GF_String[Edit] */
	{
		printf("\n\nDo you wish to keep changes?  (y or n)\n\n");
		if(getch()=='n')
		{
			printf("No changes or entries made to file '%s'.raw.\n\n",
				GF_String[Edit]);
		}
		else
		{
			strcpy(name,GF_String[Edit]);
			for(k=0;k<strlen(name);k++)
				if(name[k]=='.')
					name[k]='\0';
			printf("Now set to save output into '%s.raw'.\n\n",name);
			printf("Do you wish to change the file name?  (y or n)\n");
			if(getch()=='y')
			{
				printf("Give new file name without extension.\n\n");
				scanf("%s",name);
				for(k=0;k<strlen(name);k++)
					if(name[k]=='.')
						name[k]='\0';
			}
			save_raw(name);
			for(k=0;k<strlen(name);k++)
				if(name[k]=='.')
					name[k]='\0';
			printf(
	"Do you want to process '%s.raw' into binary files?  (y or n)\n\n",name);
			if(getch()=='y')
			{
				fcloseall();
				run_reorder(name);
				printf("Could not run 'reorder'\n\n");
				printf("Try running by entering following command line:\n\n");
				printf("     reorder %s p\n\n",name);
			}
			else
			{
				printf("To process files type:\n\n");
				printf("     reorder %s p\n\n",name);
			}
		}		
	}
	fclose(Fptmp);
}

/***********************************************************************
**
**
**
**
************************************************************************/

char m_getpad(size,x,y,speed,iop)

int *size,*x,*y;
int *speed,iop;

{
	char res='q',ans;
	int lx,ly,ret;


	if(Digit==0)
		ans=getpad(size,x,y,speed,iop);
	else
	{
		ret=mouse_a_point(x,y,res,&lx,&ly,WHITE);
		if(iop==0)
		{
			if(ret==1)
				ans=13;
			if(ret==3)
				ans=27;
		}
		else if(iop==1)
		{
			if(ret==1)
				ans='m';
			if(ret==2)
				ans='k';
			if(ret==3)
				ans=27;
			if(ret==4)
				ans='s';
		}
	}
	return(ans);
}


/***********************************************************************
**
**
**
**
************************************************************************/

int save_raw(name)

char *name;

{
	int i,j,k;
	char string[100],region[100];
	FILE *fpraw,*fpbin,*fphdr,*fpatr,*fpdel;
	int atr=0,numsec,numpt,numplot_bin=0,numplot_change=0,num_delete=0;
	long offset,attrib;
	float mll[4],latlon[2];
	int delete=0;

	for(j=0;j<NumSec;j++)
		if(E[j]==-3)
			delete+=1;
	if(delete>0)
	{
		fpdel=fopen("linefix.raw","wt");
		if(fpdel)
			printf("Deleted line going into 'linefix.raw'\n\n");
		else
			printf(
"Could not open 'linefix.raw' to write.  Deleted lines will be lost.\n\n");
	}
	sprintf(string,"%s.raw",name);
	fpraw=fopen(string,"rt");
	if(fpraw)
	{
		printf(
"\n\nFile '%s' already exits.  Do you want to overwrite it?  (y or n)\n\n",
			string);
		if(getch()!='y')
		{
			printf("Give new file name.\n");
			scanf("%s",name);
			for(k=0;k<strlen(name);k++)
				if(name[k]=='.')
					name[k]='\0';
			sprintf(string,"%s.raw",name);
		}
		fclose(fpraw);
	}
	fpraw=fopen(string,"wt");
	if(!fpraw)
	{
		printf(
"Could not open '%s' to write.  Quiting -- changes will be lost.\n\n",string);
		
		return(-1);
	}
	strcpy(name,string);
	strcpy(region,GF_String[Edit]);
	strcpy(string,region);
	strcat(string,".bin");
	fpbin=fopen(string,"rb");
	if(!fpbin)
		return(-1);
	strcpy(string,region);
	strcat(string,".hdr");
	fphdr=fopen(string,"rb");
	if(!fphdr)
	{
		fclose(fphdr);
		return(-1);
	}
	strcpy(string,region);
	strcat(string,".atr");
	fpatr=fopen(string,"rb");
	if(fpatr)
		atr=1;
	else
		printf("\nNo attribute file found.\n");
	fread((char *)&numsec,sizeof(int),1,fphdr);
	for(j=0;j<NumSec;j++)
	{
						/* read header file entry */
		fread((char *)&offset,sizeof(long),1,fphdr);
		fread((char *)mll,sizeof(float),4,fphdr);
		if(atr==1)
			fread((char *)&attrib,sizeof(long),1,fpatr);
		else
			attrib=j+1;
		if(E[j]>=0)   /* line is redone read new data from '.tmp' */
		{
			offset=TmpOffset[E[j]];
			fseek(Fptmp,offset,SEEK_SET);
			fread((char *)&attrib,sizeof(long),1,Fptmp);
			fread((char *)&numpt,sizeof(int),1,Fptmp);
			fread((char *)mll,sizeof(float),4,Fptmp);
			numplot_change+=1;
		}
		else if(E[j]>-3)		/* line not deleted read '.bin' */
		{
			fseek(fpbin,offset,SEEK_SET);
			fread((char *)&numpt,sizeof(int),1,fpbin);
			fread((char *)mll,sizeof(float),4,fpbin);
			numplot_bin+=1;
		}
		if(E[j]==-3)	/* line deleted */
		{
			num_delete+=1;
			if(fpdel)
			{
				fseek(fpbin,offset,SEEK_SET);
				fread((char *)&numpt,sizeof(int),1,fpbin);
				fread((char *)mll,sizeof(float),4,fpbin);

				fprintf(fpdel,"%6ld",attrib);
				for(i=0;i<numpt;i++)
				{
					fread((char *)latlon,sizeof(float),2,fpbin);
					if(Shift>0)
					{
						latlon[0]+=DelLat;
						latlon[1]+=DelLon;
					}
					if(i%3==0)
						fprintf(fpdel,"\n");
					else
						fprintf(fpdel,"  ");
					fprintf(fpdel,"%10.6f %11.6f",latlon[0],latlon[1]);
				}
				fprintf(fpdel,"//\n");
			}
		}
		else		/* output line */
		{
			fprintf(fpraw,"%6ld",attrib);
			for(i=0;i<numpt;i++)
			{
				if(E[j]>=0)
					fread((char *)latlon,sizeof(float),2,Fptmp);
				else
					fread((char *)latlon,sizeof(float),2,fpbin);
				if(Shift>0)
				{
					latlon[0]+=DelLat;
					latlon[1]+=DelLon;
				}
				if(i%3==0)
					fprintf(fpraw,"\n");
				else
					fprintf(fpraw,"  ");
				fprintf(fpraw,"%10.6f %11.6f",latlon[0],latlon[1]);
			}
			fprintf(fpraw,"//\n");
		}
		printf("%c%d + %d + %d = %d of %d",Cr,
			numplot_bin,numplot_change,num_delete,
			numplot_bin+numplot_change+num_delete,NumSec);
	}
	printf(
"\nPrinted %d lines into '%s'-- %d line(s) changed and %d deleted.\n\n",
		numplot_bin+numplot_change,name,numplot_change,num_delete);
	fprintf(fpraw,"\n\n");
	fclose(fpbin);
	fclose(fpatr);
	fclose(fphdr);
	fclose(fpraw);
}


/***********************************************************************
**
**
**
**
************************************************************************/

int edit()

{
	char ans;
	float pixsize=12.0,clat=38.0,clon=-95.75,space=10.0;
	int xc=0,yc=0,xs=ScreenXs,ys=ScreenYs,ix,iy,speed,isize=FontSize,color;
	int cx,cy,cspeed=16,bxs=16,bys=16;
	long seg_offset;

	Z=0;
	ZoomData[Z][0]=pixsize=M.Pixsize;
	ZoomData[Z][1]=clat=M.Clat;
	ZoomData[Z][2]=clon=M.Clon;
	space=M.Space;	
	video_on();
	set_lut('v',0);
	cx=ScreenXs/2;
	cy=ScreenYs/2;
	plot_footer(1);
	plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,1);
	do
	{
		message("choose option",BROWN,L_GREY);
		ans=getch();
		unmessage("choose option",BROWN,L_GREY);
		if(ans=='P')
			prune(&cx,&cy,&bxs,&bys,&cspeed);
		if(ans=='L')
			get_location(&cx,&cy,&cspeed);
		if(ans=='e')		/* (e)dit line  */
		{
			line_edit(&cx,&cy);
			plot_footer(1);
		}
		if(ans=='c')
		{
			plot_footer(7);
			cut_line(&cx,&cy);
			plot_footer(1);
		}
		if(ans=='a')
		{
			add_line(&cx,&cy);
			plot_footer(1);
		}
		if(Z<ZOOM_NUM-1&&ans=='z')
		{
			pixsize=ZoomData[Z][0];
			clat=ZoomData[Z][1];
			clon=ZoomData[Z][2];
			plot_footer(4);
			ans=Zoom(xc,yc,ScreenXs,ScreenYs,&pixsize,&clat,&clon,&space);
			plot_footer(1);
			if(ans!=27)
			{
				Z+=1;
				ZoomData[Z][0]=pixsize;
				ZoomData[Z][1]=clat;
				ZoomData[Z][2]=clon;
				plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
			}
		}
		if(ans=='R')
		{
			pixsize=ZoomData[Z][0];
			clat=ZoomData[Z][1];
			clon=ZoomData[Z][2];
			plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
		}
		if(ans=='S')
		{
			pixsize=ZoomData[Z][0];
			clat=ZoomData[Z][1];
			clon=ZoomData[Z][2];
			if(Shift>0)
			{
				DelLat=DelLon=0.0;
				plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
			}
			plot_footer(5);
			shift_lines(&cx,&cy);
			plot_footer(1);
			plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
		}
		if(Z>0&&ans=='r')
		{
			Z=0;
			pixsize=ZoomData[Z][0];
			clat=ZoomData[Z][1];
			clon=ZoomData[Z][2];
			plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
		}
		if(Z>0&&ans=='l')
		{
			Z-=1;
			pixsize=ZoomData[Z][0];
			clat=ZoomData[Z][1];
			clon=ZoomData[Z][2];
			plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
		}
	}while(ans!='x');
	video_off();
}

/***********************************************************************
**
**
**
**
************************************************************************/

int prune(int *cx,int *cy,int *cxs,int *cys,int *cspeed)

{
	int i,j,k;
	int xc=*cx,yc=*cy,xs=*cxs,ys=*cys,speed=*cspeed;
	char string[100];
	FILE *fphdr;
	int numsec,numpt,num=0,numsec0,doit;
	double maxlat,minlat,maxlon,minlon,x,y,lat,lon;
	float mll[4],mnln,mxln,mnlt,mxlt;

	long offset,attrib;
	limit_area_all(&xc,&yc,&xs,&ys,&speed,ScreenXs,ScreenYs,
		1,MT_Buff,WHITE,WHITE,128);
	x=xc+xs;
	y=yc;
	xy_ll(x,y,&maxlat,&maxlon);
	x=xc;
	y=yc+ys;
	xy_ll(x,y,&minlat,&minlon);
	sprintf(string,"%s.hdr",GF_String[Edit]);
	fphdr=fopen(string,"rb");
	if(!fphdr)
		return(-1);
	fread((char *)&numsec,sizeof(int),1,fphdr);
	numsec0=numsec;
	numsec=NumSec;
	sprintf(string,"working %d segments (%d are original)",numsec,numsec0);
	message(string,BROWN,L_GREY);
	for(j=0;j<numsec;j++)
	{
		doit=0;
		if(j<numsec0)
		{
			fread((char *)&offset,sizeof(long),1,fphdr);
			fread((char *)mll,sizeof(float),4,fphdr);
			doit=1;
		}
		if(E[j]>=0)   /* if line is redone read new data from '.tmp' */
		{
			offset=TmpOffset[E[j]];
			fseek(Fptmp,offset,SEEK_SET);
			fread((char *)&attrib,sizeof(long),1,Fptmp);
			fread((char *)&numpt,sizeof(int),1,Fptmp);
			fread((char *)mll,sizeof(float),4,Fptmp);
			doit=1;
		}
		if(doit==1)
		{
			mnln=mll[0];mxln=mll[1];
			mnlt=mll[2];mxlt=mll[3];
			if(E[j]!=-3&&mnln>=minlon&&mxln<=maxlon&&mnlt>=minlat&&mxlt<=maxlat)
			{
				plot_segment(&L1,BUFF,BUFF,GF_String[Edit],offset,j,0);
				unmessage(string,BROWN,L_GREY);
				sprintf(string,"%d deleted segment %d",++num,j+1);
				message(string,BROWN,L_GREY);
				E[j]=-3;	/* marked as deleted */
				NumChange+=1;
			}
		}
	}
	*cx=xc;
	*cy=yc;
	*cspeed=speed;
	*cxs=xs;
	*cys=ys;
	fclose(fphdr);
	unmessage(string,BROWN,L_GREY);
	return(num);
}

/***********************************************************************
**
**
**
**
************************************************************************/

int shift_lines(cx,cy)

int *cx,*cy;

{
	int i,j,k;
	char ans,string[100],ans2;
	int speed=16,size=5;	
	int ixo=*cx,iyo=*cy,pxo,pyo;
	double x,y,lat1,lon1,lat2,lon2;
	int segnum=NumSec;
	long offset=0,seg_offset=0;

	message("Move to point on yellow line & hit <Enter> <Esc> to quit"
		,BROWN,L_GREY);
	if(Digit==0)
	{
		tcursor(0,*cx,*cy,size,Buffer);
		cursor(0,*cx,*cy,WHITE,size);
	}
	while((ans=m_getpad(&size,cx,cy,&speed,0))!=27&&ans!=13)
	{
		uncursor(0,ixo,iyo,size,Buffer);
		ixo=*cx;iyo=*cy;
		tcursor(0,*cx,*cy,size,Buffer);
		cursor(0,*cx,*cy,WHITE,5);
	}
	if(Digit==0)
	{
		uncursor(0,ixo,iyo,size,Buffer);
		tcursor(0,*cx,*cy,size,Buffer);
		cursor(0,*cx,*cy,ORANGE,size);
	}
	unmessage("Move to point on yellow line & hit <Enter> <Esc> to quit"
		,BROWN,L_GREY);
	if(ans==27)
	{
		if(Digit==0)
			uncursor(0,*cx,*cy,size,Buffer);
		return(-1);
	}
	x=pxo=*cx;y=pyo=*cy;
	xy_ll(x,y,&lat1,&lon1);

	message("Move to new point on grey line & hit <Enter> <Esc> to quit"
		,BROWN,L_GREY);
	if(Digit==0)
	{
		tcursor(0,*cx,*cy,size,Buffer);
		cursor(0,*cx,*cy,WHITE,size);
	}
	while((ans=m_getpad(&size,cx,cy,&speed,0))!=27&&ans!=13)
	{
		uncursor(0,ixo,iyo,size,Buffer);
		ixo=*cx;iyo=*cy;
		tcursor(0,*cx,*cy,size,Buffer);
		cursor(0,*cx,*cy,WHITE,5);
	}
	if(Digit==0)
	{
		uncursor(0,ixo,iyo,size,Buffer);
		tcursor(0,*cx,*cy,size,Buffer);
		cursor(0,*cx,*cy,ORANGE,size);
	}
	unmessage("Move to new point on grey line & hit <Enter> <Esc> to quit"
		,BROWN,L_GREY);
	if(ans==27)
	{
		if(Digit==0)
			uncursor(0,*cx,*cy,size,Buffer);
		return(-1);
	}
	x=pxo=*cx;y=pyo=*cy;
	xy_ll(x,y,&lat2,&lon2);
	Shift+=1;
	NumChange+=1;
	DelLat=lat2-lat1;
	DelLon=lon2-lon1;
}

/***********************************************************************
**
**
**
**
************************************************************************/

int cut_line(int *cx,int *cy)

{
	int i,j,k;
	int segnum=-10;
	long seg_offset,offset=0;
	char string[100],ans,ans2;
	float pixsize=12.0,clat=38.0,clon=-95.75,space=10.0;
	double x,y,lat,lon,curlat,curlon,mindist,dist;
	int xc=0,yc=0,xs=ScreenXs,ys=ScreenYs,ix,iy,speed=1,isize=FontSize,color;
	int cxo,cyo,cspeed=16,px1,py1,px2,py2,p1,p2,pxo,pyo,point,csize=5;
	int num_cut=0,num,numpt,size=5;

	for(i=0;i<L1.numpt;i++)
		Cut[i]=0;
	message("Move near line & hit <Enter>",BROWN,L_GREY);
	segnum=find_segment(RED,GF_String[Edit],&seg_offset,cx,cy);
	if(segnum<0)
	{
		video_off();
		printf("find_segment() failed\n");
		exit(0);
	}
	if(segnum>=0)
		plot_segment(&L1,RED,WHITE,GF_String[Edit],seg_offset,segnum,0);
	else
	{
		video_off();
		printf("plot_segment() failed\n");
		exit(0);
	}
	unmessage("Move near line & hit <Enter>",BROWN,L_GREY);
	do
	{
		sprintf(string,"line number %d  attribute %ld:   CHOOSE OPTION:",
			segnum+1,L1.attrib);
		message(string,BROWN,L_GREY);
		ans=getpad(&csize,cx,cy,&cspeed,0);
		unmessage(string,BROWN,L_GREY);
		if(Z<ZOOM_NUM-1&&ans=='z')
		{
			pixsize=ZoomData[Z][0];
			clat=ZoomData[Z][1];
			clon=ZoomData[Z][2];
			plot_footer(4);
			ans=Zoom(xc,yc,ScreenXs,ScreenYs,&pixsize,&clat,&clon,&space);
			plot_footer(2);
			if(ans!=27)
			{
				Z+=1;
				ZoomData[Z][0]=pixsize;
				ZoomData[Z][1]=clat;
				ZoomData[Z][2]=clon;
				plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
				if(segnum>=0)
					replot_segment(&L1,RED,WHITE,GF_String[Edit],seg_offset,segnum);
			}
			plot_footer(7);
		}
		if(Digit==0&&ans=='c')		/*  cut at a point -- KeyBoard*/
		{
			cspeed=16;
			message("Move near point to cut & hit <Enter>",BROWN,L_GREY);
			get_xy(cx,cy,&cspeed);
			unmessage("Move near point to cut & hit <Enter>",BROWN,L_GREY);
			x=*cx;y=*cy;
			xy_ll(x,y,&curlat,&curlon);
			mindist=100000.0;
			point=0;		/* find the closest point */
			for(i=0;i<L1.numpt;i++)
			{
				lat=L1.latlon[i][0];
				lon=L1.latlon[i][1];
				dist=(curlat-lat)*(curlat-lat)+(curlon-lon)*(curlon-lon);
				if(dist<mindist)
				{
					mindist=dist;
 					point=i;
				}
			}
			lat=L1.latlon[point][0];
			lon=L1.latlon[point][1];
			ll_xy(&x,&y,lat,lon);
			*cx=x;*cy=y;
			tcursor(0,*cx,*cy,size,Buffer);
			cursor(0,*cx,*cy,WHITE,size);
			if(point==0||point==L1.numpt-1||Cut[point]!=0) /* end point */
			{															  /* or cut */
				uncursor(0,*cx,*cy,size,Buffer);
				if(point==0||point==L1.numpt-1)
					sprintf(string,"End Point -- Cannot Cut  'HIT ANY KEY'");
				else
					sprintf(string,"Point Already Cut  'HIT ANY KEY'");
				message(string,BROWN,L_GREY);
				getch();
				unmessage(string,BROWN,L_GREY);
			}
			if(point>0&&point<L1.numpt-1&&	/* not an end point */
				Cut[point]==0)						/* and not already cut */
			{
				sprintf(string,"hit <Enter> to cut <Esc> to ignor");
				message(string,BROWN,L_GREY);
				ans2=getch();
				if(ans2==13)
				{
					Cut[point]=1;
					cursor(0,*cx,*cy,WHITE,7);
					num_cut+=1;
				}
				else
					uncursor(0,*cx,*cy,size,Buffer);
				unmessage(string,BROWN,L_GREY);
			}
		}
	}while(ans!='x'&&ans!=27);
	if(ans==27||num_cut==0)
		return(0);
	k=0;j=1;num=1;
	for(i=0;i<num_cut+1;i++)
	{
		while(Cut[j]==0&&j<L1.numpt)
		{
			j+=1;
			num+=1;
		}
		while(j>=L1.numpt)
		{
			j-=1;
			num-=1;
		}	
		numpt=num+1;
		fseek(Fptmp,offset,SEEK_END);
		E[NumSec++]=NumSeg;
		TmpOffset[NumSeg++]=ftell(Fptmp);
		fwrite((char *)&L1.attrib,sizeof(long),1,Fptmp);
		fwrite((char *)&numpt,sizeof(int),1,Fptmp);
		fwrite((char *)L1.mmll,sizeof(float),4,Fptmp);
		fwrite((char *)L1.latlon[k],sizeof(float),numpt*2,Fptmp);
		j+=1;
		k+=num;
		num=1;
	}
	fflush(Fptmp);
	replot_segment(&L1,GREEN,M_GREY,GF_String[Edit],seg_offset,segnum);
	E[segnum]=-3;	/* mark original line as deleted */
	NumChange+=1;
	return(1);
}




/***********************************************************************
**
**
**
**
************************************************************************/

int add_line(cx,cy)

int *cx,*cy;

{
	int i,j,k;
	char ans,string[100],ans2;
	int speed=16,size=5;	
	int ixo=*cx,iyo=*cy,pxo,pyo;
	double x,y,lat,lon;
	int segnum=NumSec;
	long offset=0,seg_offset=0;

	plot_footer(3);
	message("Move to start of line & hit <Enter> [<Esc> to quit",BROWN,L_GREY);
	i=0;
	tcursor(0,*cx,*cy,size,Buffer);
	cursor(0,*cx,*cy,WHITE,size);
	while((ans=m_getpad(&size,cx,cy,&speed,0))!=27&&ans!=13)
	{
		uncursor(0,ixo,iyo,size,Buffer);
		ixo=*cx;iyo=*cy;
		tcursor(0,*cx,*cy,size,Buffer);
		cursor(0,*cx,*cy,WHITE,5);
	}
	uncursor(0,ixo,iyo,size,Buffer);
	tcursor(0,*cx,*cy,size,Buffer);
	cursor(0,*cx,*cy,ORANGE,size);
	unmessage("Move to start of line & hit <Enter> [<Esc> to quit",BROWN,L_GREY);
	if(ans==27)
	{
		uncursor(0,*cx,*cy,size,Buffer);
		return(-1);
	}
	x=pxo=*cx;y=pyo=*cy;
	xy_ll(x,y,&lat,&lon);
	L1.latlon[i][0]=lat;
	L1.latlon[i++][1]=lon;
	sprintf(string,"Move to point %d & hit <Enter> or option",i+1);
	message(string,BROWN,L_GREY);
	tplotln_inv_dot(0,pxo,pyo,*cx,*cy,Mbuff[0],ScreenXs);
	do
	{
		ans=getpad(&size,cx,cy,&speed,0);
		uncursor(0,ixo,iyo,size,Buffer);
		unplotln(0,pxo,pyo,ixo,iyo,Mbuff[0],ScreenXs);
		tplotln_inv_dot(0,pxo,pyo,*cx,*cy,Mbuff[0],ScreenXs);
		plotln(0,pxo,pyo,*cx,*cy,WHITE);
		if(ans==13)		/* plot a point */
		{
			unmessage(string,BROWN,L_GREY);
			plotln(0,pxo,pyo,*cx,*cy,RED);
			plotpt(0,pxo,pyo,WHITE);
			x=pxo=*cx;y=pyo=*cy;
			xy_ll(x+0.5,y+0.5,&lat,&lon);
			L1.latlon[i][0]=lat;
			L1.latlon[i++][1]=lon;
			sprintf(string,"Move to point %d & hit <Enter> or option",i+1);
			message(string,BROWN,L_GREY);
		}
		if(ans=='e'||ans=='c')
		{
			if(ans=='c')	/* close polygon */
			{
				L1.latlon[i][0]=L1.latlon[0][0];
				L1.latlon[i++][1]=L1.latlon[0][1];
			}
			L1.numpt=i;
			L1.attrib=9999;
			fseek(Fptmp,offset,SEEK_END);
			E[NumSec++]=NumSeg;
			TmpOffset[NumSeg++]=ftell(Fptmp);
			fwrite((char *)&L1.attrib,sizeof(long),1,Fptmp);
			fwrite((char *)&L1.numpt,sizeof(int),1,Fptmp);
			set_mmll(L1.latlon,L1.mmll,L1.numpt);
			fwrite((char *)L1.mmll,sizeof(float),4,Fptmp);
			fwrite((char *)L1.latlon,sizeof(float),L1.numpt*2,Fptmp);
			fflush(Fptmp);
			replot_segment(&L1,ORANGE,M_GREY,GF_String[Edit],seg_offset,segnum);
			NumChange+=1;
			return(1);
		}
		if(ans=='x'||ans==27)
			replot_segment(&L1,BUFF,BUFF,GF_String[Edit],seg_offset,segnum);
		ixo=*cx;iyo=*cy;
		tcursor(0,*cx,*cy,size,Buffer);
		cursor(0,*cx,*cy,ORANGE,size);
	}while(ans!='x');
	uncursor(0,*cx,*cy,size,Buffer);
}

/***********************************************************************
**
**
**
**
************************************************************************/

int set_mmll(latlon,mmll,numpt)

float *latlon,*mmll;
int numpt;

{
	int i,j,k;
	float maxlat=-100.0,minlat=100.0,maxlon=-400.0,minlon=400.0;
	float lat,lon;

	for(i=0;i<numpt;i++)
	{
		lat=latlon[i*2];
		lon=latlon[i*2+1];
		if(lat>maxlat)
			maxlat=lat;
		if(lat<minlat)
			minlat=lat;
		if(lon>maxlon)
			maxlon=lon;
		if(lon<minlon)
			minlon=lon;
		mmll[0]=minlon;
		mmll[1]=maxlon;
		mmll[2]=minlat;
		mmll[3]=maxlat;
	}
}




/***********************************************************************
**
**
**
**
************************************************************************/

int line_edit(e_cx,e_cy)

int *e_cx,*e_cy;

{
	int i,j,k;
	char ans,string[100],ans2;
	float pixsize=12.0,clat=38.0,clon=-95.75,space=10.0;
	int xc=0,yc=0,xs=ScreenXs,ys=ScreenYs,ix,iy,speed=1,isize=FontSize,color;
	int cx,cy,cxo,cyo,cspeed=16,px1,py1,px2,py2,p1,p2,pxo,pyo;
	int segnum=-10,point,size=5,n1,n2;
	long seg_offset,offset=0;
	double x,y,lat,lon,curlat,curlon,mindist,dist;
	float latp,lonp;

	plot_footer(2);
	cx=*e_cx;
	cy=*e_cy;
	message("Move near line & hit <Enter>",BROWN,L_GREY);
	segnum=find_segment(RED,GF_String[Edit],&seg_offset,&cx,&cy);
	if(segnum<0)
	{
		*e_cx=cx;
		*e_cy=cy;
		unmessage("Move near line & hit <Enter>",BROWN,L_GREY);
		return(0);
	}
	if(segnum>=0)
		plot_segment(&L1,RED,WHITE,GF_String[Edit],seg_offset,segnum,0);
	else
	{
		*e_cx=cx;
		*e_cy=cy;
		unmessage("Move near line & hit <Enter>",BROWN,L_GREY);
		return(0);
	}
	unmessage("Move near line & hit <Enter>",BROWN,L_GREY);
	do
	{
		sprintf(string,"line number %d  attribute %ld:   CHOOSE OPTION:",
			segnum+1,L1.attrib);
		message(string,BROWN,L_GREY);
		ans=getch();
		unmessage(string,BROWN,L_GREY);
		if(Z<ZOOM_NUM-1&&ans=='z')
		{
			pixsize=ZoomData[Z][0];
			clat=ZoomData[Z][1];
			clon=ZoomData[Z][2];
			plot_footer(4);
			ans=Zoom(xc,yc,ScreenXs,ScreenYs,&pixsize,&clat,&clon,&space);
			plot_footer(2);
			if(ans!=27)
			{
				Z+=1;
				ZoomData[Z][0]=pixsize;
				ZoomData[Z][1]=clat;
				ZoomData[Z][2]=clon;
				plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
				if(segnum>=0)
					replot_segment(&L1,RED,WHITE,GF_String[Edit],seg_offset,segnum);
			}
		}
		if(Z>0&&ans=='r')
		{
			Z=0;
			pixsize=ZoomData[Z][0];
			clat=ZoomData[Z][1];
			clon=ZoomData[Z][2];
			plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
			if(segnum>=0)
				replot_segment(&L1,RED,WHITE,GF_String[Edit],seg_offset,segnum);
		}
		if(ans=='R')
		{
			pixsize=ZoomData[Z][0];
			clat=ZoomData[Z][1];
			clon=ZoomData[Z][2];
			plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
			if(segnum>=0)
				replot_segment(&L1,RED,WHITE,GF_String[Edit],seg_offset,segnum);
		}
		if(Z>0&&ans=='l')
		{
			Z-=1;
			pixsize=ZoomData[Z][0];
			clat=ZoomData[Z][1];
			clon=ZoomData[Z][2];
			plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,2);
			if(segnum>=0)
				replot_segment(&L1,RED,WHITE,GF_String[Edit],seg_offset,segnum);
		}
		if(ans=='c')		/*  close polygon (join first and last point */
		{
			n1=0;					/* first point */
			n2=L1.numpt-1;		/* last point  */
			if(L1.latlon[n1][0]!=L1.latlon[n2][0]||		/* lats different or */
				L1.latlon[n1][1]!=L1.latlon[n2][1])			/* lons different */
			{						/* then add first point to end of line */
				L1.numpt+=1;
				L1.latlon[n2+1][0]=L1.latlon[n1][0];
				L1.latlon[n2+1][1]=L1.latlon[n1][1];
									/* plot line section */
				lat=L1.latlon[n1][0];
				lon=L1.latlon[n1][1];
				ll_xy(&x,&y,lat,lon);
				px1=x+0.5;py1=y+0.5;
				lat=L1.latlon[n2][0];
				lon=L1.latlon[n2][1];
				ll_xy(&x,&y,lat,lon);
				px2=x+0.5;py2=y+0.5;
				plotln(0,px1,py1,px2,py2,RED);
				plotpt(0,px1,py1,WHITE);
				plotpt(0,px2,py2,WHITE);
			}
		}
		if(ans=='a')		/*  add points */
		{
			cspeed=16;
			message("Move near center point & hit <Enter>",BROWN,L_GREY);
			get_xy(&cx,&cy,&cspeed);
			unmessage("Move near center point & hit <Enter>",BROWN,L_GREY);
			x=cx;y=cy;
			xy_ll(x,y,&curlat,&curlon);
			mindist=100000.0;
			point=0;		/* find the closest point */
			for(i=0;i<L1.numpt;i++)
			{
				lat=L1.latlon[i][0];
				lon=L1.latlon[i][1];
				dist=(curlat-lat)*(curlat-lat)+(curlon-lon)*(curlon-lon);
				if(dist<mindist)
				{
					mindist=dist;
					point=i;
				}
			}
			replot_segment(&L1,BUFF,BUFF,GF_String[Edit],seg_offset,segnum);
			if(point==L1.numpt-1)	/* last point chosen */
			{
				k=L1.numpt;
				L1.latlon[k][0]=L1.latlon[k-1][0];	/* move last point over 1 */
				L1.latlon[k][1]=L1.latlon[k-1][1];
																/* place point in middle */
				L1.latlon[k-1][0]=(L1.latlon[k-2][0]+L1.latlon[k][0])/2.0;
				L1.latlon[k-1][1]=(L1.latlon[k-2][1]+L1.latlon[k][1])/2.0;
				L1.numpt+=1;
			}
			else if(point==0)	/* first point chosen */
			{
				for(k=L1.numpt;k>1;k--)
				{
					L1.latlon[k][0]=L1.latlon[k-1][0];	/* move points over 1 */
					L1.latlon[k][1]=L1.latlon[k-1][1];
				}
				k=1;											/* place point in middle */
				L1.latlon[k][0]=(L1.latlon[k+1][0]+L1.latlon[k-1][0])/2.0;
				L1.latlon[k][1]=(L1.latlon[k+1][1]+L1.latlon[k-1][1])/2.0;
				L1.numpt+=1;
			}
			else 			/* other point chosen */
			{
				for(k=L1.numpt+1;k>point+2;k--)
				{
					L1.latlon[k][0]=L1.latlon[k-2][0]; /* move top points over 2 */
					L1.latlon[k][1]=L1.latlon[k-2][1];
				}
				k=point+1;
				L1.latlon[k][0]=L1.latlon[k-1][0];	/* move point over 1 */
				L1.latlon[k][1]=L1.latlon[k-1][1];
				k=point+2;							/* place upper point in middle */
				L1.latlon[k][0]=(L1.latlon[k-1][0]+L1.latlon[k+1][0])/2.0;
				L1.latlon[k][1]=(L1.latlon[k-1][1]+L1.latlon[k+1][1])/2.0;
				k=point;								/* place lower point in middle */
				L1.latlon[k][0]=(L1.latlon[k-1][0]+L1.latlon[k+1][0])/2.0;
				L1.latlon[k][1]=(L1.latlon[k-1][1]+L1.latlon[k+1][1])/2.0;
				L1.numpt+=2;
			}
			replot_segment(&L1,RED,WHITE,GF_String[Edit],seg_offset,segnum);
		}
		if(ans=='D')
		{
			replot_segment(&L1,BUFF,BUFF,GF_String[Edit],seg_offset,segnum);
			E[segnum]=-3;	/* marked as deleted */
			*e_cx=cx;
			*e_cy=cy;
			NumChange+=1;
			return(1);
		}
		if(Digit==1&&ans=='m')		/*  move a point -- SketMaster */
		{
			ans2='\0';
			do
			{
				if(ans2==27)
					break;
				message("Move near a point & hit <Enter>",BROWN,L_GREY);
				get_xy(&cx,&cy,&cspeed);
				unmessage("Move near a point & hit <Enter>",BROWN,L_GREY);
				x=cx;y=cy;
				xy_ll(x,y,&curlat,&curlon);
				mindist=100000.0;
				point=0;		/* find the closest point */
				plot_footer(6);
				for(i=0;i<L1.numpt;i++)
				{
					lat=L1.latlon[i][0];
					lon=L1.latlon[i][1];
					dist=(curlat-lat)*(curlat-lat)+(curlon-lon)*(curlon-lon);
					if(dist<mindist)
					{
						mindist=dist;
						point=i;
						latp=lat;
						lonp=lon;
					}
				}
				sprintf(string,
					"Move point %d to new location & hit <Enter>",point+1);
				message(string,BROWN,L_GREY);
				lat=L1.latlon[point][0];
				lon=L1.latlon[point][1];
				ll_xy(&x,&y,lat,lon);
				pxo=cxo=cx=x;
				pyo=cyo=cy=y;
				px1=py1=px2=py2=p1=p2=-1;
				if(point>0)				/* not the first point of the segment */
				{
					lat=L1.latlon[point-1][0];
					lon=L1.latlon[point-1][1];
					ll_xy(&x,&y,lat,lon);
					px1=x;py1=y;p1=1;
				}
				if(point<L1.numpt-1)
				{
					lat=L1.latlon[point+1][0];
					lon=L1.latlon[point+1][1];
					ll_xy(&x,&y,lat,lon);
					px2=x;py2=y;p2=1;
				}
				if(p1>0)
					tplotln_inv_dot(0,px1,py1,cx,cy,Mbuff[0],ScreenXs);
				if(p2>0)
					tplotln_inv_dot(0,px2,py2,cx,cy,Mbuff[1],ScreenXs);
				if(p1>0)
					plotln(0,px1,py1,cx,cy,WHITE);
				if(p2>0)
					plotln(0,px2,py2,cx,cy,WHITE);
				x=cx;y=cy;
				xy_ll(x+0.5,y+0.5,&lat,&lon);
				do
				{
					if(ans2==27)
						break;
					ans2=m_getpad(&size,&cx,&cy,&speed,1);
					if(ans2=='m')	/* move point */
					{
						if(p2>0)
							unplotln(0,px2,py2,cxo,cyo,Mbuff[1],ScreenXs);
						if(p1>0)
							unplotln(0,px1,py1,cxo,cyo,Mbuff[0],ScreenXs);
						cxo=cx;cyo=cy;
						if(p1>0)
							tplotln_inv_dot(0,px1,py1,cx,cy,Mbuff[0],ScreenXs);
						if(p2>0)
							tplotln_inv_dot(0,px2,py2,cx,cy,Mbuff[1],ScreenXs);
						if(p1>0)
							plotln(0,px1,py1,cx,cy,WHITE);
						if(p2>0)
							plotln(0,px2,py2,cx,cy,WHITE);
					}
					if(p2>0)
						unplotln(0,px2,py2,cxo,cyo,Mbuff[1],ScreenXs);
					if(p1>0)
						unplotln(0,px1,py1,cxo,cyo,Mbuff[0],ScreenXs);
					if(ans2=='s')
					{
						replot_segment(&L1,BUFF,BUFF,
							GF_String[Edit],seg_offset,segnum);
						for(i=point;i<L1.numpt-1;i++)
							for(k=0;k<2;k++)
								L1.latlon[i][k]=L1.latlon[i+1][k];
						L1.numpt-=1;
						replot_segment(&L1,RED,WHITE,
								GF_String[Edit],seg_offset,segnum);
					}
					if(ans2=='k'||ans2=='m')
					{
						x=cxo;y=cyo;
						xy_ll(x+0.5,y+0.5,&lat,&lon);
						L1.latlon[point][0]=lat;
						L1.latlon[point][1]=lon;
					}
					replot_segment(&L1,RED,WHITE,GF_String[Edit],
						seg_offset,segnum);
					if(ans2=='k'||ans2==27)
					{
						if(p1>0)
							plotln(0,px1,py1,pxo,pyo,BUFF);
						if(p2>0)
							plotln(0,px2,py2,pxo,pyo,BUFF);
						if(p1>0)
							plotln(0,px1,py1,cxo,cyo,RED);
						if(p2>0)
							plotln(0,px2,py2,cxo,cyo,RED);
						if(p1>0)
							plotpt(0,px1,py1,WHITE);
						if(p2>0)
							plotpt(0,px2,py2,WHITE);
						plotpt(0,cxo,cyo,WHITE);
					}
				}while(ans2=='m');
			}while(ans2!=27);
			plot_footer(2);
		}
		if(Digit==0&&ans=='m')		/*  move a point -- KeyBoard*/
		{
			cspeed=16;
			message("Move near a point & hit <Enter>",BROWN,L_GREY);
			get_xy(&cx,&cy,&cspeed);
			unmessage("Move near a point & hit <Enter>",BROWN,L_GREY);
			x=cx;y=cy;
			xy_ll(x,y,&curlat,&curlon);
			mindist=100000.0;
			point=0;		/* find the closest point */
			plot_footer(6);
			for(i=0;i<L1.numpt;i++)
			{
				lat=L1.latlon[i][0];
				lon=L1.latlon[i][1];
				dist=(curlat-lat)*(curlat-lat)+(curlon-lon)*(curlon-lon);
				if(dist<mindist)
				{
					mindist=dist;
					point=i;
				}
			}
			sprintf(string,"Move point %d to new location & hit <Enter>",point+1);
			message(string,BROWN,L_GREY);
			lat=L1.latlon[point][0];
			lon=L1.latlon[point][1];
			ll_xy(&x,&y,lat,lon);
			pxo=cxo=cx=x;
			pyo=cyo=cy=y;
			px1=py1=px2=py2=p1=p2=-1;
			if(point>0)				/* not the first point of the segment */
			{
				lat=L1.latlon[point-1][0];
				lon=L1.latlon[point-1][1];
				ll_xy(&x,&y,lat,lon);
				px1=x;py1=y;p1=1;
			}
			if(point<L1.numpt-1)
			{
				lat=L1.latlon[point+1][0];
				lon=L1.latlon[point+1][1];
				ll_xy(&x,&y,lat,lon);
				px2=x;py2=y;p2=1;
			}
			if(p1>0)
				tplotln_inv_dot(0,px1,py1,cx,cy,Mbuff[0],ScreenXs);
			if(p2>0)
				tplotln_inv_dot(0,px2,py2,cx,cy,Mbuff[1],ScreenXs);
			if(p1>0)
				plotln(0,px1,py1,cx,cy,WHITE);
			if(p2>0)
				plotln(0,px2,py2,cx,cy,WHITE);
			if(Cursor==1)
			{
				tcursor(0,cx,cy,size,Buffer);
				cursor(0,cx,cy,GREEN,5);
			}
			x=cx;y=cy;
			xy_ll(x+0.5,y+0.5,&lat,&lon);
			while((ans2=m_getpad(&size,&cx,&cy,&speed,1))!=27&&ans2!=13)
			{
				if(ans2=='c')
				{
					if(Cursor==0)	/* cursor off */
					{
						Cursor==1;	/* turn cursor on */
						tcursor(0,cx,cy,size,Buffer);
						cursor(0,cx,cy,GREEN,5);
						x=cx;y=cy;
						xy_ll(x+0.5,y+0.5,&lat,&lon);
						sprintf(string,
			"pt %d [%ld] lat/lon %0.5lf %0.5lf <Enter> to keep  <Esc> to quit",
						point,L1.attrib,lat,lon);	
						message(string,BROWN,L_GREY);
					}
					else			/* cursor on */
					{
						Cursor=0;	/* turn cursor off */
						unmessage(string,BROWN,L_GREY);
						uncursor(0,cxo,cyo,size,Buffer);
					}
					plot_footer(6);
				}
				if(cx!=cxo||cy!=cyo)
				{
					if(Cursor==1)
					{
						unmessage(string,BROWN,L_GREY);
						uncursor(0,cxo,cyo,size,Buffer);
					}
					if(p2>0)
						unplotln(0,px2,py2,cxo,cyo,Mbuff[1],ScreenXs);
					if(p1>0)
						unplotln(0,px1,py1,cxo,cyo,Mbuff[0],ScreenXs);
					cxo=cx;cyo=cy;
					if(p1>0)
						tplotln_inv_dot(0,px1,py1,cx,cy,Mbuff[0],ScreenXs);
					if(p2>0)
						tplotln_inv_dot(0,px2,py2,cx,cy,Mbuff[1],ScreenXs);
					if(p1>0)
						plotln(0,px1,py1,cx,cy,WHITE);
					if(p2>0)
						plotln(0,px2,py2,cx,cy,WHITE);
					if(Cursor==1)
					{
						tcursor(0,cx,cy,size,Buffer);
						cursor(0,cx,cy,GREEN,5);
						x=cx;y=cy;
						xy_ll(x+0.5,y+0.5,&lat,&lon);
						sprintf(string,
			"pt %d [%ld] lat/lon %0.5lf %0.5lf <Enter> to keep  <Esc> to quit",
						point,L1.attrib,lat,lon);	
						message(string,BROWN,L_GREY);
					}
				}
			}
			if(Cursor==1)
			{
				unmessage(string,BROWN,L_GREY);
				uncursor(0,cxo,cyo,size,Buffer);
			}
			if(p2>0)
				unplotln(0,px2,py2,cxo,cyo,Mbuff[1],ScreenXs);
			if(p1>0)
				unplotln(0,px1,py1,cxo,cyo,Mbuff[0],ScreenXs);
			if(ans2==13)
			{
				x=cxo;y=cyo;
				xy_ll(x+0.5,y+0.5,&lat,&lon);
				L1.latlon[point][0]=lat;
				L1.latlon[point][1]=lon;
				replot_segment(&L1,RED,WHITE,GF_String[Edit],seg_offset,segnum);
			}
			if(p1>0)
				plotln(0,px1,py1,pxo,pyo,BUFF);
			if(p2>0)
				plotln(0,px2,py2,pxo,pyo,BUFF);
			if(p1>0)
				plotln(0,px1,py1,cxo,cyo,RED);
			if(p2>0)
				plotln(0,px2,py2,cxo,cyo,RED);
			if(p1>0)
				plotpt(0,px1,py1,WHITE);
			if(p2>0)
				plotpt(0,px2,py2,WHITE);
			plotpt(0,cxo,cyo,WHITE);
			plot_footer(2);
		}
		if(ans=='d')		/*  delete a point */
		{
			message("Move near a point & hit <Enter>",BROWN,L_GREY);
			get_xy(&cx,&cy,&cspeed);
			unmessage("Move near a point & hit <Enter>",BROWN,L_GREY);
			x=cx;y=cy;
			xy_ll(x,y,&curlat,&curlon);
			mindist=100000.0;
			point=0;		/* find the closest point */
			for(i=0;i<L1.numpt;i++)
			{
				lat=L1.latlon[i][0];
				lon=L1.latlon[i][1];
				dist=(curlat-lat)*(curlat-lat)+(curlon-lon)*(curlon-lon);
				if(dist<mindist)
				{
					mindist=dist;
					point=i;
				}
			}
			replot_segment(&L1,BUFF,BUFF,GF_String[Edit],seg_offset,segnum);
			for(i=point;i<L1.numpt-1;i++)
				for(k=0;k<2;k++)
					L1.latlon[i][k]=L1.latlon[i+1][k];
			L1.numpt-=1;
			replot_segment(&L1,RED,WHITE,GF_String[Edit],seg_offset,segnum);
		}
		if(ans=='s')
		{
			fseek(Fptmp,offset,SEEK_END);
			E[segnum]=NumSeg;
			TmpOffset[NumSeg++]=ftell(Fptmp);
			if(fwrite((char *)&L1.attrib,sizeof(long),1,Fptmp)<=0)
			{
				printf("Write failure at 1\n");
				exit(0);
			}
			if(fwrite((char *)&L1.numpt,sizeof(int),1,Fptmp)<=0)
			{
				printf("Write failure at 2\n");
				exit(0);
			}
			if(fwrite((char *)L1.mmll,sizeof(float),4,Fptmp)<=0)
			{
				printf("Write failure at 3\n");
				exit(0);
			}
			if(fwrite((char *)L1.latlon,sizeof(float),L1.numpt*2,Fptmp)<=0)
			{
				printf("Write failure at 4\n");
				exit(0);
			}
			fflush(Fptmp);
			replot_segment(&L1,ORANGE,M_GREY,GF_String[Edit],seg_offset,segnum);
			*e_cx=cx;
			*e_cy=cy;
			NumChange+=1;
			return(1);
		}
	}while(ans!='x');
}

/***********************************************************************
**
**
**
**
************************************************************************/

int get_xy(x,y,speed)

int *x,*y,*speed;

{
	int size=5;
	int ixo=*x,iyo=*y;
	char ans;

	if(Digit==0)
	{
		tcursor(0,*x,*y,size,Buffer);
		cursor(0,*x,*y,WHITE,5);
	}
	while((ans=m_getpad(&size,x,y,speed,0))!=27&&ans!=13)
	{
		if(Digit==0)
		{
			uncursor(0,ixo,iyo,size,Buffer);
			ixo=*x;iyo=*y;
			tcursor(0,*x,*y,size,Buffer);
			cursor(0,*x,*y,WHITE,5);
		}
	}
	if(Digit==0)
		uncursor(0,ixo,iyo,size,Buffer);
}


/***********************************************************************
**
**
***********************************************************************/

int replot_segment(segment,line,dot,region,seg_offset,segnum)

struct line *segment;
int line,dot;	/* colors */
char *region;
long seg_offset;
int segnum;

{
	int i,j,k,m,n=0,p;
	double x,y,xo,yo;
	int ix,iy,ixo=-1,iyo=-1;
	double dx,dy,dist;
	int secnum,numsec,ival;
	float latlon[2],mll[4],mnln,mxln,mnlt,mxlt;
	char string[40],pth[40],name[40],type[40];
	int doit,numplot=0;
	int first_seg=-1,last_seg=-1,atr=0;

	for(i=0;i<segment->numpt;i++)
	{
		latlon[0]=segment->latlon[i][0];
		latlon[1]=segment->latlon[i][1];
		ll_xy(&x,&y,(double)latlon[0],(double)latlon[1]);
		if(i==0)
		{
			xo=x;yo=y;
			ixo=ix;iyo=iy;
		}
		ix=x;iy=y;
		if(i>0)
			plotln(0,ix,iy,ixo,iyo,line);
		ixo=ix;iyo=iy;
		xo=ix;yo=iy;
	}
	for(i=0;i<segment->numpt;i++)
	{
		latlon[0]=segment->latlon[i][0];
		latlon[1]=segment->latlon[i][1];
		ll_xy(&x,&y,(double)latlon[0],(double)latlon[1]);
		ix=x;iy=y;
		plotpt(0,ix,iy,dot);
	}
}




/***********************************************************************
**
**    iop:
**
**       0 -- use bin or update if available
**       1 -- use only bin file
**
***********************************************************************/

int plot_segment(segment,line,dot,region,seg_offset,segnum,iop)

struct line *segment;
int line,dot;	/* colors */
char *region;
long seg_offset;
int segnum,iop;

{
	int i,j,k,m,n=0,p;
	double x,y,xo,yo;
	int ix,iy,ixo=-1,iyo=-1;
	double dx,dy,dist;
	int secnum,numpt,numsec,ival;
	FILE *fp,*fpatr;
	float latlon[2],mll[4],mnln,mxln,mnlt,mxlt;
	char string[40],pth[40],name[40],type[40];
	int doit,numplot=0;
	int first_seg=-1,last_seg=-1,atr=0;
	long offset,attrib;

	if(iop==1||E[segnum]<0)		/* plot from '.bin' file */
	{
		strcpy(string,region);
		strcat(string,".bin");
		fp=fopen(string,"rb");
		if(!fp)
			return(-1);
		strcpy(string,region);
		strcat(string,".atr");
		fpatr=fopen(string,"rb");
		if(fpatr)
		{
			offset=(long)segnum*(long)4;
			fseek(fpatr,offset,SEEK_SET);
			fread((char *)&attrib,sizeof(long),1,fpatr);
			segment->attrib=attrib;
		}
		else
		segment->attrib=9999;
		fseek(fp,seg_offset,SEEK_SET);
		fread((char *)&numpt,sizeof(int),1,fp);
		segment->numpt=numpt;
		fread((char *)mll,sizeof(float),4,fp);
		for(k=0;k<4;k++)
			segment->mmll[k]=mll[k];
		for(i=0;i<numpt;i++)
		{
			fread((char *)latlon,sizeof(float),2,fp);
			segment->latlon[i][0]=latlon[0];
			segment->latlon[i][1]=latlon[1];
			ll_xy(&x,&y,(double)latlon[0],(double)latlon[1]);
			if(i==0)
			{
				xo=x;yo=y;
				ixo=ix;iyo=iy;
			}
			ix=x;iy=y;
			if(i>0)
				plotln(0,ix,iy,ixo,iyo,line);
			ixo=ix;iyo=iy;
			xo=ix;yo=iy;
		}
		for(i=0;i<numpt;i++)
		{
			latlon[0]=segment->latlon[i][0];
			latlon[1]=segment->latlon[i][1];
			ll_xy(&x,&y,(double)latlon[0],(double)latlon[1]);
			ix=x;iy=y;
			plotpt(0,ix,iy,dot);
		}
		fclose(fp);
		fclose(fpatr);
	}
	else		/* plot from update file */
	{
		offset=TmpOffset[E[segnum]];
		fseek(Fptmp,seg_offset,SEEK_SET);
		fread((char *)&attrib,sizeof(long),1,Fptmp);
		segment->attrib=attrib;
		fread((char *)&numpt,sizeof(int),1,Fptmp);
		segment->numpt=numpt;
		fread((char *)mll,sizeof(float),4,Fptmp);
		for(k=0;k<4;k++)
			segment->mmll[k]=mll[k];
		for(i=0;i<numpt;i++)
		{
			fread((char *)latlon,sizeof(float),2,Fptmp);
			segment->latlon[i][0]=latlon[0];
			segment->latlon[i][1]=latlon[1];
			ll_xy(&x,&y,(double)latlon[0],(double)latlon[1]);
			if(i==0)
			{
				xo=x;yo=y;
				ixo=ix;iyo=iy;
			}
			ix=x;iy=y;
			if(i>0)
				plotln(0,ix,iy,ixo,iyo,line);
			ixo=ix;iyo=iy;
			xo=ix;yo=iy;
		}
		for(i=0;i<numpt;i++)
		{
			latlon[0]=segment->latlon[i][0];
			latlon[1]=segment->latlon[i][1];
			ll_xy(&x,&y,(double)latlon[0],(double)latlon[1]);
			ix=x;iy=y;
			plotpt(0,ix,iy,dot);
		}
	}
}


/***********************************************************************
**
**   Finds the closest point to the cursor and
**
**   (1) returns the segment number which contains the point
**   (2) the offset of the segment in the '.bin' file and
**   (3) the x and y values of the closest point
**
**
**
***********************************************************************/

int find_segment(val,region,seg_offset,cur_x,cur_y)

char *region;
int val,*cur_x,*cur_y;
long *seg_offset;

{
	int i,j,k,m,n=0,p;
	double x,y,xo,yo,cx,cy;
	int ix,iy,ixo=*cur_x,iyo=*cur_y;
	double dx,dy,mindist=10000.0,dist;
	int secnum,numpt,numsec,ival;
	FILE *fp,*fphdr;
	float latlon[2],mll[4],mnln,mxln,mnlt,mxlt;
	char string[40],pth[40],name[40],type[40],ans;
	int doit,numplot=0;
	int first_seg=-1,last_seg=-1,atr=0;
	long offset,attrib;
	int seg_num=-1,size=5,speed=16;

	if(Digit==0)
	{
		tcursor(0,*cur_x,*cur_y,size,Buffer);
		cursor(0,*cur_x,*cur_y,WHITE,size);
	}
	while((ans=m_getpad(&size,cur_x,cur_y,&speed,0))!=27&&ans!=13)
	{
		uncursor(0,ixo,iyo,size,Buffer);
		ixo=*cur_x;iyo=*cur_y;
		tcursor(0,*cur_x,*cur_y,size,Buffer);
		cursor(0,*cur_x,*cur_y,WHITE,size);
	}
	if(Digit==0)
		uncursor(0,ixo,iyo,size,Buffer);
	if(ans==27)
		return(-4);
	cx=*cur_x;
	cy=*cur_y;
	strcpy(string,region);
	strcat(string,".bin");
	fp=fopen(string,"rb");
	if(!fp)
		return(-1);
	strcpy(string,region);
	strcat(string,".hdr");
	fphdr=fopen(string,"rb");
	if(!fphdr)
	{
		fclose(fp);
		return(-1);
	}
	fread((char *)&numsec,sizeof(int),1,fphdr);
	numsec=NumSec;
	for(j=0;j<numsec;j++)
	{
		if(kbhit()>0)
		{
			ival=getch();
			if(ival==27)
			{
				fclose(fp);
				fclose(fphdr);
				return(-2);
			}
		}
						/* read header file entry */
		doit=1;
		fread((char *)&offset,sizeof(long),1,fphdr);
		fread((char *)mll,sizeof(float),4,fphdr);
		if(E[j]>=0)   /* if line is redone read new data from '.tmp' */
		{
			offset=TmpOffset[E[j]];
			fseek(Fptmp,offset,SEEK_SET);
			fread((char *)&attrib,sizeof(long),1,Fptmp);
			fread((char *)&numpt,sizeof(int),1,Fptmp);
			fread((char *)mll,sizeof(float),4,Fptmp);
		}
		mnln=mll[0];mxln=mll[1];mnlt=mll[2];mxlt=mll[3];
		if(mnln>MaxLon||mnlt>MaxLat||mxln<MinLon||mxlt<MinLat)
			doit=0;
		if(E[j]==-3)	/* line deleted */
			doit=0;
		if(doit==1)
		{
			numplot+=1;
			if(E[j]<0)
			{
				fseek(fp,offset,SEEK_SET);
				fread((char *)&numpt,sizeof(int),1,fp);
				fread((char *)mll,sizeof(float),4,fp);
			}
			m=0;
			for(i=0;i<numpt;i++)
			{
				if(E[j]>=0)
					fread((char *)latlon,sizeof(float),2,Fptmp);
				else
					fread((char *)latlon,sizeof(float),2,fp);
				if(doit==1)
				{
					ll_xy(&x,&y,(double)latlon[0],(double)latlon[1]);
					dist=(x-cx)*(x-cx)+(y-cy)*(y-cy);
					if(dist<mindist)
					{
						mindist=dist;
						*seg_offset=offset;
						*cur_x=x;
						*cur_y=y;
						seg_num=j;
					}
				}
			}
		}
	}
	fclose(fp);
	fclose(fphdr);
	return(seg_num);
}

/********************************************************************
**
**
**
********************************************************************* */

char Zoom(xc,yc,xs,ys,pixsize,clat,clon,space)

int xc,yc,xs,ys;
float *pixsize,*clat,*clon,*space;

{
	int i,j,k,k1,k2;
	char ans,ans2;
	int ix[4],iy[4],speed=16;
	float m_ps=*pixsize,m_clon=*clon,m_clat=*clat,m_space=*space;
	float cur_ps=*pixsize/2.0,cur_clon=*clon,cur_clat=*clat,cur_space=*space;
	float cur_scale=1.189207115;
	int cur_ix=xc+xs/2,cur_iy=yc+ys/2;
	double dlat,dlon;
	int txs,screen_xs,lsize=FontSize,white=WHITE;

	if(VideoType=='S')
		lsize=3;
	if(cur_ps<MIN_PIX)cur_ps=MIN_PIX;

	xy_ll((double)cur_ix,(double)cur_iy,&dlat,&dlon);
	cur_clat=dlat;cur_clon=dlon;
	get_corners(xc,yc,xs,ys,m_ps,m_clat,m_clon,cur_ps,cur_clat,cur_clon,ix,iy);

	tplotln_val(0,ix[0],iy[0]+1,ix[1],iy[1],MT_Buff[0],WHITE);
	tplotln_val(0,ix[1]+1,iy[1],ix[2],iy[2],MT_Buff[1],WHITE);
	tplotln_val(0,ix[2],iy[2]-1,ix[3],iy[3],MT_Buff[2],WHITE);
	tplotln_val(0,ix[3]-1,iy[3],ix[0],iy[0],MT_Buff[3],WHITE);

	do
	{
		ans=getch();
		if(ans==0)
		{
			ans2=getch();
			if(ans2==59)		/* F1 */
				ans='H';
			else
			{
				if(ans2=='G'||ans2=='H'||ans2=='I')cur_iy-=speed;
				if(ans2=='I'||ans2=='M'||ans2=='Q')cur_ix+=speed;
				if(ans2=='O'||ans2=='P'||ans2=='Q')cur_iy+=speed;
				if(ans2=='O'||ans2=='K'||ans2=='G')cur_ix-=speed;
				if(cur_iy<yc-ScreenYs/2)cur_iy=yc-ScreenYs/2;
				if(cur_ix<xc-ScreenXs/2)cur_ix=xc-ScreenXs/2;
				if(cur_iy>yc+ys+ScreenXs/2-1)cur_iy=yc+ys+ScreenXs/2-1;
				if(cur_ix>xc+xs+ScreenYs/2-1)cur_ix=xc+xs+ScreenYs/2-1;
			}
		}
		if(ans=='h'||ans=='H')
		{
			txs=ScreenXs;
			ScreenXs=FullScreenXs;
			save_box(0,M.Back,Mbuff,
				0,0,FullScreenXs,32/lsize);
			paint_box(0,M.Back,MT_Buff,0,0,ScreenXs,32/lsize);
			sprintf(String,
"(s)maller (l)arger 'Enter'=plot  'Esc'=exit without plotting");
			plot_font_h(WHITE,1,1,String,FontSize,Font);
			ans=getch();
			unsave_box(0,M.Back,Mbuff,
				0,0,FullScreenXs,32/lsize);
			ScreenXs=txs;
		}		

		if(ans=='l')cur_ps*=cur_scale;
		if(ans=='s')cur_ps/=cur_scale;
		if(cur_ps<MIN_PIX)cur_ps=MIN_PIX;
		if(ans=='-')
		{
			if(speed>1)
				speed/=2;
			else
				cur_scale=sqrt((double)cur_scale);
		}
		if(ans=='+'&&speed<256)speed*=2;

		unplotln(0,ix[0],iy[0]+1,ix[1],iy[1],MT_Buff[0]);
		unplotln(0,ix[1]+1,iy[1],ix[2],iy[2],MT_Buff[1]);
		unplotln(0,ix[2],iy[2]-1,ix[3],iy[3],MT_Buff[2]);
		unplotln(0,ix[3]-1,iy[3],ix[0],iy[0],MT_Buff[3]);

		xy_ll((double)cur_ix,(double)cur_iy,&dlat,&dlon);
		cur_clat=dlat;cur_clon=dlon;
		get_corners(xc,yc,xs,ys,
			m_ps,m_clat,m_clon,cur_ps,cur_clat,cur_clon,ix,iy);

		tplotln_val(0,ix[0],iy[0]+1,ix[1],iy[1],MT_Buff[0],WHITE);
		tplotln_val(0,ix[1]+1,iy[1],ix[2],iy[2],MT_Buff[1],WHITE);
		tplotln_val(0,ix[2],iy[2]-1,ix[3],iy[3],MT_Buff[2],WHITE);
		tplotln_val(0,ix[3]-1,iy[3],ix[0],iy[0],MT_Buff[3],WHITE);

	}while(ans!=83&&ans!=27&&ans!=13);

	unplotln(0,ix[0],iy[0]+1,ix[1],iy[1],MT_Buff[0]);
	unplotln(0,ix[1]+1,iy[1],ix[2],iy[2],MT_Buff[1]);
	unplotln(0,ix[2],iy[2]-1,ix[3],iy[3],MT_Buff[2]);
	unplotln(0,ix[3]-1,iy[3],ix[0],iy[0],MT_Buff[3]);

	M.Pixsize=*pixsize=cur_ps;
	M.Clat=*clat=cur_clat;
	M.Clon=*clon=cur_clon;

	if(cur_ps<20)
	{	
		*space=30.0;
		return(ans);
	}
	if(cur_ps<10)
	{	
		*space=15.0;
		return(ans);
	}
	if(cur_ps<2)
	{	
		*space=5.0;
		return(ans);
	}
	*space=1.0;
	return(ans);

}

/********************************************************************
**
**
**
********************************************************************* */

int get_corners(xc,yc,xs,ys,m_pixsize,m_clat,m_clon,
		cur_pixsize,cur_clat,cur_clon,x,y)

int xc,yc,xs,ys;
float m_pixsize,m_clat,m_clon,cur_pixsize,cur_clat,cur_clon;
int x[4],y[4];

{
	int i,j,k;
	double lat[4],lon[4],dx,dy;

	miv.indat[0]=cur_pixsize;
	miv.indat[1]=cur_clat;
	miv.indat[2]=cur_clon;
	miv.indat[3]=cur_clon;
	miv.indat[4]=cur_clat+10;
	miv.indat[5]=cur_clat-10;
	map_pos.xc=xc;	
	map_pos.yc=yc;
	map_pos.xs=xs;
	map_pos.ys=ys;
	auto_set_cc(miv.type,miv.indat);
	
	xy_ll((double)xc,(double)yc,lat+0,lon+0);
	xy_ll((double)xc,(double)yc+ys-1,lat+1,lon+1);
	xy_ll((double)xc+xs-1,(double)yc+ys-1,lat+2,lon+2);
	xy_ll((double)xc+xs-1,(double)yc,lat+3,lon+3);

	miv.indat[0]=m_pixsize;
	miv.indat[1]=m_clat;
	miv.indat[2]=m_clon;
	miv.indat[3]=m_clon;
	miv.indat[4]=m_clat+10;
	miv.indat[5]=m_clat-10;
	map_pos.xc=xc;	
	map_pos.yc=yc;
	map_pos.xs=xs;
	map_pos.ys=ys;
	auto_set_cc(miv.type,miv.indat);

	for(i=0;i<4;i++)
	{
		ll_xy(&dx,&dy,lat[i],lon[i]);
		x[i]=dx;
		y[i]=dy;
	}
}


/***********************************************************************
**
**
**
**
************************************************************************/

int get_location(x,y,speed)

int *x,*y,*speed;

{
	int xc=0,yc=FullScreenYs-BottomHigh/FontSize,high=32/FontSize;
	int size=5,xo=*x,yo=*y;
	char string[100],ans;
	double lat,lon,dx,dy;

	yc+=high*2;
	ScreenYs=FullScreenYs;	
	paint_box(0,D_GREY,MT_Buff,xc,yc,ScreenXs,high);
	plot_font_h(L_GREY,xc+5,yc+2,
		"<Esc> to exit",
		FontSize,Font);
	xc=15*15/FontSize;
	tcursor(0,*x,*y,size,Buffer);
	cursor(0,*x,*y,WHITE,5);
	while((ans=getpad(&size,x,y,speed,0))!=27&&ans!=13)
	{
		uncursor(0,xo,yo,size,Buffer);
		xo=dx=*x;yo=dy=*y;
		tcursor(0,*x,*y,size,Buffer);
		cursor(0,*x,*y,WHITE,5);
		xy_ll(dx,dy,&lat,&lon);
		paint_box(0,D_GREY,MT_Buff,xc,yc,ScreenXs-xc,high);
		sprintf(string,"x=%3d  y=%d  lat=%10.6lf  lon=%11.6lf",
			*x,*y,lat,lon);
		plot_font_h(L_GREY,xc+5,yc,string,FontSize,Font);
	}
	uncursor(0,xo,yo,size,Buffer);
	paint_box(0,D_GREY,MT_Buff,xc,yc,ScreenXs,high);
	ScreenYs=FullScreenYs-BottomHigh/FontSize;
}

/***********************************************************************
**
**
**
**
************************************************************************/

int message(string,box,text)

char *string;
int box,text;

{
	int xc=0,yc=FullScreenYs-BottomHigh/FontSize,ys=32/FontSize;
	int xs=strlen(string)*15/FontSize+5;

	yc+=ys*2;
	ScreenYs=FullScreenYs;	
	paint_box(0,box,MT_Buff,xc,yc,xs,ys);
	plot_font_h(text,xc+3,yc+2,string,FontSize,Font);
	ScreenYs=FullScreenYs-BottomHigh/FontSize;
}

/***********************************************************************
**
**
**
**
************************************************************************/

int unmessage(string,box,text)

char *string;
int box,text;

{
	int xc=0,yc=FullScreenYs-BottomHigh/FontSize,ys=32/FontSize;
	int xs=strlen(string)*15/FontSize+5;

	yc+=ys*2;
	ScreenYs=FullScreenYs;	
	paint_box(0,D_GREY,MT_Buff,xc,yc,xs,ys);
	ScreenYs=FullScreenYs-BottomHigh/FontSize;
}




/***********************************************************************
**
**   num:
**        1 -- main footer
**        2 -- EDIT SEGMENT:  footer
**        3 -- ADD SEGMENT:  footer
**        4 -- ZOOM:  footer
**        5 -- SHIFT:  footer
**        6 -- MOVE A POINT:
**        7 -- CUT A SEGMENT:
**
************************************************************************/

int plot_footer(num)

int num;

{
	int xc=0,yc=FullScreenYs-BottomHigh/FontSize,high=32/FontSize;

	ScreenYs=FullScreenYs;	
	paint_box(0,D_GREY,MT_Buff,xc,yc,ScreenXs,BottomHigh/FontSize);
	if(num==1)
	{
		plot_font_h(L_GREY,xc+5,yc+2,
"(e)dit segment  (L)ocation  (R)eplot map  (S)hift map  (P)rune  e(x)it",
			FontSize,Font);
		plot_font_h(L_GREY,xc+5,yc+high+2,
"(z)oom  (r)eset zoom  (l)ast zoom  (a)dd segment (c)ut segment",
			FontSize,Font);
	}
	if(num==2)
	{
		plot_font_h(L_GREY,xc+5,yc+2,
"EDIT SEGMENT: (m)ove point (c)lose  (a)dd points  (d)elete point  e(x)it",
			FontSize,Font);
		plot_font_h(L_GREY,xc+5,yc+high+2,
"(s)ave  (z)oom  (r)eset zoom  (l)ast zoom  (R)eplot map  (D)elete line ",
			FontSize,Font);
	}
	if(num==3)
	{
		plot_font_h(L_GREY,xc+5,yc+2,
"ADD A SEGMENT:  (c)lose and save  (e)nd and save",
			FontSize,Font);
		plot_font_h(L_GREY,xc+5,yc+high+2,
"                e(x)it without saving",
			FontSize,Font);
	}
	if(num==4)
	{
		plot_font_h(L_GREY,xc+5,yc+2,
"ZOOM:  Move Box With Arrow Keys   <Enter> to plot  <Esc> to exit",
			FontSize,Font);
		plot_font_h(L_GREY,xc+5,yc+high+2,
"Change Box Size Using  (l)arger or (s)maller  '+' = faster  '-' = slower",
			FontSize,Font);
	}
	if(num==5)
	{
		plot_font_h(L_GREY,xc+5,yc+2,
"SHIFT:  Mark a point on the on the yellow lines    <Enter> to plot",
			FontSize,Font);
		plot_font_h(L_GREY,xc+5,yc+high+2,
"        then mark where it should be.               <Esc>  to exit",
			FontSize,Font);
	}
	if(num==6&&Digit==0)
	{
		plot_font_h(L_GREY,xc+5,yc+2,
"MOVE A POINT:   <Enter> to keep  <Esc> to quit",FontSize,Font);
		if(Cursor==1)
			plot_font_h(L_GREY,xc+5,yc+high+2,
"                (c)ursor OFF for faster editing",FontSize,Font);
		else
			plot_font_h(L_GREY,xc+5,yc+high+2,
"                (c)ursor ON to get lat/lon of point",FontSize,Font);
	}
	if(num==6&&Digit==1)
	{
		plot_font_h(L_GREY,xc+5,yc+2,
"MOVE A POINT:",FontSize,Font);
		plot_font_h(YELLOW,xc+5,yc+2,
"                                          Move Point",
FontSize,Font);
		plot_font_h(WHITE,xc+5,yc+high/2+2,
"                  Keep & Move Next Point",
FontSize,Font);
		plot_font_h(GREEN,xc+5,yc+high/2+2,
"                                                       Delete Point",
FontSize,Font);
		plot_font_h(BLUE,xc+5,yc+high+2,
"                                           Exit Move",
FontSize,Font);
	}
	if(num==7)
	{
		plot_font_h(L_GREY,xc+5,yc+2,
"CUT A SEGMENT:   (c)ut line  (z)oom   e(x)it to quit cut function",
				FontSize,Font);
		plot_font_h(L_GREY,xc+5,yc+high+2,
"                 <Enter> to make cut <Esc> to ignore cut",FontSize,Font);
	}

	ScreenYs=FullScreenYs-BottomHigh/FontSize;
}


/********************************************************************
**
**   iop == 1 E[] is set
**
********************************************************************* */

int plot_map(xc,yc,xs,ys,pixsize,clat,clon,space,iop)

int xc,yc,xs,ys,iop;
float pixsize,clat,clon,space;

{
	int i,j,k,l,m;
	double sp=space;
	int num,val,filled=0;
	int ix=ScreenXs/2,iy=ScreenYs/2,speed=16;
	char string[100];

	miv.type=M.Type;
	miv.indat[0]=pixsize;
	miv.indat[1]=clat;
	miv.indat[2]=clon;
	miv.indat[3]=clon;
	miv.indat[4]=clat+10;
	miv.indat[5]=clat-10;
	map_pos.xc=0;	
	map_pos.yc=0;
	map_pos.xs=ScreenXs;
	map_pos.ys=ScreenYs;
	auto_set_cc(miv.type,miv.indat);
	screen_mm();

	paint_box(0,BUFF,MT_Buff,0,0,ScreenXs,ScreenYs);
	for(i=0;i<NumBackF;i++)
	{
		if(kbhit()>0)
		{
			val=getch();
			if(val==27)
				return(-1);
		}
		sprintf(string,"Plotting Line '%s'",GF_String[BackF[i]]);
		message(string,BROWN,L_GREY);
		num=convert_bin(D_GREY,GF_String[BackF[i]],0);
		unmessage(string,BROWN,L_GREY);
		if(num<-1)
			return(-2);
	}
	sprintf(string,"Plotting Line to Edit '%s'",GF_String[Edit]);
	message(string,BROWN,L_GREY);
	num=convert_bin(YELLOW,GF_String[Edit],iop);
	unmessage(string,BROWN,L_GREY);
	if(num<-1)
		return(-2);
}

/***********************************************************************
**
**   iop:
**        0 -- just plot line
**        1 -- plot and load NumSec and E[]
**        2 -- plot and check for changed lines  (Edit line)
**
**   E[]:
**       -4 -- no segment
**       -3 -- segment has been deleted
**       -2 -- segment off screen and unchanged
**       -1 -- segment on screen and unchanged
**      >=0 -- number of segment in 'linefix.bin'
**
***********************************************************************/

int convert_bin(val,region,iop)

char *region;
int val,iop;

{
	int i,j,k,m,n=0,p;
	double x,y,xo,yo;
	int ix,iy,ixo=-1,iyo=-1;
	double dx,dy,dist;
	int secnum,numpt,numsec,ival;
	FILE *fp,*fphdr,*fpatr;
	float latlon[2],mll[4],mnln,mxln,mnlt,mxlt;
	char string[40],pth[40],name[40],type[40];
	int doit,numplot=0;
	int first_seg=-1,last_seg=-1,atr=0;
	long offset,attrib;

	strcpy(string,region);
	strcat(string,".bin");
	fp=fopen(string,"rb");
	if(!fp)
		return(-1);
	strcpy(string,region);
	strcat(string,".hdr");
	fphdr=fopen(string,"rb");
	if(!fphdr)
	{
		fclose(fp);
		return(-1);
	}
	strcpy(string,region);
	strcat(string,".atr");
	fpatr=fopen(string,"rb");
	if(fpatr)
		atr=1;
	fread((char *)&numsec,sizeof(int),1,fphdr);
	fread((char *)&numsec,sizeof(int),1,fp);
	if(iop==1)
	{
		NumSec=numsec;
		E[numsec]=-4;
	}
	if(iop==2)
		numsec=NumSec;
	for(j=0;j<numsec;j++)
	{
		if(kbhit()>0)
		{
			ival=getch();
			if(ival==27)
			{
				fclose(fp);
				fclose(fphdr);
				return(-2);
			}
		}
						/* read header file entr */
		doit=1;
		fread((char *)&offset,sizeof(long),1,fphdr);
		fread((char *)mll,sizeof(float),4,fphdr);
		if(iop==1)
			E[j]=-2;
		if(atr==1)
			fread((char *)&attrib,sizeof(long),1,fpatr);
		if(iop==2&&E[j]>=0)   /* if line is redone read new data from '.tmp' */
		{
			offset=TmpOffset[E[j]];
			fseek(Fptmp,offset,SEEK_SET);
			fread((char *)&attrib,sizeof(long),1,Fptmp);
			fread((char *)&numpt,sizeof(int),1,Fptmp);
			fread((char *)mll,sizeof(float),4,Fptmp);
		}
		if(iop!=2||Shift==0)
		{
			mnln=mll[0];mxln=mll[1];
			mnlt=mll[2];mxlt=mll[3];
		}
		else
		{
			mnln=mll[0]+DelLon;mxln=mll[1]+DelLon;
			mnlt=mll[2]+DelLat;mxlt=mll[3]+DelLat;
		}
		if(mnln>MaxLon||mnlt>MaxLat||mxln<MinLon||mxlt<MinLat)
			doit=0;
		if(iop==2&&E[j]==-3)	/* line deleted */
			doit=0;
		if(doit==1)
		{
			if(iop==1)
				E[j]=-1;
			numplot+=1;
			if(iop!=2||E[j]<0)
			{
				fseek(fp,offset,SEEK_SET);
				fread((char *)&numpt,sizeof(int),1,fp);
				fread((char *)mll,sizeof(float),4,fp);
			}
			m=0;
			for(i=0;i<numpt;i++)
			{
				if(iop==2&&E[j]>=0)
					fread((char *)latlon,sizeof(float),2,Fptmp);
				else
					fread((char *)latlon,sizeof(float),2,fp);
				if(doit==1)
				{
					if(iop!=2||Shift==0)
						ll_xy(&x,&y,(double)latlon[0],(double)latlon[1]);
					else
						ll_xy(&x,&y,(double)latlon[0]+DelLat,
										(double)latlon[1]+DelLon);
					if(i==0)
					{
						xo=x;yo=y;
						ixo=ix;iyo=iy;
					}
					ix=x;iy=y;
					plotpt(0,ix,iy,val);
					if(i>0)
						plotln(0,ix,iy,ixo,iyo,val);
					ixo=ix;iyo=iy;
					xo=ix;yo=iy;
				}
			}
		}
	}
	fclose(fp);
	fclose(fpatr);
	fclose(fphdr);
	return(numplot);
}
/**************************************************************************
**
**
**
************************************************************************* */

int video_on()

{
	int i,row;

	FontSize=2;
	i=GetVideoBoardID();
	if(VideoType=='X')
	{
		row=SetVideoMode(480,&B_Id);
		if(B_Id.row<400)
			VideoType='V';
		else
		{
			ScreenXs=640;
			ScreenYs=row;
		}
	}
	if(VideoType=='Y')
	{
		row=SetVideoMode(400,&B_Id);
		if(B_Id.row<400)
			VideoType='V';
		else
		{
			ScreenXs=640;
			ScreenYs=row;
		}
	}
	if(VideoType=='V')
	{
		row=SetVideoMode(0x12,&B_Id);
		if(B_Id.row!=480)
			VideoType='E';
		else
		{
			ScreenXs=640;
			ScreenYs=480;
		}
	}
	if(VideoType=='S')
	{
		row=SetVideoMode(0x13,&B_Id);
		if(B_Id.row!=200)
		{
			SetVideoMode(0,&B_Id);
			printf("Could not boot color board.\n");
			exit(0);
		}
		ScreenXs=320;
		ScreenYs=200;
		FontSize=3;
	}
	if(VideoType=='E')
	{
		row=SetVideoMode(0x10,&B_Id);
		if(B_Id.row!=350)
		{
			SetVideoMode(0,&B_Id);
			printf("Could not boot color board.\n");
			exit(0);
		}
		ScreenXs=640;
		ScreenYs=350;
	}
	if(B_Id.row<0)
	{
		SetVideoMode(0,&B_Id);
		printf("Could not boot board -- row = %d\n\n",row);
		exit(0);
	}
	ScreenXs=FullScreenXs=B_Id.col;
	FullScreenYs=B_Id.row;
	ScreenYs=FullScreenYs-BottomHigh/FontSize;
}
/**************************************************************************
**
**
**
************************************************************************* */

int get_color(string)

char *string;

{
	int i,j,k;
	int val=-1;

	for(i=0;i<MAX_COLORS;i++)
		if(strcmpi(M.Color_Name[i],string)==0)
			return(i);
	return(-1);
}

/********************************************************************
**
**
**
********************************************************************* */

int screen_mm()

{
	double lat,lon,x,y;
	double sx=ScreenXs,sy=ScreenYs;
	float flat,flon,lon_r,lon_l;

	MaxLat=-360.0;
	MaxLon=-360.0;
	MinLat= 360.0;
	MinLon= 360.0;

	x=0.0;y=0.0;
	xy_ll(x,y,&lat,&lon);
	flat=lat;flon=lon;
	if(flat>MaxLat)MaxLat=flat;
	if(flon>MaxLon)MaxLon=flon;
	if(flat<MinLat)MinLat=flat;
	if(flon<MinLon)MinLon=flon;
	x=0.0;y=ScreenYs/2.0;
	xy_ll(x,y,&lat,&lon);lon_l=lon;
	flat=lat;flon=lon;
	if(flat>MaxLat)MaxLat=flat;
	if(flon>MaxLon)MaxLon=flon;
	if(flat<MinLat)MinLat=flat;
	if(flon<MinLon)MinLon=flon;
	x=0.0;y=ScreenYs;
	xy_ll(x,y,&lat,&lon);
	flat=lat;flon=lon;
	if(flat>MaxLat)MaxLat=flat;
	if(flon>MaxLon)MaxLon=flon;
	if(flat<MinLat)MinLat=flat;
	if(flon<MinLon)MinLon=flon;
	x=320.0;y=ScreenYs;
	xy_ll(x,y,&lat,&lon);
	flat=lat;flon=lon;
	if(flat>MaxLat)MaxLat=flat;
	if(flon>MaxLon)MaxLon=flon;
	if(flat<MinLat)MinLat=flat;
	if(flon<MinLon)MinLon=flon;
	x=ScreenXs;y=ScreenYs;
	xy_ll(x,y,&lat,&lon);
	flat=lat;flon=lon;
	if(flat>MaxLat)MaxLat=flat;
	if(flon>MaxLon)MaxLon=flon;
	if(flat<MinLat)MinLat=flat;
	if(flon<MinLon)MinLon=flon;
	x=ScreenXs;y=ScreenXs/2;
	xy_ll(x,y,&lat,&lon);lon_r=lon;
	flat=lat;flon=lon;
	if(flat>MaxLat)MaxLat=flat;
	if(flon>MaxLon)MaxLon=flon;
	if(flat<MinLat)MinLat=flat;
	if(flon<MinLon)MinLon=flon;
	x=ScreenXs;y=  0.0;
	xy_ll(x,y,&lat,&lon);
	flat=lat;flon=lon;
	if(flat>MaxLat)MaxLat=flat;
	if(flon>MaxLon)MaxLon=flon;
	if(flat<MinLat)MinLat=flat;
	if(flon<MinLon)MinLon=flon;
	x=ScreenXs/2;y=  0.0;
	xy_ll(x,y,&lat,&lon);
	flat=lat;flon=lon;
	if(flat>MaxLat)MaxLat=flat;
	if(flon>MaxLon)MaxLon=flon;
	if(flat<MinLat)MinLat=flat;
	if(flon<MinLon)MinLon=flon;
	if(lon_l>180.0)
		lon_l-=360.0;
	if(lon_r>180.0)
		lon_r-=360.0;
	lat=-89.999;	/* south pole check */
	ll_xy(&x,&y,lat,lon);
	if(x>=0.0&&x<sx&&y>0.0&&y<sy)
	{		
		MinLat=-90.001;
		MinLon=-360.0;
		MaxLon= 360.0;
	}
	lat=89.999;	/* north pole check */
	ll_xy(&x,&y,lat,lon);
	if(x>=0.0&&x<sx&&y>0.0&&y<sy)
	{		
		MaxLat= 90.001;
		MinLon=-360.0;
		MaxLon= 360.0;
	}
	if(lon_l>lon_r)	/* date line on screen */
	{
		MinLon=-360.0;
		MaxLon= 360.0;
	}
}


/**************************************************************************
**
**
**
************************************************************************* */

int read_control(filename)

char *filename;

{
	int i,j,k,n=1,m,p=0,u,v;
	FILE *fp;
	char string1[100],string2[100],string2a[100],string3[100],string2b[100];
	char nam[100],type;
	int numread,val,numcolors;

	fp=fopen(filename,"rb");
	if(!fp)
		return(-1);
	fscanf(fp,"%s",string1);
	if(strlen(string1)==1)
	{
		type=string1[0];
		if(type=='l'||type=='a'||type=='m'||
			type=='s'||type=='p'||type=='S')
		{
			M.Type=type;
			numread=fscanf(fp,"%f%f%f%f",&M.Pixsize,&M.Clat,&M.Clon,&M.Space);
			if(numread!=4)
			{
				return(-6);
			}
		}
	}

	do
	{
		numread=fscanf(fp,"%s",string1);
	}while(numread>0&&strcmpi(string1,"begin_color")!=0);
	if(numread<1)
		return(-2);		/* could not find 'begin_color' */

	i=0;
	do
	{
		fscanf(fp,"%s",M.Color_Name[i]);
		if(strcmpi(M.Color_Name[i],"end_color")!=0)
		{
			numread=fscanf(fp,"%s%d%d%d",
				string1,&M.Color_Val[i][0],&M.Color_Val[i][1],&M.Color_Val[i][2]);
		}
		i+=1;
	}while(numread==4&&strcmpi(M.Color_Name[i-1],"end_color")!=0&&i<MAX_COLORS);
	numcolors=i;
	do
	{
		numread=fscanf(fp,"%s",string1);
		if(strcmpi(string1,"menu_background")==0)
		{
			fscanf(fp,"%s%s",string2,string3);
			val=get_color(string3);
			if(val>=0&&val<MAX_COLORS)
				M.Mback=val;
			else
			{
				printf("'%s' %s '%s'\n",string1,string2,string3);
				return(-5);	/* bad color name */
			}
		}
		if(strcmpi(string1,"text")==0)
		{
			fscanf(fp,"%s%s",string2,string3);
			val=get_color(string3);
			if(val>=0&&val<MAX_COLORS)
				M.Text=val;
			else
			{
				printf("'%s' %s '%s'\n",string1,string2,string3);
				return(-5);	/* bad color name */
			}
		}
		if(strcmpi(string1,"highlight")==0)
		{
			fscanf(fp,"%s%s",string2,string3);
			val=get_color(string3);
			if(val>=0&&val<MAX_COLORS)
				M.High=val;
			else
			{
				printf("'%s %s '%s'\n",string1,string2,string3);
				return(-5);	/* bad color name */
			}
		}
		if(strcmpi(string1,"background")==0)
		{
			fscanf(fp,"%s%s",string2,string3);
			val=get_color(string3);
			if(val>=0&&val<MAX_COLORS)
				M.Back=val;
			else
			{
				printf("'%s' %s '%s'\n",string1,string2,string3);
				return(-5);	/* bad color name */
			}
		}
		if(strcmpi(string1,"graticule")==0)
		{
			fscanf(fp,"%s%s",string2,string3);
			val=get_color(string3);
			if(val>=0&&val<MAX_COLORS)
				M.Grat=val;
			else
			{
				printf("'%s' %s '%s'\n",string1,string2,string3);
				return(-5);	/* bad color name */
			}
		}
		if(strcmpi(string1,"lat/lon")==0)
		{
			fscanf(fp,"%s%s",string2,string3);
			val=get_color(string3);
			if(val>=0&&val<MAX_COLORS)
				M.LatLon=val;
			else
			{
				printf("'%s' %s '%s'\n",string1,string2,string3);
				return(-5);	/* bad color name */
			}
		}
		if(strcmpi(string1,"black")==0)
		{
			fscanf(fp,"%s%s",string2,string3);
			val=get_color(string3);
			if(val>=0&&val<MAX_COLORS)
				M.Black=val;
			else
			{
				printf("'%s' %s '%s'\n",string1,string2,string3);
				return(-5);	/* bad color name */
			}
		}
	}while(numread>0&&strcmpi(string1,"begin_vector")!=0);
	if(numread<1)
		return(-3);		/* could not find 'begin_vector' */
	i=0;
	do
	{
		numread=fscanf(fp,"%s",M.Group_Name[i]);
		if(numread==1&&strcmpi(M.Group_Name[i],"begin_names")==0)
		{
			do
			{
				numread==fscanf(fp,"%s",String);
				if(strcmpi(String,"end_names")!=0)
				{
					strcpy(PlaceFile[p].name,String);
					fscanf(fp,"%s%s",string1,string2);
					{
						if(strcmpi(string2,"ON")==0)
							PlaceFile[p].on=1;
						else if(strcmpi(string2,"OFF")==0)
							PlaceFile[p].on=0;
						else
						{
							printf("'%s' is not marked ON or OFF\n\n",
								PlaceFile[p].name);
							exit(0);
						}
					}
					fscanf(fp,"%s%f%f%s",
						PlaceFile[p].filename,
						&PlaceFile[p].max_pix,&PlaceFile[p].min_pix,
						string3);
					val=get_color(string3);
					if(val>=0&&val<MAX_COLORS)
						PlaceFile[p].color=val;
					else
					{
						printf("'%s' %s '%s'\n",string1,string2,string3);
						return(-5);	/* bad color name */
					}
					for(u=0;u<SYM_SIZE;u++)
					{
						fscanf(fp,"%s",string1);
						if(strlen(string1)!=SYM_SIZE)
						{
							printf("Bad symbol -- must be %d x %d\n",
								Sym_Size,Sym_Size);
							printf("%s %s %f %f %d\n",
								PlaceFile[p].filename,
								PlaceFile[p].max_pix,
								PlaceFile[p].min_pix,
								PlaceFile[p].color);
							return(-12);
						}
						for(v=0;v<SYM_SIZE;v++)
							PlaceFile[p].Symbol[u][v]=string1[v];
					}
					p+=1;
					if(p>=NUM_SYM)
					{
						printf("Too many symbols\n");
						exit(0);
					}
					M.NumSymbol=p;
				}
			}while(strcmpi(String,"end_names")!=0);
			numread=3;
		}
		else if(numread==1&&strcmpi(M.Group_Name[i],"fillin_ihs")==0)
		{
			M.Fill_In=3;
			M.Fill_After=i-1;
			fscanf(fp,"%s%d",String,&M.Num_Fill);
			for(m=0;m<M.Num_Fill;m++)
			{
				fscanf(fp,"%s",M.Fill_In_Name[m]);
				numread=fscanf(fp,"%f%f",&M.FI_max[m],&M.FI_min[m]);
				if(numread<2)
				{
					printf("Bad max or min in fill group assignment '%s'\n",
						M.Fill_In_Name[m]);
					exit(0);
				}
			}
			if(numread!=2)
			{
				printf("Could not find max min pixel sizes for fill file\n");
				exit(0);
			}
			do
			{
				fscanf(fp,"%s",string1);
				if(strcmpi(string1,"end")!=0)
				{
					numread=fscanf(fp,"%s%s%s%s",string2,string2a,string2b,string3);
					if(numread<4)
					{
						printf("Error (1) reading fill color %d\n",k+1);
						exit(0);
					}
					if(sscanf(string1,"%d",&k)<1)
					{
						printf("Error (2) reading fill color %d\n",k+1);
						exit(0);
					}
					sscanf(string2,"%f",M.Fill_Color_IHS[k]);
					sscanf(string2a,"%f",M.Fill_Color_IHS[k]+1);
					sscanf(string2b,"%f",M.Fill_Color_IHS[k]+2);
					if(k>M.Max_Fill)
						M.Max_Fill=k;
					M.Fill_Order[n++]=k;
					strcpy(M.Fill_Name[k],string3);
				}
			}while(strcmpi(string1,"end")!=0);
			numread=3;
		}
		else
		{
			if(numread==1&&strcmpi(M.Group_Name[i],"end_vector")!=0)
			{
				numread=fscanf(fp,"%s%d%s",
					string1,&M.Ns[i],string2);
				strcpy(T_Menu_L[i][0],M.Group_Name[i]);
				strcat(T_Menu_L[i][0]," OFF");
				strcpy(T_Menu_L[i][1],M.Group_Name[i]);
				strcat(T_Menu_L[i][1]," ON");
				if(strcmpi(string2,"ON")==0)
					M.Mm[i]=1;
				else
					M.Mm[i]=0;
				if(M.Ns[i]==0||M.Ns[i]>NUM_SIZE)
				{
					printf("Bad M.Ns = %d\n",M.Ns[i]);
					return(-4);	/* too many vectors in a group */
				}
			}
			for(k=0;k<M.Ns[i];k++)
			{
				fscanf(fp,"%s%f%f%s",M.MapFile[i][k].filename,
					&M.MapFile[i][k].max_pix,&M.MapFile[i][k].min_pix,
					string1);
				sprintf(nam,"%s.bin",M.MapFile[i][k].filename);
				val=get_color(string1);
				if(val>=0&&val<MAX_COLORS)
					M.MapFile[i][k].color=val;
				else
				{
					printf("'%s' %f %f '%s'\n",
						M.MapFile[i][k].filename,
						M.MapFile[i][k].max_pix,M.MapFile[i][k].min_pix,
						string1);
					return(-5);	/* bad color name */
				}
			}
			i+=1;
		}
	}while(numread==3&&strcmpi(M.Group_Name[i-1],"end_vector")!=0&&i<NUM_TYPE);
	i-=1;
	M.Num_Type=i;
	strcpy(T_Menu_L[i+0][0],"all ON");
	strcpy(T_Menu_L[i+0][1],"all ON");
	strcpy(T_Menu_L[i+1][0],"all OFF");
	strcpy(T_Menu_L[i+1][1],"all OFF");
	strcpy(T_Menu_L[i+2][0],"exit");
	strcpy(T_Menu_L[i+2][1],"exit");
	fclose(fp);
	return(1);
}



/***********************************************************************
**
**
**
***
************************************************************************/

int video_off()

{
		SetVideoMode(0,&B_Id);
}

/***********************************************************************
**
**
**
**
************************************************************************/

int run_reorder(name)

char *name;

{
	execlp("reorder.exe","reorder",name,"p",NULL);
}


/********************************************************************
**
**	type  g -- grey all lut[] files
**			G -- grey only lut[]
**			i -- initial header map
**			r -- reset lut[] to lutc[]
**
**	iop == 1 don't print RGB etc.
**
**
********************************************************************* */

int set_lut(type,iop)

char type;
int iop;

{
	int i,j,k;

	if(type=='v')
	{
		for(i=0;i<16;i++)
		{
			Lut[i].r=Vcolor[i].r;
			Lut[i].g=Vcolor[i].g;
			Lut[i].b=Vcolor[i].b;
		}
	}
	WritePalette(Lut); 
}


                                               