Challenge

Project 1 - Parsing NASA thermodynamic data

%% file parsing main code%%

clear all
close all
clc
%reading the data from the file
temp=fopen('THERMO.dat','r');
fgetl(temp);
temp_data=fgetl(temp);
A=strsplit(temp_data,' ');
%extract global temperature range
low=str2num(A{2});
mid=str2num(A{3});
high=str2num(A{4});
for j=1:3
    fgetl(temp);
end
for i=1:53
    

data=fgetl(temp);
a=strsplit(data,' ');
b=a{1};


%extract coefficients of desired molecule
line1=fgetl(temp);
op1=findstr(line1,'E');
coef1=line1(1:op1(1)+3);
coef2=line1(op1(1)+4:op1(2)+3);
coef3=line1(op1(2)+4:op1(3)+3);
coef4=line1(op1(3)+4:op1(4)+3);
coef5=line1(op1(4)+4:op1(5)+3);
a1=str2num(coef1);
a2=str2num(coef2);
a3=str2num(coef3);
a4=str2num(coef4);
a5=str2num(coef5);

line2=fgetl(temp);
op2=findstr(line2,'E');
coef6=line2(1:op2(1)+3);
coef7=line2(op2(1)+4:op2(2)+3);
coef8=line2(op2(2)+4:op2(3)+3);
coef9=line2(op2(3)+4:op2(4)+3);
coef10=line2(op2(4)+4:op2(5)+3);
a6=str2num(coef6);
a7=str2num(coef7);
a8=str2num(coef8);
a9=str2num(coef9);
a10=str2num(coef10);

line3=fgetl(temp);
op3=findstr(line3,'E');
coef11=line3(1:op3(1)+3);
coef12=line3(op3(1)+4:op3(2)+3);
coef13=line3(op3(2)+4:op3(3)+3);
coef14=line3(op2(3)+4:op3(4)+3);
a11=str2num(coef11);
a12=str2num(coef12);
a13=str2num(coef13);
a14=str2num(coef14);
%extract local temperature
mlt=a(end-1);
midlt=str2double(mlt);
hlt=a(end-2);
highlt=str2double(hlt);
llt=a(end-3);
lowlt=str2double(llt);

%plot specific heat v T

T=linspace(lowlt,highlt,100);
R=8.314;
[ cp ]=specific_heat(R,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,midlt,T);
figure(1)
plot(T,cp,'linewidth',3,'color','g')
xlabel('temperature')
ylabel('specificheat')
title(b)

% plot enthalapy v  T;
[ h ] =enthalapy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,midlt,T,R);
figure(2)
plot(T,h,'linewidth',3,'color','b')
xlabel('temperature')
ylabel('enthalapy')
title(b)

%plot entropy v T
[ s ]=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,midlt,T,R);
figure(3)
plot(T,s,'linewidth',3,'color','y')
xlabel('temperature')
ylabel('entropy')
title(b)

mkdir(b)
cd (b)
saveas(1,'specificheat','jpg')
saveas(2,'enthalapy','jpg')
saveas(3,'entropy','jpg')
cd ..
[mw]=mol_weight(b);
end
fclose(temp);

%% function for specific heat%%

function [ cp ]=specific_heat(R,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,midlt,T)

for i=1:100


    if(T(i)>midlt)
        cp(i)=R*(a1+(a2*T(i))+(a3*T(i)^2)+(a4*T(i)^3)+(a5*T(i)^4));
    else
        cp(i)=R*(a8+(a9*T(i))+(a10*T(i)^2)+(a11*T(i)^3)+(a12*T(i)^4));

    end
end
end


%% function for enthalapy%%
function [ h ] =enthalapy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,midlt,T,R)
for i=1:100
    if(T(i)>midlt)
        h(i)=(a1+a2*T(i)/2+a3*T(i)^2/3+a4*T(i)^3/4+a5*T(i)^4/5+a6/T(i))*(T(i)*R);
    else
        h(i)=(a8+a9*T(i)/2+a10*T(i)^2/3+a11*T(i)^3/4+a12*T(i)^4/5+a13/T(i))*(T(i)*R);
    end
end
end

%% function for entropy %%

