Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:

  • Write a function that extracts the 14 co-efficient and calculates the enthalpy, entropy and specific heats for all the species in the data file.
  • Calculate the molecular weight of each species and display it in the command window.
  • Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
  • Please write a detailed report to describe your logic.
  • The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.
  • Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.

Introduction:

What is file Parsing?

File parsing is key technique in the process of data extraction. This becomes very useful in order to extract information from different file formats. This helps the end user to utilize their full potential of documented data, making it easier to apply it in their daily operation.

It turns the file content into more structured readable form. This step is more important than simply extracting the data, which only copies without understanding it. Tech companies benefit significantly from file parsing. It boosts data system interoperability and efficiency. By making data compatible with analytical tools, it enhances accuracy and relevance. It also improves system integration and data integrity. This leads to better data analysis, operational optimization, and strategic insights.

Advantages of File Parsing:

  • Data Integration: Varied data coming from different sources are knit together with the help of file parsing. This helps the organization to adopt to the market trends rapidly.
  • Data Analytics: Instead of analyzing the documented data, by file parsing we can extract the content and structure the output neatly. Further, applying various analytical tools to visualize the obtained data.
  • Cost Reduction: Rather than employing a human to parse the file manually, which is time consuming and increases the operational cost. With just few lines of code large file can be parsed to obtain useful data in less time.

File parsing enhances systems' ability to handle today's data complexity and volume. It smoothly integrates data from various sources into decision-making processes. For businesses, it's crucial. It automates metadata extraction or turns customer interactions into valuable data. It offers a streamlined approach to accessing and analyzing structured information within various file formats.

Matlab Syntax:

  • fopen("File_Name","mode"): It helps to open the file in which ever mode selected like Read, Write etc.
  • fgetl("File_Handler"): It helps to access the file line by line. It gets the entire line then moves the cursor the next line starting point.
  • strsplit(variable, "delimiter"): It helps to split the entire line stored in the variable at the specified delimiter points.
  • str2num(variable): It helps to convert the string datatype to integers.
  • strfind(variable, "Req_character"): It helps to find the character position from the provided variable.
  • strcmp(variable, to_compare_variable): It helps to compare the variables value, if both the values are same then it provides a True condition or else False.
  • str2double(variable): It helps to convert the string datatype variable to float datatype.
  • mkdir("File_Path"): It helps to create the folder at the mentioned file path. If the entire file path is not mentioned, then it will consider the current matlab file folder location as default and create the folder in it.
  • warning("on/off", "warning_Line"): It helps to switch OFF the warning which appears while executing the code.
  • cd("File_Path"): It helps to change the directory in which you desire to store the results.
  • cd .. : It helps to change the directory one step out from the current path.
  • fclose("File_Handler"): It helps to close the opened file by mentioning its file handler in the fclose command.
  • fullfile("folder_name",'file_name'): Instead of specifying the whole file path, simply specify the folder name on to which the file is present can be accessed.
  • saveas('img_no.','file_path','file_name'): It saves the file at the specified file path

Procedure: (main code)

  • The Data file is firstly opened in the read mode. Then with the help of file handler the content of the file can be accessed.

https://drive.google.com/file/d/17J8xGRfoDmRCv-_1XRKdxhGclWWinwxa/view?usp=sharing

  • Inside the data file, It starts with the header for the content within the file then comments followed by the useful data.
  • we start accessing the file line by line using fgetl() command and store the captured line in variable for further analyses.
  • In the second line we have the global temperature values which are captured in the string datatype. which is firstly split into individual words at the delimiter position of space using strsplit() command. These individual words are then converted into integers using str2num command.
  • There are a total 53 species present in the data file.
  • Each species consists of corresponding 4 lines of data. which are accessed using for loop command.
  • Now, using the strfind() command position of "." is located. With these positon the local temperature values are captured. Similarly using the strfind() command at each line is used to locate the position of "E". With which the coefficient values are obtained and stored in variables ranging from c1 to c14.
  • Species name is identified by using strsplit() command the line of text is split at the space and array of words are captured. Then locating the first position the species name is captured.
  • We need to create a separate directory to store all the species result at their corresponding species name folder.
  • Using mkdir() command the folder is created; while executing the code each time warning pops ups that the directory already exists. In order to avoid such warning, we switch off warning using the warning() command and the line of warning.
  • From min to max local temperature 100 points are created using linspace() command. Universal gas constant value is provided for its latter use in the code.
  • Using nested for loop the Specific heat, Enthalpy & Entropy values are calculated using the provided formula. Formula states that the first 7 coefficients corresponds to higher local temperature ranges and the next 7 coefficients corresponds to lower local temperature ranges.

