Project 1 - Parsing NASA thermodynamic data
INTRODUCTION:
File parsing is the process of reading computer files and compiling data from those files through the means of a programming language. A data file containing ordered and spaced elements of repeating pattern can be read through the compiler. The compiler must be able to modify and create files and have a preconcieved path systems. Provided it can do all of the above, file parsing can be conducted.
Here, In this project, we have a THERMO.dat file containing data from 53 species of conpounds with their local temperature range and their 14 coefficients. 1-7 are used for low temperature and 8-14 replaces 1-7 in the same order for high tempertures. Therefore, where the 1970's guys were prideful of typing speeds and such our generation has found a way to be lazy and cool in a high class way.
SNYTAX OF SOME FUNCTIONS USED:
- FileID = fopen( filename ) opens the file, filename , for binary read access, and returns an integer file identifier equal to or greater than 3
- tline = fgetl(obj) reads one line of text from the instrument connected to obj , and returns the data to tline . The returned data does not include the terminator with the text line.
- k = findstr( str1,str2 ) searches the longer of the two input arguments for any occurrences of the shorter argument and returns the starting index of each occurrence. If it finds no occurrences, then findstr returns the empty array, [] .
- status = mkdir(___) creates the specified folder and returns a status of 1 if the operation is successful or if the folder exists. Otherwise, mkdir returns 0 and does not throw a warning or error to the Command Window.
- oldFolder = cd( newFolder ) returns the existing current folder to oldFolder , and then it changes the current folder to newFolder .
- The file extension defines the file format. If you do not specify an extension, then saveas saves the figure to a FIG-file. To save the current figure, specify fig as gcf . saveas( fig , filename , formattype ) creates the file using the specified file format, formattype
FLOWCHART/PSEUDOCODE:
- f1 file ID is assigned to open the file THERMO.dat in the program. fgetl is used to skip the few lines to the data. Global temperature values are noted using str2num on 2nd line.
- We now need to read four lines of repeating data untill we reach 'END'. while is used to read the four lines for one species at a timeuntil the condition (fgetl~="END") is reached.
- str1,str2,str3,str4 is used to store the first four lines of data to be read.
- We read species name, local temperature limits(low,middle,upper) and 14 coefficients(5 in line 2, 5 in line 3 and 4 in line 4)
- first word is the species name. while loop is used to find the first word from str1 with the first " " as the end condition. str2num() is inputed with the str1(48 to 75) to get the local temperature values of lth,ltm,ltl.
- Then we read the coefficients using the e1,e2,e3 = findstr(str) and reading the left and right of the E in every coefficient.
- To calculate and display the molecular weight of each species in species_names[]. This can be done by reading each species name inside the for loop,findstr() is used to do a first readinging and orderly assigned to variable NOE. In this for loop another nested for loop is used to house a nested if statements to compensate for the number of elements in the compund formula.
- MOW-molecular weight data is matrix multiplied with number of elements NOE to get the Molecular Weight. Then it is displayed under the first for loop.
- Calculate the Cp,H,S values for all values of the species using for loop and formula. use if statement to divide between high and low temperatures.'ltm' middle value is the dividing line.
- make a new folder 'plot data' in the pwd using mkdir
- start a for loop from 1 to length(species_names). plot three figures of Cp vs T ,H vs T and S vs T. Use cd to open plot data directory. Create a folder in each iteration with species name and save 3 figures using saveas in all of them.
- end of program
ERROR FACED:

