/*  
	mapper.c  3/15/89
	updated  12/14/90

mpp1.bat = cl /AL mapper.c mouseg vmaputil vimage3 mapgraph textview graphlib
           exemod mapper.exe /stack 4000

	mapper plotting software:
	   by Russell A. Ambroziak, Ph. D.  
	   Reston, Virginia,  Phone 703-648-6168

	command line options:

	/e = EGA
	/v = VGA 640x480x16 colors
	/s = VGA 320x200x256 colors
	/y = Extended VGA 640x400x256 colors
	/x = Extended VGA 640x480x256 colors

	/l [path][file_name].[file_type] = output '.LAB' file of points
	/o [path][file_name].[file_type] = output run information file
	/a [path][file_name] = automatic map load on start
	/p [program_name] = passes control to program from point (S)earch
	/r creates a '.MPR' file with ranges of data for all files then stops
	/h help -- prints this message
	/grid [sample rate] = outputs 'grid.lab' entry for a grid of points

	Any path name for data search can by added also, e.g. D:\DATA or
	\MAPPER\DATA\MAP

cursor mode options:

	All Maps
	   (G)raph Data Points   -- see 'graph.c' for instructions
	   (L)ocate Vectors      -- labels line if '*.atr' file exists
		(S)earch for nearest point -- find and labels points if available
	  e(x)it to picture      -- lists '*.lbl' files of points if any
	Color Filled Maps All Modes
	   (k)ey                 -- plots a small key on right side of screen
	   (K)ey                 -- plots a large key on right side of screen
	   (N)ame all            -- prints polygon name at all label points
	   (P)aint area          -- fills a polygon with color entered and
	                            makes entry in '*.lab' file if /l option on
	Color Filled Maps Modes 'X', 'Y' and 'S' Only
	   (n)ame 1              -- labels polygon at cursor location
	   (p)aint mark          -- makes entry in '*.lab' file if /l option on
	   (R)emove lines        -- removes polygon boundaries
	   (E)dit colors         -- changes map color and gives color info
	   (C)olor table output  -- outputs color table
	   (f)lash               -- makes color flash white then return
	   (D)arker              -- makes map fill colors darker but not lines
	   (B)righter            -- makes map fill colors brighter but not lines

The following is the format of a map file.  All comments (which are not part of
the file) are in [square brackets].

p  0.5   18.3  -66.44  10.0

     [  The 1st line has a character and 4 numbers             ]
     [  char is projection                                     ]
     [     p = plate carree or x-y projection (recomended)     ]
     [     a = Albers equal area                               ]
     [     s = polarstereographic                              ]
     [     S = sinusoidal                                      ]
     [     m = Mercator                                        ]
     [  0.5 is pixel size of center pixel in kilometers        ]
     [ 18.3 is latitude of center pixel                        ]
     [-66.4 is longitude of center pixel                       ]
     [ 10.0 is a number of no use but it must be there         ]


begin_color                  [  You can define up to 16 VGA    ]
     black    =   0   0   0  [  (or EGA) colors here for use   ]
     d_grey   =  46  46  46  [  in line work.  The format is   ]
     l_grey   = 128 128 128  [  a color name, =, then a red,   ]
     white    = 255 255 255  [  green, and blue color value.   ]
     buff     = 105  90  75  [                                 ]
     l_buff   = 130 111  93  [                                 ]
     brown    =  96  28  14  [                                 ]
     red      = 255   0   0  [                                 ]
     orange   = 255 128   0  [                                 ]
     yellow   = 160 160   0  [                                 ]
     green    =   0 200   0  [                                 ]
     d_green  =   0 120   0  [                                 ]
     vd_green =   0  72   0  [                                 ]
     blue     =  30  60 255  [                                 ]
     d_blue   =   0  20  60  [                                 ]
     magenta  = 255   0 255  [                                 ]
end_color                    [  this must be here to mark end  ]

menu_background = d_grey     [  The pull down menu can be made ]
           text = l_grey     [  any color but something must   ]
      highlight = white      [  be given for each item.        ]
     background = buff       [  This is the blank screen color.]
      graticule = l_buff     [                                 ]
        lat/lon = d_grey     [                                 ]
          black = black      [                                 ]
begin_vector 
  geology = 2 ON
    east_geo  3.0 0.0 d_grey 
    west_geo  3.0 0.0 d_grey

[ A list of the vector filenames must be given with group      ]
[ names.  You can have up to 25 groups and up to 25 vectors in ]
[ a group.  The group names are used while the program is      ]
[ running to identify vectors for plotting or labeling.  Each  ]
[ group name is followed by '=', the number of vectors in the  ]
[ group and the word 'ON' of 'OFF' to tell wheter to plot it   ]
[ the first time through.                                      ]
[                                                              ]
[ Each vector name is followed by a maximum pixel size, a      ]
[ minimum pixel size and the color (from above) to plot the    ]
[ vector.  In this example we are going to color in geologic   ]
[ formations so the first vectors (lines) to be plotted are    ]
[ the polygons defining the formations on the map.  They will  ]
[ be plotted when the pixel size is less than 3 km.            ]
     fillin_IHS = 1
          geology.blb      3.0   0.0
  2  0.66 234  0.90 Thqdp
  3  0.61 234  0.80 Tgp
  1  0.83 254  0.33 TKgqm
  4  0.81 234  0.50 Thdap
end

[ This entry tells how to fill in the polygons.  It must be    ]
[ located after the polygons are plotted and before other lines]
[ are plotted.  The file 'geology.blb' was made from a file    ]
[ called 'geology.lab' which was made using this program and   ]
[ the /l option, and the cursor mode 'P' option, e.g.          ]
[     mapper /l geology.lab                                    ]
[ then running 'makeblb geology' on the '.lab' file to make    ]
[ the .blb file.                                               ]
[                                                              ]
[  The numbers give the pixel sizes to plot the fill.  The next]
[  1 to 240 lines are polygon fill values (used with the 'P'   ]
[  option in cursor mode) followed by intensity (0-1.0), hue   ]
[ (1-255) and saturation (0-1.0).  Use the cursor mode 'E'     ]
[ option to set and chose these colors.                        ]

pattern
15
2 * * * 2 * * *
* 2 * * 2 * * 2
* * 2 * 2 * 2 *
* * * 2 2 2 * *
2 2 2 2 2 2 2 2
* * * 2 2 2 * *
* * 2 * 2 * 2 *
* 2 * * 2 * * 2
16
9 * * * 9 * * *
* 9 * 9 * * * *
* * 9 * * * * *
* 9 * 9 * * * *
9 * * * 9 * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
end

[ This entry tells how to pattern selected colors with one of  ]
[ the 16 overlay colors.  It must be located just after the    ]
[ color definitions.  This put a l_grey pattern in color 15 and]
[ a yellow pattern in color 16.                                ]


begin_names
   Epithermal_Qtz = OFF  pr_25e.loc  3.0 0.0 blue
        ...........     
        ...*****...
        ..*.....*..
        .*.......*.           [ the map and identify them you  ]
        .*.......*.           [ can use the names option.  You ]
        .*.......*.           [ can have up to 25 icons.  This ]
        .*.......*.           [ one draws a blue circle at each] 
        .*.......*.           [ point in file 'pr_25e.loc'.    ]
        ..*.....*..           [  If you wish to plot icons on  ] 
        ...*****...           [ The file contains 2 points as  ]
        ...........           [ follows:                       ]
[ Boulder field_Topographic_high 42.39267 -70.80325 stn2_14.lbl]
[ Fine_sediment_mixed_with_gravel 42.38796 -70.81313 stn3*.lbl ]
[ If there is an image file the label is given after the       ]
[ location.  It can be a single image or a group of images as  ]
[ is the case with the 2nd entry.  All .lbl files starting with]
[ 'stn3' will be available to be viewed.  You must use the /a  ]
[ command line option with a saved map if you are going to view]
[ images.                                                      ]
   Low-Sulfide_Au = OFF  d:\prico\pr_36a.loc 3.0  0.0 white
        .....*.....
        .....*.....
        ....*.*....
        ....*.*....
        ...*...*...
        ...*...*...
        ..*.....*..
        ..*.....*..
        .*.......*.
        .*********.
        ...........
end_names
[ You can have up to 25 of these icons.  This is a white       ]
[ triangel.  The field must contain 121 entries (11,11).       ]
[ Periods are ignored and * are plotted on the map.  The icon  ]
[ is centere over its center point (6,6) of [5][5] in c.       ]


   faults = 3 ON
       eastf   10.0 0.0 black
       centf   10.0 0.0 black
       westf   10.0 0.0 black
    roads = 1 OFF
       road   100.0 0.0 green
  streams = 1 OFF
       stream 100.0 0.0 blue

end_vector

[  More vectors and an 'end_vector' statement                  ]
	
*/
#include <conio.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "font.h"
#include "mouseg.h"
#include <process.h>
#include <stdlib.h>
#include <errno.h>

#define VGA 0
/*#define NUMBER9  9*/

#ifdef VGA
#define MAX_X 640
#define MENUXC 550
#endif

#ifdef NUMBER9
#define MAX_X  640
#define MENUXC 550
#endif

#define DEG_RAD 0.01745329251				/* converts degrees to radians */
#define MAXDO 1000
#define MAX_COLORS 16
#define STR_LEN 100
#define MIN_PIX  0.000
#define TYPE 'p'

#define BLACK     0
#define D_GREY    1
#define L_GREY    2
#define WHITE     3
#define BUFF      4
#define L_BUFF    5
#define BROWN     6
#define RED       7
#define ORANGE    8
#define YELLOW    9
#define GREEN    10
#define D_GREEN  11
#define VD_GREEN 12
#define BLUE     13
#define D_BLUE   14
#define MAGENTA  15

#define NUM_TYPE 25
#define NUM_SIZE 25
#define NUM_PSIZE 3
#define NUM_FILL  5
#define NUM_SYM  25

#define MENU_M  14
#define MENU_L   8
#define MENU_P   6
#define MENU_IV  2
#define SYM_SIZE 11
#define NUM_DP 100
#define B_LINES 48

#define F_SIZE 20
#define NAME_SIZE 100
#define S_SIZE 40
#define T_SIZE 15
#define MAX_NUM 300
#define NUM_IMAGE 2

int Rxc,Ryc,Repeat=0;	/* multiple screens if Repeat>1 */
char *UpDate="4/27/92";
int AttribOn=0;
char *Menu_iv[MENU_IV]=
	{"zoom",
	"exit image"
};
struct   Color 
{ 
	unsigned char  r, g, b; 
}lutc[256],luts[256]; 
int Black=0,White=191,Center=128,Num_Color=256;
int D_Grey=46,L_Grey=191,M_Grey=128;
struct Color Blk={  0,  0,  0};
struct Color D_G={ 46, 46, 46};
struct Color M_G={127,127,127};
struct Color L_G={191,191,191};
struct Color Wht={255,255,255};
unsigned char ActiveLut;
int Img=0,Sample=1,Xc=0,Yc=0,Speed=16,Row,Col,ZoomSample=1;
char *SubType;
struct equipment
{
	int printers; 		/* number present */
	int sprinter;		/* serial printer installed?  0=no  1=yes */
	int game;			/* game adapter installed? */
	int sports;			/* number of serial ports */
	int dma;				/* DMA chip installed? */
	int drives;			/* number of disk drives */
	int video;			/* video mode 1=40 color 2=80 color 3=80 B/W */
	int disk;			/* any disk drives? */
	int memory;			/* memory size in Kbytes */
};
extern struct equipment Eq;
extern int GF_Order[MAX_NUM];
extern char GF_String[MAX_NUM][S_SIZE];
struct file_id
{
	char name[S_SIZE];
	long size;
	unsigned date,time;
};
extern struct file_id GF_File[MAX_NUM];
extern int Monitor;
char DepthFile[100]={"colo.dpt"};
float D1[MAX_NUM],D2[MAX_NUM];
struct image_header
{
	int row,col;
	int sample;
	int record_bytes,file_records;
	int record_col_header;
	int row_col_header;
	int header_bytes;
	int imbedded_header;
	int sample_bits;
	char infile[100],image[100],Lut[100],pal_type,label[100];
	int vga_color[256];
	struct Color lut[256],lutc[256];
}Hdr[NUM_IMAGE];

int Sym_Size=SYM_SIZE;
char LastMap[STR_LEN],LastFile[STR_LEN],Auto=0;
char *Exit={"exit"};
float BackBright=1.0;
int NumPath=1;
char Path[NUM_DP][STR_LEN];
char String[STR_LEN];
char String[STR_LEN];
int RawOn;
int FontSize=2;
float PixScale;
char Suffix[20];
FILE *FpLab,*FpGrd;
char VideoType='X';
int MaxXs,MaxYs;
char Disk_Dir[100];
char *Menu_V[NUM_TYPE+3];
char *Menu_P[NUM_SYM+3];
char *Menu_M[MENU_M]=
{
	"zoom",						/*  0 */
	"overlays ON/OFF",		/*  1 */
	"reset",						/*  2 */
	"Change projection",		/*  3 */
	"Save map",					/*  4 */
	"Recall map",				/*  5 */
	"cursor mode",				/*  6 */
	"vector plot",				/*  7 */
	"annotate",					/*  8 */
	"draw lines",				/*  9 */
	"graticule",				/* 10 */
	"point plot",				/* 11 */
	"multiple maps",			/* 12 */
	"exit"
};
int Mouse=0;
struct menu Menu_Mouse1[8]=
{
	{
		"Map",' ',1,
		4,
		"Recall",5,1,
		"Save",4,1,
		"Multiple",12,1,
		"Analyze",6,1
	},
	{
		"Zoom",0,1,
		0
	},
	{
		"Plot Data",' ',1,
		2,
		"Points",11,1,
		"Lines",7,1
	},
	{
		"Projection",3,1,
		6,
		"Lambert",31,1,
		"Albers",32,1,
		"Mercator",33,1,
		"P. Stereo.",34,1,
		"sinusoidal",35,1,
		"plt.carree",36,1
	},
	{
		"Draw",' ',1,
		3,
		"Lines",9,1,
		"Text",8,1,
		"Graticule",10,1
	},
	{
		"QUIT",13,1,
		0
	},
	{
		"*"
	}
};

