Challenge

Project 1 - Parsing NASA thermodynamic data

% A program to extract temperature coefficients from NASA Thermodynamic Data Set

close all;
clear;
clc

%{
First open the THERMO.dat file in my current directory as a text file to review the data
A review suggests that the first species O is on the sixth line
Using the fopen and fgetl commands to navigate the header lines to the
sixth line
%}

fid = fopen('THERMO.dat', 'r'); % Create a pointer handle to the file
% Reading each line of the data file
l1 = fgetl(fid);
l2 = fgetl(fid);
l3 = fgetl(fid);
l4 = fgetl(fid);
l5 = fgetl(fid);
%l6 = fgetl(fid);
%l7 = fgetl(fid);
%l8 = fgetl(fid);
%l9 = fgetl(fid);
%{
Running the above lines of code reveal that l6 is a string of 9 elements as shown below
l6 = 'O L 1/90O 1 G 200.000 3500.000 1000.000 1'

The local temperatures of 200.000, 3500.000 and 1000.000 are respectively the 6th, 7th and 8th elements of l6
while the specie name is the first element
element
%}

% To get the elements of l6, we use the strsplit command
%m = strsplit(l6); % This produces a 1x9 cell array

%{
Getting the low, high amd mid local temperature values, we index into m
using curly braces and the str2num commands since each element is a string
%}

% lowtemp = str2double(m{6});
% midtemp = str2double(m{8});
% hightemp = str2double(m{7});

%tline = l6;
% next create a temperature array using linspace command

% T = linspace(lowtemp, hightemp, 500);
NASA = pwd; % current directory
% NASA= 'NASA' ;
% folder = mkdir(NASA);% Create a folder in the current directory to save all plots
% Assignment = ([Assignment, NASA]); % Move to the new directory just created
% Evaluating the temperature coefficients for the species - The THERMO file has 53 species

for ii=1:53
tline=fgetl(fid);

A = strsplit(tline,' ');
species_name =(A{1});
lowtemp = str2double(A{end-3});
midtemp =str2double(A{end-1});
hightemp =str2double(A{end-2});

% Create a temperature array between the low and high temperatures using linspace command

T = linspace(lowtemp, hightemp, 1000); % Create a 1 X 1000 row vector of temperature

% Determining the coefficients, seven each for higher and lower temperatures

tline1=fgetl(fid);
a=strfind(tline1,'E');

a1=tline1(1:a(1)+3);
a1=str2double(a1);
a2=tline1(a(1)+4:a(2)+3);
a2=str2double(a2);
a3=tline1(a(2)+4:a(3)+3);
a3=str2double(a3);
a4=tline1(a(3)+4:a(4)+3);
a4=str2double(a4);
a5=tline1(a(4)+4:a(5)+3);
a5=str2double(a5);

tline2=fgetl(fid);
a=strfind(tline2,'E');

a6=tline2(1:a(1)+3);
a6=str2double(a6);
a7=tline2(a(1)+4:a(2)+3);
a7=str2double(a7);
a8=tline2(a(2)+4:a(3)+3);
a8=str2double(a8);
a9=tline2(a(3)+4:a(4)+3);
a9=str2double(a9);
a10=tline2(a(4)+4:a(5)+3);
a10=str2double(a10);


tline3=fgetl(fid);
a=strfind(tline3,'E');
a11=tline3(1:a(1)+3);
a11=str2double(a11);
a12=tline3(a(1)+4:a(2)+3);
a12=str2double(a12);
a13=tline3(a(2)+4:a(3)+3);
a13=str2double(a13);
a14=tline3(a(3)+4:a(4)+3);
a14=str2double(a14);

%Evaluating the specific heat, enthalpy, entropy and molecular weights of the species
% Define the Molar Gas Constant R

R = 8.314;

Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,R,T,midtemp);
H = enthalpy_s(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,R,T,midtemp);
S = entropy_s(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,T,midtemp);
MW = molecular_weight(species_name);
fprintf('Molecular weight of %s\n = %f\n',species_name,MW);

% Next Create folders of species to save plots
folder(ii) = mkdir(species_name); % Creates sub folders for each specie to store plots

% Graphics and Visualization

h(ii) = figure(ii); % Creates a handle for each specie plot

%{
Since there are three plots for each specie, I use the subplot command to
create a set of plot for each specie
%}

% Creates four quadrants where each quadrant holds a plot
% Plot of Specific Heat (Cp) vs Temperature (T)
subplot(2,2,1) % The first quarant for the first plot
plot(T,Cp,'linewidth',5,'color','r')
xlabel('Temperature(T)')
ylabel('Specific Heat (Cp)')
grid on
title(species_name)

% Plot of Enthalpy (H) vs Temperature (T)
subplot(2,2,2) % The second quarant for the second plot
plot(T,H,'linewidth',5,'color','b')
xlabel('Temperature(T)')
ylabel('Entalpy(H)')
title(species_name)

subplot(2,2,[3,4]) % The third and fourth quarants for the third plot
% Plot of Entropy (S) vs Temperature (T)
plot(T,S,'linewidth',5,'color','m')
xlabel('Temperature(T)')
ylabel('Entropy(S)')
title(species_name)

savefig(h,'Thermo.fig'); % Saves all plots of species to a MATLAB figure file

% Saving individual specie plot to respective specie folder
% First save the plot to a file name using the sprintf command, file format and the specie name
FileName = sprintf('%s.jpg', species_name);

% Use the fullfile() command to create path to files in a specified folder
fullFileName = fullfile(species_name, FileName);

% Save the current figure handle for each specie to respective specie folder
saveas(gcf, fullFileName);

end

% Google Drive Link to folders of CO2, N2 and O2

% https://drive.google.com/drive/folders/1yPDb6XEJQy1pG_FapJWK5X58rIrlqbCB?usp=sharing

% REFERENCES
% MATLAB HELP
%MATLAB Help Cetre

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.