Challenge

Parsing NASA thermodynamic data

Introduction:
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. The term parsing comes from Latin pars (orationis), meaning part

Parsing in computer languages refers to syntactic analysis of the input code into its components parts in order to facilitate the writingof compilers and interpreters. It can be used to describe split or separation.

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 perfoming some kind of transformation on the data

AIM:
To parse a thermodynamic data file of NASA, and to extract the coefficients and other parameters of each species and to calculate the specific heat(Cp), Entropy(S) and Enthalphy(H). Then using the local temperatures plot the values of Cp, H and S, and to display the Molecular weight of each species in MATLAB.


Logic to extract data from 'Thermo.dat' file:
-It can be seen that the 1st line is the title and the 3rd, 4th and 5th lines are just comments, which are not in the scope of our
parsing(they dont possess any valuble data needed for our calculation).
The 2nd line has 3 temperature values which are the global temperatures for the entire data set. This can be stored in a variable and also not significant for our calculation as we intend to use the local temperatures of each species in particular to plot and calculate.
From the 6th line the species data starts and goes upto the end of the dataset. It can easily be noted a significant pattern in the species data distribution. That is, each species has 4 lines of data.
1. The 1st species line has, the local low, High and mid temperatures respectively.
2. Whereas, the 2nd to 4th species line has the 14 coefiicient values(unique for each species).
3. The first 7 coefficients are high temp coeffs, ie. the temp value above mid temperature must be used in order to calcualte
Cp, H and S.
4. The last 7 coefficients are low temp coeffs, ie. the temp values below mid temperature must be used in order to calculate
Cp, H and S.


PROCEDURE:
1. The first step is to open the data file using the fopen command and the 'r' denotes reading the file.
2. Using fgetl command the first line data is taken and stored in a variable line_1.
3. Again using the fgetl command results in reading the next consecutive line in the data file. Thus using this command 5 times we
can extarct the first five lines (title, Global temp and comments) and store them in variables for our understanding.
4. In order to extract the global temperatures separately, 'strsplit' command is used( since extracted data even if number is stored
as a string). The spacing between each global temp is used as an indicator for the str split command.
5. There are totally 53 species as explained below,
total lines = 218.
irrelevant lines = 6 (first 5 lines as explained above and the last line which has 'END').
total relevant species lines = 218-6 = 212.
each species has 4 lines of data.
so, number of species = 212/4 = 53 species.
6. Using a 'for' loop with the 53 species, and with fgetl command each line of species is read and the coefficient values are stored from a1 to a14.
7. The coeff values can be found using the 'E' as an indicator and with strfind command.
8. Using a nested 'for' loop the Cp, H and S values are calculated as explained above.
9. Now the extracted data is saved in a directory with the species name using the 'mkdir' command.
10. The graphs for each species are plotted and the images are saved in the directory using the saveas command.
11. To calculate the molecular weight a separate function is used with name 'molecular_weight'.
12. The value of universal gas constant is taken as 8.314 J/mol-K.

Now create a functions

function for specific heat

% Specific heat calculation function
function Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp) 
% Comparing with global temperature
% If less than global temperature - minimum coefficients are taken
if Temp <= global_medium_temp
Cp = R*(a8 + (a9*Temp) + (a10*(Temp).^2) + (a11*(Temp).^3) + (a12*(Temp).^4)); 
else
Cp = R*(a1 + (a2*Temp) + (a3*(Temp).^2) + (a4*(Temp).^3) + (a5*(Temp).^4));
% Else maximum coefficients are taken
end

function for Enthalpy

% Enthalpy Calculation function
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp) 
% Comparing with global temperature
% If less than global temperature - minimum coefficients are taken
 if Temp <= global_medium_temp
 H = R*Temp.*(a8 + ((a9*Temp)/2) + ((a10*(Temp).^2)/3) + ((a11*(Temp).^3)/4) + ((a12*(Temp).^4)/5) +((a13./Temp)));
 else
% Else maximum coefficients are taken 
 
 H = R*Temp.*(a1 + ((a2*Temp)/2) + ((a3*(Temp).^2)/3) + ((a4*(Temp).^3)/4) + ((a5*(Temp).^4)/5) +((a6./Temp)));
end

Function for Entropy

% Entropy Calculation function
function S = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp) 
% Comparing with global temperature
% If less than global temperature - minimum coefficients are taken
 if Temp <= global_medium_temp
 S = R.*(a8.*log(Temp) + (a9.*Temp) + (a10*((Temp).^2)/2) + (a11*((Temp).^3)/3) + (a12*((Temp).^4)/4) + a14);
 else
% Else - maximum coefficients are taken
 S = R.*(a1.*log(Temp) + (a2.*Temp) + (a3*((Temp).^2)/2) + (a4*((Temp).^3)/3) + (a5*((Temp).^4)/4) + a7);