/*
	  0 -- red
	 18 -- orange
	 34 -- yellow-orange
	 50 -- yellow
	 68 -- yellow-green
	 85 -- green
	 95 -- green-cyan
	105 -- cyan
	125 -- blue-cyan
	144 -- blue
	167 -- blue-purple
	190 -- purple
	206 -- magenta-purple
	223 -- magenta
	240 -- red-magenta
	255 -- red
*/
unsigned char MaxHues[256][3]=
{
255,  0,  0,255, 32,  0,255, 42,  0,255, 53,  0,255, 60,  0,255, 68,  0,
255, 73,  0,255, 80,  0,255, 84,  0,255, 90,  0,255, 94,  0,255,100,  0,
255,103,  0,255,108,  0,255,112,  0,255,116,  0,255,121,  0,255,124,  0,
255,128,  0,255,132,  0,255,135,  0,255,139,  0,255,143,  0,255,146,  0,
255,150,  0,255,154,  0,255,157,  0,255,161,  0,255,164,  0,255,168,  0,
255,172,  0,255,176,  0,255,179,  0,255,183,  0,255,186,  0,255,190,  0,
255,194,  0,255,198,  0,255,202,  0,255,206,  0,255,210,  0,255,214,  0,
255,218,  0,255,222,  0,255,227,  0,255,231,  0,255,236,  0,255,241,  0,
255,245,  0,255,250,  0,254,255,  0,249,255,  0,244,255,  0,240,255,  0,
234,255,  0,229,255,  0,224,255,  0,220,255,  0,214,255,  0,209,255,  0,
204,255,  0,199,255,  0,194,255,  0,190,255,  0,184,255,  0,179,255,  0,
174,255,  0,169,255,  0,164,255,  0,158,255,  0,152,255,  0,147,255,  0,
141,255,  0,135,255,  0,130,255,  0,123,255,  0,116,255,  0,109,255,  0,
102,255,  0, 94,255,  0, 87,255,  0, 78,255,  0, 68,255,  0, 58,255,  0,
 42,255,  0, 23,255,  0,  0,255, 36,  0,255, 58,  0,255, 73,  0,255, 87,
  0,255,101,  0,255,113,  0,255,125,  0,255,135,  0,255,146,  0,255,156,
  0,255,166,  0,255,176,  0,255,186,  0,255,196,  0,255,204,  0,255,214,
  0,255,224,  0,255,233,  0,255,243,  0,255,252,  0,248,255,  0,239,255,
  0,230,255,  0,222,255,  0,214,255,  0,207,255,  0,200,255,  0,194,255,
  0,187,255,  0,181,255,  0,174,255,  0,169,255,  0,163,255,  0,157,255,
  0,152,255,  0,146,255,  0,141,255,  0,136,255,  0,131,255,  0,126,255,
  0,121,255,  0,116,255,  0,111,255,  0,106,255,  0,101,255,  0, 96,255,
  0, 92,255,  0, 86,255,  0, 81,255,  0, 77,255,  0, 71,255,  0, 66,255,
  0, 60,255,  0, 53,255,  0, 48,255,  0, 39,255,  0, 32,255,  0, 23,255,
  0,  0,255, 16,  0,255, 23,  0,255, 28,  0,255, 32,  0,255, 36,  0,255,
 39,  0,255, 42,  0,255, 45,  0,255, 48,  0,255, 50,  0,255, 53,  0,255,
 58,  0,255, 60,  0,255, 62,  0,255, 64,  0,255, 66,  0,255, 68,  0,255,
 70,  0,255, 73,  0,255, 75,  0,255, 77,  0,255, 78,  0,255, 81,  0,255,
 83,  0,255, 84,  0,255, 87,  0,255, 89,  0,255, 92,  0,255, 93,  0,255,
 94,  0,255, 97,  0,255, 98,  0,255,101,  0,255,103,  0,255,105,  0,255,
107,  0,255,109,  0,255,111,  0,255,113,  0,255,115,  0,255,117,  0,255,
119,  0,255,122,  0,255,124,  0,255,126,  0,255,129,  0,255,131,  0,255,
134,  0,255,135,  0,255,138,  0,255,140,  0,255,143,  0,255,145,  0,255,
148,  0,255,151,  0,255,154,  0,255,156,  0,255,160,  0,255,163,  0,255,
166,  0,255,169,  0,255,173,  0,255,176,  0,255,179,  0,255,183,  0,255,
188,  0,255,192,  0,255,196,  0,255,200,  0,255,204,  0,255,209,  0,255,
214,  0,255,220,  0,255,225,  0,255,231,  0,255,237,  0,255,244,  0,255,
250,  0,255,255,  0,252,255,  0,245,255,  0,238,255,  0,231,255,  0,224,
255,  0,217,255,  0,210,255,  0,203,255,  0,196,255,  0,190,255,  0,183,
255,  0,176,255,  0,170,255,  0,163,255,  0,156,255,  0,151,255,  0,144,
255,  0,137,255,  0,131,255,  0,125,255,  0,118,255,  0,112,255,  0,105,
255,  0, 98,255,  0, 90,255,  0, 83,255,  0, 77,255,  0, 68,255,  0, 60,
255,  0, 50,255,  0, 42,255,  0, 28,255,  0, 23
};
char T_Menu_L[NUM_TYPE+3][2][50];
char *T_Menu_P[MENU_P+3][2];
float MaxLat,MinLat,MaxLon,MinLon;
FILE *Fpout;
char SaveName[100]={"mapper.out"};
double ll[MAXDO][2];
int todo[2][MAXDO];
unsigned char MT_Buff[12][MAX_X],Mbuff[B_LINES][MAX_X],Buffer[MAX_X];
long MT_Buff_size=7680,Mbuff_size=30720;
unsigned char Inbuff[BUFSIZ],Inbuff2[BUFSIZ],Outbuff[BUFSIZ];
int SaveVec=0,SNum=1;
int ScreenXs,ScreenYs;
struct board
{
	int row,col,color;
}B_Id;
struct map_screen
{
	int xc,yc,xs,ys;
}map_pos;
struct map_input
{
	char *name,type;
	double indat[6];
}miv;
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];
int NumPicture=0,PictureOn[NUM_SYM];
int NumText=0,TextOn[NUM_SYM];
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 Range;
	int Pattern;
	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;
struct pattern
{
	int status;
	char pattern[8][8];
}Pat[240];
int FileRange=0;
struct file_range
{
	float maxlat,minlat;
	float maxlon,minlon;
}Fr[NUM_TYPE][NUM_SIZE];
unsigned char getpt();
char Saveit[50];
char Labelit[100];
int Grid=0;

int fill_in();
int auto_fill_in();
void pix_loc();
int check_point();
int purge();
void remove_boundaries();
void set_lut();
int load_miv();
void screen_mm();
int do_menu(char *[],int,int,int,int,int,int,int,int [128][25]); 
int t_do_menu();
int plot_map();
void get_corners();
int tplot_line();
int unplot_line();
char Zoom();
int convert_bin();
int read_control();
int get_color();
FILE *open_to_read_binary(char *);
FILE *open_to_read_text(char *);
FILE *open_to_write_text(char *);
FILE *open_to_write_binary(char *);
multiple_maps(int xc,int yc,int xs,int ys,float pixsize,
		float clat,float clon,float space);

main(argc,argv)

int argc;
char *argv[];

{
	int i,j,k,p;
	char ans,ans2,cfile[100],string[100];
	int mt=0,lbrite=64,nbrite=127,brite;
	int cx=256,cy=ScreenYs/2;
	int xc,yc,xs,ys,ix,iy,val,speed,isize=FontSize,color;
	float pixsize=12.0,clat=38.0,clon=-95.75,space=10.0;
	int m_num=0,plotmap=0;
	int row=ScreenYs/2,col=ScreenXs/2,mrow=5,mcol=30;
	FILE *fp;
	int point=0;

	ix=ScreenXs/2;
	iy=ScreenYs/2;
	speed=16;

	if(mouse_initialize(4))
	{
		printf("Mouse initialized.\n\n");
		Mouse=4;	/* sensitivity set to maximum (1 is minimum) */
	}
	for(i=0;i<argc;i++)
	{
		if(strcmpi(argv[i],"save")==0)
			strcpy(Saveit,argv[i+1]);
		if(strcmpi(argv[i],"label")==0)
			strcpy(Labelit,argv[i+1]);
		if(argv[i][1]==':')
			add_disk_path(argv[i]);
		if(argv[i][0]=='\\')
			add_disk_path(argv[i]);
		if(strcmpi(argv[i],"/grid")==0)
		{
			sscanf(argv[i+1],"%d",&Grid);
			FpGrd=fopen("grid.lab","wt");
			if(FpGrd)
			{
				printf("Grid Output file = 'grid.lab'\n");
				printf("Sample rate = %d\n\n",Grid);
				printf("HIT ANY KEY TO CONTINUE!\n\n");
				getch();
				fprintf(FpGrd,"   id num   latitude    longitude   color\n");
			}
			else
			{
				printf("Could not open 'grid.lab' to write.\n\n");
				printf("HIT ANY KEY TO CONTINUE!\n\n");
				getch();
			}
		}
		if(argv[i][0]=='/'&&argv[i][1]=='r')
		{
			FileRange=1;
		}
		if(argv[i][0]=='/'&&argv[i][1]=='h')
		{
			print_help();
			exit(0);
		}
		if(argv[i][0]=='/'&&argv[i][1]=='a')
		{
			Auto=1;
			strcpy(LastMap,argv[i+1]);
		}
		if(argv[i][0]=='/'&&argv[i][1]=='o')
		{
			if(M.Out==0&&i<argc-1)
			{
				add_disk_path(argv[i+1]);
				Fpout=fopen(argv[i+1],"wt");
				if(Fpout)
				{
					M.Out=1;
					printf("Output file = '%s'\n",argv[i+1]);
					printf("Hit any key to continue.\n");
					getch();
				}
				else
				{
					printf("Could not open '%s' to write.\n\n",argv[i+1]);
					M.Out=0;
					printf("HIT ANY KEY TO CONTINUE!\n\n");
					getch();
				}
			}
		}
		if(argv[i][0]=='/'&&argv[i][1]=='l')
		{
			FpLab=fopen(argv[i+1],"wt");
			if(FpLab)
			{
				printf("Label Output file = '%s'\n",argv[i+1]);
				printf("HIT ANY KEY TO CONTINUE!\n\n");
				getch();
			}
			else
			{
				printf("Could not open '%s' to write.\n\n",argv[i+1]);
				printf("HIT ANY KEY TO CONTINUE!\n\n");
				getch();
			}
		}
		if(argv[i][0]=='/'&&(argv[i][1]=='e'||argv[i][1]=='E'))
		{
			ScreenYs=380;
			cy=ScreenYs/2;
		}
		if(argv[i][0]=='/'&&argv[i][1]=='R')
			RawOn=1;
		if(argv[i][0]=='/'&&(argv[i][1]=='X'||argv[i][1]=='x'))
			VideoType='X';
		if(argv[i][0]=='/'&&(argv[i][1]=='Y'||argv[i][1]=='y'))
			VideoType='Y';
		if(argv[i][0]=='/'&&(argv[i][1]=='E'||argv[i][1]=='e'))
			VideoType='E';
		if(argv[i][0]=='/'&&(argv[i][1]=='V'||argv[i][1]=='v'))
			VideoType='V';
		if(argv[i][0]=='/'&&(argv[i][1]=='S'||argv[i][1]=='s'))
			VideoType='S';
		if(argv[i][0]=='/'&&(argv[i][1]=='g'||argv[i][1]=='G'))
			M.GratOn=1;
	}
	xc=0;yc=0;xs=ScreenXs;ys=ScreenYs;
	for(i=0;i<NUM_TYPE;i++)
		for(j=0;j<NUM_SIZE;j++)
		{
			Fr[i][j].maxlat= 360.0;
			Fr[i][j].minlat=-360.0;
			Fr[i][j].maxlon= 360.0;
			Fr[i][j].minlon=-360.0;
		}
	video_on();
	M.Pixsize*=PixScale;
	pixsize=M.Pixsize;
	clat=M.Clat;
	clon=M.Clon;
	space=M.Space;	
	cy=ScreenYs/2;
	SetVideoMode(0);
	if(Saveit[0]!=0)
	{
		strcpy(cfile,Saveit);
		if(strlen(cfile)>0)
			val=read_control(cfile);
		else
			exit(0);
		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);
		}
		if(FileRange==1)
		{
			set_range(cfile);
			exit(0);
		}
		video_on();
		M.Pixsize*=PixScale;
		pixsize=M.Pixsize;
		clat=M.Clat;
		clon=M.Clon;
		space=M.Space;	
		cy=ScreenYs/2;
		set_lut('c',0);
		plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,0);
		check_points();
		for(i=0;i<strlen(cfile);i++)
			if(cfile[i]=='.')
				cfile[i]='\0';
		plot_font_h(M.White,16,16,Labelit,FontSize,Font);
		strcat(cfile,Suffix);
		save_map(cfile);
		SetVideoMode(0);
		exit(0);
	}
	if(Auto==1&&strlen(LastMap)>0)
	{
		video_on();
		strcat(LastMap,Suffix);
		if(unsave_map(LastMap)>0)
		{
			check_points();
			plotmap=1;
			ix=ScreenXs/2;
			iy=ScreenYs/2;
			speed=16;
			m_num=6;
			pix_loc(ix,iy,&ix,&iy,&speed);
		}
		else
			SetVideoMode(0);
	}
	if(plotmap==0)
	{
		printf("     Choose a map to start with\n");
		printf("by using the up and down arrow\n");
		printf("keys, 'Pg Up' or 'Pg Dn' keys,\n");
		printf("or by typing the name of the\n");
		printf("map.  Hit 'Enter' to select a\n");
		printf("map.  If the map is not in this\n");
		printf("directory and you have entered\n");
		printf("command line path names, hit\n");
		printf("'Esc' to continue on to other\n");
		printf("paths.\n\n");
		printf("     If you wish to draw a map\n");
		printf("rather than load one that is\n");
		printf("already drawn -- hit 'Esc'\n");
		printf("until the menu contains '*.map'\n");
		printf("files.\n\n");
		printf("     If you copy drawn map\n");
		printf("files from one disc to another,\n");
		printf("the path names may be incorrect\n");
		printf("and you may not be able to\n");
		printf("zoom.\n");
		printf("\nAs of %s\n",UpDate);
		plotmap=0;
		sprintf(string,"*%s",Suffix);
		if(search_list(string,cfile)>=0)
		{
			video_on();
			unsave_map(cfile);
			check_points();
			plotmap=1;
		}
	}
	if(plotmap==0)
	{
		if(search_list("*.map",cfile)<0)
		{
			printf("No maps chosen.\n\n");
			exit(0);
		}
		if(strlen(cfile)>0)
			val=read_control(cfile);
		else
			exit(0);
		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);
		}
		if(FileRange==1)
		{
			set_range(cfile);
			exit(0);
		}
		video_on();
		M.Pixsize*=PixScale;
		pixsize=M.Pixsize;
		clat=M.Clat;
		clon=M.Clon;
		space=M.Space;	
		cy=ScreenYs/2;
		set_lut('c',0);
		plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,0);
		check_points();
	}
	do
	{
		if(Mouse==0)
			m_num=M_do_menu(Menu_M,MENU_M,MENUXC,25,
				m_num,M.Mback,M.Text,M.High,Font);
		else
			m_num=mouse_menu(Menu_Mouse1,M.Mback,M.Text,M.High,
					M.Black,
					0,ScreenXs-1,0,ScreenYs-1,
					row,col,&mrow,&mcol,Mouse,0,BUTTON_UP);
		if(m_num==0)
		{
			pixsize=M.Pixsize;
			clat=M.Clat;
			clon=M.Clon;
			space=M.Space;
			if(Mouse==0)
				ans=Zoom(xc,yc,ScreenXs,ScreenYs,&pixsize,&clat,&clon,&space);
			else
				ans=mouse_Zoom(xc,yc,ScreenXs,ScreenYs,&pixsize,&clat,&clon,&space);
			if(ans!=27)
				plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,0);
		}
		if(m_num==1)
		{
			pixsize=M.Pixsize;
			clat=M.Clat;
			clon=M.Clon;
			space=M.Space;
			T_do_menu(T_Menu_L,M.Num_Type+3,25,25,m_num,M.Mback,M.Text,M.High,
					Font,M.Mm);
			plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,0);
		}
		if(m_num==2)
		{
			xc=0;yc=0;xs=ScreenXs;ys=ScreenYs;
			pixsize=M.Pixsize;
			clat=M.Clat;
			clon=M.Clon;
			space=M.Space;
			plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,0);
		}
		if(m_num==3||(m_num>=31&&m_num<=36))
		{
			pixsize=M.Pixsize;
			clat=M.Clat;
			clon=M.Clon;
			space=M.Space;
			if(Mouse==0)
			{
				save_box(0,M.Black,Mbuff,0,MaxYs-40,MaxXs,40);
				sprintf(String,"Give new projection type.  l=Lambert  a=Albers");
				if(VideoType=='S')
					plot_font_h(M.White,5,MaxYs-38,String,3,Font);
				else
					plot_font_h(M.White,5,MaxYs-38,String,2,Font);
				sprintf(String,
"'m'=Mercator  's'=polar stereographic  'p'=plate carre   S=sinusoidal");
				if(VideoType=='S')
					plot_font_h(M.White,5,MaxYs-18,String,3,Font);
				else
					plot_font_h(M.White,5,MaxYs-18,String,2,Font);
				M.Type=getch();
			}
			else
			{
				if(m_num==31)M.Type='l';
				else if(m_num==32)M.Type='a';
				else if(m_num==33)M.Type='m';
				else if(m_num==34)M.Type='s';
				else if(m_num==35)M.Type='S';
				else if(m_num==36)M.Type='p';
			}
			if(M.Type=='l'||M.Type=='a'||
					M.Type=='m'||M.Type=='s'||M.Type=='p'||M.Type=='S')
				plot_map(xc,yc,ScreenXs,ScreenYs,pixsize,clat,clon,space,0);
			else
			{
				M.Type='p';
				unsave_box(0,M.Black,Mbuff,0,MaxYs-40,MaxXs,40);
			}
		}
		if(m_num==4)
		{
			i=0;
			do
			{
				fclose(fp);
				sprintf(string,"mapper%02d%s",i++,Suffix);
				fp=open_to_read_binary(string);
			}while(fp);
			fclose(fp);
			save_map(string);
		}
		if(m_num==5)
		{
			sprintf(string,"*%s",Suffix);
			SetVideoMode(0);
			if(search_list(string,cfile)>=0)
			{
				video_on();
				unsave_map(cfile);
				check_points();
			}
			else
			{
				printf("No maps chosen.\n\n");
				exit(0);
			}
		}
		if(m_num==6)
		{
			ix=ScreenXs/2;
			iy=ScreenYs/2;
			speed=16;
			pix_loc(ix,iy,&ix,&iy,&speed);
		}
		if(m_num==7)
		{
			pixsize=M.Pixsize;
			vector_plot(pixsize);
		}
		if(m_num==8)
		{
			ix=ScreenXs/2;
			iy=ScreenYs/2;
			isize=FontSize;
			color=M.White;
			while(annotate(&ix,&iy,&isize,&color)!=27);
		}
		if(m_num==9)
		{
			ix=ScreenXs/2;
			iy=ScreenYs/2;
			color=M.White;
			if(Mouse==0)
				while(draw_line(&ix,&iy,&color)!=27);
			else
				mouse_draw_line(&color);
		}
		if(m_num==10)
			add_graticule();
		if(m_num==11)
			point=point_plot(M.Pixsize,point);
		if(m_num==12)
		{
			pixsize=M.Pixsize;
			clat=M.Clat;
			clon=M.Clon;
			space=M.Space;	
			xc=0;yc=0;
			xs=ScreenXs;ys=ScreenYs;
			multiple_maps(xc,yc,xs,ys,pixsize,clat,clon,space);
		}
	}while(m_num!=MENU_M-1); 

	SetVideoMode(0);
	fclose(Fpout);
	fclose(FpLab);
	fclose(FpGrd);
}


