Challenge

Project 1 - Parsing NASA thermodynamic data

%Objective : To parse the NASA data including coefficients, local temperature ranges, species names. 

%Mathematical Equations of specific heat, enthalpy, entropy are given in the question. First 7 coefficiemts are for calculating parameters for high temperature and next 7 are for low temperature. 

%Parsing data is the process of reading from a file and writing in a file. 

% Below is the program for calculating thermodynamic properties. 

%% thermo.dat file

% Opening of thermo.dat file
f2 = fopen('THERMO.DAT','r')        

%input R
R = 8.314

% Getting first two lines
tline_1 = fgetl(f2)
tline_2 = fgetl(f2)
temp = strsplit(tline_2,' ')
global_min_temp = str2num(temp{2})
global_med_temp = str2num(temp{3})
global_max_temp = str2num(temp{4})

% Skipping next three lines
for i = 1:3
    skipped_lines= fgetl(f2)
end

for i =1:inf
    
    line_3 = fgetl(f2)
    if strcmp(line_3, 'END')                % check if the end of the file is reached or not...
        disp('completed successfully...')
        break                               % if yes....stop reading the file...
    end
    
    S= strsplit(line_3, ' ');
    species_name= S{1}      % use {} ...S is a cell array.
    
    %     A= strfind(line_3, 'G')
    %     temp_values= line_3(A+4:length(line_3))
    %     B = strsplit(temp_values, ' ')
    %
    local_min_temp = str2num(S{end - 3})        %  A slightly better way
    local_med_temp = str2num(S{end - 1})
    local_max_temp = str2num(S{end - 2})
    
    line_4 = fgetl(f2)
    r = strfind(line_4,'E')
    a1 = str2num(line_4(1:(r(1))+3));
    a2 = str2num(line_4(r(1)+4:r(2)+3));
    a3 = str2num(line_4(r(2)+4:r(3)+3));
    a4 = str2num(line_4(r(3)+4:r(4)+3));
    a5 = str2num(line_4(r(4)+4:r(5)+3));
    
    line_5 = fgetl(f2)
    a6 = str2num(line_4(1:(r(1))+3));
    a7 = str2num(line_4(r(1)+4:r(2)+3));
    a8 = str2num(line_4(r(2)+4:r(3)+3));
    a9 = str2num(line_4(r(3)+4:r(4)+3));
    a10 = str2num(line_4(r(4)+4:r(5)+3));
    
    line_6 = fgetl(f2)
    
    a11 = str2num(line_4(1:(r(1))+3));
    a12 = str2num(line_4(r(1)+4:r(2)+3));
    a13 = str2num(line_4(r(2)+4:r(3)+3));
    a14 = str2num(line_4(r(3)+4:r(4)+3));
    
    T = linspace(local_min_temp, local_max_temp, 100)
    
    c_p = specificheat_fp(a1, a2, a3, a4, a5,a6, a7, a8,a9,a10,a11,a12, a13, a14,T, R,local_min_temp, local_med_temp, local_max_temp);
    
    h = enthalpy_fp(R, T, a1, a2, a3, a4, a5,a6, a7, a8,a9,a10,a11,a12, a13, a14,local_min_temp, local_med_temp, local_max_temp );
    
    s = entropy_fp(R, T, a1, a2, a3, a4, a5,a6, a7, a8,a9,a10,a11,a12, a13, a14,local_min_temp, local_med_temp, local_max_temp);
    
    mkdir(species_name);
    cd(species_name)
    req_species = ['O', 'H', 'N2'];
    
    % use a seperate function to calculate the
    % molecular weight...send the species name
    % and return the weight.
    
    
    
    figure(1)
    plot(T,c_p)
    title('Speciefic heat vs temperature')
    xlabel('Temperature')
    ylabel('Specific heat')
    saveas(1,'specific_heat.jpg')
    
    
    figure(2)
    plot(T,h)
    title('Enthalpy vs temperature')
    xlabel('Temperature')
    ylabel('Enthalpy')
    saveas(2,'enthalpy.jpg')
    
    figure(3)
    plot(T,s)
    title('Entropy vs temperature')
    xlabel('Temperature')
    ylabel('Entropy')
    saveas(3,'entropy.jpg')
    
    
    cd ..
    
    M= molecularwt_fp(species_name);
    K= sprintf('%s mol_wt=%f\n',species_name, M);
    disp(K)
    
    %  mkdir(species_name) ..... to create a folder with the species name
    %  cd(species_name) ......to change the folder
    %  saveas(1,'specific_heat.jpg').....for saving the images
    %  siimilarly for the other plots
    %  cd ..            for going back to parent directory
    
    
end

% function program for c_p 
function c_p = specificheat_fp (a1, a2, a3, a4, a5,a6, a7, a8,a9,a10,a11,a12, a13, a14,T, R,local_min_temp, local_med_temp, local_max_temp)

if T > local_med_temp
    c_p = (a1 + a2*T + a3* T.^2 + a4* T.^3 + a5* T.^4)*R ;
else
    c_p = (a8 + a9*T + a10*T.^2 + a11*T.^3 + a12* T.^4)*R ;
end 

%function program for h
function [h] = enthalpy_fp(R, T, a1, a2, a3, a4, a5,a6, a7, a8,a9,a10,a11,a12, a13, a14,local_min_temp, local_med_temp, local_max_temp )

if T>local_med_temp 
    h = (a1 + ((a2 .*T)/2) + (a3* (T.^2)/3 )+ ((a4.* T.^3)/4) + ((a5.* T.^4)/5) + (a6./T))*R.*T; 
    
else
    h = (a8 + ((a9 .*T)/2) + (a10* (T.^2)/3 )+ ((a11.* T.^3)/4) + ((a12.* T.^4)/5) + (a13./T))*R.*T;
end 


% function program for s
function [s] = entropy_fp(R, T, a1, a2, a3, a4, a5,a6, a7, a8,a9,a10,a11,a12, a13, a14,local_min_temp, local_med_temp, local_max_temp)

if T>local_med_temp 
    
    s= ((a1* log(T))+ (a2.* T)+ ((a3*T.^2)/2)+ ((a4*T.^3)/3)+ ((a5*T.^4)/4)+a7)*R ;
    
else
     s= ((a8* log(T))+ (a9.* T)+ ((a10*T.^2)/2)+ ((a11*T.^3)/3)+ ((a12*T.^4)/4)+a14)*R ;
end 

% Program for MW
function [M]= molecularwt_fp(species_name)

atomic_mass = [16, 1, 12, 14, 40]
elements = ['O','H','C','N','A']

mol_wt = 0
for i= 1:length(species_name)
    for j=1:length(elements)
        if strcmpi(species_name(i),elements(j))
            mol_wt= mol_wt+atomic_mass(j)
            z=j 
        end 
    end 
    t= str2double(species_name(i));
    if (t>1)
        mol_wt = mol_wt +(atomic_mass(z)*(t-1));
    end 
    M =mol_wt ;
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.