Parsing NASA Thermodynamic Data Using Matlab
AIM:
To write a code in MATLAB for parsing the NASA Thermodynamic data file and using this extracted data to calculate specific data, enthalpy, entropy and molecular mass of each species.
FILE PARSING:
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. Parsing, syntax analysis, or syntactic analysis is the process of analysing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a 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 a syntactically ambiguous input.
GOVERNING EQUATIONS:
For Specific Heat
`(Cp)/R=a_1+a_2*T+a_3*T^2+a_4*T^3+a_5*T^4`
For Enthalpy
`H/(RT)=a_1+a_2*T/2+a_3*T^2/3+a_4*T^3/4+a_5*T^4/5+(a_6)/T`
For Entropy
`S/R=a_1*log(T)+a_2*T+a_3*T^2/2+a_4*T^3/3+a_5*T^4/4+a_7`
Where,
T = Local Temperature
`a_1` to `a_7` = High Temperature coefficients
`a_8` to `a_14` = Low Temperature coefficients
Cp = Specific heat
H = Enthalpy
S = Entropy
R = Universal Gas Constant = 8.314 J⋅K−1⋅mol−1
OBJECTIVES:
- To extract data from the given NASA Thermodynamic data file. (link for data file- https://drive.google.com/file/d/1AikCHDlz9s_fu8whE81qee_GD4AyYpCP/view)
- To find Specific Heat, Enthalpy and of each species at different local temperatures.
- To find a logic to calculate Molecular Weight of each species.
- To plot ‘Cp’ vs ‘T’ , ‘H’ vs ‘T’ and ‘S’ vs ‘T’ graphs using the calculation results.
- To save the graphs of each species in a folder.
PROGRAM:
CODE:
close all
clear all
clc
% To open the data file
f1 = fopen('THERMO.dat','r');
%Reading Heading line
Heading = fgetl(f1);
%Reading Global Temperature line
tline = fgetl(f1);
temp=strsplit(tline,' ');
global_low_temp = str2num(temp{2});
global_mid_temp = str2num(temp{3});
global_high_temp = str2num(temp{4});
%Reading comment lines
comment1 = fgetl(f1);
comment2 = fgetl(f1);
comment3 = fgetl(f1);
% To Extraxt coefficients of species and doing calculation
for i= 1:53
species_line = fgetl(f1);
species_name =strsplit(species_line,' ');
%Finding Coefficients for each Species by reading each line
first_line= fgetl(f1);
a=findstr(first_line,'E');
second_line = fgetl(f1);
b=findstr(second_line,'E');
third_line= fgetl(f1);
c=findstr(third_line,'E');
%Coefficients are:-
C1 = str2num(first_line(1:a(1)+3));
C2 = str2num(first_line(a(1)+4:a(2)+3));
C3 = str2num(first_line(a(2)+5:a(3)+3));
C4 = str2num(first_line(a(3)+4:a(4)+3));
C5 = str2num(first_line(a(4)+5:a(5)+3));
C6 = str2num(second_line(1:a(1)+3));
C7 = str2num(second_line(a(1)+4:a(2)+3));
C8 = str2num(second_line(a(2)+5:a(3)+3));
C9 = str2num(second_line(a(3)+4:a(4)+3));
C10 = str2num(second_line(a(4)+5:a(5)+3));
C11 = str2num(third_line(1:a(1)+3));
C12 = str2num(third_line(a(1)+4:a(2)+3));
C13 = str2num(third_line(a(2)+5:a(3)+3));
C14 = str2num(third_line(a(3)+4:a(4)+3));
%Universal gas constant
R = 8.314;
%Extracting local temperatures for each Species line
local_low_temp=str2double(species_name{end-3});
local_mid_temp=str2double(species_name{end-1});
local_high_temp=str2double(species_name{end-2});
T = linspace(local_low_temp,local_high_temp,150);
%Calculating Cp,H,S
if T < local_mid_temp
Cp = R*(C8 + C9.*T + C10.*T^2 + C11.*T^3 + C12.*T^4);
H = (C8 + C9.*T/2 +(C10.*T.^2)/3 +(C11.*T.^3)/4 + (C12.*T.^4)/5 +C13./T)*R.*T;
S = (C8.*log(T) + C9.*T + (C10.*T.^2)/2+ (C11.*T.^3)/3 + (C12.*T.^4)/4 + C14)*R;
else
Cp = R*(C1 + C2.*T + C3.*T.^2 + C4.*T.^3 + C5.*T.^4);
H = (C1 + (C2.*T)/2 +(C3.*T.^2)/3 +(C4.*T.^3)/4 + (C5.*T.^4)/5 +C6./T)*R.*T;
S = (C1.*log(T) + C2.*T + (C3.*T.^2)/2+ (C4.*T.^3)/3 + (C5.*T.^4)/4 + C7)*R;
end
%Converting Species Name cell array into matrix
str = cell2mat(species_name(1));
% Introducing the element intials for string comparison and their weights for molecular weight calc.
s = ["H" "O" "C" "N" "A"];
atomic_weight = [1,16,12,14,40];
Mol_Weight=0;
for j=1:length(str);
for k=1:length(atomic_weight)
if strcmp(str(j),s(k));
Mol_Weight = Mol_Weight + atomic_weight(k);
position = k;
end
end
% Calculating Molecular Weight of each species
number = str2double(str(j));
if number > 1
Mol_Weight = Mol_Weight + atomic_weight(position)*(number-1);
end
end
M=Mol_Weight
fprintf("The molecular weight of %s is %d g/mol",str,M)
%Providing path for saving data
targetdirectory = 'C:UsersI S RAODesktopMatlab Programsparsing_results';
foldername = fullfile(targetdirectory, sprintf('%s',str));
mkdir(foldername);
%Going in to the folder of the species to save the plots
cd(foldername)
%Plotting
figure(1)
plot(T,Cp)
title(['Specific heat vs temperature plot for ',str])
xlabel('Temperature')
ylabel('Specific heat')
saveas(gcf, 'Cp.png');
figure(2)
plot(T,H)
title(['Enthalpy vs temperature plot for ',str])
xlabel('Temperature')
ylabel('Enthalpy')
saveas(gcf, 'H.png');
figure(3)
plot(T,S)
title(['Entropy vs temperature plot for ',str])
xlabel('Temperature')
ylabel('Entropy')
saveas(gcf, 'S.png');
cd ..
end
EXPLANATION:
- Here, first the commands 'close all','clear all','clc', are used to close any previous plots and to clear the workspace and command window respectively.
- To read the NASA data file , first we have to open the data file, so open and read the file, we use a command ‘fopen()’, where we mention the file which has to be opened and whether we want to read that file or write in that file. Here ‘r’ means to read and ‘w’ means to write.
- Since here we have to read the file, inside the brackets of ‘fopen’ command we have given ‘THERMO.dat’ and ‘r’. This command is assigned to a variable f1.
- Now to read the first line of the NASA data file we use a command ‘fgetl()’. This command helps us to read each line of the data file individually. If we repeat this command again, then it will read next line after the previous read line by previous ‘fgetl’ command, i.e. when we type ‘fgetl’ command first time it will read first line and if we again type ‘fgetl’ it will read the second line and again if we type ‘fgetl’ command it will read third line and so on.
- So for reading each line ‘fgetl’ command is used. For example if we have to read 12 lines then we have to type ‘fgetl’ command 12 times. Then only we will be able read all the 12 lines.
- So to read the heading line i.e. first line of the NASA data file, first ‘fgetl’ is used in the above code. This first ‘fgetl’ command is assigned to a variable ‘Heading’.
- Then to read the next line i.e. global temperature line in the data file , we have typed the second ‘fgetl’ command to read that temperature line and assigned it to a variable ‘tline’.
- Now whenever we used ‘fgetl’ command if gives us output as string , so since here our global temperature line is a string we can’t use it for any calculations .
- In order to convert this string into an array we have followed a logic here. Now when we observe the temperature string we can see that each value is separated by space. Now we use this space to separate these values.
- Here to separate the values we have used a command ‘strsplit()’. This command here takes the inputs as the line that has to be split and at which part of the line it has to be splited. In the above as mentioned before we are splitting the line where the space is there so as input we have given line that has to split as ‘tline’ and it has to be split at space so we have enter second input as ‘ ’ i.e space in inverted commas.
- Once we execute this here we will get a cell array where each value has been separated in the string. Now cell array means that it is a data type with indexed data containers called cells, where each cell can contain any type of data. Cell arrays commonly contain either lists of character vectors of different lengths, or mixes of strings and numbers, or numeric arrays of different sizes. This cell array is assigned to a variable ‘temp’.
- Once we have obtained cell array , still these values are characters not numbers , so convert them into number array we have used a command ‘str2num()’, which converts a string value into number. Now inputs for this command are, which character has to be change to number. In our code the from the cell array we need to convert ‘300’ , ‘1000’, ‘5000’ into number array. So in the ‘str2num’ we have given the position of these characters and assingned them to variables ‘global_low_temp’ , ‘global_mid_temp’ and ‘global_high_temp’ respectively. Since we are giving position in of characters in a cell array we have use {} (curle brackets) in the command. We can’t use general brackets that we use normally to define the index or position of an element.
- Next we have entered ‘fgetl()’ command three times each one is assigned to variables ‘comment1’ , ‘comment2’ , ‘comment3’. This is just to read the next three line that are comments in the data file.
- After the comments lines information regarding species are there in the data file. Now to extract all these information, we have first extracted information of first species then, for this logic we applied FOR LOOP so that for all 53 species we can extract data easily.
- Now logic for extracting the coefficients of specie is, first using ‘fgetl’ command species first line is read, and it is assigned to a variable ‘species_line’. Then by using ‘strsplit()’ command the values in the species line are split and it is assigned to a variable ‘species_name’.
- Then in the data file next line after the species line is the first line of coefficients, so to read this line again ‘fgetl()’ is used.
- Now to get each coefficient individually, first what we have done is that we have identified the position of ‘E’ in the first line string, to identify this ‘E’ we have used a command ‘findstr()’. For this command, inputs should be given as string in which we are finding and what we are finding in that string.
- So in our code as mentioned above we are finding ‘E’ in the first line of coefficients, so we have given them as input to the ‘findstr()’ command.

FIGURE.1
- Above figure shows us the results we got in command window after executing code till here.
- When we execute the command this command will give us the position of ‘E’, i.e. at which positions in the string ‘E’ is present. By using the position of ‘E’ we can find the coefficient by defining the positions before and after ‘E’.
- Thus by following this logic we have found each coefficient and converted them into number using ‘str2num()’ command. Each coefficient in the first line is assigned to a variable C1, C2, C3, C4, C5.
- Similar logic is applied for extracting coefficients from second and third line in the data file.
- Since we have use FOR LOOP, so every time loop is executed automatically it will extract coefficients of each species when each time FOR LOOP is executed and variables are assigned accordingly.
- Now once the coefficients are extracted these are used for calculating specific heat, enthalpy and entropy.
- Before entering formulas for specific heat, enthalpy and entropy, universal gas constant value is entered and assigned to a variable ‘R’.
- Now for each species in the data file there are separate local temperatures. These temperature ranges should be used to calculate specific heat, enthalpy and entropy.
- Now to extract this temperature value, we have followed same process what we followed to extract global temperature values. To get the local temperatures we have used command ‘str2double()’ command instead of ‘str2num()’ command, this ‘str2double’ command converts the strings in the cell array of strings to double precision. Its inputs are same that of ‘str2num’ command.
- Since local temperature are at the end in the ‘species_name’ line, so the inputs are given accordingly to convert them into cell array and then to number array.
- Then we have used ‘if ’ statements to find specific heat, enthalpy and entropy at low temperatures and high temperatures. Since first 7 coefficients are high temperature coefficients and next 7 coefficients are low temperature coefficients.
- So ‘if ’ statement executes first set formulas if temperatures are less than mid temperature and if temperature is higher than mid temperature then it will execute second set of formulas.
- Formulas for specific heat, enthalpy and entropy are assigned to variables ‘Cp’, ‘H’ and ‘S’ respectively.
- Now to calculate molecular weight of each species, logic followed is, first in the ‘species_name’ line’s first character is name of the species and this name of species is converted into matrix using ‘cell2mat()’. Here this commandconverts a cell array into an ordinary array. The elements of the cell array must all contain the same data type, and the resulting array is of that data type. Since ‘species_name’ is a cell array as per the code and first position of this ‘species_name’ cell array is the name of the species, so first position of cell array is given as input to the ‘cell2mat()’ command to covert that first position into an matrix array. By doing this we can use this for our calculations for finding molecular weight. This command is assigned to a variable ‘str’.
- If we observe the data file, every species comprises of carbon, hydrogen, oxygen, nitrogen and argon, so using these we will compare the species with them and see what are the elements that each specie consists of, and then adding up atomic weight of each element to find molecular weight of the species.
- So first we have created a string that comprises of above elements first letter. It is assigned to a variable ‘s’. Then an array is created comprising of each of the above element’s atomic mass values. This arraty is assigned to a variable ‘atomic_weight’.
- A variable ‘Mol_Weight’ is assigned and its value is given 0 initially. Then a FOR LOOP is used in which the number of characters in a specie’s name is taken as length and then again in another FOR LOOP length of ‘atomic_weight’ array is taken and for each value of ‘str’ , each value of ‘atomic weight’ is taken.
- Then an ‘if ’ statement is used with a command ‘strcmp()’ which compares the characters entered in it as input and if both the characters match with each other then it give output as 1 that means it will allow the next line to execute in the code which is typed after ‘if ’ statement and if the characters do not match then it will return 0 as output that means ‘if ’ statement will stop the code to execute further and the code go back to repeat the FOR LOOP with new values. It acts like a gateway.
- If the ‘if ’ statement is satisfied then the atomic weight is added to the initial molecular weight and new molecular weight is obtained, now this molecular weight is the initial molecular weight for the next iteration in the FOR LOOP.
- Then another ‘if ’ statement is enter as in some species more than one same elements are present. So to calculate that also another ‘if ’ statements is used. Now in ‘str’ numbers are present when same element is more than one in a single species. When these number comes in an iteration, the first ‘if ’ statement will not pass this to execute the line inside this ‘if ’ statement.
- Then the second ‘if ’ statement is checked ,before going to second ‘if ’ statement this number is a character datatype so it is changed to number using ‘str2double()’. This is assigned to a variable ‘number’. After converting, the ‘if ’ statement is checked and if the statement is satisfied ‘Mol_Weight’ is further calculated and new value is stored. Thus giving us the final molecular weight for that particular iteration.
- This new value of ‘Mol_Weight’ is then taken as initial molecular weight for next iteration and this continues until atomic weights of all elements in the species are added up and final molecular weight is obtained.
- FOR LOOP is ended and final molecular weight is calculated and this molecular weight is assigned to a variable ‘M’.
- Then ‘fprint()’ command is used to print a text statement in the command window that tell us the molecular weight of each specie.
- Next to save the graphs first a path where these graphs are to be saved is entered first and then ‘fullfill()’ command is used to save a folder with specified name and its path is given as input.
- Then ‘mkdir(foldername)’ command is used to create the directory ‘foldername’ in the current directory. Then using command ‘cd(foldername)’ the current working directory is set to directory that we opened using ‘mkdir’.
- Then plotting is done using commands ‘plot()’ , ‘title()’ , ‘xlabel()’ , ‘ylabel()’ and ‘saveas()’.
- Here each plot is separated with ‘figure()’ command and the ‘saveas()’ command saves the each figure in the folder that we opened before, we entered before plotting commands and also it saves with the name of file that we have mention for each graph in that ‘saveas()’ command.
- Finally ‘cd..’ is used to close the opened folder. Thus creating each folder for each species and each species having three graphs in their individual folder.
- Then the main FOR LOOP is ended and thus coefficients of each specie is extracted and used to calculate specific heat, enthalpy, entropy and molecular weight .

FIGURE.2

FIGURE.3
FIGURE.4
|
FIGURE.5 |
FIGURE.6 |
FIGURE.7 |
|
FIGURE.8 |
FIGURE.9 |
FIGURE.10 |
|
FIGURE.11 |
FIGURE.12 |
FIGURE.13 |
- In figure.2 we can observe that molecular weight of each species is calculated and displayed in command window.
- In figure.3 we can observe that each folder with species name is created in the path which we defined in the code.
- In figure.4 we can observe that graphs for each species are saved in each species folder and each graph’s name displayed is same as what we have defined in the code.
- In figure.5 to figure.13 we can observe curves in each graph and how specific heat, enthalpy and entropy are changing with change in temperatures for each species.
- Similarly respective curve graphs are saved in the respective species folders that are created in the specified path.
CONCLUSIONS:
- Thus all the objectives are achieved successfully.
- Similar approach and logics can be used to extract data from any data file provided and required calculations can be done.
- Using above approach any data file can also be read and edited accordingly.