/***********************************************************************
**
**
**
***********************************************************************/

int set_range(mapfile)

char *mapfile;

{
	int i,j,k;
	char string[100];
	FILE *fphdr,*fpmpr;
	int numsec;
	float mll[4];
	float mnln,mxln,mnlt,mxlt;
	long offset;

	strcpy(string,mapfile);
	for(k=0;k<strlen(string);k++)
		if(string[k]=='.')
			string[k]='\0';
	strcat(string,".mpr");
	printf("\n\nCreating file '%s'\n\n",string);
	if(!fpmpr)
	{
		printf("Could not open file '%s' to write.\n\n",string);
		exit(0);
	}
	fpmpr=open_to_write_binary(string);
	for(i=0;i<M.Num_Type;i++)
	{
		for(j=0;j<M.Ns[i];j++)
		{
			Fr[i][j].maxlat=Fr[i][j].maxlon=-360.0;
			Fr[i][j].minlat=Fr[i][j].minlon= 360.0;
			strcpy(string,M.MapFile[i][j].filename);
			strcat(string,".hdr");
			fphdr=open_to_read_binary(string);
			if(fphdr)
			{
				fread((char *)&numsec,sizeof(int),1,fphdr);
				printf("file = '%s'  %d segments\n",string,numsec);
				if(Fpout)
					fprintf(Fpout,"file = '%s'  %d segments\n",string,numsec);
				for(k=0;k<numsec;k++)
				{
					fread((char *)&offset,sizeof(long),1,fphdr);
					fread((char *)mll,sizeof(float),4,fphdr);
					mnln=mll[0];mxln=mll[1];mnlt=mll[2];mxlt=mll[3];
					if(mnln<Fr[i][j].minlon)
						Fr[i][j].minlon=mnln;
					if(mnlt<Fr[i][j].minlat)
						Fr[i][j].minlat=mnlt;
					if(mxln>Fr[i][j].maxlon)
						Fr[i][j].maxlon=mxln;
					if(mxlt>Fr[i][j].maxlat)
						Fr[i][j].maxlat=mxlt;
				}
				printf("Latitude[%2d][%2d]:     %11.6f  %11.6f\n",
					i,j,Fr[i][j].minlat,Fr[i][j].maxlat);
				if(Fpout)
					fprintf(Fpout,"Latitude[%2d][%2d]:     %11.6f  %11.6f\n",
						i,j,Fr[i][j].minlat,Fr[i][j].maxlat);
				printf("Longitude[%2d][%2d]:    %11.6f  %11.6f\n",
					i,j,Fr[i][j].minlon,Fr[i][j].maxlon);
				if(Fpout)
					fprintf(Fpout,"Longitude[%2d][%2d]:    %11.6f  %11.6f\n",
					i,j,Fr[i][j].minlon,Fr[i][j].maxlon);
			}
			else
			{
				printf("\nCould not find '%s'\n",string);
				if(Fpout)
					fprintf(Fpout,"\nCould not find '%s'\n",string);
			}
			fclose(fphdr);
		}
	}
	fwrite((char *)Fr,sizeof(struct file_range),NUM_TYPE*NUM_SIZE,fpmpr);
	fclose(fpmpr);
	return(1);
}


/***********************************************************************
**
**  sets NumPicture, PictureOn[], NumText and TextOn[]
**
**  Picture files have file type '.lbl' as last field
**  Text files have file type '.txt' as last field
**
************************************************************************/

int check_points()

{
	int i,j,k;
	FILE *fploc;
	char string[STR_LEN],id[STR_LEN],id1[STR_LEN],*result;
	char dsk,pth[STR_LEN],nam[10],typ[10],str[STR_LEN];
	double lat,lon;

	NumPicture=NumText=0;
	for(i=0;i<M.NumSymbol;i++)
	{
		PictureOn[i]=TextOn[i]=0;		
		fploc=open_to_read_binary(PlaceFile[i].filename);
		if(fploc!=NULL)
		{
			while((result=fgets(string,80,fploc))!=NULL)
			{
				string[strlen(string)-1]='\0';
				if(sscanf(string,"%s%lf%lf%s",id1,&lat,&lon,id)!=4)
				{
					fclose(fploc);
					break;	
				}
				dsk=fsplit(id,pth,nam,typ);
				if(strlen(typ)>0)
				{
					if(PictureOn[i]==0&&strcmpi(typ,"lbl")==0)
					{
						PictureOn[i]=1;
						NumPicture+=1;
					}
					if(TextOn[i]==0&&strcmpi(typ,"lbl")==0)
					{
						TextOn[i]=1;
						NumText+=1;
					}
				}
			}
		}
		fclose(fploc);
	}
}






/**************************************************************************
**
**
**
************************************************************************* */

int vector_label(vi,ix,iy,size)

int vi,ix,iy,size;

{
	int i,j,k,ij;
	int boxsize=10;
	double x,y,maxlat,minlat,maxlon,minlon,mindist=100000000.0,rot,r;
	int v_num=0,val,cx,cy,hit=0,d1,d2=11/size,len;
	long attr,attrib;
	char string[100];
	float pixsize=M.Pixsize;

	x=ix+boxsize;
	y=iy-boxsize;
	xy_ll(x,y,&maxlat,&maxlon);
	x=ix-boxsize;
	y=iy+boxsize;
	xy_ll(x,y,&minlat,&minlon);
	x=ix;
	y=iy;
	i=vi;
	if(i>=0&&i<M.Num_Type)
	{
		for(j=0;j<M.Ns[i];j++)
		{
			if(M.MapFile[i][j].max_pix*PixScale>pixsize
				&&M.MapFile[i][j].min_pix*PixScale<=pixsize
				&&Fr[i][j].maxlon>MinLon
				&&Fr[i][j].minlon<MaxLon
				&&Fr[i][j].maxlat>MinLat
				&&Fr[i][j].minlat<MaxLat)
			{
				if(kbhit()>0)
				{
					val=getch();
					if(val==27)
						return(-2);
				}
				if(check_atr(M.MapFile[i][j].filename,&mindist,x,y,&cx,&cy,
					&attr,&r,(float)maxlat,(float)minlat,
					(float)maxlon,(float)minlon)>0)
				{
					attrib=attr;
					ij=j;
					ix=cx;
					iy=cy;
					hit+=1;
					rot=r;
				}
			}
		}
	}
	if(hit>0)
	{
		sprintf(string,"%ld",attrib);
		len=strlen(string)*15/size;
		d1=len/2;
		if(rot==0.0)
		{
			iy-=d2;
			ix-=d1;
			plot_font_h(M.White,ix,iy,string,size,Font);
/*			plot_font_h(M.MapFile[i][ij].color,ix,iy,string,size,Font);*/
		}
		else if(rot==90.0)
		{
			iy+=d1;
			ix-=d2;
			plot_font_v(M.White,ix,iy,string,size,Font);
/*			plot_font_v(M.MapFile[i][ij].color,ix,iy,string,size,Font);*/
		}
		else
		{
			r=rot*DEG_RAD;
			ix-=d1*cos(r)+d2*sin(r);
			iy+=d1*sin(r)-d2*cos(r);
			plot_font_rot(M.White,ix,iy,string,size,Font,rot);
/*			plot_font_rot(M.MapFile[i][ij].color,ix,iy,string,size,Font,rot);*/
		}
	}
}

/***********************************************************************
**
**
**
***********************************************************************/

int check_atr(region,mindist,cur_x,cur_y,cx,cy,
		attrib,rot,maxlat,minlat,maxlon,minlon)

char *region;
double *mindist,cur_x,cur_y;
int *cx,*cy;
long *attrib;
double *rot;
float maxlat,minlat,maxlon,minlon;

{
	int i,i1,i2,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=1;
	long offset,attr;
	int changed=0,lag=2;
	float px[21],py[21],pi=-1,ox1,oy1,ox2,oy2;

	strcpy(string,region);
	strcat(string,".atr");
	fpatr=open_to_read_binary(string);
	if(!fpatr)
		return(-1);
	strcpy(string,region);
	strcat(string,".hdr");
	fphdr=open_to_read_binary(string);
	if(!fphdr)
		return(-1);
	strcpy(string,region);
	strcat(string,".bin");
	fp=open_to_read_binary(string);
	if(!fp)
		return(-1);
	fread((char *)&numsec,sizeof(int),1,fphdr);
	fread((char *)&numsec,sizeof(int),1,fp);
	for(j=0;j<numsec;j++)
	{
		if(kbhit()>0)
		{
			ival=getch();
			if(ival==27)
			{
				fclose(fp);
				fclose(fpatr);
				fclose(fphdr);
				return(-2);
			}
		}
		fread((char *)&offset,sizeof(long),1,fphdr);
		fread((char *)mll,sizeof(float),4,fphdr);
		fread((char *)&attr,sizeof(long),1,fpatr);
		doit=1;
		mnln=mll[0];mxln=mll[1];mnlt=mll[2];mxlt=mll[3];
		if(mnln>maxlon||mnlt>maxlat||mxln<minlon||mxlt<minlat)
		{
			doit=0;
		}
		if(doit==1)
		{
			numplot+=1;
			fseek(fp,offset,SEEK_SET);
			fread((char *)&numpt,sizeof(int),1,fp);
			fread((char *)mll,sizeof(float),4,fp);
			m=0;
			pi=-1000;
			for(i=0;i<numpt;i++)
			{
				fread((char *)latlon,sizeof(float),2,fp);
				ll_xy(&x,&y,(double)latlon[0],(double)latlon[1]);
				px[i%21]=x;
				py[i%21]=y;
				if(doit==1)
				{
					dist=(cur_x-x)*(cur_x-x)+(cur_y-y)*(cur_y-y);
					if(dist<*mindist)
					{
						*attrib=attr;
						*mindist=dist;
						*cx=x;
						*cy=y;
						changed+=1;
						pi=i;
						i1=i-lag;
						if(i1<0)
							i1=0;
						ox1=px[i1%21];
						oy1=py[i1%21];
					}
				}
				if(i-pi<=lag)
				{
					ox2=x;
					oy2=y;
				}
			}
		}
	}
	fclose(fp);
	fclose(fphdr);
	fclose(fpatr);
	x=ox2-ox1;
	y=oy2-oy1;
	*rot=0.0;
	if(x==0.0)
		*rot=90.0;
	else
	{
		 if(y!=0.0)
			*rot=atan2(-y,x)/DEG_RAD;	
		else
			*rot=0.0;
	}
	if(*rot>90.0)
		*rot-=180.0;
	if(*rot<-90.0)
		*rot+=180.0;
	return(changed);
}



/***********************************************************************
**
**
**
**
************************************************************************/

int picture_view(string)

char *string;

{
	int i,j,k;
	char filename[STR_LEN],str1[50],str2[50],str3[50],str4[50];
	int ans;
	FILE *fp;
	char instring[STR_LEN],infile[STR_LEN];

	if(sscanf(string,"%s%s%s%s",str1,str2,str3,str4)<4)
		return(-1);
	SetVideoMode(0);

	do
	{
		SetCursor(0,0);
		ans=get_file_name(str4,filename);
		if(ans<0)
		{
			video_on();
			return(1);
		}
		strcpy(Hdr[Img].label,filename);
		for(k=0;k<strlen(filename);k++)
		if(filename[k]=='.')
			filename[k]='\0';
		strcpy(string,filename);
		strcat(string,".lbl");		
		if(read_lbl(Hdr[Img].label,Img)>0)
		{
			video_on();
			process_image(Img);
			SetVideoMode(0);			
		}
	}while(ans>=0);
	video_on();
}

/**************************************************************************
**
**
**
************************************************************************* */

int process_image(filenum)

int filenum;

{
	int num=0;
	int ix=ScreenXs/2,iy=ScreenYs/2,speed=16;
	int meni_iv=0;

	load_image(filenum);	
	do
	{
		num=M_do_menu(Menu_iv,MENU_IV,MENUXC,25,num,D_Grey,L_Grey,White,Font);
		if(num==0)
		{
			load_image(filenum);			
		}
	}while(num<MENU_IV-1);
	ZoomSample=1;
}

/**************************************************************************
**
**
**
************************************************************************* */

int load_image(filenum)

int filenum;

{
	int i,j,k;
	int sample=0;
	int xs,ys;
	FILE *fp;
	long offset=0;
	char string[100];

	Row=Hdr[filenum].row;
	Col=Hdr[filenum].col;
	if(VideoType=='S')
		Speed=8;

	if(Hdr[filenum].sample_bits==0)
		Hdr[filenum].sample_bits=8;

	if((Row==0||Col==0)&&Hdr[filenum].row_col_header==0)
		return(-1);
	fp=open_to_read_binary(Hdr[filenum].image);
	if(!fp)
		return(-1);
	if(Hdr[filenum].row_col_header==1)
	{
		fread((char *)&Hdr[filenum].row,sizeof(int),1,fp);
		fread((char *)&Hdr[filenum].col,sizeof(int),1,fp);
		Col=Hdr[filenum].col;
		Row=Hdr[filenum].row;
		if(Row<=0||Col<=0)
			return(-1);
	}
	else if(Hdr[filenum].imbedded_header==1)
	{
		offset=Hdr[filenum].col;
		fseek(fp,offset,SEEK_SET);
	}
	else if(Hdr[filenum].header_bytes>0)
	{
		offset=Hdr[filenum].header_bytes;
		fseek(fp,offset,SEEK_SET);
	}
	video_on();
	do
	{
		Sample=sample+=1;
		Row=Hdr[filenum].row/sample;
		Col=Hdr[filenum].col/sample;
		offset=(sample-1);
		offset*=Hdr[filenum].col;
	}while(Col>ScreenXs||Row>ScreenYs);
	Hdr[filenum].sample=sample;
	if(Hdr[filenum].pal_type>0)
		IV_set_lut(Hdr[filenum].pal_type);
	else
	{
		if(IV_set_lut('P',Hdr[filenum].Lut)<0)
			IV_set_lut('G');
	}
	plot_full_image(fp,sample,filenum);

	if(sample>1)
	{
		sample=ZoomSample;
		if(limit_area_fixed(&Xc,&Yc,&xs,&ys,&Speed,&sample)!=27)
		{
			ZoomSample=sample;
			plot_sub_image(fp,sample,filenum);
		}
		else
		{
			ZoomSample=Sample;
		}
	}
	fclose(fp);
}

/********************************************************************
**
**	type  g -- grey all lut[] files
**			G -- grey only lut[]
**			i -- initial header map
**			r -- reset lut[] to lutc[]
**			p -- positive (0-127) magenta, negative (128-255) green
**
********************************************************************* */

int IV_set_lut(type,filename)

char type,*filename;