This error is solved by making sure that only numbers could reach the str2num(ch(m)) function using an if statement.Therefore this program only works for compunds with less than 10 numbers of each element.
MAIN PROGRAM:
close
clear
clc
%Extracting data from the THERMO.dat file
%Assigning Thermo.dat file a file ID
f1 = fopen("THERMO.dat","r");
fgetl(f1);
GTVs = str2num(fgetl(f1));
%ignoring lines that start with a '!'
fgetl(f1);
fgetl(f1);
fgetl(f1);
%Reading the File to get the data for all species, every species is 4 lines
%Arrays will be created for the variables
%Species Name, Local Temperature Values, Coefficients(a1-a14)
condition=0;
i=1;
while(condition==0)
str1=fgetl(f1);%line 1
if(str1~="END")
%EXTRACTIHG SPECIES DATA
n=1;
species_names(i)="";
while(str1(n)~=" ")
species_names(i)=species_names(i)+str1(n);
n=n+1;
end
LTVs=str2num(str1(48:75));
lth(i)=LTVs(2);
ltm(i)=LTVs(3);
ltv(i)=LTVs(1);
str2=fgetl(f1);%line 2
e1=findstr(str2,"E");
a01(i)=str2num(str2(1:e1(1)+3));
a02(i)=str2num(str2(e1(1)+4:e1(2)+3));
a03(i)=str2num(str2(e1(2)+4:e1(3)+3));
a04(i)=str2num(str2(e1(3)+4:e1(4)+3));
a05(i)=str2num(str2(e1(4)+4:e1(5)+3));
str3=fgetl(f1);%line 3
e2=findstr(str3,"E");
a06(i)=str2num(str3(1:e2(1)+3));
a07(i)=str2num(str3(e2(1)+4:e2(2)+3));
a08(i)=str2num(str3(e2(2)+4:e2(3)+3));
a09(i)=str2num(str3(e2(3)+4:e2(4)+3));
a10(i)=str2num(str3(e2(4)+4:e2(5)+3));
str4=fgetl(f1);%line 4
e3=findstr(str4,"E");
a11(i)=str2num(str4(1:e3(1)+3));
a12(i)=str2num(str4(e3(1)+4:e3(2)+3));
a13(i)=str2num(str4(e3(2)+4:e3(3)+3));
a14(i)=str2num(str4(e3(3)+4:e3(4)+3));
i=i+1;
else
condition=1;
end
end
%Moleculr Weight Display
element=["H","C","N","O","S","AR"];
MOW=[1,12,14,16,32,40];
for l=1:length(species_names)
ch =char(species_names(l));
NOE=[length(findstr(ch,"H")),length(findstr(ch,"C")),length(findstr(ch,"N")),length(findstr(ch,"O")),length(findstr(ch,"S")),length(findstr(ch,"AR"))];
ch=ch';
for m=2:length(ch)
if str2num(ch(m))<10
if ch(m-1)=="H"
NOE(1)=NOE(1)+str2num(ch(m));
NOE(1)=NOE(1)-1;
elseif ch(m-1)=="C"
NOE(2)=NOE(2)+str2num(ch(m));
NOE(2)=NOE(2)-1;
elseif ch(m-1)=="N"
NOE(3)=NOE(3)+str2num(ch(m));
NOE(3)=NOE(3)-1;
elseif ch(m-1)=="O"
NOE(4)=NOE(4)+str2num(ch(m));
NOE(4)=NOE(4)-1;
elseif ch(m-1)=="S"
NOE(5)=NOE(5)+str2num(ch(m));
NOE(5)=NOE(5)-1;
end
end
end
disp('Molecular Weight of '+species_names(l)+'= '+NOE*MOW')
end
%plot data folder in pwd
mkdir('plot data');%creating folder called plot data
current_Dir=pwd+"\plot data\";
%plotting and saving all the plots
for j=1:length(species_names)
T=linspace(ltv(j),lth(j),100);%creating a temperature array of 30 values
for k=1:length(T)
if T(k)<ltm(j)
%formula given used with a1-a7 for low temp
Cp(k)=8.314.*(a01(j)+a02(j).*T(k)+a03(j).*T(k).^2+a04(j).*T(k).^3+a05(j).*T(k).^4);
H(k)=8.314.*T(k).*(a01(j)+(a02(j)./2).*T(k)+(a03(j)./3).*T(k).^2+(a04(j)./4).*T(j).^3+(a05(j)./5).*T(k).^4+a06(j)./T(k));
S(k)=8.314.*(a01(j).*log(T(k))+a02(j).*T(k)+(a03(j)./2).*T(k).^2+(a04(j)./3).*T(k).^3+(a05(j)./4).*T(k).^4+a07(j));
else
%formula given used with a7-a14 for high temp
Cp(k)=8.314.*(a08(j)+a09(j).*T(k)+a10(j).*T(k).^2+a11(j).*T(k).^3+a12(j).*T(k).^4);
H(k)=8.314.*T(k).*(a08(j)+(a09(j)./2).*T(k)+(a10(j)./3).*T(k).^2+(a11(j)./4).*T(k).^3+(a12(j)./5).*T(k).^4+a13(j)./T(k));
S(k)=8.314.*(a08(j).*log(T(k))+a09(j).*T(k)+(a10(j)./2).*T(k).^2+(a11(j)./3).*T(k).^3+(a12(j)./4).*T(k).^4+a14(j));
end
end
mkdir(current_Dir,species_names(j))
figure(1)
plot(T,Cp)
xlabel('TEMPERATURE(K)')
ylabel('SPECIFIC HEAT-Cp')
title('Cp vs T ('+species_names(j)+')')
figure(2)
plot(T,H)
xlabel('TEMPERATURE(K)')
ylabel('ETHALPY')
title('H vs T ('+species_names(j)+')')
figure(3)
plot(T,S)
xlabel('TEMPERATURE(K)')
ylabel('ENTROPY')
title('S vs T ('+species_names(j)+')')
%changeing directory and saving the plots
cd(current_Dir+species_names(j))
saveas(figure(1),'Cp vs T','jpg');
saveas(figure(2),'H vs T','jpg');
saveas(figure(3),'S vs T','jpg');
end
%end of program
OUTPUT:

Picture of all folders after the program runs.

Some of the output graphs

Sample output of the Molecular Weights. refer to results.pdf for full output.