Challenge

Project 1 - Parsing NASA thermodynamic data

File Parsing:

File parsing refers to the file examine a file. File parsing is used data can be extracted from the file which can be stored and used to perform calculation.

Here Thermo.dat file have temperature coefficient values for different species is extracted and stored to calculate Specific heat, Enthalpy, Entropy.

Explanation:

Formula used to calculate Specifie heat, Entropy, Enthalpy

To open the Thermo data file fopen command is used.

k=fopen('THERMO.dat','r');

'r' keyword is denoted to read the file. If 'w' is written then the file is editable.

Thermo data file read and assigned to k.

'fgetl' is used to show the content of the line in a file.

To convert the string characters into number characters 'str2num' command is used.

To find the position of a character in a line,'findstr' command is used.

To split the content in a line 'strsplit' is used.

To compare the string elements 'strcmp' command is used.

Function to calculate molecular weight

function[M_w]=Molecular(Species)

%Totally 5 elemnts are there in that file
Elements=['H','C','O','N','A'];


%declaring the molecular weight of those 5 elements.
Atomic_weight=[1.0079 12.0107 15.999 14.0067 39.948];

%Initialising molecular weight as 0
M_w=0;

%To compare the characters of species and atomic weight nested for loopused

for l=1:length(Species)
for m=1:length(Atomic_weight)


if strcmp(Species(l),Elements(m))
M_w=M_w+Atomic_weight(m);


%some elements contained in a species for morethan one time. To calculate those repeated elements molecular weight, storing the values of m to position variable
position=m;
end
end

%converting the string into number characters
N=str2num(Species(l));


%Those characters converted into number which is greater than 1 enters the loop


if N>1
M_w=M_w+Atomic_weight(position)*(N-1);

end

end
end

%Opening the Thermo.dat file to read and assigning it to the variable k
k=fopen('THERMO.dat','r');

%fgetl is used to show the content of a line
fgetl(k);


%Converting the string characters to number charactes using str2num


temp=str2num(fgetl(k));
global_low_temp=temp(1);
global_mid_temp=temp(2);
global_high_temp=temp(3);

%To pass through the comment lines
for i=1:3
fgetl(k);
end


%Totally 53 species are there in that file. so for loop for an iteration of 53 is created
for i=1:53

%Getting the 1st line of species 1.
data_1=fgetl(k);


%spliting that line to remove the empty space characters to get req.charac.

%The line is splitted using the space is=dentifier(' ') for the purpose of removing the empty spaces and to get the alphabets and numerical characters.


splitingline=strsplit(data_1,' ');

%Species name is at position of 1 in Splitingline variable
Species=splitingline{1};

%To get the temp values, using (.) character for identying

%To find the temperature values in a line, finding those values with help of dot character

low=findstr(data_1,'.');


Low_temp_1=str2num(data_1(low(1)-3:low(1)));
Mid_temp_1=str2num(data_1(low(3)-4:low(3)));
High_temp_1=str2num(data_1(low(2)-4:low(2)));

data1=fgetl(k);%To get the coeffcient values
%To find the temp val, using E charaacter to identify the position
X_1=findstr(data1,'E');

%a1,a2,a3,a4,a5 are the temp obtained from species 2nd line
%num characters are in string. To convert into num str2num is used
a1=str2num(data1(1:(X_1(1)+3)));
a2=str2num(data1(X_1(1)+4:X_1(2)+3));
a3=str2num(data1(X_1(2)+4:X_1(3)+3));
a4=str2num(data1(X_1(3)+4:X_1(4)+3));
a5=str2num(data1(X_1(4)+4:X_1(5)+3));

data1=fgetl(k);%To get the coeffcient values
X_2=findstr(data1,'E');

%a6,a7,a8,a9,a10 are the temp obtained from species 3rd line
%num characters are in string. To convert into num str2num is used
a6=str2num(data1(1:X_2(1)+3));
a7=str2num(data1(X_2(1)+4:X_2(2)+3));
a8=str2num(data1(X_2(2)+4:X_2(3)+3));
a9=str2num(data1(X_2(3)+4:X_2(4)+3));
a10=str2num(data1(X_2(4)+4:X_2(5)+3));

data1=fgetl(k);%To get the coeffcient values
X_3=findstr(data1,'E');%X_3 is the cofficient string of line 4

%a11,a12,a13,a14 are the temp obtained from species 4th line
%num characters are in string. To convert into num str2num is used
a11=str2num(data1(1:X_3(1)+3));
a12=str2num(data1(X_3(1)+4:X_3(2)+3));
a13=str2num(data1(X_3(2)+4:X_3(3)+3));
a14=str2num(data1(X_3(3)+4:X_3(4)+3));

