Home > pointing > fitsheader.m

fitsheader

PURPOSE ^

FITSHEADER returns header information from a FITS file.

SYNOPSIS ^

function out=fitsheader(filename,verbose)

DESCRIPTION ^

FITSHEADER returns header information from a FITS file.

  FITSHEADER determines the value of a keyword or the number 
  of 36 card header blocks in a FITS file. As a side effect 
  the function can display all of the header information in the file 
  onto the screen.

  n_hdu=fitsheader(filename)
  value=fitsheader(filname,'keyword')
  n_hdu=fitsheader(filename,'silent_mode')
 
  The first form displays all header information on the
  screen and returns the number of 36 card header blocks in the file.

  The second form displays only the value corresponding to
  a specified keyword.
 
  The third form does not display any header information, but
  returns the number of 36 card header blocks in the file.

  Useful FITS Definitions:

  "card" = 80 byte line of ASCII data in a file. This 
           contains keyword/value pairs and/or comments.
           The FITS standard requires that cards occur
           36 at a time. Blank lines are used to fill
           in space if required.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function out=fitsheader(filename,verbose)
0002 %FITSHEADER returns header information from a FITS file.
0003 %
0004 %  FITSHEADER determines the value of a keyword or the number
0005 %  of 36 card header blocks in a FITS file. As a side effect
0006 %  the function can display all of the header information in the file
0007 %  onto the screen.
0008 %
0009 %  n_hdu=fitsheader(filename)
0010 %  value=fitsheader(filname,'keyword')
0011 %  n_hdu=fitsheader(filename,'silent_mode')
0012 %
0013 %  The first form displays all header information on the
0014 %  screen and returns the number of 36 card header blocks in the file.
0015 %
0016 %  The second form displays only the value corresponding to
0017 %  a specified keyword.
0018 %
0019 %  The third form does not display any header information, but
0020 %  returns the number of 36 card header blocks in the file.
0021 %
0022 %  Useful FITS Definitions:
0023 %
0024 %  "card" = 80 byte line of ASCII data in a file. This
0025 %           contains keyword/value pairs and/or comments.
0026 %           The FITS standard requires that cards occur
0027 %           36 at a time. Blank lines are used to fill
0028 %           in space if required.
0029 
0030 % Version 1.0
0031 % R. Abraham, Institute of Astronomy, Cambridge University
0032 %
0033 % For FITS Info, check out the following document:
0034 %
0035 %      NASA/Science Office of Standards and Technology
0036 %      Definition of the Flexible Image Transport System"
0037 %      NOST 100-1.0
0038 %
0039 %      This and other FITS documents are available on-line at:
0040 %      http://www.gsfc.nasa.gov/astro/fits/basics_info.html
0041 
0042 if nargin<2
0043     verbose='FULL';
0044 else
0045     verbose=upper(verbose);
0046 end;
0047 
0048 %Open the file
0049 fid=-1;
0050 if ~isstr(filename)
0051     filename=setstr(filename);
0052 end;
0053 if (isempty(findstr(filename,'.'))==1)
0054     filename=[filename,'.fits'];
0055 end
0056 [file,message] = fopen(filename,'r','l');
0057 if file == -1
0058     error(message);
0059 end
0060 
0061 
0062 %Read the header information
0063 n_card=0;
0064 keyword=[];
0065 while(~strcmp(upper(keyword),'END'))
0066     n_card=n_card+1;
0067     card=setstr(fread(file,80,'uchar')');
0068     [keyword,value,comment]=parse_card(card);
0069     if strcmp(verbose,'FULL')
0070         disp(card)
0071     elseif strcmp(keyword,verbose)
0072         out=value;
0073         return
0074     end
0075 end;
0076 
0077 %Clean up and output data
0078 fclose(file);
0079 
0080 if (strcmp(verbose,'FULL') | strcmp(verbose,'SILENT_MODE'))
0081     out=ceil(n_card/36); %Return number of HDUs
0082 else
0083     disp('Keyword not found.');
0084     out=[];
0085 end
0086 
0087 
0088 function [keyword,value,comment] = parse_card(s)
0089 %Parses a FITS header card.
0090 %Reference:
0091 %                NASA/Science Office of Standards and Technology
0092 %           "Definition of the Flexible Image Transport System (FITS)"
0093 %                        NOST 100-1.0    June 19, 1993
0094       
0095 %Set defaults
0096 keyword=[];value=[];comment=[];
0097 
0098 %Get keyword in bytes 1 - 8
0099 keyword=s(1:8);
0100 if nargout==1
0101    return;
0102 end
0103 
0104 %If keyword is blank then the line is a comment
0105 if strmatch(keyword,'       ')
0106     keyword=[];
0107     value=[];
0108     comment=deblank(s(11:80));
0109     return;
0110 end;
0111 
0112 
0113 %Keyword is non-blank. Check if there is a corresponding value/comment.
0114 %If not then the only possibilities are that bytes 11:80 are a comment
0115 %or that they are blank
0116 if ~strmatch(s(9:10),'= ')
0117     keyword=deblank(keyword);
0118     value=[];
0119     comment=deblank(s(11:80));
0120     return;
0121 end;
0122 
0123 %Card is a standard keyword/value/comment structure. Break the value/comment
0124 %string (bytes 11 - 80) into separate strings by tokenizing on "/" character.
0125 %Remove the leading and trailing blanks on the value and the trailing blanks
0126 %on the comment.
0127 
0128 keyword=deblank(keyword);
0129 [value,comment]=strtok(s(11:80),'/');
0130 comment=deblank(comment);
0131 value=fliplr(deblank(fliplr(deblank(value))));
0132 
0133 %Now figure out whether to output the value as a string or as a number.
0134 %The FITS standard requires all values that are strings to be in single
0135 %quotes like this: 'foo bar', so I can simply look for occurences of a
0136 %single quote to flag a string. However, logical variables can take the
0137 %values T or F without having any single quotes, so I'll have to look
0138 %out for those also.
0139 
0140 %Test for logical. Return logical as a string.
0141 if strmatch(upper(value),'T') | strmatch(upper(value),'F')
0142     return;
0143 end;
0144 
0145 %Test for string. Return string unconverted.
0146 if length(findstr('''',value)) ~= 0
0147     return;
0148 end;
0149 
0150 %Only thing left is a number. Convert string to number.
0151 value=str2num(value);

Generated on Sun 14-Jun-2015 17:12:45 by m2html © 2005