/********************************************************************\
|*	Portons Copyright © 1998-99 RasSimTech Ltd.                    	*|
|*	Printed in the United States of America.                       	*|
|*	All rights reserved.                                           	*|
|*	                                                               	*|
|*	This product or document is protected by copyright and         	*|
|*	distributed under licenses restricting its use,                	*|
|*	copyrightand distribution under licenses restricting           	*|
|*	its use, copying, distribution and decompilation.              	*|
|*	                                                               	*|
|*	No part of this product or document may be reproduced in       	*|
|*	any form by any means without prior written authorization of   	*|
|*	RasSimTech Ltd.							                     	*|
|*	                                                               	*|
\********************************************************************/
/************************************************************************\
|*	Copyright © 1997 Artificial Horizons, Inc..                    	*|
|*	Printed in the United States of America.                       	*|
|*	44 Montgomery. Suite 1480, San Francisco, CA, 94104 U.S.A.     	*|
|*	All rights reserved.                                           	*|
|*	                                                               	*|
|*	This product or document is protected by copyright and         	*|
|*	distributed under licenses restricting its use,                	*|
|*	copyright and distribution under licenses restricting          	*|
|*	its use, copying, distribution and decompilation.              	*|
|*	                                                               	*|
|*	No part of this product or document may be reproduced in       	*|
|*	any form by any means without prior written authorization of   	*|
|*	Artificial Horizons, Inc. and its licensors.                   	*|
|*	                                                               	*|
|*	Artificial Horizons, Aviator, AVDS, FlightCam, ChaseCam,       	*|
|*	and TowerCam are trademarks or registered trademarks of        	*|
|*	of Artificial Horizons, Inc.                                   	*|
\************************************************************************/

 /***************************************************************************\
|*                                                                           *|
|* Copyright © 1989-1992 by Artificial Horizons®, Inc.  All rights reserved. *|
|*                                                                           *|
|*     This material  constitutes  the trade  secrets  and confidential,     *|
|*     proprietary information of Bruce Factor and Curtis Priem,  and is     *|
|*     under license  to Artificial Horizons, Inc.   The material is not     *|
|*     to be disclosed,  reproduced,  copied,  or used in any manner not     *|
|*     permitted under license from Artificial Horizons, Inc.   For more     *|
|*     information email "ahi-interest@ahi.com".                             *|
|*                                                                           *|
 \***************************************************************************/

#define PROGRAM_NAME		"USGS2AVDS"


#include <stdio.h>
#include <sys/types.h>
#include <math.h>

double dem_long_min, dem_long_max, dem_lat_min, dem_lat_max;
int 	DEM_cood1,DEM_cood2,DEM_colo,DEM_rowo;
double dLong_00, dLat_00, dLong_0c, dLat_0c, dLong_r0, dLat_r0, dLong_rc, dLat_rc;

#define MAX_ARRAY (1600)
int		elev_array[MAX_ARRAY][MAX_ARRAY];


void usgs2rowcol(char*);

main(int argc,char *argv[])
{
	double long_00, lat_00, long_0c, lat_0c, long_r0, lat_r0, long_rc, lat_rc;
	double long_min, long_max, lat_min, lat_max;


	if ( argc != 2 ) {
		fprintf(stderr,"\n usage: %s DEM_filename \n",argv[0]);
		return(-1);
	}

	usgs2rowcol(argv[1]);

	/***** convert elevations *****/
	convert_block(DEM_cood1,DEM_cood2,DEM_colo,DEM_rowo);

	long_min = dem_long_min;
	long_max = dem_long_max;
	lat_min  = dem_lat_min;
	lat_max  = dem_lat_max;

	fprintf(stderr,"long_min = %f \n",long_min);
	fprintf(stderr,"long_max = %f \n",long_max);
	fprintf(stderr,"lat_min = %f \n",lat_min);
	fprintf(stderr,"lat_max = %f \n",lat_max);
 

	/***** calculate longitude and latitude that covers everything *****/
	long_00 = long_min * 1024.0 / 1200.0 - 180.0;
	lat_00  = lat_min  * 1024.0 / 1200.0 -  90.0;
	long_0c = (long_max * 1024.0 + 1023.0) / 1200.0 - 180.0;
	lat_0c  = lat_00;
	long_r0 = long_00;
	lat_r0  = (lat_max  * 1024.0 + 1023.0) / 1200.0 -  90.0;
	long_rc = long_0c;
	lat_rc  = lat_r0;

}


