MATLAB - Parsing NASA thermodynamic data
AIM
To parse the NASA's THERMO.dat file to calculate required datas
CODE EXPLANATION
To find no of lines and no of species, seperate code is done
%parsing file
clc
clear all
close all
f1=fopen('THERMO.dat','r')
c=0;
noline=0;
ct=0;
while (c==0)
line=fgetl(f1);
if (line(length(line))=='1')
ct=ct+1;
end
noline=noline+1;
if (strcmp(line,'END'))
c=1;
end
end
sprintf(' No. of lines=%f \n No. of species=%f',noline,ct)
Main program:
- clc, clear all, close all to clear screen, clear workspace, close figures.
- Open thermo.dat in reading mode using fopen
- First 2 lines saved as line1 and line2 respectively. Form line 2 global temperatures extracted.
- Next 3 lines are ignored by fgetl as they are comments
- Started for loop for 53 nos(no of species)
- Inside it the next line was saved as species using fgetl.
- From species, name and local temperatures were extracted
- Then called function calc_cp_h_s providing no. of inputs (including pointer for file opening) to return specific heat, enthalphy, entrophy
- From the values, 3 required plots were plotted, which subsequently saved in a folder seperately which was created in function
- Changed directory to current location
- name changed to upper case
- Atomic weights for elements array created
- Then to find molecular mass, 2 for loops used, one for counting name array, another for elements which was compared inside it
- If same, then current molecular mass value added with respective molecular weight of elements
- Another if loop created to add atomic weight of previous element if no presents
- Displayed the values using sprintf command.
- Closed file using fclose command.
close all
clear all
clc
%Opening and reading
f1=fopen('THERMO.dat','r')
%Heading line
line1=fgetl(f1);
%Global temperatures
line2=fgetl(f1) ;
%Ignoring comments
fgetl(f1);
fgetl(f1);
fgetl(f1);
%Extracting global low,mid,high temperatures individually
G=strsplit(line2,' ');
GLT=str2num(G{2});
GMT=str2num(G{3});
GHT=str2num(G{4});
%loop for storing each species information in seperate folders
for i=1:53
%Universal gas constant
R =8.314;
%first line saved as species
species=fgetl(f1);
%since max length of species name is 6, 1:6 used
name=species(1:6);
%Extracting local low, high, mid temperatures respectively
lt=strfind(species,'.');
llt=str2num(species((lt(1)-4):(lt(1)+3)));
lht=str2num(species((lt(1)+4):(lt(2)+3)));
lmt=str2num(species((lt(2)+4):(lt(3)+3)));
[Cp,H,S]=calc_cp_h_s(f1,llt,lht,lmt,name);
temp=linspace(llt,lht,100);
%plotting
%Temperature vs Specific heat
figure(1)
plot(temp,Cp,'linewidth',3,'color','r')
title(name)
xlabel('Temperature')
ylabel('Specific heat')
%Temperature vs Enthalpy
figure(2)
plot(temp,H,'linewidth',3,'color','b')
title(name)
xlabel('Temperature')
ylabel('Enthalpy')
%Temperature vs Entropy
figure(3)
plot(temp,S,'linewidth',3,'color','g')
title(name)
xlabel('Temperature')
ylabel('Entropy')
%changing directory
dir=(['F:\Octave files\parse file challenge\',name]);
cd(dir)
%saving the plots
saveas(1,'Specific heat.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Entropy.jpg');
%changing the directory (to read thermodat file for calculation of molecular mass)
cd('F:/Octave files')
%calculation of molecular mass
name=upper(name);
elements=['H', 'C', 'N', 'O', 'A'];
%Atomic weights of elements wrt elements array
aw=[1.00794 12.0107 14.0067 15.9994 39.948];
mm=0;
for i=1:length(name)
for j=1:length(elements)
if strcmp(name(i),elements(j))
mm=mm+aw(j);
f=j;
end
end
%if no. present in the name
n=str2num(name(i));
if n>1
mm=mm + aw(f)*(n-1);
end
end
sprintf('molecular weight of %s=%d',name,mm)
end
%closeing file
fclose(f1)
Function code:
- Since pointer is passed in function cursor position remains same.
- Next 3 lines were received as seperate variables using fgetl
- using findstr 'E' located. From which the coefficients are extracted sequentialy as shown(for ex column 1 to E+3 column for first coefficients)
- temperature range defined from low local temperature to high local temperature of 100 divisions
- Folder with the name of species created here.
- Cp,H,S calculated by given formula(if temp>mid local temperature use high temperature coefficients)
- return Cp,H,S
function [Cp,H,S]=calc_cp_h_s(f1,llt,lht,lmt,name)
%Extracting line 1 coefficients
line_1=fgetl(f1);
l1=strfind(line_1,'E');
coef1=str2num(line_1(1:(l1(1)+3)));
coef2=str2num(line_1((l1(1)+4):(l1(2)+3)));
coef3=str2num(line_1((l1(2)+4):(l1(3)+3)));
coef4=str2num(line_1((l1(3)+4):(l1(4)+3)));
coef5=str2num(line_1((l1(4)+4):(l1(5)+3)));
%Extracting line 2 coefficients
line_2=fgetl(f1);
l2=strfind(line_2,'E');
coef6=str2num(line_2(1:(l2(1)+3)));
coef7=str2num(line_2((l2(1)+4):(l2(2)+3)));
coef8=str2num(line_2((l2(2)+4):(l2(3)+3)));
coef9=str2num(line_2((l2(3)+4):(l2(4)+3)));
coef10=str2num(line_2((l2(4)+4):(l2(5)+3)));
%Extracting line 3 coefficients
line_3=fgetl(f1);
l3=strfind(line_3,'E');
coef11=str2num(line_3(1:(l3(1)+3)));
coef12=str2num(line_3((l3(1)+4):(l3(2)+3)));
coef13=str2num(line_3((l3(2)+4):(l3(3)+3)));
coef14=str2num(line_3((l3(3)+4):(l3(4)+3)));
%creating folder for each species
mkdir(['F:\Octave files\parse file challenge\',name]);
%Universal gas constant
R =8.314;
%calculation of properties based on the lower and upper temperature
temp=linspace(llt,lht,100);
for j=1:length(temp)
if j
Results:
molecular mass
Folders
Plots
O2
N2
CO2
ERRORS FACED
Few brackets missed here and there, which was added appropriately for successful execution
CONCLUSION
Successfully THERMO.dat was parsed and required datas were collected.