Exemplo n.º 1
0
/*--------------------------------------------------------------------*/
int mbr_rt_hsurivax(int verbose, void *mbio_ptr, void *store_ptr, int *error) {
	char *function_name = "mbr_rt_hsurivax";
	int status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbf_hsuricen_struct *dataplus;
	struct mbf_hsuricen_data_struct *data;
	struct mbsys_hsds_struct *store;
	char *datacomment;
	int time_j[5];
	int i;
	int id;

	/* print input debug statements */
	if (verbose >= 2) {
		fprintf(stderr, "\ndbg2  MBIO function <%s> called\n", function_name);
		fprintf(stderr, "dbg2  Revision id: %s\n", rcs_id);
		fprintf(stderr, "dbg2  Input arguments:\n");
		fprintf(stderr, "dbg2       verbose:    %d\n", verbose);
		fprintf(stderr, "dbg2       mbio_ptr:   %p\n", (void *)mbio_ptr);
		fprintf(stderr, "dbg2       store_ptr:  %p\n", (void *)store_ptr);
	}

	/* get pointer to mbio descriptor */
	mb_io_ptr = (struct mb_io_struct *)mbio_ptr;

	/* get pointer to raw data structure */
	dataplus = (struct mbf_hsuricen_struct *)mb_io_ptr->raw_data;
	data = &(dataplus->data);
	datacomment = (char *)data;
	dataplus->kind = MB_DATA_DATA;
	store = (struct mbsys_hsds_struct *)store_ptr;

	/* set file position */
	mb_io_ptr->file_pos = mb_io_ptr->file_bytes;

	/* read next record from file */
	if ((status = fread(data, 1, mb_io_ptr->data_structure_size, mb_io_ptr->mbfp)) == mb_io_ptr->data_structure_size) {
		mb_io_ptr->file_bytes += status;
		status = MB_SUCCESS;
		*error = MB_ERROR_NO_ERROR;
	}
	else {
		mb_io_ptr->file_bytes += status;
		status = MB_FAILURE;
		*error = MB_ERROR_EOF;
	}

/* byte swap the data if necessary */
#ifndef BYTESWAPPED
	if (status == MB_SUCCESS && data->sec != 25443) {
		data->sec = mb_swap_short(data->sec);
		data->min = mb_swap_short(data->min);
		data->day = mb_swap_short(data->day);
		data->year = mb_swap_short(data->year);
		data->lat = mb_swap_int(data->lat);
		data->lon = mb_swap_int(data->lon);
		data->hdg = mb_swap_short(data->hdg);
		data->course = mb_swap_short(data->course);
		data->speed = mb_swap_short(data->speed);
		data->pitch = mb_swap_short(data->pitch);
		data->scale = mb_swap_short(data->scale);
		for (i = 0; i < MBSYS_HSDS_BEAMS; i++) {
			data->dist[i] = mb_swap_short(data->dist[i]);
			data->deph[i] = mb_swap_short(data->deph[i]);
		}
	}
#endif

	/* check for comment or unintelligible records */
	if (status == MB_SUCCESS) {
		if (data->sec == 25443) {
			dataplus->kind = MB_DATA_COMMENT;
		}
		else if (data->year == 0) {
			dataplus->kind = MB_DATA_NONE;
			status = MB_FAILURE;
			*error = MB_ERROR_UNINTELLIGIBLE;
		}
		else {
			dataplus->kind = MB_DATA_DATA;
		}
	}

	/* set kind and error in mb_io_ptr */
	mb_io_ptr->new_kind = dataplus->kind;
	mb_io_ptr->new_error = *error;

	/* translate values to hydrosweep data storage structure */
	if (status == MB_SUCCESS && store != NULL) {
		/* type of data record */
		store->kind = dataplus->kind;

		/* time stamp (all records ) */
		time_j[0] = data->year;
		time_j[1] = data->day;
		time_j[2] = data->min;
		time_j[3] = data->sec / 100;
		time_j[4] = 10000 * (data->sec - 100 * time_j[3]);
		mb_get_itime(verbose, time_j, mb_io_ptr->new_time_i);
		mb_get_time(verbose, mb_io_ptr->new_time_i, &(mb_io_ptr->new_time_d));
		store->year = mb_io_ptr->new_time_i[0];
		store->month = mb_io_ptr->new_time_i[1];
		store->day = mb_io_ptr->new_time_i[2];
		store->hour = mb_io_ptr->new_time_i[3];
		store->minute = mb_io_ptr->new_time_i[4];
		store->second = mb_io_ptr->new_time_i[5];
		store->alt_minute = 0;
		store->alt_second = 0;

		/* position (all records ) */
		store->lon = 0.0000001 * data->lon;
		store->lat = 0.0000001 * data->lat;
		if (store->lon > 180.)
			store->lon = store->lon - 360.;
		else if (store->lon < -180.)
			store->lon = store->lon + 360.;

		/* additional navigation and depths (ERGNMESS and ERGNEICH) */
		store->course_true = 0.1 * data->hdg;
		store->speed_transverse = 0.0;
		store->speed = 0.005092593 * data->speed;
		store->speed_reference[0] = data->speed_ref;
		store->pitch = 0.1 * data->pitch;
		store->track = 0;
		store->depth_scale = 0.01 * data->scale;
		store->depth_center = store->depth_scale * data->deph[MBSYS_HSDS_BEAMS / 2];
		store->spare = 1;
		id = MBSYS_HSDS_BEAMS - 1;
		for (i = 0; i < MBSYS_HSDS_BEAMS; i++) {
			store->distance[i] = data->dist[i];
			store->depth[i] = data->deph[i];
		}
		/*		for (i=0;i<MBSYS_HSDS_BEAMS;i++)
		            {
		            store->distance[id-i] = data->dist[i];
		            store->depth[id-i] = data->deph[i];
		            }*/

		/* travel time data (ERGNSLZT) */
		store->course_ground = 0.1 * data->course;
		store->speed_ground = 0.0;
		store->heave = 0.0;
		store->roll = 0.0;
		store->time_center = 0.0;
		store->time_scale = 0.0;
		for (i = 0; i < MBSYS_HSDS_BEAMS; i++)
			store->time[i] = 0;
		for (i = 0; i < 11; i++)
			store->gyro[i] = 0.0;

		/* amplitude data (ERGNAMPL) */
		store->mode[0] = '\0';
		store->trans_strbd = 0;
		store->trans_vert = 0;
		store->trans_port = 0;
		store->pulse_len_strbd = 0;
		store->pulse_len_vert = 0;
		store->pulse_len_port = 0;
		store->gain_start = 0;
		store->r_compensation_factor = 0;
		store->compensation_start = 0;
		store->increase_start = 0;
		store->tvc_near = 0;
		store->tvc_far = 0;
		store->increase_int_near = 0;
		store->increase_int_far = 0;
		store->gain_center = 0;
		store->filter_gain = 0.0;
		store->amplitude_center = 0;
		store->echo_duration_center = 0;
		store->echo_scale_center = 0;
		for (i = 0; i < MBSYS_HSDS_BEAMS; i++) {
			store->amplitude[i] = 0;
			store->echo_duration[i] = 0;
		}
		for (i = 0; i < 16; i++) {
			store->gain[i] = 0;
			store->echo_scale[i] = 0;
		}

		/* mean velocity (ERGNHYDI) */
		store->draught = 0.0;
		store->vel_mean = 0.0;
		store->vel_keel = 0.0;
		store->tide = 0.0;

		/* water velocity profile (HS_ERGNCTDS) */
		store->num_vel = 0.0;

		/* navigation source (ERGNPOSI) */
		store->pos_corr_x = 0.0;
		store->pos_corr_y = 0.0;
		strncpy(store->sensors, "\0", 8);

		/* comment (LDEOCMNT) */
		strncpy(store->comment, &datacomment[2], MBSYS_HSDS_MAXLINE);

		/* processed backscatter */
		store->back_scale = 0.0;
		for (i = 0; i < MBSYS_HSDS_BEAMS; i++)
			store->back[i] = 0;
	}

	/* 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);
}
Exemplo n.º 2
0
int main(int argc, char **argv) {
	char program_name[] = "mbminirovnav";
	char help_message[] = " MBminirov reads USBL tracking and CTD day files from the MBARI MiniROV\n"
                            "\tand produces a single ROV navigation file in one of the standard MBARI\n"
                            "\tformats handles preprocessing of swath sonar data as part of setting up\n"
                            "\tan MB-System processing structure for a dataset.\n";
	char usage_message[] = "mbminirovnav\n"
	                       "\t--help\n\n"
	                       "\t--input=fileroot\n"
	                       "\t--input-ctd-file=file\n"
	                       "\t--input-dvl-file=file\n"
	                       "\t--input-nav-file=file\n"
	                       "\t--input-rov-file=file\n"
						   "\t--interpolate-position\n"
						   "\t--interval=seconds\n"
	                       "\t--output=file\n"
						   "\t--rov-dive-start=yyyymmddhhmmss\n"
						   "\t--rov-dive-end=yyyymmddhhmmss\n"
						   "\t--utm-zone=zone_id/NorS\n"
	                       "\t--verbose\n\n";

	extern char *optarg;
	int option_index;
	int errflg = 0;
	int c;
	int help = MB_NO;
	
	/* ROV dive time start and end */
	int rov_dive_start_time_set = MB_NO;
	double rov_dive_start_time_d;
	int rov_dive_start_time_i[7];
	int rov_dive_end_time_set = MB_NO;
	double rov_dive_end_time_d;
	int rov_dive_end_time_i[7];
	int interpolate_position = MB_NO;

	/* MBIO status variables */
	int status = MB_SUCCESS;
	int verbose = 0;
	int error = MB_ERROR_NO_ERROR;
	char *message;
    
    int num_nav_alloc = 0;
    int num_nav = 0;
    double *nav_time_d = NULL;
    double *nav_lon = NULL;
    double *nav_lat = NULL;
   
    int num_ctd_alloc = 0;
    int num_ctd = 0;
    double *ctd_time_d = NULL;
    double *ctd_depth = NULL;
   
    int num_rov_alloc = 0;
    int num_rov = 0;
    double *rov_time_d = NULL;
    double *rov_heading = NULL;
    double *rov_roll = NULL;
    double *rov_pitch = NULL;
  
    int num_dvl_alloc = 0;
    int num_dvl = 0;
    double *dvl_time_d = NULL;
    double *dvl_altitude = NULL;
    double *dvl_stime = NULL;
    double *dvl_vx = NULL;
    double *dvl_vy = NULL;
    double *dvl_vz = NULL;
    double *dvl_status = NULL;
		
	double time_d;
	double rawlat, rawlon, dummydouble, ldegrees, lminutes;
	double reference_lon = 0.0;
	double reference_lat = 0.0;
	int utm_zone_set = MB_NO;
	int	utm_zone = 0;
	mb_path projection_id;
	void *pjptr = NULL;
	char NorS, EorW;
	mb_path dummystring;
	double ctd_C, ctd_T, ctd_D, ctd_S;
	double ctd_O2uM, ctd_O2raw, ctd_DGH_T, ctd_C2_T, ctd_C2_C;
	double rov_x, rov_y, rov_z, rov_yaw, rov_magna_amps;
	double rov_F1, rov_F2, rov_F3, rov_F4, rov_F5;
	double rov_Heading, rov_Pitch, rov_Roll;
	double dvl_Altitude, dvl_Stime, dvl_Vx, dvl_Vy, dvl_Vz, dvl_Status;

	double start_time_d = 0.0;
	double end_time_d = 0.0;
	double interval = 1.0;
	int onav_time_i[7], onav_time_j[5];
	int onav_year, onav_jday, onav_timetag;
	double num_output;
	double onav_time_d;
	double onav_lon;
	double onav_lat;
	double onav_easting;
	double onav_northing;
	double onav_depth;
	double onav_pressure;
	double onav_heading;
	double onav_altitude;
	double onav_pitch;
	double onav_roll;
	int onav_position_flag;
	int onav_pressure_flag;
	int onav_heading_flag;
	int onav_altitude_flag;
	int onav_attitude_flag;
    
	char buffer[MB_PATH_MAXLINE], *result;
	int nrecord;
	int nchar, nget, nscan;
	int ioutput;
	size_t size;
	FILE *fp;
	int jnav = 0;
	int jctd = 0;
	int jdvl = 0;
	int jrov = 0;
	int interp_status = MB_SUCCESS;
	int interp_error = MB_ERROR_NO_ERROR;
	int proj_status = 0;

	/* command line option definitions */
	/* mbminirovnav
	 * 		--verbose
	 * 		--help
	 *
	 * 		--input-nav=file
	 * 		--input-ctd=file
	 * 		--output=file
	 */
	static struct option options[] = {{"help", no_argument, NULL, 0},
	                                  {"input", required_argument, NULL, 0},
	                                  {"input-nav-file", required_argument, NULL, 0},
	                                  {"input-ctd-file", required_argument, NULL, 0},
	                                  {"input-dvl-file", required_argument, NULL, 0},
	                                  {"input-rov-file", required_argument, NULL, 0},
	                                  {"interpolate-position", no_argument, NULL, 0},
	                                  {"interval", required_argument, NULL, 0},
	                                  {"output", required_argument, NULL, 0},
	                                  {"rov-dive-start", required_argument, NULL, 0},
	                                  {"rov-dive-end", required_argument, NULL, 0},
	                                  {"utm-zone", required_argument, NULL, 0},
									  {"verbose", no_argument, NULL, 0},
	                                  {NULL, 0, NULL, 0}};

    /* files */
    mb_path input_root = "";
    mb_path input_nav_file = "";
    mb_path input_ctd_file = "";
    mb_path input_dvl_file = "";
    mb_path input_rov_file = "";
    mb_path output_file = "";

	/* process argument list */
	while ((c = getopt_long(argc, argv, "", options, &option_index)) != -1)
		switch (c) {
		/* long options all return c=0 */
		case 0:
			/* verbose */
			if (strcmp("verbose", options[option_index].name) == 0) {
				verbose++;
			}

			/* help */
			else if (strcmp("help", options[option_index].name) == 0) {
				help = MB_YES;
			}

			/*-------------------------------------------------------
			 * Define input and output files */

			/* input=file */
			else if (strcmp("input", options[option_index].name) == 0) {
				strcpy(input_root, optarg);
				sprintf(input_nav_file, "NAV_%s000000.txt", input_root);
				sprintf(input_ctd_file, "CTD_%s000000.txt", input_root);
				sprintf(input_dvl_file, "DVL_%s000000.txt", input_root);
				sprintf(input_rov_file, "ROV_%s000000.txt", input_root);
				sprintf(output_file, "MiniROV_nav_%s.mb165", input_root);
			}

			/* input-ctd=file */
			else if (strcmp("input-ctd-file", options[option_index].name) == 0) {
				strcpy(input_ctd_file, optarg);
			}

			/* input-dvl=file */
			else if (strcmp("input-dvl-file", options[option_index].name) == 0) {
				strcpy(input_dvl_file, optarg);
			}

			/* input-nav=file */
			else if (strcmp("input-nav-file", options[option_index].name) == 0) {
				strcpy(input_nav_file, optarg);
			}

			/* input-rov=file */
			else if (strcmp("input-rov-file", options[option_index].name) == 0) {
				strcpy(input_rov_file, optarg);
			}

			/* output=file */
			else if (strcmp("output", options[option_index].name) == 0) {
				strcpy(output_file, optarg);
			}

			/* interval */
			else if (strcmp("interval", options[option_index].name) == 0) {
				nscan = sscanf(optarg, "%lf", &interval);
				if (interval <= 0.0) {
					fprintf(stderr,"Program %s command error: %s %s\n\toutput interval reset to 1.0 seconds\n",
							program_name, options[option_index].name, optarg);
					
				}
			}

			/* start rov dive time */
			else if (strcmp("rov-dive-start", options[option_index].name) == 0) {
				nscan = sscanf(optarg, "%d/%d/%d/%d/%d/%d", &rov_dive_start_time_i[0],
					   &rov_dive_start_time_i[1], &rov_dive_start_time_i[2],
					   &rov_dive_start_time_i[3], &rov_dive_start_time_i[4],
					   &rov_dive_start_time_i[5]);
				if (nscan == 6) {
					rov_dive_start_time_i[6] = 0;
					mb_get_time(verbose, rov_dive_start_time_i, &rov_dive_start_time_d);
					rov_dive_start_time_set = MB_YES;
				}
				else {
					fprintf(stderr,"Program %s command error: %s %s\n",
							program_name, options[option_index].name, optarg);
				}
			}

			/* end rov dive time */
			else if (strcmp("rov-dive-end", options[option_index].name) == 0) {
				nscan = sscanf(optarg, "%d/%d/%d/%d/%d/%d", &rov_dive_end_time_i[0],
					   &rov_dive_end_time_i[1], &rov_dive_end_time_i[2],
					   &rov_dive_end_time_i[3], &rov_dive_end_time_i[4],
					   &rov_dive_end_time_i[5]);
				if (nscan == 6) {
					rov_dive_end_time_i[6] = 0;
					mb_get_time(verbose, rov_dive_end_time_i, &rov_dive_end_time_d);
					rov_dive_end_time_set = MB_YES;
				}
				else {
					fprintf(stderr,"Program %s command error: %s %s\n",
							program_name, options[option_index].name, optarg);
				}
			}

			/* utm zone */
			else if (strcmp("utm-zone", options[option_index].name) == 0) {
				nscan = sscanf(optarg, "%d/%c", &utm_zone, &NorS);
				if (nscan < 2)
					nscan = sscanf(optarg, "%d%c", &utm_zone, &NorS);
				if (nscan == 2) {
					utm_zone_set = MB_YES;
					if (NorS == 'N' || NorS == 'n')
						sprintf(projection_id, "UTM%2.2dN", utm_zone);
					else if (NorS == 'S' || NorS == 's')
						sprintf(projection_id, "UTM%2.2dS", utm_zone);
					else
						sprintf(projection_id, "UTM%2.2dN", utm_zone);
				}
				else {
					fprintf(stderr,"Program %s command error: %s %s\n",
							program_name, options[option_index].name, optarg);
				}
			}

			/* interpolate position over gaps in USBL fixes (rather than repeat position values) */
			else if (strcmp("interpolate-position", options[option_index].name) == 0) {
				interpolate_position = MB_YES;
			}

			/*----------------------------------------------------------------*/

			break;
		case '?':
			errflg++;
			break;
		}

	/* if error flagged then print it and exit */
	if (errflg) {
		fprintf(stderr, "usage: %s\n", usage_message);
		fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
		error = MB_ERROR_BAD_USAGE;
		exit(error);
	}

	/* print starting message */
	if (verbose == 1 || help) {
		fprintf(stderr, "\nProgram %s\n", program_name);
		fprintf(stderr, "Source File Version %s\n", version_id);
		fprintf(stderr, "MB-system Version %s\n", MB_VERSION);
	}

	/* print starting debug statements */
	if (verbose >= 2) {
		fprintf(stderr, "\ndbg2  Program <%s>\n", program_name);
		fprintf(stderr, "dbg2  Version %s\n", version_id);
		fprintf(stderr, "dbg2  MB-system Version %s\n", MB_VERSION);
		fprintf(stderr, "dbg2  Control Parameters:\n");
		fprintf(stderr, "dbg2       verbose:                      %d\n", verbose);
		fprintf(stderr, "dbg2       help:                         %d\n", help);
		fprintf(stderr, "dbg2       input_root:                   %s\n", input_root);
		fprintf(stderr, "dbg2       input_nav_file:               %s\n", input_nav_file);
		fprintf(stderr, "dbg2       input_ctd_file:               %s\n", input_ctd_file);
		fprintf(stderr, "dbg2       input_dvl_file:               %s\n", input_dvl_file);
		fprintf(stderr, "dbg2       input_rov_file:               %s\n", input_rov_file);
		fprintf(stderr, "dbg2       output_file:                  %s\n", output_file);
		fprintf(stderr, "dbg2       output time interval:         %f\n", interval);
		fprintf(stderr, "dbg2       rov_dive_start_time_set:      %d\n", rov_dive_start_time_set);
		if (rov_dive_start_time_set == MB_YES)
			fprintf(stderr, "dbg2       rov_dive_start_time_i:        %4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d.%6.6d\n",
					rov_dive_start_time_i[0], rov_dive_start_time_i[1], rov_dive_start_time_i[2],
					rov_dive_start_time_i[3], rov_dive_start_time_i[4], rov_dive_start_time_i[5],
					rov_dive_start_time_i[6]);
		fprintf(stderr, "dbg2       rov_dive_end_time_set:        %d\n", rov_dive_end_time_set);
		if (rov_dive_end_time_set == MB_YES)
			fprintf(stderr, "dbg2       rov_dive_end_time_i:          %4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d.%6.6d\n",
					rov_dive_end_time_i[0], rov_dive_end_time_i[1], rov_dive_end_time_i[2],
					rov_dive_end_time_i[3], rov_dive_end_time_i[4], rov_dive_end_time_i[5],
					rov_dive_end_time_i[6]);
		fprintf(stderr, "dbg2       utm_zone_set:                 %d\n", utm_zone_set);
		if (utm_zone_set == MB_YES) {
			fprintf(stderr, "dbg2       utm_zone:                     %d\n", utm_zone);
			fprintf(stderr, "dbg2       projection_id:                %s\n", projection_id);
		}
		fprintf(stderr, "dbg2       interpolate_position:         %d\n", interpolate_position);
	}

	/* print starting verbose */
	else if (verbose > 0) {
		fprintf(stderr, "\nProgram <%s>\n", program_name);
		fprintf(stderr, "Version %s\n", version_id);
		fprintf(stderr, "MB-system Version %s\n", MB_VERSION);
		fprintf(stderr, "Control Parameters:\n");
		fprintf(stderr, "     verbose:                      %d\n", verbose);
		fprintf(stderr, "     help:                         %d\n", help);
		fprintf(stderr, "     input_root:                   %s\n", input_root);
		fprintf(stderr, "     input_nav_file:               %s\n", input_nav_file);
		fprintf(stderr, "     input_ctd_file:               %s\n", input_ctd_file);
		fprintf(stderr, "     input_dvl_file:               %s\n", input_dvl_file);
		fprintf(stderr, "     input_rov_file:               %s\n", input_rov_file);
		fprintf(stderr, "     output_file:                  %s\n", output_file);
		fprintf(stderr, "     output time interval:         %f\n", interval);
		fprintf(stderr, "     rov_dive_start_time_set:      %d\n", rov_dive_start_time_set);
		if (rov_dive_start_time_set == MB_YES)
			fprintf(stderr, "     rov_dive_start_time_i:        %4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d.%6.6d\n",
					rov_dive_start_time_i[0], rov_dive_start_time_i[1], rov_dive_start_time_i[2],
					rov_dive_start_time_i[3], rov_dive_start_time_i[4], rov_dive_start_time_i[5],
					rov_dive_start_time_i[6]);
		fprintf(stderr, "     rov_dive_end_time_set:        %d\n", rov_dive_end_time_set);
		if (rov_dive_end_time_set == MB_YES)
			fprintf(stderr, "     rov_dive_end_time_i:          %4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d.%6.6d\n",
					rov_dive_end_time_i[0], rov_dive_end_time_i[1], rov_dive_end_time_i[2],
					rov_dive_end_time_i[3], rov_dive_end_time_i[4], rov_dive_end_time_i[5],
					rov_dive_end_time_i[6]);
		fprintf(stderr, "     utm_zone_set:                 %d\n", utm_zone_set);
		if (utm_zone_set == MB_YES) {
			fprintf(stderr, "     utm_zone:                     %d\n", utm_zone);
			fprintf(stderr, "     projection_id:                %s\n", projection_id);
		}
		fprintf(stderr, "     interpolate_position:         %d\n", interpolate_position);
	}

	/* if help desired then print it and exit */
	if (help) {
		fprintf(stderr, "\n%s\n", help_message);
		fprintf(stderr, "\nusage: %s\n", usage_message);
		exit(error);
	}

	/*-------------------------------------------------------------------*/
	/* load input nav data */

	/* count the records */
	error = MB_ERROR_NO_ERROR;
	nrecord = 0;
	nchar = MB_PATH_MAXLINE - 1;
	if ((fp = fopen(input_nav_file, "r")) != NULL) {
		/* loop over reading the records */
		while ((result = fgets(buffer, nchar, fp)) == buffer)
			if (buffer[0] != '#' && strlen(buffer) > 5)
				nrecord++;

		/* close the file */
		fclose(fp);
		fp = NULL;
	}
	else {
		error = MB_ERROR_OPEN_FAIL;
		status = MB_FAILURE;
	}

	/* allocate memory if necessary */
	if (status == MB_SUCCESS && num_nav_alloc < nrecord) {
		size = nrecord * sizeof(double);
		status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&nav_time_d, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&nav_lon, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&nav_lat, &error);
		if (status == MB_SUCCESS)
			num_nav_alloc = nrecord;
	}

	/* read the records */
	if (status == MB_SUCCESS) {
		nrecord = 0;
		if ((fp = fopen(input_nav_file, "r")) == NULL) {
			error = MB_ERROR_OPEN_FAIL;
			status = MB_FAILURE;
		}
		else {
			/* loop over reading the records - handle the different formats */
			while ((result = fgets(buffer, nchar, fp)) == buffer) {
				nget = sscanf(buffer, "%lf,$GPGLL,%lf,%c,%lf,%c,%lf,%s",
							  &time_d, &rawlat, &NorS, &rawlon, &EorW, &dummydouble, dummystring);
				if (nget == 7) {
					if (start_time_d <= 0.0)
						start_time_d = time_d;
					if (time_d > 0.0 && time_d < start_time_d)
						start_time_d = time_d;
					if (time_d > end_time_d)
						end_time_d = time_d;
						
					nav_time_d[num_nav] = time_d;
					ldegrees = floor(rawlat / 100.0);
					lminutes = rawlat - ldegrees * 100;
					nav_lat[num_nav] = ldegrees + (lminutes / 60.0);
					if (NorS == 'S')
						nav_lat[num_nav] *= -1;
					ldegrees = floor(rawlon / 100.0);
					lminutes = rawlon - ldegrees * 100;
					nav_lon[num_nav] = ldegrees + (lminutes / 60.0);
					if (EorW == 'W')
						nav_lon[num_nav] *= -1;
					
					if (interpolate_position == MB_NO
						|| num_nav <= 1
						|| nav_lon[num_nav] != nav_lon[num_nav-1]
						|| nav_lat[num_nav] != nav_lat[num_nav-1]) {
						reference_lon += nav_lon[num_nav];
						reference_lat += nav_lat[num_nav];
						if (num_nav < num_nav_alloc)
							num_nav++;
					}
						
					
				}
			}

			/* close the file */
			fclose(fp);
			
			/* calculate average longitude for UTM zone calcuation */
			if (num_nav > 0) {
				reference_lon /= num_nav;
				reference_lat /= num_nav;
			}
			if (reference_lon < 180.0)
				reference_lon += 360.0;
			if (reference_lon >= 180.0)
				reference_lon -= 360.0;
		}
	}

	/*-------------------------------------------------------------------*/
	/* load input ctd data */

	/* count the records */
	error = MB_ERROR_NO_ERROR;
	nrecord = 0;
	nchar = MB_PATH_MAXLINE - 1;
	if ((fp = fopen(input_ctd_file, "r")) != NULL) {
		/* loop over reading the records */
		while ((result = fgets(buffer, nchar, fp)) == buffer)
			if (buffer[0] != '#' && strlen(buffer) > 5)
				nrecord++;

		/* close the file */
		fclose(fp);
		fp = NULL;
	}
	else {
		error = MB_ERROR_OPEN_FAIL;
		status = MB_FAILURE;
	}

	/* allocate memory if necessary */
	if (status == MB_SUCCESS && num_ctd_alloc < nrecord) {
		size = nrecord * sizeof(double);
		status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&ctd_time_d, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&ctd_depth, &error);
		if (status == MB_SUCCESS)
			num_ctd_alloc = nrecord;
	}

	/* read the records */
	if (status == MB_SUCCESS) {
		nrecord = 0;
		if ((fp = fopen(input_ctd_file, "r")) == NULL) {
			error = MB_ERROR_OPEN_FAIL;
			status = MB_FAILURE;
		}
		else {
			/* loop over reading the records - handle the different formats */
			while ((result = fgets(buffer, nchar, fp)) == buffer) {
				nget = sscanf(buffer, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
							  &time_d, &ctd_C, &ctd_T, &ctd_D, &ctd_S,
							  &ctd_O2uM, &ctd_O2raw, &ctd_DGH_T, &ctd_C2_T, &ctd_C2_C);
				if (nget == 10) {
					if (start_time_d <= 0.0)
						start_time_d = time_d;
					if (time_d > 0.0 && time_d < start_time_d)
						start_time_d = time_d;
					if (time_d > end_time_d)
						end_time_d = time_d;
						
					ctd_time_d[num_ctd] = time_d;
					ctd_depth[num_ctd] = ctd_D;
					if (num_ctd < num_ctd_alloc)
						num_ctd++;
					}
				}

			/* close the file */
			fclose(fp);
		}
	}

	/*-------------------------------------------------------------------*/
	/* load input rov data */

	/* count the records */
	error = MB_ERROR_NO_ERROR;
	nrecord = 0;
	nchar = MB_PATH_MAXLINE - 1;
	if ((fp = fopen(input_rov_file, "r")) != NULL) {
		/* loop over reading the records */
		while ((result = fgets(buffer, nchar, fp)) == buffer)
			if (buffer[0] != '#' && strlen(buffer) > 5)
				nrecord++;

		/* close the file */
		fclose(fp);
		fp = NULL;
	}
	else {
		error = MB_ERROR_OPEN_FAIL;
		status = MB_FAILURE;
	}

	/* allocate memory if necessary */
	if (status == MB_SUCCESS && num_rov_alloc < nrecord) {
		size = nrecord * sizeof(double);
		status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&rov_time_d, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&rov_heading, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&rov_roll, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&rov_pitch, &error);
		if (status == MB_SUCCESS)
			num_rov_alloc = nrecord;
	}

	/* read the records */
	if (status == MB_SUCCESS) {
		nrecord = 0;
		if ((fp = fopen(input_rov_file, "r")) == NULL) {
			error = MB_ERROR_OPEN_FAIL;
			status = MB_FAILURE;
		}
		else {
			/* loop over reading the records - handle the different formats */
			while ((result = fgets(buffer, nchar, fp)) == buffer) {
				nget = sscanf(buffer, "%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf,%lf",
							  &time_d, &rov_x, &rov_y, &rov_z, &rov_yaw, &rov_magna_amps,
							  &rov_F1, &rov_F2, &rov_F3, &rov_F4, &rov_F5,
							  &rov_Heading, &rov_Pitch, &rov_Roll);
				if (nget == 14) {
					if (start_time_d <= 0.0)
						start_time_d = time_d;
					if (time_d > 0.0 && time_d < start_time_d)
						start_time_d = time_d;
					if (time_d > end_time_d)
						end_time_d = time_d;
						
					rov_time_d[num_rov] = time_d;
					rov_heading[num_rov] = rov_Heading;
					rov_roll[num_rov] = rov_Roll;
					rov_pitch[num_rov] = rov_Pitch;
					if (num_rov < num_rov_alloc)
						num_rov++;
					}
				}

			/* close the file */
			fclose(fp);
		}
	}

	/*-------------------------------------------------------------------*/
	/* load input dvl data */

	/* count the records */
	error = MB_ERROR_NO_ERROR;
	nrecord = 0;
	nchar = MB_PATH_MAXLINE - 1;
	if ((fp = fopen(input_dvl_file, "r")) != NULL) {
		/* loop over reading the records */
		while ((result = fgets(buffer, nchar, fp)) == buffer)
			if (buffer[0] != '#' && strlen(buffer) > 5)
				nrecord++;

		/* close the file */
		fclose(fp);
		fp = NULL;
	}
	else {
		error = MB_ERROR_OPEN_FAIL;
		status = MB_FAILURE;
	}

	/* allocate memory if necessary */
	if (status == MB_SUCCESS && num_dvl_alloc < nrecord) {
		size = nrecord * sizeof(double);
		status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&dvl_time_d, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&dvl_altitude, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&dvl_stime, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&dvl_vx, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&dvl_vy, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&dvl_vz, &error);
		if (status == MB_SUCCESS)
			status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&dvl_status, &error);
		if (status == MB_SUCCESS)
			num_dvl_alloc = nrecord;
	}

	/* read the records */
	if (status == MB_SUCCESS) {
		nrecord = 0;
		if ((fp = fopen(input_dvl_file, "r")) == NULL) {
			error = MB_ERROR_OPEN_FAIL;
			status = MB_FAILURE;
		}
		else {
			/* loop over reading the records - handle the different formats */
			while ((result = fgets(buffer, nchar, fp)) == buffer) {
				nget = sscanf(buffer, "%lf,%lf,%lf,%lf,%lf,%lf,%lf",
							  &time_d, &dvl_Altitude, &dvl_Stime, &dvl_Vx, &dvl_Vy, &dvl_Vz, &dvl_Status);
				if (nget == 7) {
					if (start_time_d <= 0.0)
						start_time_d = time_d;
					if (time_d > 0.0 && time_d < start_time_d)
						start_time_d = time_d;
					if (time_d > end_time_d)
						end_time_d = time_d;
						
					dvl_time_d[num_dvl] = time_d;
					dvl_altitude[num_dvl] = dvl_Altitude;
					dvl_stime[num_dvl] = dvl_Stime;
					dvl_vx[num_dvl] = dvl_Vx;
					dvl_vy[num_dvl] = dvl_Vy;
					dvl_vz[num_dvl] = dvl_Vz;
					dvl_status[num_dvl] = dvl_Status;
					if (num_dvl < num_dvl_alloc)
						num_dvl++;
					}
				}

			/* close the file */
			fclose(fp);
		}
	}
	/*-------------------------------------------------------------------*/
	
	fprintf(stderr,"\nProgram %s\n", program_name);
	fprintf(stderr,"Input data loaded:\n\tNavigation: %d\n\tCTD: %d\n\tAttitude:%d\n\tDVL: %d\n\n",
			num_nav, num_ctd, num_rov, num_dvl);
	
	/* get time range of output based on max bounds of any input data
		or use the specified time interval */
	if (rov_dive_start_time_set == MB_YES) {
		start_time_d = rov_dive_start_time_d;
	}
	if (rov_dive_end_time_set == MB_YES) {
		end_time_d = rov_dive_end_time_d;
	}
	start_time_d = floor(start_time_d);
	num_output = (int)(ceil((end_time_d - start_time_d) / interval));
	
	/* get UTM projection for easting and northing fields */
	if (utm_zone_set == MB_YES) {
		if (utm_zone < 0)
			sprintf(projection_id, "UTM%2.2dS", abs(utm_zone));
		else
			sprintf(projection_id, "UTM%2.2dN", utm_zone);
	}
	else {
		utm_zone = (int)(((reference_lon + 183.0) / 6.0) + 0.5);
		if (reference_lat >= 0.0)
			sprintf(projection_id, "UTM%2.2dN", utm_zone);
		else
			sprintf(projection_id, "UTM%2.2dS", utm_zone);
	}
	proj_status = mb_proj_init(verbose, projection_id, &(pjptr), &error);

	/* write the MiniROV navigation data */
	if (status == MB_SUCCESS) {
		if ((fp = fopen(output_file, "w")) == NULL) {
			error = MB_ERROR_OPEN_FAIL;
			status = MB_FAILURE;
		}
		else {
			/* loop over 1 second intervals from start time to end time */
			for (ioutput=0;ioutput<num_output;ioutput++) {
				
				/* set the output time */
				onav_time_d = start_time_d + ioutput * interval;
				mb_get_date(verbose, onav_time_d, onav_time_i);
				onav_year = onav_time_i[0];
				onav_timetag = 10000 * onav_time_i[3] + 100 * onav_time_i[4] + onav_time_i[5];
				mb_get_jtime(verbose, onav_time_i, onav_time_j);
				onav_jday = onav_time_j[1];
				
				/* interpolate values onto the target time */
				interp_status = mb_linear_interp_longitude(verbose, nav_time_d - 1, nav_lon - 1, num_nav, onav_time_d, &onav_lon, &jnav, &interp_error);
				interp_status = mb_linear_interp_latitude(verbose, nav_time_d - 1, nav_lat - 1, num_nav, onav_time_d, &onav_lat, &jnav, &interp_error);
				interp_status = mb_linear_interp(verbose, ctd_time_d - 1, ctd_depth - 1, num_ctd, onav_time_d, &onav_depth, &jctd, &interp_error);
				interp_status = mb_linear_interp(verbose, dvl_time_d - 1, dvl_altitude - 1, num_dvl, onav_time_d, &onav_altitude, &jdvl, &interp_error);
				interp_status = mb_linear_interp_heading(verbose, rov_time_d - 1, rov_heading - 1, num_rov, onav_time_d, &onav_heading, &jrov, &interp_error);
				interp_status = mb_linear_interp(verbose, rov_time_d - 1, rov_roll - 1, num_rov, onav_time_d, &onav_roll, &jrov, &interp_error);
				interp_status = mb_linear_interp(verbose, rov_time_d - 1, rov_pitch - 1, num_rov, onav_time_d, &onav_pitch, &jrov, &interp_error);
				
				if (onav_lon != 0.0 && onav_lat != 0.0)
					onav_position_flag = MB_YES;
				else
					onav_position_flag = MB_NO;
				if (onav_lon != 0.0 && onav_lat != 0.0)
					onav_pressure_flag = MB_YES;
				else
					onav_pressure_flag = MB_NO;
				if (onav_lon != 0.0 && onav_lat != 0.0)
					onav_heading_flag = MB_YES;
				else
					onav_heading_flag = MB_NO;
				if (onav_lon != 0.0 && onav_lat != 0.0)
					onav_altitude_flag = MB_YES;
				else
					onav_altitude_flag = MB_NO;
				if (onav_lon != 0.0 && onav_lat != 0.0)
					onav_attitude_flag = MB_YES;
				else
					onav_attitude_flag = MB_NO;
				
				/* get UTM eastings and northings */
				mb_proj_forward(verbose, pjptr, onav_lon, onav_lat, &onav_easting, &onav_northing, &error);

				/* get pressure from CTD depth */
				onav_pressure = onav_depth * (1.0052405 * (1 + 5.28E-3 * sin(DTR * onav_lat) * sin(DTR * onav_lat)));
		
				/* print output debug statements */
				if (verbose >= 4) {
					fprintf(stderr, "\ndbg4  Data to be written in MBIO function <%s>\n", program_name);
					fprintf(stderr, "dbg4  Values,read:\n");
					fprintf(stderr, "dbg4       onav_time_d:         %f\n", onav_time_d);
					fprintf(stderr, "dbg4       onav_lat:            %f\n", onav_lat);
					fprintf(stderr, "dbg4       onav_lon:            %f\n", onav_lon);
					fprintf(stderr, "dbg4       onav_easting:        %f\n", onav_easting);
					fprintf(stderr, "dbg4       onav_northing:       %f\n", onav_northing);
					fprintf(stderr, "dbg4       onav_depth:          %f\n", onav_depth);
					fprintf(stderr, "dbg4       onav_pressure:       %f\n", onav_pressure);
					fprintf(stderr, "dbg4       onav_heading:        %f\n", onav_heading);
					fprintf(stderr, "dbg4       onav_altitude:       %f\n", onav_altitude);
					fprintf(stderr, "dbg4       onav_pitch:          %f\n", onav_pitch);
					fprintf(stderr, "dbg4       onav_roll:           %f\n", onav_roll);
					fprintf(stderr, "dbg4       onav_position_flag:  %d\n", onav_position_flag);
					fprintf(stderr, "dbg4       onav_pressure_flag:  %d\n", onav_pressure_flag);
					fprintf(stderr, "dbg4       onav_heading_flag:   %d\n", onav_heading_flag);
					fprintf(stderr, "dbg4       onav_altitude_flag:  %d\n", onav_altitude_flag);
					fprintf(stderr, "dbg4       onav_attitude_flag:  %d\n", onav_attitude_flag);
					fprintf(stderr, "dbg4       error:               %d\n", error);
					fprintf(stderr, "dbg4       status:              %d\n", status);
				}
				
				/* write the record */
				fprintf(fp, "%4.4d,%3.3d,%6.6d,%9.0f,%10.6f,%11.6f,%7.0f,%7.0f,%7.2f,%5.1f,%6.2f,%4.1f,%4.1f,%d,%d,%d,%d,%d\n",
						onav_year, onav_jday, onav_timetag, onav_time_d,
						onav_lat, onav_lon, onav_easting, onav_northing,
						onav_pressure, onav_heading, onav_altitude, onav_pitch, onav_roll,
						onav_position_flag, onav_pressure_flag, onav_heading_flag,
						onav_altitude_flag, onav_attitude_flag);
				
			}

			/* close the file */
			fclose(fp);
		}
	}
	
	/*-------------------------------------------------------------------*/
	
	proj_status = mb_proj_free(verbose, &(pjptr), &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&nav_time_d, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&nav_lon, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&nav_lat, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&ctd_time_d, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&ctd_depth, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&rov_time_d, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&rov_heading, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&rov_roll, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&rov_pitch, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&dvl_time_d, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&dvl_altitude, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&dvl_stime, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&dvl_vx, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&dvl_vy, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&dvl_vz, &error);
	mb_freed(verbose, __FILE__, __LINE__, (void **)&dvl_status, &error);
	
	exit(0);
	
	/*-------------------------------------------------------------------*/

}
Exemplo n.º 3
0
/*--------------------------------------------------------------------*/
int mbr_mgd77dat_rd_data(int verbose, void *mbio_ptr, int *error) {
	char *function_name = "mbr_mgd77dat_rd_data";
	int status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbf_mgd77dat_struct *data;
	int *header_read;
	char line[MBF_MGD77DAT_DATA_LEN] = "";
	size_t read_len;
	size_t skip;
	int shift;
	int neg_unit;
	int itmp;
	double dtmp;
	int i, j;

	/* print input debug statements */
	if (verbose >= 2) {
		fprintf(stderr, "\ndbg2  MBIO function <%s> called\n", function_name);
		fprintf(stderr, "dbg2  Input arguments:\n");
		fprintf(stderr, "dbg2       verbose:    %d\n", verbose);
		fprintf(stderr, "dbg2       mbio_ptr:   %p\n", (void *)mbio_ptr);
	}

	/* get pointer to mbio descriptor */
	mb_io_ptr = (struct mb_io_struct *)mbio_ptr;

	/* get pointer to raw data structure */
	data = (struct mbf_mgd77dat_struct *)mb_io_ptr->raw_data;
	header_read = (int *)&mb_io_ptr->save1;

	/* initialize everything to zeros */
	mbr_zero_mgd77dat(verbose, mb_io_ptr->raw_data, error);

	/* set file position */
	mb_io_ptr->file_bytes = ftell(mb_io_ptr->mbfp);
	mb_io_ptr->file_pos = mb_io_ptr->file_bytes;

	/* read next record */
	if ((read_len = fread(line, 1, MBF_MGD77DAT_DATA_LEN, mb_io_ptr->mbfp)) == MBF_MGD77DAT_DATA_LEN) {
		mb_io_ptr->file_bytes += read_len;
		status = MB_SUCCESS;
		*error = MB_ERROR_NO_ERROR;
	}
	else {
		mb_io_ptr->file_bytes += read_len;
		status = MB_FAILURE;
		*error = MB_ERROR_EOF;
	}
	/*fprintf(stderr,"_RAWLINE:");
	for (i=0;i<MBF_MGD77DAT_DATA_LEN;i++)
	fprintf(stderr,"%c",line[i]);
	fprintf(stderr,"\n");*/

	/* handle "pseudo-mgd77" in which each record is
	 * followed by a cr or lf or both */
	skip = 0;
	if (line[0] == '\r' || line[0] == '\n')
		skip++;
	if (line[1] == '\r' || line[1] == '\n')
		skip++;
	if (skip > 0) {
		for (j = 0; j < MBF_MGD77DAT_DATA_LEN - skip; j++) {
			line[j] = line[j + skip];
		}
		if ((read_len = fread(&line[MBF_MGD77DAT_DATA_LEN - skip], 1, skip, mb_io_ptr->mbfp)) == skip) {
			mb_io_ptr->file_bytes += read_len;
			status = MB_SUCCESS;
			*error = MB_ERROR_NO_ERROR;
		}
		else {
			mb_io_ptr->file_bytes += read_len;
			status = MB_FAILURE;
			*error = MB_ERROR_EOF;
		}
		/*fprintf(stderr,"---SKIPPED %zu BYTES---\n",skip);*/
	}
	/*fprintf(stderr,"+FIXLINE:");
	for (i=0;i<MBF_MGD77DAT_DATA_LEN;i++)
	fprintf(stderr,"%c",line[i]);
	fprintf(stderr,"\n");*/

	mb_io_ptr->file_bytes = ftell(mb_io_ptr->mbfp);

	/* handle the data */
	if (status == MB_SUCCESS && *header_read > 0 && *header_read < MBF_MGD77DAT_HEADER_NUM) {
		data->kind = MB_DATA_HEADER;
		(*header_read)++;
		for (i = 0; i < MBF_MGD77DAT_DATA_LEN; i++)
			data->comment[i] = line[i];
	}
	else if (status == MB_SUCCESS && (line[0] == '1' || line[0] == '4')) {
		data->kind = MB_DATA_HEADER;
		(*header_read) = 1;
		for (i = 0; i < MBF_MGD77DAT_DATA_LEN; i++)
			data->comment[i] = line[i];
	}
	else if (status == MB_SUCCESS && line[0] == '#') {
		data->kind = MB_DATA_COMMENT;
		strncpy(data->comment, &line[1], MBF_MGD77DAT_DATA_LEN - 1);
	}
	else if (status == MB_SUCCESS && line[0] == '3') {
		data->kind = MB_DATA_DATA;

		/* get survey id */
		shift = 1;
		for (i = 0; i < 8; i++)
			data->survey_id[i] = line[i + shift];

		/* get time */
		shift += 8;
		mb_get_int(&data->timezone, &line[shift], 5);
		shift += 5;
		data->timezone = data->timezone / 100;
		mb_get_int(&itmp, &line[shift], 2);
		shift += 2;
		mb_fix_y2k(verbose, itmp, &data->time_i[0]);
		mb_get_int(&data->time_i[1], &line[shift], 2);
		shift += 2;
		mb_get_int(&data->time_i[2], &line[shift], 2);
		shift += 2;
		mb_get_int(&data->time_i[3], &line[shift], 2);
		shift += 2;
		mb_get_int(&itmp, &line[shift], 5);
		shift += 5;
		data->time_i[4] = 0.001 * itmp;
		dtmp = (itmp - 1000 * data->time_i[4]) * 0.06;
		data->time_i[5] = (int)dtmp;
		data->time_i[6] = 1000000 * (dtmp - data->time_i[5]);
		mb_get_time(verbose, data->time_i, &data->time_d);

		/* get nav */
		neg_unit = 8;
		if (line[shift] == '-') {
			shift += 1;
			neg_unit = 7;
		}
		mb_get_int(&itmp, &line[shift], neg_unit);
		shift += neg_unit;
		data->latitude = 0.00001 * itmp;
		if (neg_unit == 7)
			data->latitude = -data->latitude;

		neg_unit = 9;
		if (line[shift] == '-') {
			shift += 1;
			neg_unit = 8;
		}
		mb_get_int(&itmp, &line[shift], neg_unit);
		shift += neg_unit;
		data->longitude = 0.00001 * itmp;
		if (neg_unit == 8)
			data->longitude = -data->longitude;
		mb_get_int(&data->nav_type, &line[shift], 1);
		shift += 1;

		/* get bath */
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->tt = 0.0001 * itmp;
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->bath = 0.1 * itmp;
		mb_get_int(&data->bath_corr, &line[shift], 2);
		shift += 2;
		mb_get_int(&data->bath_type, &line[shift], 1);
		shift += 1;
		if (data->bath > 0.0 && data->bath < 99999.9) {
			data->flag = MB_FLAG_NONE;
		}
		else {
			data->flag = MB_FLAG_NULL;
		}

		/* get magnetics */
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->mag_tot_1 = 0.1 * itmp;
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->mag_tot_2 = 0.1 * itmp;
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->mag_res = 0.1 * itmp;
		mb_get_int(&data->mag_res_sensor, &line[shift], 1);
		shift += 1;
		mb_get_int(&itmp, &line[shift], 5);
		shift += 5;
		data->mag_diurnal = 0.1 * itmp;
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->mag_altitude = itmp;

		/* get gravity */
		mb_get_int(&itmp, &line[shift], 7);
		shift += 7;
		data->gravity = 0.1 * itmp;
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->eotvos = 0.1 * itmp;
		mb_get_int(&itmp, &line[shift], 5);
		shift += 5;
		data->free_air = 0.1 * itmp;
		mb_get_int(&data->seismic_line, &line[shift], 5);
		shift += 5;
		mb_get_int(&data->seismic_shot, &line[shift], 6);
		shift += 6;

		/* get nav quality */
		mb_get_int(&data->nav_quality, &line[shift], 1);
		shift += 1;
	}
	else if (status == MB_SUCCESS && line[0] == '5') {
		data->kind = MB_DATA_DATA;

		/* get survey id */
		shift = 1;
		for (i = 0; i < 8; i++)
			data->survey_id[i] = line[i + shift];

		/* get time */
		shift += 8;
		mb_get_int(&data->timezone, &line[shift], 3);
		shift += 3;
		mb_get_int(&data->time_i[0], &line[shift], 4);
		shift += 4;
		mb_get_int(&data->time_i[1], &line[shift], 2);
		shift += 2;
		mb_get_int(&data->time_i[2], &line[shift], 2);
		shift += 2;
		mb_get_int(&data->time_i[3], &line[shift], 2);
		shift += 2;
		mb_get_int(&itmp, &line[shift], 5);
		shift += 5;
		data->time_i[4] = 0.001 * itmp;
		dtmp = (itmp - 1000 * data->time_i[4]) * 0.06;
		data->time_i[5] = (int)dtmp;
		data->time_i[6] = 1000000 * (dtmp - data->time_i[5]);

		mb_get_time(verbose, data->time_i, &data->time_d);

		/* get nav */
		neg_unit = 8;
		if (line[shift] == '-') {
			shift += 1;
			neg_unit = 7;
		}
		mb_get_int(&itmp, &line[shift], neg_unit);
		shift += neg_unit;
		data->latitude = 0.00001 * itmp;
		if (neg_unit == 7)
			data->latitude = -data->latitude;

		neg_unit = 9;
		if (line[shift] == '-') {
			shift += 1;
			neg_unit = 8;
		}
		mb_get_int(&itmp, &line[shift], neg_unit);
		shift += neg_unit;
		data->longitude = 0.00001 * itmp;
		if (neg_unit == 8)
			data->longitude = -data->longitude;

		mb_get_int(&data->nav_type, &line[shift], 1);
		shift += 1;

		/* get bath */
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->tt = 0.0001 * itmp;
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->bath = 0.1 * itmp;
		mb_get_int(&data->bath_corr, &line[shift], 2);
		shift += 2;
		mb_get_int(&data->bath_type, &line[shift], 1);
		shift += 1;
		if (data->bath > 0.0 && data->bath < 99999.9) {
			data->flag = MB_FLAG_NONE;
		}
		else {
			data->flag = MB_FLAG_NULL;
		}

		/* get magnetics */
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->mag_tot_1 = 0.1 * itmp;
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->mag_tot_2 = 0.1 * itmp;
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->mag_res = 0.1 * itmp;
		mb_get_int(&data->mag_res_sensor, &line[shift], 1);
		shift += 1;
		mb_get_int(&itmp, &line[shift], 5);
		shift += 5;
		data->mag_diurnal = 0.1 * itmp;
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->mag_altitude = itmp;

		/* get gravity */
		mb_get_int(&itmp, &line[shift], 7);
		shift += 7;
		data->gravity = 0.1 * itmp;
		mb_get_int(&itmp, &line[shift], 6);
		shift += 6;
		data->eotvos = 0.1 * itmp;
		mb_get_int(&itmp, &line[shift], 5);
		shift += 5;
		data->free_air = 0.1 * itmp;
		mb_get_int(&data->seismic_line, &line[shift], 5);
		shift += 5;
		mb_get_int(&data->seismic_shot, &line[shift], 6);
		shift += 6;

		/* get nav quality */
		mb_get_int(&data->nav_quality, &line[shift], 1);
		/* shift += 1; */
	}

	/* 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);
}
Exemplo n.º 4
0
/*--------------------------------------------------------------------*/
int mbr_mbpronav_rd_data(int verbose, void *mbio_ptr, int *error) {
	char *function_name = "mbr_mbpronav_rd_data";
	int status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbf_mbpronav_struct *data;
	char line[MBF_MBPRONAV_MAXLINE + 1] = "";
	char *line_ptr;
	int nread;
	double sec;
	double d1, d2, d3, d4, d5;
	double d6, d7, d8, d9;
	double d10, d11, d12, d13;

	/* print input debug statements */
	if (verbose >= 2) {
		fprintf(stderr, "\ndbg2  MBIO function <%s> called\n", function_name);
		fprintf(stderr, "dbg2  Input arguments:\n");
		fprintf(stderr, "dbg2       verbose:    %d\n", verbose);
		fprintf(stderr, "dbg2       mbio_ptr:   %p\n", (void *)mbio_ptr);
	}

	/* get pointer to mbio descriptor */
	mb_io_ptr = (struct mb_io_struct *)mbio_ptr;

	/* get pointer to raw data structure */
	data = (struct mbf_mbpronav_struct *)mb_io_ptr->raw_data;

	/* initialize everything to zeros */
	mbr_zero_mbpronav(verbose, mb_io_ptr->raw_data, error);

	/* set file position */
	mb_io_ptr->file_bytes = ftell(mb_io_ptr->mbfp);
	mb_io_ptr->file_pos = mb_io_ptr->file_bytes;

	/* read next record */
	if ((line_ptr = fgets(line, MBF_MBPRONAV_MAXLINE, mb_io_ptr->mbfp)) != NULL) {
		mb_io_ptr->file_bytes += strlen(line);
		status = MB_SUCCESS;
		*error = MB_ERROR_NO_ERROR;
	}
	else {
		status = MB_FAILURE;
		*error = MB_ERROR_EOF;
	}
	mb_io_ptr->file_bytes = ftell(mb_io_ptr->mbfp);

	/* handle the data */
	if (status == MB_SUCCESS && line[0] == '#') {
		data->kind = MB_DATA_COMMENT;
		strncpy(data->comment, &line[1], MBF_MBPRONAV_MAXLINE);
	}
	else if (status == MB_SUCCESS) {
		data->kind = MB_DATA_DATA;

		/* read data */
		nread = sscanf(line, "%d %d %d %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", &data->time_i[0],
		               &data->time_i[1], &data->time_i[2], &data->time_i[3], &data->time_i[4], &sec, &d1, &d2, &d3, &d4, &d5, &d6,
		               &d7, &d8, &d9, &d10, &d11, &d12, &d13);
		data->time_i[5] = (int)sec;
		data->time_i[6] = 1000000.0 * (sec - data->time_i[5]);
		if (nread >= 9) {
			mb_get_time(verbose, data->time_i, &data->time_d);
			data->longitude = d2;
			data->latitude = d3;
			data->heading = 0.0;
			data->speed = 0.0;
			data->sonardepth = 0.0;
			data->roll = 0.0;
			data->pitch = 0.0;
			data->heave = 0.0;
			data->portlon = 0.0;
			data->portlat = 0.0;
			data->stbdlon = 0.0;
			data->stbdlat = 0.0;
			status = MB_SUCCESS;
			*error = MB_ERROR_NO_ERROR;
		}
		if (nread >= 10)
			data->heading = d4;
		if (nread >= 11)
			data->speed = d5;
		if (nread >= 12)
			data->sonardepth = d6;
		if (nread >= 15) {
			data->roll = d7;
			data->pitch = d8;
			data->heave = d9;
		}
		if (nread >= 17) {
			data->portlon = d10;
			data->portlat = d11;
		}
		if (nread >= 19) {
			data->stbdlon = d12;
			data->stbdlat = d13;
		}

		/* get time set if only one of two variables is defined */
		if (data->time_i[0] == 0 && data->time_d > 0.0)
			mb_get_date(verbose, data->time_d, data->time_i);
		else if (data->time_i[0] > 0 && data->time_d == 0.0)
			mb_get_time(verbose, data->time_i, &(data->time_d));

		if (status == MB_SUCCESS) {
			/* print output debug statements */
			if (verbose >= 4) {
				fprintf(stderr, "\ndbg4  Data read in MBIO function <%s>\n", function_name);
				fprintf(stderr, "dbg4  Values,read:\n");
				fprintf(stderr, "dbg4       time_i[0]:      %d\n", data->time_i[0]);
				fprintf(stderr, "dbg4       time_i[1]:      %d\n", data->time_i[1]);
				fprintf(stderr, "dbg4       time_i[2]:      %d\n", data->time_i[2]);
				fprintf(stderr, "dbg4       time_i[3]:      %d\n", data->time_i[3]);
				fprintf(stderr, "dbg4       time_i[4]:      %d\n", data->time_i[4]);
				fprintf(stderr, "dbg4       time_i[5]:      %d\n", data->time_i[5]);
				fprintf(stderr, "dbg4       time_i[6]:      %d\n", data->time_i[6]);
				fprintf(stderr, "dbg4       time_d:         %f\n", data->time_d);
				fprintf(stderr, "dbg4       latitude:       %f\n", data->latitude);
				fprintf(stderr, "dbg4       longitude:      %f\n", data->longitude);
				fprintf(stderr, "dbg4       heading:        %f\n", data->heading);
				fprintf(stderr, "dbg4       speed:          %f\n", data->speed);
				fprintf(stderr, "dbg4       sonardepth:     %f\n", data->sonardepth);
				fprintf(stderr, "dbg4       roll:           %f\n", data->roll);
				fprintf(stderr, "dbg4       pitch:          %f\n", data->pitch);
				fprintf(stderr, "dbg4       heave:          %f\n", data->heave);
				fprintf(stderr, "dbg4       portlon:        %f\n", data->portlon);
				fprintf(stderr, "dbg4       portlat:        %f\n", data->portlat);
				fprintf(stderr, "dbg4       stbdlon:        %f\n", data->stbdlon);
				fprintf(stderr, "dbg4       stbdlat:        %f\n", data->stbdlat);
				fprintf(stderr, "dbg4       error:          %d\n", *error);
				fprintf(stderr, "dbg4       status:         %d\n", status);
			}
		}

		else {
			status = MB_FAILURE;
			*error = MB_ERROR_UNINTELLIGIBLE;
		}
	}

	/* 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);
}
Exemplo n.º 5
0
/*--------------------------------------------------------------------*/
int mbr_rt_hir2rnav(int verbose, void *mbio_ptr, void *store_ptr, int *error) {
	char *function_name = "mbr_rt_hir2rnav";
	int status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbsys_singlebeam_struct *store;
	char line[MB_COMMENT_MAXLINE] = "";
	char *line_ptr;
	int *read_count;
	int nget;
	double sec;

	/* print input debug statements */
	if (verbose >= 2) {
		fprintf(stderr, "\ndbg2  MBIO function <%s> called\n", function_name);
		fprintf(stderr, "dbg2  Revision id: %s\n", rcs_id);
		fprintf(stderr, "dbg2  Input arguments:\n");
		fprintf(stderr, "dbg2       verbose:    %d\n", verbose);
		fprintf(stderr, "dbg2       mbio_ptr:   %p\n", (void *)mbio_ptr);
		fprintf(stderr, "dbg2       store_ptr:  %p\n", (void *)store_ptr);
	}

	/* get pointers to mbio descriptor and data structure */
	mb_io_ptr = (struct mb_io_struct *)mbio_ptr;
	store = (struct mbsys_singlebeam_struct *)store_ptr;

	/* get pointer to read counter */
	read_count = (int *)&mb_io_ptr->save1;

	/* set file position */
	mb_io_ptr->file_bytes = ftell(mb_io_ptr->mbfp);
	mb_io_ptr->file_pos = mb_io_ptr->file_bytes;

	/* read next record */
	if ((line_ptr = fgets(line, MB_PATH_MAXLINE, mb_io_ptr->mbfp)) != NULL) {
		/* set status */
		mb_io_ptr->file_bytes += strlen(line);
		status = MB_SUCCESS;
		*error = MB_ERROR_NO_ERROR;
	}
	else {
		status = MB_FAILURE;
		*error = MB_ERROR_EOF;
	}
	mb_io_ptr->file_bytes = ftell(mb_io_ptr->mbfp);

	/* handle the data */
	if (status == MB_SUCCESS && line[0] == '#') {
		store->kind = MB_DATA_COMMENT;
		strncpy(store->comment, &line[1], MB_COMMENT_MAXLINE);
		if (store->comment[strlen(store->comment) - 1] == '\n')
			store->comment[strlen(store->comment) - 1] = '\0';
		(*read_count)++;
	}
	else if (status == MB_SUCCESS) {
		store->kind = MB_DATA_DATA;

		/* read data */
		nget = sscanf(line, "%d-%d-%dT%d:%d:%lfZ %lf %lf %d %d %lf %d", &store->time_i[0], &store->time_i[1], &store->time_i[2],
		              &store->time_i[3], &store->time_i[4], &sec, &store->longitude, &store->latitude, &store->gps_quality,
		              &store->gps_nsat, &store->gps_dilution, &store->gps_height);
		/* fprintf(stderr,"\nLINE:%s\tnget:%d %d/%d/%d %d:%d:%f  lon:%f lat:%f  gps:%d %d %f %d\n",
		line,nget,
		store->time_i[0],store->time_i[1],store->time_i[2],
		store->time_i[3],store->time_i[4],sec,
		store->longitude,store->latitude,
		store->gps_quality,store->gps_nsat,
		store->gps_dilution,store->gps_height);*/
		if (nget != 12) {
			store->gps_quality = 0;
			store->gps_nsat = 0;
			store->gps_dilution = 0.0;
			store->gps_height = 0;
		}
		if ((nget == 8 || nget == 12) && store->time_i[0] != 0) {
			status = MB_SUCCESS;
			*error = MB_ERROR_NO_ERROR;

			store->time_i[5] = (int)floor(sec);
			store->time_i[6] = (int)((sec - store->time_i[5]) * 1000000);
			mb_get_time(verbose, store->time_i, &store->time_d);
			(*read_count)++;
		}

		/* catch erroneous records */
		else {
			status = MB_FAILURE;
			*error = MB_ERROR_UNINTELLIGIBLE;
		}
	}

	/* print output debug statements */
	if (status == MB_SUCCESS && verbose >= 4) {
		if (store->kind == MB_DATA_DATA) {
			fprintf(stderr, "\ndbg4  Data read in MBIO function <%s>\n", function_name);
			fprintf(stderr, "dbg4  Values read:\n");
			fprintf(stderr, "dbg4       time_i[0]:    %d\n", store->time_i[0]);
			fprintf(stderr, "dbg4       time_i[1]:    %d\n", store->time_i[1]);
			fprintf(stderr, "dbg4       time_i[2]:    %d\n", store->time_i[2]);
			fprintf(stderr, "dbg4       time_i[3]:    %d\n", store->time_i[3]);
			fprintf(stderr, "dbg4       time_i[4]:    %d\n", store->time_i[4]);
			fprintf(stderr, "dbg4       time_i[5]:    %d\n", store->time_i[5]);
			fprintf(stderr, "dbg4       time_i[6]:    %d\n", store->time_i[6]);
			fprintf(stderr, "dbg4       time_d:       %f\n", store->time_d);
			fprintf(stderr, "dbg4       longitude:    %f\n", store->longitude);
			fprintf(stderr, "dbg4       latitude:     %f\n", store->latitude);
			fprintf(stderr, "dbg4       gps_quality:  %d\n", store->gps_quality);
			fprintf(stderr, "dbg4       gps_nsat:     %d\n", store->gps_nsat);
			fprintf(stderr, "dbg4       gps_dilution: %f\n", store->gps_dilution);
			fprintf(stderr, "dbg4       gps_height:   %d\n", store->gps_height);
			fprintf(stderr, "dbg4       error:        %d\n", *error);
			fprintf(stderr, "dbg4       status:       %d\n", status);
		}
		else if (store->kind == MB_DATA_COMMENT) {
			fprintf(stderr, "\ndbg4  Data read in MBIO function <%s>\n", function_name);
			fprintf(stderr, "dbg4  Values read:\n");
			fprintf(stderr, "dbg4       comment:      %s\n", store->comment);
		}
	}

	/* set error and kind in mb_io_ptr */
	mb_io_ptr->new_error = *error;
	mb_io_ptr->new_kind = store->kind;

	/* 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);
}
Exemplo n.º 6
0
int main
(
  int argc,
  char **argv
)
{
  static char program_name[] = "mbotps";
  static char help_message[] =
    "MBotps predicts tides using methods and data derived from the OSU Tidal Prediction Software (OTPS) distributions.";
  static char usage_message[] =
    "mbotps [-Atideformat -Byear/month/day/hour/minute/second -Ctidestationformat\n"
    "\t-Dinterval -Eyear/month/day/hour/minute/second -Fformat\n"
    "\t-Idatalist -Lopts_path -Ntidestationfile -Ooutput -Potps_location\n"
    "\t-Rlon/lat -S -Tmodel -Utidestationlon/tidestationlat -V]";
  extern char *optarg;
  int errflg = 0;
  int c;
  int help = 0;
  int flag = 0;

  /* MBIO status variables */
  int status = MB_SUCCESS;
  int verbose = 0;
  int error = MB_ERROR_NO_ERROR;
  char *message;

  /* MBIO read control parameters */
  int read_datalist = MB_NO;
  mb_path read_file;
  void *datalist;
  int look_processed = MB_DATALIST_LOOK_UNSET;
  double file_weight;
  mb_path swath_file;
  mb_path file;
  mb_path dfile;
  int format;
  int pings;
  int lonflip;
  double bounds[4];
  double speedmin;
  double timegap;
  int beams_bath;
  int beams_amp;
  int pixels_ss;

  /* MBIO read values */
  void *mbio_ptr = NULL;
  void *store_ptr = NULL;
  int kind;
  int time_i[7];
  double time_d;
  double navlon;
  double navlat;
  double speed;
  double heading;
  double distance;
  double altitude;
  double sonardepth;
  char *beamflag = NULL;
  double *bath = NULL;
  double *bathacrosstrack = NULL;
  double *bathalongtrack = NULL;
  double *amp = NULL;
  double *ss = NULL;
  double *ssacrosstrack = NULL;
  double *ssalongtrack = NULL;
  char comment[MB_COMMENT_MAXLINE];

  /* mbotps control parameters */
  mb_path otps_location_use;
  int notpsmodels = 0;
  int nmodeldatafiles = 0;
  int mbotps_mode = MBOTPS_MODE_POSITION;
  double tidelon;
  double tidelat;
  double btime_d;
  double etime_d;
  int btime_i[7];
  int etime_i[7];
  double interval = 300.0;
  mb_path tide_file;
  int mbprocess_update = MB_NO;
  int skip_existing = MB_NO;
  int tideformat = 2;
  int ngood;

  /* tide station data */
  mb_path tidestation_file;
  double tidestation_lon = 0.0;
  double tidestation_lat = 0.0;
  int tidestation_format = 2;
  int tidestation_ok = MB_NO;
  int ntidestation = 0;
  double *tidestation_time_d = NULL;
  double *tidestation_tide = NULL;
  double *tidestation_model = NULL;
  double *tidestation_correction = NULL;
  int time_j[5];
  int tidestation_stime_i[7], tidestation_etime_i[7];
  double tidestation_stime_d, tidestation_etime_d;
  double tidestation_d_min, tidestation_d_max;
  double tidestation_m_min, tidestation_m_max;
  double tidestation_c_min, tidestation_c_max;
  int ihr, intstat, itime;
  int size;
  double sec, correction;

  /* time parameters */
  time_t right_now;
  char date[32], user[MB_PATH_MAXLINE], *user_ptr, host[MB_PATH_MAXLINE];
  int pid;

  FILE *tfp, *mfp, *ofp;
  struct stat file_status;
  int fstat;
  double start_time_d;
  double end_time_d;
  int istart, iend;
  int proceed = MB_YES;
  int input_size, input_modtime, output_size, output_modtime;
  mb_path lltfile = "";
  mb_path otpsfile = "";
  mb_path line = "";
  mb_path predict_tide = "";
  int otps_model_set = MB_NO;
  mb_path otps_model = "";
  mb_path modelname = "";
  mb_path modelfile = "";
  mb_path modeldatafile = "";
  int read_data;
  int ntime;
  int nread;
  int nline;
  int nget;
  int output;
  double savetime_d;
  double lasttime_d;
  double lastlon;
  double lastlat;
  double lon;
  double lat;
  double tide;
  double depth;
  char *result;
  int i;

  /* get current default values */
  status = mb_defaults(verbose,
    &format,
    &pings,
    &lonflip,
    bounds,
    btime_i,
    etime_i,
    &speedmin,
    &timegap);

  /* set default input to datalist.mb-1 */
  strcpy(read_file, "datalist.mb-1");

  /* set default location of the OTPS package */
  strcpy(otps_location_use, otps_location);

  /* set defaults for the AUV survey we were running on Coaxial Segment, Juan de Fuca Ridge
      while I wrote this code */
  sprintf(otps_model, MBOTPS_DEFAULT_MODEL);
  sprintf(tide_file, "tide_model.txt");
  tidelon = -129.588618;
  tidelat = 46.50459;
  interval = 60.0;
  btime_i[0] = 2009;
  btime_i[1] = 7;
  btime_i[2] = 31;
  btime_i[3] = 0;
  btime_i[4] = 0;
  btime_i[5] = 0;
  btime_i[6] = 0;
  etime_i[0] = 2009;
  etime_i[1] = 8;
  etime_i[2] = 2;
  etime_i[3] = 1;
  etime_i[4] = 0;
  etime_i[5] = 0;
  etime_i[6] = 0;

  /* process argument list */
  while ((c =
    getopt(argc, argv, "A:a:B:b:C:c:D:d:E:e:F:f:I:i:MmN:n:O:o:P:p:R:r:SST:t:U:u:VvHh")) != -1)
    switch (c)
      {
    case 'H':
    case 'h':
      help++;
      break;
    case 'V':
    case 'v':
      verbose++;
      break;
    case 'A':
    case 'a':
      sscanf(optarg, "%d", &tideformat);
      if (tideformat != 2)
        tideformat = 1;
      break;
    case 'B':
    case 'b':
      sscanf(optarg,
        "%d/%d/%d/%d/%d/%d",
        &btime_i[0],
        &btime_i[1],
        &btime_i[2],
        &btime_i[3],
        &btime_i[4],
        &btime_i[5]);
      btime_i[6] = 0;
      flag++;
      break;
    case 'C':
    case 'c':
      sscanf(optarg, "%d", &tidestation_format);
      if (tidestation_format < 1 || tidestation_format > 4)
        tidestation_format = 2;
      break;
    case 'D':
    case 'd':
      sscanf(optarg, "%lf", &interval);
      break;
    case 'E':
    case 'e':
      sscanf(optarg,
        "%d/%d/%d/%d/%d/%d",
        &etime_i[0],
        &etime_i[1],
        &etime_i[2],
        &etime_i[3],
        &etime_i[4],
        &etime_i[5]);
      etime_i[6] = 0;
      flag++;
      break;
    case 'F':
    case 'f':
      sscanf(optarg, "%d", &format);
      flag++;
      break;
    case 'I':
    case 'i':
      sscanf(optarg, "%s", read_file);
      mbotps_mode = mbotps_mode | MBOTPS_MODE_NAVIGATION;
      flag++;
      break;
    case 'M':
    case 'm':
      mbprocess_update = MB_YES;
      break;
    case 'N':
    case 'n':
      sscanf(optarg, "%s", tidestation_file);
      mbotps_mode = mbotps_mode | MBOTPS_MODE_TIDESTATION;
      break;
    case 'O':
    case 'o':
      sscanf(optarg, "%s", tide_file);
      break;
    case 'P':
    case 'p':
      sscanf(optarg, "%s", otps_location_use);
      break;
    case 'R':
    case 'r':
      sscanf(optarg, "%lf/%lf", &tidelon, &tidelat);
      break;
    case 'S':
    case 's':
      skip_existing = MB_YES;
      break;
    case 'T':
    case 't':
      sscanf(optarg, "%s", otps_model);
      otps_model_set = MB_YES;
      break;
    case 'U':
    case 'u':
      sscanf(optarg, "%lf/%lf", &tidestation_lon, &tidestation_lat);
      break;
    case '?':
      errflg++;
      }

  /* if error flagged then print it and exit */
  if (errflg)
    {
    fprintf(stderr, "usage: %s\n", usage_message);
    fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
    error = MB_ERROR_BAD_USAGE;
    exit(error);
    }

  /* print starting message */
  if (( verbose == 1) || help)
    {
    fprintf(stderr, "\nProgram %s\n", program_name);
    fprintf(stderr, "MB-system Version %s\n", MB_VERSION);
    }

  /* if help desired then print it and exit */
  if (help)
    {
    fprintf(stderr, "\n%s\n", help_message);
    fprintf(stderr, "\nusage: %s\n", usage_message);
    }

  /* Check for available tide models */
  if (help || ( verbose > 0) )
    {
    fprintf(stderr, "\nChecking for available OTPS tide models\n");
    fprintf(stderr, "OTPS location: %s\nValid OTPS tidal models:\n", otps_location_use);
    }
  notpsmodels = 0;
  sprintf(line, "/bin/ls -1 %s/DATA | grep Model_ | sed \"s/^Model_//\"", otps_location_use);
  if ((tfp = popen(line, "r")) != NULL)
    {
    /* send relevant input to predict_tide through its stdin stream */
    while (fgets(line, sizeof(line), tfp))
      {
      sscanf(line, "%s", modelname);
      sprintf(modelfile, "%s/DATA/Model_%s", otps_location_use, modelname);
      nmodeldatafiles = 0;

      /* check the files referenced in the model file */
      if ((mfp = fopen(modelfile, "r")) != NULL)
        {
        /* stat the file referenced in each line */
        while (fgets(modeldatafile, MB_PATH_MAXLINE, mfp) != NULL)
          {
          if (strlen(modeldatafile) > 0)
            modeldatafile[strlen(modeldatafile) - 1] = '\0';
          if (( (fstat =
            stat(modeldatafile,
              &file_status)) == 0) && ( (file_status.st_mode & S_IFMT) != S_IFDIR) )
            nmodeldatafiles++;
          }
        fclose(mfp);
        }
      if (nmodeldatafiles >= 3)
        {
        if (help || ( verbose > 0) )
          fprintf(stderr, "     %s\n", modelname);
        if (otps_model_set == MB_NO)
          if (( notpsmodels == 0) || ( strcmp(modelname, MBOTPS_DEFAULT_MODEL) == 0) )
            strcpy(otps_model, modelname);
        notpsmodels++;
        }
      }

    /* close the process */
    pclose(tfp);
    }
  else
    {
    error = MB_ERROR_OPEN_FAIL;
    fprintf(stderr, "\nUnable to open ls using popen()\n");
    fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
    exit(MB_FAILURE);
    }
  if (help || ( verbose > 0) )
    {
    fprintf(stderr, "Number of available OTPS tide models: %d\n", notpsmodels);
    fprintf(stderr, "\nUsing OTPS tide model:            %s\n", otps_model);
    }

  /* print starting debug statements */
  if (verbose >= 2)
    {
    fprintf(stderr, "\ndbg2  Program <%s>\n", program_name);
    fprintf(stderr, "dbg2  MB-system Version %s\n", MB_VERSION);
    fprintf(stderr, "dbg2  Control Parameters:\n");
    fprintf(stderr, "dbg2       verbose:              %d\n", verbose);
    fprintf(stderr, "dbg2       help:                 %d\n", help);
    fprintf(stderr, "dbg2       otps_location:        %s\n", otps_location);
    fprintf(stderr, "dbg2       otps_location_use:    %s\n", otps_location_use);
    fprintf(stderr, "dbg2       otps_model_set:       %d\n", otps_model_set);
    fprintf(stderr, "dbg2       otps_model:           %s\n", otps_model);
    fprintf(stderr, "dbg2       mbotps_mode:          %d\n", mbotps_mode);
    fprintf(stderr, "dbg2       tidelon:              %f\n", tidelon);
    fprintf(stderr, "dbg2       tidelat:              %f\n", tidelat);
    fprintf(stderr, "dbg2       tidestation_file:     %s\n", tidestation_file);
    fprintf(stderr, "dbg2       tidestation_lon:       %f\n", tidestation_lon);
    fprintf(stderr, "dbg2       tidestation_lat:       %f\n", tidestation_lat);
    fprintf(stderr, "dbg2       tidestation_format:    %d\n", tidestation_format);
    fprintf(stderr, "dbg2       btime_i[0]:           %d\n", btime_i[0]);
    fprintf(stderr, "dbg2       btime_i[1]:           %d\n", btime_i[1]);
    fprintf(stderr, "dbg2       btime_i[2]:           %d\n", btime_i[2]);
    fprintf(stderr, "dbg2       btime_i[3]:           %d\n", btime_i[3]);
    fprintf(stderr, "dbg2       btime_i[4]:           %d\n", btime_i[4]);
    fprintf(stderr, "dbg2       btime_i[5]:           %d\n", btime_i[5]);
    fprintf(stderr, "dbg2       btime_i[6]:           %d\n", btime_i[6]);
    fprintf(stderr, "dbg2       etime_i[0]:           %d\n", etime_i[0]);
    fprintf(stderr, "dbg2       etime_i[1]:           %d\n", etime_i[1]);
    fprintf(stderr, "dbg2       etime_i[2]:           %d\n", etime_i[2]);
    fprintf(stderr, "dbg2       etime_i[3]:           %d\n", etime_i[3]);
    fprintf(stderr, "dbg2       etime_i[4]:           %d\n", etime_i[4]);
    fprintf(stderr, "dbg2       etime_i[5]:           %d\n", etime_i[5]);
    fprintf(stderr, "dbg2       etime_i[6]:           %d\n", etime_i[6]);
    fprintf(stderr, "dbg2       interval:             %f\n", interval);
    fprintf(stderr, "dbg2       tide_file:            %s\n", tide_file);
    fprintf(stderr, "dbg2       mbprocess_update:     %d\n", mbprocess_update);
    fprintf(stderr, "dbg2       skip_existing:        %d\n", skip_existing);
    fprintf(stderr, "dbg2       tideformat:           %d\n", tideformat);
    fprintf(stderr, "dbg2       format:               %d\n", format);
    fprintf(stderr, "dbg2       read_file:            %s\n", read_file);
    }

  /* exit if no valid OTPS models can be found */
  if (notpsmodels <= 0)
    {
    error = MB_ERROR_OPEN_FAIL;
    fprintf(stderr, "\nUnable to find a valid OTPS tidal model\n");
    fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
    exit(MB_FAILURE);
    }

  /* if help was all that was desired then exit */
  if (help)
    exit(error);

  /* -------------------------------------------------------------------------
   * if specified read in tide station data and calculate model values for the
   * same location and times- the difference is applied as a correction to the
   * model values calculated at the desired locations and times
   * -----------------------------------------------------------------------*/
  if (mbotps_mode & MBOTPS_MODE_TIDESTATION)
    {

    /* make sure longitude is positive */
    if (tidestation_lon < 0.0)
      tidestation_lon += 360.0;

    /* open the tide station data file */
    if ((tfp = fopen(tidestation_file, "r")) == NULL)
      {
      error = MB_ERROR_OPEN_FAIL;
      fprintf(stderr,
        "\nUnable to open tide station file <%s> for writing\n",
        tidestation_file);
      fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
      exit(MB_FAILURE);
      }

    /* count the lines in the tide station data */
    ntidestation = 0;
    while ((result = fgets(line, MB_PATH_MAXLINE, tfp)) == line)
      ntidestation++;
    rewind(tfp);

    /* allocate memory for tide station arrays */
    size = ntidestation * sizeof(double);
    status =mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&tidestation_time_d, &error);
    if (error == MB_ERROR_NO_ERROR)
      status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&tidestation_tide, &error);
    if (error == MB_ERROR_NO_ERROR)
      status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&tidestation_model, &error);
    if (error == MB_ERROR_NO_ERROR)
      status = mb_mallocd(verbose, __FILE__, __LINE__, size, (void **)&tidestation_correction, &error);
    if (error != MB_ERROR_NO_ERROR)
      {
      mb_error(verbose, error, &message);
      fprintf(stderr, "\nMBIO Error allocating data arrays:\n%s\n", message);
      fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
      exit(error);
      }

    /* read the tide station data in the specified format */
    ntidestation = 0;
    while ((result = fgets(line, MB_PATH_MAXLINE, tfp)) == line)
      {
      tidestation_ok = MB_NO;

      /* ignore comments */
      if (line[0] != '#')
        {
        /* deal with tide station data in form: time_d tide */
        if (tidestation_format == 1)
          {
          nget = sscanf(line,
            "%lf %lf",
            &tidestation_time_d[ntidestation],
            &tidestation_tide[ntidestation]);
          if (nget == 2)
            tidestation_ok = MB_YES;
          }

        /* deal with tide station data in form: yr mon day hour min sec tide */
        else if (tidestation_format == 2)
          {
          nget = sscanf(line,
            "%d %d %d %d %d %lf %lf",
            &time_i[0],
            &time_i[1],
            &time_i[2],
            &time_i[3],
            &time_i[4],
            &sec,
            &tidestation_tide[ntidestation]);
          time_i[5] = (int)sec;
          time_i[6] = 1000000 * (sec - time_i[5]);
          mb_get_time(verbose, time_i, &time_d);
          tidestation_time_d[ntidestation] = time_d;
          if (nget == 7)
            tidestation_ok = MB_YES;
          }

        /* deal with tide station data in form: yr jday hour min sec tide */
        else if (tidestation_format == 3)
          {
          nget = sscanf(line,
            "%d %d %d %d %lf %lf",
            &time_j[0],
            &time_j[1],
            &ihr,
            &time_j[2],
            &sec,
            &tidestation_tide[ntidestation]);
          time_j[2] = time_j[2] + 60 * ihr;
          time_j[3] = (int)sec;
          time_j[4] = 1000000 * (sec - time_j[3]);
          mb_get_itime(verbose, time_j, time_i);
          mb_get_time(verbose, time_i, &time_d);
          tidestation_time_d[ntidestation] = time_d;
          if (nget == 6)
            tidestation_ok = MB_YES;
          }

        /* deal with tide station data in form: yr jday daymin sec tide */
        else if (tidestation_format == 4)
          {
          nget = sscanf(line,
            "%d %d %d %lf %lf",
            &time_j[0],
            &time_j[1],
            &time_j[2],
            &sec,
            &tidestation_tide[ntidestation]);
          time_j[3] = (int)sec;
          time_j[4] = 1000000 * (sec - time_j[3]);
          mb_get_itime(verbose, time_j, time_i);
          mb_get_time(verbose, time_i, &time_d);
          tidestation_time_d[ntidestation] = time_d;
          if (nget == 5)
            tidestation_ok = MB_YES;
          }
        }

      /* output some debug values */
      if ((verbose >= 5) && (tidestation_ok == MB_YES))
        {
        fprintf(stderr, "\ndbg5  New tide point read in program <%s>\n", program_name);
        fprintf(stderr, "dbg5       tide[%d]: %f %f\n", ntidestation,
          tidestation_time_d[ntidestation], tidestation_tide[ntidestation]);
        }
      else if (verbose >= 5)
        {
        fprintf(stderr,
          "\ndbg5  Error parsing line in tide file in program <%s>\n",
          program_name);
        fprintf(stderr, "dbg5       line: %s\n", line);
        }

      /* check for reverses or repeats in time */
      if (tidestation_ok == MB_YES)
        {
        if (ntidestation == 0)
          {
          ntidestation++;
          }
        else if (tidestation_time_d[ntidestation] > tidestation_time_d[ntidestation - 1])
          {
          ntidestation++;
          }
        else if ((ntidestation > 0) &&
          ( tidestation_time_d[ntidestation] <= tidestation_time_d[ntidestation - 1]) &&
          ( verbose >= 5) )
          {
          fprintf(stderr, "\ndbg5  Tide time error in program <%s>\n", program_name);
          fprintf(stderr,
            "dbg5       tide[%d]: %f %f\n",
            ntidestation - 1,
            tidestation_time_d[ntidestation - 1],
            tidestation_tide[ntidestation - 1]);
          fprintf(stderr,
            "dbg5       tide[%d]: %f %f\n",
            ntidestation,
            tidestation_time_d[ntidestation],
            tidestation_tide[ntidestation]);
          }
        }
      strncpy(line, "\0", sizeof(line));
      }
    fclose(tfp);

    /* now get time and tide model values at the tide station location
       first open temporary file of lat lon time*/
    pid = getpid();
    sprintf(lltfile, "tmp_mbotps_llt_%d.txt", pid);
    sprintf(otpsfile, "tmp_mbotps_llttd_%d.txt", pid);
    if ((tfp = fopen(lltfile, "w")) == NULL)
      {
      error = MB_ERROR_OPEN_FAIL;
      fprintf(stderr,
        "\nUnable to open temporary lat-lon-time file <%s> for writing\n",
        lltfile);
      fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
      exit(MB_FAILURE);
      }
    else {
      for (i = 0; i < ntidestation; i++) {
        mb_get_date(verbose, tidestation_time_d[i], time_i);
        fprintf(tfp, "%.6f %.6f %4.4d %2.2d %2.2d %2.2d %2.2d %2.2d\n",
          tidestation_lat, tidestation_lon,
          time_i[0], time_i[1], time_i[2], time_i[3], time_i[4], time_i[5]);
      }
      fclose(tfp);
    }

    /* call predict_tide with popen */
    sprintf(predict_tide, "%s/predict_tide", otps_location_use);
    if ((tfp = popen(predict_tide, "w")) == NULL)
      {
      error = MB_ERROR_OPEN_FAIL;
      fprintf(stderr, "\nUnable to open predict_time program using popen()\n");
      fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
      exit(MB_FAILURE);
      }

    /* send relevant input to predict_tide through its stdin stream */
    fprintf(tfp, "%s/DATA/Model_%s\n", otps_location_use, otps_model);
    fprintf(tfp, "%s\n", lltfile);
    fprintf(tfp, "z\n\nAP\noce\n1\n");
    /*fprintf(tfp, "z\nm2,s2,n2,k2,k1,o1,p1,q1\nAP\noce\n1\n");*/
    fprintf(tfp, "%s\n", otpsfile);

    /* close the process */
    pclose(tfp);

    /* now read results from predict_tide and rewrite them in a useful form */
    if ((tfp = fopen(otpsfile, "r")) == NULL)
      {
      error = MB_ERROR_OPEN_FAIL;
      fprintf(stderr, "\nUnable to open predict_time results temporary file <%s>\n",
        otpsfile);
      fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
      exit(MB_FAILURE);
      }

    nline = 0;
    ngood = 0;
    while ((result = fgets(line, MB_PATH_MAXLINE, tfp)) == line)
      {
      nline++;
      if (nline > 6)
        {
        nget = sscanf(line,
          "%lf %lf %d.%d.%d %d:%d:%d %lf %lf",
          &lat, &lon,
          &time_i[1], &time_i[2], &time_i[0], &time_i[3], &time_i[4], &time_i[5],
          &tide, &depth);
        if (nget == 10)
          {
          tidestation_model[ngood] = tide;
          tidestation_correction[ngood] = tidestation_tide[ngood] - tidestation_model[ngood];
          ngood++;
          }
        }
      }
    fclose(tfp);
    if (ngood != ntidestation)
      {
      error = MB_ERROR_BAD_FORMAT;
      fprintf(stderr,
        "\nNumber of tide station values does not match number of model values <%d != %d>\n",
        ntidestation,
        ngood);
      fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
      exit(MB_FAILURE);
      }

    /* get start end min max of tide station data */
    tidestation_d_min = 0.0;
    tidestation_d_max = 0.0;
    tidestation_m_min = 0.0;
    tidestation_m_max = 0.0;
    tidestation_c_min = 0.0;
    tidestation_c_max = 0.0;
    for (i = 0; i < ntidestation; i++) {
      if (i == 0) {
        tidestation_d_min = tidestation_tide[i];
        tidestation_d_max = tidestation_tide[i];
        tidestation_m_min = tidestation_model[i];
        tidestation_m_max = tidestation_model[i];
        tidestation_c_min = tidestation_correction[i];
        tidestation_c_max = tidestation_correction[i];
        tidestation_stime_d = tidestation_time_d[i];
      } else {
        tidestation_d_min = MIN(tidestation_tide[i], tidestation_d_min);
        tidestation_d_max = MAX(tidestation_tide[i], tidestation_d_max);
        tidestation_m_min = MIN(tidestation_model[i], tidestation_m_min);
        tidestation_m_max = MAX(tidestation_model[i], tidestation_m_max);
        tidestation_c_min = MIN(tidestation_correction[i], tidestation_c_min);
        tidestation_c_max = MAX(tidestation_correction[i], tidestation_c_max);
        tidestation_etime_d = tidestation_time_d[i];
      }
    }
    mb_get_date(verbose, tidestation_stime_d, tidestation_stime_i);
    mb_get_date(verbose, tidestation_etime_d, tidestation_etime_i);

    /* output info on tide station data */
    if (( verbose > 0) && mbotps_mode & MBOTPS_MODE_TIDESTATION)
      {
      fprintf(stderr, "\nTide station data file:             %s\n", tidestation_file);
      fprintf(stderr, "  Tide station longitude:           %f\n", tidestation_lon);
      fprintf(stderr, "  Tide station latitude:            %f\n", tidestation_lat);
      fprintf(stderr, "  Tide station format:              %d\n", tidestation_format);
      fprintf(stderr, "  Tide station data summary:\n");
      fprintf(stderr, "    Number of samples:              %d\n", ntidestation);
      fprintf(stderr,
        "    Start time:                     %4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d.%6.6d\n",
        tidestation_stime_i[0],
        tidestation_stime_i[1],
        tidestation_stime_i[2],
        tidestation_stime_i[3],
        tidestation_stime_i[4],
        tidestation_stime_i[5],
        tidestation_stime_i[6]);
      fprintf(stderr,
        "    End time:                       %4.4d/%2.2d/%2.2d %2.2d:%2.2d:%2.2d.%6.6d\n",
        tidestation_etime_i[0],
        tidestation_etime_i[1],
        tidestation_etime_i[2],
        tidestation_etime_i[3],
        tidestation_etime_i[4],
        tidestation_etime_i[5],
        tidestation_etime_i[6]);
      fprintf(stderr, "    Minimum values:     %7.3f %7.3f %7.3f\n",
              tidestation_d_min, tidestation_m_min, tidestation_c_min);
      fprintf(stderr, "    Maximum values:     %7.3f %7.3f %7.3f\n",
              tidestation_d_max, tidestation_m_max, tidestation_c_max);
      }

    /* remove the temporary files */
    unlink(lltfile);
    unlink(otpsfile);
    }

  /* -------------------------------------------------------------------------
   * calculate tide model  for a single position and time range
   * -----------------------------------------------------------------------*/
  if (!(mbotps_mode & MBOTPS_MODE_NAVIGATION))
    {
    /* first open temporary file of lat lon time */
    pid = getpid();
    sprintf(lltfile, "tmp_mbotps_llt_%d.txt", pid);
    sprintf(otpsfile, "tmp_mbotps_llttd_%d.txt", pid);
    if ((tfp = fopen(lltfile, "w")) == NULL)
      {
      error = MB_ERROR_OPEN_FAIL;
      fprintf(stderr,
        "\nUnable to open temporary lat-lon-time file <%s> for writing\n",
        lltfile);
      fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
      exit(MB_FAILURE);
      }

    /* make sure longitude is positive */
    if (tidelon < 0.0)
      tidelon += 360.0;

    /* loop over the time of interest generating the lat-lon-time values */
    mb_get_time(verbose, btime_i, &btime_d);
    mb_get_time(verbose, etime_i, &etime_d);
    ntime = 1 + (int)floor((etime_d - btime_d) / interval);
    for (i = 0; i < ntime; i++)
      {
      time_d = btime_d + i * interval;
      mb_get_date(verbose, time_d, time_i);
      fprintf(tfp,
        "%.6f %.6f %4.4d %2.2d %2.2d %2.2d %2.2d %2.2d\n",
        tidelat,
        tidelon,
        time_i[0],
        time_i[1],
        time_i[2],
        time_i[3],
        time_i[4],
        time_i[5]);
      }

    /* close the llt file */
    fclose(tfp);

    /* call predict_tide with popen */
    sprintf(predict_tide, "%s/predict_tide", otps_location_use);
    if ((tfp = popen(predict_tide, "w")) == NULL)
      {
      error = MB_ERROR_OPEN_FAIL;
      fprintf(stderr, "\nUnable to open predict_time program using popen()\n");
      fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
      exit(MB_FAILURE);
      }

    /* send relevant input to predict_tide through its stdin stream */
    fprintf(tfp, "%s/DATA/Model_%s\n", otps_location_use, otps_model);
    fprintf(tfp, "%s\n", lltfile);
    fprintf(tfp, "z\n\nAP\noce\n1\n");
    /*fprintf(tfp, "z\nm2,s2,n2,k2,k1,o1,p1,q1\nAP\noce\n1\n");*/
    fprintf(tfp, "%s\n", otpsfile);

    /* close the process */
    pclose(tfp);

    /* now read results from predict_tide and rewrite them in a useful form */
    if ((tfp = fopen(otpsfile, "r")) == NULL)
      {
      error = MB_ERROR_OPEN_FAIL;
      fprintf(stderr, "\nUnable to open predict_time results temporary file <%s>\n",
        otpsfile);
      fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
      exit(MB_FAILURE);
      }
    if ((ofp = fopen(tide_file, "w")) == NULL)
      {
      error = MB_ERROR_OPEN_FAIL;
      fprintf(stderr, "\nUnable to open tide output file <%s>\n", tide_file);
      fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
      exit(MB_FAILURE);
      }
    fprintf(ofp, "# Tide model generated by program %s\n", program_name);
    fprintf(ofp, "# MB-System Version: %s\n", MB_VERSION);
    fprintf(ofp, "# Tide model generated by program %s\n", program_name);
    fprintf(ofp, "# which in turn calls OTPS program predict_tide obtained from:\n");
    fprintf(ofp, "#     http://www.coas.oregonstate.edu/research/po/research/tide/\n");
    fprintf(ofp, "#\n");
    fprintf(ofp, "# OTPSnc tide model: \n");
    fprintf(ofp, "#      %s\n", otps_model);
    if (tideformat == 2)
      {
      fprintf(ofp, "# Output format:\n");
      fprintf(ofp, "#      year month day hour minute second tide\n");
      fprintf(ofp, "# where tide is in meters\n");
      }
    else
      {
      fprintf(ofp, "# Output format:\n");
      fprintf(ofp, "#      time_d tide\n");
      fprintf(ofp, "# where time_d is in seconds since January 1, 1970\n");
      fprintf(ofp, "# and tide is in meters\n");
      }
    right_now = time((time_t *)0);
    strcpy(date, ctime(&right_now));
    date[strlen(date) - 1] = '\0';
    if ((user_ptr = getenv("USER")) == NULL)
      user_ptr = getenv("LOGNAME");
    if (user_ptr != NULL)
      strcpy(user, user_ptr);
    else
      strcpy(user, "unknown");
    gethostname(host, MBP_FILENAMESIZE);
    fprintf(ofp, "# Run by user <%s> on cpu <%s> at <%s>\n", user, host, date);

    /* loop over tide model values, writing them out in the specified format */
    nline = 0;
    ngood = 0;
    while ((result = fgets(line, MB_PATH_MAXLINE, tfp)) == line)
      {
      nline++;
      if (( nline == 2) || ( nline == 3) )
        {
        fprintf(ofp, "#%s", line);
        }
      else if (nline > 6)
        {
        nget = sscanf(line,
          "%lf %lf %d.%d.%d %d:%d:%d %lf %lf",
          &lat,
          &lon,
          &time_i[1],
          &time_i[2],
          &time_i[0],
          &time_i[3],
          &time_i[4],
          &time_i[5],
          &tide,
          &depth);
        if (nget == 10)
          {
          ngood++;

          /* if tide station data have been loaded, interpolate the
           * correction value to apply to the tide model */
          if (mbotps_mode & MBOTPS_MODE_TIDESTATION && (ntidestation > 0))
            {
            intstat = mb_linear_interp(verbose,
              tidestation_time_d - 1,
              tidestation_correction - 1,
              ntidestation,
              time_d,
              &correction,
              &itime,
              &error);
            if (intstat == MB_SUCCESS)
              tide += correction;
/*fprintf(stderr,"TIDE STATION CORRECTION: intstat:%d itime:%dof%d time_d:%f correction:%f
   tide:%f\n", */
/*        intstat, itime, ntidestation, time_d, correction, tide); */
            }

          /* write out the tide model */
          if (tideformat == 2)
            {
            fprintf(ofp,
              "%4.4d %2.2d %2.2d %2.2d %2.2d %2.2d %9.4f\n",
              time_i[0],
              time_i[1],
              time_i[2],
              time_i[3],
              time_i[4],
              time_i[5],
              tide);
            }
          else
            {
            mb_get_time(verbose, time_i, &time_d);
            fprintf(ofp, "%.3f %9.4f\n", time_d, tide);
            }
          }
        }
      }
    fclose(tfp);
    fclose(ofp);

    /* remove the temporary files */
    unlink(lltfile);
    unlink(otpsfile);

    /* some helpful output */
    fprintf(stderr, "\nResults are really in %s\n", tide_file);
    }  /* end single position mode */

  /* -------------------------------------------------------------------------
   * else get tides along the navigation contained in a set of swath files
   * -----------------------------------------------------------------------*/
  else if (mbotps_mode & MBOTPS_MODE_NAVIGATION)
    {
/*fprintf(stderr,"Doing tide correction for swath navigation\n"); */
    /* get format if required */
    if (format == 0)
      mb_get_format(verbose, read_file, NULL, &format, &error);

    /* determine whether to read one file or a list of files */
    if (format < 0)
      read_datalist = MB_YES;

    /* open file list */
    if (read_datalist == MB_YES)
      {
      if ((status =
        mb_datalist_open(verbose, &datalist, read_file, look_processed,
          &error)) != MB_SUCCESS)
        {
        error = MB_ERROR_OPEN_FAIL;
        fprintf(stderr, "\nUnable to open data list file: %s\n", read_file);
        fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
        exit(error);
        }
      if ((status =
        mb_datalist_read(verbose, datalist, file, dfile, &format, &file_weight,
          &error)) == MB_SUCCESS)
        read_data = MB_YES;
      else
        read_data = MB_NO;
      }
    /* else copy single filename to be read */
    else
      {
      strcpy(file, read_file);
      read_data = MB_YES;
      }

    /* loop over all files to be read */
    while (read_data == MB_YES)
      {
      /* Figure out if the file needs a tide model - don't generate a new tide
         model if one was made previously and is up to date AND the
         appropriate request has been made */
      proceed = MB_YES;
      sprintf(tide_file, "%s.tde", file);
      if (skip_existing == MB_YES)
        {
        if (( (fstat =
          stat(file,
            &file_status)) == 0) && ( (file_status.st_mode & S_IFMT) != S_IFDIR) )
          {
          input_modtime = file_status.st_mtime;
          input_size = file_status.st_size;
          }
        else
          {
          input_modtime = 0;
          input_size = 0;
          }
        if (( (fstat =
          stat(tide_file,
            &file_status)) == 0) && ( (file_status.st_mode & S_IFMT) != S_IFDIR) )
          {
          output_modtime = file_status.st_mtime;
          output_size = file_status.st_size;
          }
        else
          {
          output_modtime = 0;
          output_size = 0;
          }
        if (( output_modtime > input_modtime) && ( input_size > 0) && ( output_size > 0) )
          proceed = MB_NO;
        }

      /* skip the file */
      if (proceed == MB_NO)
        {
        /* some helpful output */
        fprintf(stderr,
          "\n---------------------------------------\n\nProcessing tides for %s\n\n",
          file);
        fprintf(stderr, "Skipped - tide model file is up to date\n\n");
        }

      /* generate the tide model */
      else
        {
        /* some helpful output */
        fprintf(stderr,
          "\n---------------------------------------\n\nProcessing tides for %s\n\n",
          file);

        /* note tide station correction */
        if (mbotps_mode & MBOTPS_MODE_TIDESTATION && (ntidestation > 0)) {
          fprintf(stderr, "Applying tide station correction\n\n");
        }

        /* first open temporary file of lat lon time */
        pid = getpid();
        strcpy(swath_file, file);
        sprintf(lltfile, "tmp_mbotps_llt_%d.txt", pid);
        sprintf(otpsfile, "tmp_mbotps_llttd_%d.txt", pid);
        if ((tfp = fopen(lltfile, "w")) == NULL)
          {
          error = MB_ERROR_OPEN_FAIL;
          fprintf(stderr,
            "\nUnable to open temporary lat-lon-time file <%s> for writing\n",
            lltfile);
          fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
          exit(MB_FAILURE);
          }

        /* read fnv file if possible */
        mb_get_fnv(verbose, file, &format, &error);

        /* initialize reading the swath file */
        if ((status =
          mb_read_init(verbose, file, format, pings, lonflip, bounds, btime_i, etime_i,
            speedmin, timegap, &mbio_ptr, &btime_d, &etime_d, &beams_bath, &beams_amp,
            &pixels_ss, &error)) !=MB_SUCCESS)
          {
          mb_error(verbose, error, &message);
          fprintf(stderr,
            "\nMBIO Error returned from function <mb_read_init>:\n%s\n",
            message);
          fprintf(stderr, "\nMultibeam File <%s> not initialized for reading\n", file);
          fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
          exit(error);
          }

        /* allocate memory for data arrays */
        if (error == MB_ERROR_NO_ERROR)
          status =mb_register_array(verbose,
            mbio_ptr,
            MB_MEM_TYPE_BATHYMETRY,
            sizeof(char),
            (void **)&beamflag,
            &error);
        if (error == MB_ERROR_NO_ERROR)
          status =
            mb_register_array(verbose,
            mbio_ptr,
            MB_MEM_TYPE_BATHYMETRY,
            sizeof(double),
            (void **)&bath,
            &error);
        if (error == MB_ERROR_NO_ERROR)
          status =
            mb_register_array(verbose,
            mbio_ptr,
            MB_MEM_TYPE_AMPLITUDE,
            sizeof(double),
            (void **)&amp,
            &error);
        if (error == MB_ERROR_NO_ERROR)
          status =
            mb_register_array(verbose,
            mbio_ptr,
            MB_MEM_TYPE_BATHYMETRY,
            sizeof(double),
            (void **)&bathacrosstrack,
            &error);
        if (error == MB_ERROR_NO_ERROR)
          status =
            mb_register_array(verbose,
            mbio_ptr,
            MB_MEM_TYPE_BATHYMETRY,
            sizeof(double),
            (void **)&bathalongtrack,
            &error);
        if (error == MB_ERROR_NO_ERROR)
          status =mb_register_array(verbose,
            mbio_ptr,
            MB_MEM_TYPE_SIDESCAN,
            sizeof(double),
            (void **)&ss,
            &error);
        if (error == MB_ERROR_NO_ERROR)
          status =mb_register_array(verbose,
            mbio_ptr,
            MB_MEM_TYPE_SIDESCAN,
            sizeof(double),
            (void **)&ssacrosstrack,
            &error);
        if (error == MB_ERROR_NO_ERROR)
          status =mb_register_array(verbose,
            mbio_ptr,
            MB_MEM_TYPE_SIDESCAN,
            sizeof(double),
            (void **)&ssalongtrack,
            &error);

        /* if error initializing memory then quit */
        if (error != MB_ERROR_NO_ERROR)
          {
          mb_error(verbose, error, &message);
          fprintf(stderr, "\nMBIO Error allocating data arrays:\n%s\n", message);
          fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
          exit(error);
          }

        /* read and use data */
        nread = 0;
        while (error <= MB_ERROR_NO_ERROR)
          {
          /* reset error */
          error = MB_ERROR_NO_ERROR;
          output = MB_NO;

          /* read next data record */
          status = mb_get_all(verbose,
            mbio_ptr,
            &store_ptr,
            &kind,
            time_i,
            &time_d,
            &navlon,
            &navlat,
            &speed,
            &heading,
            &distance,
            &altitude,
            &sonardepth,
            &beams_bath,
            &beams_amp,
            &pixels_ss,
            beamflag,
            bath,
            amp,
            bathacrosstrack,
            bathalongtrack,
            ss,
            ssacrosstrack,
            ssalongtrack,
            comment,
            &error);

          /* print debug statements */
          if (verbose >= 2)
            {
            fprintf(stderr, "\ndbg2  Ping read in program <%s>\n", program_name);
            fprintf(stderr, "dbg2       kind:           %d\n", kind);
            fprintf(stderr, "dbg2       error:          %d\n", error);
            fprintf(stderr, "dbg2       status:         %d\n", status);
            }

          /* deal with nav and time from survey data only - not nav, sidescan, or
             subbottom */
          if (( error <= MB_ERROR_NO_ERROR) && ( kind == MB_DATA_DATA) )
            {
            /* flag positions and times for output at specified interval */
            if (( nread == 0) || ( time_d - savetime_d >= interval) )
              {
              savetime_d = time_d;
              output = MB_YES;
              }
            lasttime_d = time_d;
            lastlon = navlon;
            lastlat = navlat;

            /* increment counter */
            nread++;
            }

          /* output position and time if flagged or end of file */
          if (( output == MB_YES) || ( error == MB_ERROR_EOF) )
            {
            if (lastlon < 0.0)
              lastlon += 360.0;
            mb_get_date(verbose, lasttime_d, time_i);
            fprintf(tfp,
              "%.6f %.6f %4.4d %2.2d %2.2d %2.2d %2.2d %2.2d\n",
              lastlat,
              lastlon,
              time_i[0],
              time_i[1],
              time_i[2],
              time_i[3],
              time_i[4],
              time_i[5]);
            }
          }

        /* close the swath file */
        status = mb_close(verbose, &mbio_ptr, &error);

        /* output read statistics */
        fprintf(stderr, "%d records read from %s\n", nread, file);

        /* close the llt file */
        fclose(tfp);

        /* call predict_tide with popen */
        sprintf(predict_tide, "%s/predict_tide", otps_location_use);
        if ((tfp = popen(predict_tide, "w")) == NULL)
          {
          error = MB_ERROR_OPEN_FAIL;
          fprintf(stderr, "\nUnable to open predict_time program using popen()\n");
          fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
          exit(MB_FAILURE);
          }

        /* send relevant input to predict_tide through its stdin stream */
        fprintf(tfp, "%s/DATA/Model_%s\n", otps_location_use, otps_model);
        fprintf(tfp, "%s\n", lltfile);
        fprintf(tfp, "z\n\nAP\noce\n1\n");
        /*fprintf(tfp, "z\nm2,s2,n2,k2,k1,o1,p1,q1\nAP\noce\n1\n");*/
        fprintf(tfp, "%s\n", otpsfile);

        /* close the process */
        pclose(tfp);

        /* now read results from predict_tide and rewrite them in a useful form */
        if ((tfp = fopen(otpsfile, "r")) == NULL)
          {
          error = MB_ERROR_OPEN_FAIL;
          fprintf(stderr,
            "\nUnable to open predict_time results temporary file <%s>\n",
            otpsfile);
          fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
          exit(MB_FAILURE);
          }
        if ((ofp = fopen(tide_file, "w")) == NULL)
          {
          error = MB_ERROR_OPEN_FAIL;
          fprintf(stderr, "\nUnable to open tide output file <%s>\n", tide_file);
          fprintf(stderr, "\nProgram <%s> Terminated\n", program_name);
          exit(MB_FAILURE);
          }
        fprintf(ofp, "# Tide model generated by program %s\n", program_name);
        fprintf(ofp, "# MB-System Version: %s\n", MB_VERSION);
        fprintf(ofp, "# Tide model generated by program %s\n", program_name);
        fprintf(ofp, "# which in turn calls OTPS program predict_tide obtained from:\n");
        fprintf(ofp, "#     http://www.coas.oregonstate.edu/research/po/research/tide/\n");
        right_now = time((time_t *)0);
        strcpy(date, ctime(&right_now));
        date[strlen(date) - 1] = '\0';
        if ((user_ptr = getenv("USER")) == NULL)
          user_ptr = getenv("LOGNAME");
        if (user_ptr != NULL)
          strcpy(user, user_ptr);
        else
          strcpy(user, "unknown");
        gethostname(host, MBP_FILENAMESIZE);
        fprintf(ofp, "# Run by user <%s> on cpu <%s> at <%s>\n", user, host, date);

        /* loop over tide model values, writing them out in the specified format */
        nline = 0;
        ngood = 0;
        while ((result = fgets(line, MB_PATH_MAXLINE, tfp)) == line)
          {
          nline++;
          if (( nline == 2) || ( nline == 3) )
            {
            fprintf(ofp, "#%s", line);
            }
          else if (nline > 6)
            {
            nget = sscanf(line,
              "%lf %lf %d.%d.%d %d:%d:%d %lf %lf",
              &lat,
              &lon,
              &time_i[1],
              &time_i[2],
              &time_i[0],
              &time_i[3],
              &time_i[4],
              &time_i[5],
              &tide,
              &depth);
            if (nget == 10)
              {
              ngood++;

              /* if tide station data have been loaded, interpolate the
               * correction value to apply to the tide model */
              if (mbotps_mode & MBOTPS_MODE_TIDESTATION && (ntidestation > 0))
                {
                intstat = mb_linear_interp(verbose,
                  tidestation_time_d - 1,
                  tidestation_correction - 1,
                  ntidestation,
                  time_d,
                  &correction,
                  &itime,
                  &error);
                if (intstat == MB_SUCCESS)
                  tide += correction;
// fprintf(stderr,"TIDE STATION CORRECTION: intstat:%d itime:%dof%d time_d:%f correction:%f   tide:%f\n",
// intstat, itime, ntidestation, time_d, correction, tide);
                }

              /* write out the tide model */
              if (tideformat == 2)
                {
                fprintf(ofp,
                  "%4.4d %2.2d %2.2d %2.2d %2.2d %2.2d %9.4f\n",
                  time_i[0],
                  time_i[1],
                  time_i[2],
                  time_i[3],
                  time_i[4],
                  time_i[5],
                  tide);
                }
              else
                {
                mb_get_time(verbose, time_i, &time_d);
                fprintf(ofp, "%.3f %9.4f\n", time_d, tide);
                }
              }
            }
          }
        fclose(tfp);
        fclose(ofp);

        /* remove the temporary files */
        unlink(lltfile);
        unlink(otpsfile);

        /* some helpful output */
        fprintf(stderr, "\nResults are really in %s\n", tide_file);

        /* set mbprocess usage of tide file */
        if (( mbprocess_update == MB_YES) && ( ngood > 0) )
          {
          status = mb_pr_update_tide(verbose,
            swath_file,
            MBP_TIDE_ON,
            tide_file,
            tideformat,
            &error);
          fprintf(stderr, "MBprocess set to apply tide correction to %s\n", swath_file);
          }
        }

      /* figure out whether and what to read next */
      if (read_datalist == MB_YES)
        {
        if ((status =
          mb_datalist_read(verbose, datalist, file, dfile, &format, &file_weight,
            &error)) == MB_SUCCESS)
          read_data = MB_YES;
        else
          read_data = MB_NO;
        }
      else
        {
        read_data = MB_NO;
        }

      /* end loop over files in list */
      }
    if (read_datalist == MB_YES)
      mb_datalist_close(verbose, &datalist, &error);
    }

  /* check memory */
  if (verbose >= 4)
    status = mb_memory_list(verbose, &error);

  /* print output debug statements */
  if (verbose >= 2)
    {
    fprintf(stderr, "\ndbg2  Program <%s> completed\n", program_name);
    fprintf(stderr, "dbg2  Ending status:\n");
    fprintf(stderr, "dbg2       status:  %d\n", status);
    }

  /* end it all */
  exit(error);
}  /* main */
Exemplo n.º 7
0
/*--------------------------------------------------------------------*/
int mb_read_init(int verbose, char *file,
		int format, int pings, int lonflip, double bounds[4],
		int btime_i[7], int etime_i[7],
		double speedmin, double timegap,
		void **mbio_ptr, double *btime_d, double *etime_d,
		int *beams_bath, int *beams_amp, int *pixels_ss,
		int *error)
{
	char	*function_name = "mb_read_init";
	int	status;
	struct mb_io_struct *mb_io_ptr;
	int	status_save;
	int	error_save;
	int	sapi_status;
	char	*lastslash;
	char	path[MB_PATH_MAXLINE], name[MB_PATH_MAXLINE];
	char	prjfile[MB_PATH_MAXLINE];
	char	projection_id[MB_NAME_LENGTH];
	int	proj_status;
	FILE	*pfp;
	struct stat file_status;
	int	fstat;
	int	nscan;
	int	i;
	char	*stdin_string = "stdin";

	/* print input debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Revision id: %s\n",rcs_id);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       verbose:    %d\n",verbose);
		fprintf(stderr,"dbg2       file:       %s\n",file);
		fprintf(stderr,"dbg2       format:     %d\n",format);
		fprintf(stderr,"dbg2       pings:      %d\n",pings);
		fprintf(stderr,"dbg2       lonflip:    %d\n",lonflip);
		fprintf(stderr,"dbg2       bounds[0]:  %f\n",bounds[0]);
		fprintf(stderr,"dbg2       bounds[1]:  %f\n",bounds[1]);
		fprintf(stderr,"dbg2       bounds[2]:  %f\n",bounds[2]);
		fprintf(stderr,"dbg2       bounds[3]:  %f\n",bounds[3]);
		fprintf(stderr,"dbg2       btime_i[0]: %d\n",btime_i[0]);
		fprintf(stderr,"dbg2       btime_i[1]: %d\n",btime_i[1]);
		fprintf(stderr,"dbg2       btime_i[2]: %d\n",btime_i[2]);
		fprintf(stderr,"dbg2       btime_i[3]: %d\n",btime_i[3]);
		fprintf(stderr,"dbg2       btime_i[4]: %d\n",btime_i[4]);
		fprintf(stderr,"dbg2       btime_i[5]: %d\n",btime_i[5]);
		fprintf(stderr,"dbg2       btime_i[6]: %d\n",btime_i[6]);
		fprintf(stderr,"dbg2       etime_i[0]: %d\n",etime_i[0]);
		fprintf(stderr,"dbg2       etime_i[1]: %d\n",etime_i[1]);
		fprintf(stderr,"dbg2       etime_i[2]: %d\n",etime_i[2]);
		fprintf(stderr,"dbg2       etime_i[3]: %d\n",etime_i[3]);
		fprintf(stderr,"dbg2       etime_i[4]: %d\n",etime_i[4]);
		fprintf(stderr,"dbg2       etime_i[5]: %d\n",etime_i[5]);
		fprintf(stderr,"dbg2       etime_i[6]: %d\n",etime_i[6]);
		fprintf(stderr,"dbg2       speedmin:   %f\n",speedmin);
		fprintf(stderr,"dbg2       timegap:    %f\n",timegap);
		}

	/* allocate memory for mbio descriptor */
	status = mb_mallocd(verbose,__FILE__, __LINE__,sizeof(struct mb_io_struct),
				(void **) mbio_ptr,error);
	if (status == MB_SUCCESS)
		{
		memset(*mbio_ptr, 0, sizeof(struct mb_io_struct));
		mb_io_ptr = (struct mb_io_struct *) *mbio_ptr;
		}

	/* set system byte order flag */
	if (status == MB_SUCCESS)
		{
		mb_io_ptr->byteswapped = mb_swap_check();
		}

	/* get format information */
	if (status == MB_SUCCESS)
		{
		status = mb_format_register(verbose, &format,
					*mbio_ptr, error);
		}

	/* quit if there is a problem */
	if (status == MB_FAILURE)
		{
		/* free memory for mbio descriptor */
		if (mbio_ptr != NULL)
			{
			status_save = status;
			error_save = *error;
			status = mb_freed(verbose,__FILE__, __LINE__,(void **)mbio_ptr,error);
			status = status_save;
			*error = error_save;
			}

		/* output debug information */
		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);
		}

	/* initialize file access for the mbio descriptor */
	mb_io_ptr->filemode = MB_FILEMODE_READ;
	mb_io_ptr->mbfp = NULL;
	strcpy(mb_io_ptr->file,file);
	mb_io_ptr->file_pos = 0;
	mb_io_ptr->file_bytes = 0;
	mb_io_ptr->mbfp2 = NULL;
	strcpy(mb_io_ptr->file2,"\0");
	mb_io_ptr->file2_pos = 0;
	mb_io_ptr->file2_bytes = 0;
	mb_io_ptr->mbfp3 = NULL;
	strcpy(mb_io_ptr->file3,"\0");
	mb_io_ptr->file3_pos = 0;
	mb_io_ptr->file3_bytes = 0;
	mb_io_ptr->ncid = 0;