convert_block(x, y,num_rows,num_cols)
int x, y;
int    num_rows, num_cols;
{
  double long_00, lat_00, long_0c, lat_0c, long_r0, lat_r0, long_rc, lat_rc;
  char   actmp[256];


	long_00  = dLong_00;
	lat_00   = dLat_00;
	long_0c  = dLong_0c;
	lat_0c   = dLat_0c;
	long_r0  = dLong_r0;
	lat_r0   = dLat_r0;
	long_rc  = dLong_rc;
	lat_rc   = dLat_rc;

	fprintf(stderr,"long_00=%f \n",long_00);
	fprintf(stderr,"lat_00=%f \n",lat_00);
	fprintf(stderr,"long_0c=%f \n",long_0c);
	fprintf(stderr,"lat_0c=%f \n",lat_0c);
	fprintf(stderr,"long_r0=%f \n",long_r0);
	fprintf(stderr,"lat_r0=%f \n",lat_r0);
	fprintf(stderr,"long_rc=%f \n",long_rc);
	fprintf(stderr,"lat_rc=%f \n",lat_rc);
	/***** convert elevations *****/
	printf("\nPhase 1: converting elevations\n\n");
	sprintf(actmp, "%03d_%03d.%04dx%04d", x, y,num_rows,num_cols);
	if (atl_elevation(long_00, lat_00, long_0c, lat_0c,
					long_r0, lat_r0, long_rc, lat_rc,
					actmp, num_rows, num_cols) == -1)  {
		fprintf(stderr, "%s: error in conversion of elevation\n", PROGRAM_NAME);
		return(-1);
	}
}


/*  usgs2rowcol:
	configures USGS Small Scale (3-arc-second DEM datafiles) for use
	by the AVDS terrain conversion utilities. We obtained the orginal terrain
	data files from the U.S. Geological Survey, National Mapping Information, 
	web site: http://mapping.usgs.gov
*/



#define COLS_OUT (1201)
#define ROWS_OUT (1201)
/* the following are the Type A record position for the indicated information */
#define GRND_COOD_1_1 (22)
#define GRND_COOD_2_1 (GRND_COOD_1_1 + 1)
#define GRND_COOD_3_1 (GRND_COOD_1_1 + 2)
#define GRND_COOD_4_1 (GRND_COOD_1_1 + 3)
#define GRND_COOD_1_2 (GRND_COOD_1_1 + 4)
#define GRND_COOD_2_2 (GRND_COOD_1_1 + 5)
#define GRND_COOD_3_2 (GRND_COOD_1_1 + 6)
#define GRND_COOD_4_2 (GRND_COOD_1_1 + 7)
#define DEMA_ROWS (36)
#define DEMA_COLS (DEMA_ROWS + 1)
/* the following are the Type B record position for the indicated information */
#define DEMB_ROWS (2)
#define DEMB_COLS (DEMB_ROWS + 1)
#define DEMB_MIN_ELEV (7)
#define DEMB_MAX_ELEV (DEMB_MIN_ELEV+1)


