function pl2dmm = readpl2dmm(varargin)

% Function to read a pl2d file created using jl09 script file
%
% Format:
%  "pl2dmm = readpl2dmm;"
%  "pl2dmm = readpl2dmm('pl2dmm_file');"
%

if nargin == 0
    titlestr = 'Open jl09 pl2dmm file';
    filetypes = {'*.pl2dmm';'jl09.*';'*.*';'All files'};
    [filename,pathname] = uigetfile(filetypes,titlestr);
    if filename == 0
        error('No jl09 pl2dmm file selected.  Stopping program.')
    end
    filename = fullfile(pathname,filename);
elseif nargin == 1
    filename = varargin{1};
else
    error('Too many input arguments.  Stopping program.')
end

%opens selected file
fid = fopen(filename);

%reads header line
line = fgetl(fid);
store = sscanf(line,'%s');
pl2dmm.title = store;

%read data size's
pl2dmm.idum = fscanf(fid,'%d',1);
pl2dmm.jm   = fscanf(fid,'%d',1);
pl2dmm.km   = fscanf(fid,'%d',1);
pl2dmm.itype= fscanf(fid,'%s',1);

%read specific heat and ratio of specific 
%at each streamwise station
pl2dmm.cpd  = fscanf(fid,'%f',pl2dmm.jm);
pl2dmm.gad  = fscanf(fid,'%f',pl2dmm.jm);

%read station id's and ave method
for i = 1:pl2dmm.jm
    pl2dmm.jident(i,:) = '      ';
    jident = fscanf(fid,'%s',1);
    pl2dmm.jident(i,1:size(jident,2)) = jident;
end

for i = 1:pl2dmm.jm
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.JMETH = fscanf(fid,''%s'',1);'])
end

%next three for loops for annulus blockage x and r
for i = 1:pl2dmm.jm
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.BLK = fscanf(fid,''%f'',1);'])
end

for j = 1:pl2dmm.km
    for i = 1:pl2dmm.jm
        eval(['pl2dmm.' pl2dmm.jident(i,:) '.X(j) = fscanf(fid,''%f'',1);'])
  %      pl2dmm.jident(i,:), eval(['pl2dmm.' pl2dmm.jident(i,:) '.X(j)'])
    end
    for i = 1:pl2dmm.jm
        eval(['pl2dmm.' pl2dmm.jident(i,:) '.R(j) = fscanf(fid,''%f'',1);'])
    end
end

%read rotational speed (rad/sec) and skip a lines
for i = 1:pl2dmm.jm
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.W = fscanf(fid,''%f'',1);'])
end

dummy = fscanf(fid,'%s',4);

%read pitchwise averaged axial velocity
dummy = fscanf(fid,'%s',4);
for j = 1:pl2dmm.km
    for i = 1:pl2dmm.jm
        eval(['pl2dmm.' pl2dmm.jident(i,:) '.VX(j) = fscanf(fid,''%f'',1);'])
    end
end

%read tangential velocity
dummy = fscanf(fid,'%s',4);
for j = 1:pl2dmm.km
    for i = 1:pl2dmm.jm
        eval(['pl2dmm.' pl2dmm.jident(i,:) '.VT(j) = fscanf(fid,''%f'',1);'])
    end
end

%read radial velocity
dummy = fscanf(fid,'%s',4);
for j = 1:pl2dmm.km
    for i = 1:pl2dmm.jm
        eval(['pl2dmm.' pl2dmm.jident(i,:) '.VR(j) = fscanf(fid,''%f'',1);'])
    end
end

%read mach number
dummy = fscanf(fid,'%s',4);
for j = 1:pl2dmm.km
    for i = 1:pl2dmm.jm
        eval(['pl2dmm.' pl2dmm.jident(i,:) '.MACH(j) = fscanf(fid,''%f'',1);'])
    end
end

%read static pressure
dummy = fscanf(fid,'%s',4);
for j = 1:pl2dmm.km
    for i = 1:pl2dmm.jm
        eval(['pl2dmm.' pl2dmm.jident(i,:) '.P(j) = fscanf(fid,''%f'',1);'])
    end
end

%read density
dummy = fscanf(fid,'%s',3);
for j = 1:pl2dmm.km
    for i = 1:pl2dmm.jm
        eval(['pl2dmm.' pl2dmm.jident(i,:) '.RO(j) = fscanf(fid,''%f'',1);'])
    end
end

%read strut blockage
dummy = fscanf(fid,'%s',2);
for j = 1:pl2dmm.km
    for i = 1:pl2dmm.jm
        eval(['pl2dmm.' pl2dmm.jident(i,:) '.SBLK(j) = fscanf(fid,''%f'',1);'])
    end
end

%read turbulent viscosity ratio
dummy = fscanf(fid,'%s',3);
for j = 1:pl2dmm.km
    for i = 1:pl2dmm.jm
        eval(['pl2dmm.' pl2dmm.jident(i,:) '.TVR(j) = fscanf(fid,''%f'',1);'])
    end
end


%closes blade definiton file
fclose(fid);


for i = 1:pl2dmm.jm
    
    %calc height
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.HT = (pl2dmm.' pl2dmm.jident(i,:) '.R - min(pl2dmm.' pl2dmm.jident(i,:) '.R))./(max(pl2dmm.' pl2dmm.jident(i,:) '.R) - min(pl2dmm.' pl2dmm.jident(i,:) '.R));'])
    
    %calc Total Pressure
    eval(['fmach = 1+(0.5 * (pl2dmm.gad(i) - 1) * pl2dmm.' pl2dmm.jident(i,:) '.MACH.^2);'])
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.P0REL = pl2dmm.' pl2dmm.jident(i,:) '.P.* (fmach.^( pl2dmm.gad(i)/(pl2dmm.gad(i)-1)));'])
    
    %calc Abs Total Pressure
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.T0REL = fmach .* (pl2dmm.' pl2dmm.jident(i,:) '.P ./(pl2dmm.' pl2dmm.jident(i,:) '.RO * 287));'])
    eval(['U = pl2dmm.' pl2dmm.jident(i,:) '.W * pl2dmm.' pl2dmm.jident(i,:) '.R;'])
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.T0 = pl2dmm.' pl2dmm.jident(i,:) '.T0REL + (U .* pl2dmm.' pl2dmm.jident(i,:) '.VT + 0.5*U.^2)/pl2dmm.cpd(i);'])
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.P0 = pl2dmm.' pl2dmm.jident(i,:) '.P .*(fmach .* (pl2dmm.' pl2dmm.jident(i,:) '.T0 ./ pl2dmm.' pl2dmm.jident(i,:) '.T0REL)).^(pl2dmm.gad(i)/(pl2dmm.gad(i)-1));'])
    
    %calc angles
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.PITCH = atan2(pl2dmm.' pl2dmm.jident(i,:) '.VR,pl2dmm.' pl2dmm.jident(i,:) '.VX) * 180/pi;'])
    eval(['VMER = sqrt(pl2dmm.' pl2dmm.jident(i,:) '.VX.^2 + pl2dmm.' pl2dmm.jident(i,:) '.VR.^2);'])
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.BETA = atan2(pl2dmm.' pl2dmm.jident(i,:) '.VT,VMER) * 180/pi;']) 
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.VTAB = pl2dmm.' pl2dmm.jident(i,:) '.VT + U;'])
    eval(['pl2dmm.' pl2dmm.jident(i,:) '.ALPHA = atan2(pl2dmm.' pl2dmm.jident(i,:) '.VTAB,VMER) * 180/pi;'])
end

%ends function 
return