1
0
Fork 0

Fix release build issues with raw2ascii under cmake

This commit is contained in:
James Turner 2011-11-05 16:00:40 +00:00
parent 324ce8acd7
commit 7fb875b44b

View file

@ -26,19 +26,20 @@
# include <config.h> # include <config.h>
#endif #endif
#include <math.h> /* rint() */ #include <math.h> /* rint() */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> /* atoi() atof() */ #include <stdlib.h> /* atoi() atof() */
#include <string.h> /* swab() */ #include <string.h> /* swab() */
#include <sys/types.h> /* open() */ #include <sys/types.h> /* open() */
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h>
#ifdef _MSC_VER #ifdef _MSC_VER
# include <io.h> # include <io.h>
#endif #endif
#ifdef HAVE_UNISTD_H #ifdef HAVE_UNISTD_H
# include <unistd.h> /* close() */ # include <unistd.h> /* close() */
#endif #endif
#include "rawdem.h" #include "rawdem.h"
@ -49,9 +50,9 @@ double round( double a ) {
long i; long i;
if ( a > 0.0 ) { if ( a > 0.0 ) {
i = (long)( a + 0.5 ); i = (long)( a + 0.5 );
} else { } else {
i = (long)( a - 0.5 ); i = (long)( a - 0.5 );
} }
return (double)i; return (double)i;
@ -64,10 +65,20 @@ int reads(int fd, char *buf, unsigned int len) {
char c; char c;
len--; len--;
while ( (i < len) && (read(fd, &c, 1) != 0) ) while ( i < len)
{ {
if ((c == '\n') || (c == '\r') ) int sz = read(fd, &c, 1);
break; if (sz == 0)
return 0;
if (sz < 0)
{
printf("read failed: %s\n", strerror(errno));
return 0;
}
if ((c == '\n') || (c == '\r') )
break;
buf[i++] = c; buf[i++] = c;
} }
@ -76,7 +87,6 @@ int reads(int fd, char *buf, unsigned int len) {
buf[i++] = '\n'; buf[i++] = '\n';
buf[i] = '\0'; buf[i] = '\0';
return i; return i;
} }
@ -85,114 +95,113 @@ int reads(int fd, char *buf, unsigned int len) {
* DEM file */ * DEM file */
void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) { void rawReadDemHdr( fgRAWDEM *raw, char *hdr_file ) {
FILE *hdr; FILE *hdr;
static char line[256], key[256], value[256]; char line[256], key[256], value[256];
int i, len, offset; int i, len, offset;
double tmp; double tmp;
if ( (hdr = fopen(hdr_file, "r")) == NULL ) { if ( (hdr = fopen(hdr_file, "r")) == NULL ) {
printf("Error opening DEM header file: %s\n", hdr_file); printf("Error opening DEM header file: %s\n", hdr_file);
exit(-1); exit(-1);
} }
/* default to big endian if nothing else */ /* default to big endian if nothing else */
raw->big_endian = 1; raw->big_endian = 1;
/* process each line */ /* process each line */
while ( (reads(fileno(hdr), line, 256) != 0) ) { while ( (reads(fileno(hdr), line, 256) != 0) ) {
len = strlen(line); len = strlen(line);
while(len && ((line[len - 1] == '\n')||(line[len - 1] == '\r'))) { while(len && ((line[len - 1] == '\n')||(line[len - 1] == '\r'))) {
len--; len--;
line[len] = 0; // kill EOL characters line[len] = 0; // kill EOL characters
} }
printf("%s ", line); printf("%s ", line);
len = strlen(line);
len = strlen(line);
/* extract key */ /* extract key */
i = 0; i = 0;
while ( (line[i] != ' ') && (i < len) ) { while ( (line[i] != ' ') && (i < len) ) {
key[i] = line[i]; key[i] = line[i];
i++; i++;
} }
key[i] = '\0'; key[i] = '\0';
/* skip middle space */ /* skip middle space */
while ( (line[i] == ' ') && (i < len) ) { while ( (line[i] == ' ') && (i < len) ) {
i++; i++;
} }
offset = i; offset = i;
/* extract value */ /* extract value */
while ( (line[i] != '\n') && (i < len) ) { while ( (line[i] != '\n') && (i < len) ) {
value[i-offset] = line[i]; value[i-offset] = line[i];
i++; i++;
} }
value[i-offset] = '\0'; value[i-offset] = '\0';
/* printf("key='%s' value='%s'\n", key, value); */ /* printf("key='%s' value='%s'\n", key, value); */
if ( strcmp(key, "BYTEORDER") == 0 ) { if ( strcmp(key, "BYTEORDER") == 0 ) {
if ( strcmp( value, "M" ) == 0 ) { if ( strcmp( value, "M" ) == 0 ) {
raw->big_endian = 1; raw->big_endian = 1;
printf( "- set big_endian\n" ); printf( "- set big_endian\n" );
} else { } else {
printf( "- unset big_endian (not 'M'!)\n" ); printf( "- unset big_endian (not 'M'!)\n" );
raw->big_endian = 0; raw->big_endian = 0;
} }
} else if ( strcmp(key, "NROWS") == 0 ) { } else if ( strcmp(key, "NROWS") == 0 ) {
raw->nrows = atoi(value); raw->nrows = atoi(value);
printf( "- set rows to %d\n", raw->nrows ); printf( "- set rows to %d\n", raw->nrows );
} else if ( strcmp(key, "NCOLS") == 0 ) { } else if ( strcmp(key, "NCOLS") == 0 ) {
raw->ncols = atoi(value); raw->ncols = atoi(value);
printf( "- set cols to %d\n", raw->ncols ); printf( "- set cols to %d\n", raw->ncols );
} else if ( strcmp(key, "ULXMAP") == 0 ) { } else if ( strcmp(key, "ULXMAP") == 0 ) {
tmp = atof(value); tmp = atof(value);
#ifdef HAVE_RINT #ifdef HAVE_RINT
raw->ulxmap = (int)rint(tmp * 3600.0); /* convert to arcsec */ raw->ulxmap = (int)rint(tmp * 3600.0); /* convert to arcsec */
#else #else
raw->ulxmap = (int)round(tmp * 3600.0); /* convert to arcsec */ raw->ulxmap = (int)round(tmp * 3600.0); /* convert to arcsec */
#endif #endif
printf( "- set ulxmap to %d arcsecs (%d degrees)\n", raw->ulxmap, printf( "- set ulxmap to %d arcsecs (%d degrees)\n", raw->ulxmap,
raw->ulxmap / 3600); raw->ulxmap / 3600);
} else if ( strcmp(key, "ULYMAP") == 0 ) { } else if ( strcmp(key, "ULYMAP") == 0 ) {
tmp = atof(value); tmp = atof(value);
#ifdef HAVE_RINT #ifdef HAVE_RINT
raw->ulymap = (int)rint(tmp * 3600.0); /* convert to arcsec */ raw->ulymap = (int)rint(tmp * 3600.0); /* convert to arcsec */
#else #else
raw->ulymap = (int)round(tmp * 3600.0); /* convert to arcsec */ raw->ulymap = (int)round(tmp * 3600.0); /* convert to arcsec */
#endif #endif
printf( "- set ulymap to %d arcsecs (%d degrees)\n", raw->ulymap, printf( "- set ulymap to %d arcsecs (%d degrees)\n", raw->ulymap,
raw->ulymap / 3600); raw->ulymap / 3600);
} else if ( strcmp(key, "XDIM") == 0 ) { } else if ( strcmp(key, "XDIM") == 0 ) {
tmp = atof(value); tmp = atof(value);
#ifdef HAVE_RINT #ifdef HAVE_RINT
raw->xdim = (int)rint(tmp * 3600.0); /* convert to arcsec */ raw->xdim = (int)rint(tmp * 3600.0); /* convert to arcsec */
#else #else
raw->xdim = (int)round(tmp * 3600.0); /* convert to arcsec */ raw->xdim = (int)round(tmp * 3600.0); /* convert to arcsec */
#endif #endif
printf( "- set xdim to %d arcsecs (%f degrees)\n", raw->xdim, printf( "- set xdim to %d arcsecs (%f degrees)\n", raw->xdim,
(double)raw->xdim / 3600.0); (double)raw->xdim / 3600.0);
} else if ( strcmp(key, "YDIM") == 0 ) { } else if ( strcmp(key, "YDIM") == 0 ) {
tmp = atof(value); tmp = atof(value);
#ifdef HAVE_RINT #ifdef HAVE_RINT
raw->ydim = (int)rint(tmp * 3600.0); /* convert to arcsec */ raw->ydim = (int)rint(tmp * 3600.0); /* convert to arcsec */
#else #else
raw->ydim = (int)round(tmp * 3600.0); /* convert to arcsec */ raw->ydim = (int)round(tmp * 3600.0); /* convert to arcsec */
#endif #endif
printf( "- set ydim to %d arcsecs (%f degrees)\n", raw->ydim, printf( "- set ydim to %d arcsecs (%f degrees)\n", raw->ydim,
(double)raw->ydim / 3600.0); (double)raw->ydim / 3600.0);
} else { } else {
/* ignore for now */ /* ignore for now */
printf( "- ignore for now\n" ); printf( "- ignore for now: '%s'\n", key );
} }
} } // of line iteration
raw->rootx = raw->ulxmap - (raw->xdim / 2); raw->rootx = raw->ulxmap - (raw->xdim / 2);
raw->rooty = raw->ulymap + (raw->ydim / 2); raw->rooty = raw->ulymap + (raw->ydim / 2);
printf("%d %d %d %d %d %d %d %d\n", raw->nrows, raw->ncols, printf("%d %d %d %d %d %d %d %d\n", raw->nrows, raw->ncols,
raw->ulxmap, raw->ulymap, raw->rootx, raw->rooty, raw->xdim, raw->ulxmap, raw->ulymap, raw->rootx, raw->rooty, raw->xdim,
raw->ydim); raw->ydim);
} }
@ -204,8 +213,8 @@ void rawOpenDemFile( fgRAWDEM *raw, char *raw_dem_file ) {
#else #else
if ( (raw->fd = open(raw_dem_file ,O_RDONLY | O_BINARY)) == -1 ) { if ( (raw->fd = open(raw_dem_file ,O_RDONLY | O_BINARY)) == -1 ) {
#endif #endif
printf("Error opening Raw DEM file: %s\n", raw_dem_file); printf("Error opening Raw DEM file: %s\n", raw_dem_file);
exit(-1); exit(-1);
} }
raw->min = raw->max = 0; raw->min = raw->max = 0;
} }
@ -224,8 +233,8 @@ void rawAdvancePosition( fgRAWDEM *raw, int arcsec ) {
offset = 2 * raw->ncols * ( arcsec / raw->ydim ); offset = 2 * raw->ncols * ( arcsec / raw->ydim );
if ( (result = lseek(raw->fd, offset, SEEK_SET)) == -1 ) { if ( (result = lseek(raw->fd, offset, SEEK_SET)) == -1 ) {
printf("Error lseek filed trying to offset by %ld\n", offset); printf("Error lseek filed trying to offset by %ld\n", offset);
exit(-1); exit(-1);
} }
printf("Successful seek ahead of %ld bytes\n", result); printf("Successful seek ahead of %ld bytes\n", result);
@ -239,17 +248,17 @@ void rawReadNextRow( fgRAWDEM *raw, int index ) {
short int value; short int value;
if ( raw->ncols > MAX_COLS ) { if ( raw->ncols > MAX_COLS ) {
printf("Error, buf needs to be bigger in rawReadNextRow()\n"); printf("Error, buf needs to be bigger in rawReadNextRow()\n");
exit(-1); exit(-1);
} }
/* printf("Attempting to read %d bytes\n", 2 * raw->ncols); */ /* printf("Attempting to read %d bytes\n", 2 * raw->ncols); */
result = read(raw->fd, buf, 2 * raw->ncols); result = read(raw->fd, buf, 2 * raw->ncols);
/* printf("Read %d bytes\n", result); */ /* printf("Read %d bytes\n", result); */
if ( result != 2 * raw->ncols ) { if ( result != 2 * raw->ncols ) {
printf("Error reading %d number of bytes! Got %d instead\n", printf("Error reading %d number of bytes! Got %d instead\n",
2 * raw->ncols, result); 2 * raw->ncols, result);
exit(-1); exit(-1);
} }
/* reverse byte order */ /* reverse byte order */
@ -258,23 +267,23 @@ void rawReadNextRow( fgRAWDEM *raw, int index ) {
/* swab(frombuf, tobuf, 2 * raw->ncols); */ /* swab(frombuf, tobuf, 2 * raw->ncols); */
for ( i = 0; i < raw->ncols; i++ ) { for ( i = 0; i < raw->ncols; i++ ) {
/* printf("hi = %d lo = %d\n", buf[2*i], buf[2*i + 1]); */ /* printf("hi = %d lo = %d\n", buf[2*i], buf[2*i + 1]); */
/* the endianness of the data is determined by the creator of /* the endianness of the data is determined by the creator of
the data file, we see it as a byte stream. The actual the data file, we see it as a byte stream. The actual
endianess is in the header file someplace but I don't yet endianess is in the header file someplace but I don't yet
know how to read it out. So for the time being we assume know how to read it out. So for the time being we assume
that the data is always big endian */ that the data is always big endian */
if ( raw->big_endian ) { if ( raw->big_endian ) {
value = ( (buf[2*i]) << 8 ) | buf[2*i + 1]; value = ( (buf[2*i]) << 8 ) | buf[2*i + 1];
} else { } else {
value = ( (buf[2*i + 1]) << 8 ) | buf[2*i]; value = ( (buf[2*i + 1]) << 8 ) | buf[2*i];
} }
raw->strip[index][i] = value; raw->strip[index][i] = value;
if ( value < raw->min ) { raw->min = value; } if ( value < raw->min ) { raw->min = value; }
if ( value > raw->max ) { raw->max = value; } if ( value > raw->max ) { raw->max = value; }
} }
/* printf( " so far, min = %d max = %d\n", raw->min, raw->max ); */ /* printf( " so far, min = %d max = %d\n", raw->min, raw->max ); */
@ -303,20 +312,20 @@ void rawConvertCenter2Edge( fgRAWDEM *raw ) {
/* derive edge nodes */ /* derive edge nodes */
for ( i = 1; i < 120; i++ ) { for ( i = 1; i < 120; i++ ) {
raw->edge[i][0] = (raw->center[i-1][0] + raw->center[i][0]) / 2.0f; raw->edge[i][0] = (raw->center[i-1][0] + raw->center[i][0]) / 2.0f;
raw->edge[i][120] = (raw->center[i-1][119] + raw->center[i][119]) / 2.0f; raw->edge[i][120] = (raw->center[i-1][119] + raw->center[i][119]) / 2.0f;
raw->edge[0][i] = (raw->center[0][i-1] + raw->center[0][i]) / 2.0f; raw->edge[0][i] = (raw->center[0][i-1] + raw->center[0][i]) / 2.0f;
raw->edge[120][i] = (raw->center[119][i-1] + raw->center[119][i]) / 2.0f; raw->edge[120][i] = (raw->center[119][i-1] + raw->center[119][i]) / 2.0f;
} }
/* derive internal nodes */ /* derive internal nodes */
for ( j = 1; j < 120; j++ ) { for ( j = 1; j < 120; j++ ) {
for ( i = 1; i < 120; i++ ) { for ( i = 1; i < 120; i++ ) {
raw->edge[i][j] = ( raw->center[i-1][j-1] + raw->edge[i][j] = ( raw->center[i-1][j-1] +
raw->center[i] [j-1] + raw->center[i] [j-1] +
raw->center[i] [j] + raw->center[i] [j] +
raw->center[i-1][j] ) / 4.0f; raw->center[i-1][j] ) / 4.0f;
} }
} }
} }
@ -333,19 +342,19 @@ void rawDumpAsciiDEM( fgRAWDEM *raw, char *path, int ilon, int ilat ) {
/* Generate output file name */ /* Generate output file name */
if ( ilon >= 0 ) { if ( ilon >= 0 ) {
lon = ilon; lon = ilon;
lon_sign = 'e'; lon_sign = 'e';
} else { } else {
lon = -ilon; lon = -ilon;
lon_sign = 'w'; lon_sign = 'w';
} }
if ( ilat >= 0 ) { if ( ilat >= 0 ) {
lat = ilat; lat = ilat;
lat_sign = 'n'; lat_sign = 'n';
} else { } else {
lat = -ilat; lat = -ilat;
lat_sign = 's'; lat_sign = 's';
} }
sprintf(outfile, "%s/%c%03d%c%02d.dem", path, lon_sign, lon, lat_sign, lat); sprintf(outfile, "%s/%c%03d%c%02d.dem", path, lon_sign, lon, lat_sign, lat);
@ -362,8 +371,8 @@ void rawDumpAsciiDEM( fgRAWDEM *raw, char *path, int ilon, int ilat ) {
printf("outfile = %s\n", outfile); printf("outfile = %s\n", outfile);
if ( (fd = fopen(outfile, "w")) == NULL ) { if ( (fd = fopen(outfile, "w")) == NULL ) {
printf("Error opening output file = %s\n", outfile); printf("Error opening output file = %s\n", outfile);
exit(-1); exit(-1);
} }
/* Dump the "A" record */ /* Dump the "A" record */
@ -449,32 +458,32 @@ void rawDumpAsciiDEM( fgRAWDEM *raw, char *path, int ilon, int ilat ) {
/* Dump "B" records */ /* Dump "B" records */
for ( j = 0; j <= 120; j++ ) { for ( j = 0; j <= 120; j++ ) {
/* row / column id of this profile */ /* row / column id of this profile */
fprintf(fd, "%6d%6d", 1, j + 1); fprintf(fd, "%6d%6d", 1, j + 1);
/* Number of rows and columns (elevation points) in this /* Number of rows and columns (elevation points) in this
profile */ profile */
fprintf(fd, "%6d%6d", 3600 / raw->xdim + 1, 1); fprintf(fd, "%6d%6d", 3600 / raw->xdim + 1, 1);
/* Ground planimetric coordinates (arc-seconds) of the first /* Ground planimetric coordinates (arc-seconds) of the first
* elevation in the profile */ * elevation in the profile */
fprintf(fd, "%20.15fD+06", ilon * 3600.0 / 1000000.0); fprintf(fd, "%20.15fD+06", ilon * 3600.0 / 1000000.0);
fprintf(fd, "%20.15fD+06", (ilat * 3600.0 + j * raw->ydim) / 1000000.0); fprintf(fd, "%20.15fD+06", (ilat * 3600.0 + j * raw->ydim) / 1000000.0);
/* Elevation of local datum for the profile. Always zero for /* Elevation of local datum for the profile. Always zero for
* 1-degree DEM, the reference is mean sea level. */ * 1-degree DEM, the reference is mean sea level. */
fprintf(fd, "%6.1f", 0.0); fprintf(fd, "%6.1f", 0.0);
fprintf(fd, "%18s", ""); fprintf(fd, "%18s", "");
/* Minimum and maximum elevations for the profile. */ /* Minimum and maximum elevations for the profile. */
fprintf(fd, " %20.15E", 0.0); fprintf(fd, " %20.15E", 0.0);
fprintf(fd, " %20.15E", 0.0); fprintf(fd, " %20.15E", 0.0);
/* One (usually) dimensional array (1,prof_num_cols) of /* One (usually) dimensional array (1,prof_num_cols) of
elevations */ elevations */
for ( i = 0; i <= 120; i++ ) { for ( i = 0; i <= 120; i++ ) {
fprintf(fd, "%6.0f", raw->edge[j][i]); fprintf(fd, "%6.0f", raw->edge[j][i]);
} }
} }
fprintf(fd, "\n"); fprintf(fd, "\n");
@ -496,13 +505,13 @@ void rawProcessStrip( fgRAWDEM *raw, int lat_degrees, char *path ) {
/* convert to arcsec */ /* convert to arcsec */
lat = lat_degrees * 3600; lat = lat_degrees * 3600;
printf("Max Latitude = %d arcsec (%f degs)\n", lat, lat_degrees); printf("Max Latitude = %d arcsec (%d degs)\n", lat, lat_degrees);
/* validity check ... */ /* validity check ... */
if ( (lat > raw->rooty) || if ( (lat > raw->rooty) ||
(lat < (raw->rooty - raw->nrows * raw->ydim + 1)) ) { (lat < (raw->rooty - raw->nrows * raw->ydim + 1)) ) {
printf("Latitude out of range for this DEM file\n"); printf("Latitude out of range for this DEM file\n");
return; return;
} }
printf ("Reading strip starting at %d (top and working down)\n", lat); printf ("Reading strip starting at %d (top and working down)\n", lat);
@ -515,16 +524,16 @@ void rawProcessStrip( fgRAWDEM *raw, int lat_degrees, char *path ) {
yrange = 3600 / raw->ydim; yrange = 3600 / raw->ydim;
for ( i = 0; i < yrange; i++ ) { for ( i = 0; i < yrange; i++ ) {
index = yrange - i - 1; index = yrange - i - 1;
/* printf("About to read into row %d\n", index); */ /* printf("About to read into row %d\n", index); */
rawReadNextRow(raw, index); rawReadNextRow(raw, index);
for ( j = 0; j < raw->ncols; j++ ) { for ( j = 0; j < raw->ncols; j++ ) {
if ( raw->strip[index][j] < -1000 ) { if ( raw->strip[index][j] < -1000 ) {
/* map ocean (or stuff insanely low) to 0 for now */ /* map ocean (or stuff insanely low) to 0 for now */
raw->strip[index][j] = 0; raw->strip[index][j] = 0;
} }
} }
} }
/* extract individual tiles from the strip */ /* extract individual tiles from the strip */
@ -532,37 +541,37 @@ void rawProcessStrip( fgRAWDEM *raw, int lat_degrees, char *path ) {
num_degrees = span / 3600; num_degrees = span / 3600;
tile_width = raw->ncols / num_degrees; tile_width = raw->ncols / num_degrees;
printf("span = %d num_degrees = %d width = %d\n", printf("span = %d num_degrees = %d width = %d\n",
span, num_degrees, tile_width); span, num_degrees, tile_width);
for ( i = 0; i < num_degrees; i++ ) { for ( i = 0; i < num_degrees; i++ ) {
xstart = i * tile_width; xstart = i * tile_width;
xend = xstart + 120; xend = xstart + 120;
min = 10000; max = -10000; min = 10000; max = -10000;
for ( row = 0; row < yrange; row++ ) { for ( row = 0; row < yrange; row++ ) {
for ( col = xstart; col < xend; col++ ) { for ( col = xstart; col < xend; col++ ) {
/* Copy from strip to pixel centered tile. Yep, /* Copy from strip to pixel centered tile. Yep,
* row/col are reversed here. raw->strip is backwards * row/col are reversed here. raw->strip is backwards
* for convenience. I am converting to [x,y] now. */ * for convenience. I am converting to [x,y] now. */
raw->center[col-xstart][row] = raw->strip[row][col]; raw->center[col-xstart][row] = raw->strip[row][col];
if ( raw->strip[row][col] < min) { if ( raw->strip[row][col] < min) {
min = raw->strip[row][col]; min = raw->strip[row][col];
} }
if ( raw->strip[row][col] > max) { if ( raw->strip[row][col] > max) {
max = raw->strip[row][col]; max = raw->strip[row][col];
} }
} }
} }
raw->tmp_min = min; raw->tmp_min = min;
raw->tmp_max = max; raw->tmp_max = max;
/* Convert from pixel centered to pixel edge values */ /* Convert from pixel centered to pixel edge values */
rawConvertCenter2Edge(raw); rawConvertCenter2Edge(raw);
/* Dump out the ascii format DEM file */ /* Dump out the ascii format DEM file */
rawDumpAsciiDEM(raw, path, (raw->rootx / 3600) + i, lat_degrees - 1); rawDumpAsciiDEM(raw, path, (raw->rootx / 3600) + i, lat_degrees - 1);
} }
} }