示例#1
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);
	
	/*-------------------------------------------------------------------*/

}
示例#2
0
/*--------------------------------------------------------------------*/
int mbr_rt_swplssxp(int verbose, void *mbio_ptr, void *store_ptr, int *error) {
	char *function_name = "mbr_rt_swplssxp";
	int status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbsys_swathplus_struct *store;
	swpls_header *header;
	swpls_sxpping *ping;
	swpls_comment *comment;
	swpls_projection *projection;

	/* 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", mbio_ptr);
		fprintf(stderr, "dbg2       store_ptr:  %p\n", store_ptr);
	}

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

	/* read next data from file */
	status = mbr_swplssxp_rd_data(verbose, mbio_ptr, store_ptr, error);

	/* get pointer to data structure */
	store = (struct mbsys_swathplus_struct *)store_ptr;
	header = &store->sxp_header;
	ping = &store->sxp_ping;
	comment = &store->comment;
	projection = &store->projection;

	/* check if projection has been set from *.prj file, if so, copy into projection structure */
	if ((store->projection_set == MB_NO) && (mb_io_ptr->projection_initialized == MB_YES)) {
		projection->time_d = time(NULL);
		projection->microsec = 0;
		projection->nchars = strnlen(mb_io_ptr->projection_id, MB_NAME_LENGTH);
		if (projection->projection_alloc < projection->nchars) {
			status = mb_reallocd(verbose, __FILE__, __LINE__, (size_t)projection->nchars, (void **)&(projection->projection_id),
			                     error);
			if (status != MB_SUCCESS) {
				projection->projection_alloc = 0;
			}
			else {
				projection->projection_alloc = projection->nchars;
			}
		}

		if (status == MB_SUCCESS) {
			strncpy(projection->projection_id, mb_io_ptr->projection_id, (size_t)projection->nchars);
			store->projection_set = MB_YES;
		}
	}
	/* check if projection has been read from *mb222 file, if so, tell mb system */
	else if ((store->projection_set == MB_YES) && (mb_io_ptr->projection_initialized == MB_NO)) {
		mb_proj_init(verbose, projection->projection_id, &(mb_io_ptr->pjptr), error);
		strncpy(mb_io_ptr->projection_id, projection->projection_id, MB_NAME_LENGTH);
		mb_io_ptr->projection_initialized = MB_YES;
	}

	/* throw away data if the time stamp makes no sense */
	if ((status == MB_SUCCESS) && (store->kind == MB_DATA_DATA) && (store->time_i[0] < 2003)) {
		status = MB_FAILURE;
		*error = MB_ERROR_UNINTELLIGIBLE;
	}

	/* save fix data, used to calculate vessel speed */
	if ((status == MB_SUCCESS) && (store->kind == MB_DATA_DATA)) {
		/* add latest fix */
		mb_navint_add(verbose, mbio_ptr, store->time_d, store->sxp_ping.txer_e, store->sxp_ping.txer_n, error);
	}

	/* 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);
} /* mbr_rt_swplssxp */
示例#3
0
/*---------------------------------------------------------------*/
static int process_output(int verbose, mbdefaults *mbdflts, options *opts,
	mb_path ifile, counts *recs, int *error)
{
	char *function_name = "scan_input_heights";
	int status = MB_SUCCESS;
	int i;
	void *imbio_ptr = NULL;
	double btime_d, etime_d;
	int beams_bath_alloc, beams_amp_alloc, pixels_ss_alloc;
	void *ombio_ptr[SWPLS_MAX_TXERS];
	struct mb_io_struct *imb_io_ptr = NULL;
	void *istore_ptr = NULL;
	int ofile_init[SWPLS_MAX_TXERS];
	mb_path ofile[SWPLS_MAX_TXERS];
	struct mbsys_swathplus_struct *istore = NULL;

	/* print input debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr, "\ndbg2  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       options:    %p\n", (void *)opts);
		}

	/* open the input file */
	if ((status =
		mb_read_init(opts->verbose, ifile, opts->format, mbdflts->pings_get,
			mbdflts->lonflip, mbdflts->bounds, mbdflts->btime_i,
			mbdflts->etime_i,
			mbdflts->speedmin, mbdflts->timegap, &imbio_ptr, &btime_d, &etime_d,
			&beams_bath_alloc, &beams_amp_alloc, &pixels_ss_alloc,
			error)) != MB_SUCCESS)
		{
		char message[MAX_ERROR_STRING] = {0};
		sprintf(message, "Swath File <%s> not initialized for reading\n",
			ifile);
		error_exit(opts->verbose, *error, "mb_read_init", message);
		}
		
	/* get mbio and data structure descriptors */
	imb_io_ptr = (struct mb_io_struct *)imbio_ptr;
	istore_ptr = imb_io_ptr->store_data;
	
	/* set the projection for nav data */
	if (opts->projection_set == MB_YES)
		{
		mb_proj_init(opts->verbose, opts->proj4command, &(imb_io_ptr->pjptr), error);
		strncpy(imb_io_ptr->projection_id, opts->proj4command, MB_NAME_LENGTH);
		imb_io_ptr->projection_initialized = MB_YES;		
		}
		
	/* setup the output filename(s) for writing */
	status = set_outfile_names(opts->verbose, ofile, ifile, &opts->basename,
		opts->ofile_set, opts->split_txers, error);
	for (i = 0; i < SWPLS_MAX_TXERS; i++)
		{
		ombio_ptr[i] = NULL;
		ofile_init[i] = MB_NO;
		}

	/* start looping over data records */
	while (*error <= MB_ERROR_NO_ERROR)
		{
		int kind = MB_DATA_NONE;

		/* read the next record */
		status =
			mb_read_ping(opts->verbose, imbio_ptr, istore_ptr, &kind, error);

		/* some nonfatal errors do not matter */
		if ((*error < MB_ERROR_NO_ERROR) && (MB_ERROR_UNINTELLIGIBLE < *error))
			{
			error = MB_ERROR_NO_ERROR;
			status = MB_SUCCESS;
			}

		istore = (struct mbsys_swathplus_struct *)istore_ptr;

		if (status == MB_SUCCESS)
			{
			status = count_record(opts->verbose, recs, istore, error);
			}

		if ((status == MB_SUCCESS) && (opts->print_ascii == MB_YES))
			{
			status = print_latest_record(opts->verbose, istore, error);
			}

		/* process the sxp ping data to file */
		if ((status == MB_SUCCESS) && (istore->kind == MB_DATA_DATA) &&
			((istore->type == SWPLS_ID_PROCESSED_PING) ||
			(istore->type == SWPLS_ID_PROCESSED_PING2)))
			{
			int obeams_bath, obeams_amp, opixels_ss;
			struct mb_io_struct *omb_io_ptr = NULL;
			void *ostore_ptr = NULL;
			struct mbsys_swathplus_struct *ostore = NULL;
			int txno = 0;
			int txidx = 0;

			if ((status == MB_SUCCESS) && (opts->flip_rejected == MB_YES))
				{
				status = flip_sample_flags(opts->verbose, &(istore->sxp_ping),
					error);
				}

			if ((status == MB_SUCCESS) && (opts->remove_rejected == MB_YES))
				{
				status = remove_rejected_samps(opts->verbose,
					&(istore->sxp_ping), error);
				}

			if ((status == MB_SUCCESS) && (opts->copy_rawamp == MB_YES))
				{
				status = copy_rawamp(opts->verbose, &(istore->sxp_ping), error);
				}

			if ((status == MB_SUCCESS) && (opts->write_output == MB_YES))
				{
				/* select the output file based on the txer channel */
				status = ping_txno(opts->verbose, istore, &txno, error);
				txidx = (opts->split_txers == MB_YES) ? txno - 1 : 0;

				/* initialize the output file if necessary */
				if (ofile_init[txidx] == MB_NO)
					{
					status = mb_write_init(opts->verbose, ofile[txidx], opts->format, 
						&ombio_ptr[txidx],  &obeams_bath,
						&obeams_amp, &opixels_ss, error);
					if (status != MB_SUCCESS)
						{
						char message[MAX_ERROR_STRING] = {0};
						sprintf(message, "SWATHplus file <%s> not initialized for writing.\n",
							ofile[txidx]);
						error_exit(verbose, *error, "mb_write_init", message);
						}

					if (status == MB_SUCCESS)
						{
						ofile_init[txidx] = MB_YES;
						}
					}

				/* assign output pointers based on txer channel */
				omb_io_ptr = (struct mb_io_struct *)ombio_ptr[txidx];
				ostore_ptr = omb_io_ptr->store_data;
				ostore = (struct mbsys_swathplus_struct *)ostore_ptr;

				/* copy the ping from istore to ostore */
				if (status == MB_SUCCESS)
					{
					status = mbsys_swathplus_copy(opts->verbose, imbio_ptr,
						istore_ptr, ostore_ptr,
						error);
					}

				/* write the ping to file */
				if (status == MB_SUCCESS)
					{
					ostore->kind = MB_DATA_DATA;
					ostore->type = SWPLS_ID_PROCESSED_PING2;
					status = mb_write_ping(opts->verbose, ombio_ptr[txidx],
						ostore, error);
					}

				/* check for error writing here */
				if (status != MB_SUCCESS)
					{
					char message[MAX_ERROR_STRING] = {0};
					sprintf(message, "Data not written to file <%s>\n",
						ofile[txidx]);
					error_exit(opts->verbose, *error, "mb_write_ping", message);
					}
				}		/* end write sxp data to file */
			}		/* end processing sxp data */
		}		/* end looping over all records in file */

	/* close the files */
	status = mb_close(opts->verbose, &imbio_ptr, error);
	for (i = 0; i < SWPLS_MAX_TXERS; i++)
		{
		if (ofile_init[i] == MB_YES)
			{
			status = mb_close(opts->verbose, &ombio_ptr[i], error);
			ofile_init[i] = MB_NO;
			}
		}

	if (verbose >= 2)
		{
		fprintf(stderr, "\ndbg2  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);
} /* process_output */
示例#4
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);
}
示例#5
0
/*------------------------------------------------------------------------------*/
int mbview_setsecondarygrid(int verbose, size_t instance,
			int	secondary_grid_projection_mode,
			char	*secondary_grid_projection_id,
			float	secondary_nodatavalue,
			int	secondary_nx,
			int	secondary_ny,
			double	secondary_min,
			double	secondary_max,
			double	secondary_xmin,
			double	secondary_xmax,
			double	secondary_ymin,
			double	secondary_ymax,
			double	secondary_dx,
			double	secondary_dy,
			float	*secondary_data,
			int *error)

{
	/* local variables */
	char	*function_name = "mbview_setsecondarygrid";
	int	status = MB_SUCCESS;
	struct mbview_world_struct *view;
	struct mbview_struct *data;
	int	proj_status;
	char	*message;

	/* print starting debug statements */
	if (verbose >= 2)
		{
		fprintf(stderr,"\ndbg2  MBIO function <%s> called\n",
			function_name);
		fprintf(stderr,"dbg2  Version %s\n",rcs_id);
		fprintf(stderr,"dbg2  MB-system Version %s\n",MB_VERSION);
		fprintf(stderr,"dbg2  Input arguments:\n");
		fprintf(stderr,"dbg2       verbose:                   %d\n", verbose);
		fprintf(stderr,"dbg2       instance:                  %zu\n", instance);
		fprintf(stderr,"dbg2       secondary_grid_projection_mode:   %d\n", secondary_grid_projection_mode);
		fprintf(stderr,"dbg2       secondary_grid_projection_id:     %s\n", secondary_grid_projection_id);
		fprintf(stderr,"dbg2       secondary_nodatavalue:       %f\n", secondary_nodatavalue);
		fprintf(stderr,"dbg2       secondary_nx:                %d\n", secondary_nx);
		fprintf(stderr,"dbg2       secondary_ny:                %d\n", secondary_ny);
		fprintf(stderr,"dbg2       secondary_min:               %f\n", secondary_min);
		fprintf(stderr,"dbg2       secondary_max:               %f\n", secondary_max);
		fprintf(stderr,"dbg2       secondary_xmin:              %f\n", secondary_xmin);
		fprintf(stderr,"dbg2       secondary_xmax:              %f\n", secondary_xmax);
		fprintf(stderr,"dbg2       secondary_ymin:              %f\n", secondary_ymin);
		fprintf(stderr,"dbg2       secondary_ymax:              %f\n", secondary_ymax);
		fprintf(stderr,"dbg2       secondary_dx:                %f\n", secondary_dx);
		fprintf(stderr,"dbg2       secondary_dy:                %f\n", secondary_dy);
		fprintf(stderr,"dbg2       secondary_data:              %p\n", secondary_data);
		}

	/* get view */
	view = &(mbviews[instance]);
	data = &(view->data);

	/* set values */
        data->secondary_grid_projection_mode = secondary_grid_projection_mode;
        strcpy(data->secondary_grid_projection_id, secondary_grid_projection_id);
        data->secondary_nodatavalue = secondary_nodatavalue;
        data->secondary_nxy = secondary_nx * secondary_ny;
        data->secondary_nx = secondary_nx;
        data->secondary_ny = secondary_ny;
        data->secondary_min = secondary_min;
        data->secondary_max = secondary_max;
        data->secondary_xmin = secondary_xmin;
        data->secondary_xmax = secondary_xmax;
        data->secondary_ymin = secondary_ymin;
        data->secondary_ymax = secondary_ymax;
        data->secondary_dx = secondary_dx;
        data->secondary_dy = secondary_dy;

	/* allocate required arrays */
    	status = mb_mallocd(verbose, __FILE__, __LINE__, sizeof(float) * data->secondary_nxy,
    				(void **)&data->secondary_data, error);
	if (status != MB_SUCCESS)
	    {
	    fprintf(stderr,"\nUnable to allocate memory to store secondary grid data\n");
	    fprintf(stderr,"\nProgram terminated in function <%s>.\n",
		    function_name);
	    exit(*error);
	    }

	/* copy grid */
	memcpy(data->secondary_data, secondary_data, data->secondary_nxy * sizeof(float));

	/* check if secondary grid has same bounds and dimensions as primary grid so
		that overlay calculations are trivial */
	if (data->secondary_nx == data->primary_nx
		&& data->secondary_ny == data->primary_ny
		&& (fabs(data->secondary_xmin - data->primary_xmin) < 0.1 * data->primary_dx)
		&& (fabs(data->secondary_xmax - data->primary_xmax) < 0.1 * data->primary_dx)
		&& (fabs(data->secondary_ymin - data->primary_ymin) < 0.1 * data->primary_dy)
		&& (fabs(data->secondary_ymax - data->primary_ymax) < 0.1 * data->primary_dy))
		data->secondary_sameas_primary = MB_YES;
	else
		data->secondary_sameas_primary = MB_NO;

	/* set projection for secondary grid if needed */
	if (data->secondary_nxy > 0
		&& data->secondary_grid_projection_mode == MBV_PROJECTION_PROJECTED)
		{
		/* set projection for getting lon lat */
		proj_status = mb_proj_init(mbv_verbose,
					data->secondary_grid_projection_id,
					&(view->secondary_pjptr),
					error);
		if (proj_status == MB_SUCCESS)
			view->secondary_pj_init = MB_YES;
/*fprintf(stderr,"SECONDARY GRID PROJECTION:%d %p %s\n",
view->secondary_pj_init,view->secondary_pjptr,data->secondary_grid_projection_id);*/

		/* quit if projection fails */
		if (proj_status != MB_SUCCESS)
			{
			mb_error(verbose,*error,&message);
			fprintf(stderr,"\nMBIO Error initializing projection:\n%s\n",
				message);
			fprintf(stderr,"\nProgram terminated in <%s>\n",
				function_name);
			mb_memory_clear(mbv_verbose, error);
			exit(*error);
			}
		}

	/* reset histogram flag */
	view->secondary_histogram_set = MB_NO;

	/* 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 */
	return(status);
}
示例#6
0
/*--------------------------------------------------------------------*/
int mbr_rt_swplssxi(int verbose, void *mbio_ptr, void *store_ptr, int *error) {
	char *function_name = "mbr_rt_swplssxi";
	int status = MB_SUCCESS;
	struct mb_io_struct *mb_io_ptr;
	struct mbsys_swathplus_struct *store;
	swpls_projection *projection;

	/* 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);
		fprintf(stderr, "dbg2       store_ptr:  %p\n", (void *)store_ptr);
	}

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

	/* read next data from file */
	status = mbr_swplssxi_rd_data(verbose, mbio_ptr, store_ptr, error);

	/* get pointers to data structures */
	store = (struct mbsys_swathplus_struct *)store_ptr;
	projection = &store->projection;

	/* check if projection has been set from *.prj file, if so, copy into projection structure */
	if ((store->projection_set == MB_NO) && (mb_io_ptr->projection_initialized == MB_YES)) {
		projection->time_d = time(NULL);
		projection->microsec = 0;
		projection->nchars = strnlen(mb_io_ptr->projection_id, MB_NAME_LENGTH);
		if (projection->projection_alloc < projection->nchars) {
			status = mb_reallocd(verbose, __FILE__, __LINE__, (size_t)projection->nchars, (void **)&(projection->projection_id),
			                     error);
			if (status != MB_SUCCESS) {
				projection->projection_alloc = 0;
			}
			else {
				projection->projection_alloc = projection->nchars;
			}
		}

		if (status == MB_SUCCESS) {
			strncpy(projection->projection_id, mb_io_ptr->projection_id, (size_t)projection->nchars);
			store->projection_set = MB_YES;
		}
	}
	/* check if projection has been read from *mb222 file, if so, tell mb system */
	else if ((store->projection_set == MB_YES) && (mb_io_ptr->projection_initialized == MB_NO)) {
		mb_proj_init(verbose, projection->projection_id, &(mb_io_ptr->pjptr), error);
		strncpy(mb_io_ptr->projection_id, projection->projection_id, MB_NAME_LENGTH);
		mb_io_ptr->projection_initialized = MB_YES;
	}

	/* throw away multibeam data if the time stamp makes no sense */
	if ((status == MB_SUCCESS) && (store->kind == MB_DATA_DATA) && (store->time_i[0] < 2003)) {
		status = MB_FAILURE;
		*error = MB_ERROR_UNINTELLIGIBLE;
	}
	/* save geographic position fix data */
	else if ((status == MB_SUCCESS) && (store->kind == MB_DATA_NAV) && (store->projection_set == MB_NO)) {
		mb_navint_add(verbose, mbio_ptr, store->time_d, store->posll.longitude, store->posll.latitude, error);
	}
	/* save projected position fix data */
	else if ((status == MB_SUCCESS) && (store->kind == MB_DATA_NAV1) && (store->projection_set == MB_YES)) {
		mb_navint_add(verbose, mbio_ptr, store->time_d, store->posen.easting, store->posen.northing, error);
	}
	/* save heading and attitude fix data */
	else if ((status == MB_SUCCESS) && (store->kind == MB_DATA_ATTITUDE)) {
		mb_attint_add(verbose, mbio_ptr, store->time_d, store->attitude.height, store->attitude.roll, store->attitude.pitch,
		              error);
		mb_hedint_add(verbose, mbio_ptr, store->time_d, store->attitude.heading, error);
	}
	/* save tide data (as altitude) */
	else if ((status == MB_SUCCESS) && (store->kind == MB_DATA_TIDE)) {
		mb_altint_add(verbose, mbio_ptr, store->time_d, store->tide.tide, error);
	}

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