Project

File parsing a NASA document

Aim:-

  • We have been given an file consisting of properties of different species stored by NASA. Our aim is to extract the values of all the properties and calculate the specific heat, enthalpy, and entropy for each of this species.
  • We also have to plot three plots for each of this species and store this plots in different folders of each individual species.
  1. Temperature v/s specific heat
  2. Temperature v/s enthalpy
  3. Temperature v/s entropy
  • Also we have to calculate the molecular weight for each species.

Background:-

File parsing is a method in which data from input file is extracted and stored into memory using programing language. This method is used nearly in each programing language.

Method used:-

  • First we input the file name to open it into matlab program.
  • The file can be viewed here http://combustion.berkeley.edu/gri-mech/version30/files30/thermo30.dat
  • The file is the read line by line and only interesed data is extracted.
  • Function used:-
    file handler= fopen('file name') Open file with a file handler
    line= fgetl('file name') store the line to variable
    fclose('file name') close the file
  • First line is not required so skipped.
  • Next line consists of Global low, transitional and high temperature (kelvin), so they are stored in variables Global_low_temp, Global_intermidiate_temp and Global_low_temp respectively.
  • Next three lines are skipped are they are not useful.
  • Each species has its data stored in four lines as observed.
  • First line of species consist of its name and local low, transitional and high temperatures which are stored in cell arrays name and local_temp.
  • Cell array name is a column vector where each row element represents a species name.
  • Cell array local_temp is a matrix where each row represents a species while the first secound and third column represents local low, transitional and high temperatures.
  • Next three lines of the species consists of 14 constant values where first 7 are low temperature constants while next 7 are high temperature constants.
  • Low temperature is below local transitional temperature while high temperature is above local transitional temperature.
  • First seven constants are stored into cell array low_const while next seven are stored into high_const.
  • Each row of cell array low_const defines a species while 1,2,3,4,5,6 and 7 column represents 1,2,3,4,5,6 and 7 low temperature constant respectively.
  • Each row of cell array high_const defines a species while 1,2,3,4,5,6 and 7 column represents 1,2,3,4,5,6 and 7 high temperature constant respectively.
  • For loop is used to extract this data from each species and store into workspace.
  • Specific heat, enthalpy and entropy are calaculated using the formulaes:-

Here, Cp= Specific heat (cal/(mol K))

R= Universal gas constant (cal/(mol K))

H= Enthalpy (cal/mol)

T= Local temperature (kelvin)

S= Entropy (cal/(mol K))

  • Now, we define universal gas constant R=1.987
  • Using above formulaes specific heat, enthalpy and entropy is calculate for low temperature range and high temperature range for each species.
  • CP, H and S are stored in cell arrays with name cp, h and s respectively.
  • Each row of this cell arrays define a temperature while each colum defines a species.
  • Then, molecular weights of each of this species is calculated and displayed in command window.
  • Molecular weights (g/mol) are calculated by summing the atomic weights of all the elements in the species.
  • The temperature v/s cp, h and s plots are stored into different folders for individual species. Functions used:-
    mkdir('folder name') makes folder in the current folder
    saveas(gcf,'file location') saves the current figure to file location
  • File is then closed using fclose() function.

Assumptions made:-

  • Molecular weights:-
Element Molecular weight (g/mol)
Carbon (C) 12
Hydrogen (H) 1
Oxygen (O) 16
Nitrogen (N) 14
Argon (Ar) 40

Program:-

clear;
close all;
clc
% The program is divided into four section for better understanding


%% Take input file and extract required data
%We will store all the species properties in cells

file=input("Enter file name ");                         %file handler is 'file'
while file<0                                            %loop to avoid invalid file
    fprintf('Cannot open file, try again');             %prints if file is invalid
    file=input("Enter file name ");                     %input file name
end                                                     %end of loop
file=fopen(file,'r');                                   %open file in matlab
line=fgetl(file);                                       %stores first line to 'line'
line=fgetl(file);                                       %get sucessive line

%Store global temperatures
Global_low_temp=double(string(line(1:10)));             %store global low temp
Global_intermidiate_temp=double(string(line(11:20)));   %store global transitional temp
Global_high_temp=double(string(line(21:30)));           %store global high temp

%Skip next three lines as they are not useful
for i=1:3                                               %loop to skip next 3 lines
    line=fgetl(file);                                   %get sucessive line
