home *** CD-ROM | disk | FTP | other *** search
- /*** IOread.c - read input etc ***/
-
- #include <stdio.h>
- #include <strings.h>
- #include "common.h"
- #include "contour.h"
- #include "plot.h"
- #include "Xdefs.h"
-
- /* Read the command line */
- fileopen(argc,argv)
- int argc;
- char **argv;
- {
- FILE *fp,*optionfile, *fopen();
- double atof();
- extern char printer[MAXCHAR];
- extern int cont_out, expand, grid, postscript, oldformat, contlabel;
- extern int joincurve, printplot;
- extern char *joinlevel;
- extern double cont_level;
- int i,FILEFOUND;
-
- /* initialize general X arguments */
- progname = argv[0];
- font_name = NULL;
- geometry = NULL;
- display_name = NULL;
- border_color = NULL;
- fore_color = NULL;
- back_color = NULL;
- mouse_color = NULL;
- icon_color = NULL;
- reverse = 0;
- border_width = 0;
-
- FILEFOUND = FALSE;
- for (i=1; i < argc; i++) { /* Parse Line */
- if (argv[i][0] == '=') {
- geometry = argv[i];
- continue;
- }
- if (strncmp(argv[i],"-geom",5)==0) { /* new geometry */
- if (++i >= argc) usage(progname);
- geometry = argv[i];
- continue;
- }
- if (index(argv[i],':')!=NULL) { /* host:display */
- strncpy(display_name,argv[i],sizeof(display_name));
- continue;
- }
- if (!strncmp(argv[i],"-d",2)) { /* new display */
- if (++i >= argc) usage(progname);
- display_name = argv[i];
- continue;
- }
- if (strcmp(argv[i],"-rv") == 0) { /* reverse display */
- reverse = 1;
- continue;
- }
- if (strcmp(argv[i],"-bw") == 0) { /* border width */
- if (++i >= argc) usage(progname);
- border_width = atoi(argv[i]);
- continue;
- }
- if (strcmp(argv[i],"-bd") == 0) { /* border color */
- if (++i >= argc) usage(progname);
- border_color = argv[i];
- continue;
- }
- if (strcmp(argv[i],"-fg") == 0) { /* foreground color */
- if (++i >= argc) usage(progname);
- fore_color = argv[i];
- continue;
- }
- if (strcmp(argv[i],"-bg") == 0) { /* background color */
- if (++i >= argc) usage(progname);
- back_color = argv[i];
- continue;
- }
- if (strcmp(argv[i],"-fn") == 0) { /* font type */
- if (++i >= argc) usage(progname);
- font_name = argv[i];
- continue;
- }
- if (strcmp(argv[i],"-c") == 0) { /* output contour */
- if (++i >= argc) usage(progname);
- cont_level = atof(argv[i]);
- cont_out = ON;
- continue;
- }
- if (strcmp(argv[i],"-e") == 0) { /* expand into 3D */
- expand = ON;
- fprintf(stdout," Commands: expand (3D) = ON\n");
- continue;
- }
- if (strcmp(argv[i],"-j") == 0) { /* join curves */
- if (++i >= argc) usage(progname);
- joincurve = ON;
- joinlevel = argv[i];
- if ((strcmp(joinlevel,"HIGH")==0)||(strcmp(joinlevel,"high")==0)||
- (strcmp(joinlevel,"LOW" )==0)||(strcmp(joinlevel,"low" )==0)) {
- fprintf(stdout," Commands: joincurve = ON\n");
- fprintf(stdout," Commands: joinlevel = %s\n",joinlevel);
- } else {
- fprintf(stdout," Warning : Invalid keyword -");
- fprintf(stdout," [-j] option only accepts [HIGH/LOW]\n");
- joinlevel = OFF;
- }
- continue;
- }
- if (strcmp(argv[i],"-g") == 0) { /* grid */
- grid = ON;
- fprintf(stdout," Commands: grid = ON\n");
- continue;
- }
- if (strcmp(argv[i],"-l") == 0) { /* label */
- contlabel = OFF;
- fprintf(stdout," Commands: contlabel = OFF\n");
- continue;
- }
- if (strcmp(argv[i],"-print") == 0) { /* printplot */
- printplot = ON;
- fprintf(stdout," Commands: printplot = ON\n");
- continue;
- }
- if (strcmp(argv[i],"-ps") == 0) { /* postscript */
- postscript = OFF;
- fprintf(stdout," Commands: postscript = OFF\n");
- continue;
- }
- if (strcmp(argv[i],"-old") == 0) { /* old format */
- oldformat = ON;
- fprintf(stdout," Commands: oldformat = ON\n");
- continue;
- }
- if (strcmp(argv[i],"-s") == 0) { /* scale factor */
- if (++i >= argc) usage(progname);
- scale = atof(argv[i]);
- if (scale < 0.1 || scale > 1) {
- scale = 1.0;
- fprintf(stdout," Warning : Invalid scale -");
- fprintf(stdout," scale reset to %f\n",scale);
- }
- fprintf(stdout," Commands: scale = %f\n",scale);
- continue;
- }
- if (strcmp(argv[i],"-o") == 0) { /* options file */
- if (++i >= argc) usage(progname);
- if ((optionfile = fopen(argv[i],"r")) == NULL) {
- fprintf(stderr,"cat: can't open %s\n", argv[i]);
- usage(progname);
- } else {
- fprintf(stdout," Commands: options-file = \"%s\"\n",argv[i]);
- parse(optionfile);
- fclose(optionfile);
- }
- continue;
- }
- if (argv[i][0] == '-' && argv[i][1] == 'P') { /* Printer */
- strcpy(printer,argv[i]);
- fprintf(stdout," Commands: Printer = %s\n",printer);
- continue;
- }
- if (argv[i][0] == '-') usage(progname);
- if (argv[i][0] != '-' && argv[i][0] != '=') {
- FILEFOUND = TRUE;
- if ((fp = fopen(argv[i], "r")) == NULL) {
- fprintf(stderr,"cat: can't open %s\n", argv[i]);
- exit(1);
- } else {
- imagefile = argv[i];
- fileread(fp);
- fclose(fp);
- }
- }
- }
- if (FILEFOUND == FALSE) usage(progname);
- /* exit(0); */
- }
-
- /* read in the data */
- fileread(fp)
- FILE *fp;
- {
- float rdfloat();
- triaptr insert_tria();
- data *create_data_array();
- data get_data_array_value();
- void free_data_array();
- extern int joincurve;
- extern char *joinlevel;
- extern double xmin, xmax, ymin, ymax, zmin, zmax;
- extern int oldformat;
- double x0,xl,y0,yl,z,delta,ztarget;
- data *datapt, midpt, pt00, pt01, pt10, pt11;
- int i,j,nx,ny,isize,jsize;
-
- zmin = LARGE;
- zmax = -LARGE;
-
- if (!oldformat) {
- /* read the number of x and y points */
- xmin = rdfloat(fp);
- xmax = rdfloat(fp);
- ymin = rdfloat(fp);
- ymax = rdfloat(fp);
- xl = xmax - xmin;
- yl = ymax - ymin;
- nx = (int)rdfloat(fp);
- ny = (int)rdfloat(fp);
- } else {
- /* old contour format from SPLAT - assume 50 x 50 data */
- x0 = rdfloat(fp);
- y0 = rdfloat(fp);
- xl = rdfloat(fp);
- yl = rdfloat(fp);
- xmin = x0;
- xmax = x0+xl;
- ymin = y0;
- ymax = y0+yl;
- nx = 50;
- ny = 50;
- }
-
- if ((nx < 0) || (ny < 0)) {
- fprintf(stderr,"No of array gridpoints is less than 0 : ");
- fprintf(stderr,"nx=%d ny=%d\n",nx,ny);
- exit(1);
- }
- /* create the new array */
- isize = nx;
- jsize = ny;
- if (joincurve) {
- isize = nx + 2;
- jsize = ny + 2;
- }
- datapt = create_data_array(isize,jsize);
-
- if (joincurve == OFF) {
- /* read the intensity values next */
- /* data goes from i=0 to i=npts-1 */
- for (j=0; j<ny; j++)
- for (i=0; i<nx; i++) {
- z = rdfloat(fp);
- if (z < zmin) zmin = z;
- if (z > zmax) zmax = z;
- midpt.x = xmin + i*xl/(double)(nx-1);
- midpt.y = ymin + j*yl/(double)(ny-1);
- midpt.z = z;
- assign_data_array_value(datapt,i,j,isize,jsize,&midpt);
- }
- /* now put the values into triangles */
- for (i=0; i<nx-1; i++)
- for (j=0; j<ny-1; j++) {
- pt00 = get_data_array_value(datapt,i ,j ,isize,jsize);
- pt10 = get_data_array_value(datapt,i+1,j ,isize,jsize);
- pt01 = get_data_array_value(datapt,i ,j+1,isize,jsize);
- pt11 = get_data_array_value(datapt,i+1,j+1,isize,jsize);
- midpt.x = 0.5*(pt00.x + pt10.x);
- midpt.y = 0.5*(pt00.y + pt01.y);
- midpt.z = 0.25*(pt00.z + pt01.z +
- pt10.z + pt11.z);
- insert_tria(&(pt00),&(pt10),&midpt);
- insert_tria(&(pt10),&(pt11),&midpt);
- insert_tria(&(pt11),&(pt01),&midpt);
- insert_tria(&(pt01),&(pt00),&midpt);
- }
- } else {
- if (xl > yl) delta = 1.0e-4 * yl;
- else delta = 1.0e-4 * xl;
- /* read the intensity values next */
- /* data goes from i=1 to i=npts */
- for (j=1; j<=ny; j++)
- for (i=1; i<=nx; i++) {
- z = rdfloat(fp);
- if (z < zmin) zmin = z;
- if (z > zmax) zmax = z;
- midpt.x = xmin + (i-1)*xl/(double)(nx-1);
- midpt.y = ymin + (j-1)*yl/(double)(ny-1);
- midpt.z = z;
- assign_data_array_value(datapt,i,j,isize,jsize,&midpt);
- }
- if ((strcmp(joinlevel,"HIGH")==0)||(strcmp(joinlevel,"high")==0))
- ztarget = zmax;
- else
- ztarget = zmin;
- /* now fill up a boundary layer, i=0, i=npts+1 */
- for (i=0; i<=nx+1; i++) {
- j=0;
- midpt.x = xmin + (i-1)*xl/(double)(nx-1);
- if (i==0 ) midpt.x = xmin - delta;
- if (i==nx+1) midpt.x = xmax + delta;
- midpt.y = ymin - delta;
- midpt.z = ztarget;
- assign_data_array_value(datapt,i,j,isize,jsize,&midpt);
- j=ny+1;
- midpt.x = xmin + (i-1)*xl/(double)(nx-1);
- if (i==0 ) midpt.x = xmin - delta;
- if (i==nx+1) midpt.x = xmax + delta;
- midpt.y = ymax + delta;
- midpt.z = ztarget;
- assign_data_array_value(datapt,i,j,isize,jsize,&midpt);
- }
- for (j=0; j<=ny+1; j++) {
- i=0;
- midpt.x = xmin - delta;
- midpt.y = ymin + (j-1)*yl/(double)(ny-1);
- if (j==0 ) midpt.y = ymin - delta;
- if (j==ny+1) midpt.y = ymax + delta;
- midpt.z = ztarget;
- assign_data_array_value(datapt,i,j,isize,jsize,&midpt);
- i=nx+1;
- midpt.x = xmax + delta;
- midpt.y = ymin + (j-1)*yl/(double)(ny-1);
- if (j==0 ) midpt.y = ymin - delta;
- if (j==ny+1) midpt.y = ymax + delta;
- midpt.z = ztarget;
- assign_data_array_value(datapt,i,j,isize,jsize,&midpt);
- }
-
- /* now put the values into triangles */
- for (i=0; i<nx+1; i++)
- for (j=0; j<ny+1; j++) {
- pt00 = get_data_array_value(datapt,i ,j ,isize,jsize);
- pt10 = get_data_array_value(datapt,i+1,j ,isize,jsize);
- pt01 = get_data_array_value(datapt,i ,j+1,isize,jsize);
- pt11 = get_data_array_value(datapt,i+1,j+1,isize,jsize);
- midpt.x = 0.5*(pt00.x + pt10.x);
- midpt.y = 0.5*(pt00.y + pt01.y);
- midpt.z = 0.25*(pt00.z + pt01.z +
- pt10.z + pt11.z);
- insert_tria(&(pt00),&(pt10),&midpt);
- insert_tria(&(pt10),&(pt11),&midpt);
- insert_tria(&(pt11),&(pt01),&midpt);
- insert_tria(&(pt01),&(pt00),&midpt);
- }
- }
-
- /* now free the array */
- free_data_array(datapt);
- }
-
- /* Read a float; skip the whole line if failed */
- float rdfloat(fp)
- FILE *fp;
- {
- int n;
- char c;
- float x;
-
- /* try to read a floating variable */
- /* if fail, then skip the whole line */
- while ( (n=fscanf(fp,"%f",&x)) != EOF && n!=1) {
- if (n!=1)
- while ( (c=getc(fp)) != EOF && c != '\n');
- }
-
- if (n==EOF) {
- fprintf(stderr,"Error: Incorrect Format!\n");
- exit(1);
- }
- return(x);
- }
-
- /* the options */
- usage (program)
- char *program;
- {
- fprintf(stderr,"usage: %s [-o options_file] [-Pprinter] \
- [-s scale] [-c level] [-e] [-g] [-j joinlevel] [-l] \
- [-print] [-ps] [-old] contour_file\n",program);
- exit(1);
- }
-