home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / saoimage / sao1_07.tar / irafenv.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  10KB  |  300 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    irafenv.c (IRAF Environment)
  6.  * Purpose:    Do things needed to coorinate paths and coordinates with IRAF
  7.  * Subroutine:    update_wcs()        returns: int
  8.  * Subroutine:    get_fbconfig()        returns: int
  9.  * Subroutine:    set_path_iraf()        returns: void
  10.  * Unix calls:    fopen(), fclose(), getenv()
  11.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  12.  *        You may do anything you like with this file except remove
  13.  *        this copyright.  The Smithsonian Astrophysical Observatory
  14.  *        makes no representations about the suitability of this
  15.  *        software for any purpose.  It is provided "as is" without
  16.  *        express or implied warranty.
  17.  * Modified:    {0} Michael VanHilst    initial version          9 July 1989
  18.  *        {1} Jay Travisano (STScI)    VMS changes          10 Nov 1989
  19.  *              {2} MVH BSDonly strings.h compatability           19 Feb 1990
  20.  *        {n} <who> -- <does what> -- <when>
  21.  */
  22.  
  23. #include <ctype.h>
  24. #include <stdio.h>        /* define FILE, stderr, fopen(), fclose() */
  25.  
  26. #ifndef VMS
  27. #ifdef SYSV
  28. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  29. #else
  30. #include <strings.h>        /* strlen, etc. for unenlightened BSD's */
  31. #endif
  32. #else
  33. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  34. #endif
  35.  
  36. #include "hfiles/coord.h"
  37. #include "hfiles/define.h"    /* SZ_FNAME, etc. */
  38. #include "hfiles/image.h"
  39. #include "hfiles/imtool.h"
  40.  
  41. /*
  42.  * Subroutine:    set_path_iraf
  43.  * Purpose:    Construct the pathname of a user datafile by IRAF convention.
  44.  */
  45. void set_path_iraf ( filename )
  46.      char *filename;        /* i: root filename */
  47. {
  48.   static char temproot[64];
  49.   char *udir, *getenv();
  50.  
  51.   /* were we passed an absolute pathname as input? */
  52.   if( *filename == '/' ) {
  53.     return;
  54.   }
  55.   (void)strcpy(temproot, filename);
  56.   /* get defined directory in order of preference */
  57.   if( (udir = getenv("WCSDIR")) == NULL )
  58.     if( (udir = getenv("wcsdir")) == NULL )
  59.       if( (udir = getenv("HOME")) == NULL )
  60.     /* at this point we should be checking ~user/iraf/uparm */
  61. #ifndef VMS
  62.     udir = "/tmp";
  63. #else
  64.     udir = "TMP:";
  65. #endif
  66.   if( udir[strlen(udir)-1] == '/' )
  67.     sprintf(filename, "%s%s", udir, temproot);
  68.   else
  69. #if VMS
  70.     /* don't need slash */
  71.     sprintf(filename, "%s%s", udir, temproot);
  72. #else
  73.     sprintf(filename, "%s/%s", udir, temproot);
  74. #endif
  75. }
  76.  
  77. #ifdef IMTOOL
  78. static char title[SZ_FNAME];
  79. /* static char comment[SZ_FNAME]; */
  80.  
  81. /*
  82.  * Subroutine:    update_wcs 
  83.  * Purpose:    Load the screen WCS, if not yet validated, from the user
  84.  *        wcs file, if any.  (wcs =  "WORLD COORDINATE SYSTEM")
  85.  * UNIX calls:    fopen(), fclose()
  86.  * Note:    File format (two lines):
  87.  *        image title (imtool header label string)\n
  88.  *        a b c d tx ty (WCS coordinate transformation matrix)
  89.  */
  90. int update_wcs ( image, coord, frame_number, wcsbuf )
  91.      struct imageRec *image;
  92.      struct coordRec *coord;
  93.      int frame_number;
  94.      char *wcsbuf;
  95. {
  96.   float xx, yx, xy, yy, xo, yo;    /* l: wcs matrix values */
  97.   float low, high;        /* l: scaling limits */
  98.   int w_type;            /* l: scaling code */
  99.   int tokens;            /* l: number of tokens successfully parsed */
  100.   FILE *fp;
  101.   char label[1024];        /* l: file title */
  102.   char wcs_file[SZ_FNAME];
  103.   char root_name[64];
  104.   int guess_true_file_coords();
  105.   void set_path_iraf(), rotate_transform(), set_trans_speed(), show_filename();
  106.   void invert_transform(), combine_transform();
  107.  
  108.   if( wcsbuf == NULL ) {
  109.     /* get location of WCSFILE from environment */
  110.     sprintf(root_name, WCSFILE, frame_number);
  111.     (void)strcpy(wcs_file, root_name);
  112.     set_path_iraf(wcs_file);
  113.     fp = fopen(wcs_file, "r");
  114.     /* if environment not correctly set, try Imtool's defaults, then IRAF's */
  115.     if( fp == NULL ) {
  116.       sprintf(wcs_file, "%s%s\0", "uparm/", root_name);
  117.       fp = fopen(wcs_file, "r");
  118.     }
  119. #ifndef VMS
  120.     if( fp == NULL ) {
  121.       sprintf(wcs_file, "%s%s\0", "~/iraf/uparm/", root_name);
  122.       fp = fopen(wcs_file, "r");
  123.     }
  124.     if( fp == NULL ) {
  125.       sprintf(wcs_file, "%s%s\0", "/tmp/", root_name);
  126.       fp = fopen(wcs_file, "r");
  127.     }
  128. #else
  129.     if( fp == NULL ) {
  130.       char *getenv();
  131.  
  132.       (void)strcpy(wcs_file, getenv("HOME"));
  133.       (void)strcat(&wcs_file[strlen(wcs_file)-1], ".IRAF.UPARM]");
  134.       (void)strcat(wcs_file, root_name);
  135.       fp = fopen(wcs_file, "r");
  136.     }
  137.     if( fp == NULL ) {
  138.       sprintf(wcs_file, "%s%s\0", "TMP:", root_name);
  139.       fp = fopen(wcs_file, "r");
  140.     }
  141. #endif
  142.     if( fp == NULL ) {
  143.       (void)fprintf(stderr,
  144.             "WARNING: cannot find imtool coordinate matrix - %s\n",
  145.             wcs_file);
  146.       return( 0 );
  147.     }
  148.     /* get iraf coordinate transform matrix */
  149.     tokens = fscanf(fp, "%[^\n]\n%f%f%f%f%f%f%f%f%d", label,
  150.             &xx, &yx, &xy, &yy, &xo, &yo, &low, &high, &w_type);
  151.     (void)fclose(fp);
  152.   } else {
  153.     tokens = sscanf(wcsbuf, "%[^\n]\n%f%f%f%f%f%f%f%f%d", label,
  154.             &xx, &yx, &xy, &yy, &xo, &yo, &low, &high, &w_type);
  155.   }
  156.   if( tokens < 7 ) {
  157.     (void)fprintf(stderr, "WARNING: error reading imtool.wcs file\n");
  158.     coord->imgtofile.inx_outx = 1.0;
  159.     coord->imgtofile.iny_outx = 0.0;
  160.     coord->imgtofile.inx_outy = 0.0;
  161.     coord->imgtofile.iny_outy = 1.0;
  162.     coord->imgtofile.iadd_outx = 0.5;
  163.     coord->imgtofile.iadd_outy = 0.5;
  164.     coord->imgtofile.add_outx = 0.0;
  165.     coord->imgtofile.add_outy = 0.0;
  166.     image->fiscaled = 0;
  167.     /* update the transform for tracking info */
  168.     combine_transform(&coord->buftofile, &coord->buftoimg, &coord->imgtofile);
  169.     return( 0 );
  170.   } else {
  171.     /* copy the name */
  172.     (void)strncpy(title, label, SZ_FNAME);
  173.     label[SZ_FNAME - 1] = '\0';
  174.     image->filename = title;
  175.     /* install its coordinate transform matrix */
  176.     coord->imgtofile.inx_outx = xx;
  177.     coord->imgtofile.iny_outx = yx;
  178.     coord->imgtofile.inx_outy = xy;
  179.     coord->imgtofile.iny_outy = yy;
  180.     coord->imgtofile.iadd_outx = xo;
  181.     coord->imgtofile.iadd_outy = yo;
  182.     /* fill in the missing add_out params */
  183.     if( yx == 0.0 ) {
  184.       coord->imgtofile.add_outx = xo - (0.5 * xx);
  185.       coord->imgtofile.add_outy = yo - (0.5 * yy);
  186.     } else {
  187.       coord->imgtofile.add_outx = xo - (0.5 * yx);
  188.       coord->imgtofile.add_outy = yo - (0.5 * xy);
  189.     }
  190.     /* apply any additional user requested rotation */
  191.     if( (!image->row_order) || (image->rotate_code != 0) )
  192.       rotate_transform(&coord->img, &coord->imgtofile,
  193.                !image->row_order, image->rotate_code);
  194.     /* compute speedy integer computation parameters */
  195.     set_trans_speed(&coord->imgtofile);
  196.     /* compute the inverse parameters (complete) */
  197.     invert_transform(&coord->filetoimg, &coord->imgtofile, 0.0);
  198.     /* update the transform for tracking info */
  199.     combine_transform(&coord->buftofile, &coord->buftoimg, &coord->imgtofile);
  200.     /* check for subsection in title and compute true coords (if qp) */
  201.     coord->imtool_aux = guess_true_file_coords(title);
  202.     /* get IRAF/display scaling (uses linear scaling between low and high) */
  203.     if( (tokens < 10) || (low == high) || (w_type != 1) ) {
  204.       image->fiscaled = 0;
  205.     } else {
  206.       image->fiscaled = 1;
  207.       image->fiscale = (high - low) / (CMS_DATARANGE - 1);
  208.       image->fibias = low - image->fiscale;
  209.     }
  210.     show_filename();
  211.     return( 1 );
  212.   }
  213. }
  214.  
  215. /*
  216.  * Subroutine:    get_fbconfig
  217.  * Purpose:    Read the IMTOOL startup file to get frame buffer sizes.
  218.  * UNIX calls:    fopen(), fclose(), getenv()
  219.  * File format:        configno nframes width height [extra fields]
  220.  *        e.g.,        1  2  512  512
  221.  *                2  2  800  800
  222.  *                3  1 1024 1024        # comment
  223.  */
  224. int get_fbconfig ( config_number, width, height )
  225.      int config_number;
  226.      int *width, *height;
  227. {
  228.   register char    *ip;
  229.   register FILE    *fp;
  230.   int config, nframes;
  231.   char lbuf[SZ_LINE+1], *fname, *getenv();
  232.  
  233.   /* 0 chooses the default configuration */
  234.   if( config_number == 0 ) {
  235.     *width = DEF_FRAME_WIDTH;
  236.     *height = DEF_FRAME_HEIGHT;
  237.     return( 1 );
  238.   }
  239.   /* attempt to open the config file. */
  240.   fp = NULL;
  241.   if( (fname=getenv(FBCONFIG_ENV1)) || (fname=getenv(FBCONFIG_ENV2)) )
  242.     fp = fopen(fname, "r");
  243.   if( !fp && (fname = getenv("HOME")) ) {
  244. #ifndef VMS
  245.     sprintf(lbuf, "%s/%s", fname, FBCONFIG_1);
  246. #else
  247.     sprintf(lbuf, "%s%s", fname, FBCONFIG_1);
  248. #endif
  249.     fp = fopen(fname = lbuf, "r");
  250.   }
  251.   if( !fp )
  252.     fp = fopen(fname = FBCONFIG_2, "r");
  253.   /* if cannot find a config file, return error */
  254.   if( !fp ) {
  255.     (void)fprintf(stderr, "Error: cannot find imtool configuration table\n");
  256.     return( 0 );
  257.   }
  258.   /* scan the frame buffer configuration file. */
  259.   do {
  260.     /* read in next line and check for eof */
  261.     if( fgets (lbuf, SZ_LINE, fp) == NULL ) {
  262.       (void)fprintf(stderr,
  263.             "Error: configuration not found in %s table\n", fname);
  264.       return( 0 );
  265.     }
  266.     config = 0;
  267.     /* skip over leading spaces and tabs */
  268.     for( ip=lbuf;  (*ip == ' ') || (*ip == '\t');  ip++ );
  269.     /* skip comment lines and blank lines. */
  270.     if( *ip == '\n' || *ip == '#' )
  271.       continue;
  272.     if( !isdigit(*ip) )
  273.       continue;
  274.     switch( sscanf (ip, "%d%d%d%d", &config, &nframes, width, height) ) {
  275.     case 4:
  276.       break;            /* normal case */
  277.     case 3:
  278.       *height = *width;        /* default to square format */
  279.       break;
  280.     default:
  281.       (void)fprintf(stderr, "imtool: bad config `%s'\n", ip);
  282.       config = 0;
  283.       continue;
  284.     }
  285.     /* is this the configuration we want? */
  286.   } while( config != config_number );
  287.   if( *width < 1 ) *width = 1;
  288.   if( *height < 1 ) *height = 1;
  289.   (void)fclose(fp);
  290.   return( 1 );
  291. }
  292. #endif
  293.                     
  294.                                                                
  295.                                                                
  296.                                                                
  297.                                                                
  298.                                                                
  299.                                        
  300.