Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing NASA thermodynamic data

File Parsing :

It is basically a process to extract useful and important information from a file and at the same time it also allows us to write something to a file.

In this project we have a NASA thermodynamic data file form which

- Extract all the 14 coefficients at high and low temperature.

- Calculate the Specific heat, enthalpy, and entropy of all the species and plot these graph with temperature variation.

Code Explanation:

Initially, we starts with clear all, close all, and clc. Open file ‘THERMO.DAT’ using fopen command and use r to read that file and save it to f1(a pointer to the file). Command fgetl() help to read file line by line so to read second line of temperature we enter this command twice.

Here we use strsplit command to split the string by a space and assign a variable A to it. Then convert to number using str2num command and assign it to global_lt, global_mt, global_ht.

After that, to skip next 3 comment line use for loop with fgetl command. Now from this line6 we have total 53 species so for them we use a fore loop which range from 1 to 53 and that also include the all 14 coefficient of every species. The local temperature and species name are extracted using strsplit command and further converted to number with same str2num command.

Now again move to next line7 and from here we use strfind command till line9 to extract coefficient with ‘E’ boundary. After that assign them variables h1,h2… and again convert to a number using str2num command.

The initial 7 coefficient are of high temperature and last 7 are of low temperature.

Then create a directory using mkdr command to save all the species with their name folder and also to save graph further. Now create a range of temperature from 300 to 5000 for calculating enthalpy , entropy and specific heat. For this create a for loop up to the length of temperature and put if else for calculating the enthalpy, entropy and specific heat to high and low temperature respectively.

Then plot the graph between temperature vs enthalpy, temperature vs entropy and temperature vs specific heat using plot command and provide the x,y labels and also give title of their species. Here the directory changes and use saveas command to save all the plots of molecules.

After this we calculate the molecular mass of all species, for this we store all the basic Constituents and their Atomic weight in array for molecular weight we set the for loop initial condition as mm= 0.

Start the nested loop for the length of species and element with name l, m respectively. Then use the strcmp command to commpair the element if the is identical or not. When this match it returns 1 otherwise it will go to next line and convert the string to number and then again if more than 1 then it follow different mm formula where the number will reduced by 1and so the weight will calculated.

Code:

% fscanef()to read from a file
clear all
close all
clc
f1 = fopen('THERMO.dat', 'r');
fgetl(f1);
line2=fgetl(f1);
A=strsplit(line2,' ');
 global_lt=str2num(A{2});
 global_mt=str2num(A{3});
 global_ht=str2num(A{4});
 
% FOR 'O'

 for i = 1:3
  line5 = fgetl(f1);
 end
 
 
for i= 1:53
 line6 = fgetl(f1);
 T = strsplit(line6,' ');
 species  = (T{1});
 local_lt = str2num(T{6});
 local_ht = str2num(T{7});
 local_mt = str2num(T{8});
   
line7 = fgetl(f1); % FOR High temp. coefficient
C=strfind(line7,'E');
h1=line7(1:C(1)+3);
h2=line7(C(1)+4:C(2)+3);
h3=line7(C(2)+4:C(3)+3);
h4=line7(C(3)+4:C(4)+3);
h5=line7(C(4)+4:C(5)+3);
  h1=str2num(h1);
  h2=str2num(h2);
  h3=str2num(h3);
  h4=str2num(h4);
  h5=str2num(h5);
  
line8 = fgetl(f1); 
D=strfind(line8,'E');
h6=line8(1:D(1)+3);
h7=line8(D(1)+4:D(2)+3);
L8=line8(D(2)+4:D(3)+3); % FOR Low temp. coefficient 
L9=line8(D(3)+4:D(4)+3);
L10=line8(D(4)+4:D(5)+3);
  h6=str2num(h6);
  h7=str2num(h7);
  L8=str2num(L8);
  L9=str2num(L9);
 L10=str2num(L10); 
 
line9 = fgetl(f1); 
E=strfind(line9,'E');
L11=line9(1:E(1)+3);
L12=line9(E(1)+4:E(2)+3);
L13=line9(E(2)+4:E(3)+3); 
L14=line9(E(3)+4:E(4)+3);
  L11=str2num(L11);
  L12=str2num(L12);
  L13=str2num(L13);
  L14=str2num(L14);

           % creating directory
   mkdir ('F:\MATLAB\bin\AA___Skill Lync\figures\NASA File Parsing\species',species);
  
   
  T= linspace(300,5000,100);
  R= 8.314;
     for i = 1: length(T)
       if T>2000
          
          Cp = (h1+h2.*T+h3.*T.^2+h4.*T.^3+h5.*T.^4)*R ;  
           H = (h1+(h2.*T)/2+(h3.*T.^2)/3+(h4.*T.^3)/4+(h5.*T.^4)/5+(h6./T))*R;
           S = h1.*log(T)+h2.*T+(h3.*T.^2)/2+(h4.*T.^3)/3+(h5.*T.^4)/4+h7;
 
         else   
          Cp = (L8+L9.*T+L10.*T.^2+L11.*T.^3+L12.*T.^4)*R ;  
           H = (L8+(L9.*T)/2+(L10.*T.^2)/3+(L12.*T.^3)/4+(L13.*T.^4)/5+(L14./T))*R;
           S = L8.*log(T)+L9.*T+(L10.*T.^2)/2+(L11.*T.^3)/3+(L12.*T.^4)/4+L14;
   
     
       end
     end

       % TRMPRATURE VS ENTROPY
      figure(1);
      plot(T,S,'linewidth',3,'color','b')
      title(species);
      xlabel('Temprature')
      ylabel('Entropy')
      
       % TRMPRATURE VS ENTHALPY
      figure(2);
      plot(T,H,'linewidth',3,'color','r')
      title(species);
      xlabel('Temprature');
      ylabel('Enthalpy');
      
      % TRMPRATURE VS SPECIFIC_HEAT
      figure(3);
      plot(T,Cp,'linewidth',3,'color','g')
      title(species);
      xlabel('Temprature');
      ylabel('Specific Heat');
      
      
      Directory = (['F:\MATLAB\bin\AA___Skill Lync\figures\NASA File Parsing\species\',species]);
      
      cd(Directory);
      
      saveas(1,'Entropy.jpg');
      saveas(2,'Enthalpy.jpg');
      saveas(3,'Specific Heat.jpg');


% Atomic weight
element = ['H', 'C', 'N', 'O', 'A'];

Atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948]; 

MM = 0; 

for l=1:length(species)
    for m=1:length(element)
        if strcmpi(species(l),element(m))
           [MM] = MM + Atomic_weight(m); 
           position = m;
        end
    end
    n = str2num(species(l));
     if n>1
        [MM] = MM + Atomic_weight(position)*(n-1);
     end
    
end
end
      

Result

Error:

The first for loop for all 53 species was closed before using the saveas command, which stop the ploting and saving graph for each species in their folder so the folder were empty.

I mistakenly put the title name within (''). This put the name of graph 'species' on every plot which then resolved by removeing ('') and use only species in title.

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.