end

Function for molecular weight

% Molecular Weight Calculation function
function Molecularweight = Molecular_weight(Species_SP)
 % Standard - Atomic Name
 atomic_name = ['H','C','O','N','A']; 
 % Hydrogen, Carbon, Oxygen, Nitrogen, Argon
 
 fprintf('Molecular Weight of ');
 fprintf(Species_SP);
 fprintf(' : ');
 % Standard Atomic Number
 Atomic_Weight = [1.008 12.011 15.999 14.007 39.948]; 
 % Starting Range of Molecule 
 Molecularweight = 0; 
 
 % Adding Atomic number to obtain particular value
 for i = 1:length(Species_SP)
 for j = 1:length(atomic_name)
 if strcmpi(Species_SP(i),atomic_name(j))
 Molecularweight = Molecularweight +Atomic_Weight(j);
 position = j;
 end
 end
 n = str2num(Species_SP(i));
 
 if n>1
 
 Molecularweight = Molecularweight + Atomic_Weight(position)*(n-1);
 
 end
 end
 fprintf('molecular weight %s :',Species_SP);
 fprintf('%d',Molecularweight);
 disp(' '); 
end

Main Program

clear all
close all
clc
% Make directory
mkdir 'D:\challenges\nasa data'
% Reading Data file - NASA Thermodynamic
% Open a file & 'r' Open the file for reading 
f1=fopen('THERMO.dat','r')
gt=fgetl(f1);
% Obtaing Temperatures - by Reading Header Information
global_temp=fgetl(f1);
% Spliting
t=strsplit(global_temp);
% Converting string cells to numbers using str2num,
global_low_temp=str2num(t{2});
global_medium_temp=str2num(t{3});
global_high_temp=str2num(t{4});
% Temperature range & Gas Constant
 % Temperature Range assumed
Temp=linspace(global_low_temp,global_high_temp,1000);
% Universal Gas Constant J/(mol-K)
 R = 8.314; 
 
% skipping three comment lines in the file
for i = 1:3
 gt=fgetl(f1);
end
 for i = 1:53
gt=fgetl(f1);
A=strsplit(gt,' ');
Species_SP=(A{1})
line_1=fgetl(f1) ; 
a=strfind(line_1,'E');
% Extracting higher and lower temperature coefficients
% % Extracting FIRST 7 coefficients i.e HIGH-temperature coefficients 
a1=line_1(1:a(1)+3);
a1=str2num(a1); %1
a2=line_1(a(1)+4:a(2)+3);
a2=str2num(a2) ; %2
a3=line_1(a(2)+4:a(3)+3);
a3=str2num(a3) ; %3
a4=line_1(a(3)+4:a(4)+3);
a4=str2num(a4); %4
a5=line_1(a(4)+4:a(5)+3);
a5=str2num(a5) ; %5
line_2=fgetl(f1);
b=strfind(line_1,'E');;
a6=line_2(1:b(1)+3);
a6=str2num(a6) ; %6
a7=line_2(a(1)+4:b(2)+3);
a7=str2num(a7); %7
 % Extracting SECOND 7 coefficients - LOW-temperature coefficients
a8=line_2(b(2)+4:b(3)+3); 
a8=str2num(a8); %8
a9=line_2(b(3)+4:b(4)+3);
a9=str2num(a9) ; %9
a10=line_2(b(4)+4:b(5)+3);
a10=str2num(a10) ; %10
line_3=fgetl(f1);
c=strfind(line_1,'E');
a11=line_3(1:c(1)+3);
a11=str2num(a11); %11
a12=line_3(c(1)+4:c(2)+3);
a12=str2num(a12); %12
a13=line_3(c(2)+4:c(3)+3);
a13=str2num(a13); %13
a14=line_3(c(3)+4:c(4)+3);
a14=str2num(a14); %14
% Specific heat calculation
 Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp);
 % Enthalpy calculation
 H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp);
 % Entropy calculation
 S = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp);
 % Calculating molecular weight of species
 Molecularweight(i) = Molecular_weight(Species_SP);
 
 % Read & Write Molecular list
 Molecular_list = fopen('Molecular weight.txt','a+');
 fprintf( Molecular_list,'Molecular weight of %s is %d n',Species_SP,Molecularweight);
 fclose(Molecular_list);
 % plotting(CP,H,S,Temp,Species_SP)
 % Saving as a folder
 cur_dir=pwd;
 mkdir(Species_SP);
 cd(Species_SP);
 % Plot 1, Specific heat (CP) vs Temperature Range (Temp)
 figure(1)
 plot(Temp,Cp,'linewidth',2,'color','r');
 xlabel('Temperature [K]');
 ylabel('Specific Heat [kJ]');
 title(sprintf('Varying Specific Heat with Temperature Range of %s',Species_SP));
 grid on
 saveas(figure(1),'Specific Heat.jpg');
 % Plot 2, Enthalpy (H) vs Temperature Range (Temp)
 figure(2)
 plot(Temp,H,'linewidth',2,'color','g');
 xlabel('Temperature [K]');
 ylabel('Enthalpy in [kJ/(mol)]');
 title(sprintf('Varying Enthalpy with Temperature Range of %s',Species_SP));
 grid on
 saveas(figure(2),'Ethalpy.jpg');
 % Plot 3, Entropy (S) vs Temperature Range (Temp)
 figure(3)
 plot(Temp,S,'linewidth',2,'color','b');
 xlabel('Temperature [K]');
 ylabel('Entropy in [kJ/(mol-K)]');
 title(sprintf('Varying Entropy with Temperature Range of %s',Species_SP));
 grid on
 saveas(figure(3),'Entropy.jpg');
 cd(cur_dir); 
 % Molecular weight

 
 M = Molecular_weight(Species_SP)
 f2 = fopen('molecular mass','w');
 fprintf('%s n %s n %f n','species','Molecular mass',M)
 fclose(f2)