https://drive.google.com/file/d/1iph7ZgBvgefbKCIPpJMyWyoD7HOL4pSR/view?usp=sharing

  • Result obtained are plotted and saved in their corresponding species folder, by firstly changing the directory using cd() command. Then using the saveas() command the image is saved at the changed directory.
  • The directory needs to be changed at the end of the loop, as the function code and present code directory should appears in the same location or else the code wouldn't execute.
  • Fun_Molecular_Weight function is called to get the molecular weight of the corresponding species. and then displayed within the command window.

Main Code:

close all %close all open file

clear all %clear the workspace

clc %clear the command window

%open the themodynamic data file in read mode

file=fopen("C:\Users\91810\Downloads\THERMO.dat",'r');

%fgetl option gets each line from data file

% store the output in variable line1

line_1=fgetl(file);

%get the global temperature values

line_2=fgetl(file);

l2=strsplit(line_2); %split the string at delimiter " ".

glo_low_temp=str2num(l2{2}); %converts the scalar string value to integer

glo_mid_temp=str2num(l2{3});

glo_high_temp=str2num(l2{4});

%Total 3 lines of comment in data file

comment_1=fgetl(file);

comment_2=fgetl(file);

comment_3=fgetl(file);

total_species=53; %no. of species to evaluate

%for to extract the useful species data

for j=1:total_species

species=fgetl(file);

species_split=strsplit(species); %splitting the line into individual words

species_name=string(species_split(1)); %get the species name

%Local Temperature values specific to species

loc_min_temp=str2double(species_split(1,end-3));

loc_max_temp=str2double(species_split(1,end-2));

loc_mid_temp=str2double(species_split(1,end-1));

%Total there are 3 lines of coefficient data for i species

%Acess 1st line to exract the coefficient data

data_line1=fgetl(file);

data1=strfind(data_line1,'E'); %locating each coefficient with the help of finding character 'E'

%from the located position coefficients of species 1st line is

%extracted

c1=str2double(data_line1(data1(1)-11:data1(1)+3));

c2=str2double(data_line1(data1(2)-11:data1(2)+3));

c3=str2double(data_line1(data1(3)-11:data1(3)+3));

c4=str2double(data_line1(data1(4)-11:data1(4)+3));

c5=str2double(data_line1(data1(5)-11:data1(5)+3));

%similarly 2nd line coefficients of speices are extracted

data_line2=fgetl(file);

data2=strfind(data_line2,'E');

c6=str2double(data_line2(data2(1)-11:data2(1)+3));

c7=str2double(data_line2(data2(2)-11:data2(2)+3));

c8=str2double(data_line2(data2(3)-11:data2(3)+3));

c9=str2double(data_line2(data2(4)-11:data2(4)+3));

c10=str2double(data_line2(data2(5)-11:data2(5)+3));

%similarly 3rd line coefficients of speices are extracted

data_line3=fgetl(file);

data3=strfind(data_line3,'E');

c11=str2double(data_line3(data3(1)-11:data3(1)+3));

c12=str2double(data_line3(data3(2)-11:data3(2)+3));

c13=str2double(data_line3(data3(3)-11:data3(3)+3));

c14=str2double(data_line3(data3(4)-11:data3(4)+3));

%change the directory from 'Program-1' to 'Parsing_Thermo_Data\'