{
	int i,k,hue,brite,j;
	int r,g,b;
	float color[3];
	double dbrite,dval;
	float val,minval,sat,inten;
	float fval,fk;
	FILE *fplut;
	int end=0,scale,sum;
	char string[100];
	long del,mindel,l1,l2,l3,l4;

	scale=1;
	if(Num_Color==16)
		scale=17;
	if(type=='P')
	{
		fplut=open_to_read_binary(filename);
		if(fplut)
		{
			i=0;
			do
			{
				i+=1;
				fscanf(fplut,"%s",string);
			}while(strcmpi(string,"end")!=0&&i<50);
			if(strcmpi(string,"end")!=0)
				rewind(fplut);
			for(i=0;i<256;i++)
			{
				fscanf(fplut,"%d%d%d%d",&k,&r,&g,&b);
				Hdr[Img].lut[i].r=r;
				Hdr[Img].lut[i].g=g;
				Hdr[Img].lut[i].b=b;
			}
			fclose(fplut);
		}
		else
			return(-1);
		if(VideoType=='V'||VideoType=='E')
		{
			for(i=0;i<256;i++)
			{
				sum=(int)Hdr[Img].lut[i].r*10;
				sum+=(int)Hdr[Img].lut[i].g*8;
				sum+=(int)Hdr[Img].lut[i].b*2;
				sum/=319;
				Hdr[Img].vga_color[i]=sum;
			}
			for(k=0;k<Num_Color;k++)
			{
				Hdr[Img].lut[k].r=k*scale;
				Hdr[Img].lut[k].g=k*scale;
				Hdr[Img].lut[k].b=k*scale;
			}
		}
		ActiveLut='P';
	}
	if(type=='g')
	{
		for(k=0;k<Num_Color;k++)
		{
			Hdr[Img].lutc[k].r=Hdr[Img].lut[k].r=k*scale;
			Hdr[Img].lutc[k].g=Hdr[Img].lut[k].g=k*scale;
			Hdr[Img].lutc[k].b=Hdr[Img].lut[k].b=k*scale;
		}
		ActiveLut='g';
	}
	if(type=='G')
	{
		for(k=0;k<Num_Color;k++)
		{
			Hdr[Img].lut[k].r=k*scale;
			Hdr[Img].lut[k].g=k*scale;
			Hdr[Img].lut[k].b=k*scale;
		}
		ActiveLut='G';
	}
	if(type=='i')
	{
		for(k=0;k<Num_Color;k++)
		{
			Hdr[Img].lutc[k].r=Hdr[Img].lut[k].r=k*scale;
			Hdr[Img].lutc[k].g=Hdr[Img].lut[k].g=k*scale;
			Hdr[Img].lutc[k].b=Hdr[Img].lut[k].b=k*scale;
		}
		Hdr[Img].lut[ 64].r=  0;Hdr[Img].lut[ 64].g=  0;Hdr[Img].lut[ 64].b=128;
		Hdr[Img].lut[127].r=  0;Hdr[Img].lut[127].g=100;Hdr[Img].lut[127].b=  0;
		Hdr[Img].lut[191].r=255;Hdr[Img].lut[191].g=  0;Hdr[Img].lut[191].b=  0;
		Hdr[Img].lut[192].r=255;Hdr[Img].lut[192].g=128;Hdr[Img].lut[192].b=  0;
		Hdr[Img].lut[193].r=255;Hdr[Img].lut[193].g=255;Hdr[Img].lut[193].b=  0;
		Hdr[Img].lut[194].r=  0;Hdr[Img].lut[194].g=255;Hdr[Img].lut[194].b=  0;
		Hdr[Img].lut[195].r=  0;Hdr[Img].lut[195].g=255;Hdr[Img].lut[195].b=255;
		Hdr[Img].lut[196].r= 20;Hdr[Img].lut[196].g= 55;Hdr[Img].lut[196].b=255;
		Hdr[Img].lut[253].r=100;Hdr[Img].lut[253].g=  1;Hdr[Img].lut[253].b=  1;
		Hdr[Img].lut[254].r=100;Hdr[Img].lut[254].g=  1;Hdr[Img].lut[254].b=  1;
		ActiveLut='i';
	}
	if(type=='r')
	{
		for(k=0;k<Num_Color;k++)
		{
			Hdr[Img].lut[k].r=Hdr[Img].lutc[k].r;
			Hdr[Img].lut[k].g=Hdr[Img].lutc[k].g;
			Hdr[Img].lut[k].b=Hdr[Img].lutc[k].b;
		}
		ActiveLut='r';
	}
	mindel=16777216;
	for(i=0;i<Num_Color;i++)
	{
		l1=Blk.r;
		l2=Hdr[Img].lut[i].r;
		l3=Hdr[Img].lut[i].g;
		l4=Hdr[Img].lut[i].b;
		del=(l1-l2)*(l1-l2)+(l1-l3)*(l1-l3)+(l1-l4)*(l1-l4);
		if(del<mindel)
		{
			mindel=del;
			Black=i;
		}
	}
	mindel=16777216;
	for(i=0;i<Num_Color;i++)
	{
		l1=D_G.r;
		l2=Hdr[Img].lut[i].r;
		l3=Hdr[Img].lut[i].g;
		l4=Hdr[Img].lut[i].b;
		del=(l1-l2)*(l1-l2)+(l1-l3)*(l1-l3)+(l1-l4)*(l1-l4);
		if(del<mindel)
		{
			mindel=del;
			D_Grey=i;
		}
	}
	mindel=16777216;
	for(i=0;i<Num_Color;i++)
	{
		l1=M_G.r;
		l2=Hdr[Img].lut[i].r;
		l3=Hdr[Img].lut[i].g;
		l4=Hdr[Img].lut[i].b;
		del=(l1-l2)*(l1-l2)+(l1-l3)*(l1-l3)+(l1-l4)*(l1-l4);
		if(del<mindel)
		{
			mindel=del;
			M_Grey=i;
		}
	}
	mindel=16777216;
	for(i=0;i<Num_Color;i++)
	{
		l1=L_G.r;
		l2=Hdr[Img].lut[i].r;
		l3=Hdr[Img].lut[i].g;
		l4=Hdr[Img].lut[i].b;
		del=(l1-l2)*(l1-l2)+(l1-l3)*(l1-l3)+(l1-l4)*(l1-l4);
		if(del<mindel)
		{
			mindel=del;
			L_Grey=i;
		}
	}
	mindel=16777216;
	for(i=0;i<Num_Color;i++)
	{
		l1=Wht.r;
		l2=Hdr[Img].lut[i].r;
		l3=Hdr[Img].lut[i].g;
		l4=Hdr[Img].lut[i].b;
		del=(l1-l2)*(l1-l2)+(l1-l3)*(l1-l3)+(l1-l4)*(l1-l4);
		if(del<mindel)
		{
			mindel=del;
			White=i;
		}
	}
	WritePalette(Hdr[Img].lut);
	return(1);
}


/**************************************************************************
**
**
**
************************************************************************* */

int plot_sub_image(fp,sample,filenum)

FILE *fp;
int sample,filenum;

{
	int i,j,k;
	long offset;

	rewind(fp);
	paint_box(3,0,Mbuff,0,0,ScreenXs,ScreenYs);
	offset=Hdr[filenum].col;
	offset*=Yc*Sample;
	offset+=Xc*Sample;
	if(Hdr[filenum].header_bytes>0)
		offset+=Hdr[filenum].header_bytes;
	fseek(fp,offset,SEEK_SET);
	Row=ScreenYs;
	Col=ScreenXs;
	if((Yc*Sample+Row*sample)>Hdr[filenum].row)
		Row=(Hdr[filenum].row-Yc*Sample)/sample;
	if((Xc*Sample+Col*sample)>Hdr[filenum].col)
		Col=(Hdr[filenum].col-Xc*Sample)/sample;
	offset=Hdr[filenum].col;
	offset*=(sample-1);
	for(i=0;i<Row;i++)
	{
		if(fread((char *)Mbuff[0],sizeof(char),Hdr[filenum].col,fp)<1)
		{
			return(-1);
		}
		fseek(fp,offset,SEEK_CUR);
		if(sample>1)
			for(j=0;j<Col;j++)
				Mbuff[0][j]=Mbuff[0][j*sample];
		if(VideoType=='V'||VideoType=='E')
		{
			if(ActiveLut=='P')
			{
				for(j=0;j<Col;j++)
					Mbuff[0][j]=Hdr[filenum].vga_color[Mbuff[0][j]];
			}
			else
			{
				for(j=0;j<Col;j++)
					Mbuff[0][j]/=16;
			}
		}
		plotrow(3,0,Col-1,i,Mbuff[0]);
	}

	return(1);
}


/**************************************************************************
**
**
**
************************************************************************* */

int plot_full_image(fp,sample,filenum)

FILE *fp;
int sample,filenum;

{
	int i,j,k;
	long offset;
	char string[100];
	int row,col;
	FILE *fpsub;

	strcpy(string,Hdr[filenum].image);
	for(i=0;i<strlen(string);i++)
		if(string[i]=='.')
			string[i]='\0';
	strcat(string,SubType);
	fpsub=open_to_read_binary(string);
	if(fpsub)
	{
		fread((char *)&row,sizeof(int),1,fpsub);
		fread((char *)&col,sizeof(int),1,fpsub);
		for(i=0;i<row;i++)
		{
			fread((char *)Mbuff[0],sizeof(char),col,fpsub);
			plotrow(0,0,col-1,i,Mbuff[0]);
		}
		fclose(fpsub);
	}
	else
	{
		rewind(fp);
		offset=Hdr[filenum].header_bytes;
		if(offset>0)
			fseek(fp,offset,SEEK_SET);
		offset=(sample-1);
		offset*=Hdr[filenum].col;
		for(i=0;i<Row;i++)
		{
			fread((char *)Mbuff[0],sizeof(char),Hdr[filenum].col,fp);
			if(offset>0)
				fseek(fp,offset,SEEK_CUR);
			if(sample>1)
				for(j=0;j<Col;j++)
					Mbuff[0][j]=Mbuff[0][j*sample];
			if(VideoType=='V'||VideoType=='E')
			{
				if(ActiveLut=='P')
				{
					for(j=0;j<Col;j++)
						Mbuff[0][j]=Hdr[filenum].vga_color[Mbuff[0][j]];
				}
				else
				{
					for(j=0;j<Col;j++)
						Mbuff[0][j]/=16;
				}
			}
			plotrow(3,0,Col-1,i,Mbuff[0]);
		}
		if(sample>1)
		{
			fpsub=open_to_write_binary(string);
			if(fpsub)
			{
				fwrite((char *)&Row,sizeof(int),1,fpsub);
				fwrite((char *)&Col,sizeof(int),1,fpsub);
				for(i=0;i<Row;i++)
				{
					getrow(0,0,Col-1,i,Mbuff[0]);
					fwrite((char *)Mbuff[0],sizeof(char),Col,fpsub);
				}
				fclose(fpsub);
			}
		}
	}
}



/**************************************************************************
**
**
**
************************************************************************* */

int limit_area_fixed(xc,yc,xs,ys,speed,sample)

int *xc,*yc,*xs,*ys,*speed,*sample;

{
	int i,j,k;
	int xco=*xc,yco=*yc,xso,yso;
	char ans;

	xso=*xs=ScreenXs/Sample*(*sample);
	yso=*ys=ScreenYs/Sample*(*sample);

	tbox_val(3,*xc,*yc,*xs,*ys,Mbuff,Black,White,Center);
	do
	{
		ans=getch();
		unbox(3,*xc,*yc,*xs,*ys,Mbuff);
		if(ans==27)
		{
			return(27);
		}
		else if(ans=='-'&&*speed>1)*speed/=2;
		else if(ans=='+'&&*speed<ScreenYs)*speed*=2;
		else if(ans=='l'&&*sample<Sample)
			*sample+=1;
		else if(ans=='s'&&*sample>1)
			*sample-=1;
		*xs=ScreenXs/Sample*(*sample);
		*ys=ScreenYs/Sample*(*sample);

		if(ans==0)
		{
			ans=getch();
			if(ans==71)
			{
				*xc-=*speed;
				*yc-=*speed;
			}
			else if(ans==73)
			{
				*xc+=*speed;
				*yc-=*speed;
			}
			else if(ans==79)
			{
				*xc-=*speed;
				*yc+=*speed;
			}
			else if(ans==81)
			{
				*xc+=*speed;
				*yc+=*speed;
			}
			else if(ans==72)
				*yc-=*speed;
			else if(ans==80)
				*yc+=*speed;
			else if(ans==75)
				*xc-=*speed;
			else if(ans==77)
				*xc+=*speed;
		}
		if(*xs>ScreenXs)*xs=ScreenXs;
		if(*ys>ScreenYs)*ys=ScreenYs;
		if(*xs<2)*xs=2;
		if(*ys<2)*ys=2;
		if((*xc+*xs)>=ScreenXs)*xc=ScreenXs-*xs;
		if((*yc+*ys)>=ScreenYs)*yc=ScreenYs-*ys;
		if(*xc<0)*xc=0;
		if(*yc<0)*yc=0;
		tbox_val(3,*xc,*yc,*xs,*ys,Mbuff,Black,White,Center);
}while(ans!=83&&ans!=27&&ans!=13);
	unbox(3,*xc,*yc,*xs,*ys,Mbuff);
	return(ans);
}


/**************************************************************************
**
**
**
************************************************************************* */

int read_lbl(label,filenum)

char *label;
int filenum;

{
	int i,j,k;
	char string1[100],string2[100],string3[100];
	FILE *fp;
	int status=1,numread;

	fp=open_to_read_binary(label);
	if(!fp)
		return(-1);
	Hdr[filenum].sample_bits=Hdr[filenum].record_bytes=0;
	Hdr[filenum].file_records=0;
	Hdr[filenum].pal_type=0;
	Hdr[filenum].row_col_header=0;
	while(fscanf(fp,"%s",string1)==1)
	{
		if(strcmpi(string1,"end")==0)
		{
			fclose(fp);
			return(status);
		}
		if(strcmpi(string1,"image_lines")==0&&Hdr[filenum].file_records==0)
		{
			numread=fscanf(fp,"%s%s",string2,string3);
			if(strcmpi(string2,"end")==0||strcmpi(string3,"end")==0||
				numread<2)
			{
				fclose(fp);
				return(status);
			}			
			sscanf(string3,"%d",&Hdr[filenum].row);
			printf("Row = %d\n",Hdr[filenum].row);
		}
		if(strcmpi(string1,"sample_bits")==0)
		{
			numread=fscanf(fp,"%s%s",string2,string3);
			if(strcmpi(string2,"end")==0||strcmpi(string3,"end")==0||
				numread<2)
			{
				fclose(fp);
				return(status);
			}			
			sscanf(string3,"%d",&Hdr[filenum].sample_bits);
			printf("Sample_Bits = %d\n",Hdr[filenum].sample_bits);
		}
		if(strcmpi(string1,"file_records")==0)
		{
			numread=fscanf(fp,"%s%s",string2,string3);
			if(strcmpi(string2,"end")==0||strcmpi(string3,"end")==0||
				numread<2)
			{
				fclose(fp);
				return(status);
			}			
			sscanf(string3,"%d",&Hdr[filenum].row);
			printf("Row = %d\n",Hdr[filenum].row);
			Hdr[filenum].file_records=Hdr[filenum].row;
		}
		if(strcmpi(string1,"line_samples")==0&&Hdr[filenum].record_bytes==0)
		{
			numread=fscanf(fp,"%s%s",string2,string3);
			if(strcmpi(string2,"end")==0||strcmpi(string3,"end")==0||
				numread<2)
			{
				fclose(fp);
				return(status);
			}			
			sscanf(string3,"%d",&Hdr[filenum].col);
			printf("Col = %d\n",Hdr[filenum].col);
		}
		if(strcmpi(string1,"record_bytes")==0||
			strcmpi(string1,"image_record_bytes")==0)
		{
			numread=fscanf(fp,"%s%s",string2,string3);
			if(strcmpi(string2,"end")==0||strcmpi(string3,"end")==0||
				numread<2)
			{
				fclose(fp);
				return(status);
			}			
			sscanf(string3,"%d",&Hdr[filenum].col);
			printf("Col = %d\n",Hdr[filenum].col);
			Hdr[filenum].record_bytes=Hdr[filenum].col;
		}
		if(strcmpi(string1,"header_bytes")==0)
		{
			numread=fscanf(fp,"%s%s",string2,string3);
			if(strcmpi(string2,"end")==0||strcmpi(string3,"end")==0||
				numread<2)
			{
				fclose(fp);
				return(status);
			}			
			sscanf(string3,"%d",&Hdr[filenum].header_bytes);
			printf("Header_Bytes = %d\n",Hdr[filenum].header_bytes);
		}
		if(strcmpi(string1,"image_pointer")==0)
		{
			numread=fscanf(fp,"%s%s",string2,string3);
			if(strcmpi(string2,"end")==0||strcmpi(string3,"end")==0||
				numread<2)
			{
				fclose(fp);
				return(status);
			}			
			k=0;
			for(i=0;i<strlen(string3);i++)
				if(string3[i]!=39)		/* single quote */
					Hdr[filenum].image[k++]=string3[i];
			Hdr[filenum].image[k]='\0';
			printf("Image = %s\n",Hdr[filenum].image);
		}
		if(strcmpi(string1,"pal_type")==0)
		{
			numread=fscanf(fp,"%s%s",string2,string3);
			if(strcmpi(string2,"end")==0||strcmpi(string3,"end")==0||
				numread<2)
			{
				fclose(fp);
				return(status);
			}			
			k=0;
			for(i=0;i<strlen(string3);i++)
				if(string3[i]!=39)		/* single quote */
					string1[k++]=string3[i];
			string1[k]='\0';
			Hdr[filenum].pal_type=string1[0];
			printf("Pal_Type = %c\n",Hdr[filenum].pal_type);
		}
		if(strcmpi(string1,"pal_pointer")==0)
		{
			numread=fscanf(fp,"%s%s",string2,string3);
			if(strcmpi(string2,"end")==0||strcmpi(string3,"end")==0||
				numread<2)
			{
				fclose(fp);
				return(status);
			}			
			k=0;
			for(i=0;i<strlen(string3);i++)
				if(string3[i]!=39)		/* single quote */
					Hdr[filenum].Lut[k++]=string3[i];
			Hdr[filenum].Lut[k]='\0';
			printf("Lut = %s\n",Hdr[filenum].Lut);
		}
		if(strcmpi(string1,"row_col_header")==0)
		{
			numread=fscanf(fp,"%s%s",string2,string3);
			if(strcmpi(string2,"end")==0||strcmpi(string3,"end")==0||
				numread<2)
			{
				fclose(fp);
				return(status);
			}			
			Hdr[filenum].row_col_header=1;
			Hdr[filenum].header_bytes=4;
		}
	}
	fclose(fp);
	return(status);
}

/***********************************************************************
**
**
**
**
************************************************************************/

int add_graticule()

