Challenge

Project 1 - Parsing NASA thermodynamic data

FILE PARSING NASA's THERMODYNAMIC DATA

this project uses simple file parsing commands to access complex huge data files provided by NASA as their thermodynamic data. NASA provided this thermodynamic data with a database of over 1000 species, this data includes about 14 coefficients including Cp, H and S values of every species.

this code extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.

the following formulas were used to use the coefficients to plot the data over how it changes from one species to another.

after retreiving the files using 'fopen' and 'fgetl' commands, using these we read the files for the data, we assign different columns to different values of coefficients for different species and using the with the formulas, we come up with a relation for the data. using those 3 relations that we obtain, we create plots of the data.

after creating the plots. we use the data that we solved for and save the plots as images, write it onto new files that we create and then save it categorically along with the data we got with a seperate folder for each species.

clear all
close all
clc

%accessing NASA's THERMODYNAMIC DATA
f1=fopen('THERMO.dat','r');
first_line=fgetl(f1);
%obtaining of global maximum , medium and minimum temperature
global_temp=fgetl(f1)
temp_range=strsplit(global_temp,' ')
global_min_temp= str2num(temp_range{2})
%cell array conversion 
global_mid_temp= str2num(temp_range{3})
global_max_temp= str2num(temp_range{4})

%RANGE OF TEMP
T=linspace(global_min_temp, global_max_temp,400);
R=8.314;

%skipping the header comments 
for i=3:5
      comments_header=fgetl(f1)
end

for j = 1:53
    %extracting first and species name
    line1=fgetl(f1);
    name=strtok(line1);
    %remainists of all characters from first delimiter on
    
    %extracting higher and lower temperature coefficients
    line2=fgetl(f1);
    line3=fgetl(f1);
    line4=fgetl(f1);
    
    A=strfind(line2, 'E');
    B=strfind(line3, 'E');
    C=strfind(line4, 'E');
    
    %extracting higher temp coefficients 
    a1=str2num(line2(1:A(1)+3));
    a2=str2num(line2(A(1)+4:A(2)+3));
    a3=str2num(line2(A(2)+4:A(3)+3));
    a4=str2num(line2(A(3)+4:A(4)+3));
    a5=str2num(line2(A(4)+4:A(5)+3));
    a6=str2num(line3(1:B(1)+3));
    a7=str2num(line3(B(1)+4:B(2)+3));
    
    %EXTRACTING lower temp coefficients'
    b1=str2num(line3(B(2)+4:B(3)+3));
    b2=str2num(line3(B(3)+4:B(4)+3));
    b3=str2num(line3(B(4)+4:B(5)+3)); 
    b4=str2num(line4(1:C(1)+3));
    b5=str2num(line4(C(1)+4:C(2)+3));
    b6=str2num(line4(C(2)+4:C(3)+3));
    b7=str2num(line4(C(3)+4:C(4)+3));
    
    %specific heat calculation
    cp=specificheat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,global_mid_temp);
    
    %enthalphy calculation
    h=enthalphy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,global_mid_temp);
    
    %entropy calculations
    s=entropy_calc(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,R,global_mid_temp);
    
    %calculate molecular weight of species 
    Molecular_wt(i)=molecular_weight(name)
    
    %creating individual folders for each species
    plotting(cp,h,s,T,name)
    
end

function MW= molecular_weight(name)

atoms= ['H' , 'C' , 'O' , 'N' , 'R'];
  fprintf('MOLECULAR WEIGHT of');
  fprintf(name);
  fprintf(':');
  atomic_weight=[ 1 12 16 14 40];
  MW=0;
  for i=1:length(name)
      for j=1:length(atoms)
          if strcmp(name(i),atoms(j))
              MW=MW+atomic_weight(j);
              position=j;
          end
          %here addding the MW in the same fashion as we do in palindrome
          %numbers
      end
      n=str2num(name(i));
      if n>1
          MW+MW+atomic_weight(position)*(n-1);
      end
  end
  fprintf('%d', MW)
  disp(' ');
end

%plotting function
function plotting(cp,h,s,T,name)
cd(pwd)
dir_name=name;
mkdir(dir_name);

figure(1)
plot(T,cp,'linewidth',3,'color','b')
title(sprintf('SPECIFIC HEAT VS TEMP FOR %s',name))
%sprintf is the same as fprintf except that it returns the data in a MATLAB 
%string variable rather than writing it to a file
xlabel('TEMP')
ylabel('SPRECIFIC HEAT')

figure(2)
plot(T,h,'linewidth',3,'color','r')
title(sprintf('ENTHALPHY VS TEMP FOR %s',name))
xlabel('TEMP')
ylabel('ENTHALPHY')

figure(3)
plot(T,s,'linewidth',3,'color','r')
title(sprintf('ENTROPY VS TEMP FOR %s',name))
xlabel('TEMP')
ylabel('ENTROPY')

pause(10)
cd(dir_name)
      saveas(1,'cp_vs_t.png')
      saveas(2,'h_vs_t.png')
      saveas(3,'s_vs_t.png')

      cd ..
end

      
function s=entropy_calc(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,T,R,global_mid_temp)
if T>=global_mid_temp
    s=R.*(a1.*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7);
else
    s=R.*(b1.*log(T)+b2.*T+b3.*(T.^2)./2+b4.*(T.^3)./3+b5.*(T.^4)./4+b7);
end
end
function h=enthalphy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,T,R,global_mid_temp)
    if T>=global_mid_temp
        h=R.*(a1.*T+(a2.*T.^2)./2+(a3.*T.^3)./3+(a4.*T.^4)./4+(a5.*T.^5)./5+a6);
    else
        h=R.*(b1.*T+(b2.*T.^2)./2+(b3.*T.^3)./3+(b4.*T.^4)./4+(b5.*T.^5)./5+b6);
    end
end

function cp=specificheat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,T,R,global_mid_temp)
    if T >= global_mid_temp
        cp=R.*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
    else
        cp=R.*(b1+b2.*T+b3.*T.^2+b4.*T.^3+b5.*T.^4);
    end
end
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.