Project

File 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 (of speech).

Parsing in computer languages refers to syntactic analysis of the input code into its components parts in order to facilitate the writing of 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.

Types of parsers

The task of the parser is essentially to determine if and how the input can be derived from the start symbol of the grammar. This can be done in essentially two ways:

Top-down parsing - Top-down parsing can be viewed as an attempt to find left-most derivations of an input-stream by searching for parse trees using a top-down expansion of the given formal grammar rules. Tokens are consumed from left to right. Inclusive choice is used to accommodate ambiguity by expanding all alternative right-hand-sides of grammar rules.[11] This is known as the primordial soup approach. Very similar to sentence diagramming, primordial soup breaks down the constituencies of sentences.[12]

Bottom-up parsing - A parser can start with the input and attempt to rewrite it to the start symbol. Intuitively, the parser attempts to locate the most basic elements, then the elements containing these, and so on. LR parsers are examples of bottom-up parsers. Another term used for this type of parser is Shift-Reduce parsing.

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 c1 to c14.
  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.

MAIN PROGRAM:

close all
clear all
clc
file = fopen('THERMO.dat','r') %Opening the NASA thermo data file and reading the data
line1 = fgetl(file) %Getting the heading in the data file
line2 = fgetl(file) %Global temperature values
comment_1 = fgetl(file) %Comments which have no significance in our parsing
comment_2 = fgetl(file) %Comments which have no significance in our parsing
comment_3 = fgetl(file) %Comments which have no significance in our parsing
A = strsplit(line2,' '); %Splitting the 2nd line to extract the individual global temperature values
Global_low = str2num(A{2}) % Global low temperature
Global_mid = str2num(A{3}) % Global medium temperature
Global_high= str2num(A{4}) % Gobal high temperature

%Extracting individual Species information
for i=1:53 % total 53 individual species

species= fgetl(file); 
T1 = strfind(species,'.');
temp1 = species(T1(1)-3:T1(1)+3);
t_min = str2double(temp1); % local  %% local low temoerature
temp2 = species(T1(2)-4:T1(2)+3);
t_max = str2double(temp2);   % local high temperature
temp3 = species(T1(3)-4:T1(3)+3);
t_mid = str2double(temp3);   % local mid temperature

species_name = species(1:6); % Longest species has 6 Alphabets

Line_1 = fgetl(file);  % Getting the first line of species

%Extracting the constants c1 to c5 from 1st species line using "E" as indicator
L1 = strfind(Line_1,'E');
c1 = str2num(Line_1(1:(L1(1)+3)));
c2 = str2num(Line_1((L1(1)+4):(L1(2)+3)));
c3 = str2num(Line_1((L1(2)+4):(L1(3)+3)));
c4 = str2num(Line_1((L1(3)+4):(L1(4)+3)));
c5 = str2num(Line_1((L1(4)+4):(L1(5)+3)));

Line_2 = fgetl(file);
%Extracting the constants c6 to c10 from 2nd species line using "E" as indicator
L2 = strfind(Line_2,'E');
c6 = str2num(Line_2(1:(L2(1)+3)));
c7 = str2num(Line_2((L2(1)+4):(L2(2)+3)));
c8 = str2num(Line_2((L2(2)+4):(L2(3)+3)));
c9 = str2num(Line_2((L2(3)+4):(L2(4)+3)));
c10 = str2num(Line_2((L2(4)+4):(L2(5)+3)));

Line_3 = fgetl(file);
%Extracting the constants c11 to c14 from 3rd species line using "E" as indicator
L3 = strfind(Line_3,'E');
c11 = str2num(Line_3(1:(L3(1)+3)));
c12 = str2num(Line_3((L3(1)+4):(L3(2)+3)));
c13 = str2num(Line_3((L3(2)+4):(L3(3)+3)));
c14 = str2num(Line_3((L3(3)+4):(L3(4)+3)));

%Creating directory for each species
mkdir(['F:Matlab Skill LyncWeek 5filepars_2Extracted_DataData of ',species_name]);
warning('off', 'MATLAB:MKDIR:DirectoryExists');
Temperature = linspace(t_min,t_max,100); %Taking 100 values between local max and in temperatures
R =8.314; %Universal gas constant in J/mol-K

%calculation of properties based on the local lower and upper temperature
for j = 1:length(Temperature);
 if Temperature(j)>t_mid
spec_heat = (c1+c2.*Temperature+c3.*Temperature.^2+c4.*Temperature.^3+c5.*Temperature.^4).*R;
Enthalpy = (c1+(c2.*Temperature)./2+(c3.*Temperature.^2)/3+(c4.*Temperature.^3)/4+(c5.*Temperature.^4/5)+c6./Temperature).*Temperature.*R;
Entropy = (c1.*log(Temperature)+c2.*Temperature+(c3.*Temperature.^2)/2+(c4.*Temperature.^3)/3+(c5.*Temperature.^4)/4+c7).*R;
 else
