Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing :

Data parsing is a process in which a string of data is converted from one format to another.

Aim :

The main aim of this following code is to read the given thermo data file and calculate the required values.

Code :

clear all
close all
clc

% getting thermo file 
f = fopen('THERMO.dat','r');
fgetl(f) 

% reading header 
A = fgetl(f);
A = str2num(A);
global_low_temp = A(1);
global_mid_temp = A(2);
global_high_temp = A(3);

T = linspace(global_low_temp,global_high_temp,400);
 
%universal gas constant
R = 8.314;

% skipping next lines
% fgetl(f)
% fgetl(f)
% fgetl(f)
% instead skipping the three lines using iteration 

for i = 1:3
    fgetl(f)
end

% storing the temperature valuesin variables

for i = 1:53

    part = fgetl(f);
    [name, local_temp] = strtok(part,'G');
    species_name = strtok(nmae);
    temp = strsplit(local_temp);
    local_low_temp = str2double(temp{2});
    local_mid_temp = str2double(temp{3});
    local_high_temp = str2double(temp{4});

    t1 = local_low_temp;
    t2 = local_mid_temp;
    t3 = local_high_temp;

% getting line_1 co_eff (a1 to a5)
     line_1 = fgetl(f);

% finding the position of 'E' in line_1
     a = strfind(line_1,'E');

%co effs
     a1 = str2double(line_1(1:a(1)+3));
     a2 = str2double(line_1(a(1)+4:a(2)+3));
     a3 = str2double(line_1(a(2)+4:a(3)+3));
     a4 = str2double(line_1(a(3)+4:a(4)+3));
     a5 = str2double(line_1(a(4)+4:a(5)+3));

% getting line_2 co_effs (a6 to a10)  
     line_2 = fgetl(f);

% finding the position of 'E' in line_2
     b = strfind(line_2,'E');

% co effs
     a6 = str2double(line_2(1:b(1)+3));
     a7 = str2double(line_2(b(1)+4:b(2)+3));
     a8 = str2double(line_2(b(2)+4:b(3)+3));
     a9 = str2double(line_2(b(3)+4:b(4)+3));
     a10 = str2double(line_2(b(4)+4:b(5)+3));

% getting line_3 co_effs (a11 to a14)
     line_3 = fgetl(f);

%finding position of 'E' in line_3
     c = strfind(line_3,'E');
     a11 = str2double(line_3(1:c(1)+3));
     a12 = str2double(line_3(c(1)+4:c(2)+3));
     a13 = str2double(line_3(c(2)+4:c(3)+3));
     a14 = str2double(line_3(c(3)+4:c(4)+3));

     T = linespace(t1,t3,1000);
% universal gas constant value
     R = 8.314;

% getting specific_heat, enthalpy, entropy as arrays
for k = 1:length(T)
% to compare species_temperatures

if T(k)>t2

    cp = (a1+(a2*T)+(a3*T.^2)+(a4*T.^3)+(a5*T.^4))*R;
    H = (a1+((a2*T)./2)+((a3*T.^2)./3)+((a4*T.^3)./4)+((a5*T.^4)./5)+(a6./T))*R.*T;
    S = (a1*log(T)+(a2*T)+((a3*T.^2)./2)+((a4*T.^3)./3)+((a5*T.^4)./4)+a7);

else

    cp = (a8+(a9*T)+(a10*T.^2)+(a11*T.^3)+(a12*T.^4))*R;
    H = (a8+((a9*T)./2)+((a10*T.^2)./3)+((a11*T.^3)./4)+((a12*T.^4)./5)+(a13./T))*R.*T;
    S = (a8*log(T)+(a9*T)+((a10*T.^2)./2)+((a11*T.^3)./3)+((a12*T.^4)./4)+a14);

end
end

% plotting graphs
% temperature(T) vs specific_heat(cp)
figure(1)
plot(T,cp);
grid on
title('species_name')
xlabel('Temperature')
ylabel('Specific_heat')

% temperature(T) vs enthalpy(H)
figure(2)
plot(T,H);
grid on
title('species_name')
xlabel('Temperature')
ylabel('Enthalpy')

% Temperature(T) vs entropy(S)
figure(3)
plot(T,S);
grid on
title('species_name')
xlabel('Temperature')
ylabel('Entropy')

% changing directory and saving the files
mkdir(species_name)
cd(species_name)

saveas(1,'specific heat','jpg');
saveas(2,'Enthalpy','jpg');
saveas(3,'Entropy','jpg');
cd ..

% molecular mass calculation

elements = ['H','C','O','N','A'];
atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948];
mol_mass = 0;
for p = 1:length(species_name)
    for q =  1:length(atomic_weight)
        if strcmp(species_name(i),elements(p))
            mol_mass = mol_mass + atomic_mass(q);
        end
    end
end

n = str2double(species_name(i));
if n>1
    mol_mass = mol_mass + atomic_weight(q)*(n-1);
end

% printing the molecular mass

sprintf('Molecular weight of %s:%f u',specie_name,mol_mass)

end
fclose(f);

Calculating the molecular weight :

  • for molecular weight calculation we need the weights of hydrogen , oxygen , carbon , nitrogen and argon.
  • loop is formed and strcmp command is used to compare the elements with species.

the below code is for simpler view of molecular claculation :

% molecular mass calculation

elements = ['H','C','O','N','A'];
atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948];
mol_mass = 0;
for p = 1:length(species_name)
    for q =  1:length(atomic_weight)
        if strcmp(species_name(i),elements(p))
            mol_mass = mol_mass + atomic_mass(q);
        end
    end
end

n = str2double(species_name(i));
if n>1
    mol_mass = mol_mass + atomic_weight(q)*(n-1);
end

% printing the molecular mass

sprintf('Molecular weight of %s:%f u',specie_name,mol_mass)

Errors :

1) skipping lines throught iteration, later rectified with the help of online support executive.

2) saving file in different folders, later rectified with the help of online support executive.

3) minor errors like spelling mistakes and operator missing, later rectified by seeing the error message in the particular lines.

Folder formation:

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.