end                                                     %end of loop
line=fgetl(file);                                       %get sucessive line
i=1;                                                    %variable to count row no.
while string(line(1:3))~="END"                               %loop to check end of file
    %Extract species name and store in cell array called 'name'
    name{i,1}=string(line(~(line(1:16)==' ')));              %store name of species
    
    %Extract species locla temp. and store in cell array 'local_temp'
    local_temp{i,1}=double(string(line(46:55)));             %store local low temp
    local_temp{i,2}=double(string(line(56:65)));             %store local high temp
    local_temp{i,3}=double(string(line(66:75)));             %local transitional temp
    
    %Go to next line for operation
    line=fgetl(file);                                      
    
    %Extract constant values and store into cell array 'low, high const'
    low_const{i,1}=double(string(line(1:15)));               %store low temp a1
    low_const{i,2}=double(string(line(16:30)));              %store low temp a2
    low_const{i,3}=double(string(line(31:45)));              %store low temp a3
    low_const{i,4}=double(string(line(46:60)));              %store low temp a4
    low_const{i,5}=double(string(line(61:75)));              %store low temp a5
    
    %Go to next line
    line=fgetl(file);                                        %get sucessive line
    low_const{i,6}=double(string(line(1:15)));               %store low temp a6
    low_const{i,7}=double(string(line(16:30)));              %store low temp a7
    high_const{i,1}=double(string(line(31:45)));             %store high temp a1
    high_const{i,2}=double(string(line(46:60)));             %store high temp a2
    high_const{i,3}=double(string(line(61:75)));             %store high temp a3
    
    %Go to next line
    line=fgetl(file);                                        %get sucessive line
    high_const{i,4}=double(string(line(1:15)));              %store high temp a4
    high_const{i,5}=double(string(line(16:30)));             %store high temp a5
    high_const{i,6}=double(string(line(31:45)));             %store high temp a6
    high_const{i,7}=double(string(line(46:60)));             %store high temp a7
    
    %Get to the next species data
    line=fgetl(file);                                        %get sucessive line
    i=i+1;                                                   %get to next row
end                                                          %end of loop


%% Calculating the specific heat, enthalpy and entropy
%Universal gas constant =1.987 cal/(mol K)
R=1.987;                                                     %store universal gas const

for i=1:length(name)                                         %iterate over each species
    a=1;                                                     %variable to count row no.
    for j=local_temp{i,1}:100:local_temp{i,2}                %loop to get temp. range
        if j<=local_temp{i,3}                                %condition for low temp
            a1=low_const{i,1};                               %define a1
            a2=low_const{i,2};                               %define a2
            a3=low_const{i,3};                               %define a3
            a4=low_const{i,4};                               %define a4
            a5=low_const{i,5};                               %define a5
            a6=low_const{i,6};                               %define a6
            a7=low_const{i,7};                               %define a7
        else                                                 %for high temp range
            a1=high_const{i,1};                              %define a1
            a2=high_const{i,2};                              %define a2
            a3=high_const{i,3};                              %define a3
            a4=high_const{i,4};                              %define a4
            a5=high_const{i,5};                              %define a5
            a6=high_const{i,6};                              %define a6
            a7=high_const{i,7};                              %define a7
        end                                                  %end of loop
        t=j;                                                 %define temperature
        %Claculating cp and storing into cell array
        cp{a,i}=R*(a1+(a2*t)+(a3*(t^2))+(a4*(t^3))+(a5*(t^4)));   %calculate cp
        
        %Calculating enthalpy and storing into cell array
        h{a,i}=(t*R)*(a1+(a2*(t/2))+(a3*((t^2)/3))+(a4*((t^3)/4))+(a5*((t^4)/5))+(a6/t));
        
        %Calculating entropy and storing into cell array
        s{a,i}=R*(a1*(log(t))+(a2*t)+(a3*((t^2)/2))+(a4*((t^3)/3))+(a5*((t^4)/4))+a7);
        
        a=a+1;                                              %get to next row
    end
end


%% Claculate molecular weights
for k=1:length(name)                                   %iterate over each species
    name1=char(name{k,1});                             %store name of species
    name1('C'==name1)=12;                              %molecular weight of carbon
    name1('H'==name1)=1;                               %molecular weight of hydrogen
    name1('O'==name1)=16;                              %molecular weight of oxygen
    name1('N'==name1)=14;                              %molecular weight of nitrogen
    name1('2'==name1)=2;                               %define value of '2'
    name1('3'==name1)=3;                               %define value of '3'
    name1('4'==name1)=4;                               %define value of '4'
    name1('5'==name1)=5;                               %define value of '5'
    name1('6'==name1)=6;                               %define value of '6'
    name1('7'==name1)=7;                               %define value of '7'
    name1('8'==name1)=8;                               %define value of '8'
    name1('9'==name1)=9;                               %define value of '9'
    for i=1:length(name1)                              %iterate over name length
        itis=[12 1 16 14 2 3 4 5 6 7 8 9];             %acceptable char
        summa=sum(double(name1(i))==itis);             %sum of acceptable char
        if summa==0                                    %check value of summa
            name1(i)=0;                                %name value 0 if not acceptable
        end                                            %end of loop
        num=[2 3 4 5 6 7 8 9];                         %acceptable numbers
        summa=sum(name1(i)==num);                      %sum of logical acceptable no.
        if summa>0                                     %conditional statement
            name1(i-1)=name1(i)*name1(i-1);            %multiply no. to its element
            name1(i)=0;                                %put no. equal to zro
        end                                            %end of condition
    end                                                %end of loop
    molecular_weight=sum(name1);                       %total molecular weight
    if name{k,1}=="AR"                                 %check if species is argon
        molecular_weight=40;                           %state its molecular weight
    end                                                %end of condition
    fprintf("molecular_weight of %6s is %2d (g/mol)\n",name{k,1},molecular_weight);
