Nasa File parsing project
Link: https://drive.google.com/drive/folders/1SepZSlYmQ9son5Wt5tbRGOBAjBdErzvS
Aim:
To write a Matlab program to read data from 'THERMO.dat" file and to calculate Specific Heat, Enthalpy, Entropy and Molecular weight of the species provided. Plot graphs of Enthalpy, Entropy and specific heat.
Description:
File parsing is done to extract 14 co-efficients provided for each species and calculate enthalpy,entropy and specific heat. Given guideline is that the first 7 coefficients are HIGH-temperature co-efficents and Second 7 coefficients are Low-temperature co-efficients.
Formulae to calculate specific heat, enthalpy and entropy are:

Where R is the universal gas constant and T is the temperature.
Steps for writing Code:
1. 'THERMO.dat' file is opened in read only format. Each line of the file is read using 'fgetl' command.
2. Initial 5 lines are skipped since this information is not used in codeing.
3. In considered file, 53 different species information with 14 co-efficients is available. Each species information is of 4 lines. for loop is declared with minimum value = 1 and maximum value of 53 to read the contents of all 53 species provided in file.
5. From first line of species block,species data is read. used 'strsplit' command is used to split the information into cell array.first information of cell array is having species name. This is stored in a variable 'species_name'.
6. First line also has low temperature, mid temperature and high temperature values. These values are extracted with below code lines. length of line1 is different for each species. So length of line-1 is calculated to know the position of temperature information.
t_1=fgetl(f1) % reading line1 of first species block, monoatomic oxygen and stored in 't_1' variable.
s=strsplit(t_1,' ');% content of line-1 is split into cell array using empty space between values of line.
species_name=s{1}; % species name is extracted from cell array
zz(j)=length(s); % calculate the length of cell array & stored in array zz(j).
xx=zz(j)-3; % temperature information is calculated from last cell position. so following formulae is used to extract low temperature, mid temperature and high temperature.
least_temp = str2num(s{xx});
mid_temp = str2num(s{xx+2});
high_temp = str2num(s{xx+1});
4. Next 3 lines of species block has 14 co-efficients. These 14 values are extracted and stored in 14 different variables.
5.Using these 14 co-efficients Specific Heat, Enthalpy and Entropy are calculated as per provide respective formulae.

