NASA DATA PARSING USING MATLAB
AIM
To write a parsing code which read the thermodynamic data from nasa.dat file and plot
specific heat,enthalpy and entropy vs temperature.
INTRODUCTION
Data parsing refers to taking input from files and using that data to do mathematical operations which helps to intrepet the data for further consideration.
Commands such as fopen and fclose helps to open and close the data file respectively.
To read each line of the file we use fgetl which helps to understand that particular line
Code
clear all
close all
clc
f1=fopen(\'THERMO.dat\',\'r\'); // LINE 5 TO 10
skl=fgetl(f1)
sk2=fgetl(f1)
sk3=fgetl(f1)
sk4=fgetl(f1)
sk5=fgetl(f1)
for i=1:53 // LINE 12 TO 67
line1=fgetl(f1)
line2=fgetl(f1)
line3=fgetl(f1)
line4=fgetl(f1)
A=strsplit(line1,\' \')
species(i)=A(1)
B=strfind(line2,\'E\')
D=line2(1:B(1)+3)
C1=str2num(D)
E=line2(B(1)+4:B(2)+3)
C2=str2num(E)
F=line2(B(2)+4:B(3)+3)
C3=str2num(E)
G=line2(B(3)+4:B(4)+3)
C4=str2num(G)
H=line2(B(4)+4:B(5)+3)
C5=str2num(H)
I=strfind(line3,\'E\')
J=line3(1:I(1)+3)
C6=str2num(J)
K=line3(I(1)+4:I(2)+3)
C7=str2num(K)
L=line3(I(2)+4:I(3)+3)
C8=str2num(L)
M=line3(I(3)+4:I(4)+3)
C9=str2num(M)
N=line3(I(4)+4:I(5)+3)
C10=str2num(N)
O=strfind(line4,\'E\')
P=line3(1:O(1)+3)
C11=str2num(P)
Q=line3(O(1)+4:O(2)+3)
C12=str2num(Q)
R=line3(O(2)+4:O(3)+3)
C13=str2num(R)
T=line3(O(3)+4:O(4)+3)
C14=str2num(T)
molecular_weight=species
temp=linspace(200,3500,50)
R=8.31;
for j=1:50
T1=[1000]
if temp(j)<=T1
cp(j)=R*((C1+(C2.*temp(j))+C3.*(temp(j))^2+(C4.*temp(j)).^3+ C5.*(temp(j)).^4) )
h(j)=R.*temp(j).*(C1 + C2.*temp(j)./2 +C3.*(temp(j)).^2./3 +C4.*(temp(j)).^3./4+C5.*(temp(j)).^4/5+C6.*(temp(j)).^5./6)
s(j)=R.*(C1.*log(temp(j))+C2.*temp(j)+C3.*(temp(j))^2+C4.*(temp(j)).^3+ C5.*(temp(j)).^4+C7)
else
cp(j)=R*(C8+C9*temp(j)+C10*(temp(j))^2+C11*(temp(j))^3+ C12*(temp(j))^4 )
h(j)=R*temp(j)*(C8 + C9*temp(j)/2 +C10*(temp(j))^2/3 +C11*(temp(j))^3/4+C12*(temp(j))^4/5+C13*(temp(j))^5/6)
s(j)=R*(C8*log(temp(j))+C9*temp(j)+C10*(temp(j))^2+C11*(temp(j))^3+ C12*(temp(j))^4+C7)
end
end
end
spec=char(species) // LINE 70 TO 73
fd=sprintf(\'%s\',spec)
cur_dir=pwd
mkdir=(spec)
figure(1) // LINE 85 TO 100
plot(temp,cp)
xlabel(\'temp in K\')
ylabel(\'specific heat in KJ/K\')
title(\'TEMP VS CP\')
figure(2)
plot(temp,h)
xlabel(\'temp in K\')
ylabel(\'enthalpy in KJ/K\')
title(\'enthalpy VS CP\')
figure(3)
plot(temp,s)
xlabel(\'temp in K\')
ylabel(\'entropy in KJ/K\')
title(\'TEMP VS CP\')
saveas(1,\'cp.png\')
saveas(2,\'s.png\')
saveas(3,\'h.png\')
fclose(f1)
EXPLANATION OF THE CODE
1.Line 5 to 10
We have used fgetl to skip the first 5 lines as they are irrevalent to our aim. Skp1 refers to skipping line 1 and so forth till line 5.
2.Line 12 to 67
Since there are 53 species and each species having 4 lines consisting of species name and temprature range on the first line and the following with 14 coeffcients in the order of 5 ,5 and 4.Hence we have defined the first 4 line with line1 to line 4 for each species ,the for loop will run these 53 times hence we have defined it for i=1:53.
Next step is defining the elements in line1 we have used strsplit command which splits all the elements of th lines and we have defined the 1st element of line1 as species which represents the name of the species.
We have used the command such strdind to find the coefficents which are in character format and after that used str2num to convert them into number ,hence we found all 14 coefficents for the 53 species .
we have defined the temp range using temp and linspace which is between 200 3500 having 1000 values.After that we have used the given relation :
Now after getting all the values of the coefficients we give the if condition which states that if the temperature is below 1000 K we will use coefficient C1 TO C7 but if the temperature is above 1000 K we use the coeffiecents C8 to C14.
Here
cp=specific heat
h=enthalpy
s=entropy
These values are calculated and stored.
3.Line 70 to 73
We are basically saving the species folders to directory.
4.Line 85 to 100
We are creating the plots of different characteristics vs temp as follows :
Specific Heat vs temp
Enthalpy vs temp
Entropy vs temp
ERRORS
One major error faced was with for loop for all the species ,this was corrected by improving the thought process and writing a better code from the start
RESULTS
g
FOLDERS