{
	float pixsize=M.Pixsize;
	double sp;

	miv.type=M.Type;
	miv.indat[0]=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=0;	
	map_pos.yc=0;
	map_pos.xs=ScreenXs;
	map_pos.ys=ScreenYs;
	auto_set_cc(miv.type,miv.indat);
	screen_mm();

	if(pixsize>5.0)
	{
		sp=10;
		plot_graticule(Font,M.Grat,M.LatLon,sp,M.GratOn);
	}
	if(pixsize>2.5&&pixsize<=5.0)
	{
		sp=5;
		plot_graticule(Font,M.Grat,M.LatLon,sp,M.GratOn);
	}
	if(pixsize<=2.5&&pixsize>0.5)
	{
		sp=1;
		plot_graticule(Font,M.Grat,M.LatLon,sp,M.GratOn);
	}
	if(pixsize<=0.5&&pixsize>0.25)
	{
		sp=0.5;									
		plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
	}
	if(pixsize<=0.25&&pixsize>0.05)
	{
		sp=0.1;
		plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
	}
	if(pixsize<=0.05&&pixsize>0.025)
	{
		sp=0.05;
		plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
	}
	if(pixsize<=0.025&&pixsize>0.005)
	{
		sp=0.01;
		plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
	}
	if(pixsize<=0.005&&pixsize>0.0025)
	{
		sp=0.005;
		plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
	}
	if(pixsize<=0.0025)
	{
		sp=0.001;
		plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
	}
}



/***********************************************************************
**
**
**
**
************************************************************************/

FILE *open_to_write_text(name)

char *name;

{
	int i,j,k;
	FILE *fp;
	char string[STR_LEN];

	for(j=0;j<NumPath;j++)
	{
		sprintf(string,"%s%s",Path[j],name);
		fp=fopen(string,"wt");
		if(fp)
		{
			strcpy(LastFile,name);
			return(fp);
		}
	}
	clearerr(fp);
	fp=NULL;
	return(fp);
}

/***********************************************************************
**
**
**
**
************************************************************************/

FILE *open_to_write_binary(name)

char *name;

{
	int i,j,k;
	FILE *fp;
	char string[STR_LEN];

	for(j=0;j<NumPath;j++)
	{
		sprintf(string,"%s%s",Path[j],name);
		fp=fopen(string,"wb");
		if(fp)
		{
			strcpy(LastFile,name);
			return(fp);
		}
	}
	clearerr(fp);
	fp=NULL;
	return(fp);
}


/***********************************************************************
**
**
**
**
************************************************************************/

FILE *open_to_read_binary(name)

char *name;

{
	int i,j,k;
	FILE *fp;
	char string[STR_LEN];
	int len;

	string[0]='\0';
	for(j=0;j<NumPath;j++)
	{
		len=strlen(Path[j])+strlen(name);
		if(len>0&&len<STR_LEN);
		{
			sprintf(string,"%s%s",Path[j],name);
			fp=fopen(string,"rb");
			if(fp)
			{
				strcpy(LastFile,name);
				return(fp);
			}	
		}
	}
	clearerr(fp);
	fp=NULL;
	return(fp);
}
/***********************************************************************
**
**
**
**
************************************************************************/

FILE *open_to_read_text(name)

char *name;

{
	int i,j,k;
	FILE *fp;
	char string[STR_LEN];
	int len;

	string[0]='\0';
	for(j=0;j<NumPath;j++)
	{
		len=strlen(Path[j])+strlen(name);
		if(len>0&&len<STR_LEN);
		{
			sprintf(string,"%s%s",Path[j],name);
			fp=fopen(string,"rt");
			if(fp)
			{
				strcpy(LastFile,name);
				return(fp);
			}	
		}
	}
	clearerr(fp);
	fp=NULL;
	return(fp);
}

/**************************************************************************
**
**
**
************************************************************************* */

int add_disk_path(string)

char *string;

{
	int i,j,k;
	char dsk,pth[STR_LEN],nam[10],typ[10],str[STR_LEN];
	int match,add=0;

	dsk=fsplit(string,pth,nam,typ);
	if(dsk>0)
		sprintf(str,"%c:%s",dsk,pth);
	else
		sprintf(str,"%s",pth);
	if((dsk>0||strlen(pth)>0)&&NumPath<NUM_DP)
	{
		match=0;
		for(i=0;i<NumPath;i++)
		{
			if(strcmpi(Path[i],str)==0)
				match=1;
		}
		if(match==0)
		{
			strcpy(Path[NumPath],str);
			NumPath+=1;
			add+=1;
		}
	}
	return(add);
}


/**************************************************************************
**
**
**
************************************************************************* */

int annotate(ix,iy,size,color)

int *ix,*iy,*size,*color;

{
	int i,j,k;
	char string[STR_LEN],ans,ans2;
	int xc=*ix,yc=*iy,xs=ScreenXs-(*ix),ys=28/(*size),speed=16;
	int cxc=0,cyc=ScreenYs-28/(*size),cxs=ScreenXs,cys=28/(*size);
	int background=0;

	if(*color==0)
		background=M.Back;
	save_box(0,background,Mbuff,cxc,cyc,cxs,cys);
	i=0;
	do
	{
		ans=getch();
		if(ans==8&&i>0)
		{
			i-=1;
			string[i]='\0';
			unsave_box(0,background,Mbuff,cxc,cyc,cxs,cys);
			save_box(0,background,Mbuff,cxc,cyc,cxs,cys);
			plot_font_h(*color,cxc,cyc,string,*size,Font);
		}
		if(ans>=0x20)
		{
			string[i]=ans;
			string[i+1]='\0';
			plot_font_h(*color,cxc,cyc,string,*size,Font);
			i+=1;
		}
		if(ans==0)
		{
			ans2=getch();
			if(ans2>=71&&ans2<=73)
				*color+=1;
			if(ans2>=79&&ans2<=81)
				*color-=1;
			if(*color>15)
				*color=0;
			if(*color<0)
				*color=15;
			if(*color==0)
				background=M.Back;
			else
				background=0;
			unsave_box(0,background,Mbuff,cxc,cyc,cxs,cys);
			if(ans2==75&&*size<4)
				*size+=1;
			if(ans2==77&&*size>1)
				*size-=1;
			cyc=ScreenYs-28/(*size);
			ys=cys=28/(*size);
			save_box(0,background,Mbuff,cxc,cyc,cxs,cys);
			plot_font_h(*color,cxc,cyc,string,*size,Font);
		}
	}while(ans!=27&&ans!=13);
	unsave_box(0,background,Mbuff,cxc,cyc,cxs,cys);
	if(ans==27)
		return(ans);
	xs=strlen(string)*15/(*size);
	if(Mouse==0)
	{
		if(limit_area(&xc,&yc,&xs,&ys,&speed,ScreenXs,0,Mbuff)!=27)
			plot_font_h(*color,xc,yc,string,*size,Font);
	}
	else
	{
		if(mouse_limit_area(&xc,&yc,&xs,&ys,&speed,ScreenXs,0,Mbuff)!=27)
			plot_font_h(*color,xc,yc,string,*size,Font);
	}
	*ix=xc;
	*iy=yc;
	return(ans);
}

/**************************************************************************
**
**
**
************************************************************************* */

int draw_line(ix,iy,color)

int *ix,*iy,*color;

{
	int i,j,k;
	char string[STR_LEN],ans,ans2;
	int x1=*ix,x2,y1=*iy,y2,speed=16,xo=*ix,yo=*iy;
	int val;

	val=getpt(0,x1,y1);
	tcursor(3,x1,y1,7,Mbuff);
	cursor(0,x1,y1,M.Comp_Color[val],7);
	do
	{
		ans=getch();
		if(ans==0)
		{
			ans2=getch();
			if(ans2==71)
			{
				x1-=speed;
				y1-=speed;
			}
			if(ans2==73)
			{
				x1+=speed;
				y1-=speed;
			}
			if(ans2==79)
			{
				x1-=speed;
				y1+=speed;
			}
			if(ans2==81)
			{
				x1+=speed;
				y1+=speed;
			}
			if(ans2==72)
				y1-=speed;
			if(ans2==80)
				y1+=speed;
			if(ans2==75)
				x1-=speed;
			if(ans2==77)
				x1+=speed;
			if(x1<0)
				x1=0;
			if(x1>=ScreenXs)
				x1=ScreenXs-1;
			if(y1<0)
				y1=0;
			if(y1>=ScreenYs)
				y1=ScreenYs-1;
		}
		if(ans=='-'&&speed>2)
			speed/=2;
		if(ans=='+'&&speed<128)
			speed*=2;
		uncursor(3,xo,yo,7,Mbuff);
		val=getpt(0,x1,y1);
		tcursor(3,x1,y1,7,Mbuff);
		cursor(0,x1,y1,M.Comp_Color[val],7);
		xo=x1;
		yo=y1;
	}while(ans!=27&&ans!=13);
	uncursor(3,xo,yo,7,Mbuff);
	if(ans==27)
		return(ans);
	xo=x2=x1;yo=y2=y1;
	tplotln_inv_dot(0,x1,y1,x2,y2,Mbuff,ScreenXs);
	tcursor(3,x2,y2,7,Buffer);
	cursor(0,x2,y2,*color,7);
	do
	{
		ans=getch();
		if(ans==0)
		{
			ans2=getch();
			if(ans2==71)
			{
				x2-=speed;
				y2-=speed;
			}
			if(ans2==73)
			{
				x2+=speed;
				y2-=speed;
			}
			if(ans2==79)
			{
				x2-=speed;
				y2+=speed;
			}
			if(ans2==81)
			{
				x2+=speed;
				y2+=speed;
			}
			if(ans2==72)
				y2-=speed;
			if(ans2==80)
				y2+=speed;
			if(ans2==75)
				x2-=speed;
			if(ans2==77)
				x2+=speed;
			if(x2<0)
				x2=0;
			if(x2>=ScreenXs)
				x2=ScreenXs-1;
			if(y2<0)
				y2=0;
			if(y2>=ScreenYs)
				y2=ScreenYs-1;
		}
		if(ans=='c')
			*color-=1;
		if(ans=='C')
			*color+=1;
		if(*color>15)
			*color=0;
		if(*color<0)
			*color=15;
		if(ans=='-'&&speed>2)
			speed/=2;
		if(ans=='+'&&speed<128)
			speed*=2;
		uncursor(0,xo,yo,7,Buffer);
		unplotln(0,x1,y1,xo,yo,Mbuff);
		xo=x2;
		yo=y2;
		tplotln_inv_dot(0,x1,y1,x2,y2,Mbuff,ScreenXs);
		plotln(0,x1,y1,x2,y2,*color);
		tcursor(0,x2,y2,7,Buffer);
		cursor(0,x2,y2,*color,7);
	}while(ans!=27&&ans!=13);
	uncursor(3,xo,yo,7,Buffer);
	unplotln(0,x1,y1,xo,yo,Mbuff);
	if(ans==27)
		return(ans);
	plotln(0,x1,y1,x2,y2,*color);
	*ix=x2;
	*iy=y2;
	return(ans);
}


/**************************************************************************
**
**
**
************************************************************************* */

int vector_plot(pixsize)

float pixsize;

{
	int i,j,k;
	int v_num=0,val;

	for(i=0;i<M.Num_Type;i++)
		Menu_V[i]=M.Group_Name[i];
	Menu_V[M.Num_Type]=Exit;
	v_num=M_do_menu(Menu_V,M.Num_Type+1,MENUXC,25,v_num,
							M.Mback,M.Text,M.High,Font);
	i=v_num;
	if(i>=0&&i<M.Num_Type)
	{
		for(j=0;j<M.Ns[i];j++)
		{
			if(M.MapFile[i][j].max_pix*PixScale>pixsize
				&&M.MapFile[i][j].min_pix*PixScale<=pixsize
				&&Fr[i][j].maxlon>MinLon
				&&Fr[i][j].minlon<MaxLon
				&&Fr[i][j].maxlat>MinLat
				&&Fr[i][j].minlat<MaxLat)
			{
				if(kbhit()>0)
				{
					val=getch();
					if(val==27)
						return(-2);
				}
				convert_bin(M.MapFile[i][j].color,
					M.MapFile[i][j].filename);
			}
		}
	}
	return(0);
}


/**************************************************************************
**
**
**
************************************************************************* */

int point_plot(pixsize,start)

float pixsize;
int start;

{
	int i,j,k;
	int p_num=start,p_numo=start,val;

	for(i=0;i<M.NumSymbol;i++)
		Menu_P[i]=PlaceFile[i].name;
	Menu_P[M.NumSymbol]="Plot All";
	Menu_P[M.NumSymbol+1]="All OFF";
	Menu_P[M.NumSymbol+2]="exit";
	do
	{
		p_num=M_do_menu(Menu_P,M.NumSymbol+3,MENUXC,25,p_num,
								M.Mback,M.Text,M.High,Font);
		i=p_num;
		if(i>=0&&i<M.NumSymbol)
		{
			p_numo=p_num;
			PlaceFile[i].on=1;
			if(PlaceFile[i].on==1&&
					pixsize>=PlaceFile[i].min_pix&&pixsize<PlaceFile[i].max_pix)
				symbol_plot(i,0);
		}
		if(p_num==M.NumSymbol)
		{
			for(i=0;i<M.NumSymbol;i++)
			{
				p_numo=0;
				PlaceFile[i].on=1;
				if(PlaceFile[i].on==1&&
					pixsize>=PlaceFile[i].min_pix&&pixsize<PlaceFile[i].max_pix)
				symbol_plot(i,0);
			}
		}
		if(p_num==M.NumSymbol+1)
		{
			for(i=0;i<M.NumSymbol;i++)
			{
				p_numo=0;
				PlaceFile[i].on=0;
			}
		}
	}while(p_num<M.NumSymbol);
	return(p_numo);
}


/**************************************************************************
**
**
**
************************************************************************* */

int search_list(list,string)

char *list,*string;

{
	int i,j,k;
	int p=-1;
	int val,kbval;

	for(j=0;j<NumPath;j++)
	{
		if(kbhit()>0)
		{
			kbval=getch();
			if(kbval==27)
				return(-2);
		}
		sprintf(String,"%s%s",Path[j],list);
		if(load_names(String)>0)
			val=get_file_name(String,string);
		else
			val=-1;
		if(val>=0)
			return(val);
	}
	return(val);
}


/**************************************************************************
**
**
**
************************************************************************* */

int save_map(name)

char *name;

{
	int i,j,k;
	FILE *fp;
	double lat,lon;

	ScreenXs=MaxXs;
	ScreenYs=MaxYs;
	xy_ll((double)(ScreenXs/2),(double)(ScreenYs/2),&lat,&lon);
	miv.indat[0]=M.Pixsize;
	miv.indat[1]=M.Clat=lat;
	miv.indat[2]=M.Clon=lon;
	miv.indat[3]=M.Clon;
	miv.indat[4]=M.Clat+10;
	miv.indat[5]=M.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);
	fp=open_to_write_binary(name);
	if(!fp)
		return(-1);
	strcpy(LastMap,LastFile);
	M.screen_xs=ScreenXs;
	M.screen_ys=ScreenYs;
	fwrite((char *)&M,sizeof(struct map),1,fp);
	if(M.NumSymbol>0)
		fwrite((char *)PlaceFile,sizeof(struct symbol_name),M.NumSymbol,fp);
	if(M.Range==1)
		fwrite((char *)Fr,sizeof(struct file_range),NUM_TYPE*NUM_SIZE,fp);
	if(M.Pattern>0)
		fwrite((char *)Pat,sizeof(struct pattern),240,fp);
	for(i=0;i<MaxYs;i++)
	{
		getrow(0,0,MaxXs-1,i,Buffer);
		fwrite((char *)Buffer,sizeof(char),MaxXs,fp);
	}
	fclose(fp);
	return(1);
}
/**************************************************************************
**
**
**
************************************************************************* */

int unsave_map(name)

char *name;

{
	int i,j,k;
	FILE *fp;

	ScreenXs=MaxXs;
	ScreenYs=MaxYs;
	fp=open_to_read_binary(name);
	if(fp==NULL)
		return(-1);
	strcpy(LastMap,LastFile);
	setbuf(fp,Inbuff);
	fread((char *)&M,sizeof(struct map),1,fp);
	if(M.NumSymbol>0)
		fread((char *)PlaceFile,sizeof(struct symbol_name),M.NumSymbol,fp);
	if(M.Range==1)
		fread((char *)Fr,sizeof(struct file_range),NUM_TYPE*NUM_SIZE,fp);
	if(M.Pattern>0)
		fread((char *)Pat,sizeof(struct pattern),240,fp);
	ScreenXs=M.screen_xs;
	ScreenYs=M.screen_ys;
	WritePalette(M.lut); 
	for(i=0;i<MaxYs;i++)
	{
		fread((char *)Buffer,sizeof(char),MaxXs,fp);
		plotrow(0,0,MaxXs-1,i,Buffer);
	}
	fclose(fp);
	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;
	miv.type=M.Type;
	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();
	if(M.Num_Type>NUM_TYPE)
	{
		SetVideoMode(0);
		printf("Too many vector groups.\n");
		exit(0);
	}
	for(i=0;i<M.Num_Type;i++)
	{
		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");
	}
	i=M.Num_Type;
	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");
	return(1);
}

