Challenge

Project 1 - Parsing NASA thermodynamic data

%NASA Thermodynamic data parsing

clear all
close all
clc

f1 = fopen('THERMO.dat', 'r') %f1 is the file pointer.
%Using 'r' to read the data from THERMO.dat. .dat is the file extension
fgetl(f1) %To retrive the header
Global_tline = fgetl(f1); %Storing the following string value in Global_tline

%Since it is in string format, we need to split the string
GT = strsplit(Global_tline, ' '); %Since the srtings are splitted by the blank space, we can use blank space within the ' '(inverted commas, telling MATLAB to split the string where it encounter the blank space) to split the LHS and RHS and store it as coefficient.
%still the value is in string so to convert we need to use str2num
G_low_temp = str2num(GT{2}); %Using {} since it is a cell array. A cell array is an array which stores different format values
%above we have GT{2} because at GT{1} matlab identifies a blank space prior
%to 300. therefore I use GT{2}
G_mid_temp = str2num(GT{3});
G_high_temp = str2num(GT{4});


%Now as we have got the Header and the Global temperatures, we can now
%locate the species and get the 14 coefficients, where the 1st 7 are low
%temperature coefficients and the last 7 are high temperature coefficients.
fgetl(f1) %To skip the comment lines
fgetl(f1)
fgetl(f1)
%x = fgetl(f1)

%Assigning the values
R = 8.314; %J/mol K

%Atomic weight in u
O = 16;
C = 12;
H = 1;
N = 14;
S = 32;
AR = 39.948;
OH = 17;

S = 53; %No.of Species
CC = 3; %Coeffecients rows

%My logic here is, as the blocks are repeated we an use for loop for ease of programming and to avoid untideness of the code. But I had % also another idea to use two for loops. A for loop for species and a for loop for rows. But since the last line is not the same as the 1st %two lines I had to just use one for loop.

p = 1; %giving counter for each Species
for i = 1:S
x = fgetl(f1) %To jump to the next line
x1 = findstr(x, 'O') %To find the letter 'O' in the x line (Which contains a string value)
Spec = x(1:8); %Trying to give ample amount of space for other species to fit in as well (1:6 would have been sufficient, as no species crosses 6 characters)
t(p) = convertCharsToStrings(Spec); %In order to store in an array I had to convert the charters to strings
Species(p) = t(p); %Storing all the species in an array

x2 = strsplit(x, ' ') %Splitting the string where it encounters a blank space ' '
L_low_temp = str2num(x2{6}) %Finding local low, med and high temperatures
L_high_temp = str2num(x2{7})
L_med_temp = str2num(x2{8})
Local_Temp = [L_low_temp, L_med_temp, L_high_temp] %Assigning it in an array. This step can be skipped. But inorder to check if it generates temp for all species, I tried with different no. of species and the last number value usually gets stored.
%fgetl(f1)

%for j = 1:CC %Tried tow for loop, but it failed due to the reason that
%the coeffs are 14 and not 15. if it was equally seperated and written
%then this logic would've been correct.
y = fgetl(f1) %Again landing to the next line
y1 = findstr(y, 'E') %Now the same thing repeats, I am trying to find 'E'

A1 = y(1:y1(1)+3) %Giving +3 so that after the exp the remaining number can be included
A2 = y(y1(1)+4:y1(2)+3) %Using this logic we land exactly to the beginning of the upcoming number/ next number
A3 = y(y1(2)+4:y1(3)+3)
A4 = y(y1(3)+4:y1(4)+3)
A5 = y(y1(4)+4:y1(5)+3)

a1 = str2num(A1) %Converting string to number and storing in the varibles
a2 = str2num(A2)
a3 = str2num(A3)
a4 = str2num(A4)
a5 = str2num(A5)

y = fgetl(f1)
y1 = findstr(y, 'E')

A6 = y(1:y1(1)+3)
A7 = y(y1(1)+4:y1(2)+3)
A8 = y(y1(2)+4:y1(3)+3)
A9 = y(y1(3)+4:y1(4)+3)
A10 = y(y1(4)+4:y1(5)+3)

a6 = str2num(A6)
a7 = str2num(A7)
a8 = str2num(A8)
a9 = str2num(A9)
a10 = str2num(A10)

y = fgetl(f1)
y1 = findstr(y, 'E')

A11 = y(1:y1(1)+3)
A12 = y(y1(1)+4:y1(2)+3)
A13 = y(y1(2)+4:y1(3)+3)
A14 = y(y1(3)+4:y1(4)+3)

a11 = str2num(A11)
a12 = str2num(A12)
a13 = str2num(A13)
a14 = str2num(A14)



%Specific heat, Enthalphy and Entropy calculation for local and
%global temperature

