/*-------------------------------------------------------------------- * The MB-system: mblgpl_fbt.h 9/13/2004 * $Id: $ * * Copyright (c) 2004-2014 by * David W. Caress (caress@mbari.org) * Monterey Bay Aquarium Research Institute * Moss Landing, CA 95039 * and Dale N. Chayes (dale@ldeo.columbia.edu) * Lamont-Doherty Earth Observatory * Palisades, NY 10964 * * This file contains source code modified from source files * distributed as part of MB-System. In contrast to the GPL-licensed * MB-System distributions, the code contained here is licensed * under the Lesser GNU Public license, or LGPL. This means that * compiled versions of these functions, or functions derived from * modifications to this code, may be linked with commercial * applications without imposing any restriction on the sale and * and distribution of those applications. * * The full LGPL text may be accessed at http://www.gnu.org/ * * The release of this code under the LGPL does not change the * the GPL licensing of any code contained in the regular * MB-System distribution. * * The source code provided here does not come with any warranties, * nor is it guarenteed to work on your computer or to do * anything useful. The user assumes full responsibility for * the use of this software. In particular, David W. Caress, * Dale N. Chayes, the Lamont-Doherty Earth Observatory of * Columbia University, the Monterey Bay Aquarium Research Institute, * or any other individuals or organizations involved in the * design and maintenance of the MB-System software package * and derived distributions are NOT responsible for any * damage that may follow from correct or incorrect use of this * source code. Moreover, it should be noted that the source * code provided here has NOT been tested after modification * from the related functions that are part of MB-System. * * To access MB-System documentation or source code releases see: * http://www.mbari.org/data/mbsystem * http://www.ldeo.columbia.edu/MB-System * *--------------------------------------------------------------------*/ /* * Author: D. W. Caress * Originated: September 13, 2004 * Revised: May 16, 2012 * Revised: July 25, 2014 * Revised: November 30, 2014 * * $Log: $ * *--------------------------------------------------------------------*/ /* * This header file mblgpl_fbt.h includes structure and macro definitions * required for the source provided in the file mblgpl_fbt.c to compile. * */ /* include this code only once */ #ifndef MBLGPL_HEADER_DEF #define MBLGPL_HEADER_DEF /* MBLGPL function boolean convention */ #define MBLGPL_YES 1 #define MBLGPL_NO 0 /* MBLGPL function status convention */ #define MBLGPL_SUCCESS 1 #define MBLGPL_FAILURE 0 /* MBLGPL function fatal error values */ #define MBLGPL_ERROR_NO_ERROR 0 #define MBLGPL_ERROR_MEMORY_FAIL 1 #define MBLGPL_ERROR_EOF 4 #define MBLGPL_ERROR_BAD_USAGE 9 #define MBLGPL_ERROR_UNINTELLIGIBLE -8 /* MBLGPL data type ("kind") convention */ #define MBLGPL_DATA_NONE 0 #define MBLGPL_DATA_DATA 1 /* general survey data */ #define MBLGPL_DATA_COMMENT 2 /* general comment */ /* MBLGPL imagery source convention */ #define MBLGPL_IMAGERY_TYPE_UNKNOWN 0 #define MBLGPL_IMAGERY_TYPE_ECHOSOUNDER 1 #define MBLGPL_IMAGERY_TYPE_MULTIBEAM 2 #define MBLGPL_IMAGERY_TYPE_SIDESCAN 3 #define MBLGPL_IMAGERY_TYPE_INTERFEROMETRIC 4 #define MBLGPL_IMAGERY_TYPE_LIDAR 5 #define MBLGPL_IMAGERY_TYPE_CAMERA 6 #define MBLGPL_IMAGERY_TYPE_GRID 7 #define MBLGPL_IMAGERY_TYPE_POINT 8 /* MBLGPL topography source convention */ #define MBLGPL_TOPOGRAPHY_TYPE_UNKNOWN 0 #define MBLGPL_TOPOGRAPHY_TYPE_ECHOSOUNDER 1 #define MBLGPL_TOPOGRAPHY_TYPE_MULTIBEAM 2 #define MBLGPL_TOPOGRAPHY_TYPE_SIDESCAN 3 #define MBLGPL_TOPOGRAPHY_TYPE_INTERFEROMETRIC 4 #define MBLGPL_TOPOGRAPHY_TYPE_LIDAR 5 #define MBLGPL_TOPOGRAPHY_TYPE_CAMERA 6 #define MBLGPL_TOPOGRAPHY_TYPE_GRID 7 #define MBLGPL_TOPOGRAPHY_TYPE_POINT 8 /* define record id tags */ #define MBLGPL_FBT_ID_COMMENT1 8995 /* ## */ #define MBLGPL_FBT_ID_COMMENT2 25443 /* cc */ #define MBLGPL_FBT_ID_V1DATA 25700 /* dd */ #define MBLGPL_FBT_ID_V2DATA 28270 /* nn */ #define MBLGPL_FBT_ID_V3DATA 17476 /* DD */ #define MBLGPL_FBT_ID_V4DATA 22068 /* V4 big endian, 13398 little endian*/ #define MBLGPL_FBT_ID_V5DATA 22069 /* V5 bin endian, 13654 little endian */ /* define some important sizes */ #define MBLGPL_FBT_V1HEADERSIZE 38 #define MBLGPL_FBT_V2HEADERSIZE 44 #define MBLGPL_FBT_V3HEADERSIZE 48 #define MBLGPL_FBT_V4HEADERSIZE 90 #define MBLGPL_FBT_V5HEADERSIZE 98 #define MBLGPL_FBT_COMMENTSIZE 200 /* type definitions of signed and unsigned char */ typedef unsigned char mblgpl_u_char; /* From stdint.h if available */ #if defined INT8_MAX || defined int8_t typedef int8_t mblgpl_s_char; #else typedef signed char mblgpl_s_char; #endif /* type definitions of signed and unsigned long int (64 bit integer) */ typedef unsigned long long mblgpl_u_long; typedef long long mblgpl_s_long; /* define byte swapping macros */ #define mblgpl_swap_short(a) ( ((a & 0xff) << 8) | ((unsigned short)(a) >> 8) ) #define mblgpl_swap_int(a) ( ((a) << 24) | \ (((a) << 8) & 0x00ff0000) | \ (((a) >> 8) & 0x0000ff00) | \ ((unsigned int)(a) >>24) ) /* declare PI if needed */ #ifndef M_PI #define M_PI 3.14159265358979323846 #endif /* the natural log of 2 is always useful */ #define MB_LN_2 0.69314718056 /* multiply this by degrees to get radians */ #define DTR 0.01745329251994329500 /* multiply this by radians to get degrees */ #define RTD 57.2957795130823230000 /* min max round define */ #ifndef MIN #define MIN(A, B) ((A) < (B) ? (A) : (B)) #endif #ifndef MAX #define MAX(A, B) ((A) > (B) ? (A) : (B)) #endif #ifndef ROUND #define ROUND(X) X < 0.0 ? ceil(X - 0.5) : floor(X + 0.5) #endif /* MBLGPL sidescan types - sidescan values can be logarithmic (dB) or linear (usually voltage) */ #define MBLGPL_SIDESCAN_LOGARITHMIC 0 #define MBLGPL_SIDESCAN_LINEAR 1 /* MBIO null sidescan: - value used to flag sidescan values as undefined */ #define MBLGPL_SIDESCAN_NULL -1000000000.0 /* MBIO unknown time flag: - time_d value used to flag unknown time tag - e.g. for xyz soundings */ #define MBLGPL_TIME_D_UNKNOWN -2209075200.000000 /* * The following defines the values used to flag or * select individual bathymetry values (soundings). This scheme * is very similar to the convention used in the HMPS * hydrographic data processing package and the SAIC Hydrobat * package. The values passed in MBIO functions are single * byte characters. * * Macros used to identify the flags are also defined here. * * The flagging scheme is as follows: * * Beams cannot be both flagged and selected. However, more than * one "reason bit" can be set for either flagging or selection. * * The flag and select bits: * xxxxxx00 => This beam is neither flagged nor selected. * xxxxxx01 => This beam is flagged as bad and should be ignored. * xxxxxx10 => This beam has been selected. * * Flagging modes: * 00000001 => Flagged because no detection was made by the sonar. * xxxxx101 => Flagged by manual editing. * xxxx1x01 => Flagged by automatic filter. * xxx1xx01 => Flagged because uncertainty exceeds 1 X IHO standard. * xx1xxx01 => Flagged because uncertainty exceeds 2 X IHO standard. * x1xxxx01 => Flagged because footprint is too large * 1xxxxx01 => Flagged by sonar as unreliable. * * Selection modes: * 00000010 => Selected, no reason specified. * xxxxx110 => Selected as least depth. * xxxx1x10 => Selected as average depth. * xxx1xx10 => Selected as maximum depth. * xx1xxx10 => Selected as location of sidescan contact. * x1xxxx10 => Selected, spare. * 1xxxxx10 => Selected, spare. * */ /* Definitions for FLAG category */ #define MBLGPL_FLAG_NONE 0x00 #define MBLGPL_FLAG_FLAG 0x01 #define MBLGPL_FLAG_NULL 0x01 #define MBLGPL_FLAG_MANUAL 0x04 #define MBLGPL_FLAG_FILTER 0x08 #define MBLGPL_FLAG_FILTER2 0x10 #define MBLGPL_FLAG_GT_1X_IHO 0x10 #define MBLGPL_FLAG_GT_2X_IHO 0x20 #define MBLGPL_FLAG_FOOTPRINT 0x40 #define MBLGPL_FLAG_SONAR 0x80 /* Definitions for the SELECT category */ #define MBLGPL_SELECT_SELECT 0x02 #define MBLGPL_SELECT_LEAST 0x04 #define MBLGPL_SELECT_MAXIMUM 0x08 #define MBLGPL_SELECT_AVERAGE 0x10 #define MBLGPL_SELECT_CONTACT 0x20 #define MBLGPL_SELECT_SPARE_1 0x40 #define MBLGPL_SELECT_SPARE_2 0x80 /* Definitions for macros applying and testing flags */ #define mblgpl_beam_ok(F) ((int)(!(F & MBLGPL_FLAG_FLAG))) #define mblgpl_beam_check_flag(F) ((int)(F & MBLGPL_FLAG_FLAG)) #define mblgpl_beam_check_flag_null(F) ((int)(F == MBLGPL_FLAG_NULL)) #define mblgpl_beam_check_flag_manual(F) ((int)((F & MBLGPL_FLAG_MANUAL ) && (F & MBLGPL_FLAG_FLAG))) #define mblgpl_beam_check_flag_filter(F) ((int)((F & MBLGPL_FLAG_FILTER ) && (F & MBLGPL_FLAG_FLAG))) #define mblgpl_beam_check_flag_filter2(F) ((int)((F & MBLGPL_FLAG_FILTER2 ) && (F & MBLGPL_FLAG_FLAG))) #define mblgpl_beam_check_flag_gt_1x_iho(F) ((int)((F & MBLGPL_FLAG_GT_1X_IHO) && (F & MBLGPL_FLAG_FLAG))) #define mblgpl_beam_check_flag_gt_2x_iho(F) ((int)((F & MBLGPL_FLAG_GT_2X_IHO) && (F & MBLGPL_FLAG_FLAG))) #define mblgpl_beam_check_flag_footprint(F) ((int)((F & MBLGPL_FLAG_FOOTPRINT) && (F & MBLGPL_FLAG_FLAG))) #define mblgpl_beam_check_flag_sonar(F) ((int)((F & MBLGPL_FLAG_SONAR ) && (F & MBLGPL_FLAG_FLAG))) /* ellipsoid coefficients from World Geodetic System Ellipsoid of 1972 * - see Bowditch (H.O. 9 -- American Practical Navigator). */ #define C1 111412.84 #define C2 -93.5 #define C3 0.118 #define C4 111132.92 #define C5 -559.82 #define C6 1.175 #define C7 0.0023 /*--------------------------------------------------------------------*/ /* data storage structure for fbt swath bathymetry data */ struct mblgpl_fbt_struct { /* type of data record */ int kind; /* time stamp */ double time_d; /* decimal seconds since start of 1970 */ /* position */ double longitude; /* longitude (degrees 0-360) */ double latitude; /* latitude (degrees 0-360) */ /* sonar depth and altitude */ double sonardepth; /* meters (sonar depth for bathymetry calculation, already corrected for heave if needed, sonardepth = transducer_depth bath = altitude + sonardepth sonardepth = draft - heave draft = sonardepth + heave */ double altitude; /* meters */ /* heading and speed */ float heading; /* heading (degrees 0-360) */ float speed; /* km/hour */ /* attitude */ float roll; /* degrees */ float pitch; /* degrees */ float heave; /* meters */ /* beam widths */ float beam_xwidth; /* degrees */ float beam_lwidth; /* degrees */ /* numbers of beams */ int beams_bath; /* number of depth values */ int beams_amp; /* number of amplitude values */ int pixels_ss; /* number of sidescan pixels */ int spare1; int beams_bath_alloc; /* number of depth values allocated */ int beams_amp_alloc; /* number of amplitude values allocated */ int pixels_ss_alloc; /* number of sidescan pixels allocated */ /* scaling */ float depth_scale; /* depth[i] = (bath[i] * depth_scale) + transducer_depth */ float distance_scale; /* acrosstrackdistance[i] = acrosstrack[i] * distance_scale alongtrackdistance[i] = alongtrack[i] * distance_scale */ /* sidescan type */ mblgpl_s_char ss_scalepower; /* gives scaling factor for sidescan values in powers of 10: * ss_scalepower = 0: ss = ss_stored * 1 * ss_scalepower = 1: ss = ss_stored * 10 * ss_scalepower = 2: ss = ss_stored * 100 * ss_scalepower = 3: ss = ss_stored * 1000 */ mblgpl_u_char ss_type; /* indicates if sidescan values are logarithmic or linear ss_type = 0: logarithmic (dB) ss_type = 1: linear (voltage) */ mblgpl_u_char imagery_type; /* imagery source types defined as follows: MBLGPL_IMAGERY_TYPE_UNKNOWN 0 MBLGPL_IMAGERY_TYPE_ECHOSOUNDER 1 MBLGPL_IMAGERY_TYPE_MULTIBEAM 2 MBLGPL_IMAGERY_TYPE_SIDESCAN 3 MBLGPL_IMAGERY_TYPE_INTERFEROMETRIC 4 MBLGPL_IMAGERY_TYPE_LIDAR 5 MBLGPL_IMAGERY_TYPE_CAMERA 6 MBLGPL_IMAGERY_TYPE_GRID 7 MBLGPL_IMAGERY_TYPE_POINT 8 */ mblgpl_u_char topo_type; /* topography source types defined as follows: MBLGPL_TOPOGRAPHY_TYPE_UNKNOWN 0 MBLGPL_TOPOGRAPHY_TYPE_ECHOSOUNDER 1 MBLGPL_TOPOGRAPHY_TYPE_MULTIBEAM 2 MBLGPL_TOPOGRAPHY_TYPE_SIDESCAN 3 MBLGPL_TOPOGRAPHY_TYPE_INTERFEROMETRIC 4 MBLGPL_TOPOGRAPHY_TYPE_LIDAR 5 MBLGPL_TOPOGRAPHY_TYPE_CAMERA 6 MBLGPL_TOPOGRAPHY_TYPE_GRID 7 MBLGPL_TOPOGRAPHY_TYPE_POINT 8 */ /* pointers to arrays */ unsigned char *beamflag; short *bath; short *amp; short *bath_acrosstrack; short *bath_alongtrack; short *ss; short *ss_acrosstrack; short *ss_alongtrack; /* comment */ char comment[MBLGPL_FBT_COMMENTSIZE]; }; /*--------------------------------------------------------------------*/ struct mblgpl_oldfbt_struct { /* type of data record */ int kind; /* time stamp */ short year; /* year (4 digits) */ short day; /* julian day (1-366) */ short min; /* minutes from beginning of day (0-1439) */ short sec; /* seconds from beginning of minute (0-59) */ short msec; /* milliseconds from beginning of minute (0-59) */ /* position */ unsigned short lon2u; /* minutes east of prime meridian */ unsigned short lon2b; /* fraction of minute times 10000 */ unsigned short lat2u; /* number of minutes north of 90S */ unsigned short lat2b; /* fraction of minute times 10000 */ /* heading and speed */ unsigned short heading;/* heading: 0 = 0 degrees 1 = 0.0055 degrees 16384 = 90 degrees 65535 = 359.9945 degrees 0 = 360 degrees */ unsigned short speed; /* km/s X100 */ /* numbers of beams */ short beams_bath; /* number of depth values */ short beams_amp; /* number of amplitude values */ short pixels_ss; /* number of sidescan pixels */ short beams_bath_alloc; /* number of depth values allocated */ short beams_amp_alloc; /* number of amplitude values allocated */ short pixels_ss_alloc; /* number of sidescan pixels allocated */ /* scaling */ short depth_scale; /* 1000*scale where depth = bath*scale + transducer_depth / 1000 */ short distance_scale; /* 1000*scale where distance = dist*scale */ int transducer_depth; /* 0.001 m */ int altitude; /* 0.001 m */ short beam_xwidth; /* 0.01 degrees */ short beam_lwidth; /* 0.01 degrees */ short ss_type; /* indicates if sidescan values are logarithmic or linear ss_type = 0: logarithmic (dB) ss_type = 1: linear (voltage) */ /* pointers to arrays */ unsigned char *beamflag; short *bath; short *amp; short *bath_acrosstrack; short *bath_alongtrack; short *ss; short *ss_acrosstrack; short *ss_alongtrack; /* comment */ char comment[MBLGPL_FBT_COMMENTSIZE]; }; /*--------------------------------------------------------------------*/ int mblgpl_fbt_alloc(int verbose, struct mblgpl_fbt_struct **store_ptr, int *error); int mblgpl_fbt_deall(int verbose, struct mblgpl_fbt_struct **store_ptr, int *error); int mblgpl_read_fbt(int verbose, FILE *fp, struct mblgpl_fbt_struct *store, int *error); int mblgpl_get_time(int verbose, int time_i[7], double *time_d); int mblgpl_get_date(int verbose, double time_d, int time_i[7]); int mblgpl_get_date_string(int verbose, double time_d, char *string); int mblgpl_get_jtime(int verbose, int time_i[7], int time_j[5]); int mblgpl_get_itime(int verbose, int time_j[5], int time_i[7]); int mblgpl_get_binary_short(int swapped, void *buffer, void *ptr); int mblgpl_get_binary_int(int swapped, void *buffer, void *ptr); int mblgpl_get_binary_float(int swapped, void *buffer, void *ptr); int mblgpl_get_binary_double(int swapped, void *buffer, void *ptr); int mblgpl_get_binary_long(int swapped, void *buffer, void *ptr); int mblgpl_put_binary_short(int swapped, short value, void *buffer); int mblgpl_put_binary_int(int swapped, int value, void *buffer); int mblgpl_put_binary_float(int swapped, float value, void *buffer); int mblgpl_put_binary_double(int swapped, double value, void *buffer); int mblgpl_put_binary_long(int swapped, mblgpl_s_long value, void *buffer); int mblgpl_swap_float(float *a); int mblgpl_swap_double(double *a); int mblgpl_swap_long(mblgpl_s_long *a); int mblgpl_malloc(int verbose, size_t size, void **ptr, int *error); int mblgpl_free(int verbose, void **ptr, int *error); int mblgpl_coor_scale(int verbose, double latitude, double *mtodeglon, double *mtodeglat); /*--------------------------------------------------------------------*/ #endif