Project 1 - Parsing NASA thermodynamic data
File Parsing:Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar.
File parsing refers to read text or data file from external sources and store them.
In this process, we have used two major commands to get data out of the .dat file(data file)
1. fopen:
fileID = fopen(filename,permission) opens the file with the type of access specified by permission.
If fopen cannot open the file, then fileID is -1 in command window.
Permission:w-write permission, r -read permission.
2.fgetl:
tline = fgetl( returns the next line of the specified file, removing the newline characters.fileID)
-
If the file is nonempty, then
fgetlreturnstlineas a character vector. -
If the file is empty and contains only the end-of-file marker, then
fgetlreturnstlineas a numeric value-1.close all clear all clc %Read access to the .dat file and obtaining header info f1= fopen('THERMO.DAT','r'); fgetl(f1); tline=fgetl(f1); A=strsplit(tline,' '); global_low_temp=str2num(A{2}); global_mid_temp=str2num(A{3}); global_high_temp=str2num(A{4}); for i=1:3 fgetl(f1) end %Obtain coefficients and local temperature values for each species. for i=1:53 species=fgetl(f1); x=strsplit(species,' '); species_name=(x{1}); y=findstr(species,'.'); %obtaining local temperature values. y1=str2double(species(49:y(1)+3)); y2=str2double(species(58:y(2)+3)); y3=str2double(species(68:y(3)+3)); %obtaining Coefficient values. %Line1 coefficients. coeff_l1=fgetl(f1); a=strfind(coeff_l1,'E'); a1=str2double(coeff_l1(1:a(1)+3)); a2=str2double(coeff_l1(a(1)+4:a(2)+3)); a3=str2double(coeff_l1(a(2)+4:a(3)+3)); a4=str2double(coeff_l1(a(3)+4:a(4)+3)); a5=str2double(coeff_l1(a(4)+4:a(5)+3)); %Line2 coefficients. coeff_l2=fgetl(f1); b=strfind(coeff_l2,'E'); a6=str2double(coeff_l2(1:b(1)+3)); a7=str2double(coeff_l2(b(1)+4:b(2)+3)); a8=str2double(coeff_l2(b(2)+4:b(3)+3)); a9=str2double(coeff_l2(b(3)+4:b(4)+3)); a10=str2double(coeff_l2(b(4)+4:b(5)+3)); %Line3 coefficients. coeff_l3=fgetl(f1); c=strfind(coeff_l3,'E'); a11=str2double(coeff_l3(1:c(1)+3)); a12=str2double(coeff_l3(c(1)+4:c(2)+3)); a13=str2double(coeff_l3(c(2)+4:c(3)+3)); a14=str2double(coeff_l3(c(3)+4:c(4)+3)); %calculate Specific heat,Enthalpy and Entropy for all species. R=8.3145; T=linspace(y1,y2,1000); for k= 1:length(T) if (T(k)>y3) H(k)= R*T(k)*(a1+((a2*T(k))/2)+((a3*T(k)^2)/3)+((a4*T(k)^3)/4)+((a5*T(k)^4)/5)+(a6/T(k))); S(k)=((a1*log(T(k)))+(a2*T(k))+((a3*T(k)^2)/2)+((a4*T(k)^3)/3)+((a5*T(k)^4)/4)+(a7))*R; cp(k)= R*(a1+(a2*T(k))+(a3*T(k)^2)+(a4*T(k)^3)+(a5*T(k)^4)); else H(k)= R*T(k)*(a8+((a9*T(k))/2)+((a10*T(k)^2)/3)+((a11*T(k)^3)/4)+((a12*T(k)^4)/5)+(a13/T(k))); S(k)=((a8*log(T(k)))+(a9*T(k))+((a10*T(k)^2)/2)+((a11*T(k)^3)/3)+((a12*T(k)^4)/4)+(a14))*R; cp(k)= R*(a8+(a9*T(k))+(a10*T(k)^2)+(a11*T(k)^3)+(a12*T(k)^4)); end end %creating a directory to save the images mkdir( ['C:\Users\admin\Documents\MATLAB\File_parsing\plots\',species_name]); cd ( ['C:\Users\admin\Documents\MATLAB\File_parsing\plots\',species_name]); %Plot CP, enthalpy and entropy for each species figure(1) plot(T,cp,'linewidth',3) xlabel('Temperature:lowtohigh') ylabel('Specific heat') title(species_name) saveas(figure(1),'Specificheat.png') figure(2) plot(T,H,'linewidth',3) xlabel('Temperature:lowtohigh') ylabel('Enthalpy') title(species_name) saveas(figure(2),'Enthalpy.png') figure(3) plot(T,S,'linewidth',3) xlabel('Temperature:lowtohigh') ylabel('Entropy') title(species_name) saveas(figure(3),'Entropy.png') %Calculate molecular mass for all the species. elements=['H','C','O','N','A']; atomic_weight=[1.0079 12.0107 15.999 14.0067 39.948]; mol_mass=0; for l =1:length(species_name) for m=1:length(atomic_weight) if strcmp(species_name(1),elements(m)) mol_mass=mol_mass+atomic_weight(m); position=m; end end n=str2double(species_name(l)); if n>1 mol_mass=mol_mass+atomic_weight(position)*(n-1); end end fprintf('Molecular weight of %s:%f u',species_name,mol_mass) end fclose(f1)
Explanation:
The available file here is thermo.dat which contains
1. Header information
2.Global temperatures
3. Species name(ex. O,H,Netc) and local temperatures
4.Coefficients of the species with which we can calculate Specific heat, Enthalpy and Entropy.
Open the Thermo.dat file using fopen command.
Read the header information through fgetl command.
To get the Species name, we read the line through fgetl and split the string based on spaces present in the line.the first cell represents the species name.
Similarly we get the local temperature using the strfind command where '.' is searched and based on that local temperature is determined.
To get Coefficients: we determine the position of 'E' using strfind and store it in a array.Based on the E's position we can calculate the LHS and RHS of E so that the entire coefficient is available.
using this method, we calculate all the 14 coeeficients,out of which the first 7 represent hight temp coefficient and rest 7 low temperature coefficient.
With available formula of Cp,H and S we calculate the respective values.
Since we have to plot the values against time, we create an array using linspace command,and use if condition to sort the high and low temperature values.
plot CP,H,S against time and save them in a specific directory for future reference.
mkdir creates parentFolder folderNamefolderName in parentFolder. If parentFolder does not exist, MATLAB attempts to create it.
cd changes the current folder to newFoldernewFolder. Folder changes are global. Therefore, if you use cd within a function, the folder change persists after MATLAB® finishes executing the function
saveas( creates the file using the specified file format, fig,filename,formattype)formattype. If you do not specify a file extension in the file name, for example, 'myplot', then the standard extension corresponding to the specified format automatically appends to the file name. If you specify a file extension, it does not have to match the format. saveas uses formattype for the format, but saves the file with the specified extension. Thus, the file extension might not match the actual format used.
Mkdir creates a folder path,cd helps us to get into the folderstructure. Saveas command saves the plots into the current directory.
To calculate molecular mass.
A nested for loop is used with species name and elements as input.
String compare command,strcpm is used to compare the strings-Species name and elements, and when they are equal, molecular mass is calculated.
when the elements has a higher order eg O2, O has to be calculated twice, so we use n variable and calculate the molecular mass.
FInally we output this onto command window using fprintf command.
we use a for loop for the entire code to run it 53(num of species) times to get desired output.
Errors
While determining Cp,H and S, i had considered only T and not T(k) which resulted in wrong graphs.here T is an array while T(K) is a value.