Project 1 - Parsing NASA thermodynamic data
AIM- File Parsing using NASA Thermodynamics data
THEORY-
File Parsing :
File parsing is a method that helps us in decoding that data or in other words help us reading that data and use it in calculation of several parameters that defines the state of that environment. And analyse behaviour of different species changing with some varying inputs.
To do file parsing first we need to check weather the data is broken down into meaningful bits or not. if not than we should first break it down in a standard form so that reading for each species becomes similar.
NASA Thermodynamics Data:
NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S. They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species.
Using file parsing we need to write a function that extracts the 14 co-efficients by reading data file and calculates the enthalpy (H), entropy(S) and specific heats(Cp) for all the species. We can also make plots for better understanding the behaviour of gas w.r.t to temperature.
Formula :

Here, R is the universal gas constant, T is the temperature.
The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.
Change in Enthalpy, Entropy and Specific Heat w.r.t Temperature
Enthalpy of any substance increases with temperature, which means both the products and the reactants' enthalpies increase. The overall enthalpy of the reaction will change if the increase in the enthalpy of products and reactants is different.
Entropy:
Several factors affect the amount of entropy in a system. If you increase temperature, you increase entropy.
(1) More energy put into a system excites the molecules and the amount of random activity.
(2) As a gas expands in a system entropy increases.
Specific Heat:
As the substance heats up, the average kinetic energy of the molecules increases. The collisions impart enough energy to allow rotation to occur. Rotation then contributes to the internal energy and raises the specific heat.
Key steps to follow:
- Open the data for MATLAB to read
- Read the data line after line.
- Split the line into parts for better access to data.
- Take input from user to directly jump to species study instead of reading whole data.
- Search for matching string (species) similar to the input user made.
- Calculate and plot things for that input and make a folder of plots created for that species by species name to sort data.
- Close the data.
clear all
close all
clc
% Program to read NASA Thermodynamic Data
file = fopen ('THERMO.dat','r');
line1 = fgetl(file);
fprintf('Title of Data is : %sn',line1)
% Finding the global temperature values out of string in line 2
line2 = fgetl(file);
S2s = strsplit(line2,' ');
Global_LowestTemp = str2num(S2s{2});
Global_MediumTemp = str2num(S2s{3});
Global_HighestTemp = str2num(S2s{4});
Message = 'Please enter name of Gas in Capitals : ';
x = input(Message,'s');
while ~feof(file) % ~ Here this sign says run loop till this condition remain true i.e. 1 if it becomes false i.e. 0, than terminate loop
line = fgetl(file);
S2S1 = strsplit(line,' ');
if S2S1{1}== '!' %
fprintf('%sn',line);
fprintf( ' Operation not reguired for this linenn')
end
P = length(S2S1);
if S2S1{P} == '1'
if strcmpi(S2S1{1},x)
fprintf( ' Operation required for this linenn')
fprintf('%sn',line);
fprintf('Name of Gas is : %sn',S2S1{1})
gas_name = S2S1{1};
molecular_weight_of_gas = weight_calculation(gas_name);
fprintf('Molecular weight of Gas is : %fn',molecular_weight_of_gas)
Local_Min_Temp = str2num(S2S1{P-3});
Local_Max_Temp = str2num(S2S1{P-2});
Local_Med_Temp = str2num(S2S1{P-1});
Local_Temp = Local_Min_Temp : Local_Max_Temp;
fprintf('Local Temp. Values of Data are : %f %f %fn',Local_Min_Temp, Local_Max_Temp, Local_Med_Temp)
% Finding coefficients value for given gas
% iN LINE 1
coeff_line1 = fgetl(file);
S2S2 = findstr(coeff_line1,'E');
c1 = str2num(coeff_line1(1:S2S2(1)+3));
c2 = str2num(coeff_line1(S2S2(1)+4:S2S2(2)+3));
c3 = str2num(coeff_line1(S2S2(2)+4:S2S2(3)+3));
c4 = str2num(coeff_line1(S2S2(3)+4:S2S2(4)+3));
c5 = str2num(coeff_line1(S2S2(4)+4:S2S2(5)+3));
fprintf('Coefficients Values of Data are : %s %s %s %s %sn',c1,c2,c3,c4,c5);
% iN LINE 2
coeff_line2 = fgetl(file);
S2S3 = findstr(coeff_line2,'E');
c6 = str2num(coeff_line2(1:S2S3(1)+3));
c7 = str2num(coeff_line2(S2S3(1)+4:S2S3(2)+3));
c8 = str2num(coeff_line2(S2S3(2)+4:S2S3(3)+3));
c9 = str2num(coeff_line2(S2S3(3)+4:S2S3(4)+3));
c10 = str2num(coeff_line2(S2S3(4)+4:S2S3(5)+3));
fprintf('Coefficients Values of Data are : %s %s %s %s %sn',c6,c7,c8,c9,c10);
% iN LINE 3
coeff_line3 = fgetl(file);
S2S4 = findstr(coeff_line3,'E');
c11 = str2num(coeff_line3(1:S2S4(1)+3));
c12 = str2num(coeff_line3(S2S4(1)+4:S2S4(2)+3));
c13 = str2num(coeff_line3(S2S4(2)+4:S2S4(3)+3));
c14 = str2num(coeff_line3(S2S4(3)+4:S2S4(4)+3));
fprintf('Coefficients Values of Data are : %s %s %s %snn',c11,c12,c13,c14);
% Calulation of Cp (Specific Heat, J/K.Mol), H (Enthalpy, J/Kg), & S(Entropy, J/K)
Cp = 0;
S = 0;
H=0;
R = 8.3145;
for i = 1 : length(Local_Temp)
if Local_Temp(i) >= Local_Med_Temp
Cp(i) = (c1+(c2.*Local_Temp(i))+(c3.*(Local_Temp(i).^2))+(c4.*(Local_Temp(i).^3))+(c5.*(Local_Temp(i).^4))).*R;
H(i) = (c1+((c2.*Local_Temp(i))./2)+((c3.*(Local_Temp(i).^2))./3)+((c4.*(Local_Temp(i).^3))./4)+((c5.*(Local_Temp(i).^4))./5)+(c6./Local_Temp(i))).*R.*Local_Temp(i);
S(i) = ((c1.*log(Local_Temp(i)))+(c2.*Local_Temp(i))+(c3.*(Local_Temp(i).^2)./2)+(c4.*(Local_Temp(i).^3)./3)+(c5.*(Local_Temp(i).^4)./4)+c7).*R;
elseif Local_Temp(i) < Local_Med_Temp %For Low Temperature Range i.e. Local_Min_Temp : Local_Med_Temp
Cp(i) = (c8+(c9.*(Local_Temp(i)))+(c10.*(Local_Temp(i).^2))+(c11.*(Local_Temp(i).^3))+(c12.*(Local_Temp(i).^4))).*R;
H(i) = (c8+((c9.*Local_Temp(i))./2)+((c10.*(Local_Temp(i).^2))./3)+((c11.*(Local_Temp(i).^3))./4)+((c12.*(Local_Temp(i).^4))./5)+(c13./Local_Temp(i))).*R.*Local_Temp (i);
S(i) = ((c8.*log(Local_Temp(i)))+(c9.*Local_Temp(i))+(c10.*(Local_Temp(i).^2)./2)+(c11.*(Local_Temp(i).^3)./3)+(c12.*(Local_Temp(i).^4)./4)+c14).*R;
end
end
foldername = sprintf(S2S1{1})
mkdir(foldername)
cd(foldername)
figure(1) % Creating a figure
plot(Cp, Local_Temp,'color','b')
title('Plot between Cp and Temp.')
ylabel('Temperature [K]')
xlabel('Specific Heat [KJ/Kmol.K')
saveas (figure(1),'Cp.jpg')
figure(2)
plot(H, Local_Temp,'color','b')
title('Plot between Enthalpy and Temp.')
ylabel('Temperature [K]')
xlabel('Enthalpy [KJ/Kmol.K')
saveas(figure(2),'H.jpg')
figure(3)
plot(S, Local_Temp,'color','b')
title('Plot between Entropy and Temp.')
ylabel('Temperature [K]')
xlabel('Entropy [KJ/Kmol.K')
saveas (figure(3),'S.jpg')
cd ..
end
end
end
fclose(file);
function weight = weight_calculation (gas_name) % Function defination
gas = gas_name; % copying user input to a variable
Element = {'H' 'C' 'N' 'O' 'Ar'}; % Creating cell array of elements present in data
atom_mass=[1.00797 12.011 14.0067 15.9994 39.948]; % creating array containing atomic masses of elements
a=0;
weight = 0;
for i = 1 : length(gas) % Running loop till total length of user input
for j = 1:length(Element)
if strcmpi(gas,Element(5)) % comparying 2 strings
weight=atom_mass(5);
end
if strcmpi(gas(i),Element(j)) % comparying 2 strings
weight = weight + atom_mass(j); % Calculating weight of species
a = atom_mass(j);
end
end
x = str2num(gas(i)); % convering string to number type
if x>1
weight = (a*(x-1)) + weight; %% Calculating weight of species
end
end
end
OUTPUTS
1) Screenshot of the window where all the folders are saved.

2) Screenshot of the window asking for user input
3) Plots for o2, N2 and Co2 species
3.1) O2



3.2)N2



3.3)CO2



Errors Faced:
1) New folder for species were folder within the earlier folder
solution : use of cd.. to come 1 step back for every species
2) Some typing mistakes of not placing commas, semicolons and brackets which were overcome by having a close look at the program
Refrences:
https://socratic.org/questions/how-does-specific-heat-change-with-temperature
https://www.quora.com/How-are-entropy-and-temperature-related
https://en.wikibooks.org/wiki/Units_of_Measurement/Entropy