6.For every species separate folder with its name is created using MATLAB code. Plots of Specific Heat, Enthalpy and Entropy respective species are stored in this folder.
% create directory to save plots
SN=char(species_name)
mkdir( fullfile('D:material_downloadedMATLAB_file_parsingspecies_name', SN) );
cd ( fullfile('D:material_downloadedMATLAB_file_parsingspecies_name', SN) )
saveas(1,'Specific_Heat.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Entropy.jpg');
7. To find molecular weight for respective element atomic weight is assigned by using the 'strcmp' command. if true then molecular weight is calculated else next loop is carried out.
elements = ['H','C','O','N','A','S'];
atomic_weight=[1 12 16 14 40 32];
molecular_weight=0;
for m=1:length(species_name);
for n=1:length(atomic_weight);
if strcmp(species_name(m),elements(n));
molecular_weight=molecular_weight+atomic_weight(n)
position=n
end
end
n=str2double(species_name(m));
if n>1
molecular_weight=molecular_weight+atomic_weight(position)*(n-1);
end
8.In some species line O2 the element's atomic weight to be added twice (O2 -> O+O, 16+16=32), hence flag operator is used to get the position and that element atomic weight is added to the molecular weight by using 'if' condition.
9. species name and it's molecular weight is writen to a file.
f2=fopen('species_list.txt','a');
fprintf(f2,'%s,%d, n',species_name,molecular_weight)
fclose(f2);
Programe code lines
close all
clear all
clc
f1 = fopen('THERMO.dat','r')
t_line={1,2,3,4,5}; % varaible declared for skipping of initial 5 lines
for i=1:length(t_line) % for loop is writen to skip initial 5 lines.
fgetl(f1)
end
% reading species block
for j=1:53 % for loop declared to read 53 species available in file
% Extracting Species name
t_1=fgetl(f1); % reading first line of species block
s=strsplit(t_1,' '); % splitting line into cell array using 'strsplit' command with conition of empty cell between two information cells.
species_name=s{1};
zz(j)=length(s);
% code line to extract local temperature from species line
xx=zz(j)-3;
least_temp = str2num(s{xx});
mid_temp = str2num(s{xx+2});
high_temp = str2num(s{xx+1});
%extracting co-efficients from first line of species code block
Q=fgetl(f1);
a=findstr(Q,'E');
a1=Q(1:a(1)+3);
A1=str2double(a1);
a2=Q(a(1)+4:a(2)+3)
A2=str2double(a2);
a3=Q(a(2)+4:a(3)+3);
A3=str2double(a3);
a4=Q(a(3)+4:a(4)+3);
A4=str2double(a4);
a5=Q(a(4)+4:a(5)+3);
A5=str2double(a5);
%extracting co-efficients from second line of species code block
R=fgetl(f1);
b=findstr(R,'E');
a6=R(1:b(1)+3);
A6=str2double(a6);
a7=R(b(1)+4:b(2)+3)
A7=str2double(a7);
a8=R(b(2)+4:b(3)+3);
A8=str2double(a8);
a9=R(b(3)+4:b(4)+3);
A9=str2double(a9);
a10=R(b(4)+4:b(5)+3);
A10=str2double(a10);
%extracting co-efficients from third line of species code block
S=fgetl(f1);
c=findstr(S,'E');
a11=S(1:c(1)+3);
A11=str2double(a11);
a12=S(c(1)+4:c(2)+3)
A12=str2double(a12);
a13=S(c(2)+4:c(3)+3);
A13=str2num(a13);
a14=S(c(3)+4:c(4)+3);
A14=str2double(a14);
T=linspace(least_temp,high_temp,1000);
for k=1:length(T) % 'for' loop with temperature limits
if T(k)>mid_temp
CP(k) =(A1+(A2*T(k))+(A3*T(k)^2)+(A4*T(k)^3)+(A5*T(k)^4))*8.314;
H(k) = (A1+((A2*T(k))/2)+((A3*T(k)^2)/3)+((A4*T(k)^3)/4)+((A5*(T(k)^4))/5)+(A6/T(k)))*8.314*T(k);
Ent(k)= (((A1*log(T(k)))+(A2*T(k))+(A3*((T(k)^2)/2))+(A4*((T(k)^3)/3))+(A5*((T(k)^4)/4))+A7))*8.314;
else
CP(k) =(A8+(A9*T(k))+(A10*T(k)^2)+(A11*T(k)^3)+(A12*T(k)^4))*8.314;
H(k) = (A8+((A9*T(k))/2)+((A10*T(k)^2)/3)+((A11*T(k)^3)/4)+((A12*(T(k)^4))/5)+(A13/T(k)))*8.314*T(k);
Ent(k)= (((A8*log(T(k)))+(A9*T(k))+(A10*((T(k)^2)/2))+(A11*((T(k)^3)/3))+(A12*((T(k)^4)/4))+A14))*8.314;
end % end of 'if' loop
end %end of 'for' loop with temperature limits
% plotting of graphs
figure(1)
plot(T,CP);
grid on;
title(species_name);
xlabel('Temperature')
ylabel('Specific Heat');
figure(2)
plot(T,H);
grid on;
title(species_name);
xlabel('Temperature')
ylabel('Enthaply');
figure(3)
plot(T,Ent);
grid on;
title(species_name);
xlabel('Temperature')
ylabel('Entropy');
% create directory to save plots
SN=char(species_name)
mkdir( fullfile('D:material_downloadedMATLAB_file_parsingspecies_name', SN) );
cd ( fullfile('D:material_downloadedMATLAB_file_parsingspecies_name', SN) )
saveas(1,'Specific_Heat.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Entropy.jpg');
% Calculate molecular weight of Species
elements = ['H','C','O','N','A','S'];
atomic_weight=[1 12 16 14 40 32];
molecular_weight=0;
for m=1:length(species_name);
for n=1:length(atomic_weight);
if strcmp(species_name(m),elements(n));
molecular_weight=molecular_weight+atomic_weight(n)
position=n
end
end
n=str2double(species_name(m));
if n>1
molecular_weight=molecular_weight+atomic_weight(position)*(n-1);
end
cd (fullfile('D:material_downloadedMATLAB_file_parsing'));
end %end of 'if' statement with molecular weight calculation
f2=fopen('species_list.txt','a');
fprintf(f2,'%s,%d, n',species_name,molecular_weight)
fclose(f2);
end %end of for loop with species list

Example of plots for O and O2 inside respective folders.






Species list stored in notepad