function [ s ]=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,midlt,T,R)
for i=1:100
    if (T(i)>midlt)
        s(i)=(a1*log(T(i))+a2*T(i)+a3*T(i)^2/2+a4*T(i)^3/3+a5*T(i)^4/4+a7)*R;
    else
        s(i)=(a8*log(T(i))+a9*T(i)+a10*T(i)^2/2+a11*T(i)^3/3+a12*T(i)^4/4+a14)*R;
    end
end
end

%% function for molecular weight%%

function [mw]=mol_weight(data)
element=['N','H','C','O','A'];
atm_wt=[14,1,12,16,40];
mw=0;
for i=1:length(data)
    for j=1:length(element)
        if  strcmp(data(i),element(j))
            mw=atm_wt(j)+mw;
            pos=j;
        end
    end
    k=str2num(data(i));
    if k>1
        mw=mw+atm_wt(pos)*(k-1);
    end
end
fprintf('%s species %s %f\n',data,'mol_mass',mw);
end

EXPLANATION:

To develop the code to parse the NASA thermodynamic data file to calculate thermodynamic properties of various gases and obtain plot along with moleculaer mass.

parsing:means reading the data and fecilitating some transformations on data. it splits a file and input into pecies of data can be easily stored and manipulated.

step1:

=>reading the data from the file
temp=fopen('THERMO.dat','r');=> fopen is uesd to open file or obtain information about file in this NASA thermodynamic file THERMO.dat file is used given in challange
fgetl(temp);=> read line from files we use first fgetl command it read first line of the file second time use second line of the values
temp_data=fgetl(temp);=> we assume file as temp_data
A=strsplit(temp_data,' ');=>split string or character vector

step2:extract global temperature range using str2num it converts character array or string to numeric array
low=str2num(A{2});
mid=str2num(A{3});
high=str2num(A{4});

step3:these are used for skip the comments lines of the NASA file

for j=1:3
fgetl(temp);
end

step4:

for i=1:53 by using for loop we find values for 53 species of data

data=fgetl(temp);
a=strsplit(data,' ');
b=a{1};

%extract coefficients of desired molecule using these code

=>in nasa file given in challange we extract coefficients from the equation as per formula we assume line1 for given file for species and findstr gives values for 'E' and write same as remaining coefficiens also.

line1=fgetl(temp);
op1=findstr(line1,'E');
coef1=line1(1:op1(1)+3);
coef2=line1(op1(1)+4:op1(2)+3);
coef3=line1(op1(2)+4:op1(3)+3);
coef4=line1(op1(3)+4:op1(4)+3);
coef5=line1(op1(4)+4:op1(5)+3);
a1=str2num(coef1);
a2=str2num(coef2);
a3=str2num(coef3);
a4=str2num(coef4);
a5=str2num(coef5);

step5:

=>extract local temperature by using str2double command converts string to double precision values
mlt=a(end-1);
midlt=str2double(mlt)
hlt=a(end-2);
highlt=str2double(hlt)
llt=a(end-3)
lowlt=str2double(llt)

step6:we written a functin code for specific heat .in this function we take 14 coefficients these divided into first 7 are high temperature coefficients and last 7 are low temperature coefficients by using if and else condtions

plot specific heat v T and linspace command is used for create 100 values between lowlt to highlt here R is ideal gas constant

T=linspace(lowlt,highlt,100);
R=8.314;
[ cp ]=specific_heat(R,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,midlt,T);
figure(1)
plot(T,cp,'linewidth',3,'color','g')
xlabel('temperature')
ylabel('specificheat')
title(b)

same as for enthalapy and entropy by using functions we found coefficients

step7:

to calculate molecular mass by using function code in this strcmp command used to compare strings for different species and fprintf used to write a data to textfile

\n=> open a new line

%f=>format specifier -floating point number

step8: mkdir is used to create a directory to store the outputs and cd(changing directory) means change current folder create subfolders in folder

and saveas command used for save figures in specific file

mkdir(b)
cd (b)
saveas(1,'specificheat','jpg')
saveas(2,'enthalapy','jpg')
saveas(3,'entropy','jpg')
cd ..

[mw]=mol_weight(b);
end
fclose(temp);=> close one or all open files

all folders

molecular mass for different gas species shown in command window:

ERRORS: i for got T(i) values in function shown in command window line79

here i assume [s] for entropy function but i use h(i) in that function code

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.