Project 1 - Parsing NASA thermodynamic data
AIM -
To write a code in Matlab to parse the NASA thermodynamic data file and then calculate the thermodynamic properties of various gas species.
OBJECTIVE -
-- Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
-- Calculate the molecular weight of each species and display it in the command window.
-- Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each
species.
-- Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. all these should be generated automatically.
-- The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.
Screenshot of the window where all the folders are saved.
-- Plots for o2, N2 and Co2 species.
THEORY -
File Parsing:
It is the method of splitting the file or other input into a piece of data that can be easily stored or manipulated. By doing this we are going to split a string into parts then recognizing the parts to convert it into something simpler than a string.
Here in this project we are going to split the NASA thermodynamic data which is having 53 gas species values in 213 lines. To
perform the calculation for thermodynamic properties, we need some sort of co-efficient values and temperature ranges which is available in the NASA file. So, we are going to parse the values which are all needed for our calculation from that file.
Specific Heat:
Specific heat is the amount of heat energy required to raise the temperature of body per unit of mass. Specific heat is also
known as specific heat capacity or mass specific heat. In SI units, specific heat is the amount of heat in joules required to raise 1 gram of a substance 1 Kelvin.
Enthalpy:
When a substance changes at constant pressure, enthalpy tells how much heat and work was added or removed from the
substance. Enthalpy is similar to energy but not the same. When a substance grows or shrinks, energy is used up or released.
Entropy:
The entropy of an object is a measure of the amount of energy which is unavailable to do work. Entropy is also a measure of the number of possible arrangements of atoms a system can have. In this sense, entropy is the measure of uncertainty or randomness.
Gas Constant:
The gas constant is a physical constant denoted by R and is expressed in terms of units of energy per temperature increment
per mole. The gas constant value is equivalent to Boltzmann constant but expressed as the pressure-volume product instead of energy per increment of temperature per particle.
Molecular Weight:
Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule. Molecular weight is used in
chemistry to determine stoichiometry in chemical reactions and equations.
GOVERNING EQUATIONS -
Sample NASA Data:

Equations used:

