Project 1 - Parsing NASA thermodynamic data
Code for Parsing NASA thermodynamic data file (THERMO.dat) :-
clear all
close all
clc
%opening data file
f = fopen ('THERMO.dat','r');
fgetl(f);
%reading header infromations
tline = fgetl(f);
A= str2num(tline);
globle_low_temp = A(:,1);
global_mid_temp = A(:,2);
global_high_temp = A(:,3);
%skipping comment lines
for i = 1:3
fgetl(f);
end
%reading species data
for i = 1:53
linel = fgetl(f);
l= strsplit(linel);
m=length(l);
sp = l(1);
species = char (sp);
atom = ['H','C','O','N','Ar','S'];
atomic_weight = [1.008 12.001 15.999 14.007 39.948 32.065];
low_temp = str2double(l(m-3));
mid_temp = str2double(l(m-2));
high_temp = str2double(l(m-1));
%storing the vlue of second line
line2 = fgetl(f);
a = strfind(line2,'E');
a1 = str2double(line2(1:(a+3)));
a2 = str2double(line2((a(1)+4):(a(2)+3)));
a3 = str2double(line2((a(2)+4):(a(3)+3)));
a4 = str2double(line2((a(3)+4):(a(4)+3)));
a5 = str2double(line2((a(4)+4):(a(5)+3)));
%storing the value of 3rd line
line3 = fgetl(f);
b = strfind(line2,'E');
a6 = str2double(line3(1:(b(1)+3)));
a7 = str2double(line3((b(1)+4):(a(2)+3)));
a8 = str2double(line3((b(2)+4):(a(3)+3)));
a9 = str2double(line3((b(3)+4):(a(4)+3)));
a10 = str2double(line3((b(4)+4):(a(5)+3)));
%storing the value of 4th line
line4 = fgetl(f);
c = strfind(line3,'E');
a11 = str2double(line4(1:(c(1)+3)));
a12 = str2double(line4((c(1)+4):(a(2)+3)));
a13 = str2double(line4((c(2)+4):(a(3)+3)));
a14 = str2double(line4((c(3)+4):(a(4)+3)));
%creating temperature values for lower and higher temperature
TL = linspace (low_temp,mid_temp,100);
TH = linspace (mid_temp,high_temp,100);
%value of universal gas constant
R = 8.314;
%calculating specific heat, enthalpy and entropy for lower and higher
%temperature
for k = 1:100
%for lower temprature
cp_low(k) = (a8 + a9*TL(k)) + a10*TL(k)^2 + a11*TL(k)^3 + (a12*TL(k)^4)*R;
h_low(k) = (a8 + a9*TL(k)/2) + a10*TL(k)^2/3 + a11*TL(k)^3/4 +(a12*TL(k)^4)/5 + (a13/TL(k))*R*TH(k);
s_low(k) = (a8*log(TL(k)) + a9*TL(k) + a10*TL(k)^2)/2 + a11*(TL(k)^3)/3 + a12*(TL(k)^4)/4 + a13 ;
%for higher temperature
cp_high(k) = (a1 + a2*TH(k) + a3*TH(k)^2 + a4*TH(k)^3 + a5*TH(k)^4)*R;
h_high(k) = (a1 + a2*TH(k)/2 + a3*TH(k)^2/3 + a4*TH(k)^3/4 + a5*TH(k)^4/5 + a6/TH(k))*R*TH(k);
s_high(k) = (a1*log(TH(k))) + a2*TH(k) + a3*TH(k)^2/2 + a4*TH(k)^3/3 + a5*TH(k)^4/4 + a6*TH(k)*5/5 ;
end
%creating folder
nasa_file = species;
mkdir(nasa_file);
current = pwd;
cd(nasa_file);
%creating figure of specific heat
figure(1)
plot (TL,cp_low,'lineWidth',2,'color','r');
hold on
plot (TH,cp_high,'lineWidth',2,'color','g');
hold off
title({'Temperature VS specific heat, ',[species]});
xlabel('Temprature');
ylabel('Specific Heat');
legend('low Temperature','High Temperature');
% saving png files of figure
fig = sprintf('specific heat of %s.png',species);
saveas(gca,fig);
%creating figure of enthalpy
figure(2)
plot (TL,cp_low,'LineWidth',2,'color','r');
hold on
plot(TH,h_high,'LineWidth',2,'color','g');
hold off
title({'Temperature VS Enthalpy, ',[species]});
xlabel('Temprature');
ylabel('Enthalpy');
legend('low Temperature','High Temperature');
% saving png files of figure
fig = sprintf('enthalpy of %s.png',species);
saveas(gca,fig);
%creating figure of entropy
figure(3)
plot (TL,s_low,'LineWidth',2,'color','r');
hold on
plot(TH,s_high,'LineWidth',2,'color','g');
hold off
title({'Temperature VS Entropy, ',[species]});
xlabel('Temprature');
ylabel('Entropy');
legend('low Temperature','High Temperature');
% saving png files of figure
fig = sprintf('entropy of %s.png',species);
saveas(gca,fig);
cd(current)
% Molecular weight calculation
elements = ['H','C','O','N','A','S']; % Declaring species elements
atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948 32.065]; % Declaring the atomic weight value
mol_mass = 0; % Declaring molecular weight to be '0' in initial stage
for l = 1:length(species)
for m = 1:length(atomic_weight)
if strcmp(species(l), elements(m))
mol_mass = mol_mass + atomic_weight(m);
position = m;
end
end
n = str2double(species(l));
if n>1
mol_mass = mol_mass + atomic_weight(position)*(n-1);
end
end
% Printing the molecular weight of the gas species in the command window
sprintf('Molecular Weight of %s is %f u',species,mol_mass)
end
Explaination :-
What is File Parsing?
File parsing refers to how information can be read from a particular text file or any type of file and how that information can be used in a computer program.
- Step 1 - Define the function
- Step 2 - Create an InputParser object
- Step 3 - Add inputs to the scheme.
- Step 4 - Set properties to adjust parsing.
- Step 5 - Parse the inputs.
- Step 6 - Use the inputs in function.
- Step 7 - Calling function.
Code explaination:-
First i opend the NASA file in reading mode using fopen command the took all the data fro mit in f variable. Then read the comments line and skipped them. readign the species data and splitting the data to arrange it in matrix form. Then converted all the 14 coefficients to numbers using str2double function as all the importated data from the NASA file are stored as a string in the variable and then store all the coefficients in the variable a1, a2, a3, .... a14. creating the low tempreture and high temperature matrix using line space. calculate the enthalpy, entropy, and specific heats for all the species using for loop.
created the folder with the name of the species and saved all the three enthalpy, entropy, and specific heats curves with the proper file name. i used sprintf and saveeas command to save the figure. same way save all the files using for loop and then calculated the atomic weight with the Weight of the specific elements used for loop and if else statements.
Output in command window :-