void
usgs2rowcol(file1)
char	*file1;
{

	
	FILE	*fp1, *fp2;
	char 	fname1[256],fname2[256];
	char	c,cbuff[256],*cbuffp;
	int		ii,jj,i_outer,i_inner;
	int		scan_ans = 0;
	int		elev_dat;
	int		dema_row,dema_col;
	float	dem_gnd_cood[8];
	int		demb_row,demb_col,X,Y;


	sprintf(fname1,"%s",file1);
	if ( (fp1=fopen(fname1,"r")) == NULL ) {
		fprintf(stderr,"\n error:Could not open '%s'\n",fname1);
		exit(-1);
	}
	fprintf(stderr,"\n\nprocessing: %s \n",fname1);

		/* move pointer over the dem quad name */	
	for (ii=0;ii<143;ii++){
		c = getc(fp1);
		if (c == EOF){
			fprintf(stderr,"error processing: %s \n\n",fname1);
			exit(-1);
		}
	}

	c = ' '; 
	for(ii=0;ii<=DEMA_COLS;ii++){
		while(isspace(c)){		/* skip over white space */
			c = getc(fp1);
			if (c == EOF){
				fprintf(stderr,"error: processing: %s \n\n",fname1);
				exit(-1);
			}
		}
		jj = 0;
		cbuff[jj] = c;
		while((isdigit(c))||(c == '.')||(c == 'D')||(c == 'E')||(c == '+')||(c == '-')){		
			c = getc(fp1);
			if (c == EOF){
				fprintf(stderr,"error: processing: %s \n",fname1);
				exit(-1);
			}
			jj ++;
			cbuff[jj] = (c =='D')?'E':c;
			if (cbuff[jj] == 'E'){	/* allow two digits after exponents */
				c = getc(fp1);
				jj ++;
				cbuff[jj] = c;
				c = getc(fp1);
				jj ++;
				cbuff[jj] = c;
				c = getc(fp1);
				jj ++;
				cbuff[jj] = c;
				c = ' ';
			}
		}
		jj ++;
		cbuff[jj] = ' ';
		cbuffp = cbuff;
		switch(ii){
			case GRND_COOD_1_1:
			case GRND_COOD_2_1:
			case GRND_COOD_3_1:
			case GRND_COOD_4_1:
			case GRND_COOD_1_2:
			case GRND_COOD_2_2:
			case GRND_COOD_3_2:
			case GRND_COOD_4_2:
				scan_ans = sscanf(&cbuffp[0],"%f",
					&dem_gnd_cood[ii - GRND_COOD_1_1]);
				if (scan_ans != 1){
					fprintf(stderr,"error:dem_gnd_cood[%i] string = %s\n",(ii - GRND_COOD_1_1),cbuffp);
					exit(-1);
				}
				break; 
			case DEMA_ROWS:
				scan_ans = sscanf(&cbuffp[0],"%i",&dema_row);
				if (scan_ans != 1){
					fprintf(stderr,"error:DEM_a_row\n");
					exit(-1);
				}
				break; 
			case DEMA_COLS:
				scan_ans = sscanf(&cbuffp[0],"%i",&dema_col);
				if (scan_ans != 1){
					fprintf(stderr,"error:DEM_a_col\n");
					exit(-1);
				}
				break; 
			default:;
		}
	}	

	
	dLong_00 = (dem_gnd_cood[0]/60.0/60.0);
	dLat_00 = (dem_gnd_cood[1]/60.0/60.0);
	dLong_r0 = (dem_gnd_cood[2]/60.0/60.0);
	dLat_r0 = (dem_gnd_cood[3]/60.0/60.0);
	dLong_rc = (dem_gnd_cood[4]/60.0/60.0);
	dLat_rc = (dem_gnd_cood[5]/60.0/60.0);
	dLong_0c = (dem_gnd_cood[6]/60.0/60.0);
	dLat_0c = (dem_gnd_cood[7]/60.0/60.0);

	DEM_cood1 = (dem_gnd_cood[0]/60.0/60.0);
	X = (int) ((DEM_cood1 + 180.0) * 1200.0 * 256.0 * 32.0);
	DEM_cood1 = (X >> 23) + ((X >> 20) & 0x01); 

	DEM_cood2 = (dem_gnd_cood[1]/60.0/60.0);
	Y = (int) ((DEM_cood2 + 90.0) * 1200.0 * 256.0 * 32.0);
	DEM_cood2 = (Y >> 23) + ((Y >> 20) & 0x01); 

	dem_long_min = DEM_cood1;
	dem_long_max = dem_long_min;
	dem_lat_min = DEM_cood2;
	dem_lat_max = dem_lat_min;

	DEM_colo = COLS_OUT;
	DEM_rowo = ROWS_OUT;
	sprintf(fname2,"%03d_%03d.%04dx%04d",
			DEM_cood1,DEM_cood2,COLS_OUT,ROWS_OUT);
	if ( (fp2=fopen(fname2,"wb")) == NULL ) {
		fprintf(stderr,"\n error: Could not open '%s'\n",fname2);
		exit(-1);
	}
	elev_array[0][0] = dema_col;
	for (i_outer = 1; i_outer <= dema_col; i_outer++){
		for(ii=0;ii<=DEMB_MAX_ELEV;ii++){
			while(isspace(c)){		/* skip over white space */
				c = getc(fp1);
				if (c == EOF){
					fprintf(stderr,"error: processing: %s \n\n",fname1);
					exit(-1);
				}
			}
			jj = 0;
			cbuff[jj] = c;
			while((isdigit(c))||(c == '.')||(c == 'D')||(c == '+')||(c == '-')){		
				c = getc(fp1);
				if (c == EOF){
					fprintf(stderr,"error: processing: %s \n\n",fname1);
					exit(-1);
				}
				jj ++;
				cbuff[jj] = (c =='D')?'E':c;
			}		
			cbuffp = cbuff;
			switch(ii){
				case DEMB_ROWS:
					scan_ans = sscanf(&cbuffp[0],"%i",&demb_row);
					if (scan_ans != 1){
						fprintf(stderr,"error: DEM_b_row\n");
						exit(-1);
					}
					break; 
				case DEMB_COLS:
					scan_ans = sscanf(&cbuffp[0],"%i",&demb_col);
					if (scan_ans != 1){
						fprintf(stderr,"error:DEM_b_col\n");
						exit(-1);
					}
					break; 
				default:;
			}
		}	
		elev_array[0][i_outer] = demb_row;
		for (i_inner = 1; i_inner <= demb_row ; i_inner++){
			scan_ans = fscanf(fp1,"%i",&elev_dat);
				if (scan_ans != 1){
					fprintf(stderr,"elev_dat\n");return;
				}
			if(i_outer <= COLS_OUT){
				if(i_inner <= ROWS_OUT){
					elev_array[i_inner][i_outer] = elev_dat;
				}
			}
		}
		fprintf(stderr," \rreading file:[%i][%i] = %i",i_inner,i_outer,elev_dat); 
	}
	for (i_outer = 1; i_outer <= ROWS_OUT; i_outer++){
		for (i_inner = 1; i_inner <= COLS_OUT ; i_inner++){
			fwrite((int *)&elev_array[i_outer][i_inner],sizeof(int),1,fp2);
		}
	fprintf(stderr,"\rwriting file:[%i][%i]",COLS_OUT,(ROWS_OUT-i_outer)); 
	}
	fclose(fp2);
	fprintf(stderr,"\n\n Conversion Saved to file: %s \n",fname2); 
	fclose(fp1);
	fprintf(stderr,"\n"); 

}