/**************************************************************************
**
**
**
************************************************************************* */

int video_on()

{
	int i,row;

	i=GetVideoBoardID();
	if(i==0)
		VideoType='E';
	else if(i==1&&(VideoType=='X'||VideoType=='Y'))
		VideoType='V';
#ifdef NUMBER9
	VideoType='X';
	B_Id.row=1024;
	B_Id.col=1024;
	ScreenXs=1024;
	ScreenYs=1024;
	strcpy(Suffix,".nmp");
	FontSize=1;
#else
	if(VideoType=='X')
	{
		row=SetVideoMode(480,&B_Id);
		if(B_Id.color==256&&B_Id.row==480)
		{
			ScreenXs=640;
			ScreenYs=480;
			strcpy(Suffix,".xmp");
		}
		else if(B_Id.color==256&&B_Id.row==400)
			VideoType='Y';
		else
			VideoType='V';
	}
	if(VideoType=='Y')
	{
		row=SetVideoMode(400,&B_Id);
		if(B_Id.row<400)
			VideoType='V';
		else
		{
			ScreenXs=640;
			ScreenYs=row;
		}
		strcpy(Suffix,".ymp");
	}
	if(VideoType=='V')
	{
		row=SetVideoMode(0x12,&B_Id);
		if(B_Id.row!=480||row!=480||B_Id.color!=16||B_Id.col!=640)
			VideoType='E';
		else
		{
			ScreenXs=640;
			ScreenYs=480;
			Num_Color=M.Num_Color=16;
			strcpy(Suffix,".vmp");
		}
	}
	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;
		Num_Color=M.Num_Color=256;
		strcpy(Suffix,".smp");
		FontSize=3;
	}
	if(VideoType=='E')
	{
		row=SetVideoMode(0x10,&B_Id);
		if(B_Id.row!=350||row<350||B_Id.col!=640)
		{
			SetVideoMode(0,&B_Id);
			printf("Could not boot color board.\n");
			exit(0);
		}
		ScreenXs=640;
		ScreenYs=350;
		Num_Color=M.Num_Color=16;
		strcpy(Suffix,".emp");
	}
	if(B_Id.row<0)
	{
		SetVideoMode(0,&B_Id);
		printf("Could not boot board -- row = %d\n\n",row);
		exit(0);
	}
#endif
	MaxXs=ScreenXs=B_Id.col;
	MaxYs=ScreenYs=B_Id.row;
	PixScale=480.0;
	PixScale/=(float)ScreenYs;
	strcpy(SubType,Suffix);
	SubType[3]='g';
}



/********************************************************************
**
**	WARNING!!!!  YOU MUST CALL decode_font() or set_files() before using
**
**     options:
**
**       -1 -- remove menu but get no answer
**        0 -- return answer and set menu colors to greys
**        1 -- return answer but leave colors alone
**
********************************************************************* */

int M_do_menu(menu,num,xc,yc,start,back,words,boxs,font)

char *menu[];
int num,xc,yc,start,back,words,boxs;
int font[128][25];

{
	int i,j,k;
	char ans=-1,ans2;
	int len=0,l,size=FontSize,val,valo=-1;
	int black=0,dgrey=back,lgrey=words,white=boxs;
	int wide;
	long lxs,lys,buffersize=(long)B_LINES*(long)MAX_X;
	int io=-1,times,jo=-1;
	int row,col,right,left;
	int rowo=-1,colo=-1;

	for(k=0;k<num;k++)
		if((l=strlen(menu[k]))>len)len=l;
	if(len==0)return(-1);
	do
	{
		wide=(len+1)*15/size+1;
		if((ScreenXs-1-wide)<xc)xc=ScreenXs-1-wide;
		lxs=(len+1)*15/size+1;
		lys=30/size*num+1;
		if(lxs*lys>buffersize)
			size+=1;
		if(size>4)
		{
			SetVideoMode(0);			
			printf("Could not fit menu into buffer for 'save_box()'\n");
			exit(0);
		}
	}while(lxs*lys>buffersize);
	if((xc+(len+1)*15/size+1)<ScreenXs)
	{
		if(Mouse>0)
		{
			MouseMove=1;
			mouse_vertical_range(yc,yc+k*(30/size)-1);
			mouse_horizontal_range(xc,xc+(len+1)*15/size);
		}
		save_box(0,dgrey,Mbuff,
			xc,yc,(len+1)*15/size+1,30/size*num+1);
		for(k=0;k<num;k++)
		{	
			box(0,xc,yc+k*(30/size),dgrey,(len+1)*15/size,30/size);
			plot_font_h(lgrey,
					xc+15/size,yc+k*30/size+5/size,menu[k],size,font);
		}
		box(0,xc,yc,lgrey,(len+1)*15/size,30/size*num);

		val=valo=start;
		box(0,xc,yc+val*(30/size),white,(len+1)*15/size,30/size);
		if(Mouse==0)
		{
			do
			{
				if((ans=getch())==0)
				{
					ans2=getch();
					if(ans2==50)
					{
						unsave_box(0,dgrey,Mbuff,
							xc,yc,(len+1)*15/size+1,30/size*num+1);
						getch();
						paint_box(0,dgrey,MT_Buff,
							xc,yc,(len+1)*15/size+1,30/size*num+1);
						for(k=0;k<num;k++)
						{	
							plot_font_h(lgrey,xc+15/size,
								yc+k*30/size+5/size,menu[k],size,font);
						}
					}
					if(ans2=='H')
					{
						if(val>0)val-=1;
						else val=num-1;
					}
					if(ans2=='P')
					{
						if(val<num-1)val+=1;
						else val=0;
					}
				}
				for(i=0;i<num;i++)
					if(ans==menu[i][0])
					{
						val=i;
						ans=13;
					}
				box(0,xc,yc+valo*(30/size),dgrey,(len+1)*15/size,30/size);
				valo=val;
				box(0,xc,yc,lgrey,(len+1)*15/size,30/size*num);
				box(0,xc,yc+val*(30/size),white,(len+1)*15/size,30/size);
				if(ans==27)
				{
					val=num-1;
					ans=13;
				}
			}while(ans!=13);
		}
		else
		{
			mouse_times_released(LEFT_BUTTON,&times,&row,&col);
			mouse_times_released(RIGHT_BUTTON,&times,&row,&col);
			mouse_information(&right,&left,&row,&col);
/*			tcursor(0,col,row,5,MouseBuffer);*/
			colo=col;
			rowo=row;	
			do
			{
				mouse_information(&right,&left,&row,&col);
				if(row!=rowo||col!=colo)
				{
/*					uncursor(0,colo,rowo,5,MouseBuffer);
					tcursor(0,col,row,5,MouseBuffer);*/
					colo=col;
					rowo=row;
				}
				val=row-yc;
				val/=30/size;
				if(val!=valo)
				{
					box(0,xc,yc+valo*(30/size),dgrey,(len+1)*15/size,30/size);
					valo=val;
					box(0,xc,yc,lgrey,(len+1)*15/size,30/size*num);
					box(0,xc,yc+val*(30/size),white,(len+1)*15/size,30/size);
				}
				if(left>0)
				{
					do
					{
						mouse_times_released(LEFT_BUTTON,&times,&row,&col);
					}while(times==0);
					ans=13;
				}
				if(right>0)
				{
					val=num-1;
					ans=13;
					do
					{
						mouse_times_released(RIGHT_BUTTON,&times,&row,&col);
					}while(times==0);
				}
			}while(ans!=13);
			MouseMove=4;
			mouse_vertical_range(0,ScreenYs-1);
			mouse_horizontal_range(0,ScreenXs-1);
		}
	}
	box(0,xc,yc,dgrey,(len+1)*15/size,30/size*num);
	box(0,xc,yc+val*(30/size),lgrey,(len+1)*15/size,30/size);
	unsave_box(0,dgrey,Mbuff,
			xc,yc,(len+1)*15/size+1,30/size*num+1);
	return(val);
}



/********************************************************************
**
**	WARNING!!!!  YOU MUST CALL decode_font() or set_files() before using
**
**     options:
**
**       -1 -- remove menu but get no answer
**        0 -- return answer and set menu colors to greys
**        1 -- return answer but leave colors alone
**
********************************************************************* */

int T_do_menu(menu,num,xc,yc,start,back,words,boxs,font,off_on)

char menu[][2][50];
int num,xc,yc,start,back,words,boxs;
int font[128][25],*off_on;

{
	int i,j,k;
	char ans,ans2;
	int len=0,l,size=FontSize,val,valo;
	int black=0,dgrey=back,lgrey=words,white=boxs;
	int wide;
	long lxs,lys,buffersize=(long)B_LINES*(long)MAX_X;

	for(k=0;k<num;k++)
		if((l=strlen(menu[k][0]))>len)len=l;
	if(len==0)return(-1);
	do
	{
		wide=(len+1)*15/size+1;
		if((ScreenXs-1-wide)<xc)xc=ScreenXs-1-wide;
		lxs=(len+1)*15/size+1;
		lys=30/size*num+1;
		if(lxs*lys>buffersize)
			size+=1;
		if(size>4)
		{
			SetVideoMode(0);			
			printf("Could not fit menu into buffer for 'save_box()'\n");
			exit(0);
		}
	}while(lxs*lys>buffersize);
	if((ScreenXs-1-wide)<xc)xc=ScreenXs-1-wide;
	if((xc+(len+1)*15/size+1)<ScreenXs)
	{
		paint_box(0,dgrey,Mbuff,xc,yc,(len+1)*15/size+1,30/size*num+1);
		for(k=0;k<num-3;k++)
		{	
			box(0,xc,yc+k*(30/size),dgrey,(len+1)*15/size,30/size);
			plot_font_h(lgrey,xc+15/size,yc+k*30/size+5/size,menu[k][off_on[k]],
				size,font);
		}
		for(k=num-3;k<num;k++)
		{	
			box(0,xc,yc+k*(30/size),dgrey,(len+1)*15/size,30/size);
			plot_font_h(lgrey,xc+15/size,yc+k*30/size+5/size,menu[k][0],
				size,font);
		}
		box(0,xc,yc,lgrey,(len+1)*15/size,30/size*num);

		val=valo=start;
		box(0,xc,yc+val*(30/size),white,(len+1)*15/size,30/size);
		do
		{
			ans=getch();
			if(ans==13)
			{
				if(val<num-3)
				{
					if(off_on[val]==0)
						off_on[val]=1;
					else
						off_on[val]=0;
				}
				if(val==num-3)
					for(k=0;k<num-3;k++)
						off_on[k]=1;
				if(val==num-2)
					for(k=0;k<num-3;k++)
						off_on[k]=0;
				paint_box(0,dgrey,Mbuff,xc,yc,
					(len+1)*15/size+1,30/size*num+1);
				for(k=0;k<num-3;k++)
				{	
					box(0,xc,yc+k*(30/size),dgrey,(len+1)*15/size,30/size);
					plot_font_h(lgrey,xc+15/size,yc+k*30/size+5/size,
							menu[k][off_on[k]],size,font);
				}
				for(k=num-3;k<num;k++)
				{	
					box(0,xc,yc+k*(30/size),dgrey,(len+1)*15/size,30/size);
					plot_font_h(lgrey,xc+15/size,yc+k*30/size+5/size,
							menu[k][0],size,font);
				}
				box(0,xc,yc,lgrey,(len+1)*15/size,30/size*num);
			}
			if(ans==13&&val==num-1)
				ans=27;
			if(ans==0)
			{
				ans2=getch();
				if(ans2=='H')
				{
					if(val>0)val-=1;
					else val=num-1;
				}
				if(ans2=='P')
				{
					if(val<num-1)val+=1;
					else val=0;
				}
			}
			for(i=0;i<num;i++)
				if(ans==menu[i][0][0])
				{
					val=i;
					ans=27;
				}
			box(0,xc,yc+valo*(30/size),dgrey,(len+1)*15/size,30/size);
			valo=val;
			box(0,xc,yc,lgrey,(len+1)*15/size,30/size*num);
			box(0,xc,yc+val*(30/size),white,(len+1)*15/size,30/size);
		}while(ans!=27);
	}
	box(0,xc,yc,dgrey,(len+1)*15/size,30/size*num);
	box(0,xc,yc+val*(30/size),lgrey,(len+1)*15/size,30/size);

	return(val);
}



/**************************************************************************
**
**
**
************************************************************************* */

int read_control(filename)

char *filename;

{
	int i,j,k,n=1,m,p=0,u,v,ii,jj;
	FILE *fp,*fpmpr;
	char string1[100],string2[100],string2a[100],string3[100],string2b[100];
	char nam[100],type;
	int numread,val,numcolors;
	int numpat,patend=0;

	fp=open_to_read_binary(filename);
	if(!fp)
		return(-1);
	for(i=0;i<NUM_TYPE;i++)
		for(j=0;j<NUM_SIZE;j++)
		{
			Fr[i][j].maxlat= 360.0;
			Fr[i][j].minlat=-360.0;
			Fr[i][j].maxlon= 360.0;
			Fr[i][j].minlon=-360.0;
		}
	strcpy(string1,filename);
	for(k=0;k<strlen(string1);k++)
		if(string1[k]=='.')
			string1[k]='\0';
	strcat(string1,".mpr");
	M.Range=0;
	fpmpr=open_to_read_binary(string1);
	if(fpmpr)
	{
		fread((char *)Fr,sizeof(struct file_range),NUM_TYPE*NUM_SIZE,fpmpr);
		if(Fpout)
			fprintf(Fpout,"Loaded file '%s'\n",string1);
		M.Range=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]);
			if(strcmpi(M.Color_Name[i],"white")==0)
			{
				M.White=i;
			}
			if(strcmpi(M.Color_Name[i],"black")==0)
			{
				M.Black=i;
			}
		}
		if(numread==4&&M.Out==1&&Fpout)
			fprintf(Fpout,"%20s = %4d%4d%4d\n",M.Color_Name[i],
				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);
			if(M.Out==1&&Fpout)
				fprintf(Fpout,"%16s = %s\n",string1,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);
			if(M.Out==1&&Fpout)
				fprintf(Fpout,"%16s = %s\n",string1,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);
			if(M.Out==1&&Fpout)
				fprintf(Fpout,"%16s = %s\n",string1,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);
			if(M.Out==1&&Fpout)
				fprintf(Fpout,"%16s = %s\n",string1,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);
			if(M.Out==1&&Fpout)
				fprintf(Fpout,"%16s = %s\n",string1,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);
			if(M.Out==1&&Fpout)
				fprintf(Fpout,"%16s = %s\n",string1,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);
			if(M.Out==1&&Fpout)
				fprintf(Fpout,"%16s = %s\n",string1,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]);
				add_disk_path(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],"pattern")==0)
		{
			do
			{
				fscanf(fp,"%s",string1);
				if(strcmpi(string1,"end")==0)
				{
					patend=1;
				}
				else
				{
					sscanf(string1,"%d",&numpat);
					if(numpat>0&&numpat<240)
					{
						Pat[numpat].status=1;
						if(VideoType!='E'&&VideoType!='V')
							M.Pattern=1;
						for(ii=0;ii<8;ii++)
						{
							for(jj=0;jj<8;jj++)
							{
								val=-1;
								fscanf(fp,"%s",string1);
								if(string1[0]>='0'&&string1[0]<='9')
									val=string1[0]-'0';
								else if(string1[0]>='a'&&string1[0]<='f')
									val=string1[0]-'a'+10;
								else if(string1[0]>='A'&&string1[0]<='F')
									val=string1[0]-'A'+10;
								Pat[numpat].pattern[ii][jj]=val;
							}
						}
					}
					else
					{
						printf("error reading pattern in '.map' file.\n\n");
						exit(0);
					}
				}
			}while(patend==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(M.Out==1&&Fpout)
					fprintf(Fpout,"\n%s = %d %s \n",M.Group_Name[i],M.Ns[i],string2);
				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);
				add_disk_path(nam);
				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 */
				}
				if(M.Out==1&&Fpout)
					fprintf(Fpout,"%20s %8.4f %8.4f %10s = %2d\n",
						M.MapFile[i][k].filename,
						M.MapFile[i][k].max_pix,M.MapFile[i][k].min_pix,
						string1,M.MapFile[i][k].color);
			}
			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 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);
}

