initial upload

This commit is contained in:
2021-05-05 10:58:03 +08:00
parent d6ffc7f33c
commit f1cf25db22
114 changed files with 83953 additions and 0 deletions

53
toolkits/CMakeLists.txt Normal file
View File

@@ -0,0 +1,53 @@
# 设置编译选项
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2")
if(WIN32)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11 -O2")
endif()
# 设置可执行文件的输出地址
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin/toolkits)
# 下面设置tools的编译命令 首先设置一个宏
macro(add_tools name)
# 添加可执行程序名称
add_executable(${name} ${name}.c)
# 设置安装后的动态库调用地址
set_target_properties(${name} PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
# 链接动态库
target_link_libraries(${name} PUBLIC tesseroids)
# 将可执行程序安装到/usr/local/sbin
install(TARGETS ${name} RUNTIME DESTINATION sbin/tesseroids)
endmacro()
# 添加tools
add_tools(prismgx)
add_tools(prismgy)
add_tools(prismgz)
add_tools(prismgxx)
add_tools(prismgxy)
add_tools(prismgxz)
add_tools(prismgyy)
add_tools(prismgyz)
add_tools(prismgzz)
add_tools(prismgs)
add_tools(prismpot)
add_tools(prismpots)
add_tools(prismggts)
add_tools(tessgx)
add_tools(tessgy)
add_tools(tessgz)
add_tools(tessgxx)
add_tools(tessgxy)
add_tools(tessgxz)
add_tools(tessgyy)
add_tools(tessgyz)
add_tools(tessgzz)
add_tools(tessdefaults)
add_tools(tess2prism)
add_tools(tessgrd)
add_tools(tesslayers)
add_tools(tessmass)
add_tools(tessmodgen)
add_tools(tesspot)

249
toolkits/prismggts.c Normal file
View File

@@ -0,0 +1,249 @@
/*
Program to calculate the potential of a rectangular prism model in spherical
coordinates.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../lib/logger.h"
#include "../lib/version.h"
#include "../lib/grav_prism_sph.h"
#include "../lib/geometry.h"
#include "../lib/constants.h"
#include "../lib/parsers.h"
/* Print the help message */
void print_help()
{
printf("Usage: prismggts MODELFILE [OPTIONS]\n\n");
printf("Calculates the 6 component gravity gradient tensor due to a\n");
printf("rectangular prism model on specified observation points using\n");
printf("spherical coordinates.\n\n");
printf("All input units are SI! Output is in Eotvos.\n\n");
printf("Coordinate system:\n");
printf(" The coordinate system used for the calculations is:\n");
printf(" x->North, y->East, and z->Up\n");
printf("Input:\n");
printf(" Computation points are passed through standard input (stdin).\n");
printf(" Reads 3 or more values per line and inteprets the first 3 as:\n");
printf(" longitude latitude height \n");
printf(" longitude and latitude should be in decimal degrees, and\n");
printf(" height in meters.\n");
printf(" Other values in the line are ignored.\n");
printf(" Lines that start with # are ignored as comments.\n");
printf(" Lines should be no longer than 10000 (ten thousand) characters.");
printf(" \n\n");
printf("Output:\n");
printf(" Printed to standard output (stdout) in the form:\n");
printf(" lon lat height ... gxx gxy gxz gyy gyz gzz\n");
printf(" ... represents any values that were read from input and\n");
printf(" ignored. In other words, the result is appended to the last\n");
printf(" column of the input. Use this to pipe prism* programs\n");
printf(" together.\n\n");
printf(" Comments about the provenance of the data are inserted into\n");
printf(" the top of the output\n\n");
printf("MODELFILE: File containing the prism model\n");
printf(" * Each prism is specified by the values of its dimensions,\n");
printf(" density, and spherical coordinates of the center of its\n");
printf(" top.\n");
printf(" * The file should contain one prism per line\n");
printf(" * If a line starts with # it will be considered a comment and\n");
printf(" will be ignored.\n");
printf(" * Each line should have the following column format:\n");
printf(" DX DY DZ Density lon lat r\n");
printf(" This is the format output by tess2prism.\n\n");
printf("Options:\n");
printf(" -h Print instructions.\n");
printf(" --version Print version and license information.\n");
printf(" -v Enable verbose printing to stderr.\n");
printf(" -lFILENAME Print log messages to file FILENAME.\n");
printf("\nPart of the Tesseroids package.\n");
printf("Project site: <http://fatiando.org/software/tesseroids>\n");
printf("Report bugs at: ");
printf("<http://code.google.com/p/tesseroids/issues/list>\n");
}
/* Run the main for a generic prismg* program */
int main(int argc, char **argv)
{
BASIC_ARGS args;
PRISM *model;
int modelsize, rc, line, points = 0, error_exit = 0, bad_input = 0, i;
char buff[10000];
char progname[] = "prismggts";
double lon, lat, height, ggt[6], tmp[6];
FILE *logfile = NULL, *modelfile = NULL;
time_t rawtime;
clock_t tstart;
struct tm * timeinfo;
log_init(LOG_INFO);
strcpy(progname, progname);
rc = parse_basic_args(argc, argv, progname, &args, &print_help);
if(rc == 3)
{
log_error("%s: missing input file", progname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
if(rc == 2)
{
return 0;
}
if(rc == 1)
{
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
/* Set the appropriate logging level and log to file if necessary */
if(!args.verbose)
{
log_init(LOG_WARNING);
}
if(args.logtofile)
{
logfile = fopen(args.logfname, "w");
if(logfile == NULL)
{
log_error("unable to create log file %s", args.logfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
log_tofile(logfile, LOG_INFO);
}
/* Print standard verbose */
log_info("%s (Tesseroids project) %s", progname, tesseroids_version);
time(&rawtime);
timeinfo = localtime(&rawtime);
log_info("(local time) %s", asctime(timeinfo));
/* Read the model file */
log_info("Reading prism model from file %s", args.inputfname);
modelfile = fopen(args.inputfname, "r");
if(modelfile == NULL)
{
log_error("failed to open model file %s", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
model = read_prism_model(modelfile, 1, &modelsize);
fclose(modelfile);
if(modelsize == 0)
{
log_error("prism file %s is empty", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
if(model == NULL)
{
log_error("failed to read model from file %s", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
log_info("Total of %d prism(s) read", modelsize);
/* Print a header on the output with provenance information */
printf("# Gravity gradient tensor calculated in spherical coordinates with %s %s:\n",
progname, tesseroids_version);
printf("# local time: %s", asctime(timeinfo));
printf("# model file: %s (%d prisms)\n", args.inputfname, modelsize);
/* Read each computation point from stdin and calculate */
log_info("Calculating (this may take a while)...");
tstart = clock();
for(line = 1; !feof(stdin); line++)
{
if(fgets(buff, 10000, stdin) == NULL)
{
if(ferror(stdin))
{
log_error("problem encountered reading line %d", line);
error_exit = 1;
break;
}
}
else
{
/* Check for comments and blank lines */
if(buff[0] == '#' || buff[0] == '\r' || buff[0] == '\n')
{
printf("%s", buff);
continue;
}
if(sscanf(buff, "%lf %lf %lf", &lon, &lat, &height) != 3)
{
log_warning("bad/invalid computation point at line %d", line);
log_warning("skipping this line and continuing");
bad_input++;
continue;
}
/* Need to remove \n and \r from end of buff first to print the
result in the end */
strstrip(buff);
ggt[0] = 0;
ggt[1] = 0;
ggt[2] = 0;
ggt[3] = 0;
ggt[4] = 0;
ggt[5] = 0;
for(i = 0; i < modelsize; i++)
{
prism_ggt_sph(model[i], lon, lat, height + MEAN_EARTH_RADIUS,
tmp);
ggt[0] += tmp[0];
ggt[1] += tmp[1];
ggt[2] += tmp[2];
ggt[3] += tmp[3];
ggt[4] += tmp[4];
ggt[5] += tmp[5];
}
printf("%s %.15g %.15g %.15g %.15g %.15g %.15g\n", buff,
ggt[0],
ggt[1],
ggt[2],
ggt[3],
ggt[4],
ggt[5]);
points++;
}
}
if(bad_input)
{
log_warning("Encountered %d bad computation points which were skipped",
bad_input);
}
if(error_exit)
{
log_warning("Terminating due to error in input");
log_warning("Try '%s -h' for instructions", progname);
}
else
{
log_info("Calculated on %d points in %.5g seconds", points,
(double)(clock() - tstart)/CLOCKS_PER_SEC);
}
/* Clean up */
free(model);
log_info("Done");
if(args.logtofile)
fclose(logfile);
return 0;
}

239
toolkits/prismgs.c Normal file
View File

@@ -0,0 +1,239 @@
/*
Program to calculate the potential of a rectangular prism model in spherical
coordinates.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../lib/logger.h"
#include "../lib/version.h"
#include "../lib/grav_prism_sph.h"
#include "../lib/geometry.h"
#include "../lib/constants.h"
#include "../lib/parsers.h"
/* Print the help message */
void print_help()
{
printf("Usage: prismgs MODELFILE [OPTIONS]\n\n");
printf("Calculates the 3 component gravity vector due to a rectangular\n");
printf("prism model on specified observation points using spherical\n");
printf("coordinates.\n\n");
printf("All input units are SI! Output is in mGal.\n\n");
printf("Coordinate system:\n");
printf(" The coordinate system used for the calculations is:\n");
printf(" x->North, y->East, and z->Up\n");
printf("In order to maintain mainstream convention, component gz is\n");
printf("calculated with z-> Down.\n\n");
printf("Input:\n");
printf(" Computation points are passed through standard input (stdin).\n");
printf(" Reads 3 or more values per line and inteprets the first 3 as:\n");
printf(" longitude latitude height \n");
printf(" longitude and latitude should be in decimal degrees, and\n");
printf(" height in meters.\n");
printf(" Other values in the line are ignored.\n");
printf(" Lines that start with # are ignored as comments.\n");
printf(" Lines should be no longer than 10000 (ten thousand) characters.");
printf(" \n\n");
printf("Output:\n");
printf(" Printed to standard output (stdout) in the form:\n");
printf(" lon lat height ... gx gy gz\n");
printf(" ... represents any values that were read from input and\n");
printf(" ignored. In other words, the result is appended to the last\n");
printf(" column of the input. Use this to pipe prism* programs\n");
printf(" together.\n\n");
printf(" Comments about the provenance of the data are inserted into\n");
printf(" the top of the output\n\n");
printf("MODELFILE: File containing the prism model\n");
printf(" * Each prism is specified by the values of its dimensions,\n");
printf(" density, and spherical coordinates of the center of its\n");
printf(" top.\n");
printf(" * The file should contain one prism per line\n");
printf(" * If a line starts with # it will be considered a comment and\n");
printf(" will be ignored.\n");
printf(" * Each line should have the following column format:\n");
printf(" DX DY DZ Density lon lat r\n");
printf(" This is the format output by tess2prism.\n\n");
printf("Options:\n");
printf(" -h Print instructions.\n");
printf(" --version Print version and license information.\n");
printf(" -v Enable verbose printing to stderr.\n");
printf(" -lFILENAME Print log messages to file FILENAME.\n");
printf("\nPart of the Tesseroids package.\n");
printf("Project site: <http://fatiando.org/software/tesseroids>\n");
printf("Report bugs at: ");
printf("<http://code.google.com/p/tesseroids/issues/list>\n");
}
/* Run the main for a generic prismg* program */
int main(int argc, char **argv)
{
BASIC_ARGS args;
PRISM *model;
int modelsize, rc, line, points = 0, error_exit = 0, bad_input = 0, i;
char buff[10000];
char progname[] = "prismgs";
double lon, lat, height, gx, gy, gz, tmpx, tmpy, tmpz;
FILE *logfile = NULL, *modelfile = NULL;
time_t rawtime;
clock_t tstart;
struct tm * timeinfo;
log_init(LOG_INFO);
strcpy(progname, progname);
rc = parse_basic_args(argc, argv, progname, &args, &print_help);
if(rc == 3)
{
log_error("%s: missing input file", progname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
if(rc == 2)
{
return 0;
}
if(rc == 1)
{
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
/* Set the appropriate logging level and log to file if necessary */
if(!args.verbose)
{
log_init(LOG_WARNING);
}
if(args.logtofile)
{
logfile = fopen(args.logfname, "w");
if(logfile == NULL)
{
log_error("unable to create log file %s", args.logfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
log_tofile(logfile, LOG_INFO);
}
/* Print standard verbose */
log_info("%s (Tesseroids project) %s", progname, tesseroids_version);
time(&rawtime);
timeinfo = localtime(&rawtime);
log_info("(local time) %s", asctime(timeinfo));
/* Read the model file */
log_info("Reading prism model from file %s", args.inputfname);
modelfile = fopen(args.inputfname, "r");
if(modelfile == NULL)
{
log_error("failed to open model file %s", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
model = read_prism_model(modelfile, 1, &modelsize);
fclose(modelfile);
if(modelsize == 0)
{
log_error("prism file %s is empty", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
if(model == NULL)
{
log_error("failed to read model from file %s", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
log_info("Total of %d prism(s) read", modelsize);
/* Print a header on the output with provenance information */
printf("# Gravity vector calculated in spherical coordinates with %s %s:\n",
progname, tesseroids_version);
printf("# local time: %s", asctime(timeinfo));
printf("# model file: %s (%d prisms)\n", args.inputfname, modelsize);
/* Read each computation point from stdin and calculate */
log_info("Calculating (this may take a while)...");
tstart = clock();
for(line = 1; !feof(stdin); line++)
{
if(fgets(buff, 10000, stdin) == NULL)
{
if(ferror(stdin))
{
log_error("problem encountered reading line %d", line);
error_exit = 1;
break;
}
}
else
{
/* Check for comments and blank lines */
if(buff[0] == '#' || buff[0] == '\r' || buff[0] == '\n')
{
printf("%s", buff);
continue;
}
if(sscanf(buff, "%lf %lf %lf", &lon, &lat, &height) != 3)
{
log_warning("bad/invalid computation point at line %d", line);
log_warning("skipping this line and continuing");
bad_input++;
continue;
}
/* Need to remove \n and \r from end of buff first to print the
result in the end */
strstrip(buff);
gx = 0;
gy = 0;
gz = 0;
for(i = 0; i < modelsize; i++)
{
prism_g_sph(model[i], lon, lat, height + MEAN_EARTH_RADIUS,
&tmpx, &tmpy, &tmpz);
gx += tmpx;
gy += tmpy;
gz += tmpz;
}
printf("%s %.15g %.15g %.15g\n", buff, gx, gy, gz);
points++;
}
}
if(bad_input)
{
log_warning("Encountered %d bad computation points which were skipped",
bad_input);
}
if(error_exit)
{
log_warning("Terminating due to error in input");
log_warning("Try '%s -h' for instructions", progname);
}
else
{
log_info("Calculated on %d points in %.5g seconds", points,
(double)(clock() - tstart)/CLOCKS_PER_SEC);
}
/* Clean up */
free(model);
log_info("Done");
if(args.logtofile)
fclose(logfile);
return 0;
}

14
toolkits/prismgx.c Normal file
View File

@@ -0,0 +1,14 @@
/*
Program to calculate gx of a rectangular prism model on a set of points.
*/
#include "../lib/grav_prism.h"
#include "../lib/prismg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_prismg_main(argc, argv, "prismgx", &prism_gx);
}

14
toolkits/prismgxx.c Normal file
View File

@@ -0,0 +1,14 @@
/*
Program to calculate gxx of a rectangular prism model on a set of points.
*/
#include "../lib/grav_prism.h"
#include "../lib/prismg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_prismg_main(argc, argv, "prismgxx", &prism_gxx);
}

14
toolkits/prismgxy.c Normal file
View File

@@ -0,0 +1,14 @@
/*
Program to calculate gxy of a rectangular prism model on a set of points.
*/
#include "../lib/grav_prism.h"
#include "../lib/prismg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_prismg_main(argc, argv, "prismgxy", &prism_gxy);
}

14
toolkits/prismgxz.c Normal file
View File

@@ -0,0 +1,14 @@
/*
Program to calculate gxz of a rectangular prism model on a set of points.
*/
#include "../lib/grav_prism.h"
#include "../lib/prismg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_prismg_main(argc, argv, "prismgxz", &prism_gxz);
}

14
toolkits/prismgy.c Normal file
View File

@@ -0,0 +1,14 @@
/*
Program to calculate gy of a rectangular prism model on a set of points.
*/
#include "../lib/grav_prism.h"
#include "../lib/prismg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_prismg_main(argc, argv, "prismgy", &prism_gy);
}

14
toolkits/prismgyy.c Normal file
View File

@@ -0,0 +1,14 @@
/*
Program to calculate gyy of a rectangular prism model on a set of points.
*/
#include "../lib/grav_prism.h"
#include "../lib/prismg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_prismg_main(argc, argv, "prismgyy", &prism_gyy);
}

14
toolkits/prismgyz.c Normal file
View File

@@ -0,0 +1,14 @@
/*
Program to calculate gyz of a rectangular prism model on a set of points.
*/
#include "../lib/grav_prism.h"
#include "../lib/prismg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_prismg_main(argc, argv, "prismgyz", &prism_gyz);
}

14
toolkits/prismgz.c Normal file
View File

@@ -0,0 +1,14 @@
/*
Program to calculate gz of a rectangular prism model on a set of points.
*/
#include "../lib/grav_prism.h"
#include "../lib/prismg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_prismg_main(argc, argv, "prismgz", &prism_gz);
}

14
toolkits/prismgzz.c Normal file
View File

@@ -0,0 +1,14 @@
/*
Program to calculate gzz of a rectangular prism model on a set of points.
*/
#include "../lib/grav_prism.h"
#include "../lib/prismg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_prismg_main(argc, argv, "prismgzz", &prism_gzz);
}

14
toolkits/prismpot.c Normal file
View File

@@ -0,0 +1,14 @@
/*
Program to calculate potential of a rectangular prism model on a set of points.
*/
#include "../lib/grav_prism.h"
#include "../lib/prismg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_prismg_main(argc, argv, "prismpot", &prism_pot);
}

230
toolkits/prismpots.c Normal file
View File

@@ -0,0 +1,230 @@
/*
Program to calculate the potential of a rectangular prism model in spherical
coordinates.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "../lib/logger.h"
#include "../lib/version.h"
#include "../lib/grav_prism_sph.h"
#include "../lib/geometry.h"
#include "../lib/constants.h"
#include "../lib/parsers.h"
/* Print the help message */
void print_help()
{
printf("Usage: prismpots MODELFILE [OPTIONS]\n\n");
printf("Calculate the potential due to a rectangular prism model on\n");
printf("specified observation points using spherical coordinates.\n\n");
printf("All input and output units are SI!\n\n");
printf("Coordinate system:\n");
printf(" The coordinate system used for the calculations is:\n");
printf(" x->North, y->East, and z->Up\n\n");
printf("Input:\n");
printf(" Computation points are passed through standard input (stdin).\n");
printf(" Reads 3 or more values per line and inteprets the first 3 as:\n");
printf(" longitude latitude height \n");
printf(" longitude and latitude should be in decimal degrees, and\n");
printf(" height in meters.\n");
printf(" Other values in the line are ignored.\n");
printf(" Lines that start with # are ignored as comments.\n");
printf(" Lines should be no longer than 10000 (ten thousand) characters.");
printf(" \n\n");
printf("Output:\n");
printf(" Printed to standard output (stdout) in the form:\n");
printf(" lon lat height ... potential\n");
printf(" ... represents any values that were read from input and\n");
printf(" ignored. In other words, the result is appended to the last\n");
printf(" column of the input. Use this to pipe prism* programs\n");
printf(" together.\n\n");
printf(" Comments about the provenance of the data are inserted into\n");
printf(" the top of the output\n\n");
printf("MODELFILE: File containing the prism model\n");
printf(" * Each prism is specified by the values of its dimensions,\n");
printf(" density, and spherical coordinates of the center of its\n");
printf(" top.\n");
printf(" * The file should contain one prism per line\n");
printf(" * If a line starts with # it will be considered a comment and\n");
printf(" will be ignored.\n");
printf(" * Each line should have the following column format:\n");
printf(" DX DY DZ Density lon lat r\n");
printf(" This is the format output by tess2prism.\n\n");
printf("Options:\n");
printf(" -h Print instructions.\n");
printf(" --version Print version and license information.\n");
printf(" -v Enable verbose printing to stderr.\n");
printf(" -lFILENAME Print log messages to file FILENAME.\n");
printf("\nPart of the Tesseroids package.\n");
printf("Project site: <http://fatiando.org/software/tesseroids>\n");
printf("Report bugs at: ");
printf("<http://code.google.com/p/tesseroids/issues/list>\n");
}
/* Run the main for a generic prismg* program */
int main(int argc, char **argv)
{
BASIC_ARGS args;
PRISM *model;
int modelsize, rc, line, points = 0, error_exit = 0, bad_input = 0, i;
char buff[10000];
char progname[] = "prismpots";
double lon, lat, height, res;
FILE *logfile = NULL, *modelfile = NULL;
time_t rawtime;
clock_t tstart;
struct tm * timeinfo;
log_init(LOG_INFO);
strcpy(progname, progname);
rc = parse_basic_args(argc, argv, progname, &args, &print_help);
if(rc == 3)
{
log_error("%s: missing input file", progname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
if(rc == 2)
{
return 0;
}
if(rc == 1)
{
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
/* Set the appropriate logging level and log to file if necessary */
if(!args.verbose)
{
log_init(LOG_WARNING);
}
if(args.logtofile)
{
logfile = fopen(args.logfname, "w");
if(logfile == NULL)
{
log_error("unable to create log file %s", args.logfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
log_tofile(logfile, LOG_INFO);
}
/* Print standard verbose */
log_info("%s (Tesseroids project) %s", progname, tesseroids_version);
time(&rawtime);
timeinfo = localtime(&rawtime);
log_info("(local time) %s", asctime(timeinfo));
/* Read the model file */
log_info("Reading prism model from file %s", args.inputfname);
modelfile = fopen(args.inputfname, "r");
if(modelfile == NULL)
{
log_error("failed to open model file %s", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
model = read_prism_model(modelfile, 1, &modelsize);
fclose(modelfile);
if(modelsize == 0)
{
log_error("prism file %s is empty", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
if(model == NULL)
{
log_error("failed to read model from file %s", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
log_info("Total of %d prism(s) read", modelsize);
/* Print a header on the output with provenance information */
printf("# Potential calculated in spherical coordinates with %s %s:\n",
progname, tesseroids_version);
printf("# local time: %s", asctime(timeinfo));
printf("# model file: %s (%d prisms)\n", args.inputfname, modelsize);
/* Read each computation point from stdin and calculate */
log_info("Calculating (this may take a while)...");
tstart = clock();
for(line = 1; !feof(stdin); line++)
{
if(fgets(buff, 10000, stdin) == NULL)
{
if(ferror(stdin))
{
log_error("problem encountered reading line %d", line);
error_exit = 1;
break;
}
}
else
{
/* Check for comments and blank lines */
if(buff[0] == '#' || buff[0] == '\r' || buff[0] == '\n')
{
printf("%s", buff);
continue;
}
if(sscanf(buff, "%lf %lf %lf", &lon, &lat, &height) != 3)
{
log_warning("bad/invalid computation point at line %d", line);
log_warning("skipping this line and continuing");
bad_input++;
continue;
}
/* Need to remove \n and \r from end of buff first to print the
result in the end */
strstrip(buff);
for(res = 0, i = 0; i < modelsize; i++)
{
res += prism_pot_sph(model[i], lon, lat,
height + MEAN_EARTH_RADIUS);
}
printf("%s %.15g\n", buff, res);
points++;
}
}
if(bad_input)
{
log_warning("Encountered %d bad computation points which were skipped",
bad_input);
}
if(error_exit)
{
log_warning("Terminating due to error in input");
log_warning("Try '%s -h' for instructions", progname);
}
else
{
log_info("Calculated on %d points in %.5g seconds", points,
(double)(clock() - tstart)/CLOCKS_PER_SEC);
}
/* Clean up */
free(model);
log_info("Done");
if(args.logtofile)
fclose(logfile);
return 0;
}

222
toolkits/tess2prism.c Normal file
View File

@@ -0,0 +1,222 @@
/*
Convert a tesseroid model into a prism model in spherical coordinates
*/
#include <stdio.h>
#include <time.h>
#include "../lib/version.h"
#include "../lib/parsers.h"
#include "../lib/logger.h"
#include "../lib/geometry.h"
/** Print the help message */
void print_help()
{
printf("Usage: tess2prim TESSFILE [OPTIONS]\n\n");
printf("Convert a tesseroid model into a rectangular prism model\n");
printf("(for use with the prism* programs).\n\n");
printf("The converted prisms have the same mass as the tesseroids.\n\n");
printf("Along with each prism are given the spherical coordinates of the\n");
printf("center of the top face of the tesseroid (used as the origin of\n");
printf("the prisms coordinate system). This is needed to compute the.\n");
printf("effect of the prisms in spherical coordinates.\n\n");
printf("If option --flatten is used, the tesseroids are converted by\n");
printf("approximating 1 degree by 111.11km and no spherical coordinates\n");
printf("are given. Use this option when you want to calculate in\n");
printf("Cartesian coordinates.\n\n");
printf("In both cases, the density of the prism is adjusted so that it\n");
printf("has the same mass as the tesseroid.\n\n");
printf("All units either SI or degrees!\n\n");
printf("Input:\n");
printf(" If TESSFILE is omited, will read from standard input (stdin)\n");
printf(" TESSFILE: File containing the tesseroid model\n");
printf(" * Each tesseroid is specified by the values of its borders\n");
printf(" and density\n");
printf(" * The file should contain one tesseroid per line\n");
printf(" * Each line should have the following column format:\n");
printf(" West East South North Top Bottom Density\n");
printf(" * Top and Bottom should be read as 'height to top' and \n");
printf(" 'height to bottom' from the mean Earth radius. Use negative\n");
printf(" values if bellow the surface, for example when modeling\n");
printf(" deep structures, and positive if above the surface, for\n");
printf(" example when modeling topography.\n");
printf(" * If a line starts with # it will be considered a comment\n");
printf(" and will be ignored\n\n");
printf("Output:\n");
printf(" Printed to standard output (stdout) one prism per line in the\n");
printf(" format:\n");
printf(" dx dy dz density lon lat r\n");
printf(" lon, lat, r are the spherical coordinates of the center of\n");
printf(" top face of the prism. This is used as the origin of the\n");
printf(" local coordinate system of the prism.\n");
printf(" If options --flatten is used, the output format is:\n");
printf(" x1 x2 y1 y2 z1 z2 density\n");
printf(" Comments about the provenance of the data are inserted into\n");
printf(" the top of the output.\n\n");
printf("Options:\n");
printf(" --flatten Convert the tesseroids by approximating 1 degree\n");
printf(" by 111.11 km (for compatibility with prism*\n");
printf(" programs).\n");
printf(" -h Print instructions.\n");
printf(" --version Print version and license information.\n");
printf(" -v Enable verbose printing to stderr.\n");
printf(" -lFILENAME Print log messages to file FILENAME.\n");
print_copyright();
}
/** Main */
int main(int argc, char **argv)
{
char *progname = "tess2prism";
TESS2PRISM_ARGS args;
int rc, line, converted = 0, error_exit = 0, bad_input = 0;
char buff[10000];
TESSEROID tess;
PRISM prism;
FILE *logfile = NULL, *modelfile = NULL;
time_t rawtime;
struct tm * timeinfo;
log_init(LOG_INFO);
rc = parse_tess2prism_args(argc, argv, progname, &args, &print_help);
if(rc == 2)
{
return 0;
}
if(rc == 1)
{
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
/* Set the appropriate logging level and log to file if necessary */
if(!args.verbose)
{
log_init(LOG_WARNING);
}
if(args.logtofile)
{
logfile = fopen(args.logfname, "w");
if(logfile == NULL)
{
log_error("unable to create log file %s", args.logfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
log_tofile(logfile, LOG_INFO);
}
/* Print standard verbose */
log_info("%s (Tesseroids project) %s", progname, tesseroids_version);
time(&rawtime);
timeinfo = localtime(&rawtime);
log_info("(local time) %s", asctime(timeinfo));
/* If an input file is not given, read from stdin. Else open the file */
if(rc == 3)
{
log_info("Reading tesseroids from stdin");
modelfile = stdin;
}
else
{
log_info("Reading tesseroids from file %s", args.inputfname);
modelfile = fopen(args.inputfname, "r");
if(modelfile == NULL)
{
log_error("failed to open file %s", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
}
/* Print provenance data to stdout */
printf("# Prisms converted from tesseroid model with %s %s\n", progname,
tesseroids_version);
printf("# local time: %s", asctime(timeinfo));
printf("# tesseroids file: %s\n", rc == 3 ? "stdin" : args.inputfname);
printf("# conversion type: %s\n",
args.flatten ? "equal mass|flatten" :
"equal mass|spherical coordinates");
if(args.flatten)
{
printf("# format: x1 x2 y1 y2 z1 z2 density\n");
}
else
{
printf("# format: dx dy dz density lon lat r\n");
}
/* Read the tesseroids, convert and print to stdout */
for(line = 1; !feof(modelfile); line++)
{
if(fgets(buff, 10000, modelfile) == NULL)
{
if(ferror(stdin))
{
log_error("problem encountered reading line %d", line);
error_exit = 1;
break;
}
}
else
{
/* Check for comments and blank lines */
if(buff[0] == '#' || buff[0] == '\r' || buff[0] == '\n')
{
printf("%s", buff);
continue;
}
/* Remove any trailing spaces or newlines */
strstrip(buff);
if(gets_tess(buff, &tess))
{
log_warning("bad/invalid tesseroid at line %d", line);
bad_input++;
continue;
}
if(args.flatten)
{
tess2prism_flatten(tess, &prism);
printf("%.15g %.15g %.15g %.15g %.15g %.15g %.15g\n",
prism.x1, prism.x2, prism.y1, prism.y2, prism.z1,
prism.z2, prism.density);
}
else
{
tess2prism(tess, &prism);
printf("%.15g %.15g %.15g %.15g %.15g %.15g %.15g\n",
prism.x2 - prism.x1, prism.y2 - prism.y1,
prism.z2 - prism.z1, prism.density,
prism.lon, prism.lat, prism.r);
}
converted++;
}
}
if(bad_input)
{
log_warning("Encountered %d bad input line(s) which were skipped",
bad_input);
}
if(error_exit)
{
log_warning("Terminating due to error in input");
log_warning("Try '%s -h' for instructions", progname);
}
else
{
log_info("Converted %d tesseroids", converted);
}
/* Clean up */
fclose(modelfile);
if(args.logtofile)
fclose(logfile);
return 0;
}

123
toolkits/tessdefaults.c Normal file
View File

@@ -0,0 +1,123 @@
/*
Print the default values of the constants used in the calculations.
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "../lib/version.h"
#include "../lib/logger.h"
#include "../lib/constants.h"
#include "../lib/glq.h"
/** Print the help message */
void print_help()
{
printf("Usage: tessdefaults [OPTIONS]\n\n");
printf("Print default values of constants used.\n\n");
printf("All units either SI or degrees!\n\n");
printf("Output:\n");
printf(" Constants are printed to standard output (stdout) in the form\n");
printf(" CONST_NAME = VALUE\n");
printf(" Lines that start with a # are treated as comments.\n\n");
printf("Options:\n");
printf(" -h Print instructions.\n");
printf(" --version Print version and license information.\n");
print_copyright();
}
/** Main */
int main(int argc, char **argv)
{
char progname[] = "tessdefaults";
int i, bad_args = 0;
char *params;
log_init(LOG_INFO);
/* Parse arguments */
for(i = 1; i < argc; i++)
{
if(argv[i][0] == '-')
{
switch(argv[i][1])
{
case 'h':
if(argv[i][2] != '\0')
{
log_error("invalid argument '%s'", argv[i]);
bad_args++;
break;
}
print_help();
return 0;
case '-':
{
params = &argv[i][2];
if(strcmp(params, "version"))
{
log_error("invalid argument '%s'", argv[i]);
bad_args++;
}
else
{
print_version(progname);
return 0;
}
break;
}
default:
log_error("invalid argument '%s'", argv[i]);
bad_args++;
break;
}
}
else
{
log_error("invalid argument '%s'", argv[i]);
bad_args++;
}
}
/* Check if parsing went well */
if(bad_args > 0)
{
log_error("%d bad input argument(s)", bad_args);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
/* Print the constants from constants.c */
printf("# Mean Earth radius (m)\n");
printf("MEAN_EARTH_RADIUS = %.1f\n\n", MEAN_EARTH_RADIUS);
printf("# Gravitational constant (m^3 kg^-1 s^-1)\n");
printf("G = %.4g\n\n", G);
printf("# Conversion factor from SI units to Eotvos s^-2 = 10^9 Eotvos\n");
printf("SI2EOTVOS = %g\n\n", SI2EOTVOS);
printf("# Conversion factor from SI units to mGal m s^-2} = 10^5 mGal\n");
printf("SI2MGAL = %g\n\n", SI2MGAL);
printf("# Just pi\n");
printf("PI = %.31f\n\n", PI);
printf("# Minimum distance/size ratio for computations to be\n");
printf("# accurate. Used for knowing when to divide the tesseroids.\n");
printf("TESSEROID_POT_SIZE_RATIO = %g\n", TESSEROID_POT_SIZE_RATIO);
printf("TESSEROID_GX_SIZE_RATIO = %g\n", TESSEROID_GX_SIZE_RATIO);
printf("TESSEROID_GY_SIZE_RATIO = %g\n", TESSEROID_GY_SIZE_RATIO);
printf("TESSEROID_GZ_SIZE_RATIO = %g\n", TESSEROID_GZ_SIZE_RATIO);
printf("TESSEROID_GXX_SIZE_RATIO = %g\n", TESSEROID_GXX_SIZE_RATIO);
printf("TESSEROID_GXY_SIZE_RATIO = %g\n", TESSEROID_GXY_SIZE_RATIO);
printf("TESSEROID_GXZ_SIZE_RATIO = %g\n", TESSEROID_GXZ_SIZE_RATIO);
printf("TESSEROID_GYY_SIZE_RATIO = %g\n", TESSEROID_GYY_SIZE_RATIO);
printf("TESSEROID_GYZ_SIZE_RATIO = %g\n", TESSEROID_GYZ_SIZE_RATIO);
printf("TESSEROID_GZZ_SIZE_RATIO = %g\n\n", TESSEROID_GZZ_SIZE_RATIO);
/* Print the constants from glq.c */
printf("# Max iterations of the Legendre polynomial root-finder \
algorithm\n");
printf("GLQ_MAXIT = %d\n\n", GLQ_MAXIT);
printf("# Max error allowed for the Legendre polynomial root-finder \
algorithm\n");
printf("GLQ_MAXERROR = %g\n", GLQ_MAXERROR);
return 0;
}

165
toolkits/tessgrd.c Normal file
View File

@@ -0,0 +1,165 @@
/*
Program to generate a regular grid of points.
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "../lib/logger.h"
#include "../lib/version.h"
#include "../lib/parsers.h"
/* Print the help message for tessgrd program */
void print_tessgrd_help()
{
printf("Usage: tessgrd [PARAMS] [OPTIONS]\n\n");
printf("Make a regular grid of points.\n\n");
printf("All units either SI or degrees!\n\n");
printf("Output:\n");
printf(" Printed to standard output (stdout) in the format:\n");
printf(" lon1 lat1 height\n");
printf(" lon2 lat1 height\n");
printf(" ... ... ...\n");
printf(" lonNLON lat1 height\n");
printf(" lon1 lat2 height\n");
printf(" ... ... ...\n");
printf(" ... ... ...\n");
printf(" lonNLON latNLAT height\n\n");
printf(" * Comments about the provenance of the data are inserted into\n");
printf(" the top of the output\n\n");
printf("Parameters:\n");
printf(" -r W/E/S/N: Bounding region of the grid.\n");
printf(" -b NLON/NLAT: Number of grid points in the\n");
printf(" longitudinal and latitudinal directions.\n");
printf(" -z HEIGHT: Height of the grid with respect to the\n");
printf(" mean Earth radius.\n");
printf(" -h Print instructions.\n");
printf(" --version Print version and license information.\n");
printf("\nOptions:\n");
printf(" -v Enable verbose printing to stderr.\n");
printf(" -lFILENAME Print log messages to file FILENAME.\n");
print_copyright();
}
/** Main */
int main(int argc, char **argv)
{
TESSGRD_ARGS args;
char progname[] = "tessgrd";
int rc;
FILE *logfile = NULL;
time_t rawtime;
struct tm * timeinfo;
double dlon, dlat;
double lon, lat;
/* Keep track of how many printed. Used to check if produced right amount */
int lons = 0, lats = 0, total = 0;
log_init(LOG_INFO);
rc = parse_tessgrd_args(argc, argv, &args, &print_tessgrd_help);
if(rc == 2)
{
return 0;
}
if(rc == 1)
{
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
/* Set the appropriate logging level and log to file if necessary */
if(!args.verbose)
{
log_init(LOG_WARNING);
}
if(args.logtofile)
{
logfile = fopen(args.logfname, "w");
if(!logfile)
{
log_error("unable to create log file %s", args.logfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
log_tofile(logfile, LOG_INFO);
}
/* Print standard verbose */
log_info("%s (Tesseroids project) %s", progname, tesseroids_version);
time(&rawtime);
timeinfo = localtime(&rawtime);
log_info("(local time) %s", asctime(timeinfo));
/* CREATE THE GRID AND PRINT IT TO STDOUT */
log_info("Generating regular grid in region: %g W / %g E / %g S / %g N",
args.w, args.e, args.s, args.n);
log_info("Grid size: %d lon X %d lat = %d points in total", args.nlon,
args.nlat, args.nlon*args.nlat);
/* Define the grid spacing. used nlon or nlat -1 because the borders should
be in the grid */
dlon = (args.e - args.w)/(args.nlon - 1);
dlat = (args.n - args.s)/(args.nlat - 1);
log_info("Grid spacing: %.10f lon / %.10f lat", dlon, dlat);
/* Print a header on the output with provenance information */
printf("# Grid generated with %s %s:\n", progname, tesseroids_version);
printf("# local time: %s", asctime(timeinfo));
printf("# args: -r%g/%g/%g/%g -b%d/%d -z%g\n", args.w, args.e, args.s,
args.n, args.nlon, args.nlat, args.height);
printf("# grid spacing: %.10f lon / %.10f lat\n", dlon, dlat);
printf("# total %d points\n", args.nlon*args.nlat);
/* Make the grid points. Print lon first as x */
for(lat = args.s; lat <= args.n; lat += dlat)
{
lons = 0;
for(lon = args.w; lon <= args.e; lon += dlon)
{
printf("%.15g %.15g %.15g\n", lon, lat, args.height);
lons++;
total++;
}
/* Sometimes prints one less because of rounding errors */
if(lons != args.nlon)
{
printf("%.15g %.15g %.15g\n", lon, lat, args.height);
lons++;
total++;
}
lats++;
printf("\n"); /* To ease plotting in Gnuplot */
}
/* Sometimes prints one less because of rounding errors */
if(lats != args.nlat)
{
lons = 0;
for(lon = args.w; lon <= args.e; lon += dlon)
{
printf("%.15g %.15g %.15g\n", lon, lat, args.height);
lons++;
total++;
}
if(lons != args.nlon)
{
printf("%.15g %.15g %.15g\n", lon, lat, args.height);
lons++;
total++;
}
}
if(total != args.nlat*args.nlon)
{
log_warning("%d total points made instead of required %d", total,
args.nlat*args.nlon);
}
log_info("Total points generated: %d", total);
/* Clean up */
if(args.logtofile)
fclose(logfile);
return 0;
}

16
toolkits/tessgx.c Normal file
View File

@@ -0,0 +1,16 @@
/*
Program to calculate gx of a tesseroid model on a set of points.
*/
#include "../lib/constants.h"
#include "../lib/grav_tess.h"
#include "../lib/tessg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_tessg_main(argc, argv, "tessgx", &tess_gx,
TESSEROID_GX_SIZE_RATIO);
}

16
toolkits/tessgxx.c Normal file
View File

@@ -0,0 +1,16 @@
/*
Program to calculate gxx of a tesseroid model on a set of points.
*/
#include "../lib/constants.h"
#include "../lib/grav_tess.h"
#include "../lib/tessg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_tessg_main(argc, argv, "tessgxx", &tess_gxx,
TESSEROID_GXX_SIZE_RATIO);
}

16
toolkits/tessgxy.c Normal file
View File

@@ -0,0 +1,16 @@
/*
Program to calculate gxy of a tesseroid model on a set of points.
*/
#include "../lib/constants.h"
#include "../lib/grav_tess.h"
#include "../lib/tessg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_tessg_main(argc, argv, "tessgxy", &tess_gxy,
TESSEROID_GXY_SIZE_RATIO);
}

16
toolkits/tessgxz.c Normal file
View File

@@ -0,0 +1,16 @@
/*
Program to calculate gxz of a tesseroid model on a set of points.
*/
#include "../lib/constants.h"
#include "../lib/grav_tess.h"
#include "../lib/tessg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_tessg_main(argc, argv, "tessgxz", &tess_gxz,
TESSEROID_GXZ_SIZE_RATIO);
}

16
toolkits/tessgy.c Normal file
View File

@@ -0,0 +1,16 @@
/*
Program to calculate gy of a tesseroid model on a set of points.
*/
#include "../lib/constants.h"
#include "../lib/grav_tess.h"
#include "../lib/tessg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_tessg_main(argc, argv, "tessgy", &tess_gy,
TESSEROID_GY_SIZE_RATIO);
}

16
toolkits/tessgyy.c Normal file
View File

@@ -0,0 +1,16 @@
/*
Program to calculate gyy of a tesseroid model on a set of points.
*/
#include "../lib/constants.h"
#include "../lib/grav_tess.h"
#include "../lib/tessg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_tessg_main(argc, argv, "tessgyy", &tess_gyy,
TESSEROID_GYY_SIZE_RATIO);
}

16
toolkits/tessgyz.c Normal file
View File

@@ -0,0 +1,16 @@
/*
Program to calculate gyz of a tesseroid model on a set of points.
*/
#include "../lib/constants.h"
#include "../lib/grav_tess.h"
#include "../lib/tessg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_tessg_main(argc, argv, "tessgyz", &tess_gyz,
TESSEROID_GYZ_SIZE_RATIO);
}

16
toolkits/tessgz.c Normal file
View File

@@ -0,0 +1,16 @@
/*
Program to calculate gz of a tesseroid model on a set of points.
*/
#include "../lib/constants.h"
#include "../lib/grav_tess.h"
#include "../lib/tessg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_tessg_main(argc, argv, "tessgz", &tess_gz,
TESSEROID_GZ_SIZE_RATIO);
}

16
toolkits/tessgzz.c Normal file
View File

@@ -0,0 +1,16 @@
/*
Program to calculate gzz of a tesseroid model on a set of points.
*/
#include "../lib/constants.h"
#include "../lib/grav_tess.h"
#include "../lib/tessg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_tessg_main(argc, argv, "tessgzz", &tess_gzz,
TESSEROID_GZZ_SIZE_RATIO);
}

201
toolkits/tesslayers.c Normal file
View File

@@ -0,0 +1,201 @@
/*
Generate tesseroid model of a series of layers given their thickness.
*/
#include <stdio.h>
#include <time.h>
#include "../lib/version.h"
#include "../lib/parsers.h"
#include "../lib/logger.h"
#include "../lib/geometry.h"
#include "../lib/constants.h"
#define BUFFSIZE 1000
/** Print the help message */
void print_help()
{
printf("Usage: tesslayers [ARGUMENTS] [OPTIONS]\n\n");
printf("Generate a tesseroid model of a series of stacked layers.\n\n");
printf("All units are either SI or degrees!\n\n");
printf("Input:\n");
printf(" Regular grids passed through standard input (stdin).\n");
printf(" Grids should be in a single file in xyz format, i.e., in\n");
printf(" columns:\n");
printf(" lon lat height thickness1 dens1 thickness2 dens2 ...\n");
printf(" lon and lat are the longitude and latitude of a grid point,\n");
printf(" height is the top of the first layer at the grid point\n");
printf(" (e.g., the topography or relief of the first layer),\n");
printf(" height should be read as 'height above the mean Earth radius'\n");
printf(" (if bellow the Earth radius use negative heights),\n");
printf(" thickness1 is the thickness of the first layer,\n");
printf(" dens1 is the density of the first layer, and so forth.\n\n");
printf(" Layers MUST be ordered from top-most to bottom-most\n");
printf(" (i.e., thickness1 is of the top layer).\n\n");
printf(" Lines that start with # are ignored as comments.\n");
printf(" Lines should be no longer than 10000 (ten thousand) characters.");
printf(" \n\n");
printf("Output:\n");
printf(" Tesseroids that fill between the interfaces of the layers.\n");
printf(" Tesseroids are printed to standard output (stdout.)\n");
printf(" * Each tesseroid is specified by the values of its borders\n");
printf(" and density\n");
printf(" * Will print one tesseroid per line\n");
printf(" * Each line has the following column format:\n");
printf(" West East South North Top Bottom Density\n");
printf(" * Top and Bottom should be read as 'height to top' and \n");
printf(" 'height to bottom' from the mean Earth radius. Use negative\n");
printf(" values if bellow the surface, for example when modeling\n");
printf(" deep structures, and positive if above the surface, for\n");
printf(" example when modeling topography.\n");
printf(" * If a line starts with # it will be considered a comment\n");
printf(" and will be ignored\n\n");
printf("Arguments:\n");
printf(" -sDLON/DLAT The grid spacing in the longitude and latitude\n");
printf(" directions, respectively, in DECIMAL DEGREES.\n");
printf(" Will be used as the size of the tesseroids.\n");
printf(" WARNING: You may get wrong results if -s is \n");
printf(" different from the grid spacing!\n");
printf("Options:\n");
printf(" -h Print instructions.\n");
printf(" --version Print version and license information.\n");
printf(" -v Enable verbose printing to stderr.\n");
printf(" -lFILENAME Print log messages to file FILENAME.\n");
print_copyright();
}
/** Main */
int main(int argc, char **argv)
{
char *progname = "tesslayers";
TESSLAYERS_ARGS args;
TESSEROID tessbuff[BUFFSIZE];
int t, rc, line, error_exit = 0, bad_input = 0, size = 0, nlayers_old = -1,
nlayers_new;
char buff[10000];
FILE *logfile = NULL;
time_t rawtime;
struct tm * timeinfo;
log_init(LOG_INFO);
rc = parse_tesslayers_args(argc, argv, progname, &args, &print_help);
if(rc == 2)
{
return 0;
}
if(rc == 1)
{
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
/* Set the appropriate logging level and log to file if necessary */
if(!args.verbose)
{
log_init(LOG_WARNING);
}
if(args.logtofile)
{
logfile = fopen(args.logfname, "w");
if(logfile == NULL)
{
log_error("unable to create log file %s", args.logfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
log_tofile(logfile, LOG_INFO);
}
/* Print standard verbose */
log_info("%s (Tesseroids project) %s", progname, tesseroids_version);
time(&rawtime);
timeinfo = localtime(&rawtime);
log_info("(local time) %s", asctime(timeinfo));
log_info("Using grid spacing (size of tesseroids): %g lon / %g lat",
args.dlon, args.dlat);
/* Print a header on the output with provenance information */
printf("# Tesseroid model generated by %s %s:\n", progname,
tesseroids_version);
printf("# local time: %s", asctime(timeinfo));
printf("# grid spacing (size of tesseroids): %g deg lon / %g deg lat\n",
args.dlon, args.dlat);
/* Read each regular grid from stdin and generate the tesseroids */
for(line = 1; !feof(stdin); line++)
{
if(fgets(buff, 10000, stdin) == NULL)
{
if(ferror(stdin))
{
log_error("problem encountered reading line %d", line);
error_exit = 1;
break;
}
}
else
{
/* Check for comments and blank lines */
if(buff[0] == '#' || buff[0] == '\r' || buff[0] == '\n')
{
continue;
}
strstrip(buff);
nlayers_new = gets_layers(buff, args.dlon, args.dlat, tessbuff,
BUFFSIZE);
if(nlayers_new == -1)
{
log_error("invalid input in line %d", line);
error_exit++;
break;
}
if(nlayers_old != -1 && nlayers_old != nlayers_new)
{
log_error("different number of layers in line %d than in previous lines",
line);
error_exit++;
break;
}
nlayers_old = nlayers_new;
for(t = 0; t < nlayers_new; t++)
{
printf("%.15g %.15g %.15g %.15g %.15g %.15g %.15g\n",
tessbuff[t].w,
tessbuff[t].e,
tessbuff[t].s,
tessbuff[t].n,
tessbuff[t].r2 - MEAN_EARTH_RADIUS,
tessbuff[t].r1 - MEAN_EARTH_RADIUS,
tessbuff[t].density);
size++;
}
}
}
if(bad_input)
{
log_warning("Encountered %d bad grid points which were skipped",
bad_input);
}
if(error_exit)
{
log_warning("Terminating due to error in input");
log_warning("Try '%s -h' for instructions", progname);
}
else
{
log_info("Generated %d tesseroids.", size);
}
/* Clean up */
if(args.logtofile)
fclose(logfile);
return 0;
}

193
toolkits/tessmass.c Normal file
View File

@@ -0,0 +1,193 @@
/*
Calculate the mass of a tesseroid model.
*/
#include <stdio.h>
#include <time.h>
#include "../lib/version.h"
#include "../lib/parsers.h"
#include "../lib/logger.h"
#include "../lib/geometry.h"
/** Print the help message */
void print_help()
{
printf("Usage: tessmass TESSFILE [OPTIONS]\n\n");
printf("Calculate the mass of a tesseroid model.\n\n");
printf("All units either SI or degrees!\n\n");
printf("Input:\n");
printf(" If TESSFILE is omited, will read from standard input (stdin)\n");
printf(" TESSFILE: File containing the tesseroid model\n");
printf(" * Each tesseroid is specified by the values of its borders\n");
printf(" and density\n");
printf(" * The file should contain one tesseroid per line\n");
printf(" * Each line should have the following column format:\n");
printf(" West East South North Top Bottom Density\n");
printf(" * Top and Bottom should be read as 'depth to top' and \n");
printf(" 'depth to bottom' from the mean Earth radius. Use negative\n");
printf(" values if above the surface, for example when modeling\n");
printf(" topography\n");
printf(" * If a line starts with # it will be considered a comment\n");
printf(" and will be ignored\n\n");
printf("Output:\n");
printf(" Printed to standard output (stdout) in same units as input\n\n");
printf("Options:\n");
printf(" -rLOW/HIGH Only take into account tesseroids with\n");
printf(" density between LOW and HIGH\n");
printf(" -h Print instructions.\n");
printf(" --version Print version and license information.\n");
printf(" -v Enable verbose printing to stderr.\n");
printf(" -lFILENAME Print log messages to file FILENAME.\n");
print_copyright();
}
/** Main */
int main(int argc, char **argv)
{
char *progname = "tessmass";
TESSMASS_ARGS args;
TESSEROID tess;
double mass = 0;
int rc, line, size = 0, error_exit = 0, bad_input = 0;
FILE *logfile = NULL, *modelfile = NULL;
time_t rawtime;
struct tm * timeinfo;
char buff[10000];
log_init(LOG_INFO);
rc = parse_tessmass_args(argc, argv, progname, &args, &print_help);
if(rc == 2)
{
return 0;
}
if(rc == 1)
{
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
/* Set the appropriate logging level and log to file if necessary */
if(!args.verbose)
{
log_init(LOG_WARNING);
}
if(args.logtofile)
{
logfile = fopen(args.logfname, "w");
if(logfile == NULL)
{
log_error("unable to create log file %s", args.logfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
log_tofile(logfile, LOG_INFO);
}
/* Print standard verbose */
log_info("%s (Tesseroids project) %s", progname, tesseroids_version);
time(&rawtime);
timeinfo = localtime(&rawtime);
log_info("(local time) %s", asctime(timeinfo));
/* If an input file is not given, read from stdin. Else open the file */
if(rc == 3)
{
log_info("Reading tesseroids from stdin");
modelfile = stdin;
}
else
{
log_info("Reading tesseroids from file %s", args.inputfname);
modelfile = fopen(args.inputfname, "r");
if(modelfile == NULL)
{
log_error("failed to open file %s", args.inputfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
if(args.logtofile)
fclose(logfile);
return 1;
}
}
/* Read the tesseroids, convert and print to stdout */
for(line = 1; !feof(modelfile); line++)
{
if(fgets(buff, 10000, modelfile) == NULL)
{
if(ferror(stdin))
{
log_error("problem encountered reading line %d", line);
error_exit = 1;
break;
}
}
else
{
/* Check for comments and blank lines */
if(buff[0] == '#' || buff[0] == '\r' || buff[0] == '\n')
{
continue;
}
/* Remove any trailing spaces or newlines */
strstrip(buff);
if(gets_tess(buff, &tess))
{
log_warning("bad/invalid tesseroid at line %d", line);
bad_input++;
continue;
}
if(args.use_range)
{
mass += tess_range_mass(&tess, 1, args.low_dens,
args.high_dens);
size++;
}
else
{
mass += tess_total_mass(&tess, 1);
size++;
}
}
}
if(args.use_range)
{
log_info("Mass within density range %g/%g:", args.low_dens,
args.high_dens);
}
else
{
log_info("Total mass:");
}
printf("%.15g\n", mass);
if(bad_input)
{
log_warning("Encountered %d bad input line(s) which were skipped",
bad_input);
}
if(error_exit)
{
log_warning("Terminating due to error in input");
log_warning("Try '%s -h' for instructions", progname);
}
else
{
log_info("Mass calculated from %d tesseroids", size);
}
/* Clean up */
if(rc != 3)
fclose(modelfile);
if(args.logtofile)
fclose(logfile);
return 0;
}

230
toolkits/tessmodgen.c Normal file
View File

@@ -0,0 +1,230 @@
/*
Generate tesseroid model from a regular grid.
*/
#include <stdio.h>
#include <time.h>
#include "../lib/version.h"
#include "../lib/parsers.h"
#include "../lib/logger.h"
#include "../lib/geometry.h"
/** Print the help message */
void print_help()
{
printf("Usage: tessmodgen [ARGUMENTS] [OPTIONS]\n\n");
printf("Generate a tesseroid model of an interface, like topography,\n");
printf("moho, sediment thickness, etc.\n\n");
printf("Each tesseroid has its top face centered of the respective grid\n");
printf("point. The top and bottom of the tesseroid are define as:\n");
printf(" * top = height of grid point and bottom = reference level\n");
printf(" if height of grid point > reference level\n");
printf(" * other way around if otherwise\n\n");
printf("All units either SI or degrees!\n\n");
printf("Input:\n");
printf(" REGULAR grid passed through standard input (stdin).\n");
printf(" Reads 3 values per line: longitude latitude height\n");
printf(" height should be read as 'height above the mean Earth radius'\n");
printf(" If bellow the Earth radius use negative heights.\n");
printf(" Lines that start with # are ignored as comments.\n");
printf(" Lines should be no longer than 10000 (ten thousand) characters.");
printf(" \n\n");
printf("Output:\n");
printf(" Tesseroids printed to standard output (stdout)\n");
printf(" * Each tesseroid is specified by the values of its borders\n");
printf(" and density\n");
printf(" * Will print one tesseroid per line\n");
printf(" * Each line has the following column format:\n");
printf(" West East South North Top Bottom Density\n");
printf(" * Top and Bottom should be read as 'height to top' and \n");
printf(" 'height to bottom' from the mean Earth radius. Use negative\n");
printf(" values if bellow the surface, for example when modeling\n");
printf(" deep structures, and positive if above the surface, for\n");
printf(" example when modeling topography.\n");
printf(" * If a line starts with # it will be considered a comment\n");
printf(" and will be ignored\n\n");
printf("Arguments:\n");
printf(" -sDLON/DLAT The grid spacing in the longitude and latitude\n");
printf(" directions, respectively, in DECIMAL DEGREES.\n");
printf(" Will be used as the size of the tesseroids.\n");
printf(" WARNING: You may get wrong results if -s is \n");
printf(" different from the grid spacing!\n");
printf(" -dDENS Density of the tesseroids. If ommited will expect\n");
printf(" a 4th column on the input with DENS values for\n");
printf(" each point. Tesseroids above the reference will\n");
printf(" have density DENS, and bellow will have density\n");
printf(" -DENS.\n");
printf(" -zREF Height of the reference level with respect to the\n");
printf(" mean Earth radius. If bellow the mean Earth\n");
printf(" radius, use a negative value.\n\n");
printf("Options:\n");
printf(" -h Print instructions.\n");
printf(" --version Print version and license information.\n");
printf(" -v Enable verbose printing to stderr.\n");
printf(" -lFILENAME Print log messages to file FILENAME.\n");
print_copyright();
}
/** Main */
int main(int argc, char **argv)
{
char *progname = "tessmodgen";
TESSMODGEN_ARGS args;
int rc, line, error_exit = 0, bad_input = 0, size = 0, nchars, nread;
char buff[10000];
double lon, lat, height, w, e, s, n, top, bot, dens;
FILE *logfile = NULL;
time_t rawtime;
struct tm * timeinfo;
log_init(LOG_INFO);
rc = parse_tessmodgen_args(argc, argv, progname, &args, &print_help);
if(rc == 2)
{
return 0;
}
if(rc == 1)
{
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
/* Set the appropriate logging level and log to file if necessary */
if(!args.verbose)
{
log_init(LOG_WARNING);
}
if(args.logtofile)
{
logfile = fopen(args.logfname, "w");
if(logfile == NULL)
{
log_error("unable to create log file %s", args.logfname);
log_warning("Terminating due to bad input");
log_warning("Try '%s -h' for instructions", progname);
return 1;
}
log_tofile(logfile, LOG_INFO);
}
/* Print standard verbose */
log_info("%s (Tesseroids project) %s", progname, tesseroids_version);
time(&rawtime);
timeinfo = localtime(&rawtime);
log_info("(local time) %s", asctime(timeinfo));
/* Print a header on the output with provenance information */
printf("# Tesseroid model generated by %s %s:\n", progname,
tesseroids_version);
printf("# local time: %s", asctime(timeinfo));
printf("# grid spacing (size of tesseroids): %g deg lon / %g deg lat\n",
args.dlon, args.dlat);
printf("# reference level (depth): %g\n", args.ref);
if(args.fix_density)
{
printf("# density: %g\n", args.dens);
log_info("Using fixed density value: %g", args.dens);
}
else
{
printf("# density: read from input\n");
log_info("Reading density values from input grid");
}
/* Read each regular grid from stdin and generate the tesseroids */
for(line = 1; !feof(stdin); line++)
{
if(fgets(buff, 10000, stdin) == NULL)
{
if(ferror(stdin))
{
log_error("problem encountered reading line %d", line);
error_exit = 1;
break;
}
}
else
{
/* Check for comments and blank lines */
if(buff[0] == '#' || buff[0] == '\r' || buff[0] == '\n')
{
continue;
}
strstrip(buff);
if(args.fix_density)
{
nread = sscanf(buff, "%lf %lf %lf%n", &lon, &lat, &height,
&nchars);
/* The lon != lon looks weird but this checks for NaN values */
if(nread != 3 || buff[nchars] != '\0' ||
lon != lon || lat != lat || height != height)
{
log_warning("bad/invalid grid point at line %d", line);
log_warning("skipping this line and continuing");
bad_input++;
continue;
}
}
else
{
nread = sscanf(buff, "%lf %lf %lf %lf%n", &lon, &lat, &height,
&dens, &nchars);
if(nread != 4 || buff[nchars] != '\0' ||
lon != lon || lat != lat || height != height || dens != dens)
{
log_warning("bad/invalid grid point at line %d", line);
log_warning("skipping this line and continuing");
bad_input++;
continue;
}
}
w = lon - 0.5*args.dlon;
e = lon + 0.5*args.dlon;
s = lat - 0.5*args.dlat;
n = lat + 0.5*args.dlat;
if(height >= args.ref)
{
top = height;
bot = args.ref;
if(args.fix_density)
dens = args.dens;
}
else
{
top = args.ref;
bot = height;
if(args.fix_density)
dens = -args.dens;
else
dens *= -1;
}
printf("%.15g %.15g %.15g %.15g %.15g %.15g %.15g\n", w, e, s, n,
top, bot, dens);
size++;
}
}
if(bad_input)
{
log_warning("Encountered %d bad grid points which were skipped",
bad_input);
}
if(error_exit)
{
log_warning("Terminating due to error in input");
log_warning("Try '%s -h' for instructions", progname);
}
else
{
log_info("Generated %d tesseroids.", size);
}
/* Clean up */
if(args.logtofile)
fclose(logfile);
return 0;
}

16
toolkits/tesspot.c Normal file
View File

@@ -0,0 +1,16 @@
/*
Program to calculate potential of a tesseroid model on a set of points.
*/
#include "../lib/constants.h"
#include "../lib/grav_tess.h"
#include "../lib/tessg_main.h"
/** Main */
int main(int argc, char **argv)
{
return run_tessg_main(argc, argv, "tesspot", &tess_pot,
TESSEROID_POT_SIZE_RATIO);
}