Challenge

Project 1 - Parsing NASA thermodynamic data

%%
%primary parsing code
close all
clear all
clc
%opening and reading the required file
file = fopen('THERMO.dat','r')

%header
line1 = fgetl(file)

%global temperature values
line2 = fgetl(file)

%next 3 lines are comments
des_1 = fgetl(file)
des_2 = fgetl(file)
des_3 = fgetl(file)

%distinguishing the global temperatures by the space provided b/w them 
A = strsplit(line2,' ');
%starting with second space ' ' as first space is before the first global
%temperature 
global_low_temperature = str2num(A{2})
global_mid_temperature = str2num(A{3})
global_high_temperature = str2num(A{4})

%species information
%for loop initiated for 53 iterations as we have 53 species
for i=1:53
    species = fgetl(file);
    species_name = species(1:6)
    Line_1 = fgetl(file)
    L1 = strfind(Line_1,'E');
    %using the exponential sign 'E' to identify the length and value of
    %constants
    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\shash\Desktop\matlab programs\NASA thermodynamic data\xyz\Species\',species_name])
    
    %temperature ranges assumed for calculation purpose
    temperature = linspace(300,5000,100);
    
    %universal gas constant
    R = 8.314;
    
    %using 'for' loop for coefficients lying in two temerature ranges as 
    %mentioned in the question  
    %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
%plot

%temperature vs entropy
figure(1)
plot(temperature,S,'linewidth',3,'color','r')
title(species_name)
xlabel('temperature')
ylabel('entropy')

%temperature vs enthalpy
figure(2)
plot(temperature,H,'linewidth',3,'color','b')
title(species_name)
xlabel('temperature')
ylabel('enthalpy')

%temperature vs specific heat
figure(3)
plot(temperature,Cp,'linewidth',3,'color','y')
title(species_name)
xlabel('temperature')
ylabel('specific heat')

directory = (['C:\Users\shash\Desktop\matlab programs\NASA thermodynamic data\xyz\Species\',species_name]);
%changing the file path 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\shash\Desktop\matlab programs\NASA thermodynamic data\')
cd ..
%switching back to the directory to read THERMO.dat file for 
%molecular mass calculation

MM = molecular_mass(species_name)

end

fclose(file)



%%
%creating function for molecular mass
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
%%
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.