#ifdef WITH_GSF
	mb_io_ptr->gsfid = 0;
#else
        /* TODO: possibly set to -666 */
#endif
	mb_io_ptr->xdrs = NULL;
	mb_io_ptr->xdrs2 = NULL;
	mb_io_ptr->xdrs3 = NULL;

	/* load control parameters into the mbio descriptor */
	mb_io_ptr->format = format;
	mb_io_ptr->pings = pings;
	mb_io_ptr->lonflip = lonflip;
	for (i=0;i<4;i++)
		mb_io_ptr->bounds[i] = bounds[i];
	for (i=0;i<7;i++)
		{
		mb_io_ptr->btime_i[i] = btime_i[i];
		mb_io_ptr->etime_i[i] = etime_i[i];
		}
	mb_io_ptr->speedmin = speedmin;
	mb_io_ptr->timegap = timegap;

	/* get mbio internal time */
	status = mb_get_time(verbose,mb_io_ptr->btime_i,btime_d);
	status = mb_get_time(verbose,mb_io_ptr->etime_i,etime_d);
	mb_io_ptr->btime_d = *btime_d;
	mb_io_ptr->etime_d = *etime_d;

	/* set the number of beams and allocate storage arrays */
	*beams_bath = mb_io_ptr->beams_bath_max;
	*beams_amp = mb_io_ptr->beams_amp_max;
	*pixels_ss = mb_io_ptr->pixels_ss_max;
	mb_io_ptr->new_beams_bath = 0;
	mb_io_ptr->new_beams_amp = 0;
	mb_io_ptr->new_pixels_ss = 0;
	if (verbose >= 4)
		{
		fprintf(stderr,"\ndbg4  Beam and pixel dimensions set in MBIO function <%s>\n",
				function_name);
		fprintf(stderr,"dbg4       beams_bath: %d\n",
			mb_io_ptr->beams_bath_max);
		fprintf(stderr,"dbg4       beams_amp:  %d\n",
			mb_io_ptr->beams_amp_max);
		fprintf(stderr,"dbg4       pixels_ss:  %d\n",
			mb_io_ptr->pixels_ss_max);
		}

	/* initialize pointers */
	mb_io_ptr->raw_data = NULL;
	mb_io_ptr->store_data = NULL;
	mb_io_ptr->beamflag = NULL;
	mb_io_ptr->bath = NULL;
	mb_io_ptr->amp = NULL;
	mb_io_ptr->bath_acrosstrack = NULL;
	mb_io_ptr->bath_alongtrack = NULL;
	mb_io_ptr->bath_num = NULL;
	mb_io_ptr->amp_num = NULL;
	mb_io_ptr->ss = NULL;
	mb_io_ptr->ss_acrosstrack = NULL;
	mb_io_ptr->ss_alongtrack = NULL;
	mb_io_ptr->ss_num = NULL;
	mb_io_ptr->new_beamflag = NULL;
	mb_io_ptr->new_bath = NULL;
	mb_io_ptr->new_amp = NULL;
	mb_io_ptr->new_bath_acrosstrack = NULL;
	mb_io_ptr->new_bath_alongtrack = NULL;
	mb_io_ptr->new_ss = NULL;
	mb_io_ptr->new_ss_acrosstrack = NULL;
	mb_io_ptr->new_ss_alongtrack = NULL;

	/* initialize projection parameters */
	mb_io_ptr->projection_initialized = MB_NO;
	mb_io_ptr->projection_id[0] = '\0';
	mb_io_ptr->pjptr = NULL;

	/* initialize ancillary variables used
		to save information in certain cases */
	mb_io_ptr->save_flag = MB_NO;
	mb_io_ptr->save_label_flag = MB_NO;
	mb_io_ptr->save1 = 0;
	mb_io_ptr->save2 = 0;
	mb_io_ptr->save3 = 0;
	mb_io_ptr->save4 = 0;
	mb_io_ptr->save5 = 0;
	mb_io_ptr->save6 = 0;
	mb_io_ptr->save7 = 0;
	mb_io_ptr->save8 = 0;
	mb_io_ptr->save9 = 0;
	mb_io_ptr->save10 = 0;
	mb_io_ptr->save11 = 0;
	mb_io_ptr->save12 = 0;
	mb_io_ptr->save13 = 0;
	mb_io_ptr->save14 = 0;
	mb_io_ptr->saved1 = 0;
	mb_io_ptr->saved2 = 0;
	mb_io_ptr->saved3 = 0;
	mb_io_ptr->saved4 = 0;
	mb_io_ptr->saved5 = 0;
	mb_io_ptr->saveptr1 = NULL;
	mb_io_ptr->saveptr2 = NULL;

	/* allocate arrays */
	mb_io_ptr->beams_bath_alloc = mb_io_ptr->beams_bath_max;
	mb_io_ptr->beams_amp_alloc = mb_io_ptr->beams_amp_max;
	mb_io_ptr->pixels_ss_alloc = mb_io_ptr->pixels_ss_max;
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_bath_alloc*sizeof(char),
				(void **) &mb_io_ptr->beamflag,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_bath_alloc*sizeof(double),
				(void **) &mb_io_ptr->bath,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_amp_alloc*sizeof(double),
				(void **) &mb_io_ptr->amp,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_bath_alloc*sizeof(double),
				(void **) &mb_io_ptr->bath_acrosstrack,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_bath_alloc*sizeof(double),
				(void **) &mb_io_ptr->bath_alongtrack,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_bath_alloc*sizeof(int),
				(void **) &mb_io_ptr->bath_num,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_amp_alloc*sizeof(int),
				(void **) &mb_io_ptr->amp_num,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->pixels_ss_alloc*sizeof(double),
				(void **) &mb_io_ptr->ss,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->pixels_ss_alloc*sizeof(double),
				(void **) &mb_io_ptr->ss_acrosstrack,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->pixels_ss_alloc*sizeof(double),
				(void **) &mb_io_ptr->ss_alongtrack,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->pixels_ss_alloc*sizeof(int),
				(void **) &mb_io_ptr->ss_num,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_bath_alloc*sizeof(char),
				(void **) &mb_io_ptr->new_beamflag,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_bath_alloc*sizeof(double),
				(void **) &mb_io_ptr->new_bath,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_amp_alloc*sizeof(double),
				(void **) &mb_io_ptr->new_amp,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_bath_alloc*sizeof(double),
				(void **) &mb_io_ptr->new_bath_acrosstrack,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->beams_bath_alloc*sizeof(double),
				(void **) &mb_io_ptr->new_bath_alongtrack,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->pixels_ss_alloc*sizeof(double),
				(void **) &mb_io_ptr->new_ss,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->pixels_ss_alloc*sizeof(double),
				(void **) &mb_io_ptr->new_ss_acrosstrack,error);
	if (status == MB_SUCCESS)
		status = mb_mallocd(verbose,__FILE__, __LINE__,mb_io_ptr->pixels_ss_alloc*sizeof(double),
				(void **) &mb_io_ptr->new_ss_alongtrack,error);

	/* call routine to allocate memory for format dependent i/o */
	if (status == MB_SUCCESS)
		status = (*mb_io_ptr->mb_io_format_alloc)(verbose,*mbio_ptr,error);

	/* deal with a memory allocation failure */
	if (status == MB_FAILURE)
		{
		status = mb_deall_ioarrays(verbose, mbio_ptr, error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr,error);
		mb_io_ptr->beams_bath_alloc = 0;
		mb_io_ptr->beams_amp_alloc = 0;
		mb_io_ptr->pixels_ss_alloc = 0;
		status = MB_FAILURE;
		*error = MB_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);
		}

	/* handle normal or xdr files to be opened
	   directly with fopen */
	if (mb_io_ptr->filetype == MB_FILETYPE_NORMAL
	    || mb_io_ptr->filetype == MB_FILETYPE_XDR)
	    {
	    /* open the first file */
	    if (strncmp(file,stdin_string,5) == 0)
		mb_io_ptr->mbfp = stdin;
	    else
		if ((mb_io_ptr->mbfp = fopen(mb_io_ptr->file, "r")) == NULL)
		    {
		    *error = MB_ERROR_OPEN_FAIL;
		    status = MB_FAILURE;
		    }

	    /* open the second file if required */
	    if (status == MB_SUCCESS
		&& mb_io_ptr->numfile >= 2)
		{
		if ((mb_io_ptr->mbfp2 = fopen(mb_io_ptr->file2, "r")) == NULL)
		    {
		    *error = MB_ERROR_OPEN_FAIL;
		    status = MB_FAILURE;
		    }
		}

	    /* or open the second file if desired and possible */
	    else if (status == MB_SUCCESS
		&& mb_io_ptr->numfile <= -2)
		{
		if ((fstat = stat(mb_io_ptr->file2, &file_status)) == 0
		    && (file_status.st_mode & S_IFMT) != S_IFDIR
		    && file_status.st_size > 0)
			mb_io_ptr->mbfp2 = fopen(mb_io_ptr->file2, "r");
		}

	    /* open the third file if required */
	    if (status == MB_SUCCESS
		&& mb_io_ptr->numfile >= 3)
		{
		if ((mb_io_ptr->mbfp3 = fopen(mb_io_ptr->file3, "r")) == NULL)
		    {
		    *error = MB_ERROR_OPEN_FAIL;
		    status = MB_FAILURE;
		    }
		}

	    /* or open the third file if desired and possible */
	    else if (status == MB_SUCCESS
		&& mb_io_ptr->numfile <= -3)
		{
		if ((fstat = stat(mb_io_ptr->file2, &file_status)) == 0
		    && (file_status.st_mode & S_IFMT) != S_IFDIR
		    && file_status.st_size > 0)
			mb_io_ptr->mbfp3 = fopen(mb_io_ptr->file3, "r");
		}

	    /* if needed, initialize XDR stream */
	    if (status == MB_SUCCESS
		&& mb_io_ptr->filetype == MB_FILETYPE_XDR)
		{
		status = mb_mallocd(verbose,__FILE__, __LINE__,sizeof(XDR),
				(void **) &mb_io_ptr->xdrs,error);
		if (status == MB_SUCCESS)
		    {
		    xdrstdio_create((XDR *)mb_io_ptr->xdrs,
			    mb_io_ptr->mbfp, XDR_DECODE);
		    }
		else
		    {
		    status = MB_FAILURE;
		    *error = MB_ERROR_MEMORY_FAIL;
		    }
		}

	    /* if needed, initialize second XDR stream */
	    if (status == MB_SUCCESS
		&& mb_io_ptr->filetype == MB_FILETYPE_XDR
		&& (mb_io_ptr->numfile >= 2 || mb_io_ptr->numfile <= -2)
		&& mb_io_ptr->mbfp2 != NULL)
		{
		status = mb_mallocd(verbose,__FILE__, __LINE__,sizeof(XDR),
				(void **) &mb_io_ptr->xdrs2,error);
		if (status == MB_SUCCESS)
		    {
		    xdrstdio_create((XDR *)mb_io_ptr->xdrs2,
			    mb_io_ptr->mbfp2, XDR_DECODE);
		    }
		else
		    {
		    status = MB_FAILURE;
		    *error = MB_ERROR_MEMORY_FAIL;
		    }
		}

	    /* if needed, initialize third XDR stream */
	    if (status == MB_SUCCESS
		&& mb_io_ptr->filetype == MB_FILETYPE_XDR
		&& (mb_io_ptr->numfile >= 3 || mb_io_ptr->numfile <= -3)
		&& mb_io_ptr->mbfp3 != NULL)
		{
		status = mb_mallocd(verbose,__FILE__, __LINE__,sizeof(XDR),
				(void **) &mb_io_ptr->xdrs3,error);
		if (status == MB_SUCCESS)
		    {
		    xdrstdio_create((XDR *)mb_io_ptr->xdrs3,
			    mb_io_ptr->mbfp3, XDR_DECODE);
		    }
		else
		    {
		    status = MB_FAILURE;
		    *error = MB_ERROR_MEMORY_FAIL;
		    }
		}
	    }

	/* else handle single normal files to be opened with mb_fileio_open() */
	else if (mb_io_ptr->filetype == MB_FILETYPE_SINGLE)
	    {
	    status = mb_fileio_open(verbose, *mbio_ptr, error);
	    }