%1st for global high temperature
Cp_gh(p) = R*(a1+a2*G_high_temp+a3*G_high_temp^2+a4*G_high_temp^3+a5*G_high_temp^4); %Specific heat
%Using high temperature because the 1st 7 coefficients are high
%temperature coefficients;
H_gh(p) = R*G_high_temp*(a1+a2*(G_high_temp/2)+a3*(G_high_temp^2/3)+a4*(G_high_temp^3/4)+a5*(G_high_temp^4/5)+a6/G_high_temp); %Enthalpy
S_gh(p) = R*(a1*log(G_high_temp)+a2*G_high_temp+a3*(G_high_temp^2/2)+a4*(G_high_temp^3/3)+a5*(G_high_temp^4/4)+a7); %Entropy

%For global low temperature
Cp_gl(p) = R*(a1+a2*G_low_temp+a3*G_low_temp^2+a4*G_low_temp^3+a5*G_low_temp^4); %Specific heat
H_gl(p) = R*G_low_temp*(a1+a2*(G_low_temp/2)+a3*(G_low_temp^2/3)+a4*(G_low_temp^3/4)+a5*(G_low_temp^4/5)+a6/G_low_temp); %Enthalpy
S_gl(p) = R*(a1*log(G_low_temp)+a2*G_low_temp+a3*(G_low_temp^2/2)+a4*(G_low_temp^3/3)+a5*(G_low_temp^4/4)+a7); %Entropy



%2nd for local high temperature
Cp_lh(p) = R*(a1+a2*L_high_temp+a3*L_high_temp^2+a4*L_high_temp^3+a5*L_high_temp^4); %Specific heat
%Using high temperature because the 1st 7 coefficients are high
%temperature coefficients;
H_lh(p) = R*L_high_temp*(a1+a2*(L_high_temp/2)+a3*(L_high_temp^2/3)+a4*(L_high_temp^3/4)+a5*(L_high_temp^4/5)+a6/L_high_temp); %Enthalpy
S_lh(p) = R*(a1*log(L_high_temp)+a2*L_high_temp+a3*(L_high_temp^2/2)+a4*(L_high_temp^3/3)+a5*(L_high_temp^4/4)+a7); %Entropy


%For local low temperature
Cp_ll(p) = R*(a1+a2*L_low_temp+a3*L_low_temp^2+a4*L_low_temp^3+a5*L_low_temp^4); %Specific heat
H_ll(p) = R*L_low_temp*(a1+a2*(L_low_temp/2)+a3*(L_low_temp^2/3)+a4*(L_low_temp^3/4)+a5*(L_low_temp^4/5)+a6/L_low_temp); %Enthalpy
S_ll(p) = R*(a1*log(L_low_temp)+a2*L_low_temp+a3*(L_low_temp^2/2)+a4*(L_low_temp^3/3)+a5*(L_low_temp^4/4)+a7); %Entropy


figure
plot(Cp_ll, 'r') %Plot for local low temperature Cp
hold on
plot(Cp_lh, 'b') %Plot for local low temperature Cp
hold off
pause(0.05)

figure
plot(H_ll, 'r') %Plot for local low temperature H
hold on
plot(H_lh, 'b') %Plot for local high temperature H
hold off

figure
plot(S_ll, 'r') %Plot for local low temperature S
hold on
plot(S_lh, 'b') %Plot for local high temperature S
hold off


p=p+1;

%ct=ct+1;
%end
%fgetl(f1)
end

%Molecular Mass calculation in g/mol
O = O;
O2 = 2*O;
H = H;
H2 = 2*H;
OH = O+H;
H2O = H*2+O;
HO2 = H*O+2;
H2O2 = H*2+O*2;
C = C;
CH = C+H;
CH2 = C+H*2;
CH2_S = CH2+S;
CH3 = C+H*3;
CH4 = C+H*4;
CO = C+O;
CO2 = C+O*2;
HCO = H+C+O;
CH2O = C+H*2+O;
CH2OH = C+H*2+OH;
CH3O = C+H*3+O;
CH3OH = C+H*3+OH;
C2H = C*2+H;
C2H2 = C*2+H*2;
C2H3 = C*2+H*3;
C2H4 = C*2+H*4;
C2H5 = C*2+H*5;
C2H6 = C*2+H*6;
CH2CO = C+H*2+CO;
HCCO = H+C*2+O;
HCCOH = H+C*2+OH;
H2CN = H*2+C+N;
HCN = H+C+N;
HNO = H+N+O;
N = N;
NNH = N*2+H;
N2O = N*2+O;
NH = N+H;
NH2 = N+H*2;
NH3 = N+H*3;
NO = N+O;
NO2 = N+O*2;
HCNO = H+C+NO;
HOCN = H+O+C+N;
HNCO = HOCN;
NCO = N+C+O;
CN = C+N;
HCNN = H+C+N*2;
N2 = N*2;
AR = AR;
C3H8 = C*3+H*8;
C3H7 = C*3+H*7;
CH3CHO = C*2+H*4+O;
CH2CHO = C*2+H*3+O;

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.