end                                                    %end of loop


%% Save plots in folders
for i=1:length(name)                                   %iterate over each species
    mkdir(name{i,1});                                  %make folder of its name
    cd="./"+name{i,1};                                 %cd=location of subfolder
    fig_cp_name=name{i,1}+"_cp.jpg";                   %name of specific heat plot
    fig_h_name=name{i,1}+"_h.jpg";                     %name of enthalpy plot
    fig_s_name=name{i,1}+"_s.jpg";                     %name of entropy plot
    counter=1;                                         %Count the row no.
    value=[];                                          %blank array
    for j=local_temp{i,1}:100:local_temp{i,2}          %iterate over each temp.
        value(counter,1)=j;                            %store temp in first column
        value(counter,2)=cp{counter,i};                %store cp in secound column
        value(counter,3)=h{counter,i};                 %store enthalpy in third column
        value(counter,4)=s{counter,i};                 %store entropy in fourth column
        counter=counter+1;                             %get to next row
    end                                                %end of loop
    figure(1)                                          %make figure 1 active
    plot(value(:,1),value(:,2),'k','linewidth',1);     %plot temp v/s cp plot
    xlabel('Temperature (K)');                         %x axis label
    ylabel('Specific heat (cal/(mol K))');             %y axis label
    title('Temperature vs Specific heat');             %title of plot
    grid on;                                           %turns grid on
    saveas(gcf,fullfile(cd,fig_cp_name));              %saves the plot
    figure(2)                                          %makes figure 2 active
    plot(value(:,1),value(:,3),'k','linewidth',1);     %plot temperature v/s enthalpy
    xlabel('Temperature (K)');                         %xaxis label
    ylabel('Enthalpy (cal/mol)');                      %y axis label
    title('Temperature vs Enthalpy');                  %title of plot
    grid on;                                           %turns the grid on
    saveas(gcf,fullfile(cd,fig_h_name));               %saves the plot
    figure(3)                                          %makes figure 3 active
    plot(value(:,1),value(:,4),'k','linewidth',1);     %plot temp v/s entropy plot
    xlabel('Temperature (K)');                         %x axis label
    ylabel('Entropy (cal/(mol K))');                   %y axis label
    title('Temperature vs Entropy');                   %title of plot
    grid on;                                           %turns the grid on
    saveas(gcf,fullfile(cd,fig_s_name));               %saves the plot
end                                                    %end of loop

Possible errors:-

  • Matlab may say that the file you are trying to open is not valid or avaliable. To solve this just make sure that the file you are trying to open is stored in current folder and while giving the input the file name is given in colons i.e 'filename' with filename consisting the extension of file. After this the program may just rum fine.

Outputs:-

  • We get the molecular weights of all the species displayed in command window.
  • We get the plots of temp v/s cp, temp v/s h and temp v/s s for each species stored into individual folder of each species.
  • Plots for some of the species are shown below:-
  • Plots for 'O2'

--> Temperature v/s specific heat plot

--> Temperature v/s enthalpy plot

--> Temperature v/s entropy plot

  • Plots for 'N2'

--> Temperature v/s specific heat plot

--> Temperature v/s enthalpy plot

--> Temperature v/s entropy plot

  • Plots for 'Co2'

--> Temperature v/s specific heat plot

--> Temperature v/s enthalpy plot

--> Temperature v/s entropy plot

Conclusion:-

  • We can calculate the specific heat, enthaply and entropy for any number of species in fraction of time and store the data in an organized manner for further analysis.
  • The same program can be tweaked just a little to take different kinds of inputs and give different kinds of outputs which makes it versatile in nature and save a lot of time.
  • We can change the file parsing technique to read different types of file while getting the correct output.

References:-

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.