Rest of output results :-
Warning: Directory already exists.
> In Project_11 (line 83)
ans =
'Molecular Weight of O is 15.999000 u'
Warning: Directory already exists.
> In Project_11 (line 83)
ans =
'Molecular Weight of O2 is 31.998000 u'
ans =
'Molecular Weight of H is 1.007900 u'
ans =
'Molecular Weight of H2 is 2.015800 u'
ans =
'Molecular Weight of OH is 17.006900 u'
ans =
'Molecular Weight of H2O is 18.014800 u'
ans =
'Molecular Weight of HO2 is 33.005900 u'
ans =
'Molecular Weight of H2O2 is 34.013800 u'
ans =
'Molecular Weight of C is 12.010700 u'
ans =
'Molecular Weight of CH is 13.018600 u'
ans =
'Molecular Weight of CH2 is 14.026500 u'
ans =
'Molecular Weight of CH2(S) is 46.091500 u'
ans =
'Molecular Weight of CH3 is 15.034400 u'
ans =
'Molecular Weight of CH4 is 16.042300 u'
ans =
'Molecular Weight of CO is 28.009700 u'
ans =
'Molecular Weight of CO2 is 44.008700 u'
ans =
'Molecular Weight of HCO is 29.017600 u'
ans =
'Molecular Weight of CH2O is 30.025500 u'
ans =
'Molecular Weight of CH2OH is 31.033400 u'
ans =
'Molecular Weight of CH3O is 31.033400 u'
ans =
'Molecular Weight of CH3OH is 32.041300 u'
ans =
'Molecular Weight of C2H is 25.029300 u'
ans =
'Molecular Weight of C2H2 is 26.037200 u'
ans =
'Molecular Weight of C2H3 is 27.045100 u'
ans =
'Molecular Weight of C2H4 is 28.053000 u'
ans =
'Molecular Weight of C2H5 is 29.060900 u'
ans =
'Molecular Weight of C2H6 is 30.068800 u'
ans =
'Molecular Weight of CH2CO is 42.036200 u'
ans =
'Molecular Weight of HCCO is 41.028300 u'
ans =
'Molecular Weight of HCCOH is 42.036200 u'
ans =
'Molecular Weight of H2CN is 28.033200 u'
ans =
'Molecular Weight of HCN is 27.025300 u'
ans =
'Molecular Weight of HNO is 31.013600 u'
ans =
'Molecular Weight of N is 14.006700 u'
ans =
'Molecular Weight of NNH is 29.021300 u'
ans =
'Molecular Weight of N2O is 44.012400 u'
ans =
'Molecular Weight of NH is 15.014600 u'
ans =
'Molecular Weight of NH2 is 16.022500 u'
ans =
'Molecular Weight of NH3 is 17.030400 u'
ans =
'Molecular Weight of NO is 30.005700 u'
ans =
'Molecular Weight of NO2 is 46.004700 u'
ans =
'Molecular Weight of HCNO is 43.024300 u'
ans =
'Molecular Weight of HOCN is 43.024300 u'
ans =
'Molecular Weight of HNCO is 43.024300 u'
ans =
'Molecular Weight of NCO is 42.016400 u'
ans =
'Molecular Weight of CN is 26.017400 u'
ans =
'Molecular Weight of HCNN is 41.032000 u'
ans =
'Molecular Weight of N2 is 28.013400 u'
ans =
'Molecular Weight of AR is 39.948000 u'
ans =
'Molecular Weight of C3H8 is 44.095300 u'
ans =
'Molecular Weight of C3H7 is 43.087400 u'
ans =
'Molecular Weight of CH3CHO is 44.052000 u'
ans =
'Molecular Weight of CH2CHO is 43.044100 u'
>>
Screenshot of the folder where all the results saved :-

Plots for o2, N2 and Co2 species :
Plots for O2 :-



Plots for N2 :-



Plots for co2 :-



Errors :-
I got so many errors during writing the code. one small error screenshot is attached where I wrote the capital 's' and the variables are case sensitive in Matlab so it throws the error that unrecognized function, i solve this types of error by cheking the spellings.
