# coding: utf-8
#
# Copyright © 2021 MSC.Software Corporation
#  
# Permission to use Scripts
#  
# Permission is granted to MSC.Software customers with valid licenses of the MSC Apex™ software 
# to use script files posted on this page (collectively “Scripts”) solely for customers’ own authorized 
# internal use in conjunction with licensed use of the MSC Apex software.  
# No other use is permitted.  No license to MSC Apex or other software is granted under this Permission.
#  
# SCRIPTS ARE PROVIDED ON AN "AS-IS" BASIS, WITH NO WARRANTIES OF ANY KIND.  USE OF THE SCRIPTS IS AT CUSTOMER'S SOLE RISK.
#  
# IN NO EVENT SHALL MSC.SOFTWARE CORPORATION, ITS AFFILIATES, SUBSIDIARIES, SUPPLIERS, AGENTS, REPRESENTATIVES OR EMPLOYEES BE LIABLE FOR ANY 
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, TORT, 
# STRICT LIABILITY, OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION USE OF THE SCRIPTS, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
#  
# Customer shall indemnify, defend and hold MSC.Software Corporation, its affiliates, subsidiaries, suppliers, agents, 
# employees or representatives (collectively, “Indemnified Parties”) harmless from and against any third-party claims 
# brought against Indemnified Parties rising out of or in connection with Customer’s use of the Scripts.
#

class unit_conversion_factor():

    def __init__(self):

        self.pressure_units = {}
        self.pressure_units['Pa'] = [1.0, 0.0]
        self.pressure_units['psi'] = [6.89475729318E3, 0.0]
        self.pressure_units['psf'] = [4.78802589804E1, 0.0]
        self.pressure_units['bar'] = [1.0E5, 0.0]
        self.pressure_units['kbar'] = [1.0E8, 0.0]
        self.pressure_units['Gbar'] = [1.0E14, 0.0]
        self.pressure_units['hPa'] = [1.0E2, 0.0]
        self.pressure_units['dyn/cm2'] = [1.0E-1, 0.0]
        self.pressure_units['Mbar'] = [1.0E11, 0.0]
        self.pressure_units['kPa'] = [1.0E3, 0.0]
        self.pressure_units['MPa'] = [1.0E6, 0.0]
        self.pressure_units['GPa'] = [1.0E9, 0.0]
        self.pressure_units['kgf/mm2'] = [9.80665E6, 0.0]

        self.length_units = {}
        self.length_units['m'] = [1.0, 0.0]
        self.length_units['mm'] = [0.001, 0.0]
        self.length_units['in'] = [2.54E-2, 0.0]
        self.length_units['cm'] = [0.01, 0.0]
        self.length_units['ft'] = [3.048E-1, 0.0]

        self.temperature_units = {}
        self.temperature_units['K'] = [1.0, 0.0]
        self.temperature_units['C'] = [1.0, 273.15]
        self.temperature_units['F'] = [5.0/9.0, 255.372222222]

        self.unit_quantities = {}
        self.unit_quantities['Length'] = self.length_units
        self.unit_quantities['Pressure'] = self.pressure_units
        self.unit_quantities['Temperature'] = self.temperature_units

    def get_unit_conversion_factor(self, quantity, from_unit, to_unit):

        try:
            from_factor = self.unit_quantities[quantity][from_unit][0]
            from_offset = self.unit_quantities[quantity][from_unit][1]

            to_factor = self.unit_quantities[quantity][to_unit][0]
            to_offset = self.unit_quantities[quantity][to_unit][1]

            conversion_factor = from_factor/to_factor
            conversion_offset = from_offset + to_offset
        except:
            conversion_factor = None
            conversion_offset = None

        return conversion_factor, conversion_offset
