Project 1 - Parsing NASA thermodynamic data
Aim :- Parsing NASA thermodynamic data.
Objective:-
1.Extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file and write function for them.
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.
Theory:-
Parsing the data :-
In the field of computer programming, the definition of parsing is to analyze a string of symbols, special characters, and data structures . When you define extracting in parsing, it refers to structuring information from data sets and giving it meaning by organizing it, based on user-defined rules.
Data parsing is used for crawling information from large datasets and structuring it in a way humans can understand. Traditional data parsing is done on HTML files where the parser converts HTML text into readable data. However, not all parsers work the same and there are distinct differences in parsing technologies. There are numerous benefits of data parsing for businesses ranging from automated data extraction, improved visibility, cutting costs, and boosting employee productivity.
Thermodynamic data explanation :-

Above data is NASA thermodynamic data where 53 molecules are given with their temperature coefficients at their local temperature range respectively also their global temperature range given.
There are 14 coefficient for each species (molecules), the first 7 coefficients are high temperature coefficient and second 7 coefficients are low temperature coefficients.

Enthalpy
Enthalpy is the measurement of energy in a thermodynamic system. The quantity of enthalpy equals to the total content of heat of a system, equivalent to the system’s internal energy plus the product of volume and pressure.
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.
Specific heat
The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius.
We need to extract these coefficients for each species and calculate Enthalpy , Entropy and Specific heat using given formula also we need to calculate molecular weight of each species, Using logical codes ,for that first we need to open and read the thermodynamic data and extract the given coefficients and store them in separate directory also use them to plot the graphs between temperature and given thermodynamic properties.
Main code:-
clear all
close all
clc
f1 = fopen('THERMO.dat','r');
line1 = fgetl(f1);
%skipping the next four lines
for i = 1:4
skkippinglines = fgetl(f1);
end
%Extracting individual Species information
for i=1:53 % total 53 individual species
species = fgetl(f1);
m=strsplit(species,' ');
species_name=m{1};
local_low_temp=str2num(m{end-3});
local_high_temp=str2num(m{end-2});
local_mid_temp=str2num(m{end-1});
Line_1 = fgetl(f1);
L1 = strfind(Line_1,'E');
c1 = Line_1(1:(L1(1)+3));
C1 = str2num(c1);
c2 = Line_1((L1(1)+4):(L1(2)+3));
C2 = str2num(c2);
c3 = Line_1((L1(2)+4):(L1(3)+3));
C3 = str2num(c3);
c4 = Line_1((L1(3)+4):(L1(4)+3));
C4 = str2num(c4);
c5 = Line_1((L1(4)+4):(L1(5)+3));
C5 = str2num(c5);
Line_2 = fgetl(f1);
L2 = strfind(Line_2,'E');
c6 = Line_2(1:(L2(1)+3));
C6 = str2num(c6);
c7 = Line_2((L2(1)+4):(L2(2)+3));
C7 = str2num(c7);
c8 = Line_2((L2(2)+4):(L2(3)+3));
C8 = str2num(c8);
c9 = Line_2((L2(3)+4):(L2(4)+3));
C9 = str2num(c9);
c10 = Line_2((L2(4)+4):(L2(5)+3));
C10 = str2num(c10);
Line_3 = fgetl(f1);
L3 = strfind(Line_3,'E');
c11 = Line_3(1:(L3(1)+3));
C11 = str2num(c11);
c12 = Line_3((L3(1)+4):(L3(2)+3));
C12 = str2num(c12);
c13 = Line_3((L3(2)+4):(L3(3)+3));
C13 = str2num(c13);
c14 = Line_3((L3(3)+4):(L3(4)+3));
C14 = str2num(c14);
%Temperature ranges assumed for calculation purpose
T = linspace(local_low_temp,local_high_temp,800);
%Universal gas constant
R =8.314;
%calculation of properties based on the lower and upper temperature
for i = 1:length(T)
[cp,H,S] = calc_therm_properties(C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,T,local_mid_temp);
end
mkdir(species_name);
cd(species_name)
figure(1)
plot(T,cp,'linewidth',2,'color','b')
title('Temp vs Specific heat',species_name)
xlabel('Temperature')
ylabel('Specific heat')
grid on
saveas(figure(1),'Specific heat.jpg')
figure(2)
plot(T,H,'linewidth',2,'color','r')
title('Temp vs Enthalpy',species_name)
xlabel('Temperature')
ylabel('Enthalpy')
grid on
saveas(figure(2),'entalpy.jpg')
figure(3)
plot(T,S,'linewidth',2,'color','g')
title('Temp vs Entropy',species_name)
xlabel('Temperature')
ylabel('Entropy')
grid on
saveas(figure(3),'Entropy.jpg')
cd ..
[molecular_wt] = mw(species_name);
end
Code explanation:-
- Open and read the given thermodynamic data “THERMO.dat” file which is provided using ‘fopen’ command.
- Read the next line of the file using ‘fgetl’ command which returns THERMO ,which is 1st line of the file.
- Next line is global temperature range which is not required here as we have to consider only local thermodynamic range for each species also next 3 lines are not necessary so skip all 4 lines.
- Now extracting species information for that using ‘for loop’ ,as in data file there are 53 species so we need to extract coefficients for each species.
- Each species has 4 lines, in 1st line there is species name and different information given with local temperature range. Reading 1st line of each species using ‘fgetl’ command and saving it as species variable then splitting it using ‘strsplit’ command and saving it as ‘m’,
- In ‘m’ string the 1st element is species name so saving it as species name and last three elements are local temperature so extracting them by using their position. Also, ‘m’ is a string so converting while extracting temperature using ‘str2num’ command and saving them as local_low_temperature , local_mid_temperature and local_high_temperature.
- After extracting species name and specifying local temperature for species, now we have to extract the coefficients. 14 coeffiecients are in next 3 lines so by locating their position we can find coefficients, for that 1st find location of ‘E’ using ‘strfind’ command and saving it as L1.
- In Line L1 , there are 5 ‘E’ ,it means there are 5 coefficients, and after ‘E’ the 3 digits are value of exponents and just next to exponent value another coefficient starts , so in line 1 from 1st position to next 3 position of ‘E’ is 1st coefficient , similarly just by positioning before and after ‘E’ ,we can find 2nd ,3rd ,4th coefficient and so on.
Line_1 = fgetl(f1);
L1 = strfind(Line_1,'E');
c1 = Line_1(1:(L1(1)+3));
C1 = str2num(c1);
c2 = Line_1((L1(1)+4):(L1(2)+3));
C2 = str2num(c2);
- Above code tells, Line_1 is 1st line of coefficients, using ‘strfind’ command , ‘E’ position will be found out. Below is the position of ‘E’.
L1 =
12 27 42 57 72
- In c1, we extract 1st coefficient, from 1st element of Line_1 to next 3 element of 1st element of L1 i.e., L1(1) is 12 which is location of 1st ‘E’ and +3 is next three element which is value of exponent as explained above. Also extracted coefficient is still in string so to convert in number for calculation using ‘str2num’ and saving it in UPPERCASE of c.
- Similarly, we can find all other coefficients from Line 2 and 3 of coefficients.
- After extracting all the coefficients we have to use them for the calculation of thermodynamic variable such as Enthalpy, Entropy, Specific heat. For that creating a temperature range array staring from local low temperature to local high temperature of 800 values.
- To calculate Thermodynamic variables and molecular weight of each species, we create a separate function for calculation and just by calling that function we can get thermodynamic variables values and molecular weight.
Code for Enthlapy, Entropy, Specific heat:-
function [cp, H, S] = calc_therm_properties(C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13,C14,T,local_mid_temp)
R = 8.314;
if (T<=local_mid_temp)
cp = (C8+(C9.*T)+(C10.*((T).^2))+(C11.*((T).^3))+(C12.*((T).^4))).*R;
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 = (C1+(C2.*T)+(C3.*((T).^2))+(C4.*((T).^3))+(C5.*((T).^4))).*R;
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
Explanation:-
- Above code is the function to calculate thermodynamic variables whose name is calc_therm_properties, here independent variables are the temperature coefficients which was extracted from THERMO.dat file , Temperature range and local mid temp.
- Enthalpy , entropy and specific heat are calculated using given formula , there different coefficients are used like 1st 7 coefficients are high temperature coefficient and 2nd 7 coefficients are low temperature coefficient. So, for local_low_temp 2nd 7 coefficients i.e., from C8 to C14 is used and for local_high_temp 1st 7 coefficients i.e., from C1 to C7 is used.
- And in above function code, if there is temperature (T) less than local_mid_temp then coefficients from C8 to C14 is used in formula else C1 to C7 is used as explained above.
Code for Molecular weight calculation:-
function [molecular_wt] = mw(species_name)
elements = ['H', 'C', 'O', 'N', 'A'];
atomic_weight = [1, 12, 16, 14, 40];
molecular_wt=0;
for i=1:length(species_name)
for j=1:length(elements)
if strcmpi(species_name(i), elements(j))
molecular_wt = molecular_wt + atomic_weight(j);
pos = j;
end
end
f=str2double(species_name(i));
if f>1
molecular_wt=molecular_wt + atomic_weight(pos)*(f-1);
end
end
fprintf('Molecular weight of %s = %f', species_name,molecular_wt)
% disp(' ')
end
Explanation:-
- Above code is function code to calculate molecular weight, here variable is species_name. Each species are made up by combination of 5 basic elements whether it is monoatomic, diatomic or polyatomic which is given above in elements variable as H,C,O,N,A. And corresponding their atomic weight.
- Here, we are assigning initial molecular weight as 0 then using for loop condition to calculate molecular weight. 1st for loop goes to species name then 2nd for loop goes for elements of that species name after that if condition applied, in that ‘strcmpi’ command is used it is a comparison command, inside parantheses species_name and elements are being compared where if both are identical then it returns true otherwise false. As each elements are in species name so it is identical.
- Next formula to calculate monoatomic molecular weight, where initial molecular weight which is 0 is added to atomic weight of elements and ‘j’ which is elements is saved in ‘pos’ variable.
- Now for polyatomic molecule, str2double command is used which converts species_name string to double precision value and saved in ‘f’ variable then using if condition where if f is greater than 1 then it will use next formula in which molecular weight of previous calculated atom is added with atomic weight of next repeating element.
- At last using ‘fprint’ to write the data in text file.
Directory:-

Plots of O2 ,N2 , Co2:-
Plot of O2 :-



Plots of N2



Plot of Co2:-



Molecular weight of species:-

ERRORS:-

Above error occured as unrecognized function or variable 'pos', because in molecular weight function there instead of 'O' in elements array, '0' was there. After replacing "0" with '0' error get solved.
Conclusion:-
From the given data file THERMO.dat . All the temperature coefficients has been extracted of each species using appropriate logics and commands in MATLAB. Also seperate function is written to calculate thermodynamic variables such as Enthalpy , Entropy and Specific Heat. All the extracted coefficient are used to calculate the variables , Molecular weight of each species is also calculated using seperate function. These functions is called in main code to calculate all the variables. Also graph for each species is plotted between temperature and enthalpy,entropy,specific heat.