Project 1 - Parsing NASA thermodynamic data
#9 /PROJECT 1 – Parsing NASA thermodynamic data.
AIM:- Write a MATLAB Program that extracts the 14 co-efficient and calculates the Enthalpy, Entropy and Specific Heats for all the species in the data file – NASA Thermodynamic Data.
OBJECTIVE:-
- To 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.
- To calculate the molecular weight of each species and display it in the command window.
- To plot the Enthalpy, Entropy and Cp for the local temperature range specific for each species save the plots as images with appropriate names and dump them in separate folders for each species with suitable name.
- Plots for o2, N2 & Co2 species Enthalpy, Entropy and Cp for the local temperature range.
INTRODUCTION:-
File Parsing :- Parsing a file means reading in a data stream (or any text file), and further can be used in Programming or representation of the semantic content of that data, in order to facilitate performing some kind of transformation on the data.
In this project, we extract the co-efficient of polynomials that are stored in NASA’s Thermodynamics data file by performing file parsing and use this data in our program to calculate the thermodynamics properties like Specific heat(Cp), Enthalpy(H) and Entropy(S) using the following Equations:-

Where Cp=Specific Heat, H= Enthalpy, S= Entropy, T= Temperature, R= Universal Gas Constant,
‘a1,a2,a3,a4,a5,a6,a7’ are co-efficient of polynomial which we need to extract from the data file.
THEORY:-
Thermodynamic Properties – [Enthalpy, Entropy & Specific Heat]
Enthalpy (H) : Enthalpy is a property of a thermodynamics system, defined as the sum of the system’s internal energy and the product of it’s pressure and volume.
Entropy (S) : Entropy, is the measure of a systems 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.
Specific Heat (Cp) : Specific heat capacity is defined as the amount of heat required to raise the temperature of 1 kilogram of a substance by 1 kelvin (SI unit of specific heat capacity ‘J kg-1 K-1’).
METHODs / COMMANDs Used:-
Commands used in Program > In order to extract the required data from a given file, we first open the file and save the data into the variables.
- Thermo_file = fopen(‘THERMO.dat’, ‘r’);
Here, I use ‘fopen’ command to open & ‘r’ for read “THERMO.dat” file and save it in a variable ‘Thermo_file’.
As, there are number of string and number, we need to assign these to different variables, so that can use them as per requirement.
- To read data line by line, we use ‘fgetl’ command,
L = fgetl (‘Thermo_file’); This command read every next line. By using this, we read a file line by line and can save every line in a particular variable.
- To make the program more generic and useful, I use ‘while’ loop command, in such a way that it compare all the line, to reach the ‘END’ of the line. By doing so, if more species gets added in future to the file, program will calculate the number of species and their co-efficient automatically.
- ‘strcmp’ – Use this command to compare all the lines to get the ‘END’ character in the data sheet.
- ‘frewind’(fileID) – Use this command to reach the beginning of ‘file.strcmp’.
- ‘strsplit’ – To split into separate quantity which can be store in variable & do process.
- ‘findstr’ – use to find the location of a particular character from a string.
- ‘str2num’ – this command is used to convert the string into a number.
- ‘linspace(x1,x2,n) – generates n points between x1,x2
Use this command to generate the 1000 values between Local Minimum Temperature and Local High Teperature.
- ‘for’ – for loop to repeat specified number of time.
- ‘mkdir’ – use this command to create a folder, as we required to create a folder, so that we can save all the images of the plot to a specific folder. I put the folder name as ‘species’ to save images into it.
- ‘cd’ – use this command to change directory(cd), it changes the current folder to new Folder, to save the different files to different folder.
- ‘saveas’ – This command is used to save the plot as ‘.jpg’ format.
CODE EXPLANATIONs / PROCEDURE :
Start programming in MATLAB with these three commands, as all the previous calculations are erased.
Clear all: for clears all the data stored in the workspace previously.
Close all: for closes all the previous figures and plots.
clc: for clears the command window.
- Step1: First open the Thermodynamic data file using ‘fopen’ command and save it’s line in a specific variable.
Then use ‘while’ command next, in such way that it compare all the line by line to get the ‘END’ character, so that if more species data added to file, the program can for it.
Then calculate the number of species and save it in variable ‘num_species’.
& for Rewind the process for multiple iterations from 'Thermo_file' using 'frewind' command.
Step2: In order to extract the data for the Global (Min,Mid,High) Temperature, use ‘split’ command to split the line into separate quantity and save it into a variable. After that use ‘fgetl’ command to skip the comment lines, which are not useful.
Step3: same process of splitting use in next line to split the line for local minimum, local medium, local high temperature.
Then use ‘linspace’ command to get 1000 values between local low and local high temperature ranges.
Step4: ‘for Extracting the coefficient of species’
The logic use to extract the coefficient of species is, I analysis ‘Thermo.data’ and find that ‘Exponential (E)‘ is a scientific character and presents in every coefficients. So I locate the position of ‘E’ in coefficient by use ‘findstr’ command.
By doing so, I can locate character ‘E’ and what’s its left and right side, is the coefficient of species, which we can convert into number from string by using ‘str2num’ command.
Hence, by doing so we will get all the 14 coefficient of any species, which we save in a particular variable line by line.
Step5: here created a directory using ‘mkdir’ command in my system to save all the plot of different species in particular folder.
Step6: ‘Calculating specific heat, enthalpy & entropy for different temperature range’
We write a equations to find specific heat, enthalpy & entropy for different temperature range and plot the graph for them.
Equation:

Here, in the equation, put the value of coefficient and temperature, in ’for’ loop.
Also use ‘if’ & ‘else’ commands inside from loop, so that if temperature is smaller that local medium temperature it will take first seven coefficient, and if temperature is greater than local medium temperature it will take next 7 coefficient.
Also put the value of ‘Universal Gas Constant (R)’ =8.3145
Step7: After calculating , ‘plot’ command is used to plot ‘T vs Cp’, ‘T vs H’, ‘T vs S’, and save those plot as ‘.jpg’ format by using ‘saveas’ command in the system directory by using ‘cd’ command. Saving all plots for each species in directory in different folder.
Step8: ‘Calculation for Molecular Mass of species’-
Create two array to save atoms name and next line put the all values for all atom weight in same order as atom name.
To calculate the molecular mass we use nested ‘for’ loop initialized by variable ‘mol_mass’=0.
First ‘for’ loop run for total length of species and second ‘for’ loop run for total length of atom weight.
Then use ‘strcmp’ command to compare each character of species with the atom array, in ‘if’ command in such a condition that it calculate the molecular mass by adding molecular mass (initialize as 0) with atomic weight of each species.
‘mol_mass = mol_mass + atom_wt(n)’
-while comparing if we get a numerical value in species name, we save it in different variable x.
-Outside our for loop , to get the exact numerical value in species we use the ‘str2double’ command and convert it from string to number, & save it in new variable z.
-After getting the numeric value in species, in order to calculate the molecular mass, we use ‘if’ command, so that if the value is greater than 1, it will take the 1st character to add multiple time as that the numeric value in atomic weight-
Mol_mass=mol_mass + [atom_wt(x) * (z-1)]
[//note: this ‘nested for’ loop runs in such a way that if there is any numeric value after the any character in species name, then it will extract that value & convert into number and add that times of previous character in the formula and calculate the molecular mass of the species., like- for ‘N2’, it extract 2 from string & convert into a number and add N + N, to get the atomic weight of N2.//]
Step9: After getting the molecular mass, use ‘fprintf’ to print by using ‘%s’ and ‘%f’, we print the string and floating values of molecular mass. & 'cd..' for save those all data seperately one by one for better understanding.
MATLAB CODE : for Parsing NASA Thermodynamic Data.>>
% File Parsing to read NASA Thermodynamic data file
clear all
close all
clc
% Open & read 'THERMO.dat'
Thermo_file=fopen('THERMO.dat','r');
L=fgetl(Thermo_file); % to read next line.
count=1;
%using while loop to comapre lines for 'END'.
while strcmp(L,'END')==0
count=count+1;
L=fgetl(Thermo_file);
end
num_species=53; % calculate & save number of species.
frewind(Thermo_file) % rewind the process for multiple iteration.
H1=fgetl(Thermo_file);
H2=fgetl(Thermo_file);
%split the line from spaces.
Global_temp=strsplit(H2,' ');
%saving the local Minimum / Midium / High values in particular variable
Global_min_temp=str2num(Global_temp{2});
Global_mid_temp=str2num(Global_temp{3});
Global_high_temp=str2num(Global_temp{4});
%using for loop to skip comment lines
for l=1:3
H1=fgetl(Thermo_file);
end
%Main program to extract coefficient of species,
%calculate specific heat|enthalpy|entropy for different temperature range,
%calculate the molecular mass of each species & print it here.
for s=1:num_species
L1=fgetl(Thermo_file);
L1_split=strsplit(L1,' ');
species=(L1_split{1});
%saving the values for Local Min,Mid,High temperature in a variable
local_min_temp=str2num(L1_split{end-3});
local_mid_temp=str2num(L1_split{end-1});
local_high_temp=str2num(L1_split{end-2});
%taking 1000 values between local minimum and local high temperature.
T=linspace(local_min_temp,local_high_temp,1000);
%read line by line for coefficient of species.
L2=fgetl(Thermo_file);
L3=fgetl(Thermo_file);
L4=fgetl(Thermo_file);
%locating the Exponential('E')character and extract all 14 coefficient.
%save all coefficients into a variable.
location_E_L2=findstr(L2,'E'); %locating the 'E' character in 1st line.
c1=str2num(L2(1 : location_E_L2(1) +3));
c2=str2num(L2(location_E_L2(1) +4 : location_E_L2(2) +3));
c3=str2num(L2(location_E_L2(2) +4 : location_E_L2(3) +3));
c4=str2num(L2(location_E_L2(3) +4 : location_E_L2(4) +3));
c5=str2num(L2(location_E_L2(4) +4 : location_E_L2(5) +3));
location_E_L3=findstr(L3,'E'); %locating the 'E' character in 2nd line.
c6=str2num(L3(1 : location_E_L3(1) +3));
c7=str2num(L3(location_E_L3(1) +4 : location_E_L3(2) +3));
c8=str2num(L3(location_E_L3(2) +4 : location_E_L3(3) +3));
c9=str2num(L3(location_E_L3(3) +4 : location_E_L3(4) +3));
c10=str2num(L3(location_E_L3(4) +4 : location_E_L3(5) +3));
location_E_L4=findstr(L4,'E'); %locating the 'E' character in 3rd line.
c11=str2num(L4(1 : location_E_L4(1) +3));
c12=str2num(L4(location_E_L4(1) +4 : location_E_L4(2) +3));
c13=str2num(L4(location_E_L4(2) +4 : location_E_L4(3) +3));
c14=str2num(L4(location_E_L4(3) +4 : location_E_L4(4) +3));
%to create a folder into the Directory.
mkdir(species)
%Universal Gas Constant(J K-1.mol-1)
R=8.3145;
%calculating Specific Heat|Enthalpy|Entropy for different temp. range.
for i=1:length(T)
if T(i)>local_mid_temp
%formula to calculate Specific Heat.
Cp(i)=R*(c1 + c2*T(i) + c3*T(i)^ + c4*T(i)^3 + c5*T(i)^4);
%formula to calculate Enthalpy.
H(i)=R*T(i)*(c1 + c2*T(i)/2 + c3*T(i)^2/3 + c4*T(i)^3/4 + c5*T(i)^4/5 + c6/T(i));
%formula to calculate Entropy
S(i)=R*(c1*log(T(i)) + c2*T(i) + c3*T(i)^2/2 + c4*T(i)^3/3 + c5*T(i)^4/4 + c7);
else
%formula to calculate Specific Heat.
Cp(i)=R*(c8 + c9*T(i) + c10*T(i)^2 + c11*T(i)^3 + c12*T(i)^4);
%formula to calculate Enthalpy.
H(i)=R*T(i)*(c8+c9*T(i)/2 + c10*T(i)^2/3 + c11*T(i)^3/4 + c12*T(i)^4/5 + c13/T(i));
%formula to calculate Entropy.
S(i)=R*(c8*log(T(i)) + c9*T(i) + c10*T(i)^2/2 + c11*T(i)^3/3 + c12*T(i)^4/4 + c14);
end
end
%plot for [(Specific Heat,Enthalpy,Entropy) vs Temperature.]
figure(1)
plot(T,Cp)
xlabel('Temperature [K]')
ylabel('Specific Heat [J/kg.K]')
title('Temperature vs Specific Heat')
figure(2)
plot(T,H)
xlabel('Temperature [K]')
ylabel('Enthalpy[KJ]')
title('Temperature vs Enthalpy')
figure(3)
plot(T,S)
xlabel('Temperature [K]')
ylabel('Entropy')
title('Temperature vs Entropy')
%saving plots in Directory in different folder.
cd(species)
saveas(1,'Specific Heat','jpg')
saveas(2,'Enthalpy','jpg')
saveas(3,'Entropy','jpg')
%calculation for Molecular Mass of species.
atom=['C','H','O','N','A']; %create array to save species character into a variable.
atom_wt=[12.01, 1.0079, 16, 14.0067, 39.95]; %create array to save atomic weight into a variable.
mol_mass=0;
%nested for loop in multiple iteration.
for m=1:length(species)
for n=1:length(atom_wt)
if strcmp(species(m),atom(n))
mol_mass=mol_mass+atom_wt(n);
x=n;
end
end
z=str2double(species(m)); %to convert species sting value into number.
if z>1
mol_mass=mol_mass+(atom_wt(x)*(z-1));
end
end
%print the floating value of molecular mass.
fprintf('molecular mass of %s is = %f n',species,mol_mass)
cd ..
end
OUTPUTs:
1.Result-1 // Plots for 'o2', 'N2', 'Co2' species --:>
>>Plot for 'O2' -:>
- fig1:plot for Temperature vs Specific Heat.> 'O2'>>

- fig2:plot for Temperature vs Enthalpy.> 'O2'>>

- fig3:plot for Temperature vs Entropy.> 'O2'>>

>>Plot for 'N2' -:>
- fig1:plot for Temperature vs Specific Heat.> 'N2'>>

- fig2:plot for Temperature vs Enthalpy.> 'N2'>>

- fig3:plot for Temperature vs Entropy.> 'N2'>>

>>Plot for 'CO2' -:>
- fig1:plot for Temperature vs Specific Heat.> 'CO2'>>

- fig2:plot for Temperature vs Enthalpy.> 'CO2'>>

- fig3:plot for Temperature vs Entropy.> 'CO2'>>

2.Result-2// Molecular weight for each species and displayed in 'Command Window' || 'Work Space' values :>


3.Result-3//Save the plots as images for all species in folder :>

:>>

ERRORs: I missed out to put an 'end' for closing the 'for' loop in line42 .

Solution: Analysis it from command window and put a 'end' command at the last to complete the 'for' loop & then run the code properly. Have to carefully tackle with syntax and variables to avoid any further mistakes!
CONCLUSIONs:
So, here successfully parse the data from the 'THERMO.dat' file. Then using that data we were able to get Specific Heat, Enthalpy, Entropy plots respect to Temperature. & save the each species figure files into a perticular directory using the code.
And we also get the molecular mass for all spcies.
REFERENCEs:
The Parts of the Periodic Table (angelo.edu)