cd("Parsing_Thermo_Data\")

%within 'Parsing_Thermo_Data\' folder create new variable folder

mkdir(species_name) %folder name changes as per the looping species name

warning('off','MATLAB:MKDIR:DirectoryExists') %warning for already exisiting directory is turned off

R=8.314; %universal gas constant value

temperature=linspace(loc_min_temp,loc_max_temp,100); %range of temperature values

%createing array

Spec_heat=[0];

Enthalpy=[0];

Entropy=[0];

%Nested for loop to evaluate the properties of species

for i=1:length(temperature)

%1st seven coefficient is used to evaluate higher temperature range

if temperature>loc_mid_temp %evaluteing for higher temperature range

Spec_heat(i)=(c1 + (c2*temperature(i)) + (c3*temperature(i)^2) + (c4*temperature(i)^3) + (c5*temperature(i)^4)) * R;

Enthalpy(i)=(c1 + (c2*temperature(i))/2 + (c3*temperature(i)^2)/3 + (c4*temperature(i)^3)/4 + (c5*temperature(i)^4)/5 + (c6/temperature(i))) * (temperature(i)*R);

Entropy(i)=((c1*log(temperature(i))) + (c2*temperature(i)) + (c3*temperature(i)^2)/2 + (c4*temperature(i)^3)/3 + (c5*temperature(i)^4)/4 + c7) * R;

%last seven coefficient is used to evaluate lower temperature range

else %evaluateing for lower temperature range

Spec_heat(i)=(c8 + (c9*temperature(i)) + (c10*temperature(i)^2) + (c11*temperature(i)^3) + (c12*temperature(i)^4)) * R;

Enthalpy(i)=(c8 + (c9*temperature(i))/2 + (c10*temperature(i)^2)/3 + (c11*temperature(i)^3)/4 + (c12*temperature(i)^4)/5 + (c13/temperature(i))) * (temperature(i)*R);

Entropy(i)=((c8*log(temperature(i))) + (c9*temperature(i)) + (c10*temperature(i)^2)/2 + (c11*temperature(i)^3)/3 + (c12*temperature(i)^4)/4 + c14) * R;

end %end of if-else statment

end %end of nested for loop

figure(1) %plotting window 1

plot(temperature,Spec_heat,LineWidth=2,Color='r') %specific heat is plotted in red colour, line width is 2

xlabel("Temperature (Kelvin)") %naming x axis

ylabel("Specific Heat (Cp)") %naming y axis

title(["Specific Heat Plot of",species_name]) %tittle for the plot

axis auto %axis limits are limited automatically

grid on %axis grid is put ON

saveas(1,fullfile(a,'Spec_Heat_plot.jpg')) %1st plot is saved within the species_name variable folder

figure(2) %plotting window 2

plot(temperature,Enthalpy,LineWidth=2,Color='g') %Enthalpy is plotted in green colour, line width is 2

xlabel("Temperature (Kelvin)")

ylabel("Enthalpy (H)")

title(["Enthalpy Plot of",species_name])

%axis ([loc_min_temp loc_max_temp min(Enthalpy) max(Enthalpy)])

axis auto

grid on

saveas(2,fullfile(species_name,'Enthalpy_plot.jpg'))

figure(3) %plotting window 2

plot(temperature,Entropy,LineWidth=2,Color='b') %Entropy is plotted in blue colour, line width is 2

xlabel("Temperature (Kelvin)")

ylabel("Entropy (S)")

title(["Entropy Plot of",species_name])

%axis ([loc_min_temp loc_max_temp min(Entropy) max(Entropy)])

axis auto

grid on

saveas(3,fullfile(species_name,'Entropy_plot.jpg'))

cd (species_name); %changing the directory to species_name

cd .. %changing diretory one step up 'species_name' to 'Parsing_Thermo_Data'

cd .. %changing diretory one step up 'Parsing_Thermo_Data' to program-1,where the code is saved

%calling the Fun_Molecular_Weight function

%passing the input argument species_name

Mol_wgt=Fun_Molecular_Weight(species_name);

stmt=sprintf("Molecular Weight of %s is %d",species_name,Mol_wgt); %prepareing the display statement

disp(stmt); %calling the display function to display the prepared statement

end %end of for loop

fclose(file) %close the opened file

Procedure: (function code)

  • One should remember that the main code and function code should be saved within the same folder to in order to execute.
  • In our case, in order to save the files we have changed the directory while the code is running. so while calling the function one should get back to the main code directory.
  • Species name is provided as the input argument for the function. where in the molecular formula is stored, which is then split into individual character and stored in a array.
  • This array is then compared with element array to match, if matched then the corresponding atomic weight array element is called using the matched element index.
  • atomic weight of all the element present in the species is added. Sometimes the Molecular formula contains numeric values which represents the no. of atoms of same element present.
  • In case of numeric present, we check if the numeric is greater than 1. If yes, then we solve another expression to get the atomic weight for that element.
  • once all the characters in the species array is checked the finally obtained molecular weight value is returned to the main code.

Function Code:

function Mol_wgt=Fun_Molecular_Weight(species_name)

species=[]; %assigning empty array

%converting the species_name string to individual cell array

species=num2cell(char(species_name));

elements=['H','C','N','O','A']; %assigning the elements array

atomic_weight=[1,12,14,16,40]; %assigning the elements corresponding atomic weight array

Mol_wgt=0; %initializing the variable

%for loop to compare the species text character with element array

for i=1:length(species) %accessing the species array for loop

for j=1:length(elements) %accessing the element array for loop

if strcmp(species(i),elements(j)) %comparing the elements using if statment

Mol_wgt = Mol_wgt + atomic_weight(j); %for true if statement expression, is evaluated

atoms=j; %index of the element is stored

end %end of if statement

end %end of for loop

%molecular formula consist of numbers signifying no. of atoms

n=str2double(species(i)); %converting the scalar string to integer

%if the numeric value is present in molecular formula

%then atomic weight of previous index is considered

%expression is evaluated to obtain the molecular weight

if n>1

Mol_wgt = Mol_wgt + (atomic_weight(atoms)*(n-1));

end %end of if statement

end %end of nested for statement

end %end of for statement

Result:

1. Plots of Oxygen (O2):

https://drive.google.com/file/d/15XBO0PC19wjXUnrNor73tmkYtJdQxM1S/view?usp=sharing, https://drive.google.com/file/d/1RM_WWfOsGTmuSDyciQ0viXtxzHJQjFUY/view?usp=sharing, https://drive.google.com/file/d/1XiBHDI_8s3VWiKyEkAkY8H4ALk2cM5Dd/view?usp=sharing

2. Plots of Nitrogen (N2):

https://drive.google.com/file/d/1-u6cKEZSmVPuGsSqRHDyQaRp1Ip4_CT7/view?usp=sharing, https://drive.google.com/file/d/1KJxUxC4aE7FXHDh6n_AjPsABU1DpfcKm/view?usp=sharing, https://drive.google.com/file/d/1yhOMysdC-wiDAIzpXr9bNIgct6IN_O3C/view?usp=sharing

3. Plots of Carbon Dioxide (CO2):

https://drive.google.com/file/d/1Q0LofWfGpzoWTyrV_j-pIcGZg8ivo9ZQ/view?usp=sharing, https://drive.google.com/file/d/1Rb3izTpF6tZ-Kfq1kKiIanMG9eEs9vpD/view?usp=sharing, https://drive.google.com/file/d/1srQKnCXQ_Ri1Eiq4QydAUC17MS-GtU-B/view?usp=sharing

Screenshot of folder: file path ("C:\Users\91810\Desktop\matlab program\Program-1\Parsing_Thermo_Data\AR")

https://drive.google.com/file/d/17HiWjfRqTXxEleSKL-sEeUhc3X4hGA_r/view?usp=sharing

Workspace window Pic:

https://drive.google.com/file/d/1GM4-J5JQzmJHctHSxmvoP9Y25Q6JvvAW/view?usp=sharing, https://drive.google.com/file/d/1ne4iQeUE2yAM5Y6faMsDKS0uRF6dOw0O/view?usp=sharing

Command window, Molecular weight pic:

https://drive.google.com/file/d/1TjSKKVGo0GGMpzYMjHzhkhIRhMCoDcCL/view?usp=sharing, https://drive.google.com/file/d/1jR2_AROkoZ287kJCBeZ-1PHf8SI_pgaq/view?usp=sharing

Conclusion:

  • Parsing of the provided data file is done.
  • Specific Heat, Enthalpy & Entropy values has been evaluated.
  • Plots has been plotted for all the species and saved in their respective folder.
  • Molecular weight of the species has been evaluated and displayed in the command window

Error Faced:

1.

https://drive.google.com/file/d/1dXHyZnKEslMq7JHQqXSQ6jsiTUvCRzm3/view?usp=sharing

Error: While executing the code received this error. This error occurs when the matrix multiplication is carried out using dot operator the size of the matrix should satisfy the matrix rule.

Overcome: So tried to check all the elements size but couldn’t identify. Hence instead of using dot operator for matrix multiplication. I have indexed all the element and then carried the expression, code got executed.

2.

https://drive.google.com/file/d/140xCAtf8-0W7C4wcgQbz0W5wf7kWgkZh/view?usp=sharing

Error: Code could not be able to find the function file at the current directory.


Overcome: While executing the code in order to save the data the directory had been changed. As the code approached while calling the function it could not find the function file. So, I had to step-up twice to reach back to my code directory where my both main code as well as function code is present. Once the necessary changes are done the code got executed.

3.

https://drive.google.com/file/d/1N_wGDrF4tbjbFUHfAHa3cWNNQfY8RnLv/view?usp=sharing

Error: disp() command cannot handle many input arguments.

Overcome: I tried to reduce the line of code by accommodating it within disp() command, but disp() command does not allow many argument. Instead of using disp() command alone, I have used sprintf() command to hold the statement along with argument in a variable and then use it as argument for disp() command to rectify the error.

4.

https://drive.google.com/file/d/1m-l8SXY1-FxKQtc2cjDrOhaj_riMmwN3/view?usp=sharing

Error: str2num() command accepts only scalar string array i.e. array with size 1x1.

Overcome: So instead of str2num() command I have used str2double() which accepts non scalar string array also. So once changing the command, code got executed.

Reference:

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.