/********************************************************************
**
**
**
********************************************************************* */

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,string[100];
	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;

	save_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
	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],M.White);
	tplotln_val(0,ix[1]+1,iy[1],ix[2],iy[2],MT_Buff[1],M.White);
	tplotln_val(0,ix[2],iy[2]-1,ix[3],iy[3],MT_Buff[2],M.White);
	tplotln_val(0,ix[3]-1,iy[3],ix[0],iy[0],MT_Buff[3],M.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)cur_iy=yc;
				if(cur_ix<xc)cur_ix=xc;
				if(cur_iy>yc+ys-1)cur_iy=yc+ys-1;
				if(cur_ix>xc+xs-1)cur_ix=xc+xs-1;
			}
		}
		if(ans=='h'||ans=='H')
		{
			txs=ScreenXs;
			ScreenXs=MaxXs;
			unsave_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
			save_box(0,M.Back,Mbuff,
				0,0,MaxXs,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(M.White,1,1,String,FontSize,Font);
			ans=getch();
			unsave_box(0,M.Back,Mbuff,
				0,0,MaxXs,32/lsize);
			ScreenXs=txs;
			save_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
		}		

		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],M.White);
		tplotln_val(0,ix[1]+1,iy[1],ix[2],iy[2],MT_Buff[1],M.White);
		tplotln_val(0,ix[2],iy[2]-1,ix[3],iy[3],MT_Buff[2],M.White);
		tplotln_val(0,ix[3]-1,iy[3],ix[0],iy[0],MT_Buff[3],M.White);
		paint_box(0,M.Back,MT_Buff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
		if(cur_ps>1.0)
			sprintf(string,
				"Box Center:  pixel size = %0.2f km   lat = %9.5f  lon = %10.5f",
				cur_ps,cur_clat,cur_clon);
		else if(cur_ps>0.01)
			sprintf(string,
				"Box Center:  pixel size = %0.2f m   lat = %9.5f  lon = %10.5f",
				cur_ps*1000.0,cur_clat,cur_clon);
		else if(cur_ps>0.0001)
			sprintf(string,
				"Box Center:  pixel size = %0.4f m   lat = %9.5f  lon = %10.5f",
				cur_ps*1000.0,cur_clat,cur_clon);
		else
			sprintf(string,
				"Box Center:  pixel size = %0.6f m   lat = %9.5f  lon = %10.5f",
				cur_ps*1000.0,cur_clat,cur_clon);
		plot_font_h(M.White,2,ScreenYs-30/lsize,string,lsize,Font);
	}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;

	unsave_box(0,M.Back,Mbuff,0,ScreenYs-32/lsize,ScreenXs,32/lsize);
	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);

}




/********************************************************************
**
**
**
********************************************************************* */

void 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 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;

	if(M.Out==1&&Fpout)
	{
		fprintf(Fpout,"\npixsize = %6.3f  clat = %12.5f  clon = %12.5f\n",
			pixsize,clat,clon);
		if(Repeat>1)
		{
			fprintf(Fpout,"Repeat = %d Rxc = %d Ryc = %d\n",Repeat,Rxc,Ryc);
			flushall();
		}
	}
	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=xc;	
	map_pos.yc=yc;
	map_pos.xs=xs;
	map_pos.ys=ys;
	auto_set_cc(miv.type,miv.indat);
	screen_mm();
	if(iop==0)
	{
		paint_box(0,M.Back,MT_Buff,0,0,ScreenXs,ScreenYs);
		if(pixsize>5.0)
		{
			sp=10;
			if(M.Fill_In==0)
				plot_graticule(Font,M.Grat,M.LatLon,sp,M.GratOn);
		}
		if(pixsize>2.5&&pixsize<=5.0)
		{
			sp=5;
			if(M.Fill_In==0)
				plot_graticule(Font,M.Grat,M.LatLon,sp,M.GratOn);
		}
		if(pixsize<=2.5&&pixsize>0.5)
		{
			sp=1;
			if(M.Fill_In==0)
				plot_graticule(Font,M.Grat,M.LatLon,sp,M.GratOn);
		}
		if(pixsize<=0.5&&pixsize>0.25)
		{
			sp=0.5;									
			if(M.Fill_In==0)
				plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
		}
		if(pixsize<=0.25&&pixsize>0.05)
		{
			sp=0.1;
			if(M.Fill_In==0)
				plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
		}
		if(pixsize<=0.05&&pixsize>0.025)
		{
			sp=0.05;
			if(M.Fill_In==0)
				plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
		}
		if(pixsize<=0.025&&pixsize>0.005)
		{
			sp=0.01;
			if(M.Fill_In==0)
				plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
		}
		if(pixsize<=0.005&&pixsize>0.0025)
		{
			sp=0.005;
			if(M.Fill_In==0)
				plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
		}
		if(pixsize<=0.0025)
		{
			sp=0.001;
			if(M.Fill_In==0)
				plot_graticule_frac(Font,M.Grat,M.LatLon,sp,M.GratOn);
		}
		if(iop==0)
		{
			for(i=0;i<M.Num_Type;i++)
			{
				for(j=0;j<M.Ns[i];j++)
				{
					if(M.MapFile[i][j].max_pix*PixScale>pixsize
						&&M.MapFile[i][j].min_pix*PixScale<=pixsize
						&&M.Mm[i]==1
						&&Fr[i][j].maxlon>MinLon
						&&Fr[i][j].minlon<MaxLon
						&&Fr[i][j].maxlat>MinLat
						&&Fr[i][j].minlat<MaxLat)
					{
						if(kbhit()>0)
						{
							val=getch();
							if(val==27)
								return(-1);
						}
						num=convert_bin(M.MapFile[i][j].color,
								M.MapFile[i][j].filename);
						if(num<-1)
							return(-2);
						if(M.Out==1&&Fpout)
							fprintf(Fpout,"%s -- color = %d plotted %d segments\n",
								M.MapFile[i][j].filename,M.MapFile[i][j].color,num);
					}
				}
				if(M.Fill_In>0&&i==M.Fill_After&&M.Mm[i]==1)
				{
					filled=0;
					for(m=0;m<M.Num_Fill;m++)
					{
						if(M.FI_max[m]*PixScale>pixsize&&
										M.FI_min[m]*PixScale<=pixsize)
						{
							num=fill_in_area(M.Fill_In_Name[m]);
							if(num<-1)
								return(-2);
							filled=1;
						}
					}
					if(filled==1)
					{
						save_box(0,M.Black,Mbuff,0,0,MaxXs,36/FontSize);
						if(Mouse==0)
						{
							sprintf(String,
"You are in 'cursor mode', hit any key to continue or F1 for help.");
				plot_font_h(M.White,10/FontSize,5/FontSize,String,FontSize,Font);
							if(getch()==0)
								getch();
						}
						unsave_box(0,M.Black,Mbuff,0,0,MaxXs,36/FontSize);
						pix_loc(ix,iy,&ix,&iy,&speed);
						if(M.Pattern>0)
							make_pattern(0,ScreenXs-1);
					}
				}
			}
			for(i=0;i<M.NumSymbol;i++)
			{
				if(PlaceFile[i].on==1&&
					pixsize>=PlaceFile[i].min_pix&&pixsize<PlaceFile[i].max_pix)
				{
					symbol_plot(i,0);
				}
			}
		}
	}
}

/**************************************************************************
**
**		iop = 0 -- plot only symbols
**				1 -- names
**				2 -- id
**
************************************************************************* */

int symbol_plot(i,iop)

int i,iop;

{
	int j,k,u,v;
	int val;
	FILE *fploc;
	char string[STR_LEN],id[STR_LEN],name[STR_LEN];
	double lat,lon,x,y;
	int ix,iy,ss=SYM_SIZE/2;
	char *result;

	fploc=open_to_read_binary(PlaceFile[i].filename);
	if(fploc)
	{
		while((result=fgets(string,80,fploc))!=NULL)
		{
			string[strlen(string)-1]='\0';
			if(sscanf(string,"%s%lf%lf%s",name,&lat,&lon,id)!=4)
			{
				fclose(fploc);
				return(-1);
			}
			ll_xy(&x,&y,lat,lon);
			ix=x,iy=y;
			if(ix>=0&&ix<ScreenXs&&iy>=0&&iy<ScreenYs)
			{
				if(iop==0)
				{
					for(u=0;u<SYM_SIZE;u++)
					{
						for(v=0;v<SYM_SIZE;v++)
						{
							if(PlaceFile[i].Symbol[u][v]=='*')
								plotpt(0,ix-ss+v,iy-ss+u,PlaceFile[i].color);
						}
					}
				}
				if(iop==1)
					plot_font_h(M.White,ix-ss-strlen(name)*7,
						iy-ss+2,name,2,Font);
				if(iop==2)
					plot_font_h(M.White,ix+ss+2,iy-ss+2,id,2,Font);
			}
		}
	}
	fclose(fploc);
}

/**************************************************************************
**
**
**
************************************************************************* */

int fill_in_area(filename)

char *filename;

{
	int i,j,k,p;
	int idc[2],val1,val2;
	double lat,lon;
	FILE *fpblb;
	double x,y;
	int ix,iy,num;
	char pth[STR_LEN],nam[10],typ[10],string[STR_LEN];

	fpblb=open_to_read_binary(filename);
	split(filename,pth,nam,typ);
	if(!fpblb)
		return(0);
	while(fread((char *)idc,sizeof(int),2,fpblb)==2)
	{
		fread((char *)&lat,sizeof(double),1,fpblb);
		fread((char *)&lon,sizeof(double),1,fpblb);
		ll_xy(&x,&y,lat,lon);
		ix=x;iy=y;
		if(ix>=0&&ix<ScreenXs&&iy>=0&&iy<ScreenYs)
		{
			if(getpt(0,ix,iy)==M.Back)
			{
				if(VideoType=='X'||VideoType=='S'||VideoType=='Y')
				{
					num=auto_fill_in(0,idc[1]+16,Mbuff,todo,ix,iy);
					if(num<-1)
						return(-2);
				}
				else
				{
					val1=M.Fill_Color[idc[1]][0];
					val2=M.Fill_Color[idc[1]][1];
					if(val1==M.Back)
						val1=M.Black;
					if(val2==M.Back)
						val2=M.Black;
					num=auto_fill_in_2(0,val1,val2,Mbuff,todo,ix,iy);
					if(num<-1)
						return(-2);
				}
			}
			else if(getpt(0,ix,iy)!=idc[1]+16)
			{
				if(VideoType=='X'||VideoType=='S'||VideoType=='Y')
					plotpt(0,ix,iy,idc[1]+16);
				else
				{
					if((ix+iy)%2==0)
					{
						val1=M.Fill_Color[idc[1]][0];
						plotpt(0,ix,iy,val1);
					}
					else
					{
						val2=M.Fill_Color[idc[1]][1];
						plotpt(0,ix,iy,val2);
					}
				}
			}
		}
	}
	fclose(fpblb);
	return(1);
}




/********************************************************************
**
**
**
********************************************************************* */

void save_rectangle(xc,yc,xs,ys,fp)

int xc,yc,xs,ys;
FILE *fp;

{
	int i,j,k;

	rewind(fp);

	fwrite((char *)&ys,sizeof(int),1,fp);
	fwrite((char *)&xs,sizeof(int),1,fp);
	for(i=0;i<ys;i++)
	{
		getrow(0,xc,xc+xs-1,i+yc,MT_Buff);
		fwrite((char *)MT_Buff,sizeof(char),xs,fp);
	}
}

/********************************************************************
**
**
**
********************************************************************* */

void recall_rectangle(xc,yc,xs,ys,fp)

int xc,yc,xs,ys;
FILE *fp;

{
	int i,j,k;

	rewind(fp);
	fread((char *)&ys,sizeof(int),1,fp);
	fread((char *)&xs,sizeof(int),1,fp);
	if(xc+xs>ScreenXs)xc=ScreenXs-xs;
	if(yc+ys>ScreenYs)yc=ScreenYs-ys;
	if(xc<0||yc<0)return;

	for(i=0;i<ys;i++)
	{
		fread((char *)MT_Buff,sizeof(char),xs,fp);
		plotrow(1,xc,xc+xs-1,i+yc,MT_Buff);
	}
}


/********************************************************************
**
**
**
********************************************************************* */

void 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 auto_fill_in(hue,val,buffer,todo,cx,cy)

int todo[2][MAXDO];
int hue,val;
unsigned char *buffer;
int cx,cy;

{
	int i,j,k,m,n,l;
	int x=cx,y=cy,on=0;
	int lastup[2],lastdn[2];
	int fillval,cval,kbval;
	double lat,lon,h=0.5;

	fillval=getpt(hue,x,y);
	if(fillval==val)return(-1);

	do
	{
		if(kbhit()>0)
		{
			kbval=getch();
			if(kbval==27)
				return(-2);
		}
		lastup[0]=lastup[1]=lastdn[0]=lastdn[1]=-1;
		for(i=x;i<ScreenXs;i++)
		{
			if(i>=ScreenXs||getpt(hue,i,y)!=fillval)
				break;
			plotpt(hue,i,y,val);
			if(Grid>0&&FpGrd&&i%Grid==0&&y%Grid==0)
			{
				xy_ll((double)i+h,(double)y+h,&lat,&lon);
				fprintf(FpGrd,"   0     0  %9.6lf %11.6lf %3d %3d\n",
					lat,lon,val,val-16);
			}
			on=check_point(hue,i,y,fillval,on,todo);
			if(y>0&&getpt(hue,i,y-1)==fillval)
			{
				lastup[0]=i;
				lastup[1]=y-1;
			}
			if(y<ScreenYs-1&&getpt(hue,i,y+1)==fillval)
			{
				lastdn[0]=i;
				lastdn[1]=y+1;
			}
		}
		if(lastup[0]>=0&&on<MAXDO)
		{
			todo[0][on]=lastup[0];
			todo[1][on]=lastup[1];
			on+=1;
		}
		if(lastdn[0]>=0&&on<MAXDO)
		{
			todo[0][on]=lastdn[0];
			todo[1][on]=lastdn[1];
			on+=1;
		}
		lastup[0]=lastup[1]=lastdn[0]=lastdn[1]=-1;
		if(x>0)
		{
			for(i=x-1;i>=0;i--)
			{
				if(i<0||getpt(hue,i,y)!=fillval)
					break;
				plotpt(hue,i,y,val);
				if(Grid>0&&FpGrd&&i%Grid==0&&y%Grid==0)
				{
					xy_ll((double)i+h,(double)y+h,&lat,&lon);
					fprintf(FpGrd,"   0     0  %9.6lf %11.6lf %3d %3d\n",
						lat,lon,val,val-16);
				}
				on=check_point(hue,i,y,fillval,on,todo);
				if(y>0&&getpt(hue,i,y-1)==fillval)
				{
					lastup[0]=i;
					lastup[1]=y-1;
				}
				if(y<ScreenYs-1&&getpt(hue,i,y+1)==fillval)
				{
					lastdn[0]=i;
					lastdn[1]=y+1;
				}
			}
		}
		if(lastup[0]>=0&&on<MAXDO)
		{
			todo[0][on]=lastup[0];
			todo[1][on]=lastup[1];
			on+=1;
		}
		if(lastdn[0]>=0&&on<MAXDO)
		{
			todo[0][on]=lastdn[0];
			todo[1][on]=lastdn[1];
			on+=1;
		}
		on=purge(hue,fillval,todo,on);
		if(on>0)
		{
			x=todo[0][on-1];
			y=todo[1][on-1];
		}
	}while(on>0);
}

/***********************************************************************
**
**
**
***********************************************************************/

int auto_fill_in_2(hue,val1,val2,buffer,todo,cx,cy)

int todo[2][MAXDO];
int hue,val1,val2;
unsigned char *buffer;
int cx,cy;

{
	int i,j,k,m,n,l;
	int x=cx,y=cy,on=0;
	int lastup[2],lastdn[2];
	int fillval,cval,kbval;

	fillval=getpt(hue,x,y);
	if(fillval==val1||fillval==val2)
		return(-1);
	do
	{
		if(kbhit()>0)
		{
			kbval=getch();
			if(kbval==27)
				return(-2);
		}
		lastup[0]=lastup[1]=lastdn[0]=lastdn[1]=-1;
		for(i=x;i<ScreenXs;i++)
		{
			if(i>=ScreenXs||getpt(hue,i,y)!=fillval)
				break;
			if(((i+y)%2)==0)
				plotpt(hue,i,y,val1);
			else
				plotpt(hue,i,y,val2);
			on=check_point(hue,i,y,fillval,on,todo);
			if(y>0&&getpt(hue,i,y-1)==fillval)
			{
				lastup[0]=i;
				lastup[1]=y-1;
			}
			if(y<ScreenYs-1&&getpt(hue,i,y+1)==fillval)
			{
				lastdn[0]=i;
				lastdn[1]=y+1;
			}
		}
		if(lastup[0]>=0&&on<MAXDO)
		{
			todo[0][on]=lastup[0];
			todo[1][on]=lastup[1];
			on+=1;
		}
		if(lastdn[0]>=0&&on<MAXDO)
		{
			todo[0][on]=lastdn[0];
			todo[1][on]=lastdn[1];
			on+=1;
		}
		lastup[0]=lastup[1]=lastdn[0]=lastdn[1]=-1;
		if(x>0)
		{
			for(i=x-1;i>=0;i--)
			{
				if(i<0||getpt(hue,i,y)!=fillval)
					break;
				if(((i+y)%2)==0)
					plotpt(hue,i,y,val1);
				else
					plotpt(hue,i,y,val2);
				on=check_point(hue,i,y,fillval,on,todo);
				if(y>0&&getpt(hue,i,y-1)==fillval)
				{
					lastup[0]=i;
					lastup[1]=y-1;
				}
				if(y<ScreenYs-1&&getpt(hue,i,y+1)==fillval)
				{
					lastdn[0]=i;
					lastdn[1]=y+1;
				}
			}
		}
		if(lastup[0]>=0&&on<MAXDO)
		{
			todo[0][on]=lastup[0];
			todo[1][on]=lastup[1];
			on+=1;
		}
		if(lastdn[0]>=0&&on<MAXDO)
		{
			todo[0][on]=lastdn[0];
			todo[1][on]=lastdn[1];
			on+=1;
		}
		on=purge(hue,fillval,todo,on);
		if(on>0)
		{
			x=todo[0][on-1];
			y=todo[1][on-1];
		}
	}while(on>0);
}


