Project

Parsing NASA thermodynamic data file with MATLAB

close all
clear all
clc
%Opening and reading the required file
file = fopen(\'THERMO.dat\',\'r\')          
%Heading
line1 = fgetl(file)       
%Global temperature values
line2 = fgetl(file)      
%Next three lines are comments
des_1 = fgetl(file)                     
des_2 = fgetl(file)   
des_3 = fgetl(file)                     
A = strsplit(line2,\' \');
Global_low_temperature = str2num(A{2})
Global_mid_temperature = str2num(A{3})
Global_high_temperature = str2num(A{4})
%species information
for i=1:53
    
    species= fgetl(file);
    
    species_name = species(1:6)
    
Line_1 = fgetl(file);
    
L1 = strfind(Line_1,\'E\');

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);

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);

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)));
%creating folder for each species
mkdir([\'C:\\Users\\Admin\\File parsing NASA\\Species\\\',species_name]);
%Temperature ranges assumed for calculation purpose
Temperature = linspace(300,5000,100); 
%Universal gas constant
R =8.314;                            

%calculation of properties based on the lower and upper temperature
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
%plotting
%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\\Admin\\File parsing NASA\\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\\Admin\\File parsing NASA\\\')       %switching the directory back to read thermodat file for molecular mass calculation

MM = molecular_mass(species_name)

end

fclose(file)



the function \'Molecular_mass\' is given below

function MM = molecular_mass(species_name)
  
species = upper(species_name); 

Constituents = [\'H\', \'C\', \'N\', \'O\', \'A\'];

Atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948]; 

MM = 0; 

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

The aim of the given program is to extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file, the data file is provided and so is the equation. The equation is formulated by nasa, by solving these equation all the thermodynamic properties of a specie can be determined.

The program has to use the fopen function to access the file, and fclose there are some conventionely less used or new function present in comparison to previous programs, those are fopen, fclose, mkdir, str2num, saveas etc., we will look at each function as we explain the program

first we need to access the file that contains all the thermodynamic data. to do that we use the function fopen which can open the data file, provided it is saved in the same directory as that of the program.

then we need to extract the information and store the data in each line into an array, to do that we need a loop and to calculate the molecular mass we use a function called molecular mass, which we created.

now, in the loop we call the function \'molecular_mass\' on each iteration and create a folder which will store the graph for that specie, to create the folder we use \'mkdir\' function which makes directory for defined dataset. data is calculated for each species and the required graphs are plotted

Now the required graphs are saved using saveas function in the folder we created earlier. after all the itertions are done we use fclose function to close the file.

the screenshot of the files created by the program

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.