function [ pulsarparams ] = readparfile( parfile )

% [ pulsarparams ] = readparfile( parfile )
%
% This function takes in a pulsar TEMPO-style .par file and outputs the 
% parameters in the pulsarparams structure

% check that the file exists
if exist(parfile, 'file') == 0
    disp('Error parameter file does not exist');
    pulsarparams = 0;
    return;
end

% open par file for reading
fp = fopen(parfile, 'r');

% read names of parameters in the par file (first column of file)
parnames = textscan(fp, '%s%*[^\n]', 'CommentStyle', '%');
    
% check that some parameters were read in
if isempty(parnames{:})
    disp('No parameter were in the file!');
    pulsarparams = 0;
    return;
else
    pulsarparams.nparams = length(parnames{:});
end

% rewind the file pointer
frewind(fp);

% for each parameter scan though the file and read in the values associtaed 
% with it
for i=1:pulsarparams.nparams
    while 1
        % read in one value at a time
        value = textscan(fp, '%s', 1, 'CommentStyle', '%');

        if strcmp(char(parnames{1}(i)), char(value{1}(1)))
            break;
        end
        clear value;
    end
    
    % read in data from that line into a string
    tline = fgetl(fp);
    
    % there shouldn't be any cases where a parameter has no value
    % associated with it, but below is a check just in case - if there is 
    % no value we will skip that parameter
    if isempty(tline)
        disp(strcat('Parameter ', parnames{1}(i), ...
            ' has no value, so skip it'));
        i = i-1;
        pulsarparams.nparams = pulsarparams.nparams - 1;
        frewind(fp);
        clear tline;
        continue;
    end
    
    % set parameter name and convert to upper case
    pulsarparams.parname{i} = upper(parnames{1}(i));
    
    % do specific things for pulsar name lines and RA and DEC (i.e. read
    % the first value in as a string)
    if strcmp(char(pulsarparams.parname{i}), 'PSR') || ...
            strcmp(char(pulsarparams.parname{i}), 'PSRJ') || ...
            strcmp(char(pulsarparams.parname{i}), 'PSRB') || ...
            strcmp(char(pulsarparams.parname{i}), 'NAME') || ...
            strcmp(char(pulsarparams.parname{i}), 'RA') || ...
            strcmp(char(pulsarparams.parname{i}), 'RAJ') || ...
            strcmp(char(pulsarparams.parname{i}), 'DEC') || ...
            strcmp(char(pulsarparams.parname{i}), 'DECJ')
        nums = textscan(tline, '%s %f %f', 'ExpChars', 'eEdD');        
    else
        % scan line to extract any numbers
        nums = textscan(tline, '%f %f %f', 'ExpChars', 'eEdD');
        
        % check if the line contained any numerical data - if not it must
        % contain a string
        if isempty(nums{1})
            % check if the line in fact is a string
            str = textscan(tline, '%s');
        
            % set parameter value
            pulsarparams.parval{i} = str{1};
            pulsarparams.parerr{i} = []; % set as empty
            frewind(fp);
            clear str tline;
            continue;
        end
    end
    
    % set parameter value
    pulsarparams.parval{i} = nums{1};
    
    % check whether the line contains an error value  
    % if second value is 1 that means the third values is the error
    if ~isempty(nums{2}) && ~isempty(nums{3})
        if nums{2} == 1
            pulsarparams.parerr{i} = nums{3};
        else
            pulsarparams.parerr{i} = []; % set as empty
    
        end
    else
        pulsarparams.parerr{i} = []; % set as empty
    end
    
    
    % rewind file pointer
    frewind(fp);
    
    clear tline;
end

fclose(fp);
