Challenge

Project 1 - Parsing NASA thermodynamic data

Theory:

file parsing: this is a methode that mainly deals with using data files to extract and performe various operations within the data of the file.

In this exercise we will be using the given nasa data file to extract and read the data , split the data into multiple segments and convert the string data to number. using these we qxicute the following code to run the calculations and plot the graphs.

thus from the given nasa data file we extract the coefficients and then use them to find entropy , enthalpy , and specific heat and plot them with the variations in temperature.

for this we use for loop to run and extract all the given data .

Calculations:

As we understand that the first 7 coefficients are for the lower temperature range and the next 7 are for the heigher temperature range we split the coefficients and usinng an if else condition we define the formulaes that are to be used. the formulaes are defined within the condition.

Directory creation:

using the mkdir we can assign a directory for the images and outputs to be stored. then using the pre defined functions mkdir cd, and saveas command we can make and save the outputs in seperate folders .

Molecular_mass fun:

As required by the problem the function when called saves the species name and the executes the loop which takes the atomic weight and then using the species name and value compares the data and executes the statements to calculate the values

clc
clear 
close all


File prasing NASA DATA.
% first we use fopen in read mode to read the data file and save it in a
% variable f1

tic

f1= fopen('NASA.dat','r');

% fgetl returns the first line and there is no exite code for the command.
% Thus , every time the code is used it moves to the next line

l1 = fgetl(f1);          % this is the first line which is not useful for the code. The header file
global_temp = fgetl(f1); % gets and saves the values of gloable temperature.
Gloabal = strsplit(global_temp);  % splits the strings forming a 1x4 array ; 

% displaying the values of gloable temperatures;

disp('gloable temperature 1');g1 = str2double(Gloabal{2})
disp('gloable temperature 2');g2 = str2double(Gloabal{3})
disp('gloable temperature 3');g3 = str2double(Gloabal{4})


% the below for loop is bsically to omit the next 3 lines of in the data file f1.
for i = 3:5
    fgetl(f1);
    
    % basically we have to use for loop three timesl=1:3 for reading the
    % next 3 header lines. but for understanding and simplification code we are
    % using notation l=3:5 instead of l=1:3 saying that lines 3 to 5 are
    % omitted
    
end
  

Name of new species

for i = 1:53
    l6 = fgetl(f1);
    
    % this is done 53 times because there are 53 species present in file.
    
    B = strsplit(l6,' '); %strsplit means string splits dividing the values of the string that are spaced 
    species = B(1);       % it is a molecule species
    % we will seperate here local temprature ranges
    
    D = strfind(l6,'.'); %findstr means find string
    temp_range = l6 ((D(1)-3) : (D(1)+23));
    temperature = strsplit(temp_range,' ');
    
    disp('Name of species being exicuted is '); species
    local_low_temp = str2double(temperature{1});
    disp('lowest local temperature is '); temp_1 = local_low_temp
    %{} bracket is necessary here to call the value from the temperature
    %matrix. because this is the cell matrix,which mean it stores the value
    %with specific identity. identity meaning it can be string or float type
    %value wtc. other matrix store only single type identity for all values but
    %cell matrix can store different types also.
    
    local_mid_temp = str2double(temperature{3});
    disp('middle local temperature is '); temp_3 = local_mid_temp
    
    local_highest_temp = str2double(temperature{2});
    disp('heighest local temperature is '); temp_2 = local_highest_temp
    
    % the above code displays the 3 local temperature ranges for the molecules
    % and stores them 
    
    % line operation for the 1st line of coefficents within the molecular
    % species.
    