%Declaring the Gas constant value
R=8.3145

%Dividing the temperature value from low to high into 150 equal space using linspace command
T=linspace(Low_temp_1,High_temp_1,150)

for n=1:length(T)

%If the temperature value is greater than the mid temperature then higher temperature coefficients values a1,a2,a3,a4,a5,a6,a7 goes for specific heat (Cp) , Enthalpy(H) & Entropy calculation(S)

if T(n)>Mid_temp_1
cp=(a1+(a2.*T)+(a3.*(T.^2))+(a4.*(T.^3))+(a5.*(T.^4)))*R;
H=(a1+(a2.*T)./2+(a3.*T.^2)./3+(a4.*T.^3)./4+(a5.*T.^4)./5+(a6./T))*R.*T;
S=(a1*log(T)+a2.*T+(a3.*T.^2)./2+(a4.*T.^3)/3+(a5.*T.^4)./4+a7)*R;
else

%else the temperature value is lower than the mid temperature then lower temperature coefficients values a8,a9,a10,a11,a12,a13,a14 goes for specific heat (Cp) , Enthalpy(H) & Entropy calculation(S)

cp=(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4)*R;
H=(a8+(a9.*T)./2+(a10.*T.^2)./3+(a11.*T.^3)./4+(a12.*T.^4)./5+(a13./T))*R.*T;
S=(a8*log(T)+a9.*T+(a10.*T.^2)./2+(a11.*T.^3)/3+(a12.*T.^4)./4+a14)*R;
end
end


%to create individual folder for each species

%Printing the species name and storing it in a variable t.
t=sprintf('%s',Species);
mkdir(t)

%To enter into the folder cd command is used
cd(t)

%Plotting Temperature and specific heat graph
figure(1)
plot(T,cp)
title('Specific heat vs Temperature')
grid on
xlabel('Temperature(K)')
ylabel('Specific heat(J/KgK)')
%saving the graph
saveas(1,'specificheat.jpg')

%Plotting Temperature and Enthalpy graph
figure(2)
plot(T,H)
title('Enthalpy vs Temperature')
grid on
xlabel('Temperature(K)')
ylabel('Enthalpy(J/mol)')
%saving the graph
saveas(2,'Enthalpy.jpg')

%Plotting Temperature and Entropy graph
figure(3)
plot(T,S)
title('Entropy vs Temperature')
grid on
xlabel('Temperature(K)')
ylabel('Entropy(J/K)')
%saving the graph
saveas(3,'Entropy.jpg')

%To getout from the folder cd.. is used

cd ..

%Function is called to calculate the molecular weight

M_w=Molecular(Species);

% To print the Molecular weight values

sprintf('Molecular weight of %s:%f',Species,M_w)

end

Problem faced during programing:

During Specific heat, Enthalpy, Entropy formula execution, It showed error notification. They solved by adding . dot character.

% Main code

clear all
close all
clc

%Opening the Thermo.dat file to read and assigning it to the variable K
k=fopen('THERMO.dat','r');

%fgetl is used to show the content of a line
fgetl(k);
%Converting the string characters to number charactes using str2num
temp=str2num(fgetl(k));
global_low_temp=temp(1);
global_mid_temp=temp(2);
global_high_temp=temp(3);

%To pass through the comment lines
for i=1:3
    fgetl(k);
end
%Totally 53 species are there in that file.
for i=1:53
    %Getting the 1st line of species 1.
data_1=fgetl(k);
%spliting that line to remove the empty space characters to get req.charac.
splitingline=strsplit(data_1,'  ');

%Species name is at position of 1 in Splitingline variable
Species=splitingline{1};

%To get the temp values, using (.) dot character for identying
low=findstr(data_1,'.');
Low_temp_1=str2num(data_1(low(1)-3:low(1)));
Mid_temp_1=str2num(data_1(low(3)-4:low(3)));
High_temp_1=str2num(data_1(low(2)-4:low(2)));


data1=fgetl(k);%To get the coeffcient values
%To find the temp val, using E charaacter to identify the position
X_1=findstr(data1,'E');

%a1,a2,a3,a4,a5 are the temp obtained from species 2nd line
%num characters are in string. To convert into num str2num is used
a1=str2num(data1(1:(X_1(1)+3)));
a2=str2num(data1(X_1(1)+4:X_1(2)+3));
a3=str2num(data1(X_1(2)+4:X_1(3)+3));
a4=str2num(data1(X_1(3)+4:X_1(4)+3));
a5=str2num(data1(X_1(4)+4:X_1(5)+3));

