home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / x11 / lib2 / fitshd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-24  |  8.6 KB  |  306 lines

  1. /*
  2. %    FITSHD . C
  3. %
  4. %    FITS header handlers
  5. %
  6. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  7.  
  8. This software is copyright (C) by the Lawrence Berkeley Laboratory.
  9. Permission is granted to reproduce this software for non-commercial
  10. purposes provided that this notice is left intact.
  11.  
  12. It is acknowledged that the U.S. Government has rights to this software
  13. under Contract DE-AC03-765F00098 between the U.S.  Department of Energy
  14. and the University of California.
  15.  
  16. This software is provided as a professional and academic contribution
  17. for joint exchange. Thus, it is experimental, and is provided ``as is'',
  18. with no warranties of any kind whatsoever, no support, no promise of
  19. updates, or printed documentation. By using this software, you
  20. acknowledge that the Lawrence Berkeley Laboratory and Regents of the
  21. University of California shall have no liability with respect to the
  22. infringement of other copyrights by any part of this software.
  23.  
  24. For further information about this notice, contact William Johnston,
  25. Bld. 50B, Rm. 2239, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  26. (wejohnston@lbl.gov)
  27.  
  28. For further information about this software, contact:
  29.     Jin Guojun
  30.     Bld. 50B, Rm. 2275, Lawrence Berkeley Laboratory, Berkeley, CA, 94720.
  31.     g_jin@lbl.gov
  32.  
  33. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  34. %
  35. % AUTHOR:    Guojun Jin - LBL    8/1/90
  36. */
  37.  
  38. #include "header.def"
  39. #include "imagedef.h"
  40. #include <math.h>
  41.  
  42. #define    write_line(str, fp)    fwrite(str, sizeof(char), 80, fp);
  43.  
  44. /*    global variables    */
  45.  
  46. char    *fh_buf,
  47.     DataType[32], Date[16], Form[80];
  48. int    ByteShift=1,    /* default input format    (short)    */
  49.     oPBSize;    /* output Pix_Byte_Size */
  50. bool    DCMP=False;
  51. MType    F_Offset=0,    /* File descriptor offset    */
  52.     BUF_P=0;
  53. float    MinShort=0, MaxShort=65535;
  54. FITSType    ZLevel=0;
  55.  
  56.  
  57. convts*    GetItem(opt, buf)
  58. char    opt, *buf;
  59. {
  60. register char*    i;
  61. static    convts    cnvt;
  62.  
  63. i = (char *) index(buf, '=');
  64. while (*(++i) == ' ');
  65. switch(opt)
  66. {    case 'i':    cnvt.h = atoi(i);
  67.         break;
  68.     case 'e':
  69.     case 'f':    cnvt.f = atof(i);
  70.         break;
  71.     case 'c':
  72.         i = (char *) index(i, '/');
  73.         while (*--i == 0x20);
  74.         while (*--i != 0x20);
  75.         cnvt.c = *++i;
  76.         break;
  77.     case 's':
  78.         if (*i == '\'')    i++;
  79.         cnvt.s = i;
  80.         while (i < buf+80 && *i != '\'')
  81.             i++;
  82.         *i = 0;
  83. }
  84. return    &cnvt;    /* not a good ideal but work with most machines */
  85. }
  86.  
  87. /****************************************************************
  88. % subroutine get_fits_head - get FITS header from input file    %
  89. % The whole header is keeped in fh_buf. When get_fits_head is    %
  90. % called, this fh_buf will be free and the information is lost.    %
  91. ****************************************************************/
  92. get_fits_head(fhd, img, host, FTy)
  93. FITS_BASE    *fhd;
  94. U_IMAGE    *img;
  95. bool    FTy;
  96. {
  97. int    reloads=0, MASize=FITSBlock << (FTy=='u' && FORTRAN);
  98. short    blocks, blocksize;
  99. char    c;
  100. bool    ReLoad = True, EndOFile;
  101. register char*    ibuf;
  102.  
  103. if (BUF_P)
  104.     free(fh_buf),    BUF_P = DCMP = 0;
  105. else
  106.     fhd->date = Date,
  107.     fhd->datatype = DataType,
  108.     fhd->format = Form;
  109.  
  110. do {
  111.    if (ReLoad)    {    /* load header buffer    */
  112.     if (!BUF_P)
  113.         fh_buf = (char *) nzalloc(MASize, 1, "FITS_H");
  114.     else    fh_buf = (char *) realloc(fh_buf, MASize * (reloads+1));
  115.     ibuf = fh_buf + BUF_P;
  116.     if (FORTRAN)
  117.     {
  118.         blocksize = blocks = 0;
  119.         do    {
  120.         blocksize += read_var(fh_buf+BUF_P+blocksize,img->IN_FP,host,FTy);
  121.         blocks++;
  122.         }    while (blocksize < FITSBlock);
  123.         BUF_P += blocksize;
  124.         F_Offset += blocksize + blocks * FFCL;
  125.         if (FTy != 'v')
  126.             F_Offset += blocks * FFCL;
  127.     }
  128.     else    F_Offset = BUF_P += blocksize =
  129.             fread(fh_buf + BUF_P, 1, FITSBlock, img->IN_FP);
  130.     ReLoad = False;
  131.     EndOFile = feof(img->IN_FP);
  132.    }
  133.  
  134.     /* start heading processing    */
  135.    if (strncmp(ibuf, "SIMPLE", 6) == 0)
  136.     fhd->sample = GetItem('c', ibuf)->c;    /* maybe at different position    */
  137.  
  138.    else    if (strncmp(ibuf, "BITPIX", 6) == 0)
  139.     {
  140.         img->pxl_in =  (fhd->bits_pxl = GetItem('i', ibuf)->h) >> 3;
  141.         img->in_form = ByteShift = img->pxl_in >> 1; /* get sequence # */
  142.     }
  143.    else    if (strncmp(ibuf, "DATATYPE", 8) == 0)
  144.     {    strncpy(DataType, GetItem('s', ibuf)->s, 15);
  145.         c = 7;
  146.         if (strncmp(DataType, "INTEGER", c)==0)
  147.         {
  148.             while(!isdigit(DataType[c]))    c++;
  149.             switch(DataType[c])    /* use in type as out type */
  150.             {
  151.             case '1':
  152.                 img->pxl_out = sizeof(char);
  153.                 break;
  154.             case '2':
  155.                 img->pxl_out = sizeof(short);
  156.                 break;
  157.             case '4':
  158.                 img->pxl_out = sizeof(long);
  159.                 break;
  160. wdtf:            default:msg("undetermined format %d\n", DataType[c]);
  161.                 if (img->pxl_in == 4)    goto    ffmt;
  162.             }
  163.         }
  164.         else    if(strncmp(DataType, "FLOAT", 5)==0)
  165. ffmt:            img->pxl_out = sizeof(float);
  166.         else    goto    wdtf;
  167.         if (img->pxl_in != img->pxl_out)
  168.             msg("warning: unmatching data type\n");
  169.     }
  170.    else    if (strncmp(ibuf, "NAXIS ", 6) == 0)
  171.     fhd->naxis = GetItem('i', ibuf)->h;
  172.  
  173.    else    if (strncmp(ibuf, "NAXIS1 ", 7) ==  0)
  174.     img->width = fhd->naxis1 = GetItem('i', ibuf)->h;
  175.  
  176.    else    if (strncmp(ibuf, "NAXIS2 ", 7) == 0)
  177.     img->height = fhd->naxis2 = GetItem('i', ibuf)->h;
  178.  
  179.    else    if (strncmp(ibuf, "NAXIS3 ", 7) == 0)
  180.     img->frames = fhd->naxis3 = GetItem('i', ibuf)->h;
  181.  
  182.    else    if (strncmp(ibuf, "FORMAT", 6) == 0)
  183.     {    strcpy(Form, GetItem('s', ibuf)->s);
  184.         if (!strncmp(Form, "COMPRESSED", 10) || toupper(Form[0])=='C'){
  185.             DCMP = True;    /* no convert, goto decompress    */
  186.             if (!img->pxl_in)
  187.                 img->pxl_in = sizeof(float),
  188.                 img->in_form = IFMT_FLOAT;
  189.         }
  190.     }
  191.    else    if (strncmp(ibuf, "NAUX ", 5) == 0)
  192.     fhd->naux = GetItem('i', ibuf)->h;
  193.  
  194.    else    if (strncmp(ibuf, "DATE-", 5) == 0)
  195.     strncpy(Date, GetItem('s', ibuf)->s, 15);
  196.  
  197.    else    if (strncmp(ibuf, "SCALE", 5) == 0)
  198.     fhd->scale = GetItem('e', ibuf)->f;
  199.  
  200.    else    if (strncmp(ibuf, "BSCALE", 6) == 0)
  201.     fhd->bscale = GetItem('e', ibuf)->f;
  202.  
  203.    else    if (strncmp(ibuf, "BZERO", 5) == 0)
  204.     fhd->bzero = GetItem('e', ibuf)->f;
  205.  
  206.    else    if (strncmp(ibuf, "CRVAL1", 6) == 0)
  207.     fhd->crval1 = GetItem('e', ibuf)->f;
  208.  
  209.    else    if (strncmp(ibuf, "CRVAL2", 6) == 0)
  210.     fhd->crval2 = GetItem('e', ibuf)->f;
  211.  
  212.    else    if (strncmp(ibuf, "SEEING", 6) == 0)
  213.     fhd->seeing = GetItem('e', ibuf)->f;
  214.    else    /* not a FITS image */
  215.     if (!(isalpha(*ibuf) | isspace(*ibuf)))    return    -1;
  216. /*
  217.    else    {
  218.     for (c=0; c < 80; c++)
  219.         putc(*(ibuf+c), stderr);
  220.     mesg("\n");
  221.     }
  222. */
  223.    ibuf += 80;
  224.    c = strncmp(ibuf, "END", 3);
  225.    if (ibuf >= fh_buf + blocksize + reloads*MASize){
  226.     ReLoad = True;
  227.     reloads++;
  228.     }
  229. } while(c && !EndOFile);
  230.  
  231. if (!img->frames)    img->frames = 1;
  232.  
  233. if (F_Offset)    /*    point to data    */
  234.     fseek(img->IN_FP, F_Offset, SEEK_SET);
  235.  
  236. msg("end header (%xH)\n", F_Offset);
  237.  
  238. return    !F_Offset || EndOFile;
  239. }
  240.  
  241.  
  242.  
  243. /*    return    number of lines write to img->OUT_FP    */
  244.  
  245. put_fits_head(fhd, img, complete)
  246. FITS_BASE    *fhd;
  247. U_IMAGE    *img;
  248. bool    complete;
  249. {
  250. int    i = 0;
  251. char    line[81];
  252.  
  253. sprintf(line, "SIMPLE  =                    %c                                                  ",
  254.     fhd->sample);
  255. write_line(line, img->OUT_FP);    ++i;
  256. sprintf(line, "BITPIX  =           %10d                                                  ",
  257.     fhd->bits_pxl);
  258. write_line(line, img->OUT_FP);    ++i;
  259. sprintf(line, "NAXIS   =           %10d                                                  ",
  260.     fhd->naxis);
  261. write_line(line, img->OUT_FP);    ++i;
  262. sprintf(line, "NAXIS1  =           %10d                                                  ",
  263.     fhd->naxis1);
  264. write_line(line, img->OUT_FP);    ++i;
  265. sprintf(line, "NAXIS2  =           %10d                                                  ",
  266.     fhd->naxis2);
  267. write_line(line, img->OUT_FP);    ++i;
  268. sprintf(line, "NAUX    =           %10d                                                  ",
  269.     fhd->naux);
  270. write_line(line, img->OUT_FP);    ++i;
  271. sprintf(line, "DATAMIN =           %10d                                                  ", 0);
  272. write_line(line, img->OUT_FP);    ++i;
  273. sprintf(line, "DATAMAX =           %10d                                                  ",
  274.     fhd->maxval);
  275. write_line(line, img->OUT_FP);    ++i;
  276. sprintf(line, "BZERO   =           %10f                                                  ",
  277.     fhd->bzero);
  278. write_line(line, img->OUT_FP);    ++i;
  279. sprintf(line, "BSCALE  =           %10f                                                  ",
  280.     fhd->bscale?fhd->bscale:1.0);
  281. write_line(line, img->OUT_FP);    ++i;
  282. sprintf(line, "SCALE   =           %10f                                                  ",
  283.     fhd->scale?fhd->scale:1.0);
  284. write_line(line, img->OUT_FP);    ++i;
  285. sprintf(line, "HISTORY Created by fitstopgm.                                                   ");
  286. write_line(line, img->OUT_FP);    ++i;
  287. if (complete){
  288.     sprintf(line, "END                                                                             ");
  289.     write_line(line, img->OUT_FP);    ++i;
  290.     sprintf(line, "                                                                                ");
  291.     while (i < 36)
  292.         write_line(line, img->OUT_FP);    ++i;
  293. }
  294. return    i;
  295. }
  296.  
  297. /*
  298. write_line(str, fp)
  299. char    *str;
  300. FILE    *fp;
  301. {
  302.     if (fwrite(str, sizeof(char), 80, fp) != 80)
  303.         syserr("write line");
  304. }
  305. */
  306.