Project

NASA Thermodynamics Data Parsing

Parsing NASA thermodynamic data using MATLAB. Using this data to Evaluate cp, Enthalpy,Entropy of the different elements form the data file. Then creating useful plots of cp, Enthalpy & Entropy. At last saving this useful data in resp. folder.

%Below is the main pogram in MATLAB for Parsing NASA data

close all
clear all
clc

%parsing the NASA data

f1 = fopen(\'THERMO.dat\',\'r\');
TITLE = fgetl(f1);
temp_value = fgetl(f1);
temp = strsplit(temp_value,\' \');
global_low_temp = str2num(temp{2});
global_mid_temp = str2num(temp{3});
global_high_temp = str2num(temp{4});

fgetl(f1);
fgetl(f1);
fgetl(f1);
mkdir(\'Therm data\')

for i=1:53
    compound = fgetl(f1);
    name = compound(1:6)
    folder = pwd
    cd(\'Therm data\')
    status = mkdir(name) 
    cd(folder)
    values = fgetl(f1);
    a = findstr(values,\'E\');
    a1 = values(1:a(1)+3);  
    a1 = str2num(a1);
    A1 = a1;
    a2 = values(a(1)+4:a(2)+3);
    a2 = str2num(a2);
    A2 = a2;
    a3 = values(a(2)+4:a(3)+3);
    a3 = str2num(a3);
    A3 = a3;
    a4 = values(a(3)+4:a(4)+3);
    a4 = str2num(a4);
    A4 = a4;
    a5 = values(a(4)+4:a(5)+3);
    a5 = str2num(a5);
    A5 = a5;
    values2 = fgetl(f1);
    a6 = values2(1:a(1)+3);
    a6 = str2num(a6);
    A6 = a6;
    a7 = values2(a(1)+4:a(2)+3);
    a7 = str2num(a7);
    A7 = a7;
    a8 = values2(a(2)+4:a(3)+3);
    a8 = str2num(a8);
    A8 = a8;
    a9 = values2(a(3)+4:a(4)+3);
    a9 = str2num(a9);
    A9 = a9;
    a10 = values2(a(4)+4:a(5)+3);
    a10 = str2num(a10);
    A10 = a10;
    values3 = fgetl(f1);
    a11 = values3(1:a(1)+3);
    a11 = str2num(a11);
    A11 = a11;
    a12 = values3(a(1)+4:a(2)+3);
    a12 = str2num(a12);
    A12 = a12;
    a13 = values3(a(2)+4:a(3)+3);
    a13 = str2num(a13);
    A13 = a13;
    a14 = values3(a(3)+4:a(4)+3);
    a14 = str2num(a14);
    A14 = a14;
  
   %Function for obtaining the values of cp,h & s and then creating the plots  
  sub_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,name) 
   %Molecular Weight Function call
  m_weight = mole_mass(name)  
 
  
  %saving the plots and mole weight data
folder = pwd;
 cd(\'Therm data\');
 cd(name);
 f2 = fopen(\'Molecular Weight.txt\',\'w\');
  fprintf(f2,\'Element %s\\n\',name);
  fprintf(f2,\'Molecular Weight is %3.2f\',m_weight);
  fclose(f2);
  saveas(1,\'Specific heat.jpg\')
   saveas(2,\'Enthalpy.jpg\')
   saveas(3,\'Entropy.jpg\')
   cd(folder)
end

    


% Sub programs

%Function to Generate the values of cp,h,s and plot the resp. values
function [cp enthalpy entropy] = sub_func(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,name)
%Universal gas Constant
R = 8.314;  
%Temperature range
t = linspace(1,5000,50)

for i=1:length(t)
    if t(i)>2500
        
        cp = (a1 + a2.*t + a3.*t.^2 + a4.*t.^3 + a5.*t.^4).*R;
        entalpy = (a1 + a2.*(t./2) + a3.*(t.^2./3) + a4.*(t.^3./4) + a5.*(t.^4./5) + a6./t).*R.*t;
        entropy = (a1.*log(t) + a2.*t + a3.*(t.^2./2) + a4.*(t.^3./3) + a5.*(t.^4./4) + a7).*R;     

    else
        
         cp = (a8 + a9.*t + a10.*(t.^2) + a11.*(t.^3) + a12.*(t.^4)).*R;
        enthalpy = (a8 + a9.*(t./2) + a10.*(t.^2./3) + a11.*(t.^3./4) + a12.*(t.^4./5) + a13./t).*R.*t;
        entropy = (a8.*log(t) + a9.*t + a10.*(t.^2./2) + a11.*(t.^3./3) + a12.*(t.^4./4) + a14).*R;
        
        
    end
    
   
end
%Plotting

    figure(1)
    plot(t,cp,\'linewidth\',2,\'color\',\'r\')
    title(name)
    xlabel(\'Temperature\')
    ylabel(\'Specific Heat\')
    figure(2)
    plot(t,enthalpy,\'linewidth\',2,\'color\',\'g\')
    title(name)
    xlabel(\'Temperature\')
    ylabel(\'Enthalpy\')
    figure(3)
    plot(t,entropy,\'linewidth\',2,\'color\',\'b\')
    title(name)
    xlabel(\'Temperature\')
    ylabel(\'Entropy\')    
end
%Function to calculate the Molecular Weight 
function [m] = mole_mass(name)
%Different Molecular weight of Elements
atoms = \'OCHNR\';
aw = [16 12 1 14 39.9];
m = 0;


for i=1:length(name)
    for j=1:5
        if strcmp(name(i),atoms(j))
            m = m + aw(j);
          
        end
    end
end
for g=1:length(name)
n = str2num(name(g));
if n>1
    o = g-1;
    for b=1:5
        
    if strcmp(name(o),atoms(b))
        m = m + aw(b)*(n-1)
    end
    end
end
end

Report & The Required Plots:-

In this program, We have to read data from a file Thermo.dat which contains information reagarding temperature range and coefficient values for calculating value of cp(specific heat),h(Enthalpy) & s(Entropy).

First of all we will use fopen command to load the file and use fgetl command to read the data. now fgetl command reads the data line-wise. so using fgetl command once will read only first line.

So for parsing data, we will split the data into two portions namely header part and the species part.so we will use two different logic to read data from header and species part.

First we read header data by using fgetl command. we will skip which data set is not important and then save the temperature range data into a variable. using string split(strsplit) command we will spilt temperature range into a single temperature value and then saving this individual values into variables.

For species part of program, we read the data by fgetl and for first line we will save first six string values into a variable(called \"name\") and move to second line. Now second line contains variables for calculating cp, h & s using NASA polynomials. so we will use find string(findstr) command to find the position from which the values can be seperated. this how we will seperate the seven coefficient from first line and assign this values to resp. variables and we will do same for the next line and next to next line. Now if we see the species data set follows a particular order. so to read the furthur data which follows the above patern we will repeat this whole process again and again. we will for loop for this iteration that will repeat this process.

For calculating the cp, h & s value, we will use NASA ploynomials. we will define value of R and set Temperature range to variable T using linspace. we have to use first seven coeff. for high temp. and second seven coeff. for low temp. For doing this we will use if else command that if temp>mid temp use first seven coeffi. else use second seven coeffi. also we will use for command to run the iterations for T values of time i.e for every value of T the iteration will run once.

This how we get cp, h & s, now we will plot this values w.r.t temperature(T) and using plot command we will plot it. now will give name to this plots automatically resp. to element of which this data belongs to. In this case we already have a variable which has saved the name of element in the form of string(i.e. name). we will use title command and will assign the value of the string containing that name to title.

For calculating the Molecular Weight we will use that string that contains name of the element(i.e. name). Now we will generate two varaibles which will saves Element Alphabets and another variable which will save its Atomic weight resp. in same order. Using string Compare (strcmp) command to compare the values between parsed variable containing name (i.e. name) and this saved variable. if the string compares then it will add the atomic weight from the variable which contains weight values. If there is a multiplier in element name, we have also used str2num command which will covert string to number. for that we will read the values of variable(\"name\") and convert to number. if number>1 then we will multiply the weight of that pre element to the final weight.

Finally to save the plots and Molecular weight file into the respective folder,we will use current folder(pwd) and change folder(cd) command to save folder at necessary location. Then we will gereate the folder automatically using make directory(mkdir) command (i.e variable_name =mkdir(name)) here name is a variable which contains the name of the folder. using fopen write and fprintf to save molecular weight into a text file and saveas command to save the plots generated of cp,h & s. we will keep all this command in between cd & pwd such that we can save the data into requied location.

we will repeat this whole main program to save data for another element. Repeating this process number of time will save consecutive data range. So we use for loop for repeating this main program for number of iterations.

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.