/* * list2table.c * * Postprocess EQ3/6 output files (from a scan with EQ3NR or a single EQ6 * calculation) to get a table of the concentration of all species as * a function of pH, Eh, T, or any varied component concentration. * * Author: Dr.Vinzenz Brendler, FZ Rossendorf * * Last Modification: 21/12/2005 */ #include #include /* Matrix dimensions (rows and columns) */ #define MAX_SPECIES 100 /* Aqueous species & solids */ #define MAX_POINTS 1000 /* Number of modeling steps */ #define MAX_COLS (MAX_SPECIES + 1) #define MAX_ROWS (MAX_POINTS + 1) #define FALSE 0 #define TRUE 1 #define STEP "#" /* Marker to indicate a new step */ #define NAME_LEN 75 main(argc, argv) int argc; char *argv[]; { typedef short int FLAG; extern FILE *openfile(); /* Declaration and initialization of variables */ float numerical; float table[MAX_ROWS][MAX_COLS]; char species[MAX_COLS][NAME_LEN]; char line[BUFSIZ]; char infile[NAME_LEN]; char string[NAME_LEN]; FLAG iaflag = 1; FLAG s_flag; int cols, rows, last_col, last_row; FILE *fp; last_col = 0; rows = -1; /* Check for command line parameters and open file */ if(--argc) strcpy(infile, *++argv); if((fp = openfile("EQ3/6 scan list", infile, "r", iaflag)) == NULL) return(-1); /* Read in line until EOF and split into 2 fields (string & numerical) */ while(fgets(line, BUFSIZ, fp) != NULL) { sscanf(line, "%s %f", string, &numerical); /* Check if entry starts a new modeling step */ if( ! strncmp(string, STEP, 1)) { strcpy(species[0], string); if(rows >= MAX_ROWS - 1) { fprintf(stderr, "Too many points - stop\n"); break; } rows++; table[rows][0] = numerical; for(cols = 1; cols < MAX_COLS; cols++) table[rows][cols] = 0; continue; } /* Scan for species name and fill appropriate table entry */ s_flag = FALSE; for(cols = 0; cols <= last_col; cols++) { if( ! strcmp(string, species[cols])) table[rows][cols] = numerical, s_flag = TRUE; } /* Add new species to list */ if(s_flag == FALSE) { if(last_col == MAX_COLS - 1) { fprintf(stderr, "Too many species - %s not stored\n", string); } else { last_col++; strcpy(species[last_col], string); table[rows][last_col] = numerical; } } } /* End of line while-loop */ /* Output of table */ last_row = rows; for(cols = 0; cols <= last_col; cols++) printf("%s ", species[cols]); putchar('\n'); for(rows = 0; rows <= last_row; rows++) { for(cols = 0; cols <= last_col; cols++) printf("%g ", table[rows][cols]); putchar('\n'); } return(0); } /* End of program */