########################################################################### # # Copyright (c) 2021-2024 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file flow_sensor_data_processor.py # # @author (last) Dara Navaei # @date (last) 13-Oct-2021 # @author (original) Dara Navaei # @date (original) 04-Oct-2021 # ############################################################################ import math import os from time import time start_time = time() # This is the address that all the flow log files are located log_files_address = '/home/fw/Desktop/Users/DaraN/Flow Sensor Data' write_file = '/home/fw/Desktop/Users/DaraN/Flow Sensor Data/flow_results.csv' # Check if the report file exists. If it does, remove it first if os.path.exists(write_file): os.remove(write_file) # Create the CSV file w = open(write_file, 'w') # Search through the log files of the provided folder for root, dirs, log_files in os.walk(log_files_address): for log_file in log_files: # Get all the .log files if log_file.endswith('.log'): # Open the file as read mode r = open(os.path.join(root, log_file), 'r') is_it_fill_time = False flow_running_sum = 0.0 initial_fill_load_cell = 0.0 initial_fill_time = 0 initial_fill_flow = 0.0 initial_fill_delay = 0 counter = 0 initial_temp_flow = 0.0 temp_flow_running_sum = 0.0 # Get the log file address without the extension file_name = os.path.splitext(log_file)[0] # Remove all the addresses before the file name (i.e. home/folder/run_pump will be run_pump) file_name = file_name.split('/')[-1] print(file_name) # Add the header to the run w.write(file_name + '\r') w.write('Duty Cycle (%), Start Measured Flow (mL/min), End Measured Flow (mL/min), ' 'Avg. Measured Flow (mL/min), Calculated Flow (mL/min), Error (%)' + '\r') for data in r: counter += 1 # Parse the values the are of interest. Split from the title of the data and then split from the comma # after the data # i.e. Flow, 0.89, -> first get rid of Flow, -> 0.89, then get rid of , after the data -> 0.89 # If the data is a string, use strip() to remove all the whitespaces operation_mode = data.split('DG_op_mode,', 1)[1].split(',')[0].strip() vpo_status = data.split('VPo,', 1)[1].split(',')[0].strip() flow = float(data.split('Flow,', 1)[1].split(',')[0]) * 1000 load_cell = float(data.split('B1,', 1)[1].split(',')[0]) temp_flow = float(0.0 if 'Temp_flow' not in data else data.split('Temp_flow,', 1)[1].split(',')[0])*1000 # Check if the operation mode is fill and the VPo valve is set to fill if operation_mode == 'DG_OP_MODE_FILL' and vpo_status == 'VALVE_STATE_FILL_C_TO_NC': # Wait for the first 3 seconds of the data in the fill. This is to wait for the measured flow # to settle since the RO controller is still trying to stabilize itself initial_fill_delay += 1 # If the first 3 seconds have elapsed and the fill time has started if initial_fill_delay > 2 and is_it_fill_time is False: # Get the value of the load cell and timer (as a counter) and the flow initial_fill_load_cell = load_cell initial_fill_time = counter is_it_fill_time = True initial_fill_flow = flow initial_temp_flow = temp_flow # If it is the fill time, keep adding the parsed flow to be averaged later as the average measured # flow of the fill if is_it_fill_time is True: flow_running_sum += flow temp_flow_running_sum += temp_flow # Check if the operation mode is back to recirculate and the fill time flag is still true. This means # fill just ended and it is back to recirculate so the calculations can be done now elif operation_mode == 'DG_OP_MODE_RECIRCULATE' and is_it_fill_time is True: # Calculate the average measured flow avg_measured_flow = flow_running_sum / (counter - initial_fill_time) # Calculate the load cell flow by calculating the slope x 60 seconds to convert it to mL/min load_cell_flow = ((load_cell - initial_fill_load_cell) / (counter - initial_fill_time)) * 60 # Get the duty cycle first as the float then convert it to integer duty_cycle = float(data.split('PWM,', 1)[1].split(',')[0]) duty_cycle = int(math.floor(duty_cycle)) # Calculate the error from load cell to the average measured flow in percent error = ((load_cell_flow - avg_measured_flow) / load_cell_flow) * 100 avg_measured_temp_flow = temp_flow_running_sum / (counter - initial_fill_time) info = ('{}, {:5.3f}, {:5.3f}, {:5.3f}, {:5.3f}, {:5.3f}, {:5.3f} '.format(duty_cycle, initial_fill_flow, flow, avg_measured_flow, load_cell_flow, error, avg_measured_temp_flow)) w.write(info + '\r') # Done with this calculations, reset all the variables for the next fill initial_fill_time = 0 initial_fill_load_cell = 0 is_it_fill_time = False flow_running_sum = 0 initial_fill_delay = 0 temp_flow_running_sum = 0 elapsed_time = ('Elapsed Time: {} seconds!'.format(int(time() - start_time))) print(elapsed_time)