Project 1 - Parsing NASA thermodynamic data
NASA Thermodynamic File Parsing
Steps followed for Coding:
- Firstly, using fopen command we are reading the thermo.dat file and to read every line in the file we are using fgets command.
- Using fget command we are reading the next line from there we are getting the global temperatures.
- Here strsplit command is used to separate the low, medium and high temperature using a delimitor.
- After skipping the next three lines of comments, I am creating a loop to read and parse all the 14 coefficients where we use them to find enthalpy ,entropy and Specific heat.
- Inside the loop we are having strfind command where it has two arguments, one is the line to find and the 2nd argument is the letter or delimitor which we had to find.
- Also getting a species_name also to name the folder.
- Using str2double command we had separated the local low, med and high temperature with respect to the every species.
- By using strfind command we are parsing the coefficients and storing in the separate variables such as a1H,a2H,…etc,..
- Having the conditional statement that the temperature is greater than the average temperature then we use first 7 co-efficients and if it is less then we use last 7 co-efficients.
- Creating a directory to save 3 plots for all the species using mkdir command
- Using the plot command the plots are generated
- Using save as command all the 3 plots are saved inside the respective folder.
- Finally, file is closed using fclose command.
clear all
close all
clc
f1 = fopen('THERMO.dat','r');
header= fgetl(f1);
%Reading global temperatures
Global_temp = fgetl(f1);
C =strsplit(Global_temp,' ');
global_lowtemp = str2double(C{2});
global_midtemp = str2double(C{3});
global_hightemp = str2double(C{4});
%Skipping next 3 lines
fgetl(f1);
fgetl(f1);
fgetl(f1);
for i=1:i+4:213
line = fgetl(f1);
A = strsplit(line,' ');
species = A(1);
species_name = char(species);
if i==213
break
end
%Parsing the first line of every species
Q = strfind(line,'G')
local_lowtemp = line(Q+4:55);
local_midtemp = line(Q+13:65);
local_hightemp = line(Q+23:75);
Local_TempL = str2double(local_lowtemp);
Local_TempH= str2double(local_midtemp);
Local_TempM = str2double(local_hightemp);
T =linspace(Local_TempL,Local_TempH,500);
R = 8.314;
if (i >=2 || Q)
cfline1= fgetl(f1);
X=strfind(cfline1,'E'); %parsing rest of the 3 lines in the species
a1=cfline1(1:X(1)+3);
a1H=str2double(a1);
a2=cfline1(X(1)+4:X(2)+3);
a2H=str2double(a2);
a3=cfline1(X(2)+4:X(3)+3);
a3H=str2double(a3);
a4=cfline1(X(3)+4:X(4)+3);
a4H=str2double(a4);
a5=cfline1(X(4)+4:X(5)+3);
a5H=str2double(a5);
cfline2=fgetl(f1);
X2=strfind(cfline1,'E');
a6=cfline2(1:X2(1)+3);
a6H=str2double(a6);
a7=cfline2(X2(1)+4:X2(2)+3);
a7H=str2double(a7);
a8=cfline2(X2(2)+4:X2(3)+3);
a8=str2double(a8);
a9=cfline2(X2(3)+4:X2(4)+3);
a9=str2double(a9);
a10=cfline2(X2(4)+4:X2(5)+3);
a10=str2double(a10);
cfline3=fgetl(f1);
X3=strfind(cfline1,'E');
a11=cfline3(1:X3(1)+3);
a11=str2double(a11);
a12=cfline3(X3(1)+4:X3(2)+3);
a12=str2double(a12);
a13=cfline3(X3(2)+4:X3(3)+3);
a13=str2double(a13);
a14=cfline3(X3(3)+4:X3(4)+3);
a14=str2double(a14);
end
for j =1:length(T) %For range of temperatures
if T >=Local_TempM
Cp = R*(a1H+(a2H.*T)+(a3H.*T.^2)+(a4H.*T.^3)+(a5H.*T.^4));
H =R.*T*(a1H+(a2H.*T./2)+(a3H.*T.^2./3)+(a4H.*T.^3./4)+(a5H.*T.^4./5)+(a6H./T));
S = R.*((a1H.*log(T))+(a2H.*T)+(a3H.*T.^2./2)+(a4H.*T.^3./3)+(a5H.*T.^4./4)+(a7H));
else
Cp = R.*(a8+(a9.*T)+(a10.*T.^2)+(a11.*T.^3)+(a12.*T.^4));
H =R.*T.*(a8+(a9.*T./2)+(a10.*T.^2./3)+(a11.*T.^3./4)+(a12.*T.^4./5)+(a13./T));
S = (R)*((a8*log(T))+(a9*T)+(a10*T.^2/2)+(a11*T.^3/3)+(a12*T.^4/4)+(a14));
end
end
%Temperature vs entropy
figure(1);
plot(T,S,'linewidth',3,'color','b');
title(species_name);
xlabel('Temperature');
ylabel('Entropy');
%Temperature vs enthalpy
figure(2);
plot(T,H,'linewidth',3,'color','b');
title(species_name);
xlabel('Temperature');
ylabel('Enthalpy');
%Temperature vs specific heat
figure(3);
plot(T,Cp,'linewidth',3,'color','b');
title(species_name);
xlabel('Temperature');
ylabel('SPecific heat');
%making a directory to save the folders
mkdir('D:\MEng -Mech UC\SKill Lync Course\Matlab Challanges Code\File Parsing\File_Plots\',species_name);
directory = (['D:\MEng -Mech UC\SKill Lync Course\Matlab Challanges Code\File Parsing\File_Plots\',species_name]);
cd(directory)
%Saving the plots
saveas(1,'Entropy.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Specific heat.jpg');
%coming out of the folder for another species
cd('D:\MEng -Mech UC\SKill Lync Course\Matlab Challanges Code\File Parsing');
%Calculating the molecular weight for every species
Mol_Mass = molecular_mass(species_name)
end
fclose(f1);
function Mol_Mass = molecular_mass(species_name)
species = upper(species_name)
Constituents =['O','A','H','C','N'];
Atomic_weight = [15.9994 39.948 1.00794 12.0107 14.0067];
Mol_Mass =0 ;
for i =1:length(species)
for k =1:length(Constituents)
if(strcmp(species(i),Constituents(k)))
Mol_Mass = Mol_Mass + Atomic_weight(k);
u = k;
end
end
n = str2num(species(i));
if n>1
Mol_Mass =Mol_Mass + Atomic_weight(u)*(n-1);
end
end
end