spec_heat = (c8+c9.*Temperature+c10.*Temperature.^2+c11.*Temperature.^3+c12.*Temperature.^4).*R;
Enthalpy = (c8+(c9.*Temperature)/2+(c10.*Temperature.^2)/3+(c11.*Temperature.^3)/4+(c12.*Temperature.^4/5)+c13./Temperature).*Temperature.*R;
Entropy = (c8.*log(Temperature)+c9.*Temperature+(c10.*Temperature.^2)/2+(c11.*Temperature.^3)/3+(c12.*Temperature.^4)/4+c14).*R;
 end
end

%======Plotting======%
%Temperature vs Entropy
figure(1)
plot(Temperature,Entropy,'linewidth',2,'color','r');
title(['Entropy Plot of ', species_name]);
xlabel('Temperature in Kelvins(K) --->');
ylabel('Entropy(S)--->');
%Temperature vs Enthalpy
figure(2)
plot(Temperature,Enthalpy,'linewidth',2,'color','g')
title(['Enthalphy Plot of ', species_name]);
xlabel('Temperature in Kelvins(K) --->')
ylabel('Enthalpy(H)--->')
%Temperature vs Specific heat
figure(3)
plot(Temperature,spec_heat,'linewidth',2,'color','b')
title(['Specific Heat Plot of ', species_name])
xlabel('Temperature in Kelvins(K) --->')
ylabel('Specific heat(Cp)--->')
directory = (['F:Matlab Skill LyncWeek 5filepars_2Extracted_DataData of ',species_name]); %changing the filepath to save the plots
cd(directory)
%Saving the plots in directory
saveas(1,'Entropy.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Specific heat.jpg');

cd(['F:Matlab Skill LyncWeek 5filepars_2']);
molecular_W = molecular_weight(species_name);
K = sprintf('Molecular weight of %s is %f',species_name,molecular_W);
disp(K) % The weight is made to display in the command window

end

fclose(file)

Below is the function to calculate the Molecular weight of each species.

  • First step is to assign the value for each element using an array.
  • Then using 'strcmp' command the species name and array elements are compared. Then the atomic weight is added according to the compared string element value.
  • To find the repetiton of elements of the same species, the numerical value(n) is extracted using the str2 num and then the atomic weight of the previous element is added to 'n-1' times.
function M = molecular_weight(species_name)
species = (species_name);
elements = ['H','C','N','O','A'];
Atomic_weight = [1,12,14,16,40]; % Assigning atomic weight for each array element of a species
M=0; % Initialising value for M
for i=1:length(species)
 for j = 1:length(elements)
 if strcmp(species_name(i),elements(j))  %Compares the species name with species array 
 M=M+Atomic_weight(j);
 x=j;
 end
 end
 n=str2num(species_name(i)); % extracts the numbers present in the species name
 if n>1
 M=M+(Atomic_weight(x)*(n-1)); % To add the repetitive elements of the species with the weight
 end
end
end

Errors faced while programming:

1. Directory error

The error was due to not specifying the directory for the molecular weight function from which the Molecular weight is to be calculated.

The error was rectified by using change directory(cd) command and specifying the folder path of the Molecular weight function file.

2.

This is not an error but the graphs of HCN and CH2OH were not plotted and was empty. upon debugging came to know that the indicator(letter 'G') which was used to extract the local temperature, repeated in more than once in these two species. Thus the program detected the 'G' in a place it wasnt intended to.

The mistake was overcome by changing the indicator to a decimal point of the local temp species('.')

3. Accessing array element:

The error was due to the wrong syntax used in accessing array elements due to overlooking. The error was rectified using '{}', to access array element.

4.

The error was a typo. It was rectified using sprintf.

5.Non-Initialisation of x value in Mol weight function

The error occured because of the fact that the value of x wasnot assigned at the end of the for loop. Thus the value of x will be null and the loop doesnot run.

It was overcome by assigning the value of 'j' to x.

RESULTS:

1. Plots of Oxygen:

2. Plots of Nitrogen:

3. Plots of carbon dioxide:

Molecular weights:

References:

https://in.mathworks.com/matlabcentral/answers/168729-how-to-avoid-warnings-regarding-non-existent-directory

https://www.mathworks.com/help/matlab/ref/strcmp.html

https://in.mathworks.com/matlabcentral/answers/47477-parsing-formatted-text-file-quickly

http://chemcollective.org/activities/tutorials/stoich/calculating_molecular_weight#:~:text=We%20can%20calculate%20the%20molecular,the%20atoms%20in%20the%20molecule.

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

https://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm

https://www.wikihow.com/Calculate-Molecular-Weight

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

https://in.mathworks.com/matlabcentral/answers/317006-create-new-folders-by-mkdir

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

https://www.quora.com/What-does-it-means-to-parse-a-file#:~:text=Parsing%20a%20file%20means%20reading,of%20transformation%20on%20the%20data.

https://in.mathworks.com/matlabcentral/answers/7262-best-way-to-parse-data-from-a-large-mixed-format-text-file

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.