1 /*--------------------------------------------------------------------
2  *
3  *	Copyright (c) 2012-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  * gmt_hidden.h contains the hidden support structures for the 5 GMT resources
19  * GMT_DATASET, GMT_GRID, GMT_IMAGE, GMT_PALETTE, and GMT_POSTSCRIPT
20  * as well as the two auxiliary resources GMT_MATRIX and GMT_VECTOR.
21  *
22  * Author:	Paul Wessel
23  * Date:	27-OCT-2017
24  * Version:	6 API
25  */
26 
27 /*!
28  * \file gmt_resources.h
29  * \brief Definitions for the GMT resources (GMT_GRID, GMT_DATASET, etc...)
30  */
31 
32 #ifndef GMT_HIDDEN_H
33 #define GMT_HIDDEN_H
34 
gmt_get_DD_hidden(struct GMT_DATASET * p)35 static inline struct GMT_DATASET_HIDDEN     * gmt_get_DD_hidden (struct GMT_DATASET *p)     {return (p->hidden);}
gmt_get_DT_hidden(struct GMT_DATATABLE * p)36 static inline struct GMT_DATATABLE_HIDDEN   * gmt_get_DT_hidden (struct GMT_DATATABLE *p)   {return (p->hidden);}
gmt_get_DS_hidden(struct GMT_DATASEGMENT * p)37 static inline struct GMT_DATASEGMENT_HIDDEN * gmt_get_DS_hidden (struct GMT_DATASEGMENT *p) {return (p->hidden);}
gmt_get_C_hidden(struct GMT_PALETTE * p)38 static inline struct GMT_PALETTE_HIDDEN     * gmt_get_C_hidden (struct GMT_PALETTE *p)      {return (p->hidden);}
gmt_get_P_hidden(struct GMT_POSTSCRIPT * p)39 static inline struct GMT_POSTSCRIPT_HIDDEN  * gmt_get_P_hidden (struct GMT_POSTSCRIPT *p)   {return (p->hidden);}
gmt_get_V_hidden(struct GMT_VECTOR * p)40 static inline struct GMT_VECTOR_HIDDEN      * gmt_get_V_hidden (struct GMT_VECTOR *p)       {return (p->hidden);}
gmt_get_M_hidden(struct GMT_MATRIX * p)41 static inline struct GMT_MATRIX_HIDDEN      * gmt_get_M_hidden (struct GMT_MATRIX *p)       {return (p->hidden);}
gmt_get_G_hidden(struct GMT_GRID * p)42 static inline struct GMT_GRID_HIDDEN        * gmt_get_G_hidden (struct GMT_GRID *p)         {return (p->hidden);}
gmt_get_I_hidden(struct GMT_IMAGE * p)43 static inline struct GMT_IMAGE_HIDDEN       * gmt_get_I_hidden (struct GMT_IMAGE *p)        {return (p->hidden);}
gmt_get_H_hidden(struct GMT_GRID_HEADER * p)44 static inline struct GMT_GRID_HEADER_HIDDEN * gmt_get_H_hidden (struct GMT_GRID_HEADER *p)  {return (p->hidden);}
gmt_get_U_hidden(struct GMT_CUBE * p)45 static inline struct GMT_CUBE_HIDDEN    * gmt_get_U_hidden (struct GMT_CUBE *p)     {return (p->hidden);}
46 
47 /* Here are the GMT data types used for tables */
48 
49 struct GMT_OGR {	/* Struct with all things GMT/OGR for a table */
50 	/* The first parameters are usually set once per data set and do not change */
51 	unsigned int geometry;		/* @G: The geometry of this data set, if known [0 otherwise] */
52 	unsigned int n_aspatial;	/* @T: The number of aspatial fields */
53 	unsigned int rec_type;		/* Overall record type of aspatial data (GMT_IS_DATA, GMT_IS_TEXT, or GMT_IS_MIXED) */
54 	char *region;			/* @R: The region textstring [NULL if not set] */
55 	char *proj[4];			/* @J: The 1-4 projection strings [NULL if not set] */
56 	enum GMT_enum_type *type;	/* @T: The data types of the aspatial fields [NULL if not set]  */
57 	char **name;			/* @N The names of the aspatial fields [NULL if not set]  */
58 	/* The following are for OGR data only. It is filled during parsing (current segment) but is then copied to the segment header so it can be accessed later */
59 	enum GMT_enum_pol pol_mode;	/* @P: Either GMT_IS_PERIMETER or GMT_IS_HOLE (for polygons only) */
60 	char **tvalue;			/* @D: The text values of the current aspatial fields */
61 	double *dvalue;			/* @D: Same but converted to double (assumed possible) */
62 };
63 
64 struct GMT_OGR_SEG {	/* Struct with GMT/OGR aspatial data for a segment */
65 	enum GMT_enum_pol pol_mode;	/* @P: Either GMT_IS_PERIMETER or GMT_IS_HOLE (for polygons only) */
66 	unsigned int n_aspatial;	/* @T: The number of aspatial fields */
67 	char **tvalue;			/* @D: The values of the current aspatial fields (uses GMT_OGR's n_aspatial as length) */
68 	double *dvalue;			/* @D: Same but converted to double (assumed possible) */
69 };
70 
71 struct GMT_DATASEGMENT_HIDDEN {    /* Supporting information hidden from the API */
72 	enum GMT_enum_write mode;	/* 0 = output segment, 1 = output header only, 2 = skip segment */
73 	enum GMT_enum_pol pol_mode;	/* Either GMT_IS_PERIMETER  [-Pp] or GMT_IS_HOLE [-Ph] (for polygons only) */
74 	uint64_t id;			/* The internal number of the segment */
75 	size_t n_alloc;			/* The current allocation length of rows */
76 	unsigned int range;		/* Longitude reporting scheme, e.g. GMT_IS_GIVEN_RANGE [0] */
77 	int pole;			/* Spherical polygons only: If it encloses the S (-1) or N (+1) pole, or none (0) */
78 	double dist;			/* Distance from a point to this feature */
79 	double lat_limit;		/* For polar caps: the latitude of the point closest to the pole */
80 	struct GMT_OGR_SEG *ogr;	/* NULL unless OGR/GMT metadata exist for this segment */
81 	struct GMT_DATASEGMENT *next;	/* NULL unless polygon and has holes and pointing to next hole */
82 	enum GMT_enum_alloc *alloc_mode;	/* Allocation mode per column [GMT_ALLOC_INTERNALLY] */
83 	char *file[2];			/* Name of file or source [0 = in, 1 = out] */
84 };
85 
86 struct GMT_DATATABLE_HIDDEN {	/* Supporting information hidden from the API */
87 	uint64_t id;            /* The internal number of the table */
88 	size_t n_alloc;         /* The current allocation length of segments */
89 	double dist;            /* Distance from a point to this feature */
90 	unsigned int alloc_level;	/* The level it was allocated at */
91 	enum GMT_enum_write mode;	/* 0 = output table, 1 = output header only, 2 = skip table */
92 	struct GMT_OGR *ogr;    /* Pointer to struct with all things GMT/OGR (if MULTI-geometry and not MULTIPOINT) */
93 	enum GMT_enum_alloc alloc_mode;	/* Allocation mode [GMT_ALLOC_INTERNALLY] */
94 	char *file[2];          /* Name of file or source [0 = in, 1 = out] */
95 };
96 
97 struct GMT_DATASET_HIDDEN {	/* Supporting information hidden from the API */
98 	uint64_t id;			/* The internal number of the data set */
99 	size_t n_alloc;			/* The current allocation length of tables */
100 	uint64_t dim[4];		/* Only used by GMT_Duplicate_Data to override dimensions */
101 	unsigned int alloc_level;	/* The level it was allocated at */
102 	unsigned int geographic;	/* 0 for Cartesian, 1 for geographic, mostly used for memory files */
103 	enum GMT_enum_write io_mode;	/* -1 means write OGR format (requires proper -a),
104 					 * 0 means write everything to one destination [Default],
105 					 * 1 means use table->file[GMT_OUT] to write separate table,
106 					 * 2 means use segment->file[GMT_OUT] to write separate segments.
107 					 * 3 is same as 2 but with no filenames we create filenames from tbl and seg numbers */
108 	enum GMT_enum_alloc alloc_mode;	/* Allocation mode [GMT_ALLOC_INTERNALLY] */
109 	char *file[2];			/* Name of file or source [0 = in, 1 = out] */
110 };
111 
112 struct GMT_PALETTE_HIDDEN {	/* Supporting information hidden from the API */
113 	uint64_t id;			/* The internal number of the data set */
114 	size_t n_alloc;            	/* Memory allocated so far */
115 	enum GMT_enum_alloc alloc_mode;	/* Allocation mode [GMT_ALLOC_INTERNALLY] */
116 	enum GMT_enum_alloc alloc_mode_text[2];	/* Allocation mode per label|key [GMT_ALLOC_INTERNALLY] */
117 	unsigned int alloc_level;	/* The level it was allocated at */
118 	unsigned int auto_scale;	/* If 1 then we must resample to fit actual data range */
119 	unsigned int skip;		/* true if current z-slice is to be skipped */
120 	unsigned int z_adjust[2];	/* 1 if +u<unit> was parsed and scale set, 3 if z has been adjusted, 0 otherwise */
121 	unsigned int z_mode[2];	 	/* 1 if +U<unit> was parsed, 0 otherwise */
122 	unsigned int z_unit[2];	 	/* Unit enum specified via +u<unit> */
123 	double z_unit_to_meter[2];	/* Scale, given z_unit, to convert z from <unit> to meters */
124 };
125 
126 struct GMT_POSTSCRIPT_HIDDEN {	/* Supporting information hidden from the API */
127 	uint64_t id;			/* The internal number of the data set */
128 	size_t n_alloc;			/* Memory allocated so far */
129 	unsigned int alloc_level;	/* The level it was allocated at */
130 	enum GMT_enum_alloc alloc_mode;	/* Allocation mode [GMT_ALLOC_INTERNALLY] */
131 };
132 
133 struct GMT_VECTOR_HIDDEN {	/* Supporting information hidden from the API */
134 	uint64_t id;			/* The internal number of the data set */
135 	unsigned int alloc_level;	/* The level it was allocated at */
136 	unsigned int geographic;	/* 0 for Cartesian, 1 for geographic, mostly used for memory files */
137 	enum GMT_enum_alloc *alloc_mode;	/* Allocation mode per column [GMT_ALLOC_INTERNALLY] */
138 	enum GMT_enum_alloc alloc_mode_text;	/* Allocation mode per text [GMT_ALLOC_INTERNALLY] */
139 };
140 
141 struct GMT_MATRIX_HIDDEN {	/* Supporting information hidden from the API */
142 	uint64_t id;			/* The internal number of the data set */
143 	int pad;			/* The internal number of the data set */
144 	unsigned int grdtype;		/* 0 for Cartesian, > 0 for geographic and depends on 360 periodicity [see GMT_enum_grdtype in gmt_grd.h] */
145 	unsigned int alloc_level;	/* The level it was allocated at */
146 	enum GMT_enum_alloc alloc_mode;	/* Allocation mode [GMT_ALLOC_INTERNALLY] */
147 	enum GMT_enum_alloc alloc_mode_text;	/* Allocation mode per text [GMT_ALLOC_INTERNALLY] */
148 };
149 
150 struct GMT_GRID_HEADER_HIDDEN {
151 	/* ---- Variables "hidden" from the API ----
152 	 * This section is flexible.  It is not copied to any grid or image header
153 	 * or stored in any file.  It is considered private */
154 	unsigned int trendmode;          /* Holds status for detrending of grids.  0 if not detrended, 1 if mean, 2 if mid-value, and 3 if LS plane removed */
155 	unsigned int arrangement;        /* Holds status for complex grid as how the read/imag is placed in the grid (interleaved, R only, etc.) */
156 	unsigned int BC[4];              /* Boundary condition applied on each side via pad [0 = not set, 1 = natural, 2 = periodic, 3 = data] */
157 	unsigned int grdtype;            /* 0 for Cartesian, > 0 for geographic and depends on 360 periodicity [see GMT_enum_grdtype in gmt_grd.h] */
158 	unsigned int reset_pad;          /* true in cases where we need a subset from a memory grid and must compute node index separately */
159 	char name[GMT_GRID_NAME_LEN256]; /* Actual name of the file after any ?<varname> and =<stuff> has been removed */
160 	char varname[GMT_GRID_VARNAME_LEN80];/* NetCDF: variable name */
161 	int row_order;                   /* NetCDF: k_nc_start_south if S->N, k_nc_start_north if N->S */
162 	int z_id;                        /* NetCDF: id of z field */
163 	int ncid;                        /* NetCDF: file ID */
164 	int xy_dim[2];                   /* NetCDF: dimension order of x and y; normally {1, 0} */
165 	int xyz_id[3];                   /* NetCDF: id of x, y, and z (if cube) field */
166 	size_t t_index[3];               /* NetCDF: index of higher coordinates */
167 	size_t data_offset;              /* NetCDF: distance from the beginning of the in-memory grid */
168 	size_t n_alloc;                  /* Bytes allocated for this grid */
169 	unsigned int stride;             /* NetCDF: distance between two rows in the in-memory grid */
170 	double r_inc[2];                 /* Reciprocal incs, i.e. 1/inc */
171 	char   flags[4];                 /* Flags used for ESRI grids */
172 	char  *pocket;                   /* GDAL: A working variable handy to transmit info between funcs e.g. +b<band_info> to gdalread */
173 	double bcr_threshold;            /* sum of cardinals must >= threshold in bilinear; else NaN */
174 	unsigned int has_NaNs;           /* Is 2 if the grid contains any NaNs, 1 if it does not, and 0 if no check has yet happened */
175 	unsigned int bcr_interpolant;    /* Interpolation function used (0, 1, 2, 3) */
176 	unsigned int bcr_n;              /* Width of the interpolation function */
177 	unsigned int nxp;                /* if X periodic, nxp > 0 is the period in pixels  */
178 	unsigned int nyp;                /* if Y periodic, nxp > 0 is the period in pixels  */
179 	unsigned int no_BC;              /* If true we skip BC stuff entirely */
180 	unsigned int gn;                 /* true if top    edge will be set as N pole  */
181 	unsigned int gs;                 /* true if bottom edge will be set as S pole  */
182 	unsigned int is_netcdf4;         /* true if netCDF-4/HDF5 format */
183 	unsigned int var_spacing[2];     /* true if a netcdf grid has non-equidistant x and/or y arrays */
184 	enum GMT_enum_type orig_datatype; /* GMT_FLOAT, GMT_SHORT, etc how the source grid was represented */
185 	size_t z_chunksize[2];           /* chunk size (lat,lon) */
186 	unsigned int z_scale_given;	 /* 1 if +s was specified */
187 	unsigned int z_offset_given;	 /* 1 if +o was specified */
188 	unsigned int z_shuffle;          /* if shuffle filter is turned on */
189 	unsigned int z_deflate_level;    /* if deflate filter is in use */
190 	unsigned int z_scale_autoadjust; /* if z_scale_factor should be auto-detected */
191 	unsigned int z_offset_autoadjust;/* if z_add_offset should be auto-detected */
192 					 /* xy_*[] is separate settings for GMT_IN and GMT_OUT */
193 	unsigned int xy_adjust[2];	 /* 1 if +u<unit> was parsed and scale set, 3 if xy has been adjusted, 0 otherwise */
194 	unsigned int xy_mode[2];	 /* 1 if +U<unit> was parsed, 0 otherwise */
195 	unsigned int xy_unit[2];	 /* Unit enum specified via +u<unit> */
196 	double xy_unit_to_meter[2];	 /* Scale, given xy_unit, to convert xy from <unit> to meters */
197 	uint64_t (*index_function) (struct GMT_GRID_HEADER *, uint64_t, uint64_t, uint64_t);	/* Pointer to index function (for images only) */
198 };
199 
200 struct GMT_GRID_HIDDEN {	/* Supporting information hidden from the API */
201 	unsigned int id;                /* The internal number of the grid */
202 	unsigned int alloc_level;       /* The level it was allocated at */
203 	enum GMT_enum_alloc alloc_mode; /* Allocation mode [GMT_ALLOC_INTERNALLY] */
204 	enum GMT_enum_alloc xy_alloc_mode[2];	 /* Stores how the x and y arrays were allocated (external or internal) */
205 	void *extra;                    /* Row-by-row machinery information [NULL] */
206 };
207 
208 struct GMT_IMAGE_HIDDEN {	/* Supporting information hidden from the API */
209 	uint64_t id;                    /* The internal number of the data set */
210 	unsigned int alloc_level;       /* The level it was allocated at */
211 	enum GMT_enum_alloc alloc_mode;	/* Allocation mode [GMT_ALLOC_INTERNALLY] */
212 };
213 
214 struct GMT_CUBE_HIDDEN {	/* Supporting information hidden from the API */
215 	unsigned int id;                /* The internal number of the grid */
216 	unsigned int alloc_level;       /* The level it was allocated at */
217 	enum GMT_enum_alloc alloc_mode; /* Allocation mode [GMT_ALLOC_INTERNALLY] */
218 	enum GMT_enum_alloc xyz_alloc_mode[3];	 /* Stores how the x, y and z arrays were allocated (external or internal) */
219 };
220 
221 
222 /* Get the segments next segment (for holes in perimeters */
gmt_get_next_S(struct GMT_DATASEGMENT * S)223 static inline struct GMT_DATASEGMENT * gmt_get_next_S (struct GMT_DATASEGMENT *S) {struct GMT_DATASEGMENT_HIDDEN *SH = gmt_get_DS_hidden (S); return (SH->next);}
224 
225 #endif /* GMT_HIDDEN_H */
226