first 5 coefficients
    
    l7 = fgetl(f1); % this saves the first line of coefficients bearing 5 values to the l7 line 
    F = strfind(l7,'E'); % as we need to seperate the 5 values we find the string e positions and save it as a 1x5 matrix 
    
    first_coefficient_at_high_temp = l7(1:F(1)+3);
    % hear () is not nessary because F(1) is a position value not as a written
    % character string value asin above temperature (1)
    
    fcaht = str2double(first_coefficient_at_high_temp); % this converts the string value obtained to an number value that can be used for calculations 
    
    %fcaht mean first_coefficient_at_high_temperature. for different notation
    %and simplicity, i did like it % hear{} is also not nessary because
    %first_coefficient_at_high_temp. is not storing so many value as
    %temperature matrix was storing in above line.
    
    a1 = fcaht; % this stores the number value as a1 representing the first coefficient and on each loop it stores the values to the various molecular species.
    
    second_coefficient_at_high_temp = l7(F(1)+4:F(2)+3);
    scaht = str2double(second_coefficient_at_high_temp);
    %scaht mean second_coefficient_at_high_temp.
    
    a2 = scaht;
    
    third_coefficient_at_high_temp = l7(F(2)+4:F(3)+3);
    tcaht = str2double(third_coefficient_at_high_temp);
    a3 = tcaht;
    
    forth_coefficient_at_high_temp = l7(F(3)+4:F(4)+3);
    fourth_caht = str2double(forth_coefficient_at_high_temp);
    a4 = fourth_caht;
    
    fifth_coefficient_at_high_temp = l7(F(4)+4:F(5)+3);
    fifth_caht = str2double(fifth_coefficient_at_high_temp);
    a5 = fifth_caht;
    

Next 5 coefficients
    
    l8 = fgetl(f1);
    G = strfind(l8,'E');
    
    sixth_coefficient_at_high_temp = l8(1:G(1)+3);
    sixth_caht = str2double(sixth_coefficient_at_high_temp);
    a6 = sixth_caht;
    
    seventh_coefficient_at_high_temp = l8(G(1)+4:G(2)+3);
    seventh_caht = str2double(seventh_coefficient_at_high_temp);
    a7 = seventh_caht;
    
    eight_coefficient_at_high_temp = l8(G(2)+4:G(3)+3);
    eight_caht = str2double(eight_coefficient_at_high_temp);
    b1 = eight_caht;
    
    nine_coefficient_at_high_temp = l8(G(3)+4:G(4)+3);
    nine_caht = str2double(nine_coefficient_at_high_temp);
    b2 = nine_caht;
    
    ten_coefficient_at_high_temp = l8(G(4)+4:G(5)+3);
    ten_caht = str2double(ten_coefficient_at_high_temp);
    b3 = ten_caht;
    