/***********************************************************************
**
**
**
***********************************************************************/

int convert_bin(val,region)

char *region;
int val;

{
	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=open_to_read_binary(string);
	if(!fp)
	{
		if(M.Out==1&&Fpout)
			fprintf(Fpout,"could not open '%s'\n",string);
		return(-1);
	}
	strcpy(string,region);
	strcat(string,".hdr");
	fphdr=open_to_read_binary(string);
	if(!fphdr)
	{
		if(M.Out==1&&Fpout)
			fprintf(Fpout,"could not open '%s'\n",string);
		fclose(fp);
		return(-1);
	}
	if(AttribOn==1)
	{
		strcpy(string,region);
		strcat(string,".atr");
		fpatr=open_to_read_binary(string);
		if(fpatr)
			atr=1;
	}
	fread((char *)&numsec,sizeof(int),1,fphdr);
	fread((char *)&numsec,sizeof(int),1,fp);
	for(j=0;j<numsec;j++)
	{
		if(kbhit()>0)
		{
			ival=getch();
			if(ival==27)
			{
				fclose(fp);
				fclose(fphdr);
				return(-2);
			}
		}
		fread((char *)&offset,sizeof(long),1,fphdr);
		fread((char *)mll,sizeof(float),4,fphdr);
		if(atr==1)
			fread((char *)&attrib,sizeof(long),1,fpatr);
		doit=1;
		mnln=mll[0];mxln=mll[1];mnlt=mll[2];mxlt=mll[3];
		if(mnln>MaxLon||mnlt>MaxLat||mxln<MinLon||mxlt<MinLat)
			doit=0;
		if(doit==1)
		{
			numplot+=1;
			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++)
			{
				fread((char *)latlon,sizeof(float),2,fp);
				if(doit==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;
					dist=(xo-x)*(xo-x)+(yo-y)*(yo-y);
					if(dist<1000000.0)
					{
						plotpt(0,ix,iy,val);
						if(i>0)
							plotln(0,ix,iy,ixo,iyo,val);
						ixo=ix;iyo=iy;
						xo=ix;yo=iy;
						if(atr==1&&i==0)
						{
							sprintf(string,"%ld",attrib);
							plot_font_h(M.White,ix+2,iy-3,string,2,Font);
						}
					}
				}
			}
		}
	}
	fclose(fp);
	fclose(fphdr);
	return(numplot);
}

/**************************************************************************
**
**
**
************************************************************************* */

int split(string,path,name,type)

char *string,*path,*name,*type;

{
	int i,j,k;
	int dot=-1,last_slash=-1,len=strlen(string),num=0;

	if(strlen(string)>STR_LEN)
	{
		SetVideoMode(0,&B_Id);
		printf("In split() -- string 1 too long!\n\n");
		exit(0);
	}

	for(i=0;i<len;i++)
	{
		if(string[i]=='.')
			dot=i;
		if(string[i]=='\\')
			last_slash=i;
	}
	last_slash+=1;
	if(dot==-1)
		dot=len-1;
	if(last_slash>0)
	{
		for(i=0;i<last_slash;i++)
			path[i]=string[i];
	}
	path[last_slash]='\0';
	for(i=last_slash;i<dot;i++)
		name[i-last_slash]=string[i];
	name[dot-last_slash]='\0';
	dot+=1;
	for(i=dot;i<len;i++)
		type[i-dot]=string[i];
	type[len-dot]='\0';
	if(strlen(name)>0)
		num+=1;
	if(strlen(type)>0)
		num+=1;

	return(num);
}



/********************************************************************
**
**
**
********************************************************************* */

void remove_boundaries(val)

int val;

{
	int i,j,k;
	int xc=0,yc=0,xs=ScreenXs,ys=ScreenYs;
	int x1=xc-1,x2=xc+xs;

	if(x1<0)x1=0;
	if(x2>ScreenXs-1)x2=ScreenXs-1;

	for(i=yc;i<yc+2;i++)	/* load first 2 rows */
		getrow(0,xc,xc+xs-1,i,MT_Buff[i%3]);
	for(i=yc+1;i<yc+ys-1;i++)
	{
		getrow(0,xc,xc+xs-1,i+1,MT_Buff[(i+1)%3]);
		MT_Buff[3][0]=MT_Buff[i%3][0];
		MT_Buff[3][xs-1]=MT_Buff[i%3][xs-1];
		for(j=1;j<xs-1;j++)
		{
			if(MT_Buff[i%3][j]==val)
			{
				if(MT_Buff[(i-1)%3][j+1]!=val)
					MT_Buff[3][j]=MT_Buff[(i-1)%3][j+1];
				else if(MT_Buff[(i-1)%3][j]!=val)
					MT_Buff[3][j]=MT_Buff[(i-1)%3][j];
				else if(MT_Buff[i%3][j+1]!=val)
					MT_Buff[3][j]=MT_Buff[i%3][j+1];
				else if(MT_Buff[(i-1)%3][j-1]!=val)
					MT_Buff[3][j]=MT_Buff[(i-1)%3][j-1];
			}
			else
				MT_Buff[3][j]=MT_Buff[i%3][j];
		}
		plotrow(0,xc,xc+xs-1,i,MT_Buff[3]);
	}
}


/********************************************************************
**
**
**
********************************************************************* */

int purge(hue,fillval,todo,on)

int hue,fillval,todo[2][MAXDO],on;

{
	int i,j,k;

	k=0;
	for(i=0;i<on;i++)
	{
		if(getpt(hue,todo[0][i],todo[1][i])==fillval)
		{
			todo[0][k]=todo[0][i];
			todo[1][k]=todo[1][i];
			k+=1;
		}		
	}
	return(k);
}





/********************************************************************
**
**
**
********************************************************************* */

int check_point(hue,x,y,fillval,on,todo)

int hue,x,y,fillval,on,todo[2][MAXDO];

{
	if(on>=MAXDO)return(on);

	if(y>0&&x>0&&x<ScreenXs-1&&getpt(hue,x,y-1)==fillval)
	{
		if(getpt(hue,x-1,y-1)!=fillval||getpt(hue,x+1,y-1)!=fillval)
		{
			todo[0][on]=x;
			todo[1][on]=y-1;
			on+=1;
		}
	}
	if(on>=MAXDO)return(on);

	if(y<ScreenYs-1&&x>0&&x<ScreenXs-1&&getpt(hue,x,y+1)==fillval)
	{
		if(getpt(hue,x-1,y+1)!=fillval||getpt(hue,x+1,y+1)!=fillval)
		{
			todo[0][on]=x;
			todo[1][on]=y+1;
			on+=1;
		}
	}
	return(on);
}



/***********************************************************************
**
**
**
**
************************************************************************/

int search_points(cx,cy,pixsize,outstring)

int *cx,*cy;
float pixsize;
char *outstring;

{
	int i,j,k;
	int ix,iy;
	double dcx=*cx,dcy=*cy,dx,dy,dist,mindist=1000000000,lat,lon;
	FILE *fploc;
	char string[STR_LEN],id[STR_LEN],id1[STR_LEN],*result;
	int numpoints=0;

	outstring[0]='\0';
	for(i=0;i<M.NumSymbol;i++)
	{
		if(PlaceFile[i].on==1&&
			pixsize>=PlaceFile[i].min_pix&&pixsize<PlaceFile[i].max_pix)
		{
			fploc=open_to_read_binary(PlaceFile[i].filename);
			if(fploc!=NULL)
			{
				while((result=fgets(string,80,fploc))!=NULL&&strlen(string)>4)
				{
					string[strlen(string)-1]='\0';
					if(sscanf(string,"%s%lf%lf%s",id1,&lat,&lon,id)!=4)
					{
						fclose(fploc);
						break;	
					}
					ll_xy(&dx,&dy,lat,lon);
					ix=dx,iy=dy;
					if(ix>=0&&ix<ScreenXs&&iy>=0&&iy<ScreenYs&&
						strlen(string)<STR_LEN)
					{
						dist=(dx-dcx)*(dx-dcx)+(dy-dcy)*(dy-dcy);
						if(dist<mindist)
						{
							*cx=ix;*cy=iy;
							mindist=dist;
							strcpy(outstring,string);
							numpoints+=1;
						}
					}
				}
				fclose(fploc);
			}
		}
	}
	return(numpoints);
}




/***********************************************************************
**
**
**
**
************************************************************************/

int set_back()

{
	int i,j,k;
	float r,g,b;
	float max=0.0;

	for(i=0;i<256;i++)
	{
		lutc[i].r=M.lut[i].r;
		lutc[i].g=M.lut[i].g;
		lutc[i].b=M.lut[i].b;
	}
	for(i=15;i<256;i++)
	{
		r=M.lut[i].r;
		g=M.lut[i].g;
		b=M.lut[i].b;
		if(r>max)max=r;
		if(g>max)max=g;
		if(b>max)max=b;
	}
	if(255.0/max<BackBright)
		BackBright=255.0/max;
	for(i=16;i<256;i++)
	{
		r=M.lut[i].r;
		g=M.lut[i].g;
		b=M.lut[i].b;
		lutc[i].r=r*BackBright;
		lutc[i].g=g*BackBright;
		lutc[i].b=b*BackBright;
	}
	WritePalette(lutc); 
}


/**************************************************************************
**
**
**
**
***************************************************************************/

int plot_key(xc,yc,size)

int xc,yc,size;

{
	int i,j,k,l;
	int high=30/size,wide=0;
	int marg=6/size,val[2];
	unsigned char buffer[640];
	int x1=xc,y1=yc,color,twide,x1o;

	M.KeyPlotted=1;
	for(k=1;k<=M.Max_Fill;k++)
		if(strlen(M.Fill_Name[k])>wide)
			wide=strlen(M.Fill_Name[k]);
	wide=(wide+1)*15/size;
	twide=((M.Max_Fill-1)/(ScreenYs/high)+1)*wide;
	if(xc+twide>ScreenXs)
		x1=ScreenXs-twide;
	paint_box(0,M.Back,MT_Buff,x1,yc,twide,ScreenYs);
	x1o=x1;
	for(i=1;i<=M.Max_Fill;i++)
	{	
		k=M.Fill_Order[i];
		if(VideoType=='X'||VideoType=='S'||VideoType=='Y')
		{
			val[0]=val[1]=k+16;
		}
		else
		{
			val[0]=M.Fill_Color[k][0];
			val[1]=M.Fill_Color[k][1];
		}
		for(j=0;j<wide+1;j++)
			buffer[j]=val[j%2];
		for(l=0;l<high;l++)
			plotrow(0,x1,x1+wide-1,l+y1,buffer+(l%2));
		color=M.Comp_Color[k+16];
		if(strlen(M.Fill_Name[k])>0)
			plot_font_h(color,x1+marg,y1+marg,
				M.Fill_Name[k],size,Font);
		y1+=high;
		if(y1+high>ScreenYs)
		{
			y1=yc;
			x1+=wide;
		}
	}
	make_pattern(x1o,ScreenXs-1);
	ScreenXs-=twide;
}


/********************************************************************
**
**	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.
**
**
********************************************************************* */

void set_lut(type,iop)

char type;
int iop;

{
	int k,hue,brite,j,m,n;
	int color[3],h;
	double dbrite,dval;
	float val,minval,i,s,rgb[3];
	int c1,c2;
	long mindist,dist,r,g,b,r0,g0,b0;
	long del,mindel,l1,l2,l3,l4;

/*
	if(type=='g')
	{
		M.lut[ 0].r=  0;
		M.lut[ 0].g=  0;
		M.lut[ 0].b=  0;
		M.lut[ 1].r= 46;
		M.lut[ 1].g= 46;
		M.lut[ 1].b= 46;
		M.lut[ 2].r=128;
		M.lut[ 2].g=128;
		M.lut[ 2].b=128;
		M.lut[ 3].r=255;
		M.lut[ 3].g=255;
		M.lut[ 3].b=255;
	}
*/
	if(type=='c')
	{
		for(k=0;k<MAX_COLORS;k++)
		{		
			M.lut[ k].r=M.Color_Val[k][0];
			M.lut[ k].g=M.Color_Val[k][1];
			M.lut[ k].b=M.Color_Val[k][2];
		}
		for(k=MAX_COLORS;k<256;k++)
		{
			M.lut[ k].r=k;
			M.lut[ k].g=k;
			M.lut[ k].b=k;
		}
		if(M.Fill_In==1)
		{
			for(k=0;k<=M.Max_Fill;k++)
			{		
				M.lut[k+16].r=(M.Color_Val[M.Fill_Color[k][0]][0]+
								 M.Color_Val[M.Fill_Color[k][1]][0])/2;
				M.lut[k+16].g=(M.Color_Val[M.Fill_Color[k][0]][1]+
								 M.Color_Val[M.Fill_Color[k][1]][1])/2;
				M.lut[k+16].b=(M.Color_Val[M.Fill_Color[k][0]][2]+
								 M.Color_Val[M.Fill_Color[k][1]][2])/2;
			}
		}
		if(M.Fill_In==2)
		{
			for(k=1;k<=M.Max_Fill;k++)
			{		
				r0=M.lut[k+16].r=M.Fill_Color_RGB[k][0];
				g0=M.lut[k+16].g=M.Fill_Color_RGB[k][1];
				b0=M.lut[k+16].b=M.Fill_Color_RGB[k][2];
				mindist=16777216;
				for(m=0;m<MAX_COLORS;m++)
				{
					for(n=m;n<MAX_COLORS;n++)
					{
						r=(long)(M.Color_Val[m][0]+M.Color_Val[n][0])/2;
						g=(long)(M.Color_Val[m][1]+M.Color_Val[n][1])/2;
						b=(long)(M.Color_Val[m][2]+M.Color_Val[n][2])/2;
						dist=(r-r0)*(r-r0)+(g-g0)*(g-g0)+(b-b0)*(b-b0);
						if(dist<mindist&&m!=M.Back&&n!=M.Back)
						{
							mindist=dist;
							M.Fill_Color[k][0]=c1=m;
							M.Fill_Color[k][1]=c2=n;
						}
					}
				}
				if(Fpout&&iop!=1)
					fprintf(Fpout," %8s %8s %s\n",
						M.Color_Name[c1],M.Color_Name[c2],M.Fill_Name[k]);
			}
		}
		if(M.Fill_In==3)
		{
			for(k=1;k<=M.Max_Fill;k++)
			{		
				i=M.Fill_Color_IHS[k][0];
				h=M.Fill_Color_IHS[k][1];
				s=M.Fill_Color_IHS[k][2];
				if(Fpout&&iop!=1)
					fprintf(Fpout,"%3d %5.2f %3d %5.2f   ",k,i,h,s);
				for(j=0;j<3;j++)
				{
					rgb[j]=MaxHues[h][j];
					rgb[j]+=(255.0-rgb[j])*(1.0-s);
					rgb[j]*=i;
					if(rgb[j]<0.0)
						rgb[j]=0.0;
					if(rgb[j]>255.0)
						rgb[j]=255.0;
					if(Fpout&&iop!=1)
						fprintf(Fpout,"%3.0f ",rgb[j]);
				}
				r0=M.lut[k+16].r=rgb[0];
				g0=M.lut[k+16].g=rgb[1];
				b0=M.lut[k+16].b=rgb[2];
				mindist=16777216;
				for(m=0;m<MAX_COLORS;m++)
				{
					for(n=m;n<MAX_COLORS;n++)
					{
						r=(long)(M.Color_Val[m][0]+M.Color_Val[n][0])/2;
						g=(long)(M.Color_Val[m][1]+M.Color_Val[n][1])/2;
						b=(long)(M.Color_Val[m][2]+M.Color_Val[n][2])/2;
						dist=(r-r0)*(r-r0)+(g-g0)*(g-g0)+(b-b0)*(b-b0);
						if(dist<mindist&&m!=M.Back&&n!=M.Back)
						{
							mindist=dist;
							M.Fill_Color[k][0]=c1=m;
							M.Fill_Color[k][1]=c2=n;
						}
					}
				}
				if(Fpout&&iop!=1)
					fprintf(Fpout," %8s %8s %s\n",
						M.Color_Name[c1],M.Color_Name[c2],M.Fill_Name[k]);
			}
		}
		for(k=0;k<256;k++)
		{
			if(M.lut[k].r>128||M.lut[k].g>128||M.lut[k].b>160)
				M.Comp_Color[k]=M.Black;
			else
				M.Comp_Color[k]=M.White;
		}
	}
	WritePalette(M.lut); 
}


                                                                                       