Project 1 - Parsing NASA thermodynamic data
Aim :- Write a program for file parsing of NASA thermodynamics data using matlab.
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.
Equations of Specific heat, Enthalpy(H) and Entropy(S) are as under;

In this, The first 7 coefficients are HIGH-temperature coefficients and the second 7 coefficients are LOW-temperature coefficients. Also use the Local Temperature Ranges which is unique for each individual species, not the Global Temperature Range.
- 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 (your program should be case insensitive, if it is going to take the species name as an input).
THEORY:-
FILE PARSING :-
- Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar.
- File parsing in computer language means to give a meaning to the characters of a text file as per the formal grammar.
- Within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic relation to each other, which may also contain semantic and other information. Some parsing algorithms may generate a parse forest or list of parse trees for asyntactically ambiguous input.
- Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating performing some kind of transformation on the data.
- 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. To perform the calculation for thermodynamic properties, we need some 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 particular file.
ENTHALPY :-
- Enthalpy (H) is a property of a thermodynamic system, and is defined as the sum of the system's internal energy (U) and the product of its pressure and volume.
- H = U + pV,
where p is pressure, and V is the volume of the system.
- Enthalpy is an energy-like property or state function—it has the dimensions of energy (and is thus measured in units of joules or ergs), and its value is determined entirely by the temperature, pressure and composition of the system.
- It is a state function used in many measurements in chemical, biological, and physical systems at a constant pressure, that is conveniently provided by the large ambient atmosphere.
- In a system contained so as to prevent mass transfer, for processes at constant pressure, the heat absorbed or released equals the change in enthalpy.
ENTROPY :-
- Entropy, the measure of a system's thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.
ΔS=(Q/T)rev
where Q is the heat transfer, which is positive for heat transfer into and negative for heat transfer out of, and T is the absolute temperature at which the reversible process takes place.
- The SI unit for entropy is joules per kelvin (J/K). If temperature changes during the process, then it is usually a good approximation (for small changes in temperature) to take T to be the average temperature, avoiding the need to use integral calculus to find ΔS.

- The concept of entropy provides deep insight into the direction of spontaneous change for many everyday phenomena.
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.

- The SI unit of specific heat capacity is joule per kelvin per kilogram (J/Kg-K).
- In other words, it is defined as the amount of heat required to raise the temperature of unit mass of a substance by 1 degree.
- The specific heat capacity can be defined and measured for gases, liquids, and solids of fairly general composition and molecular structure. These include gas mixtures, solutions and alloys, or heterogenous materials such as milk, sand, granite, and concrete, if considered at a sufficiently large scale.
- The specific heat capacity can be defined also for materials that change state or composition as the temperature and pressure change, as long as the changes are reversible and gradual.
UNIVERSAL GAS CONSTANT (R) :-
- 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. - It's value is R=8.314 J/Kg-K.
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.
- Molecular weight is commonly abbreviated by M.W. or MW. Molecular weight is either unitless or expressed in terms of atomic mass units (amu) or Daltons (Da).
- The calculation for molecular weight is based on the molecular formula of a compound (i.e., not the simplest formula, which only includes the ratio of types of atoms and not the number). The number of each type of atom is multiplied by its atomic weight and then added to the weights of the other atoms.
- For example, the molecular formula of hexane is C6H14. The subscripts indicate the number of each type of atom, so there are 6 carbon atoms and 14 hydrogen atoms in each hexane molecule. The atomic weight of carbon and hydrogen may be found on a periodic table;
-

