Project 1 - Parsing NASA thermodynamic data
clear all
close all
clc
% A project report on parsing NASA's thermodyanamic data
f3 = fopen('THERMO.dat','r'); %.......the command "fopen()" is used to open the file
% by typing file name in inverted comma
% in parenthesis after fopen, 'r' stands
% for permission for read the file
% content. this result is stored in
% variable 'f3' which worked as file id
% this id is used to access the file
% content at the very first time.
for i = 1:5 %........the command 'fgetl()' is used to get the first
skiplines = fgetl(f3); % data line from the file. at each exicution,
end % command prints new line sequentially. the main
% data starts from line no. 6.
% therefore we skiped first five lines
% by running the code five times in
% forloop.
% the following forloop consists the code which extracts all 14 coeff of a single specie,
% also calculates specific heat, enthalpy & entropy for low & high temp ranges, plots
% all the results as well & prints the molecular mass of the specie. since we have 53
% different species in total hence this coding algorithm runs 53 times in forloop to get
% the results for all 53 species from 'THERMO.dat' file.
for i = 1:53
l1 = fgetl(f3); %.......using 'fgetl' command with file id 'f3'
l2 = fgetl(f3); % four times, we got the foure lines that inclues
l3 = fgetl(f3); % specie name, local temperature ranges and 14
l4 = fgetl(f3); % coefficients in sting format.
sample = strsplit(l1,' '); %........the command 'strspplit' is used to split the string.
% the command's parethesis consists string name first
% and after it ask for delimiter which is a single
% space in here. all the space seperated numbers &
% characters will be stored in seperat cell arrey.
sp_name = sample{1}; %.........declearing the specie name which is in first cell of
% sample. due to cell arrey we are
% using curly braces to acces content.
temp = l1(49:end); %.........in '.dat' file, the local temperature range starts
temp = strsplit(temp,' '); % from 49th place of line l1 & ends at the end of the
t_low = str2num(temp{1}); % line hence the numbers beween 49 to end decleared as
t_high = str2num(temp{2}); % temp. then splitting it in seperate cell & declearing
t_mid = str2num(temp{3}); % those as low, mid & high temp. using 'str2num' command
% these numbers are converted from string to numbers.
% low temp range
tl = linspace(t_low,t_mid,500); %......creating the arreys for low to mid & mid to high
% high temp range % temperatures using linspace with 500
th = linspace(t_mid,t_high,500); % values.
c2 = findstr(l2,'E'); %.......'findstr' command is used to find the specific
c3 = findstr(l3,'E'); % content in the string and it's position. this will
c4 = findstr(l4,'E'); % help to extract each coefficient from line(string) l2,l3,l4.
% here 'E' is the common element at same position in
% each coeff. c2, c3, c4 will be the arreys of
% indexing numbers of E for each line.
% using this indexing position we will
% seperate each coeff.
% extraction of first five coeff from line l2:-
a1 = l2(1:c2(1)+3); %................l2 line is a string in which 5 coeff are present. In l2, first E
a1 = str2num(a1); % is at 12th position. c2(1) is 12th element which is E. after E
a2 = str2num(l2(c2(1)+4:c2(2)+3)); % there are 3 bits in which one is mathematical sign & two are
a3 = str2num(l2(c2(2)+4:c2(3)+3)); % numbers(exponents). so 'a1' contains numbers from position 1st
a4 = str2num(l2(c2(3)+4:c2(4)+3)); % to 12th(E) & in addition three exponent values. this is the 1st
a5 = str2num(l2(c2(4)+4:c2(5)+3)); % coeff 'a1'. in next line a1 is converted from string to number
% line l3:- % using 'str2num' command.
a6 = str2num(l3(1:c2(1)+3));
a7 = str2num(l3(c2(1)+4:c2(2)+3)); % Next coeff 'a2' starts right after 'a1'. this position is located
a8 = str2num(l3(c2(2)+4:c2(3)+3)); % after c2(1) ie. at 12th(E) plus 4 bits. then 'a2' ends at c2(2)
a9 = str2num(l3(c2(3)+4:c2(4)+3)); % means 2nd E which is at 27th position plus 3 bits which are exponents.
a10= str2num(l3(c2(4)+4:c2(5)+3)); % so numbers within these positions is 2nd coeff 'a2'. in same code its
% line l4:- % converted from string to number.
a11 = str2num(l4(1:c2(1)+3)); % accept 1st coeff in each line, every coeff starts where previous
a12 = str2num(l4(c2(1)+4:c2(2)+3)); % coeff ends. By identifying the starting and ending positions, all
a13 = str2num(l4(c2(2)+4:c2(3)+3)); % 14 coeff could get extracted.
a14 = str2num(l4(c2(3)+4:c2(4)+3));
% universal gas constant R:-
R = 8.314;
% calculations & mathematical expression for specific heat,enthalpy & entropy
% specific heat
cp_h = (a1 + a2.*th + a3.*th.^2 + a4.*th.^3 + a5.*th.^4)*R; % for high temp range
cp_l = (a8 + a9.*tl + a10.*tl.^2 + a11.*tl.^3 + a12.*tl.^4)*R;% for low temp range
% enthalpy
H_h = (a1 + a2.*th/2 + a3.*th.^2/3 + a4.*th.^3/4 + a5.*th.^4/5 + a6./th)*R.*th; % for high temp range
H_l = (a8 + a9.*tl/2 + a10.*tl.^2/3 + a11.*tl.^3/4 + a12.*tl.^4/5 + a13./tl)*R.*tl;% for low temp range
% entropy
s_h = (a1.*log(th) + a2.*th + (a3.*th.^2/2) + (a4.*th.^3/3) + (a5.*th.^4/4) + a7)*R;
% for high temp range
s_l = (a8.*log(tl) + a9.*tl + (a10.*tl.^2/2) + (a11.*tl.^3/3) + (a12.*tl.^4/4) + a14)*R;
% for low temp range
%...[note: in expression, dot operator is used before variable which is an arrey.]
mkdir('E:\matlab\file_parsing',sp_name) %............using 'mkdir' command, new path & folder can be
% create to save the plots. all 3 plots for cp,s,h
% will be save in seperate folder with respective
% specie name.
cd(sp_name) %.........'cd' command is used to display current folder
% cp plot
figure(1) %.........the first plot results will stored in fig(1)
plot(tl,cp_l,'linewidth',2) %.........plotting results for low temp vs cp low
hold on %.........holding the same fig window to plot next result on it
plot(th,cp_h,'linewidth',2) %.........plotting results for high temp vs cp high
title(sp_name) %.........using 'title()' command giving title to the plot
legend('low temperature','high temperature')%.........using 'legend() we can label the curves with color codes
xlabel('temperature') %.........labeling the x axis as 'temperature'
ylabel('specific heat') %.........labeling the x axis as 'specific heat'
hold off %.........using 'hold off', figure(1) will done with plotting
% no further changes will occure in it.
saveas(figure(1),'cp.png') %.........saving figure(1) with the name & format as cp.png
% H plot
figure(2) %....the polt results for H will stored in fig(2)
plot(tl,H_l,'linewidth',2)
hold on %....holding the same fig window to plot next result on it
plot(th,H_h,'linewidth',2)
title(sp_name)
legend('low temperature','high temperature')
xlabel('temperature')
ylabel('enthalpy')
hold off %.........using 'hold off', figure(2) will done with plotting.
% no further changes will occure in it.
saveas(figure(2),'h.png')
% s plot
figure(3)
plot(tl,s_l,'linewidth',2)
hold on
plot(th,s_h,'linewidth',2)
title(sp_name)
legend('low temperature','high temperature')
xlabel('temperature')
ylabel('entropy')
hold off
saveas(figure(3),'s.png')
cd .. %.....since plots are saving in current folder(ie. sp_name)
% the command 'cd..' will go in previous folder at the
% end of the code and same process will repeate for
% next specie's data.
specie = sp_name; %.....declearing sp_name as specie.
mass = mol_mass(sp_name); %.....'mol_mass' is a seperate function for molecular
% calculations of all species.
disp(specie+"="+mass) %.....using 'disp()' command showing specie name equal
% to its mass.
end
% the following function template is for molecular mass calculations of
% the different species.
function mm=mol_mass(sp_name) %...after function keyword |mm| is the output of the function
%|mol_mass| is name of the function & parenthesis next to
%it consists an input.
atoms =['H','C','O','N','A','S']; %...creating a string arrey of letters of specie's chemical formula
at_wt=[1,12,16,14,40,32]; %...creating an arrey of corrosponding molecular mass of the species
mm=0; %...defining mm=0 initailly.
for i=1:length(sp_name) %....in forloop i & j, the command 'strcmp()'is going to run.'
for j=1:length(atoms) %'strcmp' compares the elements present in two strings.
if strcmp(sp_name(i),atoms(j)) %these two loops has no. of iterations equivivalent to the
mm=mm+at_wt(j); %no of letters present in strings. means each letter of
pos=j; %string1(sp_name) going to compare with each letter of string2
end %(atoms). when this satisfies the comparison, |if| condition will
end %print the mm as corrosponding mass value of the specie by adding
%it in 0 cause mm has 0 value initailly. this is only for single
%letter comparison, but when there is an input specie name with
%multiple letters, then loop makes comparison for first letter &
%stores its mass value in mm. then it compares next letter. after
%positive result, it adds present letter's mass value in previous
%letter's mass value. this proces is repeat till the last letter.
%assigning the variable 'pos' to store mass value of recent letter.
n=str2num(sp_name(i)); %when input specie name comes with numeric value then 'str2num'
if n>1 %convert that number from string to number & stores that in 'n'
mm=mm+at_wt(pos)*(n-1); %when if statement found n>1, it performs a mathematical rxpression.
end %(eg.H2)the mass value of letter(H) just before the number is stored in
end %mm & in pos as well & n=2 then by performing mathematical expression by
end %taking these values, matlab calculates final mass value for the specie.
error explanation:
error 1 was about improper use of dot operator while using log. dot operator was between the log and the
variable which was incorrect practice. after typing it correctly ie. [*.log(th)] the error got terminate.