Exemplo n.º 1
0
int main (int argc, char **argv)
{
	char program_name[] = "MBauvloglist";
	char help_message[] =  "MBauvloglist lists table data from an MBARI AUV mission log file.";
	char usage_message[] = "MBauvloglist -Ifile [-Fprintformat -Llonflip -Olist -H -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 = NULL;

	/* MBIO read control parameters */
	int	pings;
	int	format;
	int	lonflip;
	double	bounds[4];
	int	btime_i[7];
	int	etime_i[7];
	double	speedmin;
	double	timegap;

	/* auv log data */
	FILE	*fp;
	char	file[MB_PATH_MAXLINE];
	struct	field
		{
		int	type;
		int	size;
		int	index;
		char	name[MB_PATH_MAXLINE];
		char	format[MB_PATH_MAXLINE];
		char	description[MB_PATH_MAXLINE];
		char	units[MB_PATH_MAXLINE];
		double	scale;
		};
	struct	printfield
		{
		char	name[MB_PATH_MAXLINE];
		int	index;
		int	formatset;
		char	format[MB_PATH_MAXLINE];
		};
	int	nfields = 0;
	struct field fields[NFIELDSMAX];
	int	nprintfields = 0;
	struct printfield printfields[NFIELDSMAX];
	int	nrecord;
	int	recordsize;
	int	printheader = MB_NO;
	int	angles_in_degrees = MB_NO;

	/* navigation, heading, attitude data for merging in fnv format */
	int	nav_merge = MB_NO;
	mb_path	nav_file;
	int	nav_num = 0;
	int	nav_alloc = 0;
	double	*nav_time_d = NULL;
	double	*nav_navlon = NULL;
	double	*nav_navlat = NULL;
	double	*nav_heading = NULL;
	double	*nav_speed = NULL;
	double	*nav_sensordepth = NULL;
	double	*nav_roll = NULL;
	double	*nav_pitch = NULL;
	double	*nav_heave = NULL;
	
	/* output control */
	int	output_mode = OUTPUT_MODE_TAB;

	double	time_d = 0.0;
	int	time_i[7];
	int	time_j[5];
	char	buffer[MB_PATH_MAXLINE];
	char	type[MB_PATH_MAXLINE];
	char	printformat[MB_PATH_MAXLINE];
	char	*result;
	int	nscan;
	double	dvalue;
	double	sec;
	int	ivalue;
	int	index;
	int	jinterp = 0;
	int	nchar;
	int	nget;
	int	nav_ok;
	int	interp_status;
	int	i, j;

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

	/* set file to null */
	file[0] = '\0';
	nav_file[0] = '\0';
	strcpy(printformat, "default");

	/* process argument list */
	while ((c = getopt(argc, argv, "F:f:I:i:L:l:M:m:N:n:O:o:PpSsVvWwHh")) != -1)
	  switch (c)
		{
		case 'H':
		case 'h':
			help++;
			break;
		case 'V':
		case 'v':
			verbose++;
			break;
		case 'F':
		case 'f':
			sscanf (optarg,"%s", printformat);
			flag++;
			break;
		case 'I':
		case 'i':
			sscanf (optarg,"%s", file);
			flag++;
			break;
		case 'L':
		case 'l':
			sscanf (optarg,"%d", &lonflip);
			flag++;
			break;
		case 'M':
		case 'm':
			sscanf (optarg,"%d", &output_mode);
			flag++;
			break;
		case 'N':
		case 'n':
			sscanf (optarg,"%s", nav_file);
			nav_merge = MB_YES;
			flag++;
			break;
		case 'O':
		case 'o':
			nscan = sscanf (optarg,"%s", printfields[nprintfields].name);
			if (strlen(printformat) > 0 && strcmp(printformat, "default") != 0)
				{
				printfields[nprintfields].formatset = MB_YES;
				strcpy(printfields[nprintfields].format,printformat);
				}
			else
				{
				printfields[nprintfields].formatset = MB_NO;
				strcpy(printfields[nprintfields].format,"");
				}
			printfields[nprintfields].index = -1;
			nprintfields++;
			flag++;
			break;
		case 'P':
		case 'p':
			printheader = MB_YES;
			flag++;
			break;
		case 'S':
		case 's':
			angles_in_degrees = MB_YES;
			flag++;
			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);
		}

	/* 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       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);
		fprintf(stderr,"dbg2       file:           %s\n",file);
		fprintf(stderr,"dbg2       nav_file:       %s\n",nav_file);
		fprintf(stderr,"dbg2       output_mode:    %d\n",output_mode);
		fprintf(stderr,"dbg2       printheader:    %d\n",printheader);
		fprintf(stderr,"dbg2       angles_in_degrees:%d\n",angles_in_degrees);
		fprintf(stderr,"dbg2       nprintfields:   %d\n",nprintfields);
		for (i=0;i<nprintfields;i++)
			fprintf(stderr,"dbg2         printfields[%d]:      %s %d %s\n",
						i,printfields[i].name,
						printfields[i].formatset,
						printfields[i].format);
		}

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

	/* if nav merging to be done get nav */
	if (nav_merge == MB_YES && strlen(nav_file) > 0)
		{
		/* count the data points in the nav file */
		nav_num = 0;
		nchar = MB_PATH_MAXLINE-1;
		if ((fp = fopen(nav_file, "r")) == NULL)
			{
			error = MB_ERROR_OPEN_FAIL;
			fprintf(stderr,"\nUnable to Open Navigation File <%s> for reading\n",nav_file);
			fprintf(stderr,"\nProgram <%s> Terminated\n",
				program_name);
			exit(error);
			}
		while ((result = fgets(buffer,nchar,fp)) == buffer)
			nav_num++;
		fclose(fp);
    
		/* allocate arrays for nav */
		if (nav_num > 0)
			{
			nav_alloc = nav_num;
			status = mb_mallocd(verbose,__FILE__,__LINE__,nav_alloc*sizeof(double),(void **)&nav_time_d,&error);
			status = mb_mallocd(verbose,__FILE__,__LINE__,nav_alloc*sizeof(double),(void **)&nav_navlon,&error);
			status = mb_mallocd(verbose,__FILE__,__LINE__,nav_alloc*sizeof(double),(void **)&nav_navlat,&error);
			status = mb_mallocd(verbose,__FILE__,__LINE__,nav_alloc*sizeof(double),(void **)&nav_heading,&error);
			status = mb_mallocd(verbose,__FILE__,__LINE__,nav_alloc*sizeof(double),(void **)&nav_speed,&error);
			status = mb_mallocd(verbose,__FILE__,__LINE__,nav_alloc*sizeof(double),(void **)&nav_sensordepth,&error);
			status = mb_mallocd(verbose,__FILE__,__LINE__,nav_alloc*sizeof(double),(void **)&nav_roll,&error);
			status = mb_mallocd(verbose,__FILE__,__LINE__,nav_alloc*sizeof(double),(void **)&nav_pitch,&error);
			status = mb_mallocd(verbose,__FILE__,__LINE__,nav_alloc*sizeof(double),(void **)&nav_heave,&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 the data points in the nav file */
		nav_num = 0;
		if ((fp = fopen(nav_file, "r")) == NULL)
			{
			error = MB_ERROR_OPEN_FAIL;
			fprintf(stderr,"\nUnable to Open navigation File <%s> for reading\n",nav_file);
			fprintf(stderr,"\nProgram <%s> Terminated\n",
				program_name);
			exit(error);
			}
		while ((result = fgets(buffer,nchar,fp)) == buffer)
			{
			nget = sscanf(buffer,"%d %d %d %d %d %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
				&time_i[0],&time_i[1],&time_i[2],
				&time_i[3],&time_i[4],&sec,
				&nav_time_d[nav_num],
				&nav_navlon[nav_num],&nav_navlat[nav_num],
				&nav_heading[nav_num],&nav_speed[nav_num],&nav_sensordepth[nav_num],
				&nav_roll[nav_num],&nav_pitch[nav_num],&nav_heave[nav_num]);
			if (nget >= 9)
				nav_ok = MB_YES;
			else
				nav_ok = MB_NO;
                        if (nav_num > 0 && nav_time_d[nav_num] <= nav_time_d[nav_num-1])
                                nav_ok = MB_NO;
			if (nav_ok == MB_YES)
			    nav_num++;
			}
		fclose(fp);
 		}
fprintf(stderr,"%d %d records read from nav file %s\n",nav_alloc,nav_num,nav_file);
		
	/* open the input file */
	if ((fp = fopen(file, "r")) == NULL)
		{
		error = MB_ERROR_OPEN_FAIL;
		status = MB_FAILURE;
		fprintf(stderr,"\nUnable to open log file <%s> for reading\n",file);
		exit(status);
		}

	nfields = 0;
	recordsize = 0;
	while ((result = fgets(buffer,MB_PATH_MAXLINE,fp)) == buffer
		&& strncmp(buffer, "# begin",7) != 0)
		{
		nscan = sscanf(buffer, "# %s %s %s",
				type,
				fields[nfields].name,
				fields[nfields].format);
		if (nscan == 2)
			{
			if (printheader == MB_YES)
				fprintf(stdout,"# csv %s\n",  fields[nfields].name);
			}

		else if (nscan == 3)
			{
			if (printheader == MB_YES)
				fprintf(stdout,"%s",buffer);

			result = (char *) strchr(buffer, ',');
			strcpy(fields[nfields].description, &(result[1]));
			result = (char *) strchr(fields[nfields].description, ',');
			result[0] = 0;
			result = (char *) strrchr(buffer, ',');
			strcpy(fields[nfields].units, &(result[1]));

			fields[nfields].index = recordsize;
			if (strcmp(type, "double") == 0)
				{
				fields[nfields].type = TYPE_DOUBLE;
				fields[nfields].size = 8;
				if (angles_in_degrees == MB_YES
					&&(strcmp(fields[nfields].name, "mLatK") == 0
						|| strcmp(fields[nfields].name, "mLonK") == 0
						|| strcmp(fields[nfields].name, "mLatK") == 0
						|| strcmp(fields[nfields].name, "mRollK") == 0
						|| strcmp(fields[nfields].name, "mPitchK") == 0
						|| strcmp(fields[nfields].name, "mHeadK") == 0
						|| strcmp(fields[nfields].name, "mYawK") == 0
						|| strcmp(fields[nfields].name, "mLonCB") == 0
						|| strcmp(fields[nfields].name, "mLatCB") == 0
						|| strcmp(fields[nfields].name, "mRollCB") == 0
						|| strcmp(fields[nfields].name, "mPitchCB") == 0
						|| strcmp(fields[nfields].name, "mHeadCB") == 0
						|| strcmp(fields[nfields].name, "mYawCB") == 0))
					fields[nfields].scale = RTD;
				else
					fields[nfields].scale = 1.0;
				recordsize += 8;
				}
			else if (strcmp(type, "integer") == 0)
				{
				fields[nfields].type = TYPE_INTEGER;
				fields[nfields].size = 4;
				fields[nfields].scale = 1.0;
				recordsize += 4;
				}
			else if (strcmp(type, "timeTag") == 0)
				{
				fields[nfields].type = TYPE_TIMETAG;
				fields[nfields].size = 8;
				fields[nfields].scale = 1.0;
				recordsize += 8;
				}
			else if (strcmp(type, "angle") == 0)
				{
				fields[nfields].type = TYPE_ANGLE;
				fields[nfields].size = 8;
				if (angles_in_degrees == MB_YES
					&&(strcmp(fields[nfields].name, "mRollCB") == 0
						|| strcmp(fields[nfields].name, "mOmega_xCB") == 0
						|| strcmp(fields[nfields].name, "mPitchCB") == 0
						|| strcmp(fields[nfields].name, "mOmega_yCB") == 0
						|| strcmp(fields[nfields].name, "mYawCB") == 0
						|| strcmp(fields[nfields].name, "mOmega_zCB") == 0))
					fields[nfields].scale = RTD;
				else
					fields[nfields].scale = 1.0;
				recordsize += 8;
				}
			nfields++;
			}
		}

	/* end here if asked only to print header */
	if (nprintfields == 0 && printheader == MB_YES)
		exit(error);

	/* by default print everything */
	if (nprintfields == 0)
		{
		nprintfields = nfields;
		for (i=0;i<nfields;i++)
			{
			strcpy(printfields[i].name, fields[i].name);
			printfields[i].index = i;
			printfields[i].formatset = MB_NO;
			strcpy(printfields[i].format, fields[i].format);
			}
		}

	/* check the fields to be printed */
	for (i=0;i<nprintfields;i++)
		{
		if (strcmp(printfields[i].name,"zero") == 0)
			{
			printfields[i].index = INDEX_ZERO;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%f");
				}
			}
		else if (strcmp(printfields[i].name,"timeTag") == 0)
			{
			printfields[i].index = INDEX_ZERO;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%.8f");
				}
			}
		else if (strcmp(printfields[i].name,"mergeLon") == 0)
			{
			printfields[i].index = INDEX_MERGE_LON;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%.9f");
				}
			}
		else if (strcmp(printfields[i].name,"mergeLat") == 0)
			{
			printfields[i].index = INDEX_MERGE_LAT;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%.9f");
				}
			}
		else if (strcmp(printfields[i].name,"mergeHeading") == 0)
			{
			printfields[i].index = INDEX_MERGE_HEADING;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%.3f");
				}
			}
		else if (strcmp(printfields[i].name,"mergeSpeed") == 0)
			{
			printfields[i].index = INDEX_MERGE_SPEED;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%.3f");
				}
			}
		else if (strcmp(printfields[i].name,"mergeDraft") == 0)
			{
			printfields[i].index = INDEX_MERGE_SENSORDEPTH;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%.3f");
				}
			}
		else if (strcmp(printfields[i].name,"mergeSensordepth") == 0)
			{
			printfields[i].index = INDEX_MERGE_SENSORDEPTH;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%.3f");
				}
			}
		else if (strcmp(printfields[i].name,"mergeRoll") == 0)
			{
			printfields[i].index = INDEX_MERGE_ROLL;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%.3f");
				}
			}
		else if (strcmp(printfields[i].name,"mergePitch") == 0)
			{
			printfields[i].index = INDEX_MERGE_PITCH;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%.3f");
				}
			}
		else if (strcmp(printfields[i].name,"mergeHeave") == 0)
			{
			printfields[i].index = INDEX_MERGE_HEAVE;
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, "%.3f");
				}
			}
		else
			{
			for (j=0;j<nfields;j++)
				{
				if (strcmp(printfields[i].name, fields[j].name) == 0)
					printfields[i].index = j;
				}
			if (printfields[i].formatset == MB_NO)
				{
				strcpy(printfields[i].format, fields[printfields[i].index].format);
				}
			}
		}

	/* if verbose print list of print field names */
	if (verbose > 0)
		{
		for (i=0;i<nprintfields;i++)
			{
			if (i == 0)
				fprintf(stdout, "# ");
			fprintf(stdout, "%s", printfields[i].name);
			if (i < nprintfields-1)
				fprintf(stdout, " | ");
			else
				fprintf(stdout, "\n");
			}
		}

	/* read the data records in the auv log file */
	nrecord = 0;
	while (fread(buffer, recordsize, 1, fp) == 1)
		{
		for (i=0;i<nprintfields;i++)
			{
			index = printfields[i].index;
			if (index == INDEX_ZERO)
				{
				dvalue = 0.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			else if (index == INDEX_MERGE_LON)
				{
				interp_status = mb_linear_interp_longitude(verbose,
							nav_time_d-1, nav_navlon-1,
							nav_num, time_d, &dvalue, &jinterp,
							&error);
				if (jinterp < 2 || jinterp > nav_num-2)
					dvalue = 0.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			else if (index == INDEX_MERGE_LAT)
				{
				interp_status = mb_linear_interp_latitude(verbose,
							nav_time_d-1, nav_navlat-1,
							nav_num, time_d, &dvalue, &jinterp,
							&error);
				if (jinterp < 2 || jinterp > nav_num-2)
					dvalue = 0.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			else if (index == INDEX_MERGE_HEADING)
				{
				interp_status = mb_linear_interp_heading(verbose,
							nav_time_d-1, nav_heading-1,
							nav_num, time_d, &dvalue, &jinterp,
							&error);
				if (jinterp < 2 || jinterp > nav_num-2)
					dvalue = 0.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			else if (index == INDEX_MERGE_SPEED)
				{
				interp_status = mb_linear_interp(verbose,
							nav_time_d-1, nav_speed-1,
							nav_num, time_d, &dvalue, &jinterp,
							&error);
				if (jinterp < 2 || jinterp > nav_num-2)
					dvalue = 0.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			else if (index == INDEX_MERGE_SENSORDEPTH)
				{
				interp_status = mb_linear_interp(verbose,
							nav_time_d-1, nav_sensordepth-1,
							nav_num, time_d, &dvalue, &jinterp,
							&error);
				if (jinterp < 2 || jinterp > nav_num-2)
					dvalue = 0.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			else if (index == INDEX_MERGE_ROLL)
				{
				interp_status = mb_linear_interp(verbose,
							nav_time_d-1, nav_roll-1,
							nav_num, time_d, &dvalue, &jinterp,
							&error);
				if (jinterp < 2 || jinterp > nav_num-2)
					dvalue = 0.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			else if (index == INDEX_MERGE_PITCH)
				{
				interp_status = mb_linear_interp(verbose,
							nav_time_d-1, nav_pitch-1,
							nav_num, time_d, &dvalue, &jinterp,
							&error);
				if (jinterp < 2 || jinterp > nav_num-2)
					dvalue = 0.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			else if (index == INDEX_MERGE_HEAVE)
				{
				interp_status = mb_linear_interp(verbose,
							nav_time_d-1, nav_heave-1,
							nav_num, time_d, &dvalue, &jinterp,
							&error);
				if (jinterp < 2 || jinterp > nav_num-2)
					dvalue = 0.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			else if (fields[index].type == TYPE_DOUBLE)
				{
				mb_get_binary_double(MB_YES, &buffer[fields[index].index], &dvalue);
				dvalue *= fields[index].scale;
				if ((strcmp(fields[nfields].name, "mHeadK") == 0
					|| strcmp(fields[nfields].name, "mYawK") == 0)
					&& angles_in_degrees == MB_YES
					&& dvalue < 0.0)
					dvalue += 360.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			else if (fields[index].type == TYPE_INTEGER)
				{
				mb_get_binary_int(MB_YES, &buffer[fields[index].index], &ivalue);
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&ivalue, sizeof(int), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, ivalue);
				}
			else if (fields[index].type == TYPE_TIMETAG)
				{
				mb_get_binary_double(MB_YES, &buffer[fields[index].index], &dvalue);
				time_d = dvalue;
				if (strcmp(printfields[i].format, "time_i") == 0)
					{
					mb_get_date(verbose,time_d,time_i);
					if (output_mode == OUTPUT_MODE_BINARY)
						{
						fwrite(time_i, sizeof(int), 7, stdout);
						}
					else
						{
						fprintf(stdout,"%4.4d %2.2d %2.2d %2.2d %2.2d %2.2d.%6.6d",
							time_i[0],time_i[1],time_i[2],time_i[3],time_i[4],time_i[5],time_i[6]);
						}
					}
				else if (strcmp(printfields[i].format, "time_j") == 0)
					{
					mb_get_date(verbose,time_d,time_i);
					mb_get_jtime(verbose,time_i,time_j);
					if (output_mode == OUTPUT_MODE_BINARY)
						{
						fwrite(&time_i[0], sizeof(int), 1, stdout);
						fwrite(&time_j[1], sizeof(int), 1, stdout);
						fwrite(&time_i[3], sizeof(int), 1, stdout);
						fwrite(&time_i[4], sizeof(int), 1, stdout);
						fwrite(&time_i[5], sizeof(int), 1, stdout);
						fwrite(&time_i[6], sizeof(int), 1, stdout);
						}
					else
						{
						fprintf(stdout,"%4.4d %3.3d %2.2d %2.2d %2.2d.%6.6d",
							time_i[0],time_j[1],time_i[3],time_i[4],time_i[5],time_i[6]);
						}
					}
				else
					{
					if (output_mode == OUTPUT_MODE_BINARY)
						fwrite(&dvalue, sizeof(double), 1, stdout);
					else
						fprintf(stdout, printfields[i].format, time_d);
					}
				}
			else if (fields[index].type == TYPE_ANGLE)
				{
				mb_get_binary_double(MB_YES, &buffer[fields[index].index], &dvalue);
				dvalue *= fields[index].scale;
				if (strcmp(fields[index].name, "mYawCB") == 0
					&& angles_in_degrees == MB_YES
					&& dvalue < 0.0)
					dvalue += 360.0;
				if (output_mode == OUTPUT_MODE_BINARY)
					fwrite(&dvalue, sizeof(double), 1, stdout);
				else
					fprintf(stdout, printfields[i].format, dvalue);
				}
			if (output_mode == OUTPUT_MODE_TAB)
				{
				if (i < nprintfields - 1)
					fprintf(stdout, "\t");
				else
					fprintf(stdout, "\n");
				}
			else if (output_mode == OUTPUT_MODE_CSV)
				{
				if (i < nprintfields - 1)
					fprintf(stdout, ",");
				else
					fprintf(stdout, "\n");
				}
			}
		nrecord++;
		}
	fclose(fp);

	/* deallocate arrays for navigation */
	if (nav_alloc > 0)
		{
		mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_time_d,&error);
		mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_navlon,&error);
		mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_navlat,&error);
		mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_heading,&error);
		mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_speed,&error);
		mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_sensordepth,&error);
		mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_roll,&error);
		mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_pitch,&error);
		mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_heave,&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.º 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 main (int argc, char **argv)
{
	char program_name[] = "mbctdlist";
	char help_message[] =  "mbctdlist lists all CTD records within swath data files\nThe -O option specifies how the values are output\nin an mblist-likefashion.\n";
	char usage_message[] = "mbctdlist [-A -Ddecimate -Fformat -Gdelimeter -H -Ifile -Llonflip -Ooutput_format -V -Zsegment]";
	extern char *optarg;
	int	errflg = 0;
	int	c;
	int	help = 0;
	int	flag = 0;

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

	/* MBIO read control parameters */
	int	read_datalist = MB_NO;
	char	read_file[MB_PATH_MAXLINE];
	void	*datalist;
	int	look_processed = MB_DATALIST_LOOK_UNSET;
	double	file_weight;
	int	format;
	int	pings;
	int	decimate;
	int	lonflip;
	double	bounds[4];
	int	btime_i[7];
	int	etime_i[7];
	double	btime_d;
	double	etime_d;
	double	speedmin;
	double	timegap;
	char	file[MB_PATH_MAXLINE];
	int	beams_bath;
	int	beams_amp;
	int	pixels_ss;

	/* output format list controls */
	char	list[MAX_OPTIONS];
	int	n_list;
	double	distance_total = 0.0;
	int	time_j[5];
	int	mblist_next_value = MB_NO;
	int	invert_next_value = MB_NO;
	int	signflip_next_value = MB_NO;
	int	first = MB_YES;
	int	ascii = MB_YES;
	int	segment = MB_NO;
	char	segment_tag[MB_PATH_MAXLINE];
	char	delimiter[MB_PATH_MAXLINE];

	/* MBIO read values */
	void	*mbio_ptr = NULL;
	void	*store_ptr;
	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];

	/* navigation, heading, attitude data */
	int	survey_count = 0;
	int	survey_count_tot = 0;
	int	nnav = 0;
	int	nnav_alloc = 0;
	double	*nav_time_d = NULL;
	double	*nav_lon = NULL;
	double	*nav_lat = NULL;
	double	*nav_sonardepth = NULL;
	double	*nav_heading = NULL;
	double	*nav_speed = NULL;
	double	*nav_altitude = NULL;

	/* CTD values */
	int	ctd_count = 0;
	int	ctd_count_tot = 0;
	int	nctd;
	double	ctd_time_d[MB_CTD_MAX];
	double	ctd_conductivity[MB_CTD_MAX];
	double	ctd_temperature[MB_CTD_MAX];
	double	ctd_depth[MB_CTD_MAX];
	double	ctd_salinity[MB_CTD_MAX];
	double	ctd_soundspeed[MB_CTD_MAX];
	int	nsensor;
	double	sensor_time_d[MB_CTD_MAX];
	double	sensor1[MB_CTD_MAX];
	double	sensor2[MB_CTD_MAX];
	double	sensor3[MB_CTD_MAX];
	double	sensor4[MB_CTD_MAX];
	double	sensor5[MB_CTD_MAX];
	double	sensor6[MB_CTD_MAX];
	double	sensor7[MB_CTD_MAX];
	double	sensor8[MB_CTD_MAX];
	double	conductivity;
	double	temperature;
	double	potentialtemperature;
	double	depth;
	double	salinity;
	double	soundspeed;

	/* additional time variables */
	int	first_m = MB_YES;
	double	time_d_ref;
	int	first_u = MB_YES;
	time_t	time_u;
	time_t	time_u_ref;
	double	seconds;

	/* course calculation variables */
	double	dlon, dlat, minutes;
	int	degrees;
	char	hemi;
	double	headingx, headingy, mtodeglon, mtodeglat;
	double	course, course_old;
	double	time_d_old;
	double	time_interval;
	double	speed_made_good, speed_made_good_old;
	double	navlon_old, navlat_old;
	double	dx, dy;
	double	b;

	int	read_data;
	int	ictd;
	int	i, j;

	/* get current default values */
	status = mb_defaults(verbose,&format,&pings,&lonflip,bounds,
		btime_i,etime_i,&speedmin,&timegap);
	pings = 1;
	bounds[0] = -360.0;
	bounds[1] = 360.0;
	bounds[2] = -90.0;
	bounds[3] = 90.0;
	ctd_count = 0;
	ctd_count_tot = 0;

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

	/* set up the default list controls
		(Time, lon, lat, conductivity, temperature, depth, salinity, sound speed) */
	list[0]='T';
	list[1]='X';
	list[2]='Y';
	list[3]='H';
	list[4]='C';
	list[5]='c';
	list[6]='^';
	list[7]='c';
	list[8]='S';
	list[9]='s';
	n_list = 10;
	sprintf(delimiter, "\t");
	decimate = 1;

	/* process argument list */
	while ((c = getopt(argc, argv, "AaDdF:f:G:g:I:i:L:l:O:o:Z:z:VvHh")) != -1)
	  switch (c)
		{
		case 'H':
		case 'h':
			help++;
			break;
		case 'V':
		case 'v':
			verbose++;
			break;
		case 'A':
		case 'a':
			ascii = MB_NO;
			flag++;
			break;
		case 'D':
		case 'd':
			sscanf (optarg,"%d", &decimate);
			flag++;
			break;
		case 'F':
		case 'f':
			sscanf (optarg,"%d", &format);
			flag++;
			break;
		case 'G':
		case 'g':
			sscanf (optarg,"%s", delimiter);
			flag++;
			break;
		case 'I':
		case 'i':
			sscanf (optarg,"%s", read_file);
			flag++;
			break;
		case 'L':
		case 'l':
			sscanf (optarg,"%d", &lonflip);
			flag++;
			break;
		case 'O':
		case 'o':
			for(j=0,n_list=0;j<(int)strlen(optarg);j++,n_list++)
				if (n_list<MAX_OPTIONS)
					list[n_list] = optarg[j];
			break;
		case 'Z':
		case 'z':
			segment = MB_YES;
			sscanf (optarg,"%s", segment_tag);
			flag++;
			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);
		}

	/* 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       format:         %d\n",format);
		fprintf(stderr,"dbg2       pings:          %d\n",pings);
		fprintf(stderr,"dbg2       lonflip:        %d\n",lonflip);
		fprintf(stderr,"dbg2       decimate:       %d\n",decimate);
		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);
		fprintf(stderr,"dbg2       ascii:          %d\n",ascii);
		fprintf(stderr,"dbg2       segment:        %d\n",segment);
		fprintf(stderr,"dbg2       segment_tag:    %s\n",segment_tag);
		fprintf(stderr,"dbg2       delimiter:      %s\n",delimiter);
		fprintf(stderr,"dbg2       file:           %s\n",file);
		fprintf(stderr,"dbg2       n_list:         %d\n",n_list);
		for (i=0;i<n_list;i++)
			fprintf(stderr,"dbg2         list[%d]:      %c\n",
						i,list[i]);
		}

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

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

	/**************************************************************************************/

	/* section 1 - read all data and save nav etc for interpolation onto ctd data */


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

	/* output separator for GMT style segment file output */
	if (segment == MB_YES && ascii == MB_YES)
		{
		printf("%s\n", segment_tag);
		}

	/* output info */
	if (verbose >= 1)
		{
		fprintf(stderr, "\nSearching %s for survey records\n", file);
		}

	/* read and print data */
	survey_count = 0;
	first = MB_YES;
	while (error <= MB_ERROR_NO_ERROR)
		{
		/* read a 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);
			}

		/* if survey data save the nav etc */
		if (error <= MB_ERROR_NO_ERROR
			&& kind == MB_DATA_DATA)
			{
			/* allocate memory for navigation/attitude arrays if needed */
			if (nnav + 1 >= nnav_alloc)
				{
				nnav_alloc +=  MBCTDLIST_ALLOC_CHUNK;
				status = mb_reallocd(verbose,__FILE__,__LINE__,nnav_alloc*sizeof(double),(void **)&nav_time_d,&error);
				status = mb_reallocd(verbose,__FILE__,__LINE__,nnav_alloc*sizeof(double),(void **)&nav_lon,&error);
				status = mb_reallocd(verbose,__FILE__,__LINE__,nnav_alloc*sizeof(double),(void **)&nav_lat,&error);
				status = mb_reallocd(verbose,__FILE__,__LINE__,nnav_alloc*sizeof(double),(void **)&nav_speed,&error);
				status = mb_reallocd(verbose,__FILE__,__LINE__,nnav_alloc*sizeof(double),(void **)&nav_sonardepth,&error);
				status = mb_reallocd(verbose,__FILE__,__LINE__,nnav_alloc*sizeof(double),(void **)&nav_heading,&error);
				status = mb_reallocd(verbose,__FILE__,__LINE__,nnav_alloc*sizeof(double),(void **)&nav_altitude,&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);
					}
				}

			/* save the nav etc */
			if (nnav == 0 || time_d > nav_time_d[nnav-1])
				{
				nav_time_d[nnav] = time_d;
				nav_lon[nnav] = navlon;
				nav_lat[nnav] = navlat;
				nav_speed[nnav] = speed;
				nav_sonardepth[nnav] = sonardepth;
				nav_heading[nnav] = heading;
				nav_altitude[nnav] = altitude;
				nnav++;
				}
			survey_count++;
			survey_count_tot++;
			}

		}

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

	/* output info */
	if (verbose >= 1)
		{
		fprintf(stderr, "nav extracted from %d survey records\n", survey_count);
		}

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

	/* output info */
	if (verbose >= 1)
		{
		fprintf(stderr, "\nTotal %d survey records\n", survey_count_tot);
		}

	/**************************************************************************************/

	/* section 2 - read data and output ctd data with time interpolation of nav etc */

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

	/* output info */
	if (verbose >= 1)
		{
		fprintf(stderr, "\nSearching %s for CTD records\n", file);
		}

	/* read and print data */
	ctd_count = 0;
	first = MB_YES;
	while (error <= MB_ERROR_NO_ERROR)
		{
		/* read a 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);
			}

		/* if ctd then extract data */
		if (error <= MB_ERROR_NO_ERROR
			&& (kind == MB_DATA_CTD || kind == MB_DATA_SSV))
			{
			/* extract ctd */
			status = mb_ctd(verbose, mbio_ptr, store_ptr,
						&kind, &nctd, ctd_time_d,
						ctd_conductivity, ctd_temperature,
						ctd_depth, ctd_salinity, ctd_soundspeed, &error);

			/* extract ancilliary sensor data */
			status = mb_ancilliarysensor(verbose, mbio_ptr, store_ptr,
						&kind, &nsensor, sensor_time_d,
						sensor1, sensor2, sensor3,
						sensor4, sensor5, sensor6,
						sensor7, sensor8,
						&error);

			/* loop over the nctd ctd points, outputting each one */
			if (error == MB_ERROR_NO_ERROR && nctd > 0)
				{
				for (ictd=0;ictd<nctd;ictd++)
					{
					/* get data */
					time_d = ctd_time_d[ictd];
					mb_get_date(verbose, time_d, time_i);
					conductivity = ctd_conductivity[ictd];
					temperature = ctd_temperature[ictd];
					depth = ctd_depth[ictd];
					salinity = ctd_salinity[ictd];
					soundspeed = ctd_soundspeed[ictd];

					/* get navigation */
					j = 0;
					speed = 0.0;
					interp_status = mb_linear_interp_longitude(verbose,
								nav_time_d-1, nav_lon-1,
								nnav, time_d, &navlon, &j,
								&error);
					if (interp_status == MB_SUCCESS)
					interp_status = mb_linear_interp_latitude(verbose,
								nav_time_d-1, nav_lat-1,
								nnav, time_d, &navlat, &j,
								&error);
					if (interp_status == MB_SUCCESS)
					interp_status = mb_linear_interp_heading(verbose,
								nav_time_d-1, nav_heading-1,
								nnav, time_d, &heading, &j,
								&error);
					if (interp_status == MB_SUCCESS)
					interp_status = mb_linear_interp(verbose,
								nav_time_d-1, nav_sonardepth-1,
								nnav, time_d, &sonardepth, &j,
								&error);
					if (interp_status == MB_SUCCESS)
					interp_status = mb_linear_interp(verbose,
								nav_time_d-1, nav_altitude-1,
								nnav, time_d, &altitude, &j,
								&error);
					if (interp_status == MB_SUCCESS)
					interp_status = mb_linear_interp(verbose,
								nav_time_d-1, nav_speed-1,
								nnav, time_d, &speed, &j,
								&error);

					/* only output if interpolation of nav etc has worked */
					if (interp_status == MB_YES)
						{

						/* calculate course made good and distance */
						mb_coor_scale(verbose,navlat, &mtodeglon, &mtodeglat);
						headingx = sin(DTR * heading);
						headingy = cos(DTR * heading);
						if (first == MB_YES)
							{
							time_interval = 0.0;
							course = heading;
							speed_made_good = 0.0;
							course_old = heading;
							speed_made_good_old = speed;
							distance = 0.0;
							}
						else
							{
							time_interval = time_d - time_d_old;
							dx = (navlon - navlon_old)/mtodeglon;
							dy = (navlat - navlat_old)/mtodeglat;
							distance = sqrt(dx*dx + dy*dy);
							if (distance > 0.0)
								course = RTD*atan2(dx/distance,dy/distance);
							else
								course = course_old;
							if (course < 0.0)
								course = course + 360.0;
							if (time_interval > 0.0)
								speed_made_good = 3.6*distance/time_interval;
							else
								speed_made_good
									= speed_made_good_old;
							}
						distance_total += 0.001 * distance;

						/* reset old values */
						navlon_old = navlon;
						navlat_old = navlat;
						course_old = course;
						speed_made_good_old = speed_made_good;
						time_d_old = time_d;

						/* now loop over list of output parameters */
						ctd_count++;
						ctd_count_tot++;
						if (nctd % decimate == 0)
						for (i=0; i<n_list; i++)
							{
							switch (list[i])
								{
								case '/': /* Inverts next simple value */
									invert_next_value = MB_YES;
									break;
								case '-': /* Flip sign on next simple value */
									signflip_next_value = MB_YES;
									break;
								case '^': /* use mblist definitions of CcSsTt */
									mblist_next_value = MB_YES;
									break;
								case '1': /* Sensor 1 - volts */
									printsimplevalue(verbose, sensor1[ictd], 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case '2': /* Sensor 2 - volts */
									printsimplevalue(verbose, sensor2[ictd], 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case '3': /* Sensor 3 - volts */
									printsimplevalue(verbose, sensor3[ictd], 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case '4': /* Sensor 4 - volts */
									printsimplevalue(verbose, sensor4[ictd], 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case '5': /* Sensor 5 - volts */
									printsimplevalue(verbose, sensor5[ictd], 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case '6': /* Sensor 6 - volts */
									printsimplevalue(verbose, sensor6[ictd], 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case '7': /* Sensor 7 - volts */
									printsimplevalue(verbose, sensor7[ictd], 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case '8': /* Sensor 8 - volts */
									printsimplevalue(verbose, sensor8[ictd], 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case 'C': /* Conductivity or Sonar altitude (m) */
									if (mblist_next_value == MB_NO)
										printsimplevalue(verbose, conductivity, 0, 5, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									else
										{
										printsimplevalue(verbose, altitude, 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
										mblist_next_value = MB_NO;
										}
									break;
								case 'c': /* Temperature or sonar transducer depth (m) */
									if (mblist_next_value == MB_NO)
										printsimplevalue(verbose, temperature, 0, 5, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									else
										{
										printsimplevalue(verbose, sonardepth, 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
										mblist_next_value = MB_NO;
										}
									break;
								case 'H': /* heading */
									printsimplevalue(verbose, heading, 6, 2, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case 'h': /* course */
									printsimplevalue(verbose, course, 6, 2, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case 'J': /* time string */
									mb_get_jtime(verbose,time_i,time_j);
									seconds = time_i[5] + 0.000001 * time_i[6];
									if (ascii == MB_YES)
									    {
									    printf("%.4d %.3d %.2d %.2d %9.6f",
										time_j[0],time_j[1],
										time_i[3],time_i[4],
										seconds);
									    }
									else
									    {
									    b = time_j[0];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_j[1];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[3];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[4];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[5];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[6];
									    fwrite(&b, sizeof(double), 1, stdout);
									    }
									break;
								case 'j': /* time string */
									mb_get_jtime(verbose,time_i,time_j);
									seconds = time_i[5] + 0.000001 * time_i[6];
									if (ascii == MB_YES)
									    {
									    printf("%.4d %.3d %.4d %9.6f",
										time_j[0],time_j[1],
										time_j[2],seconds);
									    }
									else
									    {
									    b = time_j[0];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_j[1];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_j[2];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_j[3];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_j[4];
									    fwrite(&b, sizeof(double), 1, stdout);
									    }
									break;
								case 'L': /* along-track distance (km) */
									printsimplevalue(verbose, distance_total, 7, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case 'l': /* along-track distance (m) */
									printsimplevalue(verbose, 1000.0 * distance_total, 7, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case 'M': /* Decimal unix seconds since
										1/1/70 00:00:00 */
									printsimplevalue(verbose, time_d, 0, 6, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case 'm': /* time in decimal seconds since
										first record */
									if (first_m == MB_YES)
										{
										time_d_ref = time_d;
										first_m = MB_NO;
										}
									b = time_d - time_d_ref;
									printsimplevalue(verbose, b, 0, 6, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case 'P': /* potential temperature (degrees) */
									/* approximation taken from http://mason.gmu.edu/~bklinger/seawater.pdf
									  on 4/25/2012 - to be replaced by a better calculation at some point */
									potentialtemperature = temperature
												- 0.04 * (1.0 + 0.185 * temperature + 0.35 * (salinity - 35.0)) * (sonardepth / 1000.0)
												-  0.0075 * (1.0 - temperature / 30.0) * (sonardepth * sonardepth / 1000000.0);
									printsimplevalue(verbose, potentialtemperature, 0, 5, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case 'S': /* salinity or speed */
									if (mblist_next_value == MB_NO)
										printsimplevalue(verbose, salinity, 0, 5, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									else
										{
										printsimplevalue(verbose, speed, 5, 2, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
										mblist_next_value = MB_NO;
										}
									break;
								case 's': /* speed made good */
									if (mblist_next_value == MB_NO)
										printsimplevalue(verbose, soundspeed, 0, 3, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									else
										{
										printsimplevalue(verbose, speed_made_good, 5, 2, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
										mblist_next_value = MB_NO;
										}
									break;
								case 'T': /* yyyy/mm/dd/hh/mm/ss time string */
									seconds = time_i[5] + 1e-6 * time_i[6];
									if (ascii == MB_YES)
									    printf("%.4d/%.2d/%.2d/%.2d/%.2d/%9.6f",
										time_i[0],time_i[1],time_i[2],
										time_i[3],time_i[4],seconds);
									else
									    {
									    b = time_i[0];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[1];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[2];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[3];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[4];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = seconds;
									    fwrite(&b, sizeof(double), 1, stdout);
									    }
									break;
								case 't': /* yyyy mm dd hh mm ss time string */
									seconds = time_i[5] + 1e-6 * time_i[6];
									if (ascii == MB_YES)
									    printf("%.4d %.2d %.2d %.2d %.2d %9.6f",
										time_i[0],time_i[1],time_i[2],
										time_i[3],time_i[4],seconds);
									else
									    {
									    b = time_i[0];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[1];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[2];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[3];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = time_i[4];
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = seconds;
									    fwrite(&b, sizeof(double), 1, stdout);
									    }
									break;
								case 'U': /* unix time in seconds since 1/1/70 00:00:00 */
									time_u = (int) time_d;
									if (ascii == MB_YES)
									    printf("%ld",time_u);
									else
									    {
									    b = time_u;
									    fwrite(&b, sizeof(double), 1, stdout);
									    }
									break;
								case 'u': /* time in seconds since first record */
									time_u = (int) time_d;
									if (first_u == MB_YES)
										{
										time_u_ref = time_u;
										first_u = MB_NO;
										}
									if (ascii == MB_YES)
									    printf("%ld",time_u - time_u_ref);
									else
									    {
									    b = time_u - time_u_ref;
									    fwrite(&b, sizeof(double), 1, stdout);
									    }
									break;
								case 'V': /* time in seconds since last value */
								case 'v':
									if (ascii == MB_YES)
									    {
									    if ( fabs(time_interval) > 100. )
										printf("%g",time_interval);
									    else
										printf("%7.3f",time_interval);
									    }
									else
									    {
									    fwrite(&time_interval, sizeof(double), 1, stdout);
									    }
									break;
								case 'X': /* longitude decimal degrees */
									dlon = navlon;
									printsimplevalue(verbose, dlon, 11, 6, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case 'x': /* longitude degress + decimal minutes */
									dlon = navlon;
									if (dlon < 0.0)
										{
										hemi = 'W';
										dlon = -dlon;
										}
									else
										hemi = 'E';
									degrees = (int) dlon;
									minutes = 60.0*(dlon - degrees);
									if (ascii == MB_YES)
									    {
									    printf("%3d %8.5f%c",
										degrees, minutes, hemi);
									    }
									else
									    {
									    b = degrees;
									    if (hemi == 'W') b = -b;
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = minutes;
									    fwrite(&b, sizeof(double), 1, stdout);
									    }
									break;
								case 'Y': /* latitude decimal degrees */
									dlat = navlat;
									printsimplevalue(verbose, dlat, 11, 6, ascii,
											    &invert_next_value,
											    &signflip_next_value, &error);
									break;
								case 'y': /* latitude degrees + decimal minutes */
									dlat = navlat;
									if (dlat < 0.0)
										{
										hemi = 'S';
										dlat = -dlat;
										}
									else
										hemi = 'N';
									degrees = (int) dlat;
									minutes = 60.0*(dlat - degrees);
									if (ascii == MB_YES)
									    {
									    printf("%3d %8.5f%c",
										degrees, minutes, hemi);
									    }
									else
									    {
									    b = degrees;
									    if (hemi == 'S') b = -b;
									    fwrite(&b, sizeof(double), 1, stdout);
									    b = minutes;
									    fwrite(&b, sizeof(double), 1, stdout);
									    }
									break;
								default:
									if (ascii == MB_YES)
									    printf("<Invalid Option: %c>",
										list[i]);
									break;
								}
							if (ascii == MB_YES)
								{
								if (i<(n_list-1)) printf ("%s", delimiter);
								else printf ("\n");
								}
							}
						first = MB_NO;
						}
					}
				}
			}

		/* else if survey data ignore */
		else if (error <= MB_ERROR_NO_ERROR
			&& kind == MB_DATA_DATA)
			{
			}

		}

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

	/* output info */
	if (verbose >= 1)
		{
		fprintf(stderr, "%d CTD records\n", ctd_count);
		}

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

	/* output info */
	if (verbose >= 1)
		{
		fprintf(stderr, "\nTotal %d CTD records\n", ctd_count_tot);
		}

	/* deallocate navigation arrays */
	if (nnav > 0)
		{
		status = mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_time_d,&error);
		status = mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_lon,&error);
		status = mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_lat,&error);
		status = mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_speed,&error);
		status = mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_sonardepth,&error);
		status = mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_heading,&error);
		status = mb_freed(verbose,__FILE__,__LINE__,(void **)&nav_altitude,&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);
}