Project

File Parsing NASA Thermodynamic data file using MATLAB

Project Objective:

  • To read and extract the given THERMO.dat file containing NASA determined coefficient for various species.
  • To write a function to calculate the specific heat, entropy and enthalpy for various temperature ranges for each species.
  • To calculate the molecular weight of each species.
  • To plot the specific heat, entropy and enthalpy vs temperature for O2,N2 and Co2.
  • To generate and save all the plots in a seperate folder.

Main Code:

close all
clear all
clc

file = fopen(\'THERMO.dat\',\'r\');   %Opening and reading the input data file

line1=fgetl(file); %Heading
line2=fgetl(file); %Global Temperature values

A = strsplit(line2, \' \'); % split line into seperate string values 

%convert string into numbers

Global_low_temperature = str2num(A{2});
Global_mid_temperature = str2num(A{3});
Global_high_temperature = str2num(A{4});

%skip next 3 lines of the header on the file

for i = 1:3
    b = fgetl(file);
end

% Beginning of main loop to extract species name, coefficients, calculate and plot

for i=1:53
    
    species= fgetl(file);
    
    species_name = species(1:6); %specify no of strings
    
Line_1 = fgetl(file); % first line containing coefficients of \'O\'

L1 = strfind(Line_1,\'E\');  % Find letter E in the first line containing coeffiecients of O

% reading coefficients and converting them to real numbers for calculation
% later
c1 = str2num(Line_1(1:(L1(1)+3)));
c2 = str2num(Line_1((L1(1)+4):(L1(2)+3)));
c3 = str2num(Line_1((L1(2)+4):(L1(3)+3)));
c4 = str2num(Line_1((L1(3)+4):(L1(4)+3)));
c5 = str2num(Line_1((L1(4)+4):(L1(5)+3)));

Line_2 = fgetl(file); % second line containing coefficients of \'O\'
L2 = strfind(Line_2,\'E\');
c6 = str2num(Line_2(1:(L2(1)+3)));
c7 = str2num(Line_2((L2(1)+4):(L2(2)+3)));
c8 = str2num(Line_2((L2(2)+4):(L2(3)+3)));
c9 = str2num(Line_2((L2(3)+4):(L2(4)+3)));
c10 = str2num(Line_2((L2(4)+4):(L2(5)+3)));

Line_3 = fgetl(file); % third line containing coefficients of \'O\'
L3 = strfind(Line_3,\'E\');
c11 = str2num(Line_3(1:(L3(1)+3)));
c12 = str2num(Line_3((L3(1)+4):(L3(2)+3)));
c13 = str2num(Line_3((L3(2)+4):(L3(3)+3)));
c14 = str2num(Line_3((L3(3)+4):(L3(4)+3)));

%Loop repeats 53 times to extract the same information for each species


%creating new folder with species name for each species
mkdir([\'C:\\Users\\cecil\\Documents\\MATLAB\\Project 1 - Parsing NASA Thermo data\\Species\\\',species_name]);

Temperature = linspace(300,5000,100);  %Temperature ranges assumed for calculation purpose

% function to calculate Cp,H,S

[Cp,H,S] = coeff_calc(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,Temperature) ;

%plot graphs

%Temperature vs Entropy
figure(1)
plot(Temperature,S,\'linewidth\',4,\'color\',\'b\')
title(species_name)
xlabel(\'Temperature\')
ylabel(\'Entropy\')

%Temperature vs Enthalpy
figure(2)
plot(Temperature,H,\'linewidth\',4,\'color\',\'k\')
title(species_name)
xlabel(\'Temperature\')
ylabel(\'Enthalpy\')

%Temperature vs Specific heat
figure(3)
plot(Temperature,Cp,\'linewidth\',4,\'color\',\'y\')
title(species_name)
xlabel(\'Temperature\')
ylabel(\'Specific heat\')

directory = ([\'C:\\Users\\cecil\\Documents\\MATLAB\\Project 1 - Parsing NASA Thermo data\\Species\\\',species_name]);   %changing the filepath to save the plots
cd(directory)

%saving the plots

saveas(1,\'Entropy.jpg\');
saveas(2,\'Enthalpy.jpg\');
saveas(3,\'Specific heat.jpg\');

cd(\'C:\\Users\\cecil\\Documents\\MATLAB\\Project 1 - Parsing NASA Thermo data\')       %switching the directory back to read thermodat file for molecular mass calculation

fprintf(species_name)
molecular_weight = mol_wt(species_name)

   
end

fclose(file);

Function to calculate Cp,H & S:

function [cp,h,s] = coeff_calc(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,Temperature)

R = 8.314;
for j = 1:length(Temperature)
    if Temperature(j)<3000
cp = (c1+c2.*Temperature+c3.*Temperature.^2+c4.*Temperature.^3+c5.*Temperature.^4).*R;
h = (c1+(c2.*Temperature)./2+(c3.*Temperature.^2)/3+(c4.*Temperature.^3)/4+(c5.*Temperature.^4/5)+c6./Temperature).*Temperature.*R;
s = (c1*log(Temperature)+c2.*Temperature+(c3.*Temperature.^2)/2+(c4.*Temperature.^3)/3+(c5.*Temperature.^4)/4+c7).*R;
    else
cp = (c8+c9.*Temperature+c10.*Temperature.^2+c11.*Temperature.^3+c12.*Temperature.^4).*R;
h = (c8+(c9.*Temperature)/2+(c10.*Temperature.^2)/3+(c11.*Temperature.^3)/4+(c12.*Temperature.^4/5)+c13./Temperature).*Temperature.*R;
s = (c8.*log(Temperature)+c9.*Temperature+(c10.*Temperature.^2)/2+(c11.*Temperature.^3)/3+(c12.*Temperature.^4)/4+c14).*R;
    end
end

Function to calculate molecular weight:

function MW = mol_wt(species_name)
  
species = upper(species_name); % stores all species name in an array

Constituents = [\'H\', \'C\', \'N\', \'O\', \'A\']; % define necessary constituents

Atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948]; %atomic weight for defined constituents

MW = 0; 

for i=1:length(species) 
    for j=1:length(Constituents)
        if strcmp(species(i),Constituents(j))
           MW = MW + Atomic_weight(j); 
           u = j;
        end
    end
    n = str2num(species(i));
    if n>1
        MW = MW + Atomic_weight(u)*(n-1);
    end
end
end

Plot Cp,H & S for species O2,N2 and CO2:

O2:

\"\"

\"\"

\"\"

N2:

\"\"

\"\"

\"\"

CO2:

\"\"

\"\"

\"\"

Screenshot displaying molecular weights for species in command window:

\"\"

Screenshot of seperate folders created for each species in working directory:

\"\"

Code Explanation:

  • The given file is first copied into the current working directory of MATLAB and read using the fopen command.
  • fgetl command is used to read each line in the read file.
  • The second line in the file houses the global temperature range which is saved to a variable line2.
  • Since the line is read as a single string, strsplit command is used to split the string with \' \' space used as the indentifier to break down the string.
  • Next the given string needs to be converted to real number and this is done using str2num command. The 3 global temperature values are assigned to respective variables.
  • The next 3 lines are part of the header and a simple for loop is run to skip the header.
  • The next 4 lines in the data file contain the first name of species and values for 14 coefficients. In total there are 53 species in the file. Once the code is written to extract the name and coefficients and local temperature ranges, the for loop can be run to iterate the process for all the species.
  • the first line of the for loop is containing the string value of species name is assigned to a variable species. The species name is present only in the first 6 strings.
  • To read the coefficients from the corresponding line, the command strfind is used to search for string \'E\'. Each coefficient value starts after 3 places from E and is used as pointer to extract the other coefficients and convert them to numbers for use in calculation.
  • The local temperature ranges assumed to be between 300 to 5000 and is 100 inbetween sample values are stored using linspace command.
  • A function calc_coeff is defined which contains the formula to calculate the specific heat Cp, Enthapy H and entropy S.
  • The function is called using its function name and the required inputs which are the coefficients and local temperature are passed in the function.
  • A new folder called species is created using mkdir command with the path to the current working directory specified.
  • 3 plots are defined for the species to plot Cp, H and S including xlabel and ylabels.
  • To save the plots, the file directory needs to be changed pointing to the newly created species folder which is done using the cd command and saveas command is used to save the figures as jpeg files.
  • The directory needs to be changed again to working directory containing the script and user defined functions.
  • A function mol_wt is used to calculate the molecular weight of each species.
  • Each species name is stored in a string variable called species.
  • The constituents that make up the species are defined in a seperate row vector. The atomic weight for each of these constituents is stored in another array.
  • A nested for loop is used to initially run the entire length of species name and second loop runs the entire length of constituents strings. strcmp compares the values of both these strings to find a match. When it does, molecular weight will be equal to the corresponding value in the array which contains the atomic weights.
  • The file is closed using fclose command.
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.

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.