last 4 coefficients
    
    l9 = fgetl(f1);
    K = strfind(l9,'E');
    
    l1_coefficient_at_low_temp = l9(1:K(1)+3);
    l1_caht = str2double(l1_coefficient_at_low_temp);
    b4 = l1_caht;
    
    l2_coefficient_at_low_temp = l9(K(1)+4:K(2)+3);
    l2_caht = str2double(l2_coefficient_at_low_temp);
    b5 = l2_caht;
    
    l3_coefficient_at_low_temp = l9(K(2)+4:K(3)+3);
    l3_caht = str2double(l3_coefficient_at_low_temp);
    b6 = l3_caht;
    
    l4_coefficient_at_low_temp = l9(K(3)+4:K(4)+3);
    l4_caht = str2double(l4_coefficient_at_low_temp);
    b7 = l4_caht;
    
    % Togeather 14 values of coefficients have been extracted first 7 being
    % stored as 'a' belonging to the heigher temp values and the next 7 as 'b'
    % which are towards the lower temperature.
         
    molecule_name = char(species); % this extracts the molecular species name 
    mkdir(['F:\nasa\species\',molecule_name]); % this makes directory to save the molecules species.


Calculations

    temp = linspace(temp_1,temp_2,20); % this generates multiple points between the temperature ranges extracted form the local temp variables in the file.
    R = 8.314; % this is just a constant value used in calculations

    L = 1:length(temp);

    for j = 1:length(L)
         if temp(L) <= 1000
        
        Cp = R*(b1+b2*(temp(L))+b3*(temp(L)).^2+b4*(temp(L)).^3+b5*(temp(L)).^4);
        
        H = (R*temp(L)).*(b1+b2*(temp(L)./2)+b3*((temp(L)).^2/3)+b4*((temp(L)).^3/4)+b5*((temp(L)).^4/5)+b6./temp(L));
        
        S = R*(b1*log(temp(L))+b2*(temp(L))+b3*((temp(L)).^2/2)+b4*((temp(L)).^3/3)+b5*((temp(L)).^4/4)+b7);
        
         else
        Cp = R*(a1+a2*(temp(L))+a3*(temp(L)).^2+a4*(temp(L)).^3+a5*(temp(L)).^4);
        
        H = (R*temp(L)).*(a1+a2*(temp(L)./2)+a3*((temp(L)).^2/3)+a4*((temp(L)).^3/4)+a5*((temp(L)).^4/5)+a6./temp(L));
        
        S = R*(a1*log(temp(L))+a2*(temp(L))+a3*((temp(L)).^2/2)+a4*((temp(L)).^3/3)+a5*((temp(L)).^4/4)+a7);
         end
    end
    % Note: to increase the complexity it can be further devided to the middle
    % values.

Plotting the graphs:

        figure(i);
        plot(temp,H,'-*',"LineWidth",1,'Color','g');
        title(molecule_name);
        xlabel('temperature');
        ylabel('Eanthalpy');

        figure(i+54);
        plot(temp,Cp,'-*',"LineWidth",1,'Color','r');
        title(molecule_name);
        xlabel('temperature');
        ylabel('Specific heat');

        figure(i+109);
        plot(temp,S,'-*',"LineWidth",1,'Color','r');
        title(molecule_name);
        xlabel('temperature');
        ylabel('Entropy');

        cd(['F:\nasa\species\',molecule_name])
        
        saveas(i,'eanthaply.jpg');
        saveas(i+54,'specific heat.jpg');
        saveas(i+109,'entropy.jpg');
        % in the above figures the i for loop is used to change the number
        % of figure so that it can be generated and displayed in the
        % outputs as the first 53 values will be taken the enthalpy plots
        % i+54 is used in the next figure values to generate the specific
        % heat plots and i + 108 is used to generate the next series of
        % plots for entropy.
        
        cd(['F:\nasa\'])
        % changes directory back to the orginal as the molecular mass function is present in orginal dir.
        % and on each loop the program repets the loop being in the species
        % molecule name file. causing an error 
        
        weight(i)=molecular_mass(molecule_name); % calls the function to calculate and display the molecular weights 
   
end
cd(['F:\nasa\']) % this , at the end of the code changes the directory back to the orginal path. this avoides any errors that may occure as the code is being changed and run again 
time = toc

fclose(f1);





Molecular mass function:
function [M] = molecular_mass(molecule_name)

fprintf('molecular weight of ');
fprintf(molecule_name);
fprintf(' : ');
atoms = ['h','C','N',"O",'R'];

% the above are the common atoms that are used in all the species:
atom_weight = [1 12 15 16 40];
M=0;
%their atomic weight is as follows and M is the initial condition wea are
%defining.

for i = 1:length(molecule_name)
    for j=1:length(atoms)
        if strcmpi(molecule_name(i),atoms(j))
        %strcmpi means string comparison this is used to check the element.
        M=M+atom_weight(j);
        position=j;
        % we store the corrected molecules matched with the element
        % so the value of n will be same as number of value present
        
        end
    end
    n = str2double(molecule_name(i));
    %in this process the increamented molecule value is used and saved to
    %n. this is converted to a number file : thus the string position
    %values bearing any number is saved.
    
    if n> 1
        M = M+atom_weight(position)*(n-1);
        %here we are taking (n-1) in multiply instead of n because we are
        %inputs second time coding M=M+atom_weight. so by this two times
        %mass add so to over come this we are reducing n to n-1 time
         
    end
end

fprintf('%d',M);%d is for digit

disp(" ")
% it is not necessary command disp which refers to display, space (' ').but
% it for second time running the program because it seperates the molecular
% weight statement to additional matlab warning statements like directory
% is alrady exist.

end

Errors:

All 3 errors described hear mainly occured during the directory creatin :

besides these errors small mistakes like typos and wrongful use of brackets (){}[] was done which was easily realised and fixed.

1) The first error and the 3rs error both occured due to missing '\' and changing the directory . causing the running code not to be found.

2) the warning that was generated where the directory was being created on a loop causing the code to run slower

3) other warnings were all alos fixed to reduce the time of operation, like pre allocating the values, changing str2num to str2double, findstr to strfind etc.

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.