Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing NASA thermodynamic data

Introduction:

Here ,our one and only Aim is to STUDY file parsing which is also commonly known as file handling in programmig languages such as C,Java ,python and etc.

in this,we study some commands which are very Useful for our aim fullfilement like fopen('name_of_file.txt',mode of opening) ,it loads all the content in file as string into buffer memory pointer by file pointer.thats,after completing our work we need to clean the buffer memory so we use fclose(pointer) to close the file.

fgetl(pionter) access each lineat a time.strfind('variable to be find') to find particulate in file.str2num to convertstring to integer

Also,here we study sum commands like cd,mkdir,pwd.

cd: It is to go into directory.

mkdir:it is use to create newfile in the present directory.

pwd:denotes address of present directory.

here the file we loaded is Nasa thermodynamic data which contain diffrent elements coefficient at maximum and minimum temprature.

steps involved :

  • declaring universal gas constant i.e R= 8.314
  • first,we loaded the content of thermo.dat into buffer pointed by a pointer f1.
  • acessing heading of the file.(i.e Thermo)
  • from second line taking high,low,mid temprature and converting it into integer
  • skipping skipping starting 4lines i.e comments.
  • introducing a for loop to access all 52 elements at a time.
  • taking coefficient of each elements .(i.e first 7 for high temp rest for low temp)
  • creating and temprature array of 500 elements between 300 K and 5000 K.
  • for each temprature calculating enthalpy ,entropy and specific heat.under condition for if temp is smaller than 1000 then coeff of low temp will given to the given formalae.else,high temp coefficient will be considerd.
  • after evaluating sp. heat ,ethalpy and entropy, seperate graph will be plotted between these charachterics Vs temp.
  • after that making new directory with name of scpecies in current path.
  • saving obtained plots into that directory.
  • calculating molecular mass of species by standard formula.
  • same process goes at each loop for diffrent species.
  • and finally we get all outputs of each species into their rescpective directory.
  1. main code of parsing data
clear all
close all
clc
%declaring universal gas constant
R=8.314
%onpening Thermo file in file pointer
 f1= fopen('THERMO.dat','r')
 %inputing data line by line
 head=fgetl(f1)%saving heading of file into head variable
l1= fgetl(f1)%loading 2nd linewhich contain temprature range
 a=strsplit(l1,' ');%split string into word by word
 globallowtemp=str2num(a{1})
 globalmidtemp=str2num(a{2})
 globalhightemp=str2num(a{3})
 %skipping skipping starting  3 lines i.e comments
 for i =1:3
   fgetl(f1)
 endfor
 for i=1:52 %loop for accesing every species data
 line_name=fgetl(f1) %accesing name ofspecies
 a=strsplit(line_name,' ');
 species=a(1)
l2=fgetl(f1)
tp=strfind(l2,'E')%converting string into integer
%acccesesing all coeffienct of  each spececies
cp1=str2num(l2(1:tp(1)+3))
cp2=str2num(l2(tp(1)+4:tp(2)+3))
cp3=str2num(l2(tp(2)+4:tp(3)+3))
cp4=str2num(l2(tp(3)+4:tp(4)+3))
cp5=str2num(l2(tp(4)+4:tp(5)+3))

l3 = fgetl(f1);
tp2 = strfind(l3,'E');
cp6 = str2num(l3(1:(tp2(1)+3)));
cp7 = str2num(l3((tp2(1)+4):(tp2(2)+3)));
cp8 = str2num(l3((tp2(2)+4):(tp2(3)+3)));
cp9 = str2num(l3((tp2(3)+4):(tp2(4)+3)));
cp10 = str2num(l3((tp2(4)+4):(tp2(5)+3)));


