1 /*--------------------------------------------------------------------
2 *
3 * Copyright (c) 1991-2021 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 * Testing grid i/o for complex grids. Needs the two test grids
19 * in_real.grd and in_imag.grd to be present in the current directory.
20 * Also takes an optional argument =<gridformatcode>, e.g. =bf
21 * for testing that particular grid format [Default uses GMT defaults]
22 * On output the grids out_real.grd and out_real_after_demux.grd should
23 * be identical to in_real.grd and out_imag.grd and out_imag_after_demux.grd
24 * should be identical to in_imag.grd.
25 *
26 * Version: 5
27 * Created: 22-Mar-2013
28 *
29 */
30
31 #include "gmt_dev.h"
32
main(int argc,char * argv[])33 int main (int argc, char *argv[]) {
34 unsigned int k;
35 char *format[6] = {"in_real.grd%s", "in_imag.grd%s", "out_real.grd%s", "out_imag.grd%s", "out_real_after_demux.grd%s", "out_imag_after_demux.grd%s"};
36 char *name[6], *code, *def_code = "", buffer[GMT_LEN64] = {""};
37 struct GMTAPI_CTRL *API = NULL; /* GMT API control structure */
38 struct GMT_GRID *G_real = NULL, *G_imag = NULL, *G_cplx = NULL;
39
40 /* 0. Create file names using the formats and code given [if any] */
41 code = (argc == 2) ? argv[1] : def_code;
42 for (k = 0; k < 6; k++) {
43 sprintf (buffer, format[k], code);
44 name[k] = strdup (buffer);
45 }
46
47 /* 1. Initializing new GMT session */
48 if ((API = GMT_Create_Session (argv[0], GMT_PAD_DEFAULT, GMT_SESSION_NORMAL, NULL)) == NULL) exit (EXIT_FAILURE);
49
50 /* 2. Read in real and imaginary components into one grid */
51 if ((G_cplx = GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA | GMT_GRID_IS_COMPLEX_REAL, NULL, name[0], NULL)) == NULL) exit (API->error);
52 if (GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_DATA_ONLY | GMT_GRID_IS_COMPLEX_IMAG, NULL, name[1], G_cplx) == NULL) exit (API->error);
53
54 /* 3. Write out real and imaginary components from one grid */
55 if (GMT_Write_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA | GMT_GRID_IS_COMPLEX_REAL, NULL, name[2], G_cplx) != GMT_NOERROR) exit (API->error);
56 if (GMT_Write_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA | GMT_GRID_IS_COMPLEX_IMAG, NULL, name[3], G_cplx) != GMT_NOERROR) exit (API->error);
57
58 /* 4. Read in real components into one grid, multiplex, then demultiplex, then write out real again */
59 if ((G_real = GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA | GMT_GRID_IS_COMPLEX_REAL, NULL, name[0], NULL)) == NULL) exit (API->error);
60 gmt_grd_mux_demux (API->GMT, G_real->header, G_real->data, GMT_GRID_IS_INTERLEAVED); /* First multiplex ... */
61 gmt_grd_mux_demux (API->GMT, G_real->header, G_real->data, GMT_GRID_IS_SERIAL); /* Then demultiplex .. */
62 if (GMT_Write_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA | GMT_GRID_IS_COMPLEX_REAL, NULL, name[4], G_real) != GMT_NOERROR) exit (API->error);
63
64 /* 5. Read in imag component into one grid, multiplex, then demultiplex, then write out imag again */
65 if ((G_imag = GMT_Read_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA | GMT_GRID_IS_COMPLEX_IMAG, NULL, name[1], NULL)) == NULL) exit (API->error);
66 gmt_grd_mux_demux (API->GMT, G_real->header, G_real->data, GMT_GRID_IS_INTERLEAVED); /* First multiplex ... */
67 gmt_grd_mux_demux (API->GMT, G_real->header, G_real->data, GMT_GRID_IS_SERIAL); /* Then demultiplex .. */
68 if (GMT_Write_Data (API, GMT_IS_GRID, GMT_IS_FILE, GMT_IS_SURFACE, GMT_CONTAINER_AND_DATA | GMT_GRID_IS_COMPLEX_IMAG, NULL, name[5], G_imag) != GMT_NOERROR) exit (API->error);
69
70 for (k = 0; k < 6; k++) gmt_M_str_free (name[k]);
71
72 /* 8. Destroy GMT session */
73 if (GMT_Destroy_Session (API)) exit (EXIT_FAILURE);
74
75 exit (GMT_NOERROR);
76 }
77