Merge pull request #7 from kryc/cleanup-inputs

Cleanup inputs
This commit is contained in:
Alex Farrant
2017-03-07 22:13:35 +00:00
committed by GitHub
5 changed files with 571 additions and 517 deletions

191
inputs.cc
View File

@@ -3,6 +3,8 @@
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <errno.h>
#include <limits.h>
#include "common.h"
#include "main.hh"
@@ -22,9 +24,8 @@ int loadClutter(char *filename, double radius, struct site tx)
char * pch;
FILE *fd;
fd = fopen(filename, "rb");
if (fd != NULL) {
if( (fd = fopen(filename, "rb")) == NULL)
return errno;
if (fgets(line, 19, fd) != NULL) {
pch = strtok (line," ");
@@ -120,7 +121,7 @@ int loadClutter(char *filename, double radius, struct site tx)
fprintf(stderr, "Clutter error @ x %d y %d\n", x, y);
}//if
}//for
}
fclose(fd);
return 0;
}
@@ -234,9 +235,8 @@ int loadLIDAR(char *filenames)
}
while (indx < fc) {
fd = fopen(files[indx], "rb");
if (fd != NULL) {
if( (fd = fopen(files[indx], "rb")) == NULL )
return errno;
if (fgets(line, 255, fd) != NULL) {
pch = strtok (line," ");
@@ -332,9 +332,6 @@ int loadLIDAR(char *filenames)
fclose(fd);
if (debug)
fprintf(stderr, "LIDAR LOADED %d x %d\n", width, height);
} else {
return -1;
}
indx++;
} // filename(s)
IPPD=width;
@@ -353,11 +350,12 @@ int LoadSDF_SDF(char *name)
containing digital elevation model data into memory.
Elevation data, maximum and minimum elevations, and
quadrangle limits are stored in the first available
dem[] structure. */
dem[] structure.
NOTE: On error, this function returns a negative errno */
int x, y, data = 0, indx, minlat, minlon, maxlat, maxlon, j;
char found, free_page = 0, line[20], jline[20], sdf_file[255],
path_plus_name[255], *junk = NULL;
path_plus_name[PATH_MAX];
FILE *fd;
@@ -368,9 +366,8 @@ int LoadSDF_SDF(char *name)
/* Parse filename for minimum latitude and longitude values */
sscanf(sdf_file, "%d:%d:%d:%d", &minlat, &maxlat, &minlon,
&maxlon);
if( sscanf(sdf_file, "%d:%d:%d:%d", &minlat, &maxlat, &minlon, &maxlon) != 4 )
return -EINVAL;
sdf_file[x] = '.';
sdf_file[x + 1] = 's';
@@ -402,20 +399,19 @@ int LoadSDF_SDF(char *name)
if (free_page && found == 0 && indx >= 0 && indx < MAXPAGES) {
/* Search for SDF file in current working directory first */
strncpy(path_plus_name, sdf_file, 255);
strncpy(path_plus_name, sdf_file, sizeof(path_plus_name)-1);
fd = fopen(path_plus_name, "rb");
if (fd == NULL) {
if( (fd = fopen(path_plus_name, "rb")) == NULL ){
/* Next, try loading SDF file from path specified
in $HOME/.ss_path file or by -d argument */
strncpy(path_plus_name, sdf_path, 255);
strncat(path_plus_name, sdf_file, 255);
fd = fopen(path_plus_name, "rb");
strncpy(path_plus_name, sdf_path, sizeof(path_plus_name)-1);
strncat(path_plus_name, sdf_file, sizeof(path_plus_name)-1);
if( (fd = fopen(path_plus_name, "rb")) == NULL ){
return -errno;
}
}
if (fd != NULL) {
if (debug == 1) {
fprintf(stderr,
"Loading \"%s\" into page %d...",
@@ -424,19 +420,23 @@ int LoadSDF_SDF(char *name)
}
if (fgets(line, 19, fd) != NULL) {
sscanf(line, "%f", &dem[indx].max_west);
if( sscanf(line, "%f", &dem[indx].max_west) == EOF )
return -errno;
}
if (fgets(line, 19, fd) != NULL) {
sscanf(line, "%f", &dem[indx].min_north);
if( sscanf(line, "%f", &dem[indx].min_north) == EOF )
return -errno;
}
if (fgets(line, 19, fd) != NULL) {
sscanf(line, "%f", &dem[indx].min_west);
if( sscanf(line, "%f", &dem[indx].min_west) == EOF )
return -errno;
}
if (fgets(line, 19, fd) != NULL) {
sscanf(line, "%f", &dem[indx].max_north);
if( sscanf(line, "%f", &dem[indx].max_north) == EOF )
return -errno;
}
/*
Here X lines of DEM will be read until IPPD is reached.
@@ -447,10 +447,11 @@ int LoadSDF_SDF(char *name)
for (y = 0; y < ippd; y++) {
for (j = 0; j < jgets; j++) {
junk = fgets(jline, 19, fd);
if( fgets(jline, sizeof(jline), fd) == NULL )
return -EIO;
}
if (fgets(line, 19, fd) != NULL) {
if (fgets(line, sizeof(line), fd) != NULL) {
data = atoi(line);
}
@@ -468,15 +469,18 @@ int LoadSDF_SDF(char *name)
if (ippd == 600) {
for (j = 0; j < IPPD; j++) {
junk = fgets(jline, 19, fd);
if( fgets(jline, sizeof(jline), fd) == NULL )
return -EIO;
}
}
if (ippd == 300) {
for (j = 0; j < IPPD; j++) {
junk = fgets(jline, 19, fd);
junk = fgets(jline, 19, fd);
junk = fgets(jline, 19, fd);
if( fgets(jline, sizeof(jline), fd) == NULL )
return -EIO;
if( fgets(jline, sizeof(jline), fd) == NULL )
return -EIO;
if( fgets(jline, sizeof(jline), fd) == NULL )
return -EIO;
}
}
}
@@ -534,15 +538,11 @@ int LoadSDF_SDF(char *name)
return 1;
}
else
return -1;
}
else
return 0;
}
char LoadSDF(char *name)
int LoadSDF(char *name)
{
/* This function loads the requested SDF file from the filesystem.
It first tries to invoke the LoadSDF_SDF() function to load an
@@ -561,7 +561,7 @@ char LoadSDF(char *name)
/* If neither format can be found, then assume the area is water. */
if (return_value == 0 || return_value == -1) {
if ( return_value == 0 || return_value < 0 ) {
sscanf(name, "%d:%d:%d:%d", &minlat, &maxlat, &minlon,
@@ -668,14 +668,14 @@ char LoadSDF(char *name)
return return_value;
}
void LoadPAT(char *filename)
int LoadPAT(char *az_filename, char *el_filename)
{
/* This function reads and processes antenna pattern (.az
and .el) files that correspond in name to previously
loaded ss .lrp files. */
int a, b, w, x, y, z, last_index, next_index, span;
char string[255], azfile[255], elfile[255], *pointer = NULL;
char string[255], *pointer = NULL;
float az, xx, elevation, amplitude, rotation, valid1, valid2,
delta, azimuth[361], azimuth_pattern[361], el_pattern[10001],
elevation_pattern[361][1001], slant_angle[361], tilt,
@@ -683,21 +683,6 @@ void LoadPAT(char *filename)
FILE *fd = NULL;
unsigned char read_count[10001];
for (x = 0; filename[x] != '.' && filename[x] != 0 && x < 250; x++) {
azfile[x] = filename[x];
elfile[x] = filename[x];
}
azfile[x] = '.';
azfile[x + 1] = 'a';
azfile[x + 2] = 'z';
azfile[x + 3] = 0;
elfile[x] = '.';
elfile[x + 1] = 'e';
elfile[x + 2] = 'l';
elfile[x + 3] = 0;
rotation = 0.0;
got_azimuth_pattern = 0;
@@ -705,7 +690,9 @@ void LoadPAT(char *filename)
/* Load .az antenna pattern file */
fd = fopen(azfile, "r");
if( az_filename != NULL && (fd = fopen(az_filename, "r")) == NULL && errno != ENOENT )
/* Any error other than file not existing is an error */
return errno;
if( fd != NULL ){
/* Clear azimuth pattern array */
@@ -767,6 +754,7 @@ void LoadPAT(char *filename)
} while (feof(fd) == 0);
fclose(fd);
fd = NULL;
/* Handle 0=360 degree ambiguity */
@@ -837,9 +825,12 @@ void LoadPAT(char *filename)
/* Read and process .el file */
fd = fopen(elfile, "r");
if( el_filename != NULL && (fd = fopen(el_filename, "r")) == NULL && errno != ENOENT )
/* Any error other than file not existing is an error */
return errno;
if( fd != NULL ){
for (x = 0; x <= 10000; x++) {
el_pattern[x] = 0.0;
read_count[x] = 0;
@@ -1002,7 +993,6 @@ void LoadPAT(char *filename)
}
got_elevation_pattern = 255;
}
for (x = 0; x <= 360; x++) {
for (y = 0; y <= 1000; y++) {
@@ -1020,8 +1010,10 @@ void LoadPAT(char *filename)
}
}
}
return 0;
}
void LoadSignalColors(struct site xmtr)
int LoadSignalColors(struct site xmtr)
{
int x, y, ok, val[4];
char filename[255], string[80], *pointer = NULL, *s = NULL;
@@ -1106,14 +1098,13 @@ void LoadSignalColors(struct site xmtr)
region.levels = 13;
fd = fopen(filename, "r");
if (fd == NULL && xmtr.filename[0] == '\0')
/* Don't save if we don't have an output file */
return;
if ( xmtr.filename[0] == '\0' && (fd = fopen(filename, "r")) == NULL )
return 0;
if (fd == NULL) {
fd = fopen(filename, "w");
if( (fd = fopen(filename, "w")) == NULL )
return errno;
for (x = 0; x < region.levels; x++)
fprintf(fd, "%3d: %3d, %3d, %3d\n", region.level[x],
@@ -1158,9 +1149,10 @@ void LoadSignalColors(struct site xmtr)
fclose(fd);
region.levels = x;
}
return 0;
}
void LoadLossColors(struct site xmtr)
int LoadLossColors(struct site xmtr)
{
int x, y, ok, val[4];
char filename[255], string[80], *pointer = NULL, *s = NULL;
@@ -1260,14 +1252,13 @@ void LoadLossColors(struct site xmtr)
region.levels = 16;
fd = fopen(filename, "r");
if (fd == NULL && xmtr.filename[0] == '\0')
if ( xmtr.filename[0] == '\0' && (fd = fopen(filename, "r")) == NULL )
/* Don't save if we don't have an output file */
return;
return 0;
if (fd == NULL) {
fd = fopen(filename, "w");
if( (fd = fopen(filename, "w")) == NULL )
return errno;
for (x = 0; x < region.levels; x++)
fprintf(fd, "%3d: %3d, %3d, %3d\n", region.level[x],
@@ -1312,9 +1303,10 @@ void LoadLossColors(struct site xmtr)
fclose(fd);
region.levels = x;
}
return 0;
}
void LoadDBMColors(struct site xmtr)
int LoadDBMColors(struct site xmtr)
{
int x, y, ok, val[4];
char filename[255], string[80], *pointer = NULL, *s = NULL;
@@ -1414,14 +1406,13 @@ void LoadDBMColors(struct site xmtr)
region.levels = 16;
fd = fopen(filename, "r");
if (fd == NULL && xmtr.filename[0] == '\0')
if ( (fd = fopen(filename, "r")) == NULL && xmtr.filename[0] == '\0' )
/* Don't save if we don't have an output file */
return;
return 0;
if (fd == NULL) {
fd = fopen(filename, "w");
if( (fd = fopen(filename, "w")) == NULL )
return errno;
for (x = 0; x < region.levels; x++)
fprintf(fd, "%+4d: %3d, %3d, %3d\n", region.level[x],
@@ -1473,14 +1464,16 @@ void LoadDBMColors(struct site xmtr)
fclose(fd);
region.levels = x;
}
return 0;
}
void LoadTopoData(int max_lon, int min_lon, int max_lat, int min_lat)
int LoadTopoData(int max_lon, int min_lon, int max_lat, int min_lat)
{
/* This function loads the SDF files required
to cover the limits of the region specified. */
int x, y, width, ymin, ymax;
int success;
width = ReduceAngle(max_lon - min_lon);
@@ -1512,7 +1505,9 @@ void LoadTopoData(int max_lon, int min_lon, int max_lat, int min_lat)
snprintf(string, 16,
"%d:%d:%d:%d", x,
x + 1, ymin, ymax);
LoadSDF(string);
if( (success = LoadSDF(string)) < 0 ){
return -success;
}
}
}
@@ -1543,12 +1538,15 @@ void LoadTopoData(int max_lon, int min_lon, int max_lat, int min_lat)
snprintf(string, 16,
"%d:%d:%d:%d", x,
x + 1, ymin, ymax);
LoadSDF(string);
if( (success = LoadSDF(string)) < 0 ){
return -success;
}
}
}
return 0;
}
void LoadUDT(char *filename)
int LoadUDT(char *filename)
{
/* This function reads a file containing User-Defined Terrain
features for their addition to the digital elevation model
@@ -1563,13 +1561,19 @@ void LoadUDT(char *filename)
double latitude, longitude, height, tempheight;
FILE *fd1 = NULL, *fd2 = NULL;
strcpy(tempname, "/tmp/XXXXXX\0");
strcpy(tempname, "/tmp/XXXXXX");
fd1 = fopen(filename, "r");
if( (fd1 = fopen(filename, "r")) == NULL )
return errno;
if (fd1 != NULL) {
fd = mkstemp(tempname);
fd2 = fopen(tempname, "w");
if( (fd = mkstemp(tempname)) == -1 )
return errno;
if( (fd2 = fdopen(fd,"w")) == NULL ){
fclose(fd1);
close(fd);
return errno;
}
s = fgets(input, 78, fd1);
@@ -1645,10 +1649,14 @@ void LoadUDT(char *filename)
fclose(fd1);
fclose(fd2);
close(fd);
fd1 = fopen(tempname, "r");
fd2 = fopen(tempname, "r");
if( (fd1 = fopen(tempname, "r")) == NULL )
return errno;
if( (fd2 = fopen(tempname, "r")) == NULL ){
fclose(fd1);
return errno;
}
y = 0;
@@ -1696,6 +1704,5 @@ void LoadUDT(char *filename)
fclose(fd1);
fclose(fd2);
unlink(tempname);
}
return 0;
}

View File

@@ -4,14 +4,17 @@
#include "common.h"
int LoadSDF_SDF(char *name, int winfiles);
char LoadSDF(char *name, int winfiles);
void LoadPAT(char *filename);
void LoadSignalColors(struct site xmtr);
void LoadLossColors(struct site xmtr);
void LoadDBMColors(struct site xmtr);
void LoadTopoData(int max_lon, int min_lon, int max_lat, int min_lat);
void LoadUDT(char *filename);
int LoadSDF(char *name, int winfiles);
int LoadPAT(char *az_filename, char *el_filename);
int LoadSignalColors(struct site xmtr);
int LoadLossColors(struct site xmtr);
int LoadDBMColors(struct site xmtr);
int LoadTopoData(int max_lon, int min_lon, int max_lat, int min_lat);
int LoadUDT(char *filename);
int loadLIDAR(char *filename);
int loadClutter(char *filename, double radius, struct site tx);
static const char AZ_FILE_SUFFIX[] = ".az";
static const char EL_FILE_SUFFIX[] = ".el";
#endif /* _INPUTS_HH_ */

69
main.cc
View File

@@ -28,6 +28,7 @@ double version = 3.02;
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
#include "common.h"
#include "inputs.hh"
@@ -1042,14 +1043,15 @@ int main(int argc, char *argv[])
int x, y, z = 0, min_lat, min_lon, max_lat, max_lon,
rxlat, rxlon, txlat, txlon, west_min, west_max,
nortRxHin, nortRxHax, propmodel, knifeedge = 0, ppa =
0, normalise = 0, haf = 0, pmenv = 1, lidar=0, cropped;
0, normalise = 0, haf = 0, pmenv = 1, lidar=0, cropped, result;
bool use_threads = true;
unsigned char LRmap = 0, txsites = 0, topomap = 0, geo = 0, kml =
0, area_mode = 0, max_txsites, ngs = 0;
char mapfile[255], udt_file[255], ano_filename[255], lidar_tiles[4096], clutter_file[255];
char mapfile[255], ano_filename[255], lidar_tiles[4096], clutter_file[255];
char *az_filename, *el_filename, *udt_file = NULL;
double altitude = 0.0, altitudeLR = 0.0, tx_range = 0.0,
rx_range = 0.0, deg_range = 0.0, deg_limit = 0.0, deg_range_lon;
@@ -1141,7 +1143,7 @@ int main(int argc, char *argv[])
forced_erp = -1.0;
forced_freq = 0.0;
sdf_path[0] = 0;
udt_file[0] = 0;
udt_file = NULL;
path.length = 0;
max_txsites = 30;
fzone_clearance = 0.6;
@@ -1218,7 +1220,26 @@ int main(int argc, char *argv[])
strncpy(mapfile, argv[z], 253);
strncpy(tx_site[0].name, "Tx", 2);
strncpy(tx_site[0].filename, argv[z], 253);
LoadPAT(argv[z]);
/* Antenna pattern files have the same basic name as the output file
* but with a different extension. If they exist, load them now */
if( (az_filename = (char*) calloc(strlen(argv[z]) + strlen(AZ_FILE_SUFFIX) + 1, sizeof(char))) == NULL )
return ENOMEM;
strcpy(az_filename, argv[z]);
strcat(az_filename, AZ_FILE_SUFFIX);
if( (el_filename = (char*) calloc(strlen(argv[z]) + strlen(EL_FILE_SUFFIX) + 1, sizeof(char))) == NULL ){
free(az_filename);
return ENOMEM;
}
strcpy(el_filename, argv[z]);
strcat(el_filename, EL_FILE_SUFFIX);
if( (result = LoadPAT(az_filename,el_filename)) != 0 ){
fprintf(stderr,"Permissions error reading antenna pattern file\n");
free(az_filename);
free(el_filename);
exit(result);
}
free(az_filename);
free(el_filename);
} else if (z <= y && argv[z][0] && argv[z][0] == '-' && argv[z][1] == '\0' ) {
/* Handle writing image data to stdout */
to_stdout = true;
@@ -1468,10 +1489,14 @@ int main(int argc, char *argv[])
}
/*UDT*/ if (strcmp(argv[x], "-udt") == 0) {
/*UDT*/
if (strcmp(argv[x], "-udt") == 0) {
z = x + 1;
if (z <= y && argv[z][0]) {
udt_file = (char*) calloc(PATH_MAX+1, sizeof(char));
if( udt_file == NULL )
return ENOMEM;
strncpy(udt_file, argv[z], 253);
}
}
@@ -1665,17 +1690,13 @@ int main(int argc, char *argv[])
/* Load the required tiles */
if(lidar){
int err;
err = loadLIDAR(lidar_tiles);
if (err) {
if( (result = loadLIDAR(lidar_tiles)) != 0 ){
fprintf(stderr, "Couldn't find one or more of the "
"lidar files. Please ensure their paths are "
"correct and try again.\n");
exit(EXIT_FAILURE);
exit(result);
}
if(debug){
fprintf(stderr,"%.4f,%.4f,%.4f,%.4f,%d x %d\n",max_north,min_west,min_north,max_west,width,height);
}
@@ -1692,7 +1713,11 @@ int main(int argc, char *argv[])
}else{
// DEM first
LoadTopoData(max_lon, min_lon, max_lat, min_lat);
if( (result = LoadTopoData(max_lon, min_lon, max_lat, min_lat)) != 0 ){
// This only fails on errors loading SDF tiles
fprintf(stderr, "Error loading topo data\n");
return result;
}
if (area_mode || topomap) {
for (z = 0; z < txsites && z < max_txsites; z++) {
@@ -1777,7 +1802,11 @@ int main(int argc, char *argv[])
/* Load any additional SDF files, if required */
LoadTopoData(max_lon, min_lon, max_lat, min_lat);
if( (result = LoadTopoData(max_lon, min_lon, max_lat, min_lat)) != 0 ){
// This only fails on errors loading SDF tiles
fprintf(stderr, "Error loading topo data\n");
return result;
}
}
ppd=(double)ippd;
yppd=ppd;
@@ -1789,7 +1818,10 @@ int main(int argc, char *argv[])
mpi = ippd-1;
// User defined clutter file
LoadUDT(udt_file);
if( udt_file != NULL && (result = LoadUDT(udt_file)) != 0 ){
fprintf(stderr, "Error loading clutter file\n");
return result;
}
// Enrich with Clutter
if(strlen(clutter_file) > 1){
@@ -1797,7 +1829,10 @@ int main(int argc, char *argv[])
Clutter tiles cover 16 x 12 degs but we only need a fraction of that area.
Limit by max_range / miles per degree (at equator)
*/
loadClutter(clutter_file,max_range/45,tx_site[0]);
if( (result = loadClutter(clutter_file,max_range/45,tx_site[0])) != 0 ){
fprintf(stderr, "Error, invalid or clutter file not found\n");
return result;
}
}
if (ppa == 0) {
@@ -1844,8 +1879,8 @@ int main(int argc, char *argv[])
DoRxdPwr((to_stdout == true ? NULL : mapfile), geo, kml, ngs, tx_site,
txsites);
else
DoSigStr(mapfile, geo, kml, ngs, tx_site,
txsites);
if( (result = DoSigStr(mapfile, geo, kml, ngs, tx_site,txsites)) != 0 )
return result;
}
if(lidar){
east=eastoffset;

View File

@@ -44,7 +44,10 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml,
255.0 / pow((double)(max_elevation - min_elevation),
one_over_gamma);
LoadLossColors(xmtr[0]);
if( (success = LoadLossColors(xmtr[0])) != 0 ){
fprintf(stderr,"Error loading loss colors\n");
exit(success);
}
if( filename != NULL ) {
@@ -256,7 +259,7 @@ void DoPathLoss(char *filename, unsigned char geo, unsigned char kml,
}
void DoSigStr(char *filename, unsigned char geo, unsigned char kml,
int DoSigStr(char *filename, unsigned char geo, unsigned char kml,
unsigned char ngs, struct site *xmtr, unsigned char txsites)
{
/* This function generates a topographic map in Portable Pix Map
@@ -284,7 +287,10 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml,
255.0 / pow((double)(max_elevation - min_elevation),
one_over_gamma);
LoadSignalColors(xmtr[0]);
if( (success = LoadSignalColors(xmtr[0])) != 0 ){
fprintf(stderr,"Error loading signal colors\n");
return success;
}
if( filename != NULL ) {
@@ -501,7 +507,7 @@ void DoSigStr(char *filename, unsigned char geo, unsigned char kml,
fclose(fd);
fd = NULL;
}
return 0;
}
void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
@@ -532,7 +538,10 @@ void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
255.0 / pow((double)(max_elevation - min_elevation),
one_over_gamma);
LoadDBMColors(xmtr[0]);
if( (success = LoadDBMColors(xmtr[0])) != 0 ){
fprintf(stderr,"Error loading DBM colors\n");
exit(success);
}
if( filename != NULL ) {

View File

@@ -3,7 +3,7 @@
void DoPathLoss(char *filename, unsigned char geo, unsigned char kml,
unsigned char ngs, struct site *xmtr, unsigned char txsites);
void DoSigStr(char *filename, unsigned char geo, unsigned char kml,
int DoSigStr(char *filename, unsigned char geo, unsigned char kml,
unsigned char ngs, struct site *xmtr, unsigned char txsites);
void DoRxdPwr(char *filename, unsigned char geo, unsigned char kml,
unsigned char ngs, struct site *xmtr, unsigned char txsites);