- Therefore, the Atomic weight of carbon is 12.011 & Atomic weight of hydrogen is 1.008.
- Molecular weight = (number of carbon atoms)(C atomic weight) + (number of H atoms)(H atomic weight) so we calculate as follows:
- molecular weight = (6 x 12.011) + (14 x 1.008)
- molecular weight of hexane = 86.178 amu
Code for function Specific Heat (Cp) :-
function [Cp]= specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,temperature_range,R)
%a1 a2 a3 a4 a5 are high temperature coefficients
%a8 a9 a10 a11 a12 are low temperature coefficients
for i= 1:length(temperature_range)
if temperature_range(i)<=temp(2)
Cp(i)= R.*(a8 + (a9* temperature_range(i)) + (a10*(temperature_range(i).^2)) + (a11*(temperature_range(i).^3)) + (a12*(temperature_range(i).^4)));
else
Cp(i)= R.*(a1 + (a2*temperature_range(i)) + (a3*(temperature_range(i).^2)) + (a4*(temperature_range(i).^3)) + (a5*(temperature_range(i).^4)));
end
end
Explanation of function specific heat code :-
- We will find the Specific heat by creating Specific heat function and assigning its related formula coefficent from 14
temperature coefficients, Universal Gas constant, temp and temperature_range. - We have used for loop because we have to iterate till length of temperature_range.
- Further, we made an condition using if loop and make the temperature_range(i) less than and equal to local mid temperature( and since local mid temperature is second value in temp array so temp(2) is used in this function) to specify lower temperature coefficent in the formula.
- Then we will use else condition to specify Higher temperature coefficent in the formula. Then we ended the loop and function to calculate Specific Heat.
Code for function Enthalpy (H):-
function [H]= enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,temperature_range,R)
%a1 a2 a3 a4 a5 a6 are high temperature coefficients
%a8 a9 a10 a11 a12 a13 are low temperature coefficients
for i= 1:length(temperature_range)
if temperature_range(i)<=temp(2)
H(i)= R.*((a8.*temperature_range(i)) + (a9.*((temperature_range(i).^2)/2)) + (a10.*((temperature_range(i).^3)/3)) + (a11.*((temperature_range(i).^4)/4)) + (a12.*((temperature_range(i).^5)/5)) + a13);
else
H(i)= R.*((a1.*temperature_range(i)) + (a2.*((temperature_range(i).^2)/2)) + (a3.*((temperature_range(i).^3)/3)) + (a4.*((temperature_range(i).^4)/4)) + (a5.*((temperature_range(i).^5)/5)) + a6);
end
end
Explanation of function Enthalpy(H) code :-
- We will find the Enthalpy by creating Enthalpy function and assigning its related formula coefficent from 14
temperature coefficients, Universal Gas constant, temp and temperature_range. - We have used for loop because we have to iterate till length of temperature_range.
- Further, we made an condition using if loop and make the temperature_range(i) less than and equal to local mid temperature( and since local mid temperature is second value in temp array so temp(2) is used in this function) to specify lower temperature coefficent in the formula.
- Then we will use else condition to specify Higher temperature coefficent in the formula. Then we ended the loop and function to calculate Enthalpy.
Code for function Entropy (S) :-
function [S]= entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,temperature_range,R)
%a1 a2 a3 a4 a5 a7 are high temperature coefficients
%a8 a9 a10 a11 a12 a14 are low temperature coefficients
for i= 1:length(temperature_range)
if temperature_range(i)<=temp(2)
S(i)= R.*((a8.*log(temperature_range(i))) + (a9.*temperature_range(i)) + (a10.*((temperature_range(i).^2)/2)) + (a11.*((temperature_range(i).^3)/3)) + (a12.*((temperature_range(i).^4)/4)) + a14);
else
S(i)= R.*((a1.*log(temperature_range(i))) + (a2.*temperature_range(i)) + (a3.*((temperature_range(i).^2)/2)) + (a4*((temperature_range(i).^3)/3)) + (a5.*((temperature_range(i).^4)/4)) + a7);
end
end
Explanation of function Entropy (S) code :-
- We will find the Entropy by creating Entropy function and assigning its related formula coefficent from 14
temperature coefficients, Universal Gas constant, temp and temperature_range. - We have used for loop because we have to iterate till the length of temperature_range.
- Further, we made an condition using if loop and make the temperature_range(i) less than and equal to local mid temperature( and since local mid temperature is second value in temp array so temp(2) is used in this function) to specify lower temperature coefficent in the formula.
- Then we will use else condition to specify Higher temperature coefficent in the formula. Then we ended the loop and function to calculate Entropy.
Code for function Molecular Weight:-
function[weight]= molecular_weight(name_of_species)
atomic_name= {'O','H','C','N','Ar'};
atomic_weight= [15.999 1.008 12.011 14.007 39.948]; % Atomic weight
% Molecular weight starting range
weight=0;
for i= 1:length(name_of_species)
for j= 1:length(atomic_name)
if strcmp(name_of_species(i),atomic_name(j))
weight= weight + atomic_weight(j);
x= j;
end
end
% Now we will find if there is more element in the species then incrementing
% atomic weight of the species
y= str2num(name_of_species(i));
if y>1
weight= weight + atomic_weight(x)*(y-1);
end
end
end
Explanation of function Molecular weight code :-
- Firstly we create an molecular_weight functionW and assigning name_of species as input to that.
- Then define the main element of all 53 nos. of species in an array atomic_name that are :- 'O','H','C','N','Ar'.
- Further with the help of above main elements, we will create atomic_weight array by searching atomic weight of those elements online and same has been assigned respectively in the atomic _weight array.
- Then, we have assigned moleuclar weight starting range as (weight=0) W and then created an for loop condition which runs to the length of the (name_of_species) and an another nested for loop is created which runs to the length of the string atomic_name.
- Thereafter, we made a condition using if condition loop and use strcmp command to compare the name_of_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 variable y. Then we run an condition of if loop to find whether the species contains more than one element i.e. y>1 then we will increment the atomic weight by the multiplication of variable x and y-1 to the atomic_weight array.
MAIN CODE :-
% Write a program for file parsing of NASA thermodynamics data
clear all;
close all;
clc;
f1 = fopen('THERMO.dat','r');
line1 =fgetl(f1); % For skipping first line
line2 =fgetl(f1);
% Since the values of global temperature values stored in single string,
% hence we have used string split command
global_temp_values =strsplit(line2,' ');
% For converting the values of string into numbers, we used str2num command
global_low_temperature = str2num(global_temp_values{2});
global_mid_temperature = str2num(global_temp_values{3});
global_high_temperature = str2num(global_temp_values{4});
% Defining the input value of universal gas constant in KJ/mol-K
R=8.314;
% Since line 3rd to line 5th contains comment hence we have skipped
line3 =fgetl(f1); % For skipping third line
line4 =fgetl(f1); % For skipping fourth line
line5 =fgetl(f1); % For skipping fifth line
for i=1:53
complete_string_of_species=fgetl(f1); % Reading sixth line
% Extracting the name of species
% Splitting of all values in a string into cell array
cellarray_of_entire_species=strsplit(complete_string_of_species,' ');
name_of_species=(cellarray_of_entire_species{1}); % To read the name of species
% Extracting local temperature values
local_low_temp=str2num(cellarray_of_entire_species{end-3});
local_mid_temp=str2num(cellarray_of_entire_species{end-1});
local_high_temp=str2num(cellarray_of_entire_species{end-2});
% Storing local temperature in a variable
temp = [local_low_temp local_mid_temp local_high_temp];
% Defining local temperature range
temperature_range=linspace(local_low_temp,local_high_temp,1000);
% Reading the 2nd line of species block
second_line_of_species_block=fgetl(f1);
a=findstr(second_line_of_species_block,'E'); %Finding string 'E'
% Extracting the higher and lower temperature coefficients
% Extracting higher temperature coefficients
a1=second_line_of_species_block(1:a(1)+3);
a1=str2num(a1);
a2=second_line_of_species_block(a(1)+4:a(2)+3);
a2=str2num(a2);
a3=second_line_of_species_block(a(2)+4:a(3)+3);
a3=str2num(a3);
a4=second_line_of_species_block(a(3)+4:a(4)+3);
a4=str2num(a4);
a5=second_line_of_species_block(a(4)+4:a(5)+3);
a5=str2num(a5);
% Reading third line of species block
third_line_of_species_block=fgetl(f1);
b=findstr(third_line_of_species_block,'E'); %Finding string 'E'
a6=third_line_of_species_block(1:b(1)+3);
a6=str2num(a6);
a7=third_line_of_species_block(b(1)+4:b(2)+3);
a7=str2num(a7);
% Finding lower temperature coefficients
a8=third_line_of_species_block(b(2)+4:b(3)+3);
a8=str2num(a8);
a9=third_line_of_species_block(b(3)+4:b(4)+3);
a9=str2num(a9);
a10=third_line_of_species_block(b(4)+4:b(5)+3);
a10=str2num(a10);
%Reading fourth line of species block
fourth_line_of_species_block=fgetl(f1);
c=findstr(fourth_line_of_species_block,'E');
a11=fourth_line_of_species_block(1:c(1)+3);
a11=str2num(a11);
a12=fourth_line_of_species_block(c(1)+4:c(2)+3);
a12=str2num(a12);
a13=fourth_line_of_species_block(c(2)+4:c(3)+3);
a13=str2num(a13);
a14=fourth_line_of_species_block(c(3)+4:c(4)+3);
a14=str2num(a14);
% Calculation of Specific Heat
Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,temperature_range,R);
% Calculation of Enthalpy
H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,temperature_range,R);
% Calculation of Entropy
S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,temperature_range,R);
% Calculation of Molecular Weight
weight = molecular_weight(name_of_species);
% Saving molecular weight of each species in a file
molecular_weight_file = fopen('Molecular Weight.txt','a+');
fprintf(molecular_weight_file,'Molecular weight of %s is %f ',name_of_species,weight);
fclose(molecular_weight_file); % Closing the file
% Plots of Specific heat, Enthalpy and Entropy with temperature
% Making a new folder for each species
cd('C:/Users/parab/OneDrive/Documents/MATLAB/Species'); % Changing directory
mkdir(name_of_species);
cd(name_of_species);
% Plot1 - Specific Heat vs Temperature range
figure(1);
plot(temperature_range,Cp,'linewidth',3,'color','r');
xlabel('Temperature [K]');
ylabel('Specific Heat [KJ/Kg-K]');
title(sprintf('Specific Heat vs Temperature range of %s',['Species name = ',name_of_species,' & Molecular weight = ',num2str(weight)]));
grid on;
saveas(figure(1),'Specific Heat.jpg');
% Plot2 - Enthalpy vs Temperature range
figure(2);
plot(temperature_range,H,'linewidth',3,'color','k');
xlabel('Temperature [K]');
ylabel('Enthalpy [KJ]');
title(sprintf('Enthalpy vs Temperature range of %s',['Species name = ',name_of_species,' & Molecular weight = ',num2str(weight)]));
grid on;
saveas(figure(2),'Enthalpy.jpg');
% Plot3 - Entropy vs Temperature range
figure(3);
plot(temperature_range,S,'linewidth',3,'color','m');
xlabel('Temperature [K]');
ylabel('Entropy [KJ/K]');
title(sprintf('Entropy vs Temperature range of %s',['Species name = ',name_of_species,' & Molecular weight = ',num2str(weight)]));
grid on;
saveas(figure(3),'Entropy.jpg');
cd('C:/Users/parab/OneDrive/Documents/MATLAB'); %Changing directory to original path
% printing the Molecular Weight of species
sprintf('molecular weight of %s is %f',name_of_species,weight)
end
Explanation of main code:-
- Firstly, we will open a THERMO.dat file and reading this file using fopen command.
- Then, we will read first THERMO using fgetl command and then we read global temperature values which were on second line and we will split the global temperature values using strsplit command to convert it into cell array and then we will convert this string to number using str2num command to find global_low_temperature, global_mid_temperature and global_high_temperature.
- Further, we have initialized the input value of universal gas constant (R=8.314) in KJ/mol-K.
- Since, line 3 to line 5 in the THERMO.dat file are comment lines, hence we used fgetl command to skip those lines.
- Now, we will run the loop to find 53 no. of species in the data file by assigning fgetl command and then spliting into the cell array string named cellarray_of_entire_species to find the species line and then find the species name and then find the ,local low, mid and high temperature values in that species line.
- Thereafter, we have stored the local temp values in temp array. Further, we have used local_low_temperature and local_high_temperature values to array another array called temperature_range using linspace command.
- To read the second_line_of_species_block, we have used fgetl command.
- In order to read the coefficient in the data file we will first find the 'E' string using strfind command to find before and after numbers of the coefficients then we will find remaining 14 coefficients and convert them to numbers from strings.
Out of which we will find first 7 Coefficients which are for Higher temperature coefficients and then Second 7 Coefficients which are for Lower temperature coefficient. - Now, we will specify Specific heat Cp, Entropy S, Enthalpy H functions to find these parameters using these 14 coefficients for Lower and higher Temperature coefficient. Further, we have specify the molecular weight function to find the Molecular weight of the different species.
- Now we will write the Molecular Weight.txt file and fprintf all the molecular weights of the species with following syntax;
- molecular_weight_file = fopen('Molecular Weight.txt','a+') used instead of molecular_weight_file = fopen('Molecular Weight.txt','w') syntax because it only contains molecular weight of final species value. So, I need to keep the file as it was because I am adding new info. at the end of the file periodically and then we close the text file by using fclose command.
- Now we change the directory using cd command and then creating directory folder named Species where we have to save our files using mkdir command and then we will change the directory to this created folder.
- Now, we will plot Specific heat, Entropy and Enthalpy with the temperature_range variable for different species. We we will use sprintf command to in the title command to create different species title, name and their molecular weight.
- Then we will use saveas command to save the plots of different species in the directory folder. Now we will change the
directory to which our main code file runs in the MATLAB. - Further, we tried to print the molecular weight of species along with their name on the command window by using following syntax;
- sprintf('molecular weight of %s is %f',name_of_species,weight)
Error Faced during the challege :-
The below error has been corrected by deletion of one extra bracket. After deletion of extra bracket, program executed properly.

