Project

Nasa Data parsing

clear all
close all
clc

R=287 %specifing R value
f1=fopen(\'THERMO.dat\',\'r\'); %opening DAT file in code location

fgetl(f1); %get line command
%% getting the initial lines and global temperature
Global=fgetl(f1);
Global=strsplit(Global,\' \'); %gets us the string values only and deletes the spaces inbetween 
Global_ltemp=str2num(Global{2}); % reading Global temperature
Global_mtemp=str2num(Global{3});
Global_htemp=str2num(Global{4});
fgetl(f1);fgetl(f1);fgetl(f1);  % unwanted lines in the file

[fName, pName] = uigetfile(\'*.DAT\'); %getting the directory of the .m file or the .DAT file

%% Getting other lines and evaluvating them
while true %To run the loop till the lines read \'END\'
A=fgetl(f1); %getting first line


if A(1)==(\'E\')&A(2)==\'N\'&&A(3)==\'D\' %if line ==\'END\', it will break out of the loop
    break
end

A=strsplit(A,\' \');
sp_name=A{1} % getting the species name

qwe=\'\'; % empty string
qwe=strcat(qwe,sp_name(1)); %qwe will have the first character of the species name
if length(sp_name)>1
for i=2:length(sp_name)% if the name contain numbers, it is converted into Letters. Eg: O2 -->> OO,  CH4 -->> CHHHH
    if sp_name(i)==\'2\'||sp_name(i)==\'3\'||sp_name(i)==\'4\'||sp_name(i)==\'5\'||sp_name(i)==\'6\'||sp_name(i)==\'7\'||sp_name(i)==\'8\' 
        qwe1=sp_name(i-1);
        for j=1:(str2num(sp_name(i))-1)
            qwe=strcat(qwe,qwe1);
        end
    else
        qwe1=sp_name(i);
        qwe=strcat(qwe,qwe1);
    end
end
else
    qwe=sp_name;
end
%% Finding the molecular weight
mol=0; %initialize molecular weight to 0
for i=1:length(qwe) %checking every letter of the species to know what element it is
    if qwe(i)==\'O\' %this check for a particular species and add the weight of the element to molecular weight
        mol=mol+8;
    end
    if qwe(i)==\'H\'
        mol=mol+1;
    end
    if qwe(i)==\'C\'
        mol=mol+6;
    end
    if qwe(i)==\'S\'
        mol=mol+32;
    end
    if qwe(i)==\'N\'
        mol=mol+7;
    end
    if qwe(i)==\'AR\'
        mol=mol+40; 
    end
end
Molecular_Weight=mol %prints the final Molecular weight of that species

%% Getting the Local temperature
t1=str2num(A{length(A)-3}); % gets the integer value of 4rd value from right
t2=str2num(A{length(A)-2});% gets the integer value of 3rd value from right
t3=str2num(A{length(A)-1});% gets the integer value of 2rd value from right
T=sort([t1 t2 t3]); %sort the value from acsending to decending

%% Getting the constant from data
B=fgetl(f1);
b=findstr(B,\'E\'); %finds \'E\' in the line and saves that array in b
c1=str2num(B(1:b(1)+3)); %gets the string and converts to num
c2=str2num(B(b(1)+4:b(2)+3));
c3=str2num(B(b(2)+4:b(3)+3));
c4=str2num(B(b(3)+4:b(4)+3));
c5=str2num(B(b(4)+4:b(5)+3));
C=fgetl(f1);
c=findstr(C,\'E\');
c6=str2num(C(1:c(1)+3));
c7=str2num(C(c(1)+4:c(2)+3));
c8=str2num(C(c(2)+4:c(3)+3));
c9=str2num(C(c(3)+4:c(4)+3));
c10=str2num(C(c(4)+4:c(5)+3));
D=fgetl(f1);
d=findstr(D,\'E\');
c11=str2num(D(1:d(1)+3));
c12=str2num(D(d(1)+4:d(2)+3));
c13=str2num(D(d(2)+4:d(3)+3));
c14=str2num(D(d(3)+4:d(4)+3));
%% Calculating Cp,H,S
%Cp=[];H=[];S=[]; %initializing Cp,S,H arrays.  %%this step is not required
for i=1:3
    if T(i)>500 %assuming 500 is the limiting temp, temp<=500 are low temps, temp>500 are high temps 
        a1=c1;a2=c2;a3=c3;a4=c4;a5=c5;a6=c6;a7=c7; %stores the constants to a1,a2.... if the temp is high
    else
        a1=c8;a2=c9;a3=c10;a4=c11;a5=c12;a6=c13;a7=c14;%stores the constants to a1,a2.... if the temp is low
    end
    Cp(i)=[(a1+a2*T(i)+a3*(T(i))^2+a4*(T(i))^3+a5*(T(i)^4))]*(R); %calculating based on formulaes in the question
    H(i)=[a1+a2*T(i)/2+a3*T(i)^2/3+a4*T(i)^3/4+a5*T(i)^4/5+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];
end
% Cp
% H
% S

%% Generating of Folders and Image Files
mkdir(num2str(pName),sp_name); % making a folder name of the species in the .DAT file directory
FolderDestination=strcat(num2str(pName),sp_name); %folder destination
plot(T,Cp);
saveas(gcf,\'Temporary.png\'); % saving the plot in temporary image file
fname=strcat(pName,\'Temporary.png\');
Im2=imread(fname); %reading that plot
Cpimg=fullfile(FolderDestination,\'Cp VS T.jpg\'); %Generating file named as the plot
imwrite(Im2,Cpimg); %writing the temporary img file to the above file name

plot(T,H);
saveas(gcf,\'Temporary.png\')
fname=strcat(pName,\'Temporary.png\');
Im2=imread(fname);
Cpimg=fullfile(FolderDestination,\'H VS T.jpg\');
imwrite(Im2,Cpimg);

plot(T,S);
saveas(gcf,\'Temporary.png\')
fname=strcat(pName,\'Temporary.png\');
Im2=imread(fname);
Cpimg=fullfile(FolderDestination,\'S VS T.jpg\');
imwrite(Im2,Cpimg);


end %ending syntax of while loop

%%%%  end of program  %%%%

plots of O2:

plots of N2:

plots of CO2:

Report:

  1. Specifying the R value
  2. fopen= opening the DAT file from directory
  • fgetl(f1); %get line command
  1. Global=strsplit(Global,\' \'); %gets us the string values only and deletes the spaces inbetween
  2. [fName, pName] = uigetfile(\'*.DAT\'); %getting the directory of the .m file or the .DAT file. Fname=File name, pname= Path name

The DAT file has species detail in 4 lines. So we should read 4 lines to get all details of the species.

I have taken a while loop to get all species details with a break statement if a line reads “END”.

The first string has the species name. And the final 3 strings are local temperature. I have used reading method from right because some species have extra element.

For eg: (O2 TPIS89O 2 G 200.000 3500.000 1000.000 1

)8 elements and ( H L 7/88H 1 G 200.000 3500.000 1000.000 1

) 9 elements between spaces.

Then I have created a empty string ‘ ’ for getting the species elements. If the species has a number eg:CH4, I convert it into only elements i.e. CHHHH. This is done to find the Molecular weight. By checking each and every element and comparing the molecular weight is determined.

Local temperature is got by reading the 2nd ,3rd and 4th string from the right, because in {H L 7/88H 1 G 200.000 3500.000 1000.000 1} (6) = 200.000 and { O2 TPIS89O 2 G 200.000 3500.000 1000.000 1}(6) = 3500.000.

An extra gap is present in H species between L and 7/88H.

Now, we get the constants from the remaining 3 lines and save it as a number in c1,c2,……..c14.

If T>500, then a1=c1;a2=c2;a3=c3;a4=c4;a5=c5;a6=c6;a7=c7;

Else a1=c8;a2=c9;a3=c10;a4=c11;a5=c12;a6=c13;

With this info, we calculate the Cp,H and S for that temperature. And store in Cp,H,S array respectively.

mkdir(AAA); -- generates a folder directory AAA

First I generate a folder with species name.

Then I plot the graph

Then I save the image file as a .jpg in the working directory(Temporary file)

Then using imread, I read the temporary file.

Using imwrite(file,XXX), I write the Temporary file into the XXX directory which is our AAA/Cp VS T.jpg

Similar process is used to save H,S VS T graphs.

The while loop continues to do the process for every 4 lines till it reads ‘END’.

Thank you.

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.