1 /*!
2  *
3  * \mainpage
4    <b>CliffMetrics</b> (Automatic Cliff Metrics delineation) delineates the location of the coastline, coastline normals, and cliff metrics along these normals\n
5 
6    See <a href="https://github.com/coastalme/CliffMetrics" target="_blank">https://github.com/coastalme/CliffMetrics</a> for the latest version of the source code.\n
7 
8  * \section intro_sec Introduction
9  * <b>TODO</b> Say more about CliffMetrics here\n
10 
11  * \section install_sec Installation
12 
13  * \subsection step1 Step 1: Opening the box
14 
15  * \subsection step2 Step 2: Running CliffMetrics
16 
17  * \subsection step3 Step 3: Building datasets
18  *
19  * \file cliffmetrics.h
20  * \brief This file contains global definitions for CliffMetrics
21  *
22  */
23 
24 #ifndef CLIFFMETRICS_H
25 #define CLIFFMETRICS_H
26 /*===============================================================================================================================
27 
28  This file is part of CliffMetrics, the Coastal Modelling Environment.
29 
30  CliffMetrics is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
31 
32  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
33 
34  You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 
36 ===============================================================================================================================*/
37 #include <climits>
38 #include <sstream>
39 
40 #include "delineation.h"
41 
42 
43 //===================================================== platform-specific stuff =================================================
44 #ifdef _WIN32
45    #define           access   _access
46    #define           F_OK     0                                   // Test for file existence
47 #endif
48 
49 #ifdef _MSC_VER
50    // MS Visual C++, byte order is IEEE little-endian, 32-bit
51    #ifdef _DEBUG
52       #include <crtdbg.h>                          // useful
53    #endif
54 
55    // clock_t is a signed long: see <time.h>
56    long const     CLOCK_T_MIN                      = LONG_MIN;
57    double const   CLOCK_T_RANGE                    = static_cast<double>(LONG_MAX) - static_cast<double>(CLOCK_T_MIN);
58    #ifdef _M_ALPHA
59       string const PLATFORM                   = "Alpha/MS Visual C++";
60    #elif defined _M_IX86
61       string const PLATFORM                   = "Intel x86/MS Visual C++";
62    #elif defined _M_MPPC
63       string const PLATFORM                   = "Power PC/MS Visual C++";
64    #elif defined _M_MRX000
65       string const PLATFORM                   = "MIPS/MS Visual C++";
66    #else
67       string const PLATFORM                   = "Other/MS Visual C++";
68    #endif
69 #endif
70 
71 #ifdef __GNUG__
72    // GNU C++
73    #ifndef CPU
74       #error GNU CPU not defined!
75    #else
76       #ifdef x86
77          // Intel x86, byte order is little-endian, 32-bit
78          string const PLATFORM                = "Intel x86/GNU C++";
79          // clock_t is an unsigned long: see <time.h>
80          unsigned long const CLOCK_T_MIN           = 0;
81          double const CLOCK_T_RANGE                = static_cast<double>(ULONG_MAX);
82 
83       #elif defined rs6000
84          // IBM RS-6000, byte order is big-endian, 32-bit
85          string const PLATFORM                = "IBM RS-6000/GNU C++";
86          // clock_t is a signed long: see <time.h> NEED TO CHECK
87          long const CLOCK_T_MIN                    = LONG_MIN;
88          double const CLOCK_T_RANGE                = static_cast<double>(LONG_MAX) - static_cast<double>(CLOCK_T_MIN);
89       #elif defined ultrasparc
90          // Sun UltraSparc, byte order is big-endian, 32-bit
91          string const   PLATFORM              = "Sun UltraSPARC/GNU C++";
92          // clock_t is a signed long: see <time.h>
93          long const CLOCK_T_MIN                    = LONG_MIN;
94          double const CLOCK_T_RANGE                = static_cast<double>(LONG_MAX) - static_cast<double>(CLOCK_T_MIN);
95       #else
96          // Something else, assume 32-bit
97          string const PLATFORM                = "Other/GNU C++";
98          // clock_t is a signed long: NEED TO CHECK <time.h>
99          long const CLOCK_T_MIN                    = LONG_MIN;
100          double const CLOCK_T_RANGE                = static_cast<double>(LONG_MAX) - static_cast<double>(CLOCK_T_MIN);
101       #endif
102    #endif
103 #endif
104 
105 #ifdef __MINGW32__
106    // Minimalist GNU for Windows
107 //   #define __USE_MINGW_ANSI_STDIO 1        // Fix long doubles output problem, see http://stackoverflow.com/questions/7134547/gcc-printf-and-long-double-leads-to-wrong-output-c-type-conversion-messes-u
108 
109    #define WEXITSTATUS(x) ((x) & 0xff)
110 #endif
111 
112 #ifdef __HP_aCC
113    // HP-UX aCC, byte order is big-endian, can be either 32-bit or 64-bit
114    string const PLATFORM                      = "HP-UX aC++";
115    // clock_t is an unsigned long: see <time.h>
116    unsigned long const CLOCK_T_MIN                 = 0;
117    #ifdef __ia64
118       // However, clock_t is a 32-bit unsigned long and we are using 64-bit unsigned longs here
119       double const CLOCK_T_RANGE                      = 4294967295UL;   // crude, improve
120    #else
121       double const CLOCK_T_RANGE                      = static_cast<double>(ULONG_MAX);
122    #endif
123 #endif
124 
125 
126 //===================================================== hard-wired constants ====================================================
127 string const   PROGNAME                      = "CliffMetrics 0.9.9 - 9 Jan 2017";
128 string const   SHORTNAME                     = "CLIFF";
129 string const   CLIFF_INI                     = "cliffmetrics.ini";
130 
131 string const   COPYRIGHT                     = "(C) 2017 Andres Payo, David Favis-Mortlock, Martin Hurst, Monica Palaseanu-Lovejoy ";
132 string const   LINE                          = "-------------------------------------------------------------------------------";
133 string const   DISCLAIMER1                   = "This program is distributed in the hope that it will be useful, but WITHOUT ANY";
134 string const   DISCLAIMER2                   = "WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A";
135 string const   DISCLAIMER3                   = "PARTICULAR PURPOSE. See the GNU General Public License for more details. You";
136 string const   DISCLAIMER4                   = "should have received a copy of the GNU General Public License along with this";
137 string const   DISCLAIMER5                   = "program; if not, contact the Free Software Foundation, Inc., 675 Mass Ave,";
138 string const   DISCLAIMER6                   = "Cambridge, MA 02139, USA.";
139 
140 string const   ABOUT                         = "delineates the location of the coastline, coastline normals, and cliff metrics along these normals";
141 string const   THANKS                        = "Many thanks to:\n\tMike Ellis";
142 string const   GDALDRIVERS                   = "GDAL drivers";
143 
144 string const   USAGE                         = "Usage: cme [OPTION]...";
145 string const   USAGE1                        = "  --gdal             List GDAL drivers";
146 string const   USAGE2                        = "  --about            Information about this program";
147 string const   USAGE3                        = "  --help             Display this text";
148 string const   USAGE4                        = "  --home=DIRECTORY   Specify the location of the .ini file etc.";
149 string const   USAGE5                        = "  --datafile=FILE    Specify the location and name of the main datafile";
150 
151 string const   STARTNOTICE                   = "- Started on ";
152 string const   INITNOTICE                    = "- Initializing";
153 string const   READFILELOC                   = "  - Reading file locations: ";
154 string const   READRUNDATA                   = "  - Reading run data file: ";
155 string const   READDTM                       = "  - Reading Digital Terrain Model o DTM: ";
156 string const   READRASTERFILES               = "  - Reading raster GIS files";
157 string const   READVECTORFILES               = "  - Reading vector GIS files";
158 string const   READICVFILE                   = "    - Coastline: ";
159 string const   ALLOCATEMEMORY                = "  - Allocating memory for raster grid";
160 string const   INITIALIZING                  = "  - Initializing";
161 string const   RUNNOTICE                     = "- Running delineation";
162 string const   SIMULATING                    = "\r  - Delineating ";
163 string const   FINALOUTPUT                   = "- Writing final output";
164 string const   SENDEMAIL                     = "  - Sending email to ";
165 string const   RUNENDNOTICE                  = "- Run ended on ";
166 string const   PRESSKEY                      = "Press any key to continue...";
167 
168 string const   ERRORNOTICE                   = "- Run ended with error code ";
169 string const   EMAILERROR                    = "Could not send email";
170 
171 char const     PATH_SEPARATOR                = '/';               // Works for Windows too!
172 char const     SPACE                         = ' ';
173 char const     QUOTE1                        = ';';
174 char const     QUOTE2                        = '#';
175 string const   SPACESTR                      = " ";
176 
177 int const      BUFSIZE                       = 2048;              // Max length (inc. terminating NULL) of any C-type string
178 int const      CLOCKCHKITER                  = 5000;
179 int const      OUTWIDTH                      = 103;               // Width of rh bit of .out file, wrap after this
180 int const      SAVGOLPOLYMAX                 = 6;                 // Maximum order of Savitsky-Golay smoothing polynomial
181 int const      COASTMAX                      = 1000;              // For safety check when tracing coast
182 int const      COASTMIN                      = 2;                 // Ignore very short coasts less than this x profile spacing
183 
184 // TODO does this still work on 64-bit platforms?
185 unsigned long const  MASK                             = 0xfffffffful;
186 
187 double const   PI                                     = 3.141592653589793238462643;
188 
189 double const   WAVEHEIGHT_OVER_WATERDEPTH_AT_BREAKING = 0.78;              // In active zone if wave height / water depth exceeds this
190 double const   TESTDEPTH_DECREMENT                    = 0.05;              // For the wave breaking timestep
191 double const   BEACH_PROTECTION_HB_RATIO              = 0.23;              // The beach protection factor is this times breaking depth
192 double const   WALKDEN_HALL_PARAM_1                   = 3.25;              // First param in Equation 4 from Walkden & Hall, 2005
193 double const   WALKDEN_HALL_PARAM_2                   = 0.75;              // Second param in Equation 4 from Walkden & Hall, 2005
194 
195 double const   DEPTH_OVER_DB_INCREMENT                = 0.001;             // Depth Over DB increment for erosion potential look-up function
196 double const   INVERSE_DEPTH_OVER_DB_INCREMENT        = 1000;              // Inverse of the above
197 
198 // Some rather arbitrary constants
199 int const      ROUND_LOOP_MAX                         = 500000;             // In coastline tracing, give up if round loop more than this
200 int const      MIN_PROFILE_SPACING                    = 20;                // in cells: profile creation does not work well if profiles are too closely spaced
201 double const   TOLERANCE                              = 1e-6;              // For bFPIsEqual, if too small (e.g. 1e-10), get spurious "rounding" errors
202 double const   SEDIMENT_ELEV_TOLERANCE                = 1e-10;             // Throughout, differences in depth-equivalent sediment amount (m) less than this are ignored
203 
204 string const   ERR                                    = "ERROR ";
205 string const   WARN                                   = "WARNING ";
206 
207 int const      INT_NODATA                             = -999;
208 double const   DBL_NODATA                             = -999;
209 
210 
211 // From stdint.h, which is only available using the ISO C++ 2011 standard (would need the -std=c++11 or -std=gnu++11 compiler options)
212 /* Minimum of signed integral types.  */
213 #ifndef INT8_MIN
214 # define INT8_MIN    (-128)
215 # define INT16_MIN      (-32767-1)
216 # define INT32_MIN      (-2147483647-1)
217 # define INT64_MIN      (-__INT64_C(9223372036854775807)-1)
218 /* Maximum of signed integral types.  */
219 # define INT8_MAX    (127)
220 # define INT16_MAX      (32767)
221 # define INT32_MAX      (2147483647)
222 # define INT64_MAX      (__INT64_C(9223372036854775807))
223 
224 /* Maximum of unsigned integral types.  */
225 # define UINT8_MAX      (255)
226 # define UINT16_MAX     (65535)
227 # define UINT32_MAX     (4294967295U)
228 # define UINT64_MAX     (__UINT64_C(18446744073709551615))
229 #endif	// #ifndef INT8_MIN
230 
231 
232 string const   PERITERHEAD1 =
233    "<------ELAPSED-----><-SEA-><------POTENTIAL-----><-----------------ACTUAL----------------><-----POTENTIAL----><---------------ACTUAL----------------><------------BEACH--------------><-----------CLIFF COLLAPSE---------><-SUSP->";
234 
235 string const   PERITERHEAD2 =
236    "        TIME         DEPTH     PLATFORM EROSION                PLATFORM EROSION               BEACH EROSION                BEACH EROSION                        DEPOSITION                    EROSION         DEPOSITION     SED  ";
237 
238 string const   PERITERHEAD3 =
239    " Timestep  Hours   Years    Avg  % Sea    All Eroding  % Sea     All Erosion  <-sea area avg->  % Sea  All Erosion  % Sea   All Eroding  <-sea area avg->  % Sea   All Deposit <-sea avg-><------coast avg-------><-sea avg->";
240 string const   PERITERHEAD4 =
241    "Step                          Area    Sea    Area   Area     Sea    Area  Fine  Sand  Crse   Area  Sea    Area   Area   Sea    Area  Fine  Sand  Crse   Area   Sea    Area  Sand  Crse    Fine    Sand    Crse Sand  Crse";
242 string const   PERITERHEAD5 =
243    "                                      Avg     Avg            Avg     Avg                           Avg     Avg          Avg     Avg                            Avg     Avg";
244 
245 string const   PERITERHEAD =
246    "PER-ITERATION RESULTS ============================================================================================================================================================================================================";
247 string const   ENDHYDROLOGYHEAD =
248    "END OF SIMULATION: HYDROLOGY =====================================================================================================================================================================================================";
249 string const   ENDSEDIMENTHEAD =
250    "END OF SIMULATION: SEDIMENT MOVEMENT =============================================================================================================================================================================================";
251 string const   PERFORMHEAD =
252    "END OF SIMULATION: PERFORMANCE ===================================================================================================================================================================================================";
253 
254 string const   OUTEXT                              = ".out";
255 string const   LOGEXT                              = ".log";
256 string const   CSVEXT                              = ".csv";
257 
258 int const      ORIENTATION_NONE                    = 0;
259 int const      ORIENTATION_NORTH                   = 1;
260 int const      ORIENTATION_NORTH_EAST              = 2;
261 int const      ORIENTATION_EAST                    = 3;
262 int const      ORIENTATION_SOUTH_EAST              = 4;
263 int const      ORIENTATION_SOUTH                   = 5;
264 int const      ORIENTATION_SOUTH_WEST              = 6;
265 int const      ORIENTATION_WEST                    = 7;
266 int const      ORIENTATION_NORTH_WEST              = 8;
267 
268 int const      DIRECTION_DOWNCOAST                 = 0;        // Down-coast, i.e. along the coast so that the index of coastline points INCREASES
269 int const      DIRECTION_UPCOAST                   = 1;        // Up-coast, i.e. along the coast so that the index of coastline points DECREASES
270 
271 // Handedness codes, these show which side the sea is on when travelling down-coast (i.e. in the direction in which coastline point numbers INCREASE)
272 int const      NULL_HANDED                         = -1;
273 int const      RIGHT_HANDED                        = 0;
274 int const      LEFT_HANDED                         = 1;
275 
276 // Sediment texture codes
277 int const      TEXTURE_FINE                        = 0;
278 int const      TEXTURE_SAND                        = 1;
279 int const      TEXTURE_COARSE                      = 2;
280 
281 // Time unit codes
282 int const      TIME_UNKNOWN                        = -1;
283 int const      TIME_HOURS                          = 0;
284 int const      TIME_DAYS                           = 1;
285 int const      TIME_MONTHS                         = 2;
286 int const      TIME_YEARS                          = 3;
287 
288 // Generic landform code
289 int const      LF_NONE                             = 0;
290 
291 // Landform category codes for cells and coast landform objects (see separate doc for full list, to be used eventually)
292 int const      LF_CAT_HINTERLAND                   = 1;
293 int const      LF_CAT_SEA                          = 2;     // Note: no cells actually have this set as a landform category, is used only for output
294 int const      LF_CAT_CLIFF                        = 3;
295 int const      LF_CAT_DRIFT                        = 4;
296 
297 // Landform sub-category codes for cells, LF_CAT_CLIFF
298 int const      LF_SUBCAT_CLIFF_ON_COASTLINE        = 5;
299 int const      LF_SUBCAT_CLIFF_INLAND              = 6;
300 
301 // Landform sub-category codes for cells, for LF_CAT_DRIFT
302 int const      LF_SUBCAT_DRIFT_MIXED               = 7;
303 int const      LF_SUBCAT_DRIFT_TALUS               = 8;
304 int const      LF_SUBCAT_DRIFT_BEACH               = 9;
305 // TODO
306 int const      LF_SUBCAT_DRIFT_DUNES               = 10;
307 
308 // GIS raster input codes
309 int const      FINE_CONS_RASTER                    = 1;
310 int const      SAND_CONS_RASTER                    = 2;
311 int const      COARSE_CONS_RASTER                  = 3;
312 int const      FINE_UNCONS_RASTER                  = 4;
313 int const      SAND_UNCONS_RASTER                  = 5;
314 int const      COARSE_UNCONS_RASTER                = 6;
315 int const      SUSP_SED_RASTER                     = 7;
316 int const      LANDFORM_RASTER                     = 8;
317 int const      INTERVENTION_RASTER                 = 9;
318 int const      BASEMENT_RASTER                     =10;
319 
320 // GIS vector data type codes
321 int const      VEC_FIELD_DATA_ANY                  = 0;
322 int const      VEC_FIELD_DATA_INT                  = 1;
323 int const      VEC_FIELD_DATA_REAL                 = 2;
324 int const      VEC_FIELD_DATA_STRING               = 3;
325 int const      VEC_FIELD_DATA_OTHER                = 4;
326 
327 // GIS vector geometry codes
328 int const      VEC_GEOMETRY_POINT                  = 1;
329 int const      VEC_GEOMETRY_LINE                   = 2;
330 int const      VEC_GEOMETRY_POLYGON                = 3;
331 int const      VEC_GEOMETRY_OTHER                  = 4;
332 
333 // GIS vector input codes and constraints
334 // int const      COAST_VEC                           = 1;              // Initial coastline GIS vector data
335 // int const      COAST_VEC_MAX_LAYER                 = 1;
336 // int const      COAST_VEC_FIELD_DATA_TYPE           = VEC_FIELD_DATA_ANY;
337 // int const      COAST_VEC_GEOMETRY                  = VEC_GEOMETRY_LINE;
338 
339 // GIS raster output user codes
340 string const   RASTER_ALL_CODE                                       = "all";
341 string const   RASTER_BASEMENT_ELEVATION_NAME                        = "basement_elevation";
342 string const   RASTER_SEDIMENT_TOP_NAME                              = "sediment_top_elevation";
343 string const   RASTER_TOP_NAME                                       = "top_elevation";
344 string const   RASTER_LOCAL_SLOPE_NAME                               = "local_cons_sediment_slope";
345 string const   RASTER_SEA_DEPTH_NAME                                 = "sea_depth";
346 string const   RASTER_AVG_SEA_DEPTH_NAME                             = "avg_sea_depth";
347 string const   RASTER_INUNDATION_MASK_NAME                           = "inundation_mask";
348 string const   RASTER_WAVE_HEIGHT_NAME                               = "wave_height";
349 string const   RASTER_AVG_WAVE_HEIGHT_NAME                           = "avg_wave_height";
350 string const   RASTER_BEACH_MASK_NAME                                = "beach_mask";
351 string const   RASTER_BEACH_PROTECTION_NAME                          = "beach_protection";
352 string const   RASTER_POTENTIAL_PLATFORM_EROSION_MASK_NAME           = "potential_platform_erosion_mask";
353 string const   RASTER_POTENTIAL_PLATFORM_EROSION_NAME                = "potential_platform_erosion";
354 string const   RASTER_ACTUAL_PLATFORM_EROSION_NAME                   = "actual_platform_erosion";
355 string const   RASTER_TOTAL_POTENTIAL_PLATFORM_EROSION_NAME          = "total_potential_platform_erosion";
356 string const   RASTER_TOTAL_ACTUAL_PLATFORM_EROSION_NAME             = "total_actual_platform_erosion";
357 string const   RASTER_POTENTIAL_BEACH_EROSION_NAME                   = "potential_beach_erosion";
358 string const   RASTER_ACTUAL_BEACH_EROSION_NAME                      = "actual_beach_erosion";
359 string const   RASTER_TOTAL_POTENTIAL_BEACH_EROSION_NAME             = "total_potential_beach_erosion";
360 string const   RASTER_TOTAL_ACTUAL_BEACH_EROSION_NAME                = "total_actual_beach_erosion";
361 string const   RASTER_BEACH_DEPOSITION_NAME                          = "beach_deposition";
362 string const   RASTER_TOTAL_BEACH_DEPOSITION_NAME                    = "total_beach_deposition";
363 string const   RASTER_LANDFORM_NAME                                  = "landform_class";
364 string const   RASTER_INTERVENTION_NAME                              = "intervention_class";
365 string const   RASTER_SUSP_SED_NAME                                  = "susp_sed";
366 string const   RASTER_AVG_SUSP_SED_NAME                              = "avg_susp_sed";
367 string const   RASTER_FINE_UNCONS_NAME                               = "uncons_sed_fine";
368 string const   RASTER_SAND_UNCONS_NAME                               = "uncons_sed_sand";
369 string const   RASTER_COARSE_UNCONS_NAME                             = "uncons_sed_coarse";
370 string const   RASTER_FINE_CONS_NAME                                 = "cons_sed_fine";
371 string const   RASTER_SAND_CONS_NAME                                 = "cons_sed_sand";
372 string const   RASTER_COARSE_CONS_NAME                               = "cons_sed_coarse";
373 string const   RASTER_COAST_NAME                                     = "rcoast";
374 string const   RASTER_COAST_NORMAL_NAME                              = "rcoast_normal";
375 string const   RASTER_ACTIVE_ZONE_NAME                               = "active_zone";
376 string const   RASTER_CLIFF_COLLAPSE_NAME                            = "cliff_collapse";
377 string const   RASTER_TOTAL_CLIFF_COLLAPSE_NAME                      = "total_cliff_collapse";
378 string const   RASTER_CLIFF_COLLAPSE_DEPOSITION_NAME                 = "cliff_collapse_deposition";
379 string const   RASTER_TOTAL_CLIFF_COLLAPSE_DEPOSITION_NAME           = "total_cliff_collapse_deposition";
380 string const   RASTER_POLYGON_NAME                                   = "polygon_raster";
381 string const   RASTER_SLICE_NAME                                     = "slice";
382 
383 // GIS raster output codes and titles
384 int const      PLOT_BASEMENT_ELEV                           = 1;
385 string const   PLOT_BASEMENT_ELEV_TITLE                     = "Basement elevation";
386 int const      PLOT_SEDIMENT_TOP_ELEV                       = 2;
387 string const   PLOT_SEDIMENT_TOP_ELEV_TITLE                 = "Elevation of sediment top";
388 int const      PLOT_INUNDATION_MASK                         = 7;
389 string const   PLOT_INUNDATION_MASK_TITLE                   = "Inundated area mask";
390 int const      PLOT_SLICE                                   = 28;
391 string const   PLOT_SLICE_TITLE                             = "Slice though layers at elevation = ";
392 int const      PLOT_RASTER_COAST                            = 29;
393 string const   PLOT_RASTER_COAST_TITLE                      = "Rasterized coastline";
394 int const      PLOT_RASTER_NORMAL                           = 30;
395 string const   PLOT_RASTER_NORMAL_TITLE                     = "Rasterized normals to coastline";
396 
397 // GIS vector output user codes
398 string const   VECTOR_ALL_CODE                              = "all";
399 string const   VECTOR_COAST_CODE                            = "coast";
400 string const   VECTOR_COAST_NAME                            = "coast";
401 string const   VECTOR_NORMALS_CODE                          = "normals";
402 string const   VECTOR_NORMALS_NAME                          = "normals";
403 string const   VECTOR_INVALID_NORMALS_CODE                  = "invalid_normals";
404 string const   VECTOR_INVALID_NORMALS_NAME                  = "invalid_normals";
405 string const   VECTOR_COAST_CURVATURE_CODE                  = "coast_curvature";
406 string const   VECTOR_COAST_CURVATURE_NAME                  = "coast_curvature";
407 string const   VECTOR_CLIFF_TOP_CODE                        = "cliff_top";
408 string const   VECTOR_CLIFF_TOP_NAME                        = "cliff_top";
409 string const   VECTOR_CLIFF_TOE_CODE                        = "cliff_toe";
410 string const   VECTOR_CLIFF_TOE_NAME                        = "cliff_toe";
411 string const   VECTOR_COAST_POINT_NAME                      = "coast_point";
412 
413 // GIS vector output codes and titles
414 int const      PLOT_COAST                                   = 1;
415 string const   PLOT_COAST_TITLE                             = "Coastline";
416 int const      PLOT_NORMALS                                 = 2;
417 string const   PLOT_NORMALS_TITLE                           = "Coastline-normal profiles";
418 int const      PLOT_INVALID_NORMALS                         = 3;
419 string const   PLOT_INVALID_NORMALS_TITLE                   = "INVALID Coastline-normal profiles";
420 int const      PLOT_COAST_CURVATURE                         = 4;
421 string const   PLOT_COAST_CURVATURE_TITLE                   = "Coastline curvature";
422 int const      PLOT_CLIFF_TOP                               = 13;
423 string const   PLOT_CLIFF_TOP_TITLE                         = "Cliff top points";
424 int const      PLOT_CLIFF_TOE                               = 14;
425 string const   PLOT_CLIFF_TOE_TITLE                         = "Cliff toe points";
426 int const      PLOT_COAST_POINT                             = 15;
427 string const   PLOT_COAST_POINT_TITLE                       = "Coast points";
428 
429 
430 // Return codes
431 int const      RTN_OK                                 = 0;
432 int const      RTN_HELPONLY                           = 1;
433 int const      RTN_CHECKONLY                          = 2;
434 int const      RTN_USERABORT                          = 3;
435 int const      RTN_ERR_BADPARAM                       = 4;
436 int const      RTN_ERR_INI                            = 5;
437 int const      RTN_ERR_CLIFFDIR                         = 6;
438 int const      RTN_ERR_RUNDATA                        = 7;
439 int const      RTN_ERR_SHAPEFUNCTIONFILE              = 8;
440 int const      RTN_ERR_TIDEDATAFILE                   = 9;
441 int const      RTN_ERR_LOGFILE                        = 10;
442 int const      RTN_ERR_OUTFILE                        = 11;
443 int const      RTN_ERR_TSFILE                         = 12;
444 int const      RTN_ERR_DEMFILE                        = 13;
445 int const      RTN_ERR_RASTER_FILE_READ               = 14;
446 int const      RTN_ERR_VECTOR_FILE_READ               = 15;
447 int const      RTN_ERR_MEMALLOC                       = 16;
448 int const      RTN_ERR_RASTER_GIS_OUT_FORMAT          = 17;
449 int const      RTN_ERR_VECTOR_GIS_OUT_FORMAT          = 18;
450 int const      RTN_ERR_TEXT_FILE_WRITE                = 19;
451 int const      RTN_ERR_RASTER_FILE_WRITE              = 20;
452 int const      RTN_ERR_VECTOR_FILE_WRITE              = 21;
453 int const      RTN_ERR_TIMESERIES_FILE_WRITE          = 22;
454 int const      RTN_ERR_LINETOGRID                     = 23;
455 int const      RTN_ERR_PROFILESPACING                 = 24;
456 int const      RTN_ERR_OFFGRID_ENDPOINT               = 25;
457 int const      RTN_ERR_BADENDPOINT                    = 26;
458 int const      RTN_ERR_BADPROFILE                     = 27;
459 int const      RTN_ERR_NOSEACELLS                     = 28;
460 int const      RTN_ERR_GRIDTOLINE                     = 29;
461 int const      RTN_ERR_FINDCOAST                      = 30;
462 int const      RTN_ERR_MASSBALANCE                    = 31;
463 int const      RTN_ERR_PROFILEWRITE                   = 32;
464 int const      RTN_ERR_TIMEUNITS                      = 33;
465 int const      RTN_ERR_CLIFFNOTCH                     = 34;
466 int const      RTN_ERR_CLIFFDEPOSIT                   = 35;
467 int const      RTN_ERR_BAD_INDEX                      = 36;
468 int const      RTN_ERR_EDGEOFGRID                     = 37;
469 int const      RTN_ERR_BAD_BEACH_EROSION_PROFILE      = 38;
470 // int const      RTN_ERR_BAD_BEACH_DEPOSITION_PROFILE   = 39;
471 int const      RTN_ERR_LANDFORM_TO_GRID               = 40;
472 int const      RTN_ERR_NO_TOP_LAYER                   = 41;
473 int const      RTN_ERR_NO_ADJACENT_POLYGON            = 42;
474 int const      RTN_ERR_BAD_MULTILINE                  = 43;
475 int const      RTN_ERR_CANNOT_INSERT_POINT            = 44;
476 int const      RTN_ERR_CANNOT_ASSIGN_COASTAL_LANDFORM = 45;
477 
478 // Elevation and 'slice' codes
479 int const      ELEV_IN_BASEMENT                   = -1;
480 int const      ELEV_ABOVE_SEDIMENT_TOP            = 9999;
481 int const      NO_NONZERO_THICKNESS_LAYERS        = -1;
482 
483 // Vector smoothing codes
484 int const      SMOOTH_NONE                         = 0;
485 int const      SMOOTH_RUNNING_MEAN                 = 1;
486 int const      SMOOTH_SAVITZKY_GOLAY               = 2;
487 
488 // Grid-edge boundary treatment for unconsolidated sediment movement
489 int const      GRID_EDGE_CLOSED                    = 0;
490 int const      GRID_EDGE_OPEN                      = 1;
491 int const      GRID_EDGE_MOBIUS                    = 2;
492 
493 // Equation for estimating erosion of unconsolidated sediment
494 int const      EQUATION_CERC                       = 0;
495 int const      EQUATION_KAMPHUIS                   = 1;
496 
497 
498 //================================================ Globally-available functions =================================================
tMax(T a,T b)499 template <class T> T tMax(T a, T b)
500 {
501    return ((a > b) ? a : b);
502 }
503 
tMax(T a,T b,T c)504 template <class T> T tMax(T a, T b, T c)
505 {
506    T max = (a < b ) ? b : a;
507    return (( max < c ) ? c : max);
508 }
509 
tMin(T a,T b)510 template <class T> T tMin(T a, T b)
511 {
512    return ((a < b) ? a : b);
513 }
514 
tMin(T a,T b,T c)515 template <class T> T tMin(T a, T b, T c)
516 {
517    return (a < b ? (a < c ? a : c) : (b < c ? b : c));
518 }
519 
tAbs(T a)520 template <class T> T tAbs(T a)
521 {
522    // From a posting dated 18 Nov 93 by rmartin@rcmcon.com (Robert Martin), archived in cpp_tips
523    return ((a < 0) ? -a : a);
524 }
525 
tToStr(const T & t)526 template <typename T> string tToStr(const T& t)
527 {
528    // From http://stackoverflow.com/questions/2125880/convert-float-to-stdstring-in-c
529    std::ostringstream os;
530    os << t;
531    return os.str();
532 }
533 
534 // Definitions are in utilsglobal.cpp
535 bool bFPIsEqual(double const, double const, double const);
536 // bool bIsWhole(double const);
537 bool bIsNumber(double const);
538 // bool bIsFinite(double const);
539 extern double dRound(double const);
540 extern double dCrossProduct(double const, double const, double const, double const, double const, double const);
541 extern double dGetMean(vector<double>* const);
542 extern double dGetStdDev(vector<double>* const);
543 extern string strTrim(string*);
544 extern string strTrimLeft(string const*);
545 extern string strTrimRight(string const*);
546 extern string strToLower(string*);
547 // extern string strToUpper(string*);
548 extern string strRemoveSubstr(string*, string const*);
549 extern vector<string>* strSplit(string const*, char const, vector<string>*);
550 extern vector<string> strSplit(string const*, char const);
551 extern string pstrChangeToBackslash(string const*);
552 extern string pstrChangeToForwardSlash(string const*);
553 extern string NumberToString(int const);
554 
555 // Some public domain utility routines, definitions are in utilsglobal.cpp
556 extern "C"
557 {
558    void* MoveStr(char* dest, char* const source);
559 //    char* pszToLower(char* string);
560 //    char* pszToUpper(char* string);
561    char* pszTrimLeft(char* string);
562 //    char* pszTrimRight(char* string);
563    char* pszLongToSz(long num, char* string, int max_chars, int base = 10);
564 
565    // And one of my own
566 //    char* pszRemoveSubstr(char* string, char* substr, char* subpos);
567 }
568 
569 //================================================= debugging stuff =============================================================
570 //#define CLOCKCHECK          // Uncomment to check CPU clock rollover settings
571 //#define RANDCHECK           // Uncomment to check randomness of random number generator
572 
573 #endif // CLIFFMETRICS_H
574