function All = bladedPtRead(fullFile, varargin)
% bladedPtRead reads % file
%   bladedPtRead reads the .%XX file informations
% fullFile: file name including path and extension
% varargin:
% 1, file path
% % ... 'loadselection', m, n, <- m -- iVariab, n -- iAxival
% output:
%  loadselection: [max(abs), min(abs)]

%   Project: IMPACT
%   Title of Code: bladedPtRead
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.

try
    fid = fopen(fullFile);
    if fid == -1
        errordlgMLS(sprintf('Cannot open file %s. ',fullFile));
        All = [];
        return
    end
    All = bladedPtRead_true(fid, varargin{:});
    fclose(fid);
catch
    fclose(fid);
    errordlgMLS(sprintf('An error occurred when reading %s. ', fullFile));
    All = [];
    return
end

function All = bladedPtRead_true(fid, varargin)

% file
tline = fgetl(fid);
file = sscanf(tline, '%*s %s');
All.file = file;

% access
tline = fgetl(fid);
access = sscanf(tline, '%*s %s');
All.access = access;

% form
tline = fgetl(fid);
form = sscanf(tline, '%*s %s');
All.form = form;

% recl
tline = fgetl(fid);
recl = sscanf(tline, '%*s %d');
All.recl = recl;

% format
tline = fgetl(fid);
format = sscanf(tline, '%*s %s');
All.format = format;

% content
tline = fgetl(fid);
content = sscanf(tline, '%*s %s');
All.content = content;

% config
tline = fgetl(fid);
config = sscanf(tline, '%*s %s');
All.config = config;

% ndimens
tline = fgetl(fid);
ndimens = sscanf(tline, '%*s %f');
All.ndimens = ndimens;

% dimens
tline = fgetl(fid);
dimens = sscanf(tline, '%*s %f %f %f', [1, inf]);
All.dimens = dimens;

% genlab
tline = fgetl(fid);
genlabTemp = sscanf(tline, '%*s %[^\n]');
genlab = regexprep(genlabTemp,'''','');
All.genlab = genlab;

% variab
tline = fgetl(fid);

expr = '\<''[^'']*''\>|\S+';
variabTemp1 = regexp(tline, expr, 'match');
All.variabFull = variabTemp1(2:end); % this is the full name of the variable

% change to capital
variabTemp2 = regexprep(variabTemp1, '\s(\w)|\W(\w)', '${upper($1)}');
% delete all non-words
variab = regexprep(variabTemp2, '\W', '');

%%
variab(1) = [];
All.variab = variab;

% varunit
tline = fgetl(fid);

varunit = strread(tline, '%s');
All.varunit = varunit(2:end)';

if ndimens == 3
    % axislab2
    tline = fgetl(fid);
    axislab2 = sscanf(tline, '%*s %[^\n]');
    All.axislab2 = axislab2;
    % axiunit2
    tline = fgetl(fid);
    axiunit2 = sscanf(tline, '%*s %[^\n]');
    All.axiunit2 = axiunit2;
    % aximeth2
    tline = fgetl(fid);
    aximeth2 = sscanf(tline, '%*s %d');
    All.aximeth2 = aximeth2;
    % axival
    tline = fgetl(fid);
    nAxival = dimens(2);
    str = ['%*s', repmat('%f', 1, nAxival)];
    axival = strread(tline, '%s', 'delimiter', '''');
    if strncmpi(axival{1}, 'axitick', 7) % tidal bladed
        axival = axival(2:2:end);
    else
        axival = sscanf(tline, str, [1, inf]);
    end
    All.axival = axival;
end

% axislab
tline = fgetl(fid);
if strfind(tline, 'VARSCALE');
    All.varscale = sscanf(tline, '%*s %[^\n]');
    tline = fgetl(fid);
end
    
axislab = sscanf(tline, '%*s %[^\n]');
All.axislab = axislab;


% axiunit
tline = fgetl(fid);
axiunit = sscanf(tline, '%*s %[^\n]');
All.axiunit = axiunit;

% aximeth
tline = fgetl(fid);
aximeth = sscanf(tline, '%*s %d');
All.aximeth = aximeth;

% min
tline = fgetl(fid);
minVal = sscanf(tline, '%*s %f');
All.min = minVal;

% step
tline = fgetl(fid);
step = sscanf(tline, '%*s %f');
All.step = step;


