/*-------------------------------------------------------------------- * The MB-system: mblgpl_fbt.c 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: $ * *--------------------------------------------------------------------*/ /* * The header file mblgpl_fbt.h contains structure and macro definitions * required for this source to compile. * * The function mblgpl_read_fbt() is intended to provide an * an example template for reading swath bathymetry stored in * "fbt" files as part of the MB-System processing environment. * * An application using this function must have already opened the * the fbt file (ergo the FILE pointer fp) and allocated the data * storage structure referenced by store using mblgpl_fbt_alloc(). * * */ /* standard include files */ #include #include #include #include /* mblgpl include file */ #include "mblgpl_fbt.h" /* time conversion constants and variables */ #define SECINYEAR 31536000.0 #define SECINDAY 86400.0 #define SECINHOUR 3600.0 #define SECINMINUTE 60.0 #define IMININHOUR 60 int yday[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; static char version_id[]="$Id: $"; /*--------------------------------------------------------------------*/ int mblgpl_fbt_alloc(int verbose, struct mblgpl_fbt_struct **store_ptr, int *error) { char *function_name = "mblgpl_fbt_alloc"; int status = MBLGPL_SUCCESS; struct mblgpl_fbt_struct *store; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBLGPL function <%s> called\n", function_name); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 store_ptr: %p\n",store_ptr); } /* allocate memory for data structure */ status = mblgpl_malloc(verbose,sizeof(struct mblgpl_fbt_struct), (void **) store_ptr,error); /* get store */ store = *store_ptr; /* initialize values in structure */ store->kind = MBLGPL_DATA_NONE; store->time_d = 0.0; store->longitude = 0.0; store->latitude = 0.0; store->sonardepth = 0.0; store->altitude = 0.0; store->heading = 0.0; store->speed = 0.0; store->roll = 0.0; store->pitch = 0.0; store->heave = 0.0; store->beam_xwidth = 0.0; store->beam_lwidth = 0.0; store->beams_bath = 0; store->beams_amp = 0; store->pixels_ss = 0; store->spare1 = 0; store->beams_bath_alloc = 0; store->beams_amp_alloc = 0; store->pixels_ss_alloc = 0; store->depth_scale = 0.0; store->distance_scale = 0.0; store->ss_scalepower = 0; store->ss_type = 0; store->imagery_type = MBLGPL_IMAGERY_TYPE_UNKNOWN; store->topo_type = MBLGPL_TOPOGRAPHY_TYPE_UNKNOWN; store->beamflag = NULL; store->bath = NULL; store->amp = NULL; store->bath_acrosstrack = NULL; store->bath_alongtrack = NULL; store->ss = NULL; store->ss_acrosstrack = NULL; store->ss_alongtrack = NULL; memset(store->comment, 0, MBLGPL_FBT_COMMENTSIZE); /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBLGPL function <%s> completed\n", function_name); fprintf(stderr,"dbg2 Return values:\n"); fprintf(stderr,"dbg2 *store_ptr: %p\n",*store_ptr); fprintf(stderr,"dbg2 error: %d\n",*error); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return status */ return(status); } /*--------------------------------------------------------------------*/ int mblgpl_fbt_deall(int verbose, struct mblgpl_fbt_struct **store_ptr, int *error) { char *function_name = "mblgpl_fbt_deall"; int status = MBLGPL_SUCCESS; struct mblgpl_fbt_struct *store; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBLGPL function <%s> called\n", function_name); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 *store_ptr: %p\n",*store_ptr); } /* get store */ store = *store_ptr; /* deallocate memory for data structures */ status = mblgpl_free(verbose,(void **)&store->beamflag,error); status = mblgpl_free(verbose,(void **)&store->bath,error); status = mblgpl_free(verbose,(void **)&store->bath_acrosstrack,error); status = mblgpl_free(verbose,(void **)&store->bath_alongtrack,error); status = mblgpl_free(verbose,(void **)&store->amp,error); status = mblgpl_free(verbose,(void **)&store->ss,error); status = mblgpl_free(verbose,(void **)&store->ss_acrosstrack,error); status = mblgpl_free(verbose,(void **)&store->ss_alongtrack,error); /* deallocate memory for data structure */ status = mblgpl_free(verbose,(void **)store_ptr,error); /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBLGPL function <%s> completed\n", function_name); fprintf(stderr,"dbg2 Return values:\n"); fprintf(stderr,"dbg2 store_ptr: %p\n",store_ptr); fprintf(stderr,"dbg2 error: %d\n",*error); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return status */ return(status); } /*--------------------------------------------------------------------*/ int mblgpl_read_fbt(int verbose, FILE *fp, struct mblgpl_fbt_struct *store, int *error) { char *function_name = "mblgpl_read_fbt"; int status = MBLGPL_SUCCESS; struct mblgpl_oldfbt_struct oldstore; int read_size; short *flag; short short_transducer_depth; short short_altitude; short short_beams_bath, short_beams_amp, short_pixels_ss, short_spare1; int header_length; char buffer[MBLGPL_FBT_COMMENTSIZE]; int index; double newdepthscale; double depthmax; int time_i[7], time_j[6]; int version; int i; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBLGPL function <%s> called\n", function_name); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 fp: %p\n",fp); fprintf(stderr,"dbg2 store: %p\n",store); } /* read next header id from file */ if ((status = fread(buffer,1,2,fp)) == 2) { status = MBLGPL_SUCCESS; *error = MBLGPL_ERROR_NO_ERROR; } else { status = MBLGPL_FAILURE; *error = MBLGPL_ERROR_EOF; } /* read rest of header from file */ if (status == MBLGPL_SUCCESS) { flag = (short *) buffer; #ifdef BYTESWAPPED *flag = mblgpl_swap_short(*flag); #endif if (*flag == MBLGPL_FBT_ID_COMMENT1) { store->kind = MBLGPL_DATA_COMMENT; header_length = MBLGPL_FBT_V1HEADERSIZE; } else if (*flag == MBLGPL_FBT_ID_COMMENT2) { store->kind = MBLGPL_DATA_COMMENT; header_length = 2; } else if (*flag == MBLGPL_FBT_ID_V5DATA) { store->kind = MBLGPL_DATA_DATA; header_length = MBLGPL_FBT_V5HEADERSIZE; version = 5; } else if (*flag == MBLGPL_FBT_ID_V4DATA) { store->kind = MBLGPL_DATA_DATA; header_length = MBLGPL_FBT_V4HEADERSIZE; version = 4; } else if (*flag == MBLGPL_FBT_ID_V3DATA) { store->kind = MBLGPL_DATA_DATA; header_length = MBLGPL_FBT_V3HEADERSIZE; version = 3; } else if (*flag == MBLGPL_FBT_ID_V2DATA) { store->kind = MBLGPL_DATA_DATA; header_length = MBLGPL_FBT_V2HEADERSIZE; version = 2; } else if (*flag == MBLGPL_FBT_ID_V1DATA) { store->kind = MBLGPL_DATA_DATA; header_length = MBLGPL_FBT_V1HEADERSIZE; version = 1; } else { status = MBLGPL_FAILURE; *error = MBLGPL_ERROR_UNINTELLIGIBLE; store->kind = MBLGPL_DATA_NONE; } } if (status == MBLGPL_SUCCESS && header_length == 2) { /* only 2 byte header for new style comment */ status = MBLGPL_SUCCESS; *error = MBLGPL_ERROR_NO_ERROR; } else if (status == MBLGPL_SUCCESS && (status = fread(&buffer[2],1,header_length-2, fp)) == header_length-2) { status = MBLGPL_SUCCESS; *error = MBLGPL_ERROR_NO_ERROR; } else { status = MBLGPL_FAILURE; *error = MBLGPL_ERROR_EOF; } if (status == MBLGPL_SUCCESS && store->kind == MBLGPL_DATA_DATA) { if (version == 5) { index = 2; mblgpl_get_binary_double(MBLGPL_NO, (void *) &buffer[index], &store->time_d); index +=8; mblgpl_get_binary_double(MBLGPL_NO, (void *) &buffer[index], &store->longitude); index +=8; mblgpl_get_binary_double(MBLGPL_NO, (void *) &buffer[index], &store->latitude); index +=8; mblgpl_get_binary_double(MBLGPL_NO, (void *) &buffer[index], &store->sonardepth); index +=8; mblgpl_get_binary_double(MBLGPL_NO, (void *) &buffer[index], &store->altitude); index +=8; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->heading); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->speed); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->roll); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->pitch); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->heave); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->beam_xwidth); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->beam_lwidth); index +=4; mblgpl_get_binary_int(MBLGPL_NO, (void *) &buffer[index], &store->beams_bath); index +=4; mblgpl_get_binary_int(MBLGPL_NO, (void *) &buffer[index], &store->beams_amp); index +=4; mblgpl_get_binary_int(MBLGPL_NO, (void *) &buffer[index], &store->pixels_ss); index +=4; mblgpl_get_binary_int(MBLGPL_NO, (void *) &buffer[index], &store->spare1); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->depth_scale); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->distance_scale); index +=4; store->ss_scalepower = buffer[index]; index++; store->ss_type = buffer[index]; index++; store->imagery_type = buffer[index]; index++; store->topo_type = buffer[index]; index++; } else if (version == 4) { index = 2; mblgpl_get_binary_double(MBLGPL_NO, (void *) &buffer[index], &store->time_d); index +=8; mblgpl_get_binary_double(MBLGPL_NO, (void *) &buffer[index], &store->longitude); index +=8; mblgpl_get_binary_double(MBLGPL_NO, (void *) &buffer[index], &store->latitude); index +=8; mblgpl_get_binary_double(MBLGPL_NO, (void *) &buffer[index], &store->sonardepth); index +=8; mblgpl_get_binary_double(MBLGPL_NO, (void *) &buffer[index], &store->altitude); index +=8; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->heading); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->speed); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->roll); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->pitch); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->heave); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->beam_xwidth); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->beam_lwidth); index +=4; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &short_beams_bath); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &short_beams_amp); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &short_pixels_ss); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &short_spare1); index +=2; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->depth_scale); index +=4; mblgpl_get_binary_float(MBLGPL_NO, (void *) &buffer[index], &store->distance_scale); index +=4; store->ss_scalepower = buffer[index]; index++; store->ss_type = buffer[index]; index++; store->imagery_type = buffer[index]; index++; store->topo_type = buffer[index]; index++; store->beams_bath = short_beams_bath; store->beams_amp = short_beams_amp; store->pixels_ss = short_pixels_ss; store->spare1 = short_spare1; } else { index = 2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.year); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.day); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.min); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.sec); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.msec); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.lon2u); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.lon2b); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.lat2u); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.lat2b); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.heading); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.speed); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.beams_bath); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.beams_amp); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.pixels_ss); index +=2; if (version == 1) { mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.depth_scale); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.distance_scale); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &short_transducer_depth); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &short_altitude); index +=2; oldstore.transducer_depth = (int) (oldstore.depth_scale * short_transducer_depth); oldstore.altitude = (int) (oldstore.depth_scale * short_altitude); } else if (version == 2) { mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.depth_scale); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.distance_scale); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &short_transducer_depth); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &short_altitude); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.beam_xwidth); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.beam_lwidth); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.ss_type); index +=2; oldstore.transducer_depth = (int) (oldstore.depth_scale * short_transducer_depth); oldstore.altitude = (int) (oldstore.depth_scale * short_altitude); } else if (version == 3) { mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.depth_scale); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.distance_scale); index +=2; mblgpl_get_binary_int(MBLGPL_NO, (void *) &buffer[index], &oldstore.transducer_depth); index +=4; mblgpl_get_binary_int(MBLGPL_NO, (void *) &buffer[index], &oldstore.altitude); index +=4; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.beam_xwidth); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.beam_lwidth); index +=2; mblgpl_get_binary_short(MBLGPL_NO, (void *) &buffer[index], &oldstore.ss_type); index +=2; } /* translate old data to current */ /* get time */ time_j[0] = oldstore.year; time_j[1] = oldstore.day; time_j[2] = oldstore.min; time_j[3] = oldstore.sec; time_j[4] = 1000 * oldstore.msec; mblgpl_get_itime(verbose,time_j,time_i); mblgpl_get_time(verbose,time_i,&store->time_d); /* get navigation */ store->longitude = ((double) oldstore.lon2u)/60. + ((double) oldstore.lon2b)/600000.; store->latitude = ((double) oldstore.lat2u)/60. + ((double) oldstore.lat2b)/600000. - 90.; /* get sonardepth and altitude */ store->sonardepth = 0.001 * oldstore.transducer_depth; store->altitude = 0.001 * oldstore.altitude; /* get heading (360 degrees = 65536) and speed */ store->heading = (float) (0.0054932 * oldstore.heading); store->speed = (float)(0.01 * oldstore.speed); /* set roll and pitch to zero */ store->roll = 0.0; store->pitch = 0.0; /* set beamwidths in mblgpl_io structure */ if (oldstore.beam_xwidth > 0) store->beam_xwidth = 0.01 * oldstore.beam_xwidth; else store->beam_xwidth = 2.0; if (oldstore.beam_lwidth > 0) store->beam_lwidth = 0.01 * oldstore.beam_lwidth; else store->beam_lwidth = 2.0; /* get beams_bath, beams_amp, pixels_ss */ store->beams_bath = oldstore.beams_bath; store->beams_amp = oldstore.beams_amp; store->pixels_ss = oldstore.pixels_ss; store->spare1 = 0; /* get scaling */ store->depth_scale = 0.001 * oldstore.depth_scale; store->distance_scale = 0.001 * oldstore.distance_scale; /* get sidescan type */ store->ss_scalepower = 0; store->ss_type = oldstore.ss_type; store->imagery_type = MBLGPL_IMAGERY_TYPE_UNKNOWN; store->topo_type = MBLGPL_TOPOGRAPHY_TYPE_UNKNOWN; } } /* print debug statements */ if (verbose >= 5) { fprintf(stderr,"\ndbg5 New header read in function <%s>\n",function_name); fprintf(stderr,"dbg5 flag: %d\n",*flag); } if (verbose >= 5 && store->kind == MBLGPL_DATA_DATA && version < 4) { fprintf(stderr,"\ndbg5 Old version header read in function <%s>\n",function_name); fprintf(stderr,"dbg5 version: %d\n",version); fprintf(stderr,"dbg5 year: %d\n",oldstore.year); fprintf(stderr,"dbg5 day: %d\n",oldstore.day); fprintf(stderr,"dbg5 minute: %d\n",oldstore.min); fprintf(stderr,"dbg5 second: %d\n",oldstore.sec); fprintf(stderr,"dbg5 msec: %d\n",oldstore.msec); fprintf(stderr,"dbg5 lonu: %d\n",oldstore.lon2u); fprintf(stderr,"dbg5 lonb: %d\n",oldstore.lon2b); fprintf(stderr,"dbg5 latu: %d\n",oldstore.lat2u); fprintf(stderr,"dbg5 latb: %d\n",oldstore.lat2b); fprintf(stderr,"dbg5 heading: %d\n",oldstore.heading); fprintf(stderr,"dbg5 speed: %d\n",oldstore.speed); fprintf(stderr,"dbg5 beams bath: %d\n",oldstore.beams_bath); fprintf(stderr,"dbg5 beams amp: %d\n",oldstore.beams_amp); fprintf(stderr,"dbg5 pixels ss: %d\n",oldstore.pixels_ss); fprintf(stderr,"dbg5 depth scale: %d\n",oldstore.depth_scale); fprintf(stderr,"dbg5 dist scale: %d\n",oldstore.distance_scale); fprintf(stderr,"dbg5 transducer_depth: %d\n",oldstore.transducer_depth); fprintf(stderr,"dbg5 altitude: %d\n",oldstore.altitude); fprintf(stderr,"dbg5 beam_xwidth: %d\n",oldstore.beam_xwidth); fprintf(stderr,"dbg5 beam_lwidth: %d\n",oldstore.beam_lwidth); fprintf(stderr,"dbg5 ss_type: %d\n",oldstore.ss_type); fprintf(stderr,"dbg5 status: %d\n",status); fprintf(stderr,"dbg5 error: %d\n",*error); } if (verbose >= 5 && store->kind == MBLGPL_DATA_DATA) { fprintf(stderr,"\ndbg5 Current version header values in function <%s>\n",function_name); fprintf(stderr,"dbg5 time_d: %f\n",store->time_d); fprintf(stderr,"dbg5 longitude: %f\n",store->longitude); fprintf(stderr,"dbg5 latitude: %f\n",store->latitude); fprintf(stderr,"dbg5 sonardepth: %f\n",store->sonardepth); fprintf(stderr,"dbg5 altitude: %f\n",store->altitude); fprintf(stderr,"dbg5 heading: %f\n",store->heading); fprintf(stderr,"dbg5 speed: %f\n",store->speed); fprintf(stderr,"dbg5 roll: %f\n",store->roll); fprintf(stderr,"dbg5 pitch: %f\n",store->pitch); fprintf(stderr,"dbg5 heave: %f\n",store->heave); fprintf(stderr,"dbg5 beam_xwidth: %f\n",store->beam_xwidth); fprintf(stderr,"dbg5 beam_lwidth: %f\n",store->beam_lwidth); fprintf(stderr,"dbg5 beams_bath: %d\n",store->beams_bath); fprintf(stderr,"dbg5 beams_amp: %d\n",store->beams_amp); fprintf(stderr,"dbg5 pixels_ss: %d\n",store->pixels_ss); fprintf(stderr,"dbg5 spare1: %d\n",store->spare1); fprintf(stderr,"dbg5 depth_scale: %f\n",store->depth_scale); fprintf(stderr,"dbg5 distance_scale: %f\n",store->distance_scale); fprintf(stderr,"dbg5 ss_scalepower: %d\n",store->ss_scalepower); fprintf(stderr,"dbg5 ss_type: %d\n",store->ss_type); fprintf(stderr,"dbg5 imagery_type: %d\n",store->imagery_type); fprintf(stderr,"dbg5 sonartype: %d\n",store->topo_type); fprintf(stderr,"dbg5 status: %d\n",status); fprintf(stderr,"dbg5 error: %d\n",*error); } /* read next chunk of the data */ if (status == MBLGPL_SUCCESS && store->kind == MBLGPL_DATA_COMMENT) { read_size = 128; if ((status = fread(store->comment,1,read_size,fp)) == read_size) { status = MBLGPL_SUCCESS; *error = MBLGPL_ERROR_NO_ERROR; } else { status = MBLGPL_FAILURE; *error = MBLGPL_ERROR_EOF; } /* print debug messages */ if (verbose >= 5 && status == MBLGPL_SUCCESS) { fprintf(stderr,"\ndbg5 New header comment in function <%s>\n",function_name); fprintf(stderr,"dbg5 comment: %s\n",store->comment); } } else if (status == MBLGPL_SUCCESS && store->kind == MBLGPL_DATA_DATA) { /* if needed reset numbers of beams and allocate memory for store arrays */ if (store->beams_bath > store->beams_bath_alloc) { store->beams_bath_alloc = store->beams_bath; if (store->beamflag != NULL) status = mblgpl_free(verbose, (void **) &store->beamflag, error); if (store->bath != NULL) status = mblgpl_free(verbose, (void **) &store->bath, error); if (store->bath_acrosstrack != NULL) status = mblgpl_free(verbose, (void **) &store->bath_acrosstrack, error); if (store->bath_alongtrack != NULL) status = mblgpl_free(verbose, (void **) &store->bath_alongtrack, error); status = mblgpl_malloc(verbose, store->beams_bath_alloc * sizeof(char), (void **)&store->beamflag,error); status = mblgpl_malloc(verbose, store->beams_bath_alloc * sizeof(short), (void **)&store->bath,error); status = mblgpl_malloc(verbose, store->beams_bath_alloc * sizeof(short), (void **)&store->bath_acrosstrack,error); status = mblgpl_malloc(verbose, store->beams_bath_alloc * sizeof(short), (void **)&store->bath_alongtrack,error); /* deal with a memory allocation failure */ if (status == MBLGPL_FAILURE) { status = mblgpl_free(verbose, (void **) &store->beamflag, error); status = mblgpl_free(verbose, (void **) &store->bath, error); status = mblgpl_free(verbose, (void **) &store->bath_acrosstrack, error); status = mblgpl_free(verbose, (void **) &store->bath_alongtrack, error); status = MBLGPL_FAILURE; *error = MBLGPL_ERROR_MEMORY_FAIL; if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> terminated with error\n",function_name); fprintf(stderr,"dbg2 Return values:\n"); fprintf(stderr,"dbg2 error: %d\n",*error); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } return(status); } } if (store->beams_amp > store->beams_amp_alloc) { store->beams_amp_alloc = store->beams_amp; if (store->amp != NULL) status = mblgpl_free(verbose, (void **) &store->amp, error); status = mblgpl_malloc(verbose, store->beams_amp_alloc * sizeof(short), (void **)&store->amp,error); /* deal with a memory allocation failure */ if (status == MBLGPL_FAILURE) { status = mblgpl_free(verbose, (void **) &store->amp, error); status = MBLGPL_FAILURE; *error = MBLGPL_ERROR_MEMORY_FAIL; if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> terminated with error\n",function_name); fprintf(stderr,"dbg2 Return values:\n"); fprintf(stderr,"dbg2 error: %d\n",*error); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } return(status); } } if (store->pixels_ss > store->pixels_ss_alloc) { store->pixels_ss_alloc = store->pixels_ss; if (store->ss != NULL) status = mblgpl_free(verbose, (void **) &store->ss, error); if (store->ss_acrosstrack != NULL) status = mblgpl_free(verbose, (void **) &store->ss_acrosstrack, error); if (store->ss_alongtrack != NULL) status = mblgpl_free(verbose, (void **) &store->ss_alongtrack, error); status = mblgpl_malloc(verbose, store->pixels_ss_alloc * sizeof(short), (void **)&store->ss,error); status = mblgpl_malloc(verbose, store->pixels_ss_alloc * sizeof(short), (void **)&store->ss_acrosstrack,error); status = mblgpl_malloc(verbose, store->pixels_ss_alloc * sizeof(short), (void **)&store->ss_alongtrack,error); /* deal with a memory allocation failure */ if (status == MBLGPL_FAILURE) { status = mblgpl_free(verbose, (void **) &store->ss, error); status = mblgpl_free(verbose, (void **) &store->ss_acrosstrack, error); status = mblgpl_free(verbose, (void **) &store->ss_alongtrack, error); status = MBLGPL_FAILURE; *error = MBLGPL_ERROR_MEMORY_FAIL; if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> terminated with error\n",function_name); fprintf(stderr,"dbg2 Return values:\n"); fprintf(stderr,"dbg2 error: %d\n",*error); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } return(status); } } /* read bathymetry */ read_size = sizeof(char)*store->beams_bath; status = fread(store->beamflag,1,read_size,fp); read_size = sizeof(short int)*store->beams_bath; status = fread(store->bath,1,read_size,fp); status = fread(store->bath_acrosstrack,1,read_size,fp); status = fread(store->bath_alongtrack,1,read_size,fp); /* read amplitudes */ read_size = sizeof(short int)*store->beams_amp; status = fread(store->amp,1,read_size,fp); /* read sidescan */ read_size = sizeof(short int)*store->pixels_ss; status = fread(store->ss,1,read_size,fp); status = fread(store->ss_acrosstrack,1,read_size,fp); status = fread(store->ss_alongtrack,1,read_size,fp); /* byte swap the data if necessary */ #ifdef BYTESWAPPED for (i=0;ibeams_bath;i++) { store->bath[i] = mblgpl_swap_short(store->bath[i]); store->bath_acrosstrack[i] = mblgpl_swap_short(store->bath_acrosstrack[i]); store->bath_alongtrack[i] = mblgpl_swap_short(store->bath_alongtrack[i]); } for (i=0;ibeams_amp;i++) { store->amp[i] = mblgpl_swap_short(store->amp[i]); } for (i=0;ipixels_ss;i++) { store->ss[i] = mblgpl_swap_short(store->ss[i]); store->ss_acrosstrack[i] = mblgpl_swap_short(store->ss_acrosstrack[i]); store->ss_alongtrack[i] = mblgpl_swap_short(store->ss_alongtrack[i]); } #endif /* subtract the transducer depth from the bathymetry if version 1 or 2 data has been read */ if (version < 3) { depthmax = 0.0; for (i=0;ibeams_bath;i++) { depthmax = MAX(depthmax, (store->depth_scale * store->bath[i] - store->sonardepth)); } if (depthmax > 0.0) newdepthscale = 0.001 * (double)(MAX((int) (1 + depthmax / 30.0), 1)); for (i=0;ibeams_bath;i++) { store->bath[i] = (short)((store->depth_scale * store->bath[i] - store->sonardepth) / newdepthscale); } store->depth_scale = newdepthscale; } /* check for end of file */ if (status == read_size) { status = MBLGPL_SUCCESS; *error = MBLGPL_ERROR_NO_ERROR; } else { status = MBLGPL_FAILURE; *error = MBLGPL_ERROR_EOF; } /* print debug messages */ if (verbose >= 5 && status == MBLGPL_SUCCESS) { fprintf(stderr,"\ndbg5 New data read in function <%s>\n",function_name); fprintf(stderr,"dbg5 beams_bath: %d\n", store->beams_bath); for (i=0;ibeams_bath;i++) fprintf(stderr,"dbg5 beam:%d flag:%d bath:%d acrosstrack:%d alongtrack:%d\n", i,store->beamflag[i],store->bath[i], store->bath_acrosstrack[i],store->bath_alongtrack[i]); fprintf(stderr,"dbg5 beams_amp: %d\n", store->beams_amp); for (i=0;ibeams_amp;i++) fprintf(stderr,"dbg5 beam:%d flag:%d amp:%d acrosstrack:%d alongtrack:%d\n", i,store->beamflag[i],store->amp[i], store->bath_acrosstrack[i],store->bath_alongtrack[i]); fprintf(stderr,"dbg5 pixels_ss: %d\n", store->pixels_ss); for (i=0;ipixels_ss;i++) fprintf(stderr,"dbg5 pixel:%d ss:%d acrosstrack:%d alongtrack:%d\n", i,store->ss[i], store->ss_acrosstrack[i],store->ss_alongtrack[i]); } } /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> completed\n",function_name); fprintf(stderr,"dbg2 Return values:\n"); fprintf(stderr,"dbg2 error: %d\n",*error); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return status */ return(status); } /*--------------------------------------------------------------------*/ /* function mblgpl_get_time returns the number of seconds from * 1/1/70 00:00:00 calculated from (yy/mm/dd/hr/mi/sc). */ int mblgpl_get_time(int verbose, int time_i[7], double *time_d) { char *function_name = "mblgpl_get_time"; int status; int yearday; int leapday; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> called\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 year: %d\n",time_i[0]); fprintf(stderr,"dbg2 month: %d\n",time_i[1]); fprintf(stderr,"dbg2 day: %d\n",time_i[2]); fprintf(stderr,"dbg2 hour: %d\n",time_i[3]); fprintf(stderr,"dbg2 minute: %d\n",time_i[4]); fprintf(stderr,"dbg2 second: %d\n",time_i[5]); fprintf(stderr,"dbg2 microsec:%d\n",time_i[6]); } /* get time */ yearday = yday[time_i[1]-1]; if (((time_i[0] % 4 == 0 && time_i[0] % 100 != 0) || time_i[0]%400==0) && (time_i[1] > 2)) yearday++; leapday = (time_i[0] - 1969)/4; *time_d = (time_i[0] - 1970)*SECINYEAR + (yearday - 1 + leapday + time_i[2])*SECINDAY + time_i[3]*SECINHOUR + time_i[4]*SECINMINUTE + time_i[5] + 0.000001*time_i[6]; /* assume success */ status = MBLGPL_SUCCESS; /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> completed\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Return value:\n"); fprintf(stderr,"dbg2 time_d: %f\n",*time_d); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return success */ return(status); } /*--------------------------------------------------------------------*/ /* function mblgpl_get_date returns yy/mm/dd/hr/mi/sc calculated * from the number of seconds after 1/1/70 00:00:0 */ int mblgpl_get_date(int verbose, double time_d, int time_i[7]) { char *function_name = "mblgpl_get_date"; int status; int i; int daytotal; int yearday; int leapday; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> called\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 time_d: %f\n",time_d); } /* get the date */ daytotal = (int) (time_d/SECINDAY); time_i[3] = (int) ((time_d - daytotal*SECINDAY)/SECINHOUR); time_i[4] = (int) ((time_d - daytotal*SECINDAY - time_i[3]*SECINHOUR)/SECINMINUTE); time_i[5] = (int) (time_d - daytotal*SECINDAY - time_i[3]*SECINHOUR - time_i[4]*SECINMINUTE); time_i[6] = (int) 1000000*(time_d - daytotal*SECINDAY - time_i[3]*SECINHOUR - time_i[4]*SECINMINUTE - time_i[5]); time_i[0] = (int) (time_d/SECINYEAR) + 1970; leapday = (time_i[0] - 1969)/4; yearday = daytotal - 365*(time_i[0] - 1970) - leapday + 1; if (yearday <= 0) { time_i[0]--; leapday = (time_i[0] - 1969)/4; yearday = daytotal - 365*(time_i[0] - 1970) - leapday + 1; } leapday = 0; if (((time_i[0] % 4 == 0 && time_i[0] % 100 != 0) || time_i[0]%400 == 0) && yearday > yday[2]) leapday = 1; for (i=0;i<12;i++) if (yearday > (yday[i] + leapday)) time_i[1] = i + 1; time_i[2] = yearday - yday[time_i[1]-1] - leapday; /* assume success */ status = MBLGPL_SUCCESS; /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\nMBIO function <%s> completed\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Return values:\n"); fprintf(stderr,"dbg2 year: %d\n",time_i[0]); fprintf(stderr,"dbg2 month: %d\n",time_i[1]); fprintf(stderr,"dbg2 day: %d\n",time_i[2]); fprintf(stderr,"dbg2 hour: %d\n",time_i[3]); fprintf(stderr,"dbg2 minute: %d\n",time_i[4]); fprintf(stderr,"dbg2 second: %d\n",time_i[5]); fprintf(stderr,"dbg2 microsec:%d\n",time_i[6]); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return success */ return(status); } /*--------------------------------------------------------------------*/ /* function mblgpl_get_date_string returns a string formated as: * yyyy/mm/dd:hh:mm:ss.ssssss * from the number of seconds after 1/1/70 00:00:0 */ int mblgpl_get_date_string(int verbose, double time_d, char *string) { char *function_name = "mblgpl_get_date_string"; int status; int time_i[7]; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> called\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 time_d: %f\n",time_d); } /* get the date */ mblgpl_get_date(verbose, time_d, time_i); sprintf(string, "%4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d.%6.6d", time_i[0], time_i[1], time_i[2], time_i[3], time_i[4], time_i[5], time_i[6]); /* assume success */ status = MBLGPL_SUCCESS; /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\nMBIO function <%s> completed\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Return values:\n"); fprintf(stderr,"dbg2 string: %s\n",string); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return success */ return(status); } /*--------------------------------------------------------------------*/ /* function mblgpl_get_jtime returns the day of year calculated * from (yy/mm/dd/hr/mi/sc). */ int mblgpl_get_jtime(int verbose, int time_i[7], int time_j[5]) { char *function_name = "mblgpl_get_jtime"; int status; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> called\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 year: %d\n",time_i[0]); fprintf(stderr,"dbg2 month: %d\n",time_i[1]); fprintf(stderr,"dbg2 day: %d\n",time_i[2]); fprintf(stderr,"dbg2 hour: %d\n",time_i[3]); fprintf(stderr,"dbg2 minute: %d\n",time_i[4]); fprintf(stderr,"dbg2 second: %d\n",time_i[5]); fprintf(stderr,"dbg2 microsecond:%d\n",time_i[6]); } /* get time with day of year */ time_j[0] = time_i[0]; time_j[1] = yday[time_i[1]-1] + time_i[2]; if (((time_i[0] % 4 == 0 && time_i[0] % 100 != 0) || time_i[0] % 400 == 0) && (time_i[1] > 2)) time_j[1]++; time_j[2] = time_i[3]*IMININHOUR + time_i[4]; time_j[3] = time_i[5]; time_j[4] = time_i[6]; /* assume success */ status = MBLGPL_SUCCESS; /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> completed\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Return value:\n"); fprintf(stderr,"dbg2 year: %d\n",time_j[0]); fprintf(stderr,"dbg2 day of year:%d\n",time_j[1]); fprintf(stderr,"dbg2 minute: %d\n",time_j[2]); fprintf(stderr,"dbg2 second: %d\n",time_j[3]); fprintf(stderr,"dbg2 microsecond:%d\n",time_j[4]); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return success */ return(status); } /*--------------------------------------------------------------------*/ /* function mblgpl_get_itime returns the time in (yy/mm/dd/hr/mi/sc) * calculated from the time in (yy/yd/hr/mi/sc) where yd is the * day of the year. */ int mblgpl_get_itime(int verbose, int time_j[5], int time_i[7]) { char *function_name = "mblgpl_get_itime"; int status; int leapday; int i; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> called\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 year: %d\n",time_j[0]); fprintf(stderr,"dbg2 day of year:%d\n",time_j[1]); fprintf(stderr,"dbg2 minute: %d\n",time_j[2]); fprintf(stderr,"dbg2 second: %d\n",time_j[3]); fprintf(stderr,"dbg2 microsecond:%d\n",time_j[4]); } /* get the date */ time_i[0] = time_j[0]; time_i[3] = time_j[2]/IMININHOUR; time_i[4] = time_j[2] - time_i[3]*IMININHOUR; time_i[5] = time_j[3]; time_i[6] = time_j[4]; if (((time_j[0] % 4 == 0 && time_j[0] % 100 != 0) || time_j[0] % 400 == 0) && time_j[1] > yday[2]) leapday = 1; else leapday = 0; time_i[1] = 0; for (i=0;i<12;i++) if (time_j[1] > (yday[i] + leapday)) time_i[1] = i + 1; if(leapday==1 && time_j[1] == yday[2]+1) leapday=0; time_i[2] = time_j[1] - yday[time_i[1]-1] - leapday; /* assume success */ status = MBLGPL_SUCCESS; /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> completed\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Return value:\n"); fprintf(stderr,"dbg2 year: %d\n",time_i[0]); fprintf(stderr,"dbg2 month: %d\n",time_i[1]); fprintf(stderr,"dbg2 day: %d\n",time_i[2]); fprintf(stderr,"dbg2 hour: %d\n",time_i[3]); fprintf(stderr,"dbg2 minute: %d\n",time_i[4]); fprintf(stderr,"dbg2 second: %d\n",time_i[5]); fprintf(stderr,"dbg2 microsecond:%d\n",time_i[6]); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return success */ return(status); } /*--------------------------------------------------------------------*/ /* function mblgpl_get_binary_short copies a binary short from * a buffer, swapping if necessary */ int mblgpl_get_binary_short(int swapped, void *buffer, void *ptr) { short *value; value = (short *) ptr; memcpy(value, buffer, sizeof(short)); #ifdef BYTESWAPPED if (swapped == MBLGPL_NO) *value = mblgpl_swap_short(*((short *)value)); #else if (swapped == MBLGPL_YES) *value = mblgpl_swap_short(*value); #endif return(0); } /*--------------------------------------------------------------------*/ /* function mblgpl_get_binary_int copies a binary int from * a buffer, swapping if necessary */ int mblgpl_get_binary_int(int swapped, void *buffer, void *ptr) { int *value; value = (int *) ptr; memcpy(value, buffer, sizeof(int)); #ifdef BYTESWAPPED if (swapped == MBLGPL_NO) *value = mblgpl_swap_int(*value); #else if (swapped == MBLGPL_YES) *value = mblgpl_swap_int(*value); #endif return(0); } /*--------------------------------------------------------------------*/ /* function mblgpl_get_binary_float copies a binary float from * a buffer, swapping if necessary */ int mblgpl_get_binary_float(int swapped, void *buffer, void *ptr) { float *value; value = (float *) ptr; memcpy(value, buffer, sizeof(float)); #ifdef BYTESWAPPED if (swapped == MBLGPL_NO) mblgpl_swap_float(value); #else if (swapped == MBLGPL_YES) mblgpl_swap_float(value); #endif return(0); } /*--------------------------------------------------------------------*/ /* function mblgpl_get_binary_double copies a binary double from * a buffer, swapping if necessary */ int mblgpl_get_binary_double(int swapped, void *buffer, void *ptr) { double *value; value = (double *) ptr; memcpy(value, buffer, sizeof(double)); #ifdef BYTESWAPPED if (swapped == MBLGPL_NO) mblgpl_swap_double(value); #else if (swapped == MBLGPL_YES) mblgpl_swap_double(value); #endif return(0); } /*--------------------------------------------------------------------*/ /* function mblgpl_get_binary_long copies a binary long from * a buffer, swapping if necessary */ int mblgpl_get_binary_long(int swapped, void *buffer, void *ptr) { mblgpl_s_long *value; value = (mblgpl_s_long *) ptr; memcpy(value, buffer, sizeof(mblgpl_s_long)); #ifdef BYTESWAPPED if (swapped == MBLGPL_NO) mblgpl_swap_long(value); #else if (swapped == MBLGPL_YES) mblgpl_swap_long(value); #endif return(0); } /*--------------------------------------------------------------------*/ /* function mblgpl_put_binary_short copies a binary short to * a buffer, swapping if necessary */ int mblgpl_put_binary_short(int swapped, short value, void *buffer) { #ifdef BYTESWAPPED if (swapped == MBLGPL_NO) value = mblgpl_swap_short(value); #else if (swapped == MBLGPL_YES) value = mblgpl_swap_short(value); #endif memcpy(buffer, &value, sizeof(short)); return(0); } /*--------------------------------------------------------------------*/ /* function mblgpl_put_binary_int copies a binary int to * a buffer, swapping if necessary */ int mblgpl_put_binary_int(int swapped, int value, void *buffer) { #ifdef BYTESWAPPED if (swapped == MBLGPL_NO) value = mblgpl_swap_int(value); #else if (swapped == MBLGPL_YES) value = mblgpl_swap_int(value); #endif memcpy(buffer, &value, sizeof(int)); return(0); } /*--------------------------------------------------------------------*/ /* function mblgpl_put_binary_float copies a binary float to * a buffer, swapping if necessary */ int mblgpl_put_binary_float(int swapped, float value, void *buffer) { #ifdef BYTESWAPPED if (swapped == MBLGPL_NO) mblgpl_swap_float(&value); #else if (swapped == MBLGPL_YES) mblgpl_swap_float(&value); #endif memcpy(buffer, &value, sizeof(float)); return(0); } /*--------------------------------------------------------------------*/ /* function mblgpl_put_binary_double copies a binary double to * a buffer, swapping if necessary */ int mblgpl_put_binary_double(int swapped, double value, void *buffer) { #ifdef BYTESWAPPED if (swapped == MBLGPL_NO) mblgpl_swap_double(&value); #else if (swapped == MBLGPL_YES) mblgpl_swap_double(&value); #endif memcpy(buffer, &value, sizeof(double)); return(0); } /*--------------------------------------------------------------------*/ /* function mblgpl_put_binary_long copies a binary long to * a buffer, swapping if necessary */ int mblgpl_put_binary_long(int swapped, mblgpl_s_long value, void *buffer) { #ifdef BYTESWAPPED if (swapped == MBLGPL_NO) mblgpl_swap_long(&value); #else if (swapped == MBLGPL_YES) mblgpl_swap_long(&value); #endif memcpy(buffer, &value, sizeof(mblgpl_s_long)); return(0); } /*--------------------------------------------------------------------*/ /* function mblgpl_swap_float swaps the bytes of an 4 byte float value */ int mblgpl_swap_float(float *a) { unsigned int *t; t = (unsigned int *) a; *t = mblgpl_swap_int(*t); return(MBLGPL_SUCCESS); } /*--------------------------------------------------------------------*/ /* function mblgpl_swap_double swaps the bytes of an 8 byte double value */ int mblgpl_swap_double(double *a) { mblgpl_u_char bc[8]; mblgpl_u_char *ac; ac = (mblgpl_u_char *) a; bc[0] = ac[7]; bc[1] = ac[6]; bc[2] = ac[5]; bc[3] = ac[4]; bc[4] = ac[3]; bc[5] = ac[2]; bc[6] = ac[1]; bc[7] = ac[0]; ac[0] = bc[0]; ac[1] = bc[1]; ac[2] = bc[2]; ac[3] = bc[3]; ac[4] = bc[4]; ac[5] = bc[5]; ac[6] = bc[6]; ac[7] = bc[7]; return(MBLGPL_SUCCESS); } /*--------------------------------------------------------------------*/ /* function mblgpl_swap_long swaps the bytes of an 8 byte long value */ int mblgpl_swap_long(mblgpl_s_long *a) { mblgpl_u_char bc[8]; mblgpl_u_char *ac; ac = (mblgpl_u_char *) a; bc[0] = ac[7]; bc[1] = ac[6]; bc[2] = ac[5]; bc[3] = ac[4]; bc[4] = ac[3]; bc[5] = ac[2]; bc[6] = ac[1]; bc[7] = ac[0]; ac[0] = bc[0]; ac[1] = bc[1]; ac[2] = bc[2]; ac[3] = bc[3]; ac[4] = bc[4]; ac[5] = bc[5]; ac[6] = bc[6]; ac[7] = bc[7]; return(MBLGPL_SUCCESS); } /*--------------------------------------------------------------------*/ int mblgpl_malloc(int verbose, size_t size, void **ptr, int *error) { char *function_name = "mblgpl_malloc"; int status = MBLGPL_SUCCESS; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBLGPL function <%s> called\n", function_name); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 size: %lu\n",size); fprintf(stderr,"dbg2 ptr: %p\n",ptr); fprintf(stderr,"dbg2 *ptr: %p\n",*ptr); } /* allocate memory */ *ptr = NULL; if (size > 0) { if ((*ptr = (void *) malloc(size)) == NULL) { *error = MBLGPL_ERROR_MEMORY_FAIL; status = MBLGPL_FAILURE; } else { *error = MBLGPL_ERROR_NO_ERROR; status = MBLGPL_SUCCESS; } } else { *ptr = NULL; *error = MBLGPL_ERROR_NO_ERROR; status = MBLGPL_SUCCESS; } /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBLGPL function <%s> completed\n", function_name); fprintf(stderr,"dbg2 Return values:\n"); fprintf(stderr,"dbg2 ptr: %p\n",*ptr); fprintf(stderr,"dbg2 error: %d\n",*error); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return status */ return(status); } /*--------------------------------------------------------------------*/ int mblgpl_free(int verbose, void **ptr, int *error) { char *function_name = "mblgpl_free"; int status = MBLGPL_SUCCESS; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBLGPL function <%s> called\n", function_name); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 ptr: %p\n",*ptr); } /* deallocate the memory if *ptr is not NULL */ if (*ptr != NULL) { /* free the memory */ free(*ptr); *ptr = NULL; } /* assume success */ *error = MBLGPL_ERROR_NO_ERROR; status = MBLGPL_SUCCESS; /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBLGPL function <%s> completed\n", function_name); fprintf(stderr,"dbg2 Return value:\n"); fprintf(stderr,"dbg2 ptr: %p\n",*ptr); fprintf(stderr,"dbg2 error: %d\n",*error); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return status */ return(status); } /*--------------------------------------------------------------------*/ int mblgpl_coor_scale(int verbose, double latitude, double *mtodeglon, double *mtodeglat) { char *function_name = "mblgpl_coor_scale"; int status; double radlat; /* print input debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> called\n",function_name); fprintf(stderr,"dbg2 Version id: %s\n",version_id); fprintf(stderr,"dbg2 Input arguments:\n"); fprintf(stderr,"dbg2 verbose: %d\n",verbose); fprintf(stderr,"dbg2 latitude: %f\n",latitude); } /* check that the latitude value is sensible */ if (fabs(latitude) <= 90.0) { radlat = DTR*latitude; *mtodeglon = 1./fabs(C1*cos(radlat) + C2*cos(3*radlat) + C3*cos(5*radlat)); *mtodeglat = 1./fabs(C4 + C5*cos(2*radlat) + C6*cos(4*radlat) + C7*cos(6*radlat)); status = MBLGPL_SUCCESS; } /* set error flag if needed */ else { status = MBLGPL_FAILURE; } /* print output debug statements */ if (verbose >= 2) { fprintf(stderr,"\ndbg2 MBIO function <%s> completed\n",function_name); fprintf(stderr,"dbg2 Return arguments:\n"); fprintf(stderr,"dbg2 mtodeglon: %g\n",*mtodeglon); fprintf(stderr,"dbg2 mtodeglat: %g\n",*mtodeglat); fprintf(stderr,"dbg2 Return status:\n"); fprintf(stderr,"dbg2 status: %d\n",status); } /* return status */ return(status); } /*--------------------------------------------------------------------*/