Project 1 - Parsing NASA thermodynamic data
close all;
clear all;
clc;
%step:1
%opening the data file
data_thermo = fopen('THERMO (1).dat')
%Extrating Heading From First line
Header=fgetl(data_thermo);
%Moving to next line
A1=fgetl(data_thermo);
%extracting global temperation from that line
temp=strsplit(A1,' ');
Global_low_temp=str2num(temp{2});
Global_mid_temp=str2num(temp{3});
Global_high_temp=str2num(temp{4});
%step:2
%There are 3 comments in next 3 line. So we need Skip those lines
for i=1:3
fgetl(data_thermo);
end
%step:3
%moving to Next line
A2=fgetl(data_thermo);
%searching to get location of local temperature
a=strfind(A2,'G');
temp=strsplit(A2(a+1:end-1),' ');
local_low_temp=str2num(temp{2});
local_mid_temp=str2num(temp{3});
local_high_temp=str2num(temp{4});
Temp_range=linspace(local_low_temp,local_high_temp,200)
%STEP:4
%creating a new folder
mkdir parsing_data
%STEP:5
%for-loop is to extract all the data of 53 compound
for j=1:53
%name of compound at begining of line command strtok to get first
%string
species=strtok(A2);
%moving inside the folder parsing_data
cd parsing_data;
%creating new folder named species
mkdir(species);
%moing inside the time species folder
cd(species);
%pointer to next 3 lines
A3=fgetl(data_thermo);
A4=fgetl(data_thermo);
A5=fgetl(data_thermo);
%'E' is present in all data. so searching the location of E and with
%its reference location is provied
a=strfind(A3,'E');
b=strfind(A4,'E');
c=strfind(A5,'E');
%step:6
%High temperature coefficient
a1=str2num(A3(1:a(1)+3));
a2=str2num(A3(a(1)+4:a(2)+3));
a3=str2num(A3(a(2)+4:a(3)+3));
a4=str2num(A3(a(3)+4:a(4)+3));
a5=str2num(A3(a(4)+4:a(5)+3));
a6=str2num(A4(1:b(1)+3));
a7=str2num(A4(b(1)+4:b(2)+3));
%low temperature coeff
b1=str2num(A4(b(2)+4:b(3)+3));
b2=str2num(A4(b(3)+4:b(4)+3));
b3=str2num(A4(b(4)+4:b(5)+3));
b4=str2num(A5(1:c(1)+3));
b5=str2num(A5(c(1)+4:c(2)+3));
b6=str2num(A5(c(2)+4:c(3)+3));
b7=str2num(A5(c(3)+4:c(4)+3));
%step 7
%calling fuction to calculate specific heat , Enthalpy and Entropy
[cp,enthalpy,entropy]=species_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,Temp_range);
%calling function to calculate molecular weight of compound
weight=molecular_weight(species);
display(weight,species);
%step8
%ploting figure and saving in JPEG format
figure(1)
plot(Temp_range,cp,'Color','r','LineWidth',3);
xlabel('Temperature');
ylabel('Specific heat');
saveas(1,'specific heat vs Temperature.jpg');
figure(2)
plot(Temp_range,enthalpy,'Color','g','lineWidth',3);
xlabel('Temperature');
ylabel('Enthalpy');
saveas(2,'Enthalpy vs Temperature.jpg');
figure(3)
plot(Temp_range,entropy,"Color",'m','LineWidth',3);
xlabel('Temperature');
ylabel('Entropy');
saveas(3,'Entropy vs Temperature.jpg');
%step:9
%moving to folder Matlab i.e. Foldr inside which folder parsing_data
%is
cd('C:\Users\91976\Documents\MATLAB');
%moving to next line
A2=fgetl(data_thermo);
end
specific heat :
function[cp,enthalpy,entropy]=species_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,Temp_range)
R=8.3145;
T=Temp_range;
if Temp_range<200
cp=[a1+a2.*(T)+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4)]*R;
enthalpy=[a1+a2.*(T./2)+a3.*(T.^2)./3+a4.(T.^3)./4+a5.*(T.^4)./5+a6./T]*R.*T;
entropy=[a1*log(T)+a2.*(T)+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7]*R;
else
cp=[b1+b2.*(T)+b3.*(T.^2)+b4.*(T.^3)+b5.*(T.^4)]*R;
enthalpy=[b1+b2.*(T./2)+b3.*(T.^2)./3+b4*(T.^3)./4+b5.*(T.^4)./5+b6./T]*R.*T;
entropy=[b1.*log(T)+b2.*(T)+b3.*(T.^2)./2+b4.*(T.^3)./3+b5.*(T.^4)./4+a7]*R;
end
molecular weight :
function[Wt]=molecular_weight(A)
atom=['C','H','O','N'];
mass=[12 1 16 14];
Wt=0;
for i=1:length(A)
for j=1:length(atom)
if A(i)==atom(j)
Wt=Wt+mass(j);
z=j;
end
end
b=str2num(A(i));
if isempty(b)==1
;
else
Wt=Wt+(b-1)*mass(z);
end
end
report :
AIM : File parsing nasa data.
File parsing refers to how information can be read or extreacted from a particular file and how that information can be used in a computer program .with the help of file parsing we can read data from external files and databases and we use use that information or data in our calculations.
Here in this project we will be using the concept of data parsing to extract the coefficients from a text file . with the help of these coefficients the thermodynamic properties such specific heat [CP] enthalpy [H] and entropy [S] will be evaluated for different species given the fille .
Certain formulae will be used to calculate these thermodynamic properties which are as follows
CP/R=a1+a2*T+a3*T^2+a4*T^3+a5*T^4
H/R=a1+a2*T/2+a3*T^2/3+a4*T^3/4+a5*T^4/5+a6/T
S/R=a1*inT+a2*T+a3*T^2/2+a4*T^3
Code :
close all;
clear all;
clc;
%step:1
%opening the data file
data_thermo = fopen('THERMO (1).dat')
%Extrating Heading From First line
Header=fgetl(data_thermo);
%Moving to next line
A1=fgetl(data_thermo);
%extracting global temperation from that line
temp=strsplit(A1,' ');
Global_low_temp=str2num(temp{2});
Global_mid_temp=str2num(temp{3});
Global_high_temp=str2num(temp{4});
%step:2
%There are 3 comments in next 3 line. So we need Skip those lines
for i=1:3
fgetl(data_thermo);
end
%step:3
%moving to Next line
A2=fgetl(data_thermo);
%searching to get location of local temperature
a=strfind(A2,'G');
temp=strsplit(A2(a+1:end-1),' ');
local_low_temp=str2num(temp{2});
local_mid_temp=str2num(temp{3});
local_high_temp=str2num(temp{4});
Temp_range=linspace(local_low_temp,local_high_temp,200)
%STEP:4
%creating a new folder
mkdir parsing_data
%STEP:5
%for-loop is to extract all the data of 53 compound
for j=1:53
%name of compound at begining of line command strtok to get first
%string
species=strtok(A2);
%moving inside the folder parsing_data
cd parsing_data;
%creating new folder named species
mkdir(species);
%moing inside the time species folder
cd(species);
%pointer to next 3 lines
A3=fgetl(data_thermo);
A4=fgetl(data_thermo);
A5=fgetl(data_thermo);
%'E' is present in all data. so searching the location of E and with
%its reference location is provied
a=strfind(A3,'E');
b=strfind(A4,'E');
c=strfind(A5,'E');
%step:6
%High temperature coefficient
a1=str2num(A3(1:a(1)+3));
a2=str2num(A3(a(1)+4:a(2)+3));
a3=str2num(A3(a(2)+4:a(3)+3));
a4=str2num(A3(a(3)+4:a(4)+3));
a5=str2num(A3(a(4)+4:a(5)+3));
a6=str2num(A4(1:b(1)+3));
a7=str2num(A4(b(1)+4:b(2)+3));
%low temperature coeff
b1=str2num(A4(b(2)+4:b(3)+3));
b2=str2num(A4(b(3)+4:b(4)+3));
b3=str2num(A4(b(4)+4:b(5)+3));
b4=str2num(A5(1:c(1)+3));
b5=str2num(A5(c(1)+4:c(2)+3));
b6=str2num(A5(c(2)+4:c(3)+3));
b7=str2num(A5(c(3)+4:c(4)+3));
%step 7
%calling fuction to calculate specific heat , Enthalpy and Entropy
[cp,enthalpy,entropy]=species_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,Temp_range);
%calling function to calculate molecular weight of compound
weight=molecular_weight(species);
display(weight,species);
%step8
%ploting figure and saving in JPEG format
figure(1)
plot(Temp_range,cp,'Color','r','LineWidth',3);
xlabel('Temperature');
ylabel('Specific heat');
saveas(1,'specific heat vs Temperature.jpg');
figure(2)
plot(Temp_range,enthalpy,'Color','g','lineWidth',3);
xlabel('Temperature');
ylabel('Enthalpy');
saveas(2,'Enthalpy vs Temperature.jpg');
figure(3)
plot(Temp_range,entropy,"Color",'m','LineWidth',3);
xlabel('Temperature');
ylabel('Entropy');
saveas(3,'Entropy vs Temperature.jpg');
%step:9
%moving to folder Matlab i.e. Foldr inside which folder parsing_data
%is
cd('C:\Users\91976\Documents\MATLAB');
%moving to next line
A2=fgetl(data_thermo);
end
specific heat function :
function[cp,enthalpy,entropy]=species_heat(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,Temp_range)
R=8.3145;
T=Temp_range;
if Temp_range<200
cp=[a1+a2.*(T)+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4)]*R;
enthalpy=[a1+a2.*(T./2)+a3.*(T.^2)./3+a4.(T.^3)./4+a5.*(T.^4)./5+a6./T]*R.*T;
entropy=[a1*log(T)+a2.*(T)+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7]*R;
else
cp=[b1+b2.*(T)+b3.*(T.^2)+b4.*(T.^3)+b5.*(T.^4)]*R;
enthalpy=[b1+b2.*(T./2)+b3.*(T.^2)./3+b4*(T.^3)./4+b5.*(T.^4)./5+b6./T]*R.*T;
entropy=[b1.*log(T)+b2.*(T)+b3.*(T.^2)./2+b4.*(T.^3)./3+b5.*(T.^4)./4+a7]*R;
end
molecular function :
function[Wt]=molecular_weight(A)
atom=['C','H','O','N'];
mass=[12 1 16 14];
Wt=0;
for i=1:length(A)
for j=1:length(atom)
if A(i)==atom(j)
Wt=Wt+mass(j);
z=j;
end
end
b=str2num(A(i));
if isempty(b)==1
;
else
Wt=Wt+(b-1)*mass(z);
end
end
Explainatoin of code
STEP 1
The code Is opening the data file using command open and it is going to first line of page i.e header using command get . it is extracting the header of fille from first line .then moving to next line extracting it this line contain global temperature in string from. Command strsplit is used to extract different temperature separated by white space and then it is converted into number using str2num command
STEP 2
Next three line are comment so; those lines are skipped
STEP 3
The is moving to next line and finding “G” using command strfind because local high low and mid temperature is just after G the code is extracting those temperature the code also creating array of temperature from local low temperature to local high temperature
STEP 4
The code is crating new folder parsing data using command mkdir
STEP 5
Since there are 53 compound in the fille a loop is run strtok command is used to extract first string of the line which is species name I.e name of compound . the code creates a folder by name of compound and movies inside it. The code moves to next three lines using different variable and search for ‘E’ because every contains it. Once we the code know the index of ‘E’ the rest of data can be extracted using suitable index .
STEP 6
All the coefficient of are extracted using suitable index and then converted from string to number.
STEP 7
Functions for the calculation of specitic heat enthalpy entropy and molecular weight are called
STEP 8
Graph is piotted for specific heat enthalpy and entropy against temperature range and and saved in particular species folder in JEPG format.
Results & conclusion :
Hence these are the final results & charts of the program.