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 /*
19  * Include file for grd i/o
20  *
21  * Author:	Paul Wessel
22  * Date:	1-JAN-2010
23  * Version:	6 API
24  */
25 
26 /*!
27  * \file gmt_grdio.h
28  * \brief Include file for grd i/o
29  */
30 
31 #ifndef GMT_GRDIO_H
32 #define GMT_GRDIO_H
33 
34 /* Constants for Sandwell/Smith *.img grids */
35 
36 #define GMT_IMG_MINLON		0.0
37 #define GMT_IMG_MAXLON		360.0
38 #define GMT_IMG_MINLAT_72	-72.0059773539
39 #define GMT_IMG_MAXLAT_72	+72.0059773539
40 #define GMT_IMG_MINLAT_80	-80.7380086280
41 #define GMT_IMG_MAXLAT_80	+80.7380086280
42 #define GMT_IMG_MINLAT_85	-85.0511287798
43 #define GMT_IMG_MAXLAT_85	+85.0511287798
44 
45 #define GMT_IMG_REMARK	"Spherical Mercator Projected with -Jm1 -R"
46 
47 enum GMT_enum_img {
48 	GMT_IMG_NLON_1M    = 21600U, /* At 1 min resolution */
49 	GMT_IMG_NLON_2M    = 10800U, /* At 2 min resolution */
50 	GMT_IMG_NLON_4M    =  5400U, /* At 4 min resolution */
51 	GMT_IMG_NLAT_1M_72 = 12672U, /* At 1 min resolution */
52 	GMT_IMG_NLAT_2M_72 = 6336U,  /* At 2 min resolution */
53 	GMT_IMG_NLAT_4M_72 = 3168U,	 /* At 4 min resolution */
54 	GMT_IMG_NLAT_1M_80 = 17280U, /* At 1 min resolution */
55 	GMT_IMG_NLAT_2M_80 = 8640U,  /* At 2 min resolution */
56 	GMT_IMG_NLAT_4M_80 = 4320U,	 /* At 4 min resolution */
57 	GMT_IMG_NLAT_1M_85 = 21600U, /* At 1 min resolution */
58 	GMT_IMG_NLAT_2M_85 = 10800U, /* At 2 min resolution */
59 	GMT_IMG_NLAT_4M_85 = 5400U,  /* At 4 min resolution */
60 	GMT_IMG_ITEMSIZE   = 2U      /* Size of 2 byte short ints */
61 };
62 #ifdef DOUBLE_PRECISION_GRID
63 #define GMT_GRD_FORMAT 'd'
64 #else
65 #define GMT_GRD_FORMAT 'f'
66 #endif
67 
68 /*! Special grid format IDs */
69 
70 enum Gmt_grid_id {
71 	/* DO NOT change the order because id values have grown historically.
72 	 * Append newly introduced id's at the end. */
73 	k_grd_unknown_fmt = 0,	/* if grid format cannot be auto-detected */
74 	GMT_GRID_IS_BF,         /* GMT native, C-binary format (32-bit float) */
75 	GMT_GRID_IS_BS,         /* GMT native, C-binary format (16-bit integer) */
76 	GMT_GRID_IS_RB,         /* SUN rasterfile format (8-bit standard) */
77 	GMT_GRID_IS_BB,         /* GMT native, C-binary format (8-bit integer) */
78 	GMT_GRID_IS_BM,         /* GMT native, C-binary format (bit-mask) */
79 	GMT_GRID_IS_SF,         /* Golden Software Surfer format 6 (32-bit float) */
80 	GMT_GRID_IS_CB,         /* GMT netCDF format (8-bit integer) */
81 	GMT_GRID_IS_CS,         /* GMT netCDF format (16-bit integer) */
82 	GMT_GRID_IS_CI,         /* GMT netCDF format (32-bit integer) */
83 	GMT_GRID_IS_CF,         /* GMT netCDF format (32-bit float) */
84 	GMT_GRID_IS_CD,         /* GMT netCDF format (64-bit float) */
85 	GMT_GRID_IS_RF,         /* GEODAS grid format GRD98 (NGDC) */
86 	GMT_GRID_IS_BI,         /* GMT native, C-binary format (32-bit integer) */
87 	GMT_GRID_IS_BD,         /* GMT native, C-binary format (64-bit float) */
88 	GMT_GRID_IS_NB,         /* GMT netCDF format (8-bit integer) */
89 	GMT_GRID_IS_NS,         /* GMT netCDF format (16-bit integer) */
90 	GMT_GRID_IS_NI,         /* GMT netCDF format (32-bit integer) */
91 	GMT_GRID_IS_NF,         /* GMT netCDF format (32-bit float) */
92 	GMT_GRID_IS_ND,         /* GMT netCDF format (64-bit float) */
93 	GMT_GRID_IS_SD,         /* Golden Software Surfer format 7 (64-bit float, read-only) */
94 	GMT_GRID_IS_AF,         /* Atlantic Geoscience Center format AGC (32-bit float) */
95 	GMT_GRID_IS_GD,         /* Import through GDAL */
96 	GMT_GRID_IS_EI,         /* ESRI Arc/Info ASCII Grid Interchange format (ASCII integer) */
97 	GMT_GRID_IS_EF          /* ESRI Arc/Info ASCII Grid Interchange format (ASCII float, write-only) */
98 };
99 #define GMT_N_GRD_FORMATS 25 /* Number of formats above plus 1 */
100 
101 #define GMT_GRID_IS_GOLDEN7	GMT_GRID_IS_SD
102 #define GMT_GRID_IS_GDAL	GMT_GRID_IS_GD
103 
104 #include "gmt_customio.h"
105 
106 /*! Holds any -R -I -F settings passed indirectly via -R<grdfile> */
107 struct GMT_GRID_INFO {
108 	struct GMT_GRID_HEADER grd;	/* Header of grid file passed via -R */
109 	bool active;			/* true if initialized via -R */
110 };
111 
112 /*! Holds book-keeping information needed for row-by-row actions */
113 struct GMT_GRID_ROWBYROW {
114 	size_t size;		/* Bytes per item [4 for float, 1 for byte, etc] */
115 	size_t n_byte;		/* Number of bytes for row */
116 	unsigned int row;	/* Current row */
117 	bool open;		/* true if we have already opened the file */
118 	bool check;		/* true if we must replace NaNs with another representation on i/o */
119 	bool auto_advance;	/* true if we want to read file sequentially */
120 
121 	int fid;		/* NetCDF file number [netcdf files only] */
122 	size_t edge[2];		/* Dimension arrays [netcdf files only] */
123 	size_t start[2];	/* Position arrays [netcdf files only] */
124 #ifdef DEBUG
125 	off_t pos;		/* Current file pos for binary files */
126 #endif
127 
128 	FILE *fp;		/* File pointer [for native files] */
129 
130 	void *v_row;		/* Void Row pointer for any data format */
131 };
132 
133 #ifdef __APPLE__ /* Accelerate framework */
134 #include <Accelerate/Accelerate.h>
135 #undef I /* Because otherwise we are in trouble with, e.g., struct GMT_IMAGE *I */
136 #endif
137 
138 /*! Routine that scales and offsets the data in a vector */
scale_and_offset_f(gmt_grdfloat * data,size_t length,gmt_grdfloat scale,gmt_grdfloat offset)139 static inline void scale_and_offset_f (gmt_grdfloat *data, size_t length, gmt_grdfloat scale, gmt_grdfloat offset) {
140 	/*  data:   Single-precision real input vector
141 	 *  length: The number of elements to process
142 	 * This function uses the vDSP portion of the Accelerate framework if possible */
143 #ifndef __APPLE__
144 	size_t n;
145 #endif
146 	if (scale == 1) /* offset only */
147 #ifdef __APPLE__ /* Accelerate framework */
148 #ifdef DOUBLE_PRECISION_GRID
149 		vDSP_vsaddD (data, 1, &offset, data, 1, length);
150 #else
151 		vDSP_vsadd (data, 1, &offset, data, 1, length);
152 #endif
153 #else
154 		for (n = 0; n < length; ++n)
155 			data[n] += offset;
156 #endif
157 	else if (offset == 0) /* scale only */
158 #ifdef __APPLE__ /* Accelerate framework */
159 #ifdef DOUBLE_PRECISION_GRID
160 		vDSP_vsmulD (data, 1, &scale, data, 1, length);
161 #else
162 		vDSP_vsmul (data, 1, &scale, data, 1, length);
163 #endif
164 #else
165 		for (n = 0; n < length; ++n)
166 			data[n] *= scale;
167 #endif
168 	else /* scale + offset */
169 #ifdef __APPLE__ /* Accelerate framework */
170 #ifdef DOUBLE_PRECISION_GRID
171 		vDSP_vsmsaD (data, 1, &scale, &offset, data, 1, length);
172 #else
173 		vDSP_vsmsa (data, 1, &scale, &offset, data, 1, length);
174 #endif
175 #else
176 		for (n = 0; n < length; ++n)
177 			data[n] = data[n] * scale + offset;
178 #endif
179 }
180 
181 EXTERN_MSC int gmt_grd_format_decoder (struct GMT_CTRL *GMT, const char *code, unsigned int *type_id);
182 EXTERN_MSC int gmt_grd_get_format (struct GMT_CTRL *GMT, char *file, struct GMT_GRID_HEADER *header, bool magic);
183 EXTERN_MSC int gmt_grd_prep_io (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *header, double wesn[], unsigned int *width, unsigned int *height, int *first_col, int *last_col, int *first_row, int *last_row, unsigned int **index);
184 EXTERN_MSC int gmt_update_grd_info (struct GMT_CTRL *GMT, char *file, struct GMT_GRID_HEADER *header);
185 EXTERN_MSC void gmt_scale_and_offset_f (struct GMT_CTRL *GMT, gmt_grdfloat *data, size_t length, double scale, double offset);
186 EXTERN_MSC int gmt_grd_layout (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *h, gmt_grdfloat *grid, unsigned int complex_mode, unsigned int direction);
187 EXTERN_MSC void gmt_grd_mux_demux (struct GMT_CTRL *GMT, struct GMT_GRID_HEADER *h, gmt_grdfloat *data, unsigned int mode);
188 
189 #endif /* GMT_GRDIO_H */
190