eddy_em: (Default)
[personal profile] eddy_em
Сижу сейчас с больной головой (по поселку ходит ГРИПП, меня тоже зацепило) и пытаюсь написать что-нибудь для попиксельной обработки FITS-файлов (плоские поля), которые мы наполучали, начиная с вчерашнего обеда, с Apogee'вского светоприемника.
Обнаружил, что для чтения шапок FITS-файлов в Octave ничего нет. Нашел нечто похожее для матлаба и немного видоизменил.


function [keyword,value,comment] = parse_card(s)
%Parses a FITS header card. 
%Reference:
%                NASA/Science Office of Standards and Technology
%           "Definition of the Flexible Image Transport System (FITS)"
%                        NOST 100-1.0    June 19, 1993

%Set defaults
keyword = [];value = [];comment = [];

% check card length
cardlength = size(s,2);
if(cardlength < 8)
	keyword = s;
	return;
endif

%Get keyword in bytes 1 - 8
keyword = deblank(s(1:8));
if (nargout == 1) % return if user don't need other fields
   return;
endif

%If keyword is blank then the line is a comment
if (strmatch(keyword,'       '))
    keyword = [];
	value = [];
	if(cardlength > 10)
		comment = deblank(s(11:end));
	endif
	return;
endif

if(cardlength < 11)
	return
endif

%Keyword is non-blank. Check if there is a corresponding value/comment.
%If not then the only possibilities are that bytes 11:80 are a comment
%or that they are blank
if (~strmatch(s(9:10),'= '))
	value = [];
	comment = deblank(s(11:end));
	return;
endif

%Card is a standard keyword/value/comment structure. Break the value/comment
%string (bytes 11 - 80) into separate strings by tokenizing on "/" character.
%Remove the leading and trailing blanks on the value and the trailing blanks
%on the comment.

[value,comment] = strtok(s(11:end),'/');
comment = deblank(comment(2:end)); % remove '/' from comment
value = fliplr(deblank(fliplr(deblank(value))));

%Now figure out whether to output the value as a string or as a number.
%The FITS standard requires all values that are strings to be in single
%quotes like this: 'foo bar'. However, logical variables can take the
%values T or F without having any single quotes, so we have to look
%out for those also.

%Test for logical. Return logical as a number.
if (strmatch(upper(value),'T'))
	value = 1;
	return;
elseif (strmatch(upper(value),'F'))
	value = 0;
	return;
endif

% Test for number
numvalue = str2num(value);
if (~isempty(numvalue)) % this is number
	value = numvalue;
else % this is a string - remove apostrophes
	if(value(1) == "'")
		value(1) = [];
	endif
	if(value(end) == "'")
		value(end) = [];
	endif
endif
endfunction



October 2025

S M T W T F S
   1234
567 89 1011
121314 15161718
19202122232425
2627 28293031 

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 24th, 2026 05:09 pm
Powered by Dreamwidth Studios