/*
	asc2lbs.c


	Takes an ASCII lon-lat-depth file and makes an LBS + DAT file.
	Data must be by rows starting in upper left corner.




*/
#include <stdio.h>

main(int argc,char *argv[])

{
	int i,j,k;
	double lat,lon,lato,lono;
	float dep;
	FILE *fpin,*fplbs,*fpd32;
	char string[100],name[100],cr=0xd;
	int row,col;
	double latmax=-90,latmin=90,lonmax=-360,lonmin=360,maxz=0,minz=0;
	double dlat=1000,dlon=1000,d;
	long num=0,rnum,n=0;

	if(argc<3)
		help();
	fpin=fopen(argv[1],"rt");
	if(!fpin)
	{
		printf("Could not open file '%s' to read.\n\n",argv[1]);
		help();
	}
	strcpy(name,argv[2]);
	for(k=0;k<strlen(name);k++)
		if(name[k]=='.')
			name[k]='\0';
	sprintf(string,"%s.d32",name);
	fpd32=fopen(string,"wb");
	if(!fpd32)
	{
		printf("Could not open '%s' to write.\n\n",string);
		help();
	}
	sprintf(string,"%s.lbs",name);
	fplbs=fopen(string,"wt");
	if(!fplbs)
	{
		printf("Could not open '%s' to write.\n\n",string);
		help();
	}
	rnum=i=0;
	while(fscanf(fpin,"%lf%lf%f",&lon,&lat,&dep)==3)
	{
		if(minz==0.0&&maxz==0.0)
		{
			maxz=minz=dep;
		}
		fwrite((char *)&dep,sizeof(float),1,fpd32);
		if(lon>lonmax)
			lonmax=lon;
		if(lon<lonmin)
			lonmin=lon;
		if(lon!=lono)
		{
			d=lono-lon;
			if(d<0)
				d*=-1.0;
			if(d<dlon)
				dlon=d;
		}
		if(lat>latmax)
			latmax=lat;
		if(lat<latmin)
			latmin=lat;
		if(lat!=lato)
		{
			d=lato-lat;
			if(d<0)
				d*=-1.0;
			if(d<dlat)
				dlat=d;
			if(rnum>1)
				printf("%c %5ld lat = %lf  num = %ld %lf Z %lf",
					cr,++n,lat,rnum,minz,maxz);
			rnum=0;
		}
		rnum+=1;
		num+=1;
		if(dep>maxz)
			maxz=dep;
		if(dep<minz)
			minz=dep;
		lato=lat;
		lono=lon;
	}
	printf("dlat = %lf  latmin = %lf  latmax = %lf\n",dlat,latmin,latmax);
	printf("dlon = %lf  lonmin = %lf  lonmax = %lf\n",dlon,lonmin,lonmax);
	row=(latmax-latmin)/dlat+1.5;
	col=(lonmax-lonmin)/dlon+1.5;
	printf("output will be %d rows x %d columns\n",row,col);

	fprintf(fplbs,"FILE_TYPE          = ELEVATION_ARRAY\n");
	fprintf(fplbs,"IMAGE_LINES        = %d\n",row);
	fprintf(fplbs,"LINE_SAMPLES       = %d\n",col);
	fprintf(fplbs,"UPPER_LEFT_LAT     = %10.5lf\n",latmax);
	fprintf(fplbs,"UPPER_LEFT_LON     = %10.5lf\n",lonmin);
	fprintf(fplbs,"LOWER_RIGHT_LAT    = %10.5lf (optional)\n",latmin);
	fprintf(fplbs,"LOWER_RIGHT_LON    = %10.5lf (optional)\n",lonmax);
	fprintf(fplbs,"DELTA_LAT          = %0.2lf points/degree\n",1.0/dlat);
	fprintf(fplbs,"DELTA_LON          = %0.2lf points/degree\n",1.0/dlon);
	fprintf(fplbs,"NUMBER_OF_LAT      = %d\n",col);
	fprintf(fplbs,"NUMBER_OF_LON      = %d\n",row);
	fprintf(fplbs,"MAXIMUM_Z          = %15.5lf (optional)\n",maxz);
	fprintf(fplbs,"MINIMUM_Z          = %15.5lf (optional)\n",minz);
	fprintf(fplbs,"DATA_TYPE          = float\n");
	fprintf(fplbs,"DATA_SIZE          = 4 bytes\n");
	fprintf(fplbs,"DATA_FILE_POINTER  = '%s.D32'\n",name);
	fprintf(fplbs,"INPUT_DATA_NAME    = '%s' (optional)\n",argv[1]);
	fclose(fplbs);
	fclose(fpd32);
	fclose(fpin);
	printf("There are %ld points\n",num);
}


/**********************************************************************
**
**
**
**********************************************************************/

int help()

{
	printf("Command Line:\n\n");
	printf("asc2lbs [input file name + suffix] [output file name]\n\n");
	printf("example:\n\n");
	printf("asc2lbs ne_bathy.dat bos_harb\n\n");
	exit(0);
}


                                                                                                      