/*************************************************************************** * FINDARCTIC.C * * version 1.2 * * Written by Matthew Blum 3-14-97 * * Last updated by Matthew Blum 4-23-97 * * * * findarctic will determine where the arctic boundary of a region is, * * based on a random matching, the minimum, and the maximum matching. * **************************************************************************/ #include #include "allocate.h" #include #define TRUE 1 #define FALSE 0 #define TEXT 0 #define MATLAB 1 #define VAX 2 #define FILLED '#' void fillup(char **random, char **matching); char **readvax(FILE *fp, char **match); int maxrow, maxcol; void main(int argc, char **argv) { int row, col, fill = FALSE, out = TEXT, intersect = FALSE; FILE *ran, *min, *max; char **random, **minimum, **maximum, **outside; if (argc < 4) { printf("Usage for findarctic.c: findarctic \n"); printf(" [-intersect | -vax | -text | -matlab]\n"); exit(1); } ran = fopen(argv[1],"r"); if (ran == NULL) { printf("Could not read random file %s for input.\n",argv[1]); exit(1); } min = fopen(argv[2],"r"); if (min == NULL) { printf("Could not read minimum file %s for input.\n",argv[2]); exit(1); } max = fopen(argv[3],"r"); if (max == NULL) { printf("Could not read maximum file %s for input.\n",argv[3]); exit(1); } if (argc > 4) if (!strcmp(argv[4],"-matlab")) out = MATLAB; else if (!strcmp(argv[4],"-vax")) out = VAX; else if (!strcmp(argv[4],"-intersect")) intersect = TRUE; random = readvax(ran, random); minimum = readvax(min, minimum); maximum = readvax(max, maximum); fclose(ran); fclose(min); fclose(max); /* fill on maximum and minimum matching */ if (intersect == FALSE) { fillup(random, maximum); fillup(random, minimum); } /* look for outside of region */ outside = (char **) Allocate_2D(maxrow+2,maxcol+2,1); for (row = 0; row < maxrow + 2; row ++) for (col = 0; col < maxcol + 2; col ++) outside[row][col] = ' '; fillup(random, outside); if (out == VAX) { printf("#Title: Frozen region\n"); printf("#Created by findarctic.c\n"); printf("%d %d\n",maxrow, maxcol); } else if (intersect) { printf("#Created by findarctic.c\n"); printf("%d %d\n",maxrow, maxcol); } for (row = 1; row < maxrow + 1; row ++) { for (col = 1; col < maxcol + 1; col ++) { if (intersect == TRUE) if (random[row][col] != ' ') printf("%c",maximum[row][col]); else printf(" "); else if (out == TEXT) if (outside[row][col] == FILLED) printf(" "); else if (maximum[row][col] == FILLED) printf("X"); else if (minimum[row][col] == FILLED) printf("#"); else printf("."); else if (out == MATLAB) if (outside[row][col] == FILLED) printf("0 "); else if (maximum[row][col] == FILLED || minimum[row][col] == FILLED) printf("1 "); else printf("2 "); else if (out == VAX) if (maximum[row][col] == FILLED || minimum[row][col] == FILLED) printf("%c",random[row][col]); else printf(" "); } printf("\n"); } } /******************************************************************** READ IN VAX FILES Vax file starts at 1 and ends at maxrow or maxcol. There is a one unit padding on all four sides. *******************************************************************/ char **readvax(FILE *fp, char **vax) { int rows, cols; int row, col; int ul, ur, ll; int matchu, matchr, matchd, matchl; int tabwarn = FALSE; char line[100]; char current, okay; /* parse comments in header */ do { fgets(line, 100, fp); } while (line[0] == '#'); /* get sizes */ sscanf(line,"%d %d",&rows, &cols); if (maxrow == 0) { maxrow = rows; maxcol = cols; } else { if (maxrow != rows || maxcol != cols) { printf("Region file and Sample file must be the same size.\n"); exit(1); } } if (maxrow == 0 || maxcol == 0) { printf("Input file is of the wrong format.\n"); exit(1); } /* allocate memory */ /* allocate two extra cells each way for padding of one space on all four sides */ vax = (char **) Allocate_2D(maxrow+2,maxcol+2,1); /* initialize matching array to 0 */ for (row = 0; row < maxrow + 2; row++) for (col = 0; col < maxcol + 2; col++) vax[row][col] = ' '; /* vax file is blank at beginning */ /* read in matching */ for (row = 0; row <= maxrow + 1; row ++) { for (col = 0; col <= maxcol; col ++) { current = fgetc(fp); if (feof(fp) == FALSE) { /* forced edges are not part of a matching, so treat them as spaces */ if (strchr("E3MW",current) != NULL) current = ' '; if (strchr("AVX<>=I \t\n",current) == NULL) { printf("Illegal character '%c' in input file.\n",current); exit(1); } /* display warning if tabs are present */ if (current == '\t') if (tabwarn == FALSE) { fprintf(stderr,"Warning, in input file being interpreted as single space.\n"); tabwarn = TRUE; } /* add one to col and row for padding */ if (current != '\n') vax[row+1][col+1] = current; if (current == '\n') col = maxcol + 3; } else { row = maxrow + 3; col = maxcol + 3; } } if (col < maxcol + 3) { printf("The input file size does not indicate enough columns.\n"); exit(1); } } if (row < maxrow + 3) { printf("The input file size does not indicate enough rows.\n"); exit(1); } return vax; } /******************************************************************** FILL MINIMUM AND MAXIMUM FILES Look for similarities between the random matching and the minimum and maximum matchings. *******************************************************************/ void fillup(char **random, char **matching) { int row, col, fill; matching[0][0] = FILLED; do { fill = FALSE; for (row = 0; row < maxrow + 2; row ++) { for (col = 0; col < maxcol + 2; col ++) /* grow in four directions */ if (matching[row][col] == FILLED) { if (col < maxcol + 1 && matching[row][col+1] == random[row][col+1]) { matching[row][col+1] = FILLED; fill = TRUE; } if (row < maxrow + 1 && matching[row+1][col] == random[row+1][col]) { matching[row+1][col] = FILLED; fill = TRUE; } if (col > 0 && matching[row][col-1] == random[row][col-1]) { matching[row][col-1] = FILLED; fill = TRUE; } if (row > 0 && matching[row-1][col] == random[row-1][col]) { matching[row-1][col] = FILLED; fill = TRUE; } } } } while (fill); }