data1=fgetl(k);%To get the coeffcient values
X_2=findstr(data1,'E');

%a6,a7,a8,a9,a10 are the temp obtained from species 3rd line
%num characters are in string. To convert into num str2num is used
a6=str2num(data1(1:X_2(1)+3));
a7=str2num(data1(X_2(1)+4:X_2(2)+3));
a8=str2num(data1(X_2(2)+4:X_2(3)+3));
a9=str2num(data1(X_2(3)+4:X_2(4)+3));
a10=str2num(data1(X_2(4)+4:X_2(5)+3));

data1=fgetl(k);%To get the coeffcient values
X_3=findstr(data1,'E');%X_3 is the cofficient string of line 4

%a11,a12,a13,a14 are the temp obtained from species 4th line
%num characters are in string. To convert into num str2num is used
a11=str2num(data1(1:X_3(1)+3));
a12=str2num(data1(X_3(1)+4:X_3(2)+3));
a13=str2num(data1(X_3(2)+4:X_3(3)+3));
a14=str2num(data1(X_3(3)+4:X_3(4)+3));
% 
% %Declaring the Gas constant value
R=8.3145

%Dividing the temperature value from low to high into 150 equal space
T=linspace(Low_temp_1,High_temp_1,150)

for n=1:length(T)

    %If the temperature value is greater than the mid temperature then
    %higher temperature coefficients values a1,a2,a3,a4,a5,a6,a7 goes for
    %specific heat, Enthalpy & Entropy calculation
if T(n)>Mid_temp_1
    cp=(a1+(a2.*T)+(a3.*(T.^2))+(a4.*(T.^3))+(a5.*(T.^4)))*R;
    H=(a1+(a2.*T)./2+(a3.*T.^2)./3+(a4.*T.^3)./4+(a5.*T.^4)./5+(a6./T))*R.*T;
    S=(a1*log(T)+a2.*T+(a3.*T.^2)./2+(a4.*T.^3)/3+(a5.*T.^4)./4+a7)*R;
else
    
    %else the temperature value is lower than the mid temperature then
    %higher temperature coefficients values a8,a9,a10,a11,a12,a13,a14 goes for
    %specific heat, Enthalpy & Entropy calculation
    cp=(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4)*R;
    H=(a8+(a9.*T)./2+(a10.*T.^2)./3+(a11.*T.^3)./4+(a12.*T.^4)./5+(a13./T))*R.*T;
    S=(a8*log(T)+a9.*T+(a10.*T.^2)./2+(a11.*T.^3)/3+(a12.*T.^4)./4+a14)*R;
end
end


%to create individual folder for each species
t=sprintf('%s',Species);
mkdir(t)
cd(t)

%Plotting Temperature and specific heat graph
figure(1)
plot(T,cp)
grid on
xlabel('Temperature(K)')
ylabel('Specific heat(J/KgK)')
title('Specific heat vs Temperature')
%saving the graph
saveas(1,'specificheat.jpg')

%Plotting Temperature and Enthalpy graph
figure(2)
plot(T,H)
grid on
xlabel('Temperature(K)')
ylabel('Enthalpy(J/mol)')
title('Enthalpy vs Temperature')
%saving the graph
saveas(2,'Enthalpy.jpg')

%Plotting Temperature and Entropy graph
figure(3)
plot(T,S)
grid on
xlabel('Temperature(K)')
ylabel('Entropy(J/K)')
title('Entropy vs Temperature')
%saving the graph
saveas(3,'Entropy.jpg')

cd ..


M_w=Molecular(Species);

sprintf('Molecular weight of %s:%f',Species,M_w)

end


%Molecular weight calculation function


function[M_w]=Molecular(Species)

%Totally 5 elemnts are there in that file
Elements=['H','C','O','N','A'];
%declaring the molecular weight of those 5 elements.
Atomic_weight=[1.0079 12.0107 15.999 14.0067 39.948];

%Initialising molecular weight as 0
M_w=0;

%To compare the characters of species and atomic weight nested for loopused

for l=1:length(Species)
    for m=1:length(Atomic_weight)
        if strcmp(Species(l),Elements(m))
            M_w=M_w+Atomic_weight(m);
%some elements contained in a species for morethan one time. To calculate those repeated elements molecular weight, storing the values of m to position variable            
            position=m;
        end
    end

%converting the string into number characters
     N=str2num(Species(l));
%Those characters converted into number which is greater than 1 enters the loop      
     if N>1
         M_w=M_w+Atomic_weight(position)*(N-1);
         
     end
  
end
end
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.