/* ---------------------------------------------------------------------------
Jason II Annotation Database Extraction (JADE) Utility.
written by C R Young
last updated 6/01/05
Copyright (C) 2005 C. R. Young
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
You may do anything you like with this code,
including, but not limited to, printing it out
and pitching it over the side of the boat.
---------------------------------------------------------------------------*/
#include
#include
#include
#include
#define MAXSTR 80
#define MAXTAGS 1000
#define MAXLINE 5000 //10000
#define MAXLINES 1
#define MAXFIELD 100 //100
#define MAXFILES 25
#define MAXDIVES 100
#define CHECKTAGSIZE 8
struct Table {
char measure[MAXSTR];
int Switch;
};
/* My error message */
void MyError(error_text)
char error_text[];
{
fprintf(stderr,"\nRY's run-time error...\n");
fprintf(stderr,"\n\t%s\n",error_text);
fprintf(stderr,"\n...now exiting to system...\n\n");
exit(1);
}
/* Converts decimal degrees to deg:min:sec vector format - makes all degrees positive */
void ddeg2dms(deg, dms, dm)
float deg;
float *dms;
float *dm;
{
float d, m, s, degrees, decmin;
if( deg < 0.0) {
degrees = -1.0 * deg;
d = floor( degrees );
decmin = (degrees - d) * 60.0;
m = floor( decmin );
s = ( decmin - m) * 60.0;
dms[1]=-d;
dms[2]=m;
dms[3]=s;
dm[1]=-d;
dm[2]=decmin;
} else {
degrees = deg;
d = floor( degrees );
decmin = (degrees - d) * 60.0;
m = floor( decmin );
s = ( decmin - m) * 60.0;
dms[1]=d;
dms[2]=m;
dms[3]=s;
dm[1]=d;
dm[2]=decmin;
}
}
/* Print data to appropriate file */
void PrintLine(file, Print_Options, data, fieldsize, dms, dm, UseLookupTable, CurrentDive, countline, fieldcount, Coordformat)
FILE *file;
struct Table Print_Options[14];
char data[][MAXLINE];
int *fieldsize;
float *dms;
float *dm;
int UseLookupTable;
int CurrentDive;
int countline;
int fieldcount;
int Coordformat;
{
int i, field, line, Fieldsize;
char strtest[MAXSTR];
float deg;
if (UseLookupTable == 1 && CurrentDive > 0) {
fprintf(file, "J2L%d,", CurrentDive);
} else if (UseLookupTable == 1 && CurrentDive == 0) {
fprintf(file, ",", CurrentDive);
}
fprintf(file, "%d", countline);
for (field=0; field < 4; field++) {
if( Print_Options[field].Switch == 1) {
fprintf(file, ",");
for (line=0; line < fieldsize[field]; line++) {
fprintf(file, "%c", data[field][line]);
}
}
}
/* Convert Deg-decimal notation to DMS notation */
if(Coordformat > 0) {
fprintf(file, ",");
/* LAT */
sprintf(strtest, "%c%c%c%c%c%c%c%c%c%c%c", data[2][0], data[2][1], data[2][2], data[2][3], data[2][4], data[2][5], data[2][6], data[2][7], data[2][8], data[2][9]);
deg = atof(strtest);
ddeg2dms(deg, dms, dm);
if (Coordformat == 2) {
for (i=1; i<3; i++){
fprintf(file, "%.0f ", dms[i]);
}
fprintf(file, "%.1f,", dms[3]);
} else if (Coordformat == 1){
fprintf(file, "%.0f ", dm[1]);
fprintf(file, "%2.4f,", dm[2]);
}
/* LON */
sprintf(strtest, "%c%c%c%c%c%c%c%c%c%c%c", data[3][0], data[3][1], data[3][2], data[3][3], data[3][4], data[3][5], data[3][6], data[3][7], data[3][8], data[3][9]);
deg = atof(strtest);
ddeg2dms(deg, dms, dm);
if (Coordformat == 2) {
for (i=1; i<3; i++){
fprintf(file, "%.0f ", dms[i]);
}
fprintf(file, "%.1f", dms[3]);
} else if (Coordformat == 1){
fprintf(file, "%.0f ", dm[1]);
fprintf(file, "%2.4f", dm[2]);
}
}
for (field=4; field < fieldcount+1; field++) {
if( Print_Options[field].Switch == 1) {
if(field < 14) fprintf(file, ",");
for (line=0; line < fieldsize[field]; line++) {
fprintf(file, "%c", data[field][line]);
}
}
}
fprintf(file, "\n");
}
main (int argc, char *argv[])
{
FILE *fptr, *Lookuptable, *Tags, *Formatfile, *OutputFiles[MAXFILES];
char fname[MAXSTR];
int i, j, end, countline, field, line, UseLookupTable, CurrentDive, dive, startswitch, endswitch, Numtags, pos, match;
char data[MAXFIELD][MAXLINE], check[5], num[2], EVT[3];
int fieldsize[MAXFIELD], fieldcount, fieldswitch, lineswitch, charcount, limit;
int JasonLookup[MAXDIVES][7], Numdives, dates[3];
struct Table Print_Options[14];
int inttest, QUALITYCONTROL, Coordformat, matchcount;
char strtest[MAXSTR], AnnTags[MAXTAGS][MAXSTR];
float dms[4];
float dm[4];
float deg;
/*********************/
/* Begin read files */
/*********************/
if( argc > 4 || argc < 4) {
printf("usage: JADE [Quality control? 1/0] [Coord. format? 0(dd)/1(dm)/2(dms)] [infile]\n");
exit(1) ;
}
QUALITYCONTROL = atoi(argv[1]);
Coordformat = atoi(argv[2]);
if(QUALITYCONTROL < 0 || QUALITYCONTROL > 1){
printf("Could not understand your answer...\nDo you want to use use the quality control option? 1=yes, 0=no\n");
MyError("Please restart the program.\n");
}
/* Read format file */
Formatfile = fopen("JADE_format.txt", "r");
if (!Formatfile)
{
printf ( "\nCould not find JADE_print.txt.\nPrinting full data.\n");
sprintf(Print_Options[0].measure , "Class");
sprintf(Print_Options[1].measure , "Date/Time");
sprintf(Print_Options[2].measure , "Lon");
sprintf(Print_Options[3].measure , "Lat");
sprintf(Print_Options[4].measure , "x");
sprintf(Print_Options[5].measure , "y");
sprintf(Print_Options[6].measure , "Hdg");
sprintf(Print_Options[7].measure , "Pitch");
sprintf(Print_Options[8].measure , "Roll");
sprintf(Print_Options[9].measure , "Alt");
sprintf(Print_Options[10].measure , "Depth");
sprintf(Print_Options[11].measure , "Bathy");
sprintf(Print_Options[12].measure , "Type");
sprintf(Print_Options[13].measure , "Event");
for (i=0; i<14; i++){
Print_Options[i].Switch = 1;
}
} else {
for (i=0; i<14; i++){
fscanf(Formatfile,"%s", &Print_Options[i].measure);
}
for (i=0; i<14; i++){
fscanf(Formatfile,"%d", &Print_Options[i].Switch);
}
printf ( "\nPrinting data:\n");
for (i=0; i<4; i++){
if(Print_Options[i].Switch == 1) printf("%s ", Print_Options[i].measure);
}
if(Coordformat == 1) printf("Lat(DM) Lon(DM) ");
if(Coordformat == 2) printf("Lat(DMS) Lon(DMS) ");
for (i=4; i<14; i++){
if(Print_Options[i].Switch == 1) printf("%s ", Print_Options[i].measure);
}
printf ( "\n");
if(fclose(Formatfile) != 0) MyError("Could not close file: divedates.txt.\n");
}
/* Read Jason dive lookup table */
Lookuptable = fopen ("divedates.txt", "r");
if (!Lookuptable)
{
printf ( "\nCould not find divedates.txt.\nNot using dive lookup table.\n");
UseLookupTable = 0 ;
} else {
end = 0;
Numdives = 0;
UseLookupTable = 1;
printf("\nDive lookup table:\n");
printf("\tDive\tStart Time\tEnd Time\n");
while (end == 0) {
fscanf(Lookuptable,"%d", &JasonLookup[Numdives][0]);
if(end == 0){
for (i=1; i<7; i++){
fscanf(Lookuptable,"%d", &JasonLookup[Numdives][i]);
}
end = feof(Lookuptable);
if(end == 0){
printf("%d:\t", Numdives+1);
printf( "%d\t", JasonLookup[Numdives][0]);
printf( "%d/%d %d:00\t", JasonLookup[Numdives][1], JasonLookup[Numdives][2], JasonLookup[Numdives][3]);
printf( "%d/%d %d:00\n", JasonLookup[Numdives][4], JasonLookup[Numdives][5], JasonLookup[Numdives][6]);
}
Numdives += 1;
}
}
Numdives -= 1;
if(fclose(Lookuptable) != 0) MyError("Could not close file: divedates.txt.\n");
}
/* Read 1st level annotation lookup table */
Tags = fopen ("tags.txt", "r");
if (!Tags)
{
printf ( "Error opening file!\nCould not read tags.txt...\n");
MyError("Required file not found!\n");
}
end = 0;
Numtags = 0;
printf("\nAnnotation tags:\n");
while (end == 0) {
if(end == 0){
fscanf(Tags,"%s", &AnnTags[Numtags]);
end = feof(Tags);
if(end == 0){
printf( "%s\n", AnnTags[Numtags]);
}
Numtags += 1;
}
}
printf("\n");
Numtags -= 1;
if(fclose(Tags) != 0) MyError("Could not close file: divedates.txt.\n");
/* memory allocation trap */
if(Numtags > MAXFILES){
limit = MAXFILES;
printf ( "Error, too many tags! (max = %d)\n", limit);
MyError("Recompile with MAXFILES set to a higher number.\n");
}
/* "open" files for reading / writing */
fptr = fopen (argv[3], "r");
OutputFiles[Numtags] = fopen ("Ann.Other.events.csv", "w");
if(QUALITYCONTROL == 1) {
printf("Quality control option on.\n\n");
for(i=0; i-1; i--){
ungetc(check[i],fptr);
}
/* Read line */
if(end == 0 ) {
fieldcount = 0;
charcount = 0;
fieldswitch = 0;
lineswitch = 0;
while(lineswitch == 0) {
charcount = 0;
while(fieldswitch == 0) {
data[fieldcount][charcount] = getc(fptr);
if(data[fieldcount][charcount] == ',' || data[fieldcount][charcount] == '\n'){
fieldswitch = 1;
} else {
charcount += 1;
}
} /* END WHILE FIELDSWITCH */
fieldsize[fieldcount] = charcount;
if(data[fieldcount][charcount] == '\n'){
lineswitch = 1;
} else {
fieldcount += 1;
fieldswitch = 0;
}
} /* END WHILE LINESWITCH */
/* Use dive lookup table */
if (UseLookupTable == 1) {
sprintf(strtest, "%c%c", data[1][5], data[1][6]);
dates[0] = atoi(strtest);
sprintf(strtest, "%c%c", data[1][8], data[1][9]);
dates[1] = atoi(strtest);
sprintf(strtest, "%c%c", data[1][11], data[1][12]);
dates[2] = atoi(strtest);
CurrentDive = 0;
startswitch = 0;
endswitch = 0;
for (dive=0; dive JasonLookup[dive][1]) { /* month of event greater than month of dive i */
startswitch = 1;
} else if(dates[0] == JasonLookup[dive][1]) { /* month of event equal to month of dive i */
if(dates[1] > JasonLookup[dive][2]) { /* day of event greater than day of dive i */
startswitch = 1;
} else if (dates[1] == JasonLookup[dive][2]) { /* day of event equal to day of dive i */
if(dates[2] >= JasonLookup[dive][3]) { /* hour of event greater than hour of dive i */
startswitch = 1;
} else {
startswitch = 0;
}
} else {
startswitch = 0;
}
} else {
startswitch = 0;
}
if(dates[0] < JasonLookup[dive][4]) { /* month of event less than month of dive i */
endswitch = 1;
} else if(dates[0] == JasonLookup[dive][4]) { /* month of event equal to month of dive i */
if(dates[1] < JasonLookup[dive][5]) { /* day of event less than day of dive i */
endswitch = 1;
} else if (dates[1] == JasonLookup[dive][5]) { /* day of event equal to day of dive i */
if(dates[2] < JasonLookup[dive][6]) { /* hour of event less than hour of dive i */
endswitch = 1;
} else {
endswitch = 0;
}
} else {
endswitch = 0;
}
} else {
endswitch = 0;
}
if (startswitch == 1 && endswitch == 1) {
CurrentDive = JasonLookup[dive][0];
}
}
}
/**********************************/
/* Print data to appropriate file */
/**********************************/
matchcount = 0;
for(i=0; i 0) {
for(i=1; i 12){
for(i=1; i 12){
for(i=1; i