Output of Molecular Weight of all the species shown on the command window is as under;















Screenshot of folder containing all plots of species as under;

Screenshot of Enthalpy, Entropy and Specific heat plots of O2 saved in folder species:-

Output Plot of O2, N2 and CO2:-
1. O2
i) Entropy vs Temperature Plot

ii) Enthalpy vs Temperature Plot 
iii) Specific Heat vs Temperature Plot 
2) N2 :-
i) Entropy vs Temperature Plot

ii) Enthalpy vs Temperature Plot 
iii) Specific Heat vs Temperature Plot

3) CO2
i) Entropy vs Temperature Plot

ii) Enthalpy vs Temperature Plot

iii) Specific Heat vs Temperature Plot

Drive folder link of all plots of 53 nos. of species :-
https://drive.google.com/drive/folders/1hbB0yaEsrDc4DL4YXILaGxwDdrxwGvC4?usp=sharing
Drive link of molecular weight of all species:-
https://drive.google.com/file/d/15BeLypbdgHHT1Fd9U2M7U9ovvi2UChLG/view?usp=sharing
Conclusion :-
1. 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.
2. 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.
Reference :-
1. https://en.wikipedia.org/wiki/Parsing.
2. https://in.mathworks.com/matlabcentral/answers
3. https://www.mathworks.com/help/matlab/ref/cd.html
4. Textbook of Engineering Thermodynamics by P K Nag