| 1 |
/*-------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* Copyright (c) 1991-2026 by the GMT Team (https://www.generic-mapping-tools.org/team.html) |
| 4 |
* See LICENSE.TXT file for copying and redistribution conditions. |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU Lesser General Public License as published by |
| 8 |
* the Free Software Foundation; version 3 or any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Contact info: www.generic-mapping-tools.org |
| 16 |
*--------------------------------------------------------------------*/ |
| 17 |
/* |
| 18 |
* grd2sph evaluates a grid using a spherical harmonics model |
| 19 |
* |
| 20 |
* Author: Paul Wessel |
| 21 |
* Date: 10-FEB-2015 |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "gmt_dev.h" |
| 25 |
|
| 26 |
#define THIS_MODULE_CLASSIC_NAME "grd2sph" |
| 27 |
#define THIS_MODULE_MODERN_NAME "grd2sph" |
| 28 |
#define THIS_MODULE_LIB "core" |
| 29 |
#define THIS_MODULE_PURPOSE "Compute spherical harmonic coefficients from global grid" |
| 30 |
#define THIS_MODULE_KEYS "<GI,>DO" |
| 31 |
|
| 32 |
#define GMT_PROG_OPTIONS "->Vbho" |
| 33 |
|
| 34 |
GMT_LOCAL struct GRD2SPH_CTRL { /* All control options for this program (except common args) */ |
| 35 |
/* active is true if the option has been activated */ |
| 36 |
struct In { /* <grdfile> */ |
| 37 |
bool active; |
| 38 |
char *file; |
| 39 |
} In; |
| 40 |
struct Out { /* -> */ |
| 41 |
bool active; |
| 42 |
char *file; |
| 43 |
} Out; |
| 44 |
struct N { /* -Ng|m|s */ |
| 45 |
bool active; |
| 46 |
char mode; |
| 47 |
} N; |
| 48 |
struct Q { /* -Q */ |
| 49 |
bool active; |
| 50 |
} Q; |
| 51 |
}; |
| 52 |
|
| 53 |
GMT_LOCAL void *New_Ctrl (struct GMT_CTRL *GMT) { /* Allocate and initialize a new control structure */ |
| 54 |
struct GRD2SPH_CTRL *C = NULL; |
| 55 |
|
| 56 |
C = GMT_memory (GMT, NULL, 1, struct GRD2SPH_CTRL); |
| 57 |
|
| 58 |
/* Initialize values whose defaults are not 0/false/NULL */ |
| 59 |
C->N.mode = 'm'; |
| 60 |
|
| 61 |
return (C); |
| 62 |
} |
| 63 |
|
| 64 |
GMT_LOCAL void Free_Ctrl (struct GMT_CTRL *GMT, struct GRD2SPH_CTRL *C) { /* Deallocate control structure */ |
| 65 |
if (!C) return; |
| 66 |
if (C->In.file) free (C->In.file); |
| 67 |
if (C->Out.file) free (C->Out.file); |
| 68 |
GMT_free (GMT, C); |
| 69 |
} |
| 70 |
|
| 71 |
GMT_LOCAL int usage (struct GMTAPI_CTRL *API, int level) { |
| 72 |
GMT_Message (API, GMT_TIME_NONE, "grd2sph - Create spherical harmonic model from a global grid\n\n"); |
| 73 |
GMT_Message (API, GMT_TIME_NONE, "usage: grd2sph <grid> [-N<norm>] [-Q] [%s] [%s] [%s]\n\n", GMT_V_OPT, GMT_bo_OPT, GMT_h_OPT); |
| 74 |
|
| 75 |
if (level == GMT_SYNOPSIS) return (EXIT_FAILURE); |
| 76 |
|
| 77 |
GMT_Message (API, GMT_TIME_NONE, "\t<grid> must be a global grid.\n"); |
| 78 |
GMT_Message (API, GMT_TIME_NONE, "\n\tOPTIONS:\n"); |
| 79 |
GMT_Message (API, GMT_TIME_NONE, "\t-N Normalization used for coefficients. Choose among\n"); |
| 80 |
GMT_Message (API, GMT_TIME_NONE, "\t m: Mathematical normalization - inner products summed over surface equal 1 [Default]\n"); |
| 81 |
GMT_Message (API, GMT_TIME_NONE, "\t g: Geodesy normalization - inner products summed over surface equal 4pi\n"); |
| 82 |
GMT_Message (API, GMT_TIME_NONE, "\t s: Schmidt normalization - as used in geomagnetism\n"); |
| 83 |
GMT_Message (API, GMT_TIME_NONE, "\t-Q Coefficients have phase convention from physics, i.e., the (-1)^m factor\n"); |
| 84 |
GMT_explain_options ("V,bo4,h,o,."); |
| 85 |
|
| 86 |
return (EXIT_FAILURE); |
| 87 |
} |
| 88 |
|
| 89 |
GMT_LOCAL int parse (struct GMT_CTRL *GMT, struct GRD2SPH_CTRL *Ctrl, struct GMT_OPTION *options) { |
| 90 |
/* This parses the options provided to grd2sph and sets parameters in CTRL. |
| 91 |
* Any GMT common options will override values set previously by other commands. |
| 92 |
* It also replaces any file names specified as input or output with the data ID |
| 93 |
* returned when registering these sources/destinations with the API. |
| 94 |
*/ |
| 95 |
|
| 96 |
unsigned int n_errors = 0, n_files[] = {0, 0}; |
| 97 |
struct GMT_OPTION *opt = NULL; |
| 98 |
struct GMTAPI_CTRL *API = GMT->parent; |
| 99 |
|
| 100 |
for (opt = options; opt; opt = opt->next) { |
| 101 |
switch (opt->option) { |
| 102 |
|
| 103 |
case '<': /* Got input files */ |
| 104 |
if (n_files[GMT_IN]++ == 0) Ctrl->In.file = strdup (opt->arg); |
| 105 |
break; |
| 106 |
case '>': /* Got named output file */ |
| 107 |
if (n_files[GMT_OUT]++ == 0 && GMT_check_filearg (GMT, '>', opt->arg, GMT_OUT, GMT_IS_DATASET)) |
| 108 |
Ctrl->Out.file = strdup (opt->arg); |
| 109 |
else |
| 110 |
n_errors++; |
| 111 |
break; |
| 112 |
|
| 113 |
/* Processes program-specific parameters */ |
| 114 |
|
| 115 |
case 'N': |
| 116 |
Ctrl->N.active = true; |
| 117 |
Ctrl->N.mode = opt->arg[0]; |
| 118 |
break; |
| 119 |
case 'Q': |
| 120 |
Ctrl->Q.active = true; |
| 121 |
break; |
| 122 |
|
| 123 |
default: /* Report bad options */ |
| 124 |
n_errors += GMT_default_error (GMT, opt->option); |
| 125 |
break; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
n_errors += GMT_check_condition (GMT, n_files[GMT_IN] > 1, "Syntax error: Can only handle one input grid file\n"); |
| 130 |
n_errors += GMT_check_condition (GMT, n_files[GMT_OUT] > 1, "Syntax error: Only one output destination can be specified\n"); |
| 131 |
n_errors += GMT_check_condition (GMT, !(Ctrl->N.mode == 'm' || Ctrl->N.mode == 'g' || Ctrl->N.mode == 's'), "Syntax error: -N Normalization must be one of m, g, or s\\n"); |
| 132 |
|
| 133 |
return (n_errors ? GMT_PARSE_ERROR : GMT_OK); |
| 134 |
} |
| 135 |
|
| 136 |
#define bailout(code) {GMT_Free_Options (mode); return (code);} |
| 137 |
#define Return(code) {Free_Ctrl (GMT, Ctrl); GMT_end_module (GMT, GMT_cpy); bailout (code);} |
| 138 |
|
| 139 |
int GMT_grd2sph (void *V_API, int mode, void *args) { |
| 140 |
int error; |
| 141 |
struct GMT_GRID *G = NULL; |
| 142 |
struct GMT_DATASET *D = NULL; |
| 143 |
struct GRD2SPH_CTRL *Ctrl; |
| 144 |
struct GMT_CTRL *GMT = NULL, *GMT_cpy = NULL; |
| 145 |
struct GMT_OPTION *options = NULL; |
| 146 |
struct GMTAPI_CTRL *API = GMT_get_API_ptr (V_API); /* Cast from void to GMTAPI_CTRL pointer */ |
| 147 |
|
| 148 |
void *New_Ctrl (), Free_Ctrl (struct GRD2SPH_CTRL *C); |
| 149 |
|
| 150 |
Ctrl = (struct GRD2SPH_CTRL *) New_Ctrl (); /* Allocate and initialize defaults in a new control structure */ |
| 151 |
|
| 152 |
/*----------------------- Standard module initialization and parsing ----------------------*/ |
| 153 |
|
| 154 |
if (API == NULL) return (GMT_NOT_A_SESSION); |
| 155 |
options = GMT_Create_Options (API, mode, args); if (API->error) return (API->error); /* Set or get option list */ |
| 156 |
|
| 157 |
if (!options || options->option == GMT_OPT_USAGE) bailout (usage (API, GMT_USAGE));/* Return the usage message */ |
| 158 |
if (options->option == GMT_OPT_SYNOPSIS) bailout (usage (API, GMT_SYNOPSIS)); /* Return the synopsis */ |
| 159 |
|
| 160 |
/* Parse the command-line arguments */ |
| 161 |
|
| 162 |
GMT = GMT_begin_module (API, NULL, THIS_MODULE_CLASSIC_NAME, &GMT_cpy); /* Save current state */ |
| 163 |
if (GMT_Parse_Common (API, GMT_PROG_OPTIONS, options)) Return (API->error); |
| 164 |
Ctrl = New_Ctrl (GMT); /* Allocate and initialize a new control structure */ |
| 165 |
if ((error = parse (GMT, Ctrl, options)) != 0) Return (error); |
| 166 |
|
| 167 |
/*---------------------------- This is the grd2sph main code ----------------------------*/ |
| 168 |
|
| 169 |
if ((G = GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_GRID_ALL, NULL, Ctrl->In.file, NULL)) == NULL) { |
| 170 |
Return (API->error); |
| 171 |
} |
| 172 |
|
| 173 |
/* Do all the hard work! */ |
| 174 |
|
| 175 |
/* Write out the coefficients in D */ |
| 176 |
|
| 177 |
if (GMT_Write_Data (API, GMT_IS_DATASET, GMT_IS_FILE, GMT_IS_NONE, GMT_WRITE_SET, NULL, Ctrl->Out.file, D) != GMT_OK) { |
| 178 |
Return (API->error); |
| 179 |
} |
| 180 |
|
| 181 |
Return (EXIT_SUCCESS); |
| 182 |
} |