Advertisement
Python253

metal_albedo_analysis_tool

Jun 7th, 2024
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.40 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: metal_albedo_analysis_tool.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script provides a comprehensive suite of tools for analyzing metal properties, focusing on albedo and index of refraction (IOR).
  10.    - Albedo refers to the proportion of incident light that is reflected by a surface, making it a crucial factor in determining the visual appearance of metals.
  11.    - Index of refraction (IOR) quantifies how light bends as it passes through a material, influencing its optical properties such as reflection and refraction.
  12.    - By leveraging the provided functionalities, users can explore and understand the albedo characteristics of various metals.
  13.    - Users can also compare the metal IOR values to gain insights into their optical behaviors.
  14.    - This script also outputs the difference in value of the compared metal IOR to the terminal.
  15.  
  16. Requirements:
  17.    - Python 3.x
  18.    - Matplotlib library for data visualization
  19.  
  20. Functions:
  21.    - visualize_metal_colors():
  22.        Visualizes metal colors by plotting their albedo.
  23.    - display_metal_data():
  24.        Displays the available metal data as a numbered list.
  25.    - calculate_albedo():
  26.        Calculates the albedo of a specific metal selected by the user.
  27.    - compare_ior(metal1, metal2):
  28.        Compares the index of refraction between two metals selected by the user.
  29.  
  30. Usage:
  31.    - Run the script in a Python environment.
  32.    - Follow the on-screen menu options to choose between different functionalities.
  33.    - Enter the appropriate input as prompted to perform desired actions.
  34.  
  35. Example Output:
  36.  
  37.                :: MENU OPTIONS ::
  38.  
  39.                1. Visualize Metal Colors
  40.                2. Calculate Albedo
  41.                3. Compare Index of Refraction
  42.                0. Exit
  43.  
  44.                Enter your choice (1-3) or type '0' to exit: 3
  45.  
  46.                Metal data:
  47.                 1. Silver
  48.                 2. Gold
  49.                 3. Copper
  50.                 4. Aluminium
  51.                 5. Chromium
  52.                 6. Lead
  53.                 7. Platinum
  54.                 8. Titanium
  55.                 9. Tungsten
  56.                10. Iron
  57.                11. Vanadium
  58.                12. Zinc
  59.                13. Nickel
  60.                14. Mercury
  61.                15. Cobalt
  62.  
  63.                Enter the number of the first metal: 7
  64.  
  65.                Enter the number of the second metal: 12
  66.  
  67.                Platinum has a higher Index of Refraction (1.0240) than Zinc (1.0110).
  68.                The difference is 0.0130.
  69.  
  70. Additional Notes:
  71.    - Metal data includes information such as RGB values and index of refraction.
  72.    - The script utilizes Matplotlib for data visualization.
  73.    - Users can interactively explore metal properties and compare them using this tool.
  74. """
  75.  
  76. import matplotlib.pyplot as plt
  77.  
  78. # Metal data
  79. metal_data = {
  80.     "Silver": {"Red_ACEScg": 0.758765, "Green_ACEScg": 0.755048, "Blue_ACEScg": 0.752412, "IOR": 1.082},
  81.     "Gold": {"Red_ACEScg": 0.659051, "Green_ACEScg": 0.458738, "Blue_ACEScg": 0.123940, "IOR": 1.35002},
  82.     "Copper": {"Red_ACEScg": 0.614256, "Green_ACEScg": 0.298303, "Blue_ACEScg": 0.213841, "IOR": 1.21901},
  83.     "Aluminium": {"Red_ACEScg": 0.618401, "Green_ACEScg": 0.672947, "Blue_ACEScg": 0.682368, "IOR": 1.002},
  84.     "Chromium": {"Red_ACEScg": 0.140349, "Green_ACEScg": 0.140349, "Blue_ACEScg": 0.140349, "IOR": 1.03},
  85.     "Lead": {"Red_ACEScg": 0.264084, "Green_ACEScg": 0.265443, "Blue_ACEScg": 0.277903, "IOR": 1.016},
  86.     "Platinum": {"Red_ACEScg": 0.654022, "Green_ACEScg": 0.616726, "Blue_ACEScg": 0.377176, "IOR": 1.024},
  87.     "Titanium": {"Red_ACEScg": 0.699456, "Green_ACEScg": 0.637539, "Blue_ACEScg": 0.297492, "IOR": 1.086},
  88.     "Tungsten": {"Red_ACEScg": 0.505142, "Green_ACEScg": 0.283667, "Blue_ACEScg": 0.179808, "IOR": 1.007},
  89.     "Iron": {"Red_ACEScg": 0.456733, "Green_ACEScg": 0.434045, "Blue_ACEScg": 0.262660, "IOR": 1.006},
  90.     "Vanadium": {"Red_ACEScg": 0.644844, "Green_ACEScg": 0.523107, "Blue_ACEScg": 0.196363, "IOR": 1.034},
  91.     "Zinc": {"Red_ACEScg": 0.423637, "Green_ACEScg": 0.418052, "Blue_ACEScg": 0.409058, "IOR": 1.011},
  92.     "Nickel": {"Red_ACEScg": 0.457723, "Green_ACEScg": 0.399388, "Blue_ACEScg": 0.169834, "IOR": 1.016},
  93.     "Mercury": {"Red_ACEScg": 0.167639, "Green_ACEScg": 0.167639, "Blue_ACEScg": 0.167639, "IOR": 1.013},
  94.     "Cobalt": {"Red_ACEScg": 0.071277, "Green_ACEScg": 0.045923, "Blue_ACEScg": 0.015718, "IOR": 1.031},
  95. }
  96.  
  97. # Function to visualize metal colors
  98. def visualize_metal_colors():
  99.     """
  100.    Visualizes metal colors by plotting their albedo.
  101.    """
  102.     # Initialize empty lists to store metal names and their respective albedo values
  103.     color_values = []                    # List to store albedo values
  104.     metal_names = []                     # List to store metal names
  105.  
  106.     # Iterate through the metal_data dictionary to extract metal names and albedo values
  107.     for metal_name, metal_data_single in metal_data.items():  # Change variable name here
  108.         metal_names.append(metal_name)   # Append the metal name to the metal_names list
  109.         # Calculate the average albedo by summing up the RGB values and dividing by 3
  110.         albedo = (metal_data_single["Red_ACEScg"] + metal_data_single["Green_ACEScg"] + metal_data_single["Blue_ACEScg"]) / 3
  111.         color_values.append(albedo)      # Append the calculated albedo to the color_values list
  112.  
  113.     # Plotting the metal albedo values as a horizontal bar chart
  114.     plt.figure(figsize=(10, 6))          # Set the figure size
  115.     plt.title('Metal Albedo')            # Set the title of the plot
  116.     plt.barh(metal_names, color_values, color='steelblue')   # Create a horizontal bar chart
  117.     plt.xlabel('Albedo')                 # Set the label for the x-axis
  118.     plt.ylabel('Metal')                  # Set the label for the y-axis
  119.     plt.grid(axis='x')                   # Add gridlines along the x-axis
  120.     plt.show()                           # Display the plot
  121.  
  122. # Function to display metal data as a numbered list
  123. def display_metal_data():
  124.     """
  125.    Displays the metal data as a numbered list.
  126.    """
  127.     print("\nMetal data:")
  128.     # Step 1: Iterate through metal_data dictionary and enumerate items
  129.     for index, (metal_name, _) in enumerate(metal_data.items(), start=1):  
  130.         metal_index = f"{index:2}"      # Pad single digits with a space
  131.         # Step 2: Print metal index and name
  132.         print(f"{metal_index}. {metal_name}")
  133.  
  134. # Function to calculate the albedo of a specific metal
  135. def calculate_albedo():
  136.     """
  137.    Calculates the albedo of a specific metal selected by the user.
  138.    """
  139.     display_metal_data()                # Step 1: Display the list of metals
  140.     metal_index_str = input("\nEnter the number of the metal: ")
  141.     try:
  142.         metal_index = int(metal_index_str)
  143.         if 1 <= metal_index <= len(metal_data):
  144.             metal_name = list(metal_data.keys())[metal_index - 1]
  145.             metal_single = metal_data[metal_name]
  146.             # Step 2: Calculate the albedo using RGB values
  147.             albedo = (metal_single["Red_ACEScg"] + metal_single["Green_ACEScg"] + metal_single["Blue_ACEScg"]) / 3
  148.             print(f"\nThe albedo of {metal_name} is: {albedo:.4f}")
  149.         else:
  150.             print("\nInvalid metal number!")
  151.     except ValueError:
  152.         print("\nInvalid input! Please enter a valid number.")
  153.  
  154. # Function to compare the index of refraction between two metals
  155. def compare_ior(metal1_str, metal2_str):
  156.     """
  157.    Compares the Index of Refraction between two metals selected by the user.
  158.    """
  159.     try:
  160.         metal1_index = int(metal1_str)
  161.         metal2_index = int(metal2_str)
  162.         if 1 <= metal1_index <= len(metal_data) and 1 <= metal2_index <= len(metal_data):
  163.             metal1_name = list(metal_data.keys())[metal1_index - 1]
  164.             metal2_name = list(metal_data.keys())[metal2_index - 1]
  165.             metal1_data = metal_data[metal1_name]
  166.             metal2_data = metal_data[metal2_name]
  167.             if metal1_data["IOR"] > metal2_data["IOR"]:
  168.                 # Step 1: Print comparison result if metal1's IOR is higher
  169.                 print(f"\n{metal1_name} has a higher Index of Refraction ({metal1_data['IOR']:.4f}) than {metal2_name} ({metal2_data['IOR']:.4f}).")
  170.                 print(f"The difference is {metal1_data['IOR'] - metal2_data['IOR']:.4f}.")
  171.             elif metal1_data["IOR"] < metal2_data["IOR"]:
  172.                 # Step 2: Print comparison result if metal2's IOR is higher
  173.                 print(f"\n{metal2_name} has a higher Index of Refraction ({metal2_data['IOR']:.4f}) than {metal1_name} ({metal1_data['IOR']:.4f}).")
  174.                 print(f"The difference is {metal2_data['IOR'] - metal1_data['IOR']:.4f}.")
  175.             else:
  176.                 # Step 3: Print if both metals have equal IOR
  177.                 print(f"\nThe Index of Refraction of {metal1_name} ({metal1_data['IOR']:.4f}) and {metal2_name} ({metal2_data['IOR']:.4f}) are equal.")
  178.         else:
  179.             print("\nInvalid metal numbers!")
  180.     except ValueError:
  181.         print("\nInvalid input! Please enter valid numbers.")
  182.     print()                             # Add a blank line for clarity
  183.  
  184. # Menu-driven interface
  185. while True:
  186.     print("\n:: MENU OPTIONS ::\n")
  187.     print("1. Visualize Metal Colors")
  188.     print("2. Calculate Albedo")
  189.     print("3. Compare Index of Refraction")
  190.     print("0. Exit")
  191.  
  192.     choice = input("\nEnter your choice (1-3) or type '0' to exit: ")
  193.  
  194.     if choice == '1':
  195.         # Print the full metal data to the terminal in a formatted manner
  196.         print("\nMetal Data:")
  197.         for metal, data in metal_data.items():
  198.             rgb_values = ', '.join([f"{color[0]}={value:.6f}" for color, value in data.items() if color != 'IOR'])
  199.             print(f"{metal}: R={data['Red_ACEScg']:.6f}, G={data['Green_ACEScg']:.6f}, B={data['Blue_ACEScg']:.6f} IOR: {data['IOR']:.4f}")
  200.         visualize_metal_colors()
  201.     elif choice == '2':
  202.         calculate_albedo()
  203.     elif choice == '3':
  204.         display_metal_data()
  205.         metal1 = input("\nEnter the number of the first metal: ")
  206.         metal2 = input("\nEnter the number of the second metal: ")
  207.         compare_ior(metal1, metal2)
  208.     elif choice == '0':
  209.         print("\nExiting program...   GoodBye!\n")
  210.         break                           # Exit the program immediately
  211.     else:
  212.         print("\nInvalid choice! Please enter a valid option.\n")
  213.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement