Project 1 - Parsing NASA thermodynamic data
AIM:- To write a program in MATLAB to parse NASA thermodynamic data file and calculate the thermodynamic properties of various species.
OBJECTIVE:-
- To write a function that extracts the 14 co-efficient and calculates the Enthalpy, Entropy and Specific heat for all species ( which are present in data file).
- Calculating the Molecular weight of each species and displaying it in command window.
- Plotting Specific heat (Cp), Enthalpy (H) and Entropy (S) for local temperature range specific for each species.
- Save the plot as images with appropriate names and dump them in separate folders for each species with suitable name.
THEORY:-
introduction of file parsing
- We live in a digital world, generating unlimited amounts of data every minute. Many users have access to this raw data in different forms such as documents, images, videos, HTML pages, etc. The ability to extract essential information from this data will decide whether we can generate a compelling advantage for our work with this data.
- Use programming to convert information into another form that’s easier to work with. This is done by partially analyzing the data , understanding its underlying structure ( by making some assumption based on what you are expecting to see), and then explicitly representing that structure in the code.
- The aim of file parsing is to read stream of data and building an in memory model of the semantic content of this data.
- In general we can say file parsing is refer to how information can be read from a particular text file or, any type of file and how that information can be used in computer programming.
NASA thermodynamic file parsing
we have a file called ('THERMO.dat') in this file they documented the co-efficients that are require to evaluate following polynomials for 1000+ species.

by the use of following polynomials we are able to calculate Cp( specific Heat), H(Enthalpy), S(Entropy) for species.
- Specific heat(Cp) - Specific heat of a substance is the heat capacity of a sample of the substance divided by the mass of the sample, also sometimes referred to as massive heat capacity. Informally, it is the amount of heat that must be added to one unit of mass of the substance in order to cause an increase of one unit in temperature. The SI unit of specific heat capacity is joule per kelvin per kilogram, ( J.kg−1⋅K−1 )
calling function for specific heat
% function for specific heat
function Cp=Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,global_medium_temp )
if (T<=global_medium_temp)
Cp=R*( a8 + (a9*T) + (a10*(T).^2) + (a11*(T).^3) + (a12*(T).^4));
else
Cp=R*( a1 +(a2*T) + (a3*(T).^2) + (a4*(T).^3) + (a5*(T).^4));
end
end
- Enthalpy(H)- it is a property of a thermodynamic system; the sum of the system's internal energy and the product of its pressure and volume. It is a state function used in many measurements in chemical, biological, and physical systems at a constant pressure, which is conveniently provided by the large ambient atmosphere. SI unit of enthalpy is [ J/kg ].
calling function for Enthalpy
% function for Enthalpy
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_medium_temp)
if(T < global_medium_temp)
H = R*((a8*T) + ((a9*(T).^2)/2) + ((a10*(T).^3)/3) + ((a11*(T).^4)/4) + ((a12*(T).^5)/5) + a13);
else
H = R*((a1*T) + ((a2*(T).^2)/2) + ((a3*(T).^3)/3) + ((a4*(T).^4)/4) + ((a5*(T).^5)/5) + a6);
end
end
- Entropy(S)- Entropy is a scientific concept as well as a measurable physical property that is most commonly associated with a state of disorder, randomness, or uncertainty. SI unit of Entropy is [ J/kg.K]
calling function for entropy
% function for Entropy
function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,~,T,R,global_medium_temp)
if (T <= global_medium_temp)
S= R*(a8.*log(T) + (a9*T) + ((a10*(T).^2)/2) + ((a11*(T).^3)/3) + ((a12*(T).^4)/4));
else
S= R*(a1 .*log(T) + (a2*T) + ((a3*(T).^2)/2) + ((a4*(T).^3)/3) + ((a5*(T).^4)/4) + a7);
end
end
- Molecular weight- it is a measure of the sum of the atomic weight values of the atoms in a molecule.
calling function for Molecular weight
% Molecular Weight Calculation function
function MW =Molecular_weight(name_of_species)
Elements = ['H','C','O','N','A'];
Atomic_weight = [1 12 16 14 40 ];
MW=0;
for i=1:length(name_of_species)
for j=1:length(Elements)
if strcmp(name_of_species(i),Elements(j))
MW=MW+Atomic_weight(j);
k=j;
end
end
n=str2double(name_of_species(i));
if (n>1)
MW=MW+(Atomic_weight(k).*(n-1));
end
end
Main program
clear all
close all
clc
R = 8.314; % [J/(mol-K)]
f1 = fopen('THERMO.dat','r');
% to read lines
l1=fgetl(f1);
l2=fgetl(f1);
value=strsplit(l2,' ');
% TO gat global temp
global_low_temp=str2num(value{2});
global_medium_temp=str2num(value{3});
global_high_temp=str2num(value{4});
% temperature range
T=linspace(global_low_temp,global_high_temp,1000);
% skipping next 3 lines
l3=fgetl(f1);
l4=fgetl(f1);
l5=fgetl(f1);
% Reading species
for i=1:53
tline=fgetl(f1);
A= strsplit(tline,' ');
name_of_species=(A{1});
low_temp=str2num(A{end-3});
mid_temp=str2num(A{end-2});
high_temp=str2num(A{end-1});
% Finding 7 higher Temperature coefficients
tline1=fgetl(f1);
a=findstr(tline1,'E');
a1=tline1(1:a(1)+3);
a1=str2num(a1);
a2=tline1(a(1)+4:a(2)+3);
a2=str2num(a2);
a3=tline1(a(2)+4:a(3)+3);
a3=str2num(a3);
a4=tline1(a(3)+4:a(4)+3);
a4=str2num(a4);
a5=tline1(a(4)+4:a(5)+3);
a5=str2num(a5);
tline2=fgetl(f1);
a=findstr(tline2,'E');
a6=tline2(1:a(1)+3);
a6=str2num(a6);
a7=tline2(a(1)+4:a(2)+3);
a7=str2num(a7);
% Low temperature coefficients
a8=tline2(a(2)+4:a(3)+3);
a8=str2num(a8);
a9=tline2(a(3)+4:a(4)+3);
a9=str2num(a9);
a10=tline2(a(4)+4:a(5)+3);
a10=str2num(a10);
tline3=fgetl(f1);
a=findstr(tline3,'E');
a11=tline3(1:a(1)+3);
a11=str2num(a11);
a12=tline3(a(1)+4:a(2)+3);
a12=str2num(a12);
a13=tline3(a(2)+4:a(3)+3);
a13=str2num(a13);
a14=tline3(a(3)+4:a(4)+3);
a14=str2num(a14);
% Specific heat,Enthalpy,Entropy
Cp=Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,global_medium_temp);
H =Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_medium_temp);
S=Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,global_medium_temp);
% Calculating molecular weight and printing it
MW = Molecular_weight(name_of_species);
fprintf('molecular weight of %s=%fn',name_of_species,MW);
% PLOTTING CP,H,T %
cd('C:\Users\Devesh kashyap\OneDrive\Desktop\matlab png')
mkdir(name_of_species);
cd(name_of_species);
% plotting CP vs T
figure(1)
plot(Cp,T,'LineWidth',3,'color','r')
title(name_of_species)
xlabel(' Temperature(T) [K] ')
ylabel('Specific hest (cp) [J/kg.K]')
grid on
saveas(figure(1),'specific heat.jpg');
% Plotting H vs T
figure(2)
plot(H,T,'LineWidth',3,'color','g')
title(name_of_species)
xlabel('Temperature(T) [K]')
ylabel('Enthalpy(H) [kJ/kg]')
grid on
saveas(figure(2),'Enthalpy.jpg');
% Plotting S vs T
figure(3)
plot(S,T,'LineWidth',3,'Color','b')
title(name_of_species)
xlabel('Entropy(S) [kJ/kgK] ')
ylabel('Temperature(T) [K] ')
grid on
saveas(figure(3),'Entropy.jpg');
cd('C:\Users\Devesh kashyap\OneDrive\Desktop\temp_program')
end
Syntax used
- fopen- open a file.
- ‘r’ – reading the information.
- fgetl – Read line from file.
- strsplit – split string or character vector at specified delimiter.
- str2num- convert character array or string to numeric array.
- strfind – find one string within another.
- linspace- generate linearly spaced vectors.
- fprintf – write formatted data to file.
- pwd –identify current directory.
- Mkdir – make new directory.
- cd-change working directory.
Output command window:- (Molecular weight of all species displayed in command window)




here showing some warning just ignore it (we run program more than one time on same path)
PROGRAM EXPLANATION:-
- We start programing with clear all (clear workspace),close all (closes all plots) and clc(clear command window).
- Define the value of universal gas constant R (8.314[J/(mol-K)]).
- Use command 'fopen' to open the THERMO.dat file and read mode 'r'.
- Now we use 'fgetl' to start reading the lines and after reading the first two lines, from the second line and converting strings to numbers by using 'str2num' command and assigning as global low temperature, global medium temperature and global high temperature.
- Then we use command 'strsplit' to get a wide temperature range for plotting and calculation purpose.
- Now we use fgetl command to skipping next 3 lines.
- Now we use for loop to get 53 species and their temperatures (low,medium,high) and 14 ( first 7 are high temperature coefficients and next 7 are low temperature coefficients ).
- In for loop we again uses 'fgetl' command for reading particular line to species name and three temperature and stored it.
- Now we further use 'fgetl' command to extract 14 coefficients and store them.
- With the help of these 14 coefficients we call a function (we coded three - function for specific heat, function for enthalpy and function for Entropy) and use them in main program.
- We use molecular weight function ( we coded molecular weight function ) to calculate molecular weight of each species.
- Now we command to change the directory so that I can save the file at a particular location in our computer. We use 'mkdir' to create a new folder with the name of species and then a 'cd' command to make mkdir as the current directory.
- Now we use plot command to plot (temperature vs specific heat, temperature vs Enthalpy, Temperature vs Entropy).
- For each plot we use figure command to get three different plot ( T vs Cp, T vs H, T vs S) and name as figure(1), figure(2) and figure(3) and saveas command use to save these figure.
- The loop continue till we get all 53 species name and their ( Molecular weight Specific Heat, Enthalpy, Entropy ) after that loop get terminated.
File Attachment:-
screenshot where all the folders are saved.

Result:- here we plot some result from output folder
- plot for CO2 Temperature vs Specific heat

Temperature vs Enthalpy

Temparature vs Entropy

- plot for O2:- Temperature vs Specific heat

Temperature vs Enthalpy

Temparature vs Entropy

- plot for N2:- Temperature vs Specific heat
Temperature vs Enthalpy

Temparature vs Entropy

Errors:-
- typing error we write here same line for global_medium_temp and global_high_temp check and fix it.

- here we write wrong syntax fget1 in place of fgetl, check and fix it.

- here we write wrong function for specific heat check and fix it.

- here we doing same mistake for function(Entropy) code, check and fix it.

- we do one more mistake while writing function code for Molecular weight in line number 23 we miss dat(.)and closed bracket in place of (*),check and fix it.

CONCLUSION:-
we successfully parsed the given thermodynamic data file and plot the graph of (Cp vs T), (H vs T) and (S vs T) for the inputted species (53 species). parsing is very important part in programing.
References:
- https://in.mathworks.com/matlabcentral/answers/341599-create-folder-and-keep-files
-
https://in.mathworks.com/matlabcentral/answers/17580-how-to-define-a-path-in-saveas-command
- support engineer 'Gokul'.
Thank you