end

Explanation of Code:


1. The program begins with clear all, close all and clc commands to free up the command window and the workspace.
2. We will then read the data from the external NASA thermodynamic data file using fopen command which is used to open the file and fgetl command which is used to read the content in the file line by line.
3. After we use the fopen command we will be assigning to the variable called f1. Thus, the fgetl(f1) is the command acts like a line cursor, which will retrieve the next time while running this command for the second time and so on.
4. Now, we will be skipping the first 5 lines to enter the pattern of values of the respective gas species. We will be using the for loop command to process the same for all the gas species.
5. The name of the gas species we will be found out using findstr command and for finding out the species we are going to locate the string ‘ ’ which is present in after each species name.
6. And then we are going to use A(1:z(1)-1) which tells us the exact location of the species name, using which the name of the gas species is stored in the variable species_name.
7. The local temperatures for the respective gas species is found similarly using findstr command and we are going to locate the string ‘.’ using which we will find out the values of low temperature, mid temperature and high temperature which is assigned to the variables t1, t2 and t3 respectively. But the values we have achieved are string values so we have to change these string values to numeric values using the str2num command.
8. Now, we are going to extract the co-efficient for the gas species in which the co-efficient locations are like in the 2 row, next 5 in the 3 row and the rest in the 4 row.
9. We are going to extract these values similarly like we did for finding out the local temperatures by using the findstr and str2num commands and store it in the variables a1 to a14. For extracting we are going to locate the string ‘E’ which is present in the each co-efficient value and so it will be followed for all the co-efficients values in the same line and then we can proceed for the next lines.
10. The values from a1 to a7 represent the high temperature co-efficients and the values from a8 to a14 represent the low
temperature co-efficients.
11. Temperature range values from low temperature to high temperature is assigned using the linspace command.
12. The for loop command is used to calculate the values of specific heat, enthalpy and entropy and the if-else command is also used to find out the required values if the temperature below the mid temperature or above the mid temperature.
13. In order to calculate the molecular weight of the species we declare the species elements in the form of cell array. Their atomic mass is declared in the form of array. Then initially molecular weight is declared as zero.
14. Now, for loop command is used to detect if there are more than one species in the variable species_name and the respective values of the gas species values are calculated and found out.
15. After the nested for loop the species_name which is in the form of string is converted as number with strdouble command.
Then the species_name which is now as number is greater than 1, molecular weight is again calculated by having the atomic
mass for the current species * n-1
16. The value of the molecular weight is calculated for the respective gas species and is displayed in the command window.
17. Now, we are going to create plots for the obtain specific heat, enthalpy and entropy values and save it in respective
species_name folder automatically.
18. In order to create a new folder with the species_name we are going to use the mkdir command so that a new folder is created in the main folder. Now, we have to change the current directory from the main folder to the species_name folder so the cd command is used to do the same.
19. The plots for specific heat vs temperature, enthalpy vs temperature and entropy vs temperature is plotted and the labelling of axis and the naming of the title is also done along with the value of molecular weight.
20. To save the plots in the species_name folder, saveas command is used to save the plots in the current folder. After which to change the current directory to the main folder the oldfolder = cd () command is used.

Output

refrences-

https://en.wikipedia.org/wiki/Parsing

https://in.mathworks.com/help/matlab/ref/mkdir.html

https://in.mathworks.com/help/matlab/ref/cd.html

https://in.mathworks.com/help/matlab/ref/saveas.html

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.