File Parsing and Data analysis in MATLAB
File Parsing NASA Thermodynamic data form THERMO.dat file
objective:
- To write a Matlab code to extract the data from the file(Thermo.dat).
- To calculate and plot the Cp,h,s for the all the gas species for a entire range of there local temperature(TminL- TmaxL )
- Calculate the molecular weight of the species and display on command window.
- Save the plots for each species and save them in a seperate folder(generated automatically).
- Attach plot for O2, N2 & Co2.
Theory:
File writting
It refers to the process of writting data to a particular file
Syntax: f1=fopen(\'file_name.txt\', \'w\')
where w- writing mode and f1 is the pointer to a file with which we can write data to the file.
Note:
- f1 gives some value which doesnot matters but should not be -ve.
- we use fclose(f1) to close the file.
1)File printf command is use to write inside a file.
syntax: fprintf(f1,\'hello \')
2)For adding float value point numbers syntax used is
syntax: fprintf(f1,\'%f\', 1.5) or fprintf(f1,\'%f %f\', 1.5,2.0)
3)Adding a new line
syntax: fprintf(f1,\'\\n\')
4)adding a header to the file
syntax:fprintf(f1,\'%16s %16 s\',\'Time\',\' Pressure\')
where \'16\' - 16 character spaces before time & %s is string formate specifier.
5)Adding places of digit and decimal no.places
syntax:fprintf(f1,\'%16f %16 f\\n\',var1, var2)
File Reading
It refers to the process of reading data from a particular file.
Syntax: f1=fopen(\'file_name.txt\', \'r\')
where r- Reading mode and f1 is the pointer to a file with which we can read data to the file.
Note:
- f1 gives some value which doesnot matters but should not be -ve.
- we use fclose(f1) to close the file.
1) fgetl( ) command is used to get the content of the file line by line.
syntax: Line=fgetl(f1); where line will store the content of the line by line
2) In many cases we need split a line to extract data therefore we use strsplit() command.
eg.line=\'200 300 400\';
A=strsplit(line,\' \');
where \' \' is delimter (i.e. space in this case) the A {} will contain string in cell array {200},{300},{400}.
3) To convert string to number we use str2num(), str2double() command (for cell array use {})
eg. a=str2num(A{1});
For the given challenge we are given a NASA Thermodynamic data file name THERMO.dat
From which we have to read the data and calculate the cp(specific heat (cp), Enthalpy(h), Entropy(s) & molecular weight( mol.wt).
There are all total of 53 gas species with 14 cofficient for each defined under there individual local temperature range. we need to extract all the data from the file and calculate the value of cp h , s and mol. wt .
Steps to extract data from the file.
1) Part 1: In a row with species name contain \'1\' in its last array which helps in calculating the no. of species in the entire file. so we can run the while loop till th end of file to get the no. of species.
2) Part 2: To get the global Temperature by spliting the line by space delimter and conversion of string in cell array into the form of number.
3) Part 3: Running a for loop for 3 time to skip 3 lines in the file.
4) Part 4: storing all species name in s_name a array & getting the values of all the species 14 cofficient values for furthur calculation.
Dividing the file into a pattern of 4 lines i.e 1st line contains species name and local temperature and rest three contains cofficients.
For every line get the position of \'E\' such that 1st coffn of line is acquire by c1(1) str2num(L2(1:E_pos(1)+3)); and rest by str2num(L2(E_pos(1)+4:E_pos(2)+3))
smilarly for rest of the three lines.For applying the Same logic for all the species run a loop for 53 time stored in \'nos\' by following part1:
mkdir helps in making directory and running inside a loop with syntax: mkdir(\'gasplot\',s_name{i}) help you to create sub directory for every species name inisde a folder name \'gasplot\'.
5) Part 5: in this we will define call function for cp , h & s who\'s expression and condition are given in the question
6)Part 6: plotting the figure plots for each species and saving them into a there sub folder.
used cd command to change directory for each species and saveas command to save the plot in there respective folders.
7)part 7: Molecular wt calculation and diplay on command window and display plot for a particular gas species. For doing so we used strcmpi()-case insenstive that search out the species name listed in the file and furthur used for mol.weight calculation.
Matlab code for file parsing data from NASA Thermo.dat file
close all
clear all
clc
% part1: calculate no. of species
f1=fopen(\'THERMO.dat\',\'r\');
nos=0;
while~feof(f1)
L=fgetl(f1);
Ts=strsplit(L,\' \');
q=length(Ts);
if(strcmpi(Ts{q},\'1\'))
nos=nos+1;
end
end
%part 2 get values for global temperature
f1=fopen(\'THERMO.dat\',\'r\');
fgetl(f1);
l2=fgetl(f1);
Ts=strsplit(l2,\' \');
for i =1:4
Global_Temp(i)=str2double(Ts{i});
end
%part 3:skip 3 lines
for i=1:3
fgetl(f1);
end
% part :4Data extraction of all 14 cofficient from the file for all species
for i=1:nos
L1=fgetl(f1);
s=strsplit(L1,\' \');
s_name{i} = s{1};
G_pos=findstr(L1,\'G \');
p=strsplit(L1(G_pos:end),\' \');
t1_loc(i)=str2num(p{2});
t2_loc(i)=str2num(p{3});
t3_loc(i)=str2num(p{4});
L2=fgetl(f1);
E_pos=findstr(L2,\'E\');
c1(i)=str2num(L2(1:E_pos(1)+3));
c2(i)=str2num(L2(E_pos(1)+4:E_pos(2)+3));
c3(i)=str2num(L2(E_pos(2)+4:E_pos(3)+3));
c4(i)=str2num(L2(E_pos(3)+4:E_pos(4)+3));
c5(i)=str2num(L2(E_pos(4)+4:E_pos(5)+3));
L3=fgetl(f1);
pos=findstr(L3,\'E\');
c6(i)=str2num(L3(1:E_pos(1)+3));
c7(i)=str2num(L3(E_pos(1)+4:E_pos(2)+3));
c8(i)=str2num(L3(E_pos(2)+4:E_pos(3)+3));
c9(i)=str2num(L3(E_pos(3)+4:E_pos(4)+3));
c10(i)=str2num(L3(E_pos(4)+4:E_pos(5)+3));
L4=fgetl(f1);
pos=findstr(L4,\'E\');
c11(i)=str2num(L4(1:E_pos(1)+3));
c12(i)=str2num(L4(E_pos(1)+4:E_pos(2)+3));
c13(i)=str2num(L4(E_pos(2)+4:E_pos(3)+3));
c14(i)=str2num(L4(E_pos(3)+4:E_pos(4)+3));
mkdir(\'gasplot\',s_name{i})
end
% part 5: function defination for cp , h, s
for n=1:nos
tmax=t2_loc(n);
tmin=t1_loc(n);
t_range=linspace(tmin,tmax,50);
cp=cp_calculation(c1(n),c2(n),c3(n),c4(n),c5(n),c8(n),c9(n),c10(n),c11(n),c12(n),t_range,t1_loc(n),t2_loc(n),t3_loc(n));
h=H_calculation(c1(n),c2(n),c3(n),c4(n),c5(n),c6(n),c8(n),c9(n),c10(n),c11(n),c12(n),c13(n),t_range,t1_loc(n),t2_loc(n),t3_loc(n));
s=s_calculation(c1(n),c2(n),c3(n),c4(n),c5(n),c6(n),c7(n),c8(n),c9(n),c10(n),c11(n),c12(n),c13(n),c14(n),t_range,t1_loc(n),t2_loc(n),t3_loc(n));
%part6: plotting the subplot for all the species
figure(1)
subplot(2,2,1)
plot(t_range,cp,\'linewidth\',2)
xlabel(\'Temperature(k)\');
ylabel(\'specific heat(Cp)\')
title(\'Cp value vs Temperature \')
subplot(2,2,2)
plot(t_range,h, \'linewidth\',2)
xlabel(\'Temperature(k)\');
ylabel(\'Enthalpy(h)\')
title(\'Enthalpy vs Temperature\')
subplot(2,2,3)
plot(t_range,s,\'linewidth\',2)
xlabel(\'Temperature(k)\');
ylabel(\'Entropy(s)\')
title(\'Entropy vs Temperature\')
cd \'C:\\Users\\Skill_Lync\\Documents\\MATLAB\\gasplot\'
path=cd (s_name{n});
saveas(figure(1),\'plot\',\'png\')
cd \'C:\\Users\\Skill_Lync\\Documents\\MATLAB\\gasplot\'
end
%part7: Molecular wt calculation and plot display for a particular gas species
%Taking input from user
gas_name=input(\'Enter the species name \', \'s\');
for i=1:length(s_name)
c=strcmpi(gas_name, s_name(i));
if (c==1)
n=i;
wt= mol_wt(gas_name);% function to calculate the mol. wt
disp(\'The molecular wt.for the gas is:\')
disp(wt)
tmax=t2_loc(n);
tmin=t1_loc(n);
t_range=linspace(tmin,tmax,50);
cp=cp_calculation(c1(n),c2(n),c3(n),c4(n),c5(n),c8(n),c9(n),c10(n),c11(n),c12(n),t_range,t1_loc(n),t2_loc(n),t3_loc(n));
h=H_calculation(c1(n),c2(n),c3(n),c4(n),c5(n),c6(n),c8(n),c9(n),c10(n),c11(n),c12(n),c13(n),t_range,t1_loc(n),t2_loc(n),t3_loc(n));
s=s_calculation(c1(n),c2(n),c3(n),c4(n),c5(n),c6(n),c7(n),c8(n),c9(n),c10(n),c11(n),c12(n),c13(n),c14(n),t_range,t1_loc(n),t2_loc(n),t3_loc(n));
figure(2)
subplot(2,2,1)
plot(t_range,cp,\'linewidth\',2)
xlabel(\'Temperature(k)\');
ylabel(\'specific heat(Cp)\')
title(\'Cp value vs Temperature \')
subplot(2,2,2)
plot(t_range,h, \'linewidth\',2)
xlabel(\'Temperature(k)\');
ylabel(\'Enthalpy(h)\')
title(\'Enthalpy vs Temperature\')
subplot(2,2,3)
plot(t_range,s,\'linewidth\',2)
xlabel(\'Temperature(k)\');
ylabel(\'Entropy(s)\')
title(\'Entropy vs Temperature\')
break
end
end
if (c==0)
disp(\'Not in the list\')
end
Matlab code for the function that calculates cp(specific heat) for the range of temperature.
function [cp]= cp_calculation(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,t_range,t1,t2,t3)
R=8.314;
tmax=t2;
tmid=t3;
tmin=t1;
for i=1:length(t_range)
t=t_range(i);
if (tmax>=t)&&(t>=tmid)
cp(i)=(c1+(c2*t)+(c3*t^2)+(c4*t^3)+(c5*t^4))*R;
elseif (tmid>t)&&(t>=tmin)
cp(i)=(c8+(c9*t)+(c10*t^2)+(c11*t^3)+(c12*t^4))*R;
else
cp=\' not in range\';
disp(\'out of range\');
end
end
From the above function the calculation for the cp is done over a range of local temperatures
the 1st 7 cofficient(C1 to C7) are used for the higher temperature range and next 7(C8 to C14) are used for lower temperature range.
Matlab code for the function that calculates h(enthalpy) for the range of temperature.
function [H]= H_calculation(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,t_range,t1,t2,t3)
R=8.314;
tmax=t2;
tmid=t3;
tmin=t1;
for i=1:length(t_range)
t=t_range(i);
if (tmax>=t)&&(t>=tmid)
H(i)=(c1+((c2*t)/2)+((c3*t^2)/3)+((c4*t^3)/4)+((c5*t^4)/5)+(c6/t))*R*t;
elseif (tmid>t)&&(t>=tmin)
H(i)=(c8+((c9*t)/2)+((c10*t^2)/3)+((c11*t^3)/4)+((c12*t^4)/5)+(c13/t))*R*t;
else
disp(\'out of range\');
end
end
From the above function the calculation for the h(enthalpy) is done over a range of local temperatures
the 1st 7 cofficient(C1 to C7) are used for the higher temperature range and next 7(C8 to C14) are used for lower temperature range.
Matlab code for the function that calculates s (entropy) for the range of temperature.
function [S]= s_calculation(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,t_range,t1,t2,t3)
R=8.314;
tmax=t2;
tmid=t3;
tmin=t1;
for i=1:length(t_range)
t=t_range(i);
if (tmax>=t)&&(t>=tmid)
S(i)=((c1*log(t))+(c2*t)+((c3*t^2)/2)+((c4*t^3)/3)+((c5*t^4)/4)+c7)*R;
elseif (tmid>t)&&(t>=tmin)
S(i)=((c8*log(t))+(c9*t)+((c10*t^2)/2)+((c11*t^3)/3)+((c12*t^4)/4)+c14)*R;
else
disp(\'out of range\');
end
end
From the above function the calculation for the s(entropy) is done over a range of local temperatures
the 1st 7 cofficient(C1 to C7) are used for the higher temperature range and next 7(C8 to C14) are used for lower temperature range.
Matlab code for the function that calculate the molecular weight for the given gas-species .
function [wt]= mol_wt(g_name)
E_name=[\"O\",\"C\",\"H\",\"N\",\"Ar\"];
E_weight=[15.9994,12.011,1.00797,14.0067,39.948];
wt=0;
for i=1:length(g_name)
for j=1:length(E_name)
tf=strcmpi(g_name(i),E_name(j));
if(tf==1)
wt=wt+E_weight(j);
x=E_weight(j);
end
end
num=str2num(g_name(i));
if(num>0)
wt=wt+(num-1)*x;
end
end
From the above function the calculation for the molecular wt is done. The different element are stored in array called E_name and their respective weight in array E_weight. The string are compared and there particular weight is calculated.
Below also attached the screeshot of workspace to compare the variables where s_name contain all species name in (1X53)matrix and c1,c2.......c14 contains value store for the cofficient for all species.
Results and Attachments
1) Cp, h, s plots vs temperature range for O2 gas .(shown below)
2) Cp, h, s plots vs temperature range for N2 gas .(shown below)
3) Cp, h, s plots vs temperature range for CO2 gas .(shown below)
4)Screen shot of folder and sub-folder with individual species name
5) Command window displaying the molecular weight for species name entered by user(shown below)
Note: if it is required to display the molecular weight for all the species then we just need to run the for loop from i= 1 to length(s_name) for all s_name(i) instead of taking the gas name as input.
Conclusion: So with the help of the method called file parsing where we can extract the data from a file in a reading mode and furthur that data can be used for calculation of properties, values etc.. Additional those extracted data is can also be converted in plots or graphs which help us to analyize information in a quick detailed and an organized way.