Challenge

Project 1 - Parsing NASA thermodynamic data

AIM : 1)To get familiar with file parsing 
2) Read the NASA thermo file 
3) Extract the imformation needed to plot various graphs of different species with temperature.
4) Find molecular  mass of each specie.








close all 
clear all 
clc
%opening the file 
f1 = fopen('THERMO.dat','r');
f2 = fopen('Molecular_mass.txt','w');
%as first 5 lines do not matter to us we want to skip them so we are using for loop to execute fgetl 5 times 
for j=1:5
    fgetl(f1);
end


for  i = 1:53
%fgetl here reads the 6 the line of  the data which contains local mid, low and high temp and name of species.
%to extract the specie name we have to split the coloumns using strsplit
k = fgetl(f1);
s = strsplit(k,'  ');
species  = char(s(1));%specie name extracted
%now to extract the local temp we know there is common DOT BETWEEN  them so
%with the help of strsplit we carry out extraction
str1=strfind(k,'.');
%now the req no. is 4 places behind and 3 places ahead of DOT so using this
%logic to get out number and storing it as number from cell with help pf
%str2double
local_low_temp=str2double(k(str1(1)-4:str1(1)+3))
local_high_temp=str2double(k(str1(2)-4:str1(2)+3))
local_mid_temp=str2double(k(str1(3)-4:str1(3)+3))
%this fgetl reads the next line which contains 5 varaibles of the specie as
%common in them is E letter we use it as splitter in strsplit command
p = fgetl(f1);
    t =findstr(p,'E');
    %now with the referemce we extract out the number with help of
    %str2double.
 
a1= str2double(p(1:t(1)+3));
a2 = str2double(p(t(1)+4:t(2)+3));
a3 = str2double(p(t(2)+4:t(3)+3));
a4 = str2double(p(t(3)+4:t(4)+3));
a5 = str2double(p(t(4)+4:t(5)+3));
 
    p1 = fgetl(f1);%next line in data
    t1 =findstr(p1,'E');
   
a6=str2double(p1(1:t1(1)+3));
a7 = str2double(p1(t1(1)+4:t1(2)+3));
a8 = str2double(p1(t1(2)+4:t1(3)+3));
a9 = str2double(p1(t1(3)+4:t1(4)+3));
a10 = str2double(p1(t1(4)+4:t1(5)+3));
 
p2 = fgetl(f1);%next line
t2 =findstr(p2,'E');
   
a11= str2double(p2(1:t2(1)+3));
a12 = str2double(p2(t2(1)+4:t2(2)+3));
a13 = str2double(p2(t2(2)+4:t2(3)+3));
a14 = str2double(p2(t2(3)+4:t2(4)+3));

%now as we have to plot graph of temp to the S,H,CP dividing the local temp
%from local low to local high temp into 1000 parts 

local_temp = linspace(local_low_temp,local_high_temp,1000);

R = 8.314; %defining the value of R that is universal constant

%as we know first 7 coefficient are hight temp coeffecient and nex t 7 are
%low temp coefficient using if loop to execute the same logic
    
    if local_temp>=local_mid_temp
        CP = (a1 + a2.*local_temp + a3.*(local_temp).^2 +a4.*(local_temp).^3 + a5.*(local_temp).^4).*R ;
        H  = ((a1 + a2.*(local_temp)/2 + a3.*(local_temp).^2)/3 +a4.*((local_temp).^3)/4 + a5.*((local_temp).^4)/5 + a6/(local_temp)).*R.*(local_temp); 
        S  = (a1.*ln((local_temp)) + a2.*(local_temp) + a3.*((local_temp).^2)/2 + a4.*((local_temp).^3) + a5.*((local_temp)^4)/4 + a7).*R;

    else
         
        CP = (a8 + a9.*local_temp + a10.*(local_temp).^2 +a11.*(local_temp).^3 + a12.*(local_temp).^4).*R ;
         H  = ((a8 + a9.*(local_temp)/2 + a10.*(local_temp).^2)/3 +a11.*((local_temp).^3)/4 + a12.*((local_temp).^4)/5 + a13)/(local_temp).*R.*(local_temp) ;
        S  = (a8.*log((local_temp)) + a9.*(local_temp) + a10.*((local_temp).^2)/2 + a11.*((local_temp).^3) + a12.*((local_temp).^4)/4 + a14).*R;
    end

%as the name of specie id cell array to convert it into character char command is used.    
sp = char(species)
%this tells the current directory where the program is stored
prev = pwd

%MOLECULAR WEIGHT function calling
mm = molecular_weights(species);

%printing value of molecular mass
fprintf(f2,'%s \n %s %f \n',sp,'MOLECULAR MASS = ',mm)
%creating new directory and naming it on specie name
mkdir(sp)
cd(sp)


   figure(1)
%plotting of graph of Temp Vs CP
plot(local_temp,CP)
xlabel('local temp')%labeling 
ylabel('Specific Heat(CP)')
title( 'Temp Vs CP for ',species) %providing title to plot

figure(2)
plot( local_temp,H)%plotting of graph of Temp Vs H
xlabel('local temp')%labeling 
ylabel('Enthalpy(H)')
title( 'Temp Vs H for ',species)%providing title to plot

figure(3)
plot( local_temp,S)%plotting of graph of Temp Vs S
xlabel('local temp')%labeling 
ylabel('Entropy(S)')
title( 'Temp Vs S for ',species)%providing title to plot
   
%saving each figure
saveas(figure(1),'SpecificHeat_vs_T.jpg')
saveas(figure(2),'Enthalpy_vs_T.jpg')
saveas(figure(3),'Entropy_vs_T.jpg')

%redirecting to previous directory
cd(prev)

end

%closing file f1 and f2
fclose(f2);
fclose(f1);

===============================================




function M = molecular_weights(species_element)
%storing alphabates in array
atoms = ['H','C','N','O','R'];
%displaying molecular weight
disp('MOLECULAR weight of ')
%disp specie name
disp(species_element);
disp(':');
%storing value of each element in array
Atomic_weight = [1 12 14 16 40]
%settinf initial value to 0
M = 0;
%here erach aphlabet saved id compared with the each element in specie name
%and if matched realted atomic weight is added and calulation is carried
%out
for i = 1:length(species_element)
    
    for j = 1:length(atoms)
        
        if strcmp(species_element(i),atoms(j))
            M = M + Atomic_weight(j)
            p=j;
        end
    end
    n = str2num(species_element(i));
    if n>1
        M = M+( Atomic_weight(p))*(n-1)
    end
end
%displaying the final value of molecular weight 
fprintf('%d',M)
disp('');
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.