Challenge

Project 1 - Parsing NASA thermodynamic data

clear all
close all
clc

%opening the file
f1 = fopen('THERMO.dat','r')
fgetl(f1);

%inserting the file in tline with the 2,2 matrix
tline = fscanf(f1,'%f',[2 2]);

% dividing the the temp

global_low_temp = tline(1,1)
global_mid_temp = tline(1,2)
global_high_temp = tline(2,1)

%for skipping 3 lines

for i=1:3
    fgetl(f1);
end

%for 53 species for loop had to run 53 times

for i = 1:53
    line1 = fgetl(f1);
    l = strsplit(line1,' ');
    %m = length(l);
    species = l{(1)};
    posdot = strfind(line1,'.')

    % taking local temperatures

    lowtemp = line1(posdot(1)-3:posdot(1)+3);
    lowtemp = str2num(lowtemp);
    midtemp = line1(posdot(2)-4:posdot(2)+4);
    midtemp = str2num(midtemp);
    hightemp = line1(posdot(3)-4:posdot(3)+4);
    hightemp = str2num(hightemp);

    line2=fgetl(f1);
    posE =strfind(line2,'E');
  %now getting high temperature coefficients
  %storing them
    a1 = line2(1:posE(1)+3);
    al = str2num(a1);
    a1 = str2num(a1);

    a2 = line2(posE(1)+3:posE(2)+3);
    a2 = str2num(a2);

    a3 = line2(posE(2)+5:posE(3)+3)
    a3 = str2num(a3);

    a4 = line2(posE(3)+4:posE(4)+3);
    a4 = str2num(a4);

    a5 = line2(posE(4)+4:posE(5)+3);
    a5 = str2num(a5);

    line3 = fgetl(f1);
    posE1 =strfind(line3,'E');

    a6 = line3(1:posE1(1)+3);
    a6 = str2num(a6);

    a7 = line3(posE1(1)+4:posE1(2)+3);
    a7 = str2num(a7);
    
    a8 = line3(posE1(2)+4:posE1(3)+3);
    a8 = str2num(a8);

    a9 = line3(posE1(3)+4:posE1(4)+3);
    a9 = str2num(a9);

    a10 = line3(posE1(4)+4:posE1(5)+3);
    a10 = str2num(a10);

    line4 = fgetl(f1);
    posE2 =strfind(line4,'E');

    a11 = line4(1:posE2(1)+3);
    a11 = str2num(a11);

    a12 = line4(posE2(1)+4:posE2(2)+3);
    a12 = str2num(a12);

    a13 = line4(posE2(2)+4:posE2(3)+3);
    a13 = str2num(a13);

    a14 = line4(posE2(3)+4:posE2(4)+3);
    a14 = str2num(a14);

    % coefficients applying in the formula
    t= linspace(lowtemp,hightemp,500);
    R = 8.314;

    for i= 1:length(t)
        if t(i)<=midtemp
           
         cP(i) = (a8+a9.*t(i)+a10.*t(i).^2+a11.*t(i).^3+a12.*t(i).^4)*R;

         H(i) = (a8+a9*t(i)/2+a10*t(i).^2/3+a11*t(i).^3/4+a12*t(i).^4/4+a13./t(i))*R.*t(i);

         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;

        else %t <=midtemp
            
            cP(i) = (a1+(a2*t(i))+(a3*(t(i)).^2)+(a4*(t(i)).^3)+(a5*(t(i)).^4)).*R;

            H(i) = (a1+a2*t(i)/2+a3*t(i).^2/3+a4*t(i).^3/4+a5*t(i).^4/4+a6./t(i))*R.*t(i);

            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;
        end
    end

    % saving folder  
    cd('F:\all softwares\matlab\nasa');
    mkdir(species);
    cd(species);

    figure(1);
    plot(t,cP,'LineWidth',3,'Color','r');
    xlabel('TEMPERATURE[K]');
    ylabel('SPECIFIC HEAT [KJ/MOL-K]');
    title('TEMPERATURE VS SPECIFIC HEAT');
    grid on
    saveas(figure(1),'cp.jpg');


    figure(2);
    plot(t,H,'LineWidth',3,'Color','b');
    xlabel('TEMPERATURE[K]');
    ylabel('ENTHALPY[KJ/KG]');
    title('TEMPERATURE VS ENTHALPY');
    grid on
    saveas(figure(2),'H.jpg');

    figure(3);
    plot(t,S,'LineWidth',3,'Color','b');
    xlabel('TEMPERATURE[K]');
    ylabel('ENTROPY[KJ/K]');
    title('TEMPERATURE VS ENTROPY');
    grid on
    saveas(figure(3),'S.jpg');
    
    cd('F:\all softwares\matlab\nasa')

    % findinf molecular weight

    atomicname = ['H','C','O','N','AR'];
    atomicweight = [1, 12, 16, 14, 40];
    W = 0;

    for i = length(species)
        for j = length(atomicname)
            if species(i) == atomicname(j)
                W = W+atomicweight;
                position = j;
            end
        end
        n = str2num(species(i));
        if n>=1
                W = W+atomicweight(position)*(n-1);
        end
        fprintf('molecularweight of %s = %f',species,W)
        disp(' ')
    end



end
fclose(f1)

EXPLANATION_

-opening the file and stored in f1.
- inserting the file in tline with the 2,2 matrix
- dividing the the temperature.
-using for llop to skipp 3 lines.
-for 53 species for loop had to run 53 times
-taking local temperatures
-now getting high temperature coefficients
- and storing them
- coefficients applying in the formula
-saving folder in separate
-plotting the figures in separate automatically and giving labels,titles,colors.
-saving them in respective directory.
-finally calculating the molecular weight using for loop and conditional statememt.

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.