l4 = fgetl(f1);
tp3 = strfind(l4,'E');
cp11 = str2num(l4(1:(tp3(1)+3)));
cp12 = str2num(l4((tp3(1)+4):(tp3(2)+3)));
cp13 = str2num(l4((tp3(2)+4):(tp3(3)+3)));
cp14 = str2num(l4((tp3(3)+4):(tp3(4)+3)));   
%saving first 7 coefficient into high temp. coef and rest into low temp coefficient
htcoef=[cp1 cp2 cp3 cp4 cp5 cp6 cp7]; 
ltcoef=[cp8 cp9 cp10 cp11 cp12 cp13 cp14];
T=linspace(globallowtemp,globalhightemp,500);
for j=1:length(T);%loop for each  temprature
 if T(j)<=globalmidtemp;
 % calculating enthalpy,entropy,sp. heat      
        specific_heat(j) = R*(ltcoef(1)+ltcoef(2)*T(j)+ltcoef(3)*(T(j))^2 +ltcoef(4)*(T(j))^3 +ltcoef(5)*(T(j))^4);
         enthalpy(j) = R*T(j)*(ltcoef(1)+ltcoef(2)*T(j)/2 +ltcoef(3)*((T(j))^2)/3 +ltcoef(4)*((T(j))^3)/4 + ltcoef(5)*((T(5))^4)/5 + ltcoef(6)/T(j));
         entropy(j) = R*(ltcoef(1)*2.303*log(T(j))+ltcoef(2)*T(j)+ltcoef(3)*((T(j))^2)/2 +ltcoef(4)*((T(j))^3)/3 + ltcoef(5)*((T(j))^4)/4 + ltcoef(7));
         
    else
        specific_heat(j) = R*(htcoef(1)+htcoef(2)*T(j)+htcoef(3)*(T(j))^2 +htcoef(4)*(T(j))^3 +htcoef(5)*(T(j))^4);
         enthalpy(j) = R*T(j)*(htcoef(1)+htcoef(2)*T(j)/2 +htcoef(3)*((T(j))^2)/3 +htcoef(4)*((T(j))^3)/4 + htcoef(5)*((T(5))^4)/5 + htcoef(6)/T(j));
         entropy(j) = R*(htcoef(1)*2.303*log(T(j))+htcoef(2)*T(j)+htcoef(3)*((T(j))^2)/2 +htcoef(4)*((T(j))^3)/3 + htcoef(5)*((T(j))^4)/4 + htcoef(7));
        
    
 end
 end
spec=char(species)
 
 fd = sprintf('%s',spec);
 cur_dir=pwd %saving current directry into cur_dir
mkdir(spec); %making new directory in current directorywith name of species
    cd(spec);  %entring into new director of species name

%plotting graph of enthalpy,entropy ,sp. heat Vs temp variation
figure(1)
plot(specific_heat,T,'linewidth',2);
xlabel('temprature(K)-->')
ylabel('specific heat(-->)')
title('specific heat Vs teprature')
figure(2)
plot(enthalpy,T,'linewidth',2);
xlabel('temprature(K)-->')
ylabel('enthalpy(-->)')
title('enthalpy Vs teprature')
figure(3)
plot(entropy,T,'linewidth',2);
xlabel('temprature(K)-->')
ylabel('entropy(-->)')
title('entropy Vs teprature')

%saving each plot into directory
saveas(figure(1), 'cp.png');
saveas(figure(2), 'h.png');
saveas(figure(3), 's.png');
cd(cur_dir); %moving back to previous directory
%determining molecular mass of each species by passing into Moleculor_Mass function 
species
molecular_weight=Molecular_Weight(spec)

end

2.supporting function

function  mass= Molecular_Weight(species)
  
species = upper(species); %converting cases of element into uppar case

Constituents = ['H', 'C', 'N', 'O', 'A'];%declaring mass of halocarbons

Atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948]; %thier atomic wieghts

mass = 0; 

for i=1:length(species);
    for j=1:length(Constituents);
        if strcmp(species(i),Constituents(j));%comparing each species with constituents 
           mass = mass + Atomic_weight(j); %evaluating atomic wieght
           u = j;
        end
    end
    n = str2num(species(i));
    if n>1
        mass = mass + Atomic_weight(u)*(n-1);
    end
end
end

3.Outputs

1.O2

2.2

2.N2

3

3.CO2

4

4.Moleculor wieght

5.picture of file saved


Problem i faced:

the problem i faced is that strfind() is invaid in gnu ocatve.

problem solved:

after doing some research about strfind() for gnu octave.i come to conclusion that instead of strfind()we can use findstr() only on octave.

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.