#ifdef WITH_GSF
	/* else handle gsf files to be opened with gsflib */
	else if (mb_io_ptr->filetype == MB_FILETYPE_GSF)
	    {
	    status = gsfOpen(mb_io_ptr->file,
				GSF_READONLY,
				(int *) &(mb_io_ptr->gsfid));
	    if (status == 0)
		{
		status = MB_SUCCESS;
		*error = MB_ERROR_NO_ERROR;
		}
	    else
		{
		status = MB_FAILURE;
		*error = MB_ERROR_OPEN_FAIL;
		}
	    }
#else
        /* TODO: should issue an error */
#endif

	/* else handle netcdf files to be opened with libnetcdf */
	else if (mb_io_ptr->filetype == MB_FILETYPE_NETCDF)
	    {
	    status = nc_open(mb_io_ptr->file,
				NC_NOWRITE,
				(int *) &(mb_io_ptr->ncid));
	    if (status == 0)
		{
		status = MB_SUCCESS;
		*error = MB_ERROR_NO_ERROR;
		}
	    else
		{
		status = MB_FAILURE;
		*error = MB_ERROR_OPEN_FAIL;
		}
	    }

	/* else handle surf files to be opened with libsapi */
	else if (mb_io_ptr->filetype == MB_FILETYPE_SURF)
	    {
	    lastslash = strrchr(file, '/');
	    if (lastslash != NULL && strlen(lastslash) > 1)
	    	{
		strcpy(name,&(lastslash[1]));
		strcpy(path,file);
		path[strlen(file) - strlen(lastslash)] = '\0';
		}
	    else if (strlen(file) > 0)
	    	{
		strcpy(path, ".");
		strcpy(name, file);
		}
	    else
	     	{
		status = MB_FAILURE;
		*error = MB_ERROR_OPEN_FAIL;
		}
	    if (status == MB_SUCCESS)
	    	{
		if (strcmp(&name[strlen(name)-4],".sda") == 0)
			name[strlen(name)-4] = '\0';
		else if (strcmp(&name[strlen(name)-4],".SDA") == 0)
			name[strlen(name)-4] = '\0';
		else if (strcmp(&name[strlen(name)-4],".six") == 0)
			name[strlen(name)-4] = '\0';
		else if (strcmp(&name[strlen(name)-4],".SIX") == 0)
			name[strlen(name)-4] = '\0';
	    	sapi_status = SAPI_open(path,name,verbose);
	    	if (sapi_status == 0)
			{
			status = MB_SUCCESS;
			*error = MB_ERROR_NO_ERROR;
			}
	    	else
			{
			status = MB_FAILURE;
			*error = MB_ERROR_OPEN_FAIL;
			}
		}
	    else
		{
		status = MB_FAILURE;
		*error = MB_ERROR_OPEN_FAIL;
		}
	    }

	/* else handle segy files to be opened with mb_segy */
	else if (mb_io_ptr->filetype == MB_FILETYPE_SEGY)
	    {
	    status = mb_segy_read_init(verbose, mb_io_ptr->file,
		(void **)&(mb_io_ptr->mbfp), NULL, NULL, error);
	    if (status != MB_SUCCESS)
		{
		status = MB_FAILURE;
		*error = MB_ERROR_OPEN_FAIL;
		}
	    }

	/* if error terminate */
	if (status == MB_FAILURE)
		{
		/* save status and error values */
		status_save = status;
		error_save = *error;

		/* free allocated memory */
		if (mb_io_ptr->filetype == MB_FILETYPE_XDR
		    && mb_io_ptr->xdrs != NULL)
			status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->xdrs,error);
		if (mb_io_ptr->filetype == MB_FILETYPE_XDR
		    && mb_io_ptr->xdrs2 != NULL)
			status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->xdrs2,error);
		if (mb_io_ptr->filetype == MB_FILETYPE_XDR
		    && mb_io_ptr->xdrs3 != NULL)
			status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->xdrs3,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->beamflag,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->bath,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->amp,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->bath_acrosstrack,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->bath_alongtrack,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->bath_num,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->amp_num,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->ss,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->ss_acrosstrack,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->ss_alongtrack,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->ss_num,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->new_beamflag,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->new_bath,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->new_amp,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->new_bath_acrosstrack,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->new_bath_alongtrack,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->new_ss,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->new_ss_acrosstrack,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr->new_ss_alongtrack,error);
		status = mb_freed(verbose,__FILE__, __LINE__,(void **)&mb_io_ptr,error);

		/* restore error and status values */
		status = status_save;
		*error = error_save;

		/* output debug message */
		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);
		}

	/* initialize the working variables */
	mb_io_ptr->ping_count = 0;
	mb_io_ptr->nav_count = 0;
	mb_io_ptr->comment_count = 0;
	if (pings == 0)
		mb_io_ptr->pings_avg = 2;
	else
		mb_io_ptr->pings_avg = pings;
	mb_io_ptr->pings_read = 0;
	mb_io_ptr->error_save = MB_ERROR_NO_ERROR;
	mb_io_ptr->last_time_d = 0.0;
	mb_io_ptr->last_lon = 0.0;
	mb_io_ptr->last_lat = 0.0;
	mb_io_ptr->old_time_d = 0.0;
	mb_io_ptr->old_lon = 0.0;
	mb_io_ptr->old_lat = 0.0;
	mb_io_ptr->old_ntime_d = 0.0;
	mb_io_ptr->old_nlon = 0.0;
	mb_io_ptr->old_nlat = 0.0;
	mb_io_ptr->time_d = 0.0;
	mb_io_ptr->lon = 0.0;
	mb_io_ptr->lat = 0.0;
	mb_io_ptr->speed = 0.0;
	mb_io_ptr->heading = 0.0;
	for (i=0;i<mb_io_ptr->beams_bath_max;i++)
		{
		mb_io_ptr->beamflag[i] = MB_FLAG_NULL;
		mb_io_ptr->bath[i] = 0.0;
		mb_io_ptr->bath_acrosstrack[i] = 0.0;
		mb_io_ptr->bath_alongtrack[i] = 0.0;
		mb_io_ptr->bath_num[i] = 0;
		}
	for (i=0;i<mb_io_ptr->beams_amp_max;i++)
		{
		mb_io_ptr->amp[i] = 0.0;
		mb_io_ptr->amp_num[i] = 0;
		}
	for (i=0;i<mb_io_ptr->pixels_ss_max;i++)
		{
		mb_io_ptr->ss[i] = 0.0;
		mb_io_ptr->ss_acrosstrack[i] = 0.0;
		mb_io_ptr->ss_alongtrack[i] = 0.0;
		mb_io_ptr->ss_num[i] = 0;
		}
	mb_io_ptr->need_new_ping = MB_YES;

	/* initialize variables for interpolating asynchronous data */
	mb_io_ptr->nfix = 0;
	mb_io_ptr->nattitude = 0;
	mb_io_ptr->nheading = 0;
	mb_io_ptr->nsonardepth = 0;
	mb_io_ptr->naltitude = 0;
	for (i=0;i<MB_ASYNCH_SAVE_MAX;i++)
		{
		mb_io_ptr->fix_time_d[i] = 0.0;
		mb_io_ptr->fix_lon[i] = 0.0;
		mb_io_ptr->fix_lat[i] = 0.0;
		mb_io_ptr->attitude_time_d[i] = 0.0;
		mb_io_ptr->attitude_heave[i] = 0.0;
		mb_io_ptr->attitude_roll[i] = 0.0;
		mb_io_ptr->attitude_pitch[i] = 0.0;
		mb_io_ptr->heading_time_d[i] = 0.0;
		mb_io_ptr->heading_heading[i] = 0.0;
		mb_io_ptr->sonardepth_time_d[i] = 0.0;
		mb_io_ptr->sonardepth_sonardepth[i] = 0.0;
		mb_io_ptr->altitude_time_d[i] = 0.0;
		mb_io_ptr->altitude_altitude[i] = 0.0;
		}

	/* initialize notices */
	for (i=0;i<MB_NOTICE_MAX;i++)
		mb_io_ptr->notice_list[i] = 0;

	/* check for projection specification file */
	sprintf(prjfile, "%s.prj", file);
	if ((pfp = fopen(prjfile, "r")) != NULL)
		{
		nscan = fscanf(pfp,"%s", projection_id);
		proj_status = mb_proj_init(verbose,projection_id,
			&(mb_io_ptr->pjptr), error);
		if (proj_status == MB_SUCCESS)
			{
			mb_io_ptr->projection_initialized = MB_YES;
			strcpy(mb_io_ptr->projection_id, projection_id);
			}
		else
			{
			fprintf(stderr, "Unable to initialize projection %s from file %s\n\n",
				projection_id, prjfile);
			}
		fclose(pfp);
		}
	else
		{
		*error = MB_ERROR_OPEN_FAIL;
		status = MB_FAILURE;
		}

	/* set error and status (if you got here you succeeded */
	*error = MB_ERROR_NO_ERROR;
	status = MB_SUCCESS;

	/* print output debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> completed\n",function_name);
		fprintf(stderr,"dbg2  Revision id: %s\n",rcs_id);
		fprintf(stderr,"dbg2  Return values:\n");
		fprintf(stderr,"dbg2       mbio_ptr:   %p\n",(void *)*mbio_ptr);
		fprintf(stderr,"dbg2       ->numfile:  %d\n",mb_io_ptr->numfile);
		fprintf(stderr,"dbg2       ->file:     %s\n",mb_io_ptr->file);
		if (mb_io_ptr->numfile >= 2 || mb_io_ptr->numfile <= -2)
		    fprintf(stderr,"dbg2       ->file2:    %s\n",mb_io_ptr->file2);
		if (mb_io_ptr->numfile >= 3 || mb_io_ptr->numfile <= -3)
		    fprintf(stderr,"dbg2       ->file3:    %s\n",mb_io_ptr->file3);
		fprintf(stderr,"dbg2       ->mbfp:     %p\n",(void *)mb_io_ptr->mbfp);
		if (mb_io_ptr->numfile >= 2 || mb_io_ptr->numfile <= -2)
		    fprintf(stderr,"dbg2       ->mbfp2:    %p\n",(void *)mb_io_ptr->mbfp2);
		if (mb_io_ptr->numfile >= 3 || mb_io_ptr->numfile <= -3)
		    fprintf(stderr,"dbg2       ->mbfp3:    %p\n",(void *)mb_io_ptr->mbfp3);
		fprintf(stderr,"dbg2       btime_d:    %f\n",*btime_d);
		fprintf(stderr,"dbg2       etime_d:    %f\n",*etime_d);
		fprintf(stderr,"dbg2       beams_bath: %d\n",*beams_bath);
		fprintf(stderr,"dbg2       beams_amp:  %d\n",*beams_amp);
		fprintf(stderr,"dbg2       pixels_ss:  %d\n",*pixels_ss);
		fprintf(stderr,"dbg2       error:      %d\n",*error);
		fprintf(stderr,"dbg2  Return status:\n");
		fprintf(stderr,"dbg2       status:  %d\n",status);
		}

	/* return status */
	return(status);
}
Exemplo n.º 8
0
/*--------------------------------------------------------------------*/
int mbr_wt_hsldedmb(int verbose, void *mbio_ptr, void *store_ptr, int *error)
{
	char	*function_name = "mbr_wt_hsldedmb";
	int	status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbf_hsldedmb_struct *dataplus;
	struct mbf_hsldedmb_data_struct *data;
	struct mbsys_hsds_struct *store;
	char	*datacomment;
	int	time_i[7];
	double	time_d;
	int	i;
	int	id;

	/* print input debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       verbose:    %d\n",verbose);
		fprintf(stderr,"dbg2       mbio_ptr:   %lu\n",(size_t)mbio_ptr);
		fprintf(stderr,"dbg2       store_ptr:  %lu\n",(size_t)store_ptr);
		}

	/* get pointer to mbio descriptor */
	mb_io_ptr = (struct mb_io_struct *) mbio_ptr;

	/* get pointer to raw data structure */
	dataplus = (struct mbf_hsldedmb_struct *) mb_io_ptr->raw_data;
	data = &(dataplus->data);
	datacomment = (char *) data;
	store = (struct mbsys_hsds_struct *) store_ptr;

	/* print debug statements */
	if (verbose >= 5)
		{
		fprintf(stderr,"\ndbg5  Status at beginning of MBIO function <%s>\n",function_name);
		if (store != NULL)
			fprintf(stderr,"dbg5       store->kind:    %d\n",
				store->kind);
		fprintf(stderr,"dbg5       new_kind:       %d\n",
			mb_io_ptr->new_kind);
		fprintf(stderr,"dbg5       new_error:      %d\n",
			mb_io_ptr->new_error);
		fprintf(stderr,"dbg5       error:          %d\n",*error);
		fprintf(stderr,"dbg5       status:         %d\n",status);
		}

	/* first set some plausible amounts for some of the 
		variables in the HSLDEDMB record */
	data->course = 0;
	data->pitch = 0;
	data->scale = 100;	/* this is a unit scale factor */
	data->speed_ref = 'B';	/* assume speed is over the ground */
	data->quality = '\0';
	for (i=0;i<4;i++)
		data->flag[i] = 0.0;

	/* second translate values from hydrosweep data storage structure */
	if (store != NULL)
		{
		dataplus->kind = store->kind;
		if (store->kind == MB_DATA_DATA)
			{
			/* position */
			if (store->lon < -180.0)
				store->lon = store->lon + 360.0;
			if (store->lon > 180.0)
				store->lon = store->lon - 360.0;
			data->lon = (int)(0.5 + 10000000.0*store->lon);
			data->lat = (int)(0.5 + 10000000.0*store->lat);

			/* time stamp */
			time_i[0] = store->year;
			time_i[1] = store->month;
			time_i[2] = store->day;
			time_i[3] = store->hour;
			time_i[4] = store->minute;
			time_i[5] = store->second;
			time_i[6] = 0;
			mb_get_time(verbose,time_i,&time_d);
			data->seconds = (int) time_d;

			/* additional navigation and depths */
			data->heading = 10.0*store->course_true;
			data->course = 10.0*store->course_ground;
			data->speed = 10*store->speed;
			data->speed_ref = store->speed_reference[0];
			data->pitch = 10.0*store->pitch;
			data->scale = 100*store->depth_scale;
			id = MBSYS_HSDS_BEAMS - 1;
			for (i=0;i<MBSYS_HSDS_BEAMS;i++)
				{
				data->range[i] = store->distance[id-i];
				data->depth[i] = store->depth[id-i];
				}
			}

		/* comment */
		else if (store->kind == MB_DATA_COMMENT)
			{
			strcpy(datacomment,"zzzz");
			strncat(datacomment,store->comment,MBSYS_HSDS_MAXLINE);
			}
		}

	/* byte swap the data if necessary */
#ifdef BYTESWAPPED
	if (dataplus->kind == MB_DATA_DATA)
		{
		data->seconds = mb_swap_int(data->seconds);
		data->microseconds = mb_swap_int(data->microseconds);
		data->alt_seconds = mb_swap_int(data->alt_seconds);
		data->alt_microseconds = mb_swap_int(data->alt_microseconds);
		data->lat = mb_swap_int(data->lat);
		data->lon = mb_swap_int(data->lon);
		data->heading = mb_swap_short(data->heading);
		data->course = mb_swap_short(data->course);
		data->speed = mb_swap_short(data->speed);
		data->pitch = mb_swap_short(data->pitch);
		data->scale = mb_swap_short(data->scale);
		for (i=0;i<MBSYS_HSDS_BEAMS;i++)
			{
			data->depth[i] = mb_swap_short(data->depth[i]);
			data->range[i] = mb_swap_short(data->range[i]);
			}
		for (i=0;i<4;i++)
			data->flag[i] = mb_swap_int(data->flag[i]);

		}
#endif

	/* write next record to file */
	if (dataplus->kind == MB_DATA_DATA
		|| dataplus->kind == MB_DATA_COMMENT)
		{
		if ((status = fwrite(data,1,mb_io_ptr->data_structure_size,
			mb_io_ptr->mbfp)) 
			== mb_io_ptr->data_structure_size) 
			{
			status = MB_SUCCESS;
			*error = MB_ERROR_NO_ERROR;
			}
		else
			{
			status = MB_FAILURE;
			*error = MB_ERROR_WRITE_FAIL;
			}
		}
	else
		{
		status = MB_SUCCESS;
		*error = MB_ERROR_NO_ERROR;
		if (verbose >= 5)
			fprintf(stderr,"\ndbg5  No data written in MBIO function <%s>\n",function_name);
		}

	/* 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);
}
Exemplo n.º 9
0
int main (int argc, char **argv)
{
	static char rcs_id[] = "$Id$";
	static char program_name[] = "mbotps";
	static char help_message[] =  "MBotps predicts tides using methods and data derived from the OSU Tidal Prediction Software (OTPS) distributions.";
	static char usage_message[] = "mbotps [-Atideformat -Byear/month/day/hour/minute/second -Dinterval\n\t-Eyear/month/day/hour/minute/second -Fformat\n"
					"\t-Idatalist.mb-1 -Lopts_path -Ooutput -Potps_location -Rlon/lat -Tmodel -V]";
	extern char *optarg;
	int	errflg = 0;
	int	c;
	int	help = 0;
	int	flag = 0;

	/* MBIO status variables */
	int	status = MB_SUCCESS;
	int	verbose = 0;
	int	error = MB_ERROR_NO_ERROR;
	char	*message;

	/* MBIO read control parameters */
	int	read_datalist = MB_NO;
	mb_path	read_file;
	void	*datalist;
	int	look_processed = MB_DATALIST_LOOK_UNSET;
	double	file_weight;
	mb_path	swath_file;
	mb_path	file;
	int	format;
	int	pings;
	int	lonflip;
	double	bounds[4];
	double	speedmin;
	double	timegap;
	int	beams_bath;
	int	beams_amp;
	int	pixels_ss;

	/* MBIO read values */
	void	*mbio_ptr = NULL;
	void	*store_ptr = NULL;
	int	kind;
	int	time_i[7];
	double	time_d;
	double	navlon;
	double	navlat;
	double	speed;
	double	heading;
	double	distance;
	double	altitude;
	double	sonardepth;
	char	*beamflag = NULL;
	double	*bath = NULL;
	double	*bathacrosstrack = NULL;
	double	*bathalongtrack = NULL;
	double	*amp = NULL;
	double	*ss = NULL;
	double	*ssacrosstrack = NULL;
	double	*ssalongtrack = NULL;
	char	comment[MB_COMMENT_MAXLINE];

	/* mbotps control parameters */
	mb_path	otps_location_use;
	int	notpsmodels = 0;
	int	nmodeldatafiles = 0;
	int	mbotps_mode = MBOTPS_MODE_POSITION;
	double	tidelon;
	double	tidelat;
	double	btime_d;
	double	etime_d;
	int	btime_i[7];
	int	etime_i[7];
	double	interval = 300.0;
	mb_path	tidefile;
	int	mbprocess_update = MB_NO;
	int	tideformat = 2;
	int	ngood;

	/* time parameters */
	time_t	right_now;
	char	date[32], user[MB_PATH_MAXLINE], *user_ptr, host[MB_PATH_MAXLINE];
	int	pid;

	FILE	*tfp, *mfp, *ofp;
	struct stat file_status;
	int	fstat;
	mb_path	lltfile;
	mb_path	otpsfile;
	mb_path	line;
	mb_path	predict_tide;
	int	otps_model_set = MB_NO;
	mb_path	otps_model;
	mb_path	modelname;
	mb_path	modelfile;
	mb_path	modeldatafile;
	int	read_data;
	int	ntime;
	int	nread;
	int	nline;
	int	nget;
	int	output;
	double	savetime_d;
	double	lasttime_d;
	double	lastlon;
	double	lastlat;
	double	lon;
	double	lat;
	double	tide;
	double	depth;
	char	*result;
	int	i;

	/* get current default values */
	status = mb_defaults(verbose,&format,&pings,&lonflip,bounds,
		btime_i,etime_i,&speedmin,&timegap);

	/* set default input to datalist.mb-1 */
	strcpy (read_file, "datalist.mb-1");

	/* set default location of the OTPS package */
	strcpy(otps_location_use, otps_location);

	/* set defaults for the AUV survey we were running on Coaxial Segment, Juan de Fuca Ridge
		while I wrote this code */
	sprintf(otps_model, "tpxo7.2");
	sprintf(tidefile, "tide_model.txt");
	tidelon = -129.588618;
	tidelat = 46.50459;
	interval = 60.0;
	btime_i[0] = 2009;
	btime_i[1] = 7;
	btime_i[2] = 31;
	btime_i[3] = 0;
	btime_i[4] = 0;
	btime_i[5] = 0;
	btime_i[6] = 0;
	etime_i[0] = 2009;
	etime_i[1] = 8;
	etime_i[2] = 2;
	etime_i[3] = 1;
	etime_i[4] = 0;
	etime_i[5] = 0;
	etime_i[6] = 0;

	/* process argument list */
	while ((c = getopt(argc, argv, "A:a:B:b:D:d:E:e:F:f:I:i:MmO:o:P:p:R:r:T:t:VvHh")) != -1)
	  switch (c)
		{
		case 'H':
		case 'h':
			help++;
			break;
		case 'V':
		case 'v':
			verbose++;
			break;
		case 'A':
		case 'a':
			sscanf (optarg,"%d", &tideformat);
			if (tideformat != 2)
				tideformat = 1;
			break;
		case 'B':
		case 'b':
			sscanf (optarg,"%d/%d/%d/%d/%d/%d",
				&btime_i[0],&btime_i[1],&btime_i[2],
				&btime_i[3],&btime_i[4],&btime_i[5]);
			btime_i[6] = 0;
			flag++;
			break;
		case 'D':
		case 'd':
			sscanf (optarg,"%lf", &interval);
			break;
		case 'E':
		case 'e':
			sscanf (optarg,"%d/%d/%d/%d/%d/%d",
				&etime_i[0],&etime_i[1],&etime_i[2],
				&etime_i[3],&etime_i[4],&etime_i[5]);
			etime_i[6] = 0;
			flag++;
			break;
		case 'F':
		case 'f':
			sscanf (optarg,"%d", &format);
			flag++;
			break;
		case 'I':
		case 'i':
			sscanf (optarg,"%s", read_file);
			mbotps_mode = MBOTPS_MODE_NAVIGATION;
			flag++;
			break;
		case 'M':
		case 'm':
			mbprocess_update = MB_YES;
			break;
		case 'O':
		case 'o':
			sscanf (optarg,"%s", tidefile);
			break;
		case 'P':
		case 'p':
			sscanf (optarg,"%s", otps_location_use);
			break;
		case 'R':
		case 'r':
			sscanf (optarg,"%lf/%lf", &tidelon, &tidelat);
			break;
		case 'T':
		case 't':
			sscanf (optarg,"%s", otps_model);
			otps_model_set = MB_YES;
			break;
		case '?':
			errflg++;
		}

	/* if error flagged then print it and exit */
	if (errflg)
		{
		fprintf(stderr,"usage: %s\n", usage_message);
		fprintf(stderr,"\nProgram <%s> Terminated\n",
			program_name);
		error = MB_ERROR_BAD_USAGE;
		exit(error);
		}

	/* print starting message */
	if (verbose == 1 || help)
		{
		fprintf(stderr,"\nProgram %s\n",program_name);
		fprintf(stderr,"Version %s\n",rcs_id);
		fprintf(stderr,"MB-system Version %s\n",MB_VERSION);
		}

	/* if help desired then print it and exit */
	if (help)
		{
		fprintf(stderr,"\n%s\n",help_message);
		fprintf(stderr,"\nusage: %s\n", usage_message);
		}

	/* Check for available tide models */
	if (help || verbose > 0)
		{
		fprintf(stderr,"\nChecking for available OTPS tide models\n");
		fprintf(stderr,"OTPS location: %s\nValid OTPS tidal models:\n", otps_location_use);
		}
	notpsmodels = 0;
	sprintf(line, "/bin/ls -1 %s/DATA | grep Model_ | sed \"s/^Model_//\"", otps_location_use);
	if ((tfp = popen(line, "r")) != NULL)
		{
		/* send relevant input to predict_tide through its stdin stream */
		while (fgets(line, sizeof(line), tfp))
			{
			sscanf(line, "%s", modelname);
			sprintf(modelfile, "%s/DATA/Model_%s", otps_location_use, modelname);
			nmodeldatafiles = 0;

			/* check the files referenced in the model file */
			if ((mfp = fopen(modelfile, "r")) != NULL)
				{
				/* stat the file referenced in each line */
				while (fgets(modeldatafile, MB_PATH_MAXLINE, mfp) != NULL)
					{
					if (strlen(modeldatafile) > 0)
						modeldatafile[strlen(modeldatafile)-1] = '\0';
					if ((fstat = stat(modeldatafile, &file_status)) == 0
						&& (file_status.st_mode & S_IFMT) != S_IFDIR)
						{
						nmodeldatafiles++;
						}
					}
				fclose(mfp);
				}
			if (nmodeldatafiles >= 3)
				{
				if (help || verbose > 0)
					fprintf(stderr,"     %s\n", modelname);
				if (otps_model_set == MB_NO)
					{
					if (notpsmodels == 0 || strcmp(modelname, "tpxo7.2") == 0)
						strcpy(otps_model, modelname);
					}
				notpsmodels++;
				}
			}

		/* close the process */
		pclose(tfp);
		}
	else
		{
		error = MB_ERROR_OPEN_FAIL;
		fprintf(stderr,"\nUnable to open ls using popen()\n");
		fprintf(stderr,"\nProgram <%s> Terminated\n",
			program_name);
		exit(MB_FAILURE);
		}
	if (help || verbose > 0)
		{
		fprintf(stderr,"Number of available OTPS tide models: %d\n", notpsmodels);
		fprintf(stderr,"\nUsing OTPS tide model:            %s\n", otps_model);
		}

	/* print starting debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  Program <%s>\n",program_name);
		fprintf(stderr,"dbg2  Version %s\n",rcs_id);
		fprintf(stderr,"dbg2  MB-system Version %s\n",MB_VERSION);
		fprintf(stderr,"dbg2  Control Parameters:\n");
		fprintf(stderr,"dbg2       verbose:          %d\n",verbose);
		fprintf(stderr,"dbg2       help:             %d\n",help);
		fprintf(stderr,"dbg2       otps_location:    %s\n",otps_location);
		fprintf(stderr,"dbg2       otps_location_use:%s\n",otps_location_use);
		fprintf(stderr,"dbg2       otps_model_set:   %d\n",otps_model_set);
		fprintf(stderr,"dbg2       otps_model:       %s\n",otps_model);
		fprintf(stderr,"dbg2       mbotps_mode:      %d\n",mbotps_mode);
		fprintf(stderr,"dbg2       tidelon:          %f\n",tidelon);
		fprintf(stderr,"dbg2       tidelat:          %f\n",tidelat);
		fprintf(stderr,"dbg2       btime_i[0]:       %d\n",btime_i[0]);
		fprintf(stderr,"dbg2       btime_i[1]:       %d\n",btime_i[1]);
		fprintf(stderr,"dbg2       btime_i[2]:       %d\n",btime_i[2]);
		fprintf(stderr,"dbg2       btime_i[3]:       %d\n",btime_i[3]);
		fprintf(stderr,"dbg2       btime_i[4]:       %d\n",btime_i[4]);
		fprintf(stderr,"dbg2       btime_i[5]:       %d\n",btime_i[5]);
		fprintf(stderr,"dbg2       btime_i[6]:       %d\n",btime_i[6]);
		fprintf(stderr,"dbg2       etime_i[0]:       %d\n",etime_i[0]);
		fprintf(stderr,"dbg2       etime_i[1]:       %d\n",etime_i[1]);
		fprintf(stderr,"dbg2       etime_i[2]:       %d\n",etime_i[2]);
		fprintf(stderr,"dbg2       etime_i[3]:       %d\n",etime_i[3]);
		fprintf(stderr,"dbg2       etime_i[4]:       %d\n",etime_i[4]);
		fprintf(stderr,"dbg2       etime_i[5]:       %d\n",etime_i[5]);
		fprintf(stderr,"dbg2       etime_i[6]:       %d\n",etime_i[6]);
		fprintf(stderr,"dbg2       interval:         %f\n",interval);
		fprintf(stderr,"dbg2       tidefile:         %s\n",tidefile);
		fprintf(stderr,"dbg2       mbprocess_update: %d\n",mbprocess_update);
		fprintf(stderr,"dbg2       tideformat:       %d\n",tideformat);
		fprintf(stderr,"dbg2       format:           %d\n",format);
		fprintf(stderr,"dbg2       read_file:        %s\n",read_file);
		}

	/* exit if no valid OTPS models can be found */
	if (notpsmodels <= 0)
		{
		error = MB_ERROR_OPEN_FAIL;
		fprintf(stderr,"\nUnable to find a valid OTPS tidal model\n");
		fprintf(stderr,"\nProgram <%s> Terminated\n",
			program_name);
		exit(MB_FAILURE);
		}

	/* if help was all that was desired then exit */
	if (help)
		{
		exit(error);
		}

	/* get tides for a single position and time range */
	if (mbotps_mode == MBOTPS_MODE_POSITION)
		{
		/* first open temporary file of lat lon time */
		pid = getpid();
		sprintf(lltfile, "tmp_mbotps_llt_%d.txt", pid);
		sprintf(otpsfile, "tmp_mbotps_llttd_%d.txt", pid);
		if ((tfp = fopen(lltfile,"w")) == NULL)
			{
			error = MB_ERROR_OPEN_FAIL;
			fprintf(stderr,"\nUnable to open temporary lat-lon-time file <%s> for writing\n",lltfile);
			fprintf(stderr,"\nProgram <%s> Terminated\n",
				program_name);
			exit(MB_FAILURE);
			}

		/* make sure longitude is positive */
		if (tidelon < 0.0)
			tidelon += 360.0;

		/* loop over the time of interest generating the lat-lon-time values */
		mb_get_time(verbose, btime_i, &btime_d);
		mb_get_time(verbose, etime_i, &etime_d);
		ntime = 1 + (int)floor((etime_d - btime_d) / interval);
		for (i=0;i<ntime;i++)
			{
			time_d = btime_d + i * interval;
			mb_get_date(verbose, time_d, time_i);
			fprintf(tfp, "%.6f %.6f %4.4d %2.2d %2.2d %2.2d %2.2d %2.2d\n",
				tidelat, tidelon, time_i[0], time_i[1], time_i[2], time_i[3], time_i[4], time_i[5]);
			}

		/* close the llt file */
		fclose(tfp);

		/* call predict_tide with popen */
		sprintf(predict_tide, "%s/predict_tide", otps_location_use);
		if ((tfp = popen(predict_tide, "w")) == NULL)
			{
			error = MB_ERROR_OPEN_FAIL;
			fprintf(stderr,"\nUnable to open predict_time program using popen()\n");
			fprintf(stderr,"\nProgram <%s> Terminated\n",
				program_name);
			exit(MB_FAILURE);
			}

		/* send relevant input to predict_tide through its stdin stream */
		fprintf(tfp, "%s/DATA/Model_%s\n", otps_location_use,otps_model);
		fprintf(tfp, "%s\n", lltfile);
		fprintf(tfp, "z\n\nAP\noce\n1\n");
		/*fprintf(tfp, "z\nm2,s2,n2,k2,k1,o1,p1,q1\nAP\noce\n1\n");*/
		fprintf(tfp, "%s\n", otpsfile);

		/* close the process */
		pclose(tfp);

		/* now read results from predict_tide and rewrite them in a useful form */
		if ((tfp = fopen(otpsfile, "r")) == NULL)
			{
			error = MB_ERROR_OPEN_FAIL;
			fprintf(stderr,"\nUnable to open predict_time results temporary file <%s>\n", otpsfile);
			fprintf(stderr,"\nProgram <%s> Terminated\n",
				program_name);
			exit(MB_FAILURE);
			}
		if ((ofp = fopen(tidefile, "w")) == NULL)
			{
			error = MB_ERROR_OPEN_FAIL;
			fprintf(stderr,"\nUnable to open tide output file <%s>\n", tidefile);
			fprintf(stderr,"\nProgram <%s> Terminated\n",
				program_name);
			exit(MB_FAILURE);
			}
		fprintf(ofp, "# Tide model generated by program %s\n", program_name);
		fprintf(ofp, "# Version: %s\n", rcs_id);
		fprintf(ofp, "# MB-System Version: %s\n", MB_VERSION);
		fprintf(ofp, "# Tide model generated by program %s\n", program_name);
		fprintf(ofp, "# which in turn calls OTPS program predict_tide obtained from:\n");
		fprintf(ofp, "#     http://www.coas.oregonstate.edu/research/po/research/tide/\n");
		fprintf(ofp, "#\n");
		fprintf(ofp, "# OTPSnc tide model: \n");
		fprintf(ofp, "#      %s\n",otps_model);
		if (tideformat == 2)
			{
			fprintf(ofp, "# Output format:\n");
			fprintf(ofp, "#      year month day hour minute second tide\n");
			fprintf(ofp, "# where tide is in meters\n");
			}
		else
			{
			fprintf(ofp, "# Output format:\n");
			fprintf(ofp, "#      time_d tide\n");
			fprintf(ofp, "# where time_d is in seconds since January 1, 1970\n");
			fprintf(ofp, "# and tide is in meters\n");
			}
		right_now = time((time_t *)0);
		strcpy(date,ctime(&right_now));
                date[strlen(date)-1] = '\0';
		if ((user_ptr = getenv("USER")) == NULL)
			user_ptr = getenv("LOGNAME");
		if (user_ptr != NULL)
			strcpy(user,user_ptr);
		else
			strcpy(user, "unknown");
		gethostname(host,MBP_FILENAMESIZE);
		fprintf(ofp,"# Run by user <%s> on cpu <%s> at <%s>\n", user,host,date);

		nline = 0;
		ngood = 0;
		while ((result = fgets(line,MB_PATH_MAXLINE,tfp)) == line)
			{
			nline++;
			if (nline == 2 || nline == 3)
				{
				fprintf(ofp, "#%s", line);
				}
			else if (nline > 6)
				{
				nget = sscanf(line,"%lf %lf %d.%d.%d %d:%d:%d %lf %lf",
					&lat, &lon, &time_i[1], &time_i[2], &time_i[0],
					&time_i[3], &time_i[4], &time_i[5], &tide, &depth);
				if (nget == 10)
					{
					ngood++;
					if (tideformat ==2)
						{
						fprintf(ofp, "%4.4d %2.2d %2.2d %2.2d %2.2d %2.2d %9.4f\n",
							time_i[0], time_i[1],  time_i[2],
							time_i[3], time_i[4],  time_i[5], tide);
						}
					else
						{
						mb_get_time(verbose,time_i,&time_d);
						fprintf(ofp, "%.3f %9.4f\n",time_d, tide);
						}
					}
				}
			}
		fclose(tfp);
		fclose(ofp);

		/* remove the temporary files */
		unlink("lltfile");
		unlink("otpsfile");

		/* some helpful output */
		fprintf(stderr, "\nResults are really in %s\n", tidefile);
		} /* end single position mode */

	/* else get tides along the navigation contained in a set of swath files */
	else if (mbotps_mode == MBOTPS_MODE_NAVIGATION)
		{
		/* get format if required */
		if (format == 0)
			mb_get_format(verbose,read_file,NULL,&format,&error);

		/* determine whether to read one file or a list of files */
		if (format < 0)
			read_datalist = MB_YES;

		/* open file list */
		if (read_datalist == MB_YES)
		    {
		    if ((status = mb_datalist_open(verbose,&datalist,
						    read_file,look_processed,&error)) != MB_SUCCESS)
			{
			error = MB_ERROR_OPEN_FAIL;
			fprintf(stderr,"\nUnable to open data list file: %s\n",
				read_file);
			fprintf(stderr,"\nProgram <%s> Terminated\n",
				program_name);
			exit(error);
			}
		    if ((status = mb_datalist_read(verbose,datalist,
				    file,&format,&file_weight,&error))
				    == MB_SUCCESS)
			read_data = MB_YES;
		    else
			read_data = MB_NO;
		    }
		/* else copy single filename to be read */
		else
		    {
		    strcpy(file, read_file);
		    read_data = MB_YES;
		    }

		/* loop over all files to be read */
		while (read_data == MB_YES)
			{
			/* some helpful output */
			fprintf(stderr, "\n---------------------------------------\n\nProcessing tides for %s\n\n", file);

			/* first open temporary file of lat lon time */
			pid = getpid();
			strcpy(swath_file, file);
			sprintf(lltfile, "tmp_mbotps_llt_%d.txt", pid);
			sprintf(otpsfile, "tmp_mbotps_llttd_%d.txt", pid);
			sprintf(tidefile, "%s.tde", file);
			if ((tfp = fopen(lltfile,"w")) == NULL)
				{
				error = MB_ERROR_OPEN_FAIL;
				fprintf(stderr,"\nUnable to open temporary lat-lon-time file <%s> for writing\n",lltfile);
				fprintf(stderr,"\nProgram <%s> Terminated\n",
					program_name);
				exit(MB_FAILURE);
				}

			/* read fnv file if possible */
			mb_get_fnv(verbose, file, &format, &error);

			/* initialize reading the swath file */
			if ((status = mb_read_init(
				verbose,file,format,pings,lonflip,bounds,
				btime_i,etime_i,speedmin,timegap,
				&mbio_ptr,&btime_d,&etime_d,
				&beams_bath,&beams_amp,&pixels_ss,&error)) != MB_SUCCESS)
				{
				mb_error(verbose,error,&message);
				fprintf(stderr,"\nMBIO Error returned from function <mb_read_init>:\n%s\n",message);
				fprintf(stderr,"\nMultibeam File <%s> not initialized for reading\n",file);
				fprintf(stderr,"\nProgram <%s> Terminated\n",
					program_name);
				exit(error);
				}

			/* allocate memory for data arrays */
			if (error == MB_ERROR_NO_ERROR)
				status = mb_register_array(verbose, mbio_ptr, MB_MEM_TYPE_BATHYMETRY,
								sizeof(char), (void **)&beamflag, &error);
			if (error == MB_ERROR_NO_ERROR)
				status = mb_register_array(verbose, mbio_ptr, MB_MEM_TYPE_BATHYMETRY,
								sizeof(double), (void **)&bath, &error);
			if (error == MB_ERROR_NO_ERROR)
				status = mb_register_array(verbose, mbio_ptr, MB_MEM_TYPE_AMPLITUDE,
								sizeof(double), (void **)&amp, &error);
			if (error == MB_ERROR_NO_ERROR)
				status = mb_register_array(verbose, mbio_ptr, MB_MEM_TYPE_BATHYMETRY,
								sizeof(double), (void **)&bathacrosstrack, &error);
			if (error == MB_ERROR_NO_ERROR)
				status = mb_register_array(verbose, mbio_ptr, MB_MEM_TYPE_BATHYMETRY,
								sizeof(double), (void **)&bathalongtrack, &error);
			if (error == MB_ERROR_NO_ERROR)
				status = mb_register_array(verbose, mbio_ptr, MB_MEM_TYPE_SIDESCAN,
								sizeof(double), (void **)&ss, &error);
			if (error == MB_ERROR_NO_ERROR)
				status = mb_register_array(verbose, mbio_ptr, MB_MEM_TYPE_SIDESCAN,
								sizeof(double), (void **)&ssacrosstrack, &error);
			if (error == MB_ERROR_NO_ERROR)
				status = mb_register_array(verbose, mbio_ptr, MB_MEM_TYPE_SIDESCAN,
								sizeof(double), (void **)&ssalongtrack, &error);

			/* if error initializing memory then quit */
			if (error != MB_ERROR_NO_ERROR)
				{
				mb_error(verbose,error,&message);
				fprintf(stderr,"\nMBIO Error allocating data arrays:\n%s\n",
					message);
				fprintf(stderr,"\nProgram <%s> Terminated\n",
					program_name);
				exit(error);
				}

			/* read and use data */
			nread = 0;
			while (error <= MB_ERROR_NO_ERROR)
				{
				/* reset error */
				error = MB_ERROR_NO_ERROR;
				output = MB_NO;

				/* read next data record */
				status = mb_get_all(verbose,mbio_ptr,&store_ptr,&kind,
				    time_i,&time_d,&navlon,&navlat,
				    &speed,&heading,
				    &distance,&altitude,&sonardepth,
				    &beams_bath,&beams_amp,&pixels_ss,
				    beamflag,bath,amp,bathacrosstrack,bathalongtrack,
				    ss,ssacrosstrack,ssalongtrack,
				    comment,&error);

				/* print debug statements */
				if (verbose >= 2)
					{
					fprintf(stderr,"\ndbg2  Ping read in program <%s>\n",
						program_name);
					fprintf(stderr,"dbg2       kind:           %d\n",kind);
					fprintf(stderr,"dbg2       error:          %d\n",error);
					fprintf(stderr,"dbg2       status:         %d\n",status);
					}

				/* deal with nav and time from survey data only - not nav, sidescan, or subbottom */
				if (error <= MB_ERROR_NO_ERROR && kind == MB_DATA_DATA)
					{
					/* flag positions and times for output at specified interval */
					if (nread == 0 || time_d - savetime_d >= interval)
						{
						savetime_d = time_d;
						output = MB_YES;
						}
					lasttime_d = time_d;
					lastlon = navlon;
					lastlat = navlat;

					/* increment counter */
					nread++;
					}

				/* output position and time if flagged or end of file */
				if (output == MB_YES || error == MB_ERROR_EOF)
					{
					if (lastlon < 0.0)
						lastlon += 360.0;
					mb_get_date(verbose, lasttime_d, time_i);
					fprintf(tfp, "%.6f %.6f %4.4d %2.2d %2.2d %2.2d %2.2d %2.2d\n",
							lastlat, lastlon, time_i[0], time_i[1], time_i[2],
							time_i[3], time_i[4], time_i[5]);
					}
				}

			/* close the swath file */
			status = mb_close(verbose,&mbio_ptr,&error);

			/* output read statistics */
			fprintf(stderr,"%d records read from %s\n", nread, file);

			/* close the llt file */
			fclose(tfp);

			/* call predict_tide with popen */
			sprintf(predict_tide, "%s/predict_tide", otps_location_use);
			if ((tfp = popen(predict_tide, "w")) == NULL)
				{
				error = MB_ERROR_OPEN_FAIL;
				fprintf(stderr,"\nUnable to open predict_time program using popen()\n");
				fprintf(stderr,"\nProgram <%s> Terminated\n",
					program_name);
				exit(MB_FAILURE);
				}

			/* send relevant input to predict_tide through its stdin stream */
			fprintf(tfp, "%s/DATA/Model_%s\n", otps_location_use,otps_model);
			fprintf(tfp, "%s\n", lltfile);
			fprintf(tfp, "z\n\nAP\noce\n1\n");
			/*fprintf(tfp, "z\nm2,s2,n2,k2,k1,o1,p1,q1\nAP\noce\n1\n");*/
			fprintf(tfp, "%s\n", otpsfile);

			/* close the process */
			pclose(tfp);

			/* now read results from predict_tide and rewrite them in a useful form */
			if ((tfp = fopen(otpsfile, "r")) == NULL)
				{
				error = MB_ERROR_OPEN_FAIL;
				fprintf(stderr,"\nUnable to open predict_time results temporary file <%s>\n", otpsfile);
				fprintf(stderr,"\nProgram <%s> Terminated\n",
					program_name);
				exit(MB_FAILURE);
				}
			if ((ofp = fopen(tidefile, "w")) == NULL)
				{
				error = MB_ERROR_OPEN_FAIL;
				fprintf(stderr,"\nUnable to open tide output file <%s>\n", tidefile);
				fprintf(stderr,"\nProgram <%s> Terminated\n",
					program_name);
				exit(MB_FAILURE);
				}
			fprintf(ofp, "# Tide model generated by program %s\n", program_name);
			fprintf(ofp, "# Version: %s\n", rcs_id);
			fprintf(ofp, "# MB-System Version: %s\n", MB_VERSION);
			fprintf(ofp, "# Tide model generated by program %s\n", program_name);
			fprintf(ofp, "# which in turn calls OTPS program predict_tide obtained from:\n");
			fprintf(ofp, "#     http://www.coas.oregonstate.edu/research/po/research/tide/\n");
			right_now = time((time_t *)0);
			strcpy(date,ctime(&right_now));
			date[strlen(date)-1] = '\0';
			if ((user_ptr = getenv("USER")) == NULL)
				user_ptr = getenv("LOGNAME");
			if (user_ptr != NULL)
				strcpy(user,user_ptr);
			else
				strcpy(user, "unknown");
			gethostname(host,MBP_FILENAMESIZE);
			fprintf(ofp,"# Run by user <%s> on cpu <%s> at <%s>\n", user,host,date);

			nline = 0;
			ngood = 0;
			while ((result = fgets(line,MB_PATH_MAXLINE,tfp)) == line)
				{
				nline++;
				if (nline == 2 || nline == 3)
					{
					fprintf(ofp, "#%s", line);
					}
				else if (nline > 6)
					{
					nget = sscanf(line,"%lf %lf %d.%d.%d %d:%d:%d %lf %lf",
						&lat, &lon, &time_i[1], &time_i[2], &time_i[0],
						&time_i[3], &time_i[4], &time_i[5], &tide, &depth);
					if (nget == 10)
						{
						ngood++;
						if (tideformat == 2)
							{
							fprintf(ofp, "%4.4d %2.2d %2.2d %2.2d %2.2d %2.2d %9.4f\n",
								time_i[0], time_i[1],  time_i[2],
								time_i[3], time_i[4],  time_i[5], tide);
							}
						else
							{
							mb_get_time(verbose,time_i,&time_d);
							fprintf(ofp, "%.3f %9.4f\n",time_d, tide);
							}
						}
					}
				}
			fclose(tfp);
			fclose(ofp);

			/* remove the temporary files */
			unlink(lltfile);
			unlink(otpsfile);

			/* some helpful output */
			fprintf(stderr, "\nResults are really in %s\n", tidefile);

			/* set mbprocess usage of tide file */
			if (mbprocess_update == MB_YES && ngood > 0)
				{
				status = mb_pr_update_tide(verbose, swath_file,
							MBP_TIDE_ON, tidefile, tideformat, &error);
				fprintf(stderr, "MBprocess set to apply tide correction to %s\n", swath_file);
				}

			/* figure out whether and what to read next */
        		if (read_datalist == MB_YES)
                		{
				if ((status = mb_datalist_read(verbose,datalist,
					    file,&format,&file_weight,&error))
					    == MB_SUCCESS)
                        		read_data = MB_YES;
                		else
                        		read_data = MB_NO;
                		}
        		else
                		{
                		read_data = MB_NO;
                		}

			/* end loop over files in list */
			}
		if (read_datalist == MB_YES)
			mb_datalist_close(verbose,&datalist,&error);
		}

	/* check memory */
	if (verbose >= 4)
		status = mb_memory_list(verbose,&error);

	/* print output debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  Program <%s> completed\n",
			program_name);
		fprintf(stderr,"dbg2  Ending status:\n");
		fprintf(stderr,"dbg2       status:  %d\n",status);
		}

	/* end it all */
	exit(error);
}
Exemplo n.º 10
0
/*--------------------------------------------------------------------*/
int mbsys_hsmd_extract_nav(int verbose, void *mbio_ptr, void *store_ptr,
		int *kind, int time_i[7], double *time_d,
		double *navlon, double *navlat,
		double *speed, double *heading, double *draft,
		double *roll, double *pitch, double *heave,
		int *error)
{
	char	*function_name = "mbsys_hsmd_extract_nav";
	int	status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbsys_hsmd_struct *store;

	/* print input debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Revision id: %s\n",rcs_id);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       verbose:    %d\n",verbose);
		fprintf(stderr,"dbg2       mb_ptr:     %p\n",(void *)mbio_ptr);
		fprintf(stderr,"dbg2       store_ptr:  %p\n",(void *)store_ptr);
		}

	/* get mbio descriptor */
	mb_io_ptr = (struct mb_io_struct *) mbio_ptr;

	/* get data structure pointer */
	store = (struct mbsys_hsmd_struct *) store_ptr;

	/* get data kind */
	*kind = store->kind;

	/* extract data from structure */
	if (*kind == MB_DATA_DATA
		|| *kind == MB_DATA_NAV)
		{
		/* get time */
		time_i[0] = store->year;
		time_i[1] = store->month;
		time_i[2] = store->day;
		time_i[3] = store->hour;
		time_i[4] = store->minute;
		time_i[5] = store->second;
		time_i[6] = store->millisecond;
		mb_get_time(verbose,time_i,time_d);

		/* get navigation */
		*navlon = store->lon;
		*navlat = store->lat;

		/* get heading */
		*heading = store->heading_tx;

		/* get speed */
		*speed = store->speed;

		/* get draft */
		*draft = 0.0;

		/* get roll pitch and heave */
		*roll = store->roll_tx;
		*pitch = store->pitch_tx;
		*heave = store->heave;

		/* print debug statements */
		if (verbose >= 5)
			{
			fprintf(stderr,"\ndbg4  Data extracted by MBIO function <%s>\n",
				function_name);
			fprintf(stderr,"dbg4  Extracted values:\n");
			fprintf(stderr,"dbg4       kind:       %d\n",
				*kind);
			fprintf(stderr,"dbg4       error:      %d\n",
				*error);
			fprintf(stderr,"dbg4       time_i[0]:  %d\n",
				time_i[0]);
			fprintf(stderr,"dbg4       time_i[1]:  %d\n",
				time_i[1]);
			fprintf(stderr,"dbg4       time_i[2]:  %d\n",
				time_i[2]);
			fprintf(stderr,"dbg4       time_i[3]:  %d\n",
				time_i[3]);
			fprintf(stderr,"dbg4       time_i[4]:  %d\n",
				time_i[4]);
			fprintf(stderr,"dbg4       time_i[5]:  %d\n",
				time_i[5]);
			fprintf(stderr,"dbg4       time_i[6]:  %d\n",
				time_i[6]);
			fprintf(stderr,"dbg4       time_d:     %f\n",
				*time_d);
			fprintf(stderr,"dbg4       longitude:  %f\n",
				*navlon);
			fprintf(stderr,"dbg4       latitude:   %f\n",
				*navlat);
			fprintf(stderr,"dbg4       speed:      %f\n",
				*speed);
			fprintf(stderr,"dbg4       heading:    %f\n",
				*heading);
			fprintf(stderr,"dbg4       draft:      %f\n",
				*draft);
			fprintf(stderr,"dbg4       roll:       %f\n",
				*roll);
			fprintf(stderr,"dbg4       pitch:      %f\n",
				*pitch);
			fprintf(stderr,"dbg4       heave:      %f\n",
				*heave);
			}

		/* done translating values */

		}

	/* deal with comment */
	else if (*kind == MB_DATA_COMMENT)
		{
		/* set status */
		*error = MB_ERROR_COMMENT;
		status = MB_FAILURE;
		}

	/* deal with other record type */
	else
		{
		/* set status */
		*error = MB_ERROR_OTHER;
		status = MB_FAILURE;
		}

	/* 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       kind:       %d\n",*kind);
		}
	if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR
		&& (*kind == MB_DATA_DATA
		    || *kind == MB_DATA_NAV))
		{
		fprintf(stderr,"dbg2       time_i[0]:     %d\n",time_i[0]);
		fprintf(stderr,"dbg2       time_i[1]:     %d\n",time_i[1]);
		fprintf(stderr,"dbg2       time_i[2]:     %d\n",time_i[2]);
		fprintf(stderr,"dbg2       time_i[3]:     %d\n",time_i[3]);
		fprintf(stderr,"dbg2       time_i[4]:     %d\n",time_i[4]);
		fprintf(stderr,"dbg2       time_i[5]:     %d\n",time_i[5]);
		fprintf(stderr,"dbg2       time_i[6]:     %d\n",time_i[6]);
		fprintf(stderr,"dbg2       time_d:        %f\n",*time_d);
		fprintf(stderr,"dbg2       longitude:     %f\n",*navlon);
		fprintf(stderr,"dbg2       latitude:      %f\n",*navlat);
		fprintf(stderr,"dbg2       speed:         %f\n",*speed);
		fprintf(stderr,"dbg2       heading:       %f\n",*heading);
		fprintf(stderr,"dbg2       draft:         %f\n",*draft);
		fprintf(stderr,"dbg2       roll:          %f\n",*roll);
		fprintf(stderr,"dbg2       pitch:         %f\n",*pitch);
		fprintf(stderr,"dbg2       heave:         %f\n",*heave);
		}
	if (verbose >= 2)
		{
		fprintf(stderr,"dbg2       error:      %d\n",*error);
		fprintf(stderr,"dbg2  Return status:\n");
		fprintf(stderr,"dbg2       status:     %d\n",status);
		}

	/* return status */
	return(status);
}
Exemplo n.º 11
0
/*--------------------------------------------------------------------*/
int mbsys_hsmd_extract(int verbose, void *mbio_ptr, void *store_ptr,
		int *kind, int time_i[7], double *time_d,
		double *navlon, double *navlat,
		double *speed, double *heading,
		int *nbath, int *namp, int *nss,
		char *beamflag, double *bath, double *amp,
		double *bathacrosstrack, double *bathalongtrack,
		double *ss, double *ssacrosstrack, double *ssalongtrack,
		char *comment, int *error)
{
	char	*function_name = "mbsys_hsmd_extract";
	int	status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbsys_hsmd_struct *store;
	int	i, j;

	/* print input debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Revision id: %s\n",rcs_id);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       verbose:    %d\n",verbose);
		fprintf(stderr,"dbg2       mb_ptr:     %p\n",(void *)mbio_ptr);
		fprintf(stderr,"dbg2       store_ptr:  %p\n",(void *)store_ptr);
		}


	/* get mbio descriptor */
	mb_io_ptr = (struct mb_io_struct *) mbio_ptr;

	/* get data structure pointer */
	store = (struct mbsys_hsmd_struct *) store_ptr;

	/* get data kind */
	*kind = store->kind;

	/* extract data from structure */
	if (*kind == MB_DATA_DATA)
		{
		/* get time */
		time_i[0] = store->year;
		time_i[1] = store->month;
		time_i[2] = store->day;
		time_i[3] = store->hour;
		time_i[4] = store->minute;
		time_i[5] = store->second;
		time_i[6] = store->millisecond;
		mb_get_time(verbose,time_i,time_d);

		/* get navigation */
		*navlon = store->lon;
		*navlat = store->lat;

		/* get heading */
		*heading = store->heading_tx;

		/* set speed to zero */
		*speed = store->speed;

		/* set beamwidths in mb_io structure */
		mb_io_ptr->beamwidth_ltrack = 1.7;
		mb_io_ptr->beamwidth_xtrack = 1.7;

		/* zero bathymetry and sidescan */
		for (i=0;i<MBSYS_HSMD_BEAMS;i++)
			{
			beamflag[i] = MB_FLAG_NULL;
			bath[i] = 0.0;
			bathacrosstrack[i] = 0.0;
			bathalongtrack[i] = 0.0;
			}
		for (i=0;i<MBSYS_HSMD_PIXELS;i++)
			{
			ss[i] = 0.0;
			ssacrosstrack[i] = 0.0;
			ssalongtrack[i] = 0.0;
			}

		/* get bathymetry */
		*nbath = MBSYS_HSMD_BEAMS;

		/* deal with a ping to port */
		if (store->Port == -1)
			{
			for (i=0;i<MBSYS_HSMD_BEAMS_PING;i++)
				{
				j = MBSYS_HSMD_BEAMS_PING - i - 1;
				if (store->depth[i] > 0.0)
				    {
				    beamflag[j] = MB_FLAG_NONE;
				    bath[j] = store->depth[i];
				    }
				else if (store->depth[i] < 0.0)
				    {
				    beamflag[j] =
					MB_FLAG_MANUAL + MB_FLAG_FLAG;
				    bath[j] = -store->depth[i];
				    }
				else
				    {
				    beamflag[j] = MB_FLAG_NULL;
				    bath[j] = store->depth[i];
				    }
				bathacrosstrack[j] = store->distance[i];
	 			bathalongtrack[j] = 0.0;
     	 			}
    			}

		/* deal with a ping to starboard */
		else
			{
			for (i=0;i<MBSYS_HSMD_BEAMS_PING;i++)
				{
				j = i + MBSYS_HSMD_BEAMS_PING - 1;
				if (store->depth[i] > 0.0)
				    {
				    beamflag[j] = MB_FLAG_NONE;
				    bath[j] = store->depth[i];
				    }
				else if (store->depth[i] < 0.0)
				    {
				    beamflag[j] =
					MB_FLAG_MANUAL + MB_FLAG_FLAG;
				    bath[j] = -store->depth[i];
				    }
				else
				    {
				    beamflag[j] = MB_FLAG_NULL;
				    bath[j] = store->depth[i];
				    }
	 			bathacrosstrack[j] =
	    					store->distance[i];
	 			bathalongtrack[j] = 0.0;
      				}
    			}

		/* Deal with the sidescan */
		*nss = MBSYS_HSMD_PIXELS;

		/* deal with a ping to port */
		if (store->Port == -1)
			{
			for (i=0;i<MBSYS_HSMD_PIXELS_PING;i++)
				{
				j = MBSYS_HSMD_PIXELS_PING - i - 1;
				ss[j] = store->ss[i];
				ssacrosstrack[j] =
					-store->ss_range * i
					/ ((double)(MBSYS_HSMD_PIXELS_PING - 1));
	 			ssalongtrack[j] = 0.0;
      				}
    			}

		/* deal with a ping to starboard */
		else
			{
			for (i=0;i<MBSYS_HSMD_PIXELS_PING;i++)
				{
				j = i + MBSYS_HSMD_PIXELS_PING - 1;
				ss[j] = store->ss[i];
				ssacrosstrack[j] =
					store->ss_range * i
					/ ((double)(MBSYS_HSMD_PIXELS_PING - 1));
	 			ssalongtrack[j] = 0.0;
      				}
    			}

		/* print debug statements */
		if (verbose >= 5)
			{
			fprintf(stderr,"\ndbg4  Data extracted by MBIO function <%s>\n",
				function_name);
			fprintf(stderr,"dbg4  Extracted values:\n");
			fprintf(stderr,"dbg4       kind:       %d\n",
				*kind);
			fprintf(stderr,"dbg4       error:      %d\n",
				*error);
			fprintf(stderr,"dbg4       time_i[0]:  %d\n",
				time_i[0]);
			fprintf(stderr,"dbg4       time_i[1]:  %d\n",
				time_i[1]);
			fprintf(stderr,"dbg4       time_i[2]:  %d\n",
				time_i[2]);
			fprintf(stderr,"dbg4       time_i[3]:  %d\n",
				time_i[3]);
			fprintf(stderr,"dbg4       time_i[4]:  %d\n",
				time_i[4]);
			fprintf(stderr,"dbg4       time_i[5]:  %d\n",
				time_i[5]);
			fprintf(stderr,"dbg4       time_i[6]:  %d\n",
				time_i[6]);
			fprintf(stderr,"dbg4       time_d:     %f\n",
				*time_d);
			fprintf(stderr,"dbg4       longitude:  %f\n",
				*navlon);
			fprintf(stderr,"dbg4       latitude:   %f\n",
				*navlat);
			fprintf(stderr,"dbg4       speed:      %f\n",
				*speed);
			fprintf(stderr,"dbg4       heading:    %f\n",
				*heading);
			fprintf(stderr,"dbg4       nbath:      %d\n",
				*nbath);
			for (i=0;i<*nbath;i++)
			  fprintf(stderr,"dbg4       beam:%d  flag:%3d  bath:%f  acrosstrack:%f  alongtrack:%f\n",
				i,beamflag[i],bath[i],
				bathacrosstrack[i],bathalongtrack[i]);
			fprintf(stderr,"dbg4        namp:      %d\n",
				*namp);
			for (i=0;i<*nss;i++)
			  fprintf(stderr,"dbg4        ss[%d]: %f  ssdist[%d]:%f\n",
				i,ss[i],i,ssacrosstrack[i]);
			}

		/* done translating values */

		}

	/* extract comment from structure */
	else if (*kind == MB_DATA_COMMENT)
		{
		/* copy comment */
		strcpy(comment,store->comment);

		/* print debug statements */
		if (verbose >= 4)
			{
			fprintf(stderr,"\ndbg4  New ping read by MBIO function <%s>\n",
				function_name);
			fprintf(stderr,"dbg4  New ping values:\n");
			fprintf(stderr,"dbg4       error:      %d\n",
				*error);
			fprintf(stderr,"dbg4       comment:    %s\n",
				comment);
			}
		}

	/* 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       kind:       %d\n",*kind);
		}
	if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR
		&& *kind == MB_DATA_COMMENT)
		{
		fprintf(stderr,"dbg2       comment:     \ndbg2       %s\n",
			comment);
		}
	else if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR
		&& *kind != MB_DATA_COMMENT)
		{
		fprintf(stderr,"dbg2       time_i[0]:     %d\n",time_i[0]);
		fprintf(stderr,"dbg2       time_i[1]:     %d\n",time_i[1]);
		fprintf(stderr,"dbg2       time_i[2]:     %d\n",time_i[2]);
		fprintf(stderr,"dbg2       time_i[3]:     %d\n",time_i[3]);
		fprintf(stderr,"dbg2       time_i[4]:     %d\n",time_i[4]);
		fprintf(stderr,"dbg2       time_i[5]:     %d\n",time_i[5]);
		fprintf(stderr,"dbg2       time_i[6]:     %d\n",time_i[6]);
		fprintf(stderr,"dbg2       time_d:        %f\n",*time_d);
		fprintf(stderr,"dbg2       longitude:     %f\n",*navlon);
		fprintf(stderr,"dbg2       latitude:      %f\n",*navlat);
		fprintf(stderr,"dbg2       speed:         %f\n",*speed);
		fprintf(stderr,"dbg2       heading:       %f\n",*heading);
		}
	if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR
		&& *kind == MB_DATA_DATA)
		{
		fprintf(stderr,"dbg2         nbath:         %d\n",*nbath);
		for (i=0;i<*nbath;i++)
		  fprintf(stderr,"dbg2       beam:%d  flag:%3d  bath:%f  acrosstrack:%f  alongtrack:%f\n",
			i,beamflag[i],bath[i],
			bathacrosstrack[i],bathalongtrack[i]);
		fprintf(stderr,"dbg2         nss:           %d\n",*nss);
		for (i=0;i<*nss;i++)
		  fprintf(stderr,"dbg2       ss[%d]:   %f  ssdist[%d]:   %f\n",
			i,ss[i],i,ssacrosstrack[i]);
		}
	if (verbose >= 2)
		{
		fprintf(stderr,"dbg2       error:      %d\n",*error);
		fprintf(stderr,"dbg2  Return status:\n");
		fprintf(stderr,"dbg2       status:     %d\n",status);
		}

	/* return status */
	return(status);
}
Exemplo n.º 12
0
/*--------------------------------------------------------------------*/
int mbr_mgd77txt_rd_data(int verbose, void *mbio_ptr, int *error)
{
	char	*function_name = "mbr_mgd77txt_rd_data";
	int	status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbf_mgd77txt_struct *data;
	int	*header_read;
	char	line[MB_COMMENT_MAXLINE];
	char	*read_ptr;
	int	shift;
	int 	neg_unit;
	int	itmp;
	double	dtmp;
	int	i;

	/* print input debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Revision id: %s\n",rcs_id);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       verbose:    %d\n",verbose);
		fprintf(stderr,"dbg2       mbio_ptr:   %p\n",(void *)mbio_ptr);
		}

	/* get pointer to mbio descriptor */
	mb_io_ptr = (struct mb_io_struct *) mbio_ptr;

	/* get pointer to raw data structure */
	data = (struct mbf_mgd77txt_struct *) mb_io_ptr->raw_data;
	header_read = (int *) &mb_io_ptr->save1;

	/* initialize everything to zeros */
	mbr_zero_mgd77txt(verbose,mb_io_ptr->raw_data,error);

	/* set file position */
	mb_io_ptr->file_bytes = ftell(mb_io_ptr->mbfp);
	mb_io_ptr->file_pos = mb_io_ptr->file_bytes;

	/* read next record */
	if ((read_ptr = fgets(line, MB_PATH_MAXLINE, mb_io_ptr->mbfp)) != NULL)
		{
		mb_io_ptr->file_bytes += strlen(line);
		status = MB_SUCCESS;
		*error = MB_ERROR_NO_ERROR;
		}
	else
		{
		status = MB_FAILURE;
		*error = MB_ERROR_EOF;
		}
	mb_io_ptr->file_bytes = ftell(mb_io_ptr->mbfp);

	/* handle the data */
	if (status == MB_SUCCESS
	    && *header_read > 0
	    && *header_read < MBF_MGD77TXT_HEADER_NUM)
	    {
	    data->kind = MB_DATA_HEADER;
	    (*header_read)++;
	    strncpy(data->comment, line, strlen(line)-2);
	    }
	else if (status == MB_SUCCESS
	    && (line[0] == '1' || line[0] == '4'))
	    {
	    data->kind = MB_DATA_HEADER;
	    (*header_read) = 1;
	    strncpy(data->comment, line, strlen(line)-2);
	    }
	else if (status == MB_SUCCESS
	    && line[0] == '#')
	    {
	    data->kind = MB_DATA_COMMENT;
            strncpy(data->comment,&line[1],strlen(line)-3);
	    }
	else if (status == MB_SUCCESS
	    && line[0] == '3')
	    {
	    data->kind = MB_DATA_DATA;

	    /* get survey id */
	    shift = 1;
	    for (i=0;i<8;i++)
		data->survey_id[i] = line[i+shift];

	    /* get time */
	    shift += 8;
	    mb_get_int(&data->timezone, &line[shift], 5); shift += 5;
	    data->timezone = data->timezone / 100;
	    mb_get_int(&itmp, &line[shift], 2); shift += 2;
	    mb_fix_y2k(verbose, itmp, &data->time_i[0]);
	    mb_get_int(&data->time_i[1], &line[shift], 2); shift += 2;
	    mb_get_int(&data->time_i[2], &line[shift], 2); shift += 2;
	    mb_get_int(&data->time_i[3], &line[shift], 2); shift += 2;
	    mb_get_int(&itmp, &line[shift], 5); shift += 5;
	    data->time_i[4] = 0.001 * itmp;
	    dtmp = (itmp - 1000 * data->time_i[4]) * 0.06;
	    data->time_i[5] = (int) dtmp;
	    data->time_i[6] = 1000000 * (dtmp - data->time_i[5]);
	    mb_get_time(verbose,data->time_i,&data->time_d);

	    /* get nav */
	    neg_unit = 8;
	    if (line[shift] == '-') {
		    shift += 1;
		    neg_unit = 7;
	    }
	    mb_get_int(&itmp, &line[shift], neg_unit); shift += neg_unit;
	    data->latitude = 0.00001 * itmp;
	    if (neg_unit == 7)
		    data->latitude = -data->latitude;

	    neg_unit = 9;
	    if (line[shift] == '-') {
		    shift += 1;
		    neg_unit = 8;
	    }
	    mb_get_int(&itmp, &line[shift], neg_unit); shift += neg_unit;
	    data->longitude = 0.00001 * itmp;
	    if (neg_unit == 8)
		    data->longitude = -data->longitude;
	    mb_get_int(&data->nav_type, &line[shift], 1); shift += 1;

	    /* get bath */
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->tt = 0.0001 * itmp;
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->bath = 0.1 * itmp;
	    mb_get_int(&data->bath_corr, &line[shift], 2); shift += 2;
	    mb_get_int(&data->bath_type, &line[shift], 1); shift += 1;
	    if (data->bath > 0.0 && data->bath < 99999.9)
		{
		data->flag = MB_FLAG_NONE;
		}
	    else
		{
		data->flag = MB_FLAG_NULL;
		}

	    /* get magnetics */
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->mag_tot_1 = 0.1 * itmp;
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->mag_tot_2 = 0.1 * itmp;
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->mag_res = 0.1 * itmp;
	    mb_get_int(&data->mag_res_sensor, &line[shift], 1); shift += 1;
	    mb_get_int(&itmp, &line[shift], 5); shift += 5;
	    data->mag_diurnal = 0.1 * itmp;
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->mag_altitude = itmp;

	    /* get gravity */
	    mb_get_int(&itmp, &line[shift], 7); shift += 7;
	    data->gravity = 0.1 * itmp;
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->eotvos = 0.1 * itmp;
	    mb_get_int(&itmp, &line[shift], 5); shift += 5;
	    data->free_air = 0.1 * itmp;
	    mb_get_int(&data->seismic_line, &line[shift], 5); shift += 5;
	    mb_get_int(&data->seismic_shot, &line[shift], 6); shift += 6;

	    /* get nav quality */
	    mb_get_int(&data->nav_quality, &line[shift], 1); shift += 1;
	    }
	else if (status == MB_SUCCESS
	    && line[0] == '5')
	    {
	    data->kind = MB_DATA_DATA;

	    /* get survey id */
	    shift = 1;
	    for (i=0;i<8;i++)
		data->survey_id[i] = line[i+shift];

	    /* get time */
	    shift += 8;
	    mb_get_int(&data->timezone, &line[shift], 3); shift += 3;
	    mb_get_int(&data->time_i[0], &line[shift], 4); shift += 4;
	    mb_get_int(&data->time_i[1], &line[shift], 2); shift += 2;
	    mb_get_int(&data->time_i[2], &line[shift], 2); shift += 2;
	    mb_get_int(&data->time_i[3], &line[shift], 2); shift += 2;
	    mb_get_int(&itmp, &line[shift], 5); shift += 5;
	    data->time_i[4] = 0.001 * itmp;
	    dtmp = (itmp - 1000 * data->time_i[4]) * 0.06;
	    data->time_i[5] = (int) dtmp;
	    data->time_i[6] = 1000000 * (dtmp - data->time_i[5]);

	    mb_get_time(verbose,data->time_i,&data->time_d);

	    /* get nav */
	    neg_unit = 8;
	    if (line[shift] == '-') {
		    shift += 1;
		    neg_unit = 7;
	    }
	    mb_get_int(&itmp, &line[shift], neg_unit); shift += neg_unit;
	    data->latitude = 0.00001 * itmp;
	    if (neg_unit == 7)
		    data->latitude = -data->latitude;

	    neg_unit = 9;
	    if (line[shift] == '-') {
		    shift += 1;
		    neg_unit = 8;
	    }
	    mb_get_int(&itmp, &line[shift], neg_unit); shift += neg_unit;
	    data->longitude = 0.00001 * itmp;
	    if (neg_unit == 8)
		    data->longitude = -data->longitude;

	    mb_get_int(&data->nav_type, &line[shift], 1); shift += 1;

	    /* get bath */
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->tt = 0.0001 * itmp;
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->bath = 0.1 * itmp;
	    mb_get_int(&data->bath_corr, &line[shift], 2); shift += 2;
	    mb_get_int(&data->bath_type, &line[shift], 1); shift += 1;
	    if (data->bath > 0.0 && data->bath < 99999.9)
		{
		data->flag = MB_FLAG_NONE;
		}
	    else
		{
		data->flag = MB_FLAG_NULL;
		}

	    /* get magnetics */
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->mag_tot_1 = 0.1 * itmp;
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->mag_tot_2 = 0.1 * itmp;
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->mag_res = 0.1 * itmp;
	    mb_get_int(&data->mag_res_sensor, &line[shift], 1); shift += 1;
	    mb_get_int(&itmp, &line[shift], 5); shift += 5;
	    data->mag_diurnal = 0.1 * itmp;
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->mag_altitude = itmp;

	    /* get gravity */
	    mb_get_int(&itmp, &line[shift], 7); shift += 7;
	    data->gravity = 0.1 * itmp;
	    mb_get_int(&itmp, &line[shift], 6); shift += 6;
	    data->eotvos = 0.1 * itmp;
	    mb_get_int(&itmp, &line[shift], 5); shift += 5;
	    data->free_air = 0.1 * itmp;
	    mb_get_int(&data->seismic_line, &line[shift], 5); shift += 5;
	    mb_get_int(&data->seismic_shot, &line[shift], 6); shift += 6;

	    /* get nav quality */
	    mb_get_int(&data->nav_quality, &line[shift], 1); shift += 1;
	    }

	/* 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);
}
Exemplo n.º 13
0
/*--------------------------------------------------------------------*/
int mbsys_hs10_extract_nav(int verbose, void *mbio_ptr, void *store_ptr,
		int *kind, int time_i[7], double *time_d,
		double *navlon, double *navlat,
		double *speed, double *heading, double *draft, 
		double *roll, double *pitch, double *heave, 
		int *error)
{
	char	*function_name = "mbsys_hs10_extract_nav";
	int	status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbsys_hs10_struct *store;

	/* print input debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       verbose:    %d\n",verbose);
		fprintf(stderr,"dbg2       mb_ptr:     %lu\n",(size_t)mbio_ptr);
		fprintf(stderr,"dbg2       store_ptr:  %lu\n",(size_t)store_ptr);
		}

	/* get mbio descriptor */
	mb_io_ptr = (struct mb_io_struct *) mbio_ptr;

	/* get data structure pointer */
	store = (struct mbsys_hs10_struct *) store_ptr;

	/* get data kind */
	*kind = store->kind;

	/* extract data from structure */
	if (*kind == MB_DATA_DATA)
		{
		/* get time */
		mb_fix_y2k(verbose, store->year, &time_i[0]);
		time_i[1] = store->month;
		time_i[2] = store->day;
		time_i[3] = store->hour;
		time_i[4] = store->minute;
		time_i[5] = store->tenth_second / 10;
		time_i[6] = 100000 * (store->tenth_second - 10 * time_i[5]);
		mb_get_time(verbose,time_i,time_d);

		/* get navigation */
		*navlon = ((double) store->londeg) + (store->lonmin / 60000.0);
		if (store->EorW == 'W')
		    *navlon *= -1.0;
		*navlat = ((double) store->latdeg) + (store->latmin / 60000.0);
		if (store->NorS == 'S')
		    *navlat *= -1.0;

		/* get heading */
		*heading = (0.1 * (double) store->heading);

		/* get speed */
		*speed = 0.0;

		/* get draft */
 		*draft = 0.0;

		/* get roll pitch and heave */
		*roll = 0.0;
		*pitch = 0.0;
		*heave = 0.0;

		/* print debug statements */
		if (verbose >= 5)
			{
			fprintf(stderr,"\ndbg4  Data extracted by MBIO function <%s>\n",
				function_name);
			fprintf(stderr,"dbg4  Extracted values:\n");
			fprintf(stderr,"dbg4       kind:       %d\n",
				*kind);
			fprintf(stderr,"dbg4       error:      %d\n",
				*error);
			fprintf(stderr,"dbg4       time_i[0]:  %d\n",
				time_i[0]);
			fprintf(stderr,"dbg4       time_i[1]:  %d\n",
				time_i[1]);
			fprintf(stderr,"dbg4       time_i[2]:  %d\n",
				time_i[2]);
			fprintf(stderr,"dbg4       time_i[3]:  %d\n",
				time_i[3]);
			fprintf(stderr,"dbg4       time_i[4]:  %d\n",
				time_i[4]);
			fprintf(stderr,"dbg4       time_i[5]:  %d\n",
				time_i[5]);
			fprintf(stderr,"dbg4       time_i[6]:  %d\n",
				time_i[6]);
			fprintf(stderr,"dbg4       time_d:     %f\n",
				*time_d);
			fprintf(stderr,"dbg4       longitude:  %f\n",
				*navlon);
			fprintf(stderr,"dbg4       latitude:   %f\n",
				*navlat);
			fprintf(stderr,"dbg4       speed:      %f\n",
				*speed);
			fprintf(stderr,"dbg4       heading:    %f\n",
				*heading);
			fprintf(stderr,"dbg4       draft:      %f\n",
				*draft);
			fprintf(stderr,"dbg4       roll:       %f\n",
				*roll);
			fprintf(stderr,"dbg4       pitch:      %f\n",
				*pitch);
			fprintf(stderr,"dbg4       heave:      %f\n",
				*heave);
			}

		/* done translating values */

		}

	/* deal with comment */
	else if (*kind == MB_DATA_COMMENT)
		{
		/* set status */
		*error = MB_ERROR_COMMENT;
		status = MB_FAILURE;
		}

	/* deal with other record type */
	else
		{
		/* set status */
		*error = MB_ERROR_OTHER;
		status = MB_FAILURE;
		}

	/* 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       kind:       %d\n",*kind);
		}
	if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR 
		&& *kind == MB_DATA_DATA)
		{
		fprintf(stderr,"dbg2       time_i[0]:     %d\n",time_i[0]);
		fprintf(stderr,"dbg2       time_i[1]:     %d\n",time_i[1]);
		fprintf(stderr,"dbg2       time_i[2]:     %d\n",time_i[2]);
		fprintf(stderr,"dbg2       time_i[3]:     %d\n",time_i[3]);
		fprintf(stderr,"dbg2       time_i[4]:     %d\n",time_i[4]);
		fprintf(stderr,"dbg2       time_i[5]:     %d\n",time_i[5]);
		fprintf(stderr,"dbg2       time_i[6]:     %d\n",time_i[6]);
		fprintf(stderr,"dbg2       time_d:        %f\n",*time_d);
		fprintf(stderr,"dbg2       longitude:     %f\n",*navlon);
		fprintf(stderr,"dbg2       latitude:      %f\n",*navlat);
		fprintf(stderr,"dbg2       speed:         %f\n",*speed);
		fprintf(stderr,"dbg2       heading:       %f\n",*heading);
		fprintf(stderr,"dbg2       draft:         %f\n",*draft);
		fprintf(stderr,"dbg2       roll:          %f\n",*roll);
		fprintf(stderr,"dbg2       pitch:         %f\n",*pitch);
		fprintf(stderr,"dbg2       heave:         %f\n",*heave);
		}
	if (verbose >= 2)
		{
		fprintf(stderr,"dbg2       error:      %d\n",*error);
		fprintf(stderr,"dbg2  Return status:\n");
		fprintf(stderr,"dbg2       status:     %d\n",status);
		}

	/* return status */
	return(status);
}
Exemplo n.º 14
0
/*--------------------------------------------------------------------*/
int mbsys_hs10_extract(int verbose, void *mbio_ptr, void *store_ptr, 
		int *kind, int time_i[7], double *time_d,
		double *navlon, double *navlat,
		double *speed, double *heading,
		int *nbath, int *namp, int *nss,
		char *beamflag, double *bath, double *amp, 
		double *bathacrosstrack, double *bathalongtrack,
		double *ss, double *ssacrosstrack, double *ssalongtrack,
		char *comment, int *error)
{
	char	*function_name = "mbsys_hs10_extract";
	int	status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbsys_hs10_struct *store;
	int	i;

	/* print input debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       verbose:    %d\n",verbose);
		fprintf(stderr,"dbg2       mb_ptr:     %lu\n",(size_t)mbio_ptr);
		fprintf(stderr,"dbg2       store_ptr:  %lu\n",(size_t)store_ptr);
		}

	/* get mbio descriptor */
	mb_io_ptr = (struct mb_io_struct *) mbio_ptr;

	/* get data structure pointer */
	store = (struct mbsys_hs10_struct *) store_ptr;

	/* get data kind */
	*kind = store->kind;

	/* extract data from structure */
	if (*kind == MB_DATA_DATA)
		{
		/* get time */
		mb_fix_y2k(verbose, store->year, &time_i[0]);
		time_i[1] = store->month;
		time_i[2] = store->day;
		time_i[3] = store->hour;
		time_i[4] = store->minute;
		time_i[5] = store->tenth_second / 10;
		time_i[6] = 100000 * (store->tenth_second - 10 * time_i[5]);
		mb_get_time(verbose,time_i,time_d);

		/* get navigation */
		*navlon = ((double) store->londeg) + (store->lonmin / 60000.0);
		if (store->EorW == 'W')
		    *navlon *= -1.0;
		*navlat = ((double) store->latdeg) + (store->latmin / 60000.0);
		if (store->NorS == 'S')
		    *navlat *= -1.0;

		/* get heading */
		*heading = (0.1 * (double) store->heading);

		/* get speed */
		*speed = 0.0;
			
		/* set beamwidths in mb_io structure */
		mb_io_ptr->beamwidth_ltrack = 3.0;
		mb_io_ptr->beamwidth_xtrack = 3.0;

		/* read distance and depth values into storage arrays */
		*nbath = MBSYS_HS10_BEAMS;
		*namp = MBSYS_HS10_BEAMS;
		*nss = 0;
		for (i=0;i<*nbath;i++)
			{
			if (store->depth[i] >= 29999)
			    {
			    beamflag[i] = MB_FLAG_NULL;
			    bath[i] = 0.0;
			    bathacrosstrack[i] = 0.0;
			    }
			else if (store->depth[i] > 0)
			    {
			    beamflag[i] = MB_FLAG_NONE;
			    bath[i] = (double) store->depth[i];
			    bathacrosstrack[i] = (double) store->acrosstrack[i];
			    }
			else if (store->depth[i] < 0)
			    {
			    beamflag[i] 
				= MB_FLAG_MANUAL + MB_FLAG_FLAG;
			    bath[i] = (double) -store->depth[i];
			    bathacrosstrack[i] = (double) store->acrosstrack[i];
			    }
			amp[i] = store->amplitude[i];
			bathalongtrack[i] = 0.0;
			}

		/* print debug statements */
		if (verbose >= 5)
			{
			fprintf(stderr,"\ndbg4  Data extracted by MBIO function <%s>\n",
				function_name);
			fprintf(stderr,"dbg4  Extracted values:\n");
			fprintf(stderr,"dbg4       kind:       %d\n",
				*kind);
			fprintf(stderr,"dbg4       error:      %d\n",
				*error);
			fprintf(stderr,"dbg4       time_i[0]:  %d\n",
				time_i[0]);
			fprintf(stderr,"dbg4       time_i[1]:  %d\n",
				time_i[1]);
			fprintf(stderr,"dbg4       time_i[2]:  %d\n",
				time_i[2]);
			fprintf(stderr,"dbg4       time_i[3]:  %d\n",
				time_i[3]);
			fprintf(stderr,"dbg4       time_i[4]:  %d\n",
				time_i[4]);
			fprintf(stderr,"dbg4       time_i[5]:  %d\n",
				time_i[5]);
			fprintf(stderr,"dbg4       time_i[6]:  %d\n",
				time_i[6]);
			fprintf(stderr,"dbg4       time_d:     %f\n",
				*time_d);
			fprintf(stderr,"dbg4       longitude:  %f\n",
				*navlon);
			fprintf(stderr,"dbg4       latitude:   %f\n",
				*navlat);
			fprintf(stderr,"dbg4       speed:      %f\n",
				*speed);
			fprintf(stderr,"dbg4       heading:    %f\n",
				*heading);
			fprintf(stderr,"dbg4       nbath:      %d\n",
				*nbath);
			for (i=0;i<*nbath;i++)
			  fprintf(stderr,"dbg4       beam:%d  flag:%3d  bath:%f  acrosstrack:%f  alongtrack:%f\n",
				i,beamflag[i],bath[i],bathacrosstrack[i],bathalongtrack[i]);
			fprintf(stderr,"dbg4        namp:     %d\n",
				*namp);
			for (i=0;i<*namp;i++)
			  fprintf(stderr,"dbg4        beam:%d   amp:%f  acrosstrack:%f  alongtrack:%f\n",
				i,amp[i],bathacrosstrack[i],bathalongtrack[i]);
			}

		/* done translating values */

		}

	/* extract comment from structure */
	else if (*kind == MB_DATA_COMMENT)
		{
		/* copy comment */
		strncpy(comment, store->comment, MBSYS_HS10_COMMENT);

		/* print debug statements */
		if (verbose >= 4)
			{
			fprintf(stderr,"\ndbg4  New ping read by MBIO function <%s>\n",
				function_name);
			fprintf(stderr,"dbg4  New ping values:\n");
			fprintf(stderr,"dbg4       error:      %d\n",
				*error);
			fprintf(stderr,"dbg4       comment:    %s\n",
				comment);
			}
		}

	/* 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       kind:       %d\n",*kind);
		}
	if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR 
		&& *kind == MB_DATA_COMMENT)
		{
		fprintf(stderr,"dbg2       comment:     \ndbg2       %s\n",
			comment);
		}
	else if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR 
		&& *kind != MB_DATA_COMMENT)
		{
		fprintf(stderr,"dbg2       time_i[0]:     %d\n",time_i[0]);
		fprintf(stderr,"dbg2       time_i[1]:     %d\n",time_i[1]);
		fprintf(stderr,"dbg2       time_i[2]:     %d\n",time_i[2]);
		fprintf(stderr,"dbg2       time_i[3]:     %d\n",time_i[3]);
		fprintf(stderr,"dbg2       time_i[4]:     %d\n",time_i[4]);
		fprintf(stderr,"dbg2       time_i[5]:     %d\n",time_i[5]);
		fprintf(stderr,"dbg2       time_i[6]:     %d\n",time_i[6]);
		fprintf(stderr,"dbg2       time_d:        %f\n",*time_d);
		fprintf(stderr,"dbg2       longitude:     %f\n",*navlon);
		fprintf(stderr,"dbg2       latitude:      %f\n",*navlat);
		fprintf(stderr,"dbg2       speed:         %f\n",*speed);
		fprintf(stderr,"dbg2       heading:       %f\n",*heading);
		}
	if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR 
		&& *kind == MB_DATA_DATA)
		{
		fprintf(stderr,"dbg2       nbath:      %d\n",
			*nbath);
		for (i=0;i<*nbath;i++)
		  fprintf(stderr,"dbg2       beam:%d  flag:%3d  bath:%f  acrosstrack:%f  alongtrack:%f\n",
			i,beamflag[i],bath[i],
			bathacrosstrack[i],bathalongtrack[i]);
		fprintf(stderr,"dbg2        namp:     %d\n",
			*namp);
		for (i=0;i<*namp;i++)
		  fprintf(stderr,"dbg2       beam:%d   amp:%f  acrosstrack:%f  alongtrack:%f\n",
			i,amp[i],bathacrosstrack[i],bathalongtrack[i]);
		}
	if (verbose >= 2)
		{
		fprintf(stderr,"dbg2       error:      %d\n",*error);
		fprintf(stderr,"dbg2  Return status:\n");
		fprintf(stderr,"dbg2       status:     %d\n",status);
		}

	/* return status */
	return(status);
}
Exemplo n.º 15
0
/*--------------------------------------------------------------------*/
int mbsys_sb2100_extract(int verbose, void *mbio_ptr, void *store_ptr,
		int *kind, int time_i[7], double *time_d,
		double *navlon, double *navlat,
		double *speed, double *heading,
		int *nbath, int *namp, int *nss,
		char *beamflag, double *bath, double *amp,
		double *bathacrosstrack, double *bathalongtrack,
		double *ss, double *ssacrosstrack, double *ssalongtrack,
		char *comment, int *error)
{
	char	*function_name = "mbsys_sb2100_extract";
	int	status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbsys_sb2100_struct *store;
	int	time_j[5];
	double	gain_db;
	double	gain_factor;
	int	center_pixel;
	int	i;

	/* print input debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Revision id: %s\n",rcs_id);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       verbose:    %d\n",verbose);
		fprintf(stderr,"dbg2       mb_ptr:     %p\n",(void *)mbio_ptr);
		fprintf(stderr,"dbg2       store_ptr:  %p\n",(void *)store_ptr);
		}

	/* get mbio descriptor */
	mb_io_ptr = (struct mb_io_struct *) mbio_ptr;

	/* get data structure pointer */
	store = (struct mbsys_sb2100_struct *) store_ptr;

	/* get data kind */
	*kind = store->kind;

	/* extract data from structure */
	if (*kind == MB_DATA_DATA)
		{
		/* get time */
		time_j[0] = store->year;
		time_j[1] = store->jday;
		time_j[2] = 60*store->hour + store->minute;
		time_j[3] = store->sec;
		time_j[4] = 1000 * ((int)store->msec);
		mb_get_itime(verbose,time_j,time_i);
		mb_get_time(verbose,time_i,time_d);

		/* get navigation */
		*navlon = store->longitude;
		*navlat = store->latitude;

		/* get heading */
		*heading = store->heading;

		/* get speed */
		*speed = 0.18553167*store->speed;

		/* set beamwidths in mb_io structure */
		mb_io_ptr->beamwidth_ltrack = 2.0;
		mb_io_ptr->beamwidth_xtrack = 2.0;

		/* read beam and pixel values into storage arrays */
		*nbath = store->nbeams;
		*namp = store->nbeams;
		*nss = store->npixels;
		center_pixel = store->npixels / 2;
		gain_db = store->ping_gain
			- store->transmitter_attenuation
			+ 10.0 * log10( store->ping_pulse_width / 5.0)
			- 30.0;
		gain_factor = pow(10.0, (-gain_db / 20.0));
		for (i=0;i<*nbath;i++)
			{
			if (store->beams[i].quality == ' ')
			    beamflag[i] = MB_FLAG_NONE;
			else if (store->beams[i].quality == '\n')
			    beamflag[i] = MB_FLAG_NONE;
			else if (store->beams[i].quality == '0')
			    beamflag[i] = MB_FLAG_NULL;
			else if (store->beams[i].quality == 'Q')
			    beamflag[i] = MB_FLAG_SONAR + MB_FLAG_FLAG;
			else if (store->beams[i].quality == 'E')
			    beamflag[i] = MB_FLAG_MANUAL + MB_FLAG_FLAG;
			else if (store->beams[i].quality == 'F')
			    beamflag[i] = MB_FLAG_FILTER + MB_FLAG_FLAG;
			bath[i] = store->beams[i].depth;
			bathacrosstrack[i] = store->beams[i].acrosstrack;
			bathalongtrack[i] = store->beams[i].alongtrack;
			}
		for (i=0;i<*namp;i++)
			{
			amp[i] = 0.25 * store->beams[i].amplitude - gain_db;
			}
		for (i=0;i<*nss;i++)
			{
			if (store->pixels[i].amplitude > 0)
				ss[i] = gain_factor * store->pixels[i].amplitude;
			else
				ss[i] = MB_SIDESCAN_NULL;
			ssacrosstrack[i] = store->pixel_size
				* (i - center_pixel);
			ssalongtrack[i] = store->pixels[i].alongtrack;
			}

		/* print debug statements */
		if (verbose >= 5)
			{
			fprintf(stderr,"\ndbg4  Data extracted by MBIO function <%s>\n",
				function_name);
			fprintf(stderr,"dbg4  Extracted values:\n");
			fprintf(stderr,"dbg4       kind:       %d\n",
				*kind);
			fprintf(stderr,"dbg4       error:      %d\n",
				*error);
			fprintf(stderr,"dbg4       time_i[0]:  %d\n",
				time_i[0]);
			fprintf(stderr,"dbg4       time_i[1]:  %d\n",
				time_i[1]);
			fprintf(stderr,"dbg4       time_i[2]:  %d\n",
				time_i[2]);
			fprintf(stderr,"dbg4       time_i[3]:  %d\n",
				time_i[3]);
			fprintf(stderr,"dbg4       time_i[4]:  %d\n",
				time_i[4]);
			fprintf(stderr,"dbg4       time_i[5]:  %d\n",
				time_i[5]);
			fprintf(stderr,"dbg4       time_i[6]:  %d\n",
				time_i[6]);
			fprintf(stderr,"dbg4       time_d:     %f\n",
				*time_d);
			fprintf(stderr,"dbg4       longitude:  %f\n",
				*navlon);
			fprintf(stderr,"dbg4       latitude:   %f\n",
				*navlat);
			fprintf(stderr,"dbg4       speed:      %f\n",
				*speed);
			fprintf(stderr,"dbg4       heading:    %f\n",
				*heading);
			fprintf(stderr,"dbg4       nbath:      %d\n",
				*nbath);
			for (i=0;i<*nbath;i++)
			  fprintf(stderr,"dbg4       beam:%d  flag:%3d  bath:%f  acrosstrack:%f  alongtrack:%f\n",
				i,beamflag[i],bath[i],bathacrosstrack[i],bathalongtrack[i]);
			fprintf(stderr,"dbg4        namp:     %d\n",
				*namp);
			for (i=0;i<*namp;i++)
			  fprintf(stderr,"dbg4        beam:%d   amp:%f  acrosstrack:%f  alongtrack:%f\n",
				i,amp[i],bathacrosstrack[i],bathalongtrack[i]);
			fprintf(stderr,"dbg4        nss:      %d\n",
				*nss);
			for (i=0;i<*nss;i++)
			  fprintf(stderr,"dbg4        pixel:%d   ss:%f  acrosstrack:%f  alongtrack:%f\n",
				i,ss[i],ssacrosstrack[i],ssalongtrack[i]);
			}

		/* done translating values */

		}

	/* extract comment from structure */
	else if (*kind == MB_DATA_COMMENT)
		{
		/* copy comment */
		strcpy(comment,store->comment);

		/* print debug statements */
		if (verbose >= 4)
			{
			fprintf(stderr,"\ndbg4  New ping read by MBIO function <%s>\n",
				function_name);
			fprintf(stderr,"dbg4  New ping values:\n");
			fprintf(stderr,"dbg4       error:      %d\n",
				*error);
			fprintf(stderr,"dbg4       comment:    %s\n",
				comment);
			}
		}

	/* print output debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> completed\n",function_name);
		fprintf(stderr,"dbg2  Revision id: %s\n",rcs_id);
		fprintf(stderr,"dbg2  Return values:\n");
		fprintf(stderr,"dbg2       kind:       %d\n",*kind);
		}
	if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR
		&& *kind == MB_DATA_COMMENT)
		{
		fprintf(stderr,"dbg2       comment:     \ndbg2       %s\n",
			comment);
		}
	else if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR
		&& *kind != MB_DATA_COMMENT)
		{
		fprintf(stderr,"dbg2       time_i[0]:     %d\n",time_i[0]);
		fprintf(stderr,"dbg2       time_i[1]:     %d\n",time_i[1]);
		fprintf(stderr,"dbg2       time_i[2]:     %d\n",time_i[2]);
		fprintf(stderr,"dbg2       time_i[3]:     %d\n",time_i[3]);
		fprintf(stderr,"dbg2       time_i[4]:     %d\n",time_i[4]);
		fprintf(stderr,"dbg2       time_i[5]:     %d\n",time_i[5]);
		fprintf(stderr,"dbg2       time_i[6]:     %d\n",time_i[6]);
		fprintf(stderr,"dbg2       time_d:        %f\n",*time_d);
		fprintf(stderr,"dbg2       longitude:     %f\n",*navlon);
		fprintf(stderr,"dbg2       latitude:      %f\n",*navlat);
		fprintf(stderr,"dbg2       speed:         %f\n",*speed);
		fprintf(stderr,"dbg2       heading:       %f\n",*heading);
		}
	if (verbose >= 2 && *error <= MB_ERROR_NO_ERROR
		&& *kind == MB_DATA_DATA)
		{
		fprintf(stderr,"dbg2       nbath:      %d\n",
			*nbath);
		for (i=0;i<*nbath;i++)
		  fprintf(stderr,"dbg2       beam:%d  flag:%3d  bath:%f  acrosstrack:%f  alongtrack:%f\n",
			i,beamflag[i],bath[i],
			bathacrosstrack[i],bathalongtrack[i]);
		fprintf(stderr,"dbg2        namp:     %d\n",
			*namp);
		for (i=0;i<*namp;i++)
		  fprintf(stderr,"dbg2       beam:%d   amp:%f  acrosstrack:%f  alongtrack:%f\n",
			i,amp[i],bathacrosstrack[i],bathalongtrack[i]);
		fprintf(stderr,"dbg2        nss:      %d\n",
			*nss);
		for (i=0;i<*nss;i++)
		  fprintf(stderr,"dbg2        pixel:%d   ss:%f  acrosstrack:%f  alongtrack:%f\n",
			i,ss[i],ssacrosstrack[i],ssalongtrack[i]);
		}
	if (verbose >= 2)
		{
		fprintf(stderr,"dbg2       error:      %d\n",*error);
		fprintf(stderr,"dbg2  Return status:\n");
		fprintf(stderr,"dbg2       status:     %d\n",status);
		}

	/* return status */
	return(status);
}
Exemplo n.º 16
0
/*--------------------------------------------------------------------*/
int mbr_rt_segysegy(int verbose, void *mbio_ptr, void *store_ptr, int *error)
{
	char	*function_name = "mbr_rt_segysegy";
	int	status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbsys_singlebeam_struct *store;
	struct mb_segyio_struct *mb_segyio_ptr;
	struct mb_segytraceheader_struct traceheader;
	float	*trace;
	int	time_j[5];
	int	i;

	/* print input debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",function_name);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       verbose:    %d\n",verbose);
		fprintf(stderr,"dbg2       mbio_ptr:   %lu\n",(size_t)mbio_ptr);
		fprintf(stderr,"dbg2       store_ptr:  %lu\n",(size_t)store_ptr);
		}

	/* get pointers to mbio descriptor and data structures */
	mb_io_ptr = (struct mb_io_struct *) mbio_ptr;
	store = (struct mbsys_singlebeam_struct *) store_ptr;
	mb_segyio_ptr = (struct mb_segyio_struct *) (mb_io_ptr->mbfp);

	/* read next data from file */
	status = mb_segy_read_trace(verbose, (void *) mb_segyio_ptr, 
		&traceheader, &trace, error);

	/* set error and kind in mb_io_ptr */
	mb_io_ptr->new_error = *error;
	mb_io_ptr->new_kind = MB_DATA_DATA;

	/* translate values to data storage structure */
	if (status == MB_SUCCESS
		&& store != NULL)
		{
		store->kind = MB_DATA_DATA;
		for (i=0;i<8;i++)
		    store->survey_id[i] = '\0';
		time_j[0] = traceheader.year;
		time_j[1] = traceheader.day_of_yr;
		time_j[2] = 60 * traceheader.hour + traceheader.min;
		time_j[3] = traceheader.sec;
		time_j[4] = 1000 * traceheader.mils;
		mb_get_itime(verbose,time_j,store->time_i);
		mb_get_time(verbose,store->time_i,&store->time_d);
		store->timezone = 0;
		store->longitude = ((double)traceheader.src_long) / 360000.0;
		store->latitude = ((double)traceheader.src_lat) / 360000.0;
		store->easting = 0.0;
		store->northing = 0.0;
		store->heading = traceheader.heading;
		store->speed = 0.0;
		store->nav_type = 9;
		store->nav_quality = 9;
		store->roll = 0.0;
		store->pitch = 0.0;
		store->heave = 0.0;
		store->sonar_depth = 0.01 * traceheader.src_depth;
		store->rov_pressure = 0.0;
		store->rov_altitude = 0.01 * traceheader.src_wbd - 0.01 * traceheader.src_depth;
		store->flag = MB_FLAG_NONE;
		store->tt = traceheader.wbt_secs;
		store->bath = 0.01 * traceheader.src_wbd;
		store->tide = 0.0;
		store->bath_corr = 99;
		store->bath_type = 1;
		store->mag_tot_1 = 0.0;
		store->mag_tot_2 = 0.0;
		store->mag_res = 0.0;
		store->mag_res_sensor = 0.0;
		store->mag_diurnal = 0.0;
		store->mag_altitude = 0.0;
		store->gravity = 0.0;
		store->eotvos = 0.0;
		store->free_air = 0.0;
		store->seismic_line = mb_segyio_ptr->fileheader.line;
		store->seismic_shot = traceheader.shot_num;
		store->seismic_cdp = traceheader.rp_num;
		for (i=0;i<MBSYS_SINGLEBEAM_MAXLINE;i++)
		    store->comment[i] = '\0';
		}

	/* 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);
}