nasa file parsing
Aim:To read the thermodat file and make use of that values to plot variation of various thermodynamic properties with temperature.Also to calculate the molecular mass of the required specie and store their plot in specific folders automatically created on their name.
CODE: In code the temperature i.e global min ,mid and max temperature in the header of the file are extracted by using strsplit to split the line containing the three temperatures and space is used to split them and 'str2double' is used to convert string to floating point number.
For specie name fgetl is used to read the line containing the name of the specie and strsplit is used to split all the words and 'strcmp(a,b)' is used to compare according to the input of the user.
Each coefficient is extracted using their position relative to each \'E\' in line .findstr command is used to find \'E\' and as each coefficient has fixed decimal places so relative position of starting and ending digit can be found and then 'str2double' is used to convert it to a floating point number
for molecular mass 'strcmp' is used to compare various members of the name of specie and the set of elements and logic behind is that if any string member in specie name matches with the input element its coreesponding mass is added to the molecular mass which is initially set to 0 ,and c is count of number of times the same element appears.
for saving the plot in folder mkdir command is used to make new directory of the name same as that of specie and cd commond is used to browse between different directories.https://drive.google.com/drive/folders/1oLnabLjAOKu7fOCIKmgbLimB2Dhx3BUL?usp=sharing
%MAIN PROGRAM
close all
clear all
clc
%Opening file
f = fopen('thermodat.txt','r');
%storing
dir_name = 'NASA data';
mkdir(dir_name)
%variables
x = 0;
i = 0;
T = linspace(300,5000,100);
%reading the temperature value
temp = fgetl(f);
temp = fgetl(f);
temp1 = strsplit(temp,' ');
global_low_temp = str2double(temp1{2});
global_mid_temp = str2double(temp1{3});
global_high_temp = str2double(temp1{4});
GLT = linspace(global_low_temp,global_mid_temp,global_mid_temp-global_low_temp+1);
GHT = linspace(global_mid_temp,global_high_temp,global_high_temp-global_mid_temp+1);
GLT = GLT';
GHT = GHT';
%Enter the Specie Formula (ALL CAPS)
speciesname = 'H2O';
%looping the data to acquire the required block
while x == 0
b = fgetl(f);
c = strsplit(b,' ');
d = c(1);
x = strcmp(speciesname,d);
i = i+1;
end
%Reading Coefficients
b1 = fgetl(f);
c1 = findstr(b1,'E');
A1 = str2double(b1(1:c1(1)+3));
A2 = str2double(b1(c1(1)+4:c1(2)+3));
A3 = str2double(b1(c1(2)+4:c1(3)+3));
A4 = str2double(b1(c1(3)+4:c1(4)+3));
A5 = str2double(b1(c1(4)+4:c1(5)+3));
b2 = fgetl(f);
c2 = findstr(b2,'E');
A6 = str2double(b2(1:c2(1)+3));
A7 = str2double(b2(c2(1)+4:c2(2)+3));
A8 = str2double(b2(c2(2)+4:c2(3)+3));
A9 = str2double(b2(c2(3)+4:c2(4)+3));
A10 = str2num(b2(c2(4)+4:c2(5)+3));
b3 = fgetl(f);
c3 = findstr(b3,'E');
A11 = str2double(b3(1:c3(1)+3));
A12 = str2double(b3(c3(1)+4:c3(2)+3));
A13 = str2double(b3(c3(2)+4:c3(3)+3));
A14 = str2double(b3(c3(3)+4:c3(4)+3));
coeff=[A1 A2 A3 A4 A5 A6 A7 A8 A9 A10 A11 A12 A13 A14];
[CP,H,S] = cp_func(T,coeff,global_mid_temp);
[MW] = mol_wt(speciesname);
all_plot(T,CP,H,S,speciesname,MW,dir_name);
fclose(f);
%function to calculate cp,s,h
function[CP,H,S] = cp_func(T,coeff,global_mid_temp)
R=8.314;
if T > global_mid_temp
CP=(coeff(1)+coeff(2)*T+coeff(3)*T.^2+coeff(4)*T.^3+coeff(5)*T.^4)*R;
H=(coeff(1)*T+coeff(2).*(T.^2/2)+coeff(3).*(T.^3/3)+coeff(4)*(T.^4/4)+coeff(5)*(T.^5/5)+coeff(6))*R;
S=(coeff(1)*log(T)+coeff(2)*T+coeff(3)*T.^2+coeff(4)*T.^3+coeff(5)*T.^4+coeff(7))*R;
else
CP=(coeff(8)+coeff(9)*T+coeff(10)*T.^2+coeff(11)*T.^3+coeff(12)*T.^4)*R;
H=(coeff(8)*T+coeff(9).*(T.^2/2)+coeff(10).*(T.^3/3)+coeff(11)*(T.^4/4)+coeff(12)*(T.^5/5)+coeff(13))*R;
S=(coeff(8)*log(T)+coeff(9)*T+coeff(10)*T.^2+coeff(11)*T.^3+coeff(12)*T.^4+coeff(14))*R;
end
% calculates molecular weight of the species
function [MW] = mol_wt(speciesname)
g = {'H' 'C' 'N' 'O' 'Ar'};
atomicweight = [1 12 14 16 40];
MW = 0;
for i = 1:length(speciesname)
for j = 1:length(g)
if strcmp(speciesname(i), g(j))
MW = MW + atomicweight(j);
c = j;
end
end
n = str2num(speciesname(i));
if n > 1
MW = MW + atomicweight(c)*(n-1);
end
end
end
%function to plot
function all_plot(T,CP,H,S,speciesname,MW,dir_name)
p = pwd; % Prints current working directory
cd(dir_name) % Change directory
ele_dir = sprintf('%s',speciesname); % making a directory with compound name
mkdir(ele_dir);
% Generating plots for Cp vs Temperature
% and switching off the display to reduce time
figure(1)
set(figure(1),'visible','off')
plot(T,CP,'color','k','Marker','o','markersize',5,'markerfacecolor','b');
title(sprintf('Specific heat vs Temprature n Name: %s Molecular Weight: %d',speciesname,MW));
xlabel('Temperature');
ylabel('Cp');
% Generating plots for H vs Temperature
% and switching off the display to reduce time
figure(2)
set(figure(2),'visible','off')
plot(T,H,'color','k','Marker','o','MarkerFaceColor','g','markersize',5);
title(sprintf('Enthalpy vs Temprature \\n Name: %s Molecular Weight: %d',speciesname,MW));
xlabel('Temperature');
ylabel('H');
% Generating plots for entropy vs Temperature
% and switching off the display to reduce time
figure(3)
set(figure(3),'visible','off')
plot(T,S,'color','k','Marker','o','MarkerFaceColor','y','markersize',5);
title(sprintf('Entropy vs Temprature \\n Name: %s Molecular Weight: %d',speciesname,MW));
xlabel('Temperature');
ylabel('S');
% Switiching between directories and saving plots
cd(ele_dir)
saveas(figure(1),'Cp.jpg');
saveas(figure(2),'H.jpg');
saveas(figure(3),'S.jpg');
cd(p)
end