where,
R = Universal Gas constant = 8.134(J/k-mol)
T = Temperature(K)
Cp = Specific heat (KJ)
S = Entropy(KJ/mol-K)
H = Enthalpy(KJ/mol)
CODE ALGORITHM -
Main program :
% PROJECT - 1 // PARSING NASA THERMODYNAMIC DATA USING MATLAB
clear all
close all
clc
% Opening the file
f1 = fopen('THERMO.dat','r');
% using for loop to read first 5 lines
for i = 1:5
%fgetl reads each 5 line from a file
a = fgetl(f1);
end
for i = 1:53
% Getting first header line which is a character with different data's
header_line = fgetl(f1);
% Splitting the string based on delimiter
% Our delimiter is space
a = strsplit(header_line,' ');
% We use curly brackets because it is character
% 1st cell represents the species_name
species_name = a{1};
% Local lower, medium, higher temperatures, we are converting string to number
llt = str2double(a{end-3});
lmt = str2double(a{end-1});
lht = str2double(a{end-2});
T = linspace(llt,lht,500);
% Getting species with exponential coeifficent
line1 = fgetl(f1);
% Finding the location of exponent(E) for line 1
e1 = strfind(line1,'E');
% We are converting string to number
a1 = str2double(line1(1:e1(1)+3));
a2 = str2double(line1(e1(1)+4:e1(2)+3));
a3 = str2double(line1(e1(2)+4:e1(3)+3));
a4 = str2double(line1(e1(3)+4:e1(4)+3));
a5 = str2double(line1(e1(4)+4:e1(5)+3));
% Getting species with exponential coeifficent
line2 = fgetl(f1);
% Finding the location of exponent(E) for line 2
e2 = strfind(line2,'E');
% We are converting string to number
a6 = str2double(line2(1:e2(1)+3));
a7 = str2double(line2(e2(1)+4:e2(2)+3));
a8 = str2double(line2(e2(2)+4:e2(3)+3));
a9 = str2double(line2(e2(3)+4:e2(4)+3));
a10 = str2double(line2(e2(4)+4:e2(5)+3));
% Getting species with exponential coeifficent
line3 = fgetl(f1);
% Finding the location of exponent(E) for line 3
e3 = strfind(line3,'E');
% We are converting string to number
a11 = str2double(line3(1:e3(1)+3));
a12 = str2double(line3(e3(1)+4:e3(2)+3));
a13 = str2double(line3(e3(2)+4:e3(3)+3));
a14 = str2double(line3(e3(3)+4:e3(4)+3));
% Defining Universal Gas Constant 'r'
r = 8.314;
% Calling functions in main body: specific_heat,enthalpy,entropy
[cp] = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,lmt,r);
[H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,lmt,r);
[S] = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,lmt,r);
% Creating new folder for species
mkdir(['C:\Users\ankit\OneDrive\Documents\MATLAB\File parsing project\Plotting part\',species_name])
% Entering current folder
cd(['C:\Users\ankit\OneDrive\Documents\MATLAB\File parsing project\Plotting part\',species_name])
% Plot 1
figure(1)
plot(T,cp)
xlabel('Temperture')
ylabel('Specific heat')
title('Specific heat vs Temperture of',sprintf('%s',species_name))
% Saveas saves figure to a specific format
saveas(figure(1),'cp vs T.jpg')
% Plot 1
figure(2)
plot(T,H)
xlabel('Temperture')
ylabel('Enthalpy')
title('Enthalpy vs Temperture of',sprintf('%s',species_name))
% Saveas saves figure to a specific format
saveas(figure(2),'H vs T.jpg')
% Plot 1
figure(3)
plot(T,S)
xlabel('Temperture')
ylabel('Entropy')
title('Entropy vs Temperture of',sprintf('%s',species_name))
% Saveas saves figure to a specific format
saveas(figure(1),'S vs T.jpg')
%Returning back to initial directory
cd ..
[M] = mol_wt(species_name);
K = sprintf('%s weight = %f',species_name,M);
disp(K)
end
fclose(f1);
Explanation of Code :
1. The program begins with clear all, close all and clc commands to free up the command window and the workspace.
2. We will then read the data from the external NASA thermodynamic data file using fopen command which is used to open the file and fgetl command which is used to read the content in the file line by line.
3. After we use the fopen command we will be assigning to the variable called f1. Thus, the fgetl(f1) is the command acts like a line cursor, which will retrieve the next time while running this command for the second time and so on.
4. Now, we will be skipping the first 5 lines to enter the pattern of values of the respective gas species. We will be using the for loop command to process the same for all the gas species.
5. The name of the gas species we will be found out using findstr command and for finding out the species we are going to locate the string ‘ ’ which is present in after each species name.
6. And then we are going to use species_name = a{1}which tells us the exact location of the species name, using which the name of the gas species is stored in the variable species_name.
7. The local temperatures for the respective gas species is found similarly using findstr command and we are going to locate the string ‘.’ using which we will find out the values of low temperature, mid temperature and high temperature which is assigned to the variables llt, lmt and lht respectively. But the values we have achieved are string values so we have to change these string values to numeric values using the str2num command.
8. Now, we are going to extract the co-efficient for the gas species in which the co-efficient locations are like in the 2 row, next 5 in the 3 row and the rest in the 4 row.
9. We are going to extract these values similarly like we did for finding out the local temperatures by using the findstr and str2num commands and store it in the variables a1 to a14. For extracting we are going to locate the string ‘E’ which is present in the each co-efficient value and so it will be followed for all the co-efficients values in the same line and then we can proceed for the next lines.
10. The values from a1 to a7 represent the high temperature co-efficients and the values from a8 to a14 represent the low
temperature co-efficients.
11. Temperature range values from low temperature to high temperature is assigned using the linspace command.
12. The for loop command is used to calculate the values of specific heat, enthalpy and entropy and the if-else command is also used to find out the required values if the temperature below the mid temperature or above the mid temperature.
13. In order to calculate the molecular weight of the species we declare the species elements in the form of cell array. Their atomic mass is declared in the form of array. Then initially molecular weight is declared as zero.
14. Now, for loop command is used to detect if there are more than one species in the variable species_name and the respective values of the gas species values are calculated and found out.
15. After the nested for loop the species_name which is in the form of string is converted as number with strdouble command. Then the species_name which is now as number is greater than 1, molecular weight is again calculated by having the atomic mass for the current species * n-1
16. The value of the molecular weight is calculated for the respective gas species and is displayed in the command window.
17. Now, we are going to create plots for the obtain specific heat, enthalpy and entropy values and save it in respective
species_name folder automatically.
18. In order to create a new folder with the species_name we are going to use the mkdir command so that a new folder is created in the main folder. Now, we have to change the current directory from the main folder to the species_name folder so the cd command is used to do the same.
19. The plots for specific heat vs temperature, enthalpy vs temperature and entropy vs temperature is plotted and the labelling of axis and the naming of the title is also done along with the value of molecular weight.
20. To save the plots in the species_name folder, saveas command is used to save the plots in the current folder. After which to change the current directory to the main folder the oldfolder = cd () command is used.
FUNCTION PART
Function to find specific heat:
% challenge 8 - NASA file parsing Specific Heat Function calculation
% Code
function [cp] = specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,temp,lmt,const_R)
if temp<=lmt
cp = (x8+(x9*temp)+(x10*temp.^2)+(x11*temp.^3)+(x12*temp.^4))*const_R;
else
cp = (x1+(x2*temp)+(x3*temp.^2)+(x4*temp.^3)+(x5*temp.^4))*const_R;
end
end
STEPS :-
-- Now we will find the Specific heat by creating Specific heat function and assigning its relted formula coefficent from 14
coefficients, Gas constant, Temperature and mid temperature.
-- Now we will creatre an condition using if loop and make the temperature less than and equal to mid temperature to specify
lower temperature coefficent in the formula.
-- Then we will use else condition to specify coefficient of Higher temperature coefficient in the formula.
-- Then end the loop and function to Calculate Specific Heat
Function to find Enthalpy:
% challenge 8 - NASA file parsing Enthalpy Function calculation
% Code
function [H] = enthalpy(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,temp,lmt,const_R)
if temp<=lmt
H = (x8+(x9*(temp/2))+(x10*(temp.^2/3))+(x11*(temp.^3/4))+(x12*(temp.^4/5))+(x13./temp)).*temp*const_R;
else
H = (x1+(x2*(temp/2))+(x3*(temp.^2/3))+(x4*(temp.^3/4))+(x5*(temp.^4/5))+(x6./temp)).*temp*const_R;
end
end
STEPS :-
-- Now we will find the Enthalpy by creating Enthalpy function and assigning its relted formula coefficent from 14 coefficients, Gas constant, Temperature and mid temperature.
-- Now we will creatre an condition using if loop and make the temperature less than and equal to mid temperature to specify
lower temperature coefficent in the formula.
-- Then we will use else condition to specify coefficient of Higher temperature coefficent in the formula.
-- Then end the loop and function to calculate Enthalpy.
Function to find Entropy:
% challenge 8 - NASA file parsing Entropy Function calculation
% Code
function [S] = entropy(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,temp,lmt,const_R)
if temp>=lmt
S = (x1*log(temp)+x2*temp+x3*(temp.^2/2)+x4*(temp.^3/3)+x5*(temp.^4/4)+x7)*const_R;
else
S = (x8*log(temp)+x9*temp+(x10*(temp.^2)/2)+(x11*(temp.^3)/3)+(x12*(temp.^4)/4)+x14)*const_R;
end
end
STEPS :-
-- Now we will find the Entropy by creating Entropy function and assigning its relted formula coefficent from 14 coefficients, Gas constant, Temperature and mid temperature.
-- Now we will creatre an condition using if loop and make the temperature less than and equal to mid temperature to specify
lower temperature coefficent in the formula.
-- Then we will use else condition to specify coefficient of Higher temperature coefficent in the formula.
-- Then end the loop and function to calculate Entropy.
Function to find Molecular Weight:
% challenge 8 - NASA file parsing molecular weight Function calculation
% Code
function Mole_wt = mol_wt(species_name)
weight = 0;
elements = ['H','C','N','O','Ar'];
mass = [1.0,29.36,52.37,14.008,15.78,45.63];
for i = 1:length(species_name)
for j = 1:length(elements)
if strcmpi(species_name(i),elements(j))
weight = weight+mass(j);
y = j;
end
end
number = str2double(species_name(i));
if(number>1)
weight = weight+(mass(y)*(number-1));
end
Mole_wt = weight;
end
STEPS :-
-- Firstly we create an Molecular_weight function W and assigning species input to that.
-- Then define the main element of all 53 species in an array atomic_name that are :- 'H','C','O','N','Ar'.
Then by taking above main elements we will create their atomic_wieght array from any source like the internet that are :- 1.0,29.36,52.37,14.008,15.78,45.63.
-- Then we will assign molecular starting range W To zero and then create an for loop condition which runs to the length of the
species and an another nested for loop is created which runs to the length of the string atomic_name.
-- Then we will create an condition using if condition and use strcmp command to compare the species and atomic_name
strings to calculate molecular masses if by comparison both string found same then we will assign its molecular weight.
-- Now, we will string to the number using str2num command assign to n. Then we run an condition of if loop to find weather the species contains more than one element if there are more then 1 elements that is n>1 then we will increment the atomic weight by the multiplication of position and n-1 to the atomic_weight array.
-- Then we will print the Molecular weight using fprintf command and display it using disp command.
RESULT -
OUTPUT :
plots of O2 -


Plots of N2 -



plots of CO2 -



Screenshot of the directory where all different species are stored :

Output of molecular weight for all the species in command window :

CONCLUSION -
-- Therefore, we have successfully parsed the given NASA thermodynamic data file and plotted the graphs of specific heat, enthalpy and entropy for the respective inputted species name.
-- Parsing is a very important part of many computer science disciplines. For example, compilers must parse source code to be able to translate it into object code. Likewise, any application that processes complex commands must be able to parse the commands. This includes virtually all end-user applications.