Unit Conversion

Introduction

By default units are converted from the US to the SI system, but this module can also do the inverse conversions.

Quick Overview

The public procedures of this module can be made accessible by USEing the following modules:

use m_unit_conversion

From the module m_unit_conversion, two procedures are public, these are CONVERT_QUANTITY and PRINT_QUANTITY. The former subroutine must be supplied with the following five arguments, (of which the fifth is optional):

call convert_quantity ( value_in , unit_in , unit_out , value_out , system )

The first two are the supplied value and unit of the quantity to convert, the third is the numerical unit and value_out contains the value of the supplied quantity when converted to system (optional, defaults to SI).

The string representation of the output unit can be obtained with a call to print_quantity:

call print_quantity ( value , num_unit , system )

Limitations

The supported units are hard coded, and not all units are available, so it may happen that a call doesn't result in the correct result. Most conventional units of length, mass, area, volume, temperature, time and electrical current are supported (check the sources if you want to know exactly which ones).

Officially not a base unit, but angles are converted to radians.

Magnitude prefixes (such as mega, giga, kilo, nano, ...) are also supported in the following format:

deca-liter (10 liter), hecto-N (100 Newton).

To avoid confusion, US force and mass units are the lbf and lbm, respectively (next to slugs for mass).

Temperature conversions are supported, but only the rankine-kelvin conversion. The conversions with offset (Celcius) are on the TODO list.

Don't assume it work flawlessly :)

Sources

The module and a test program can be downloaded here as a compressed tar archive. This archive does not contain build files, but using ifort, the module library and program can be build as follows:

$ wget http://users.telenet.be/tuinbels/fortran/m_unit_conversion-2.0.tgz
$ tar -xzf m_unit_conversion-2.0.tgz
$ cd m_unit_conversion
$ ifort -c -o m_precision.o m_precision.F90
$ ifort -c -o m_constants.o m_constants.f90
$ ifort -c -o m_option_parser.o m_option_parser.F90
$ ifort -c -o m_sys_fun.o m_sys_fun.f90
$ ifort -c -o m_unit_conversion.o m_unit_conversion.f90
$ ifort -o test_unit_conversion test_unit_conversion.f90 m_precision.o m_constants.o m_option_parser.o m_sys_fun.o m_unit_conversion.o

The supplied test program can then be executed with the option "-h" or "--help", which will print all available options,

Usage: ./test_unit_conversion [options]

options:

  --unit, -u
    unit to convert to SI system
    [character]  default:    m

  --value, -v
    value of quantity to convert to SI-system
    [real_dp]  default:       1.00000000000000

  --system, -s
    unit system to convert to
    [character]  default:     SI

  --help, -h
    print help message and quit
    [logical]  default:     F

License

The sources are licensed under a 3-clause BSD license.

API docs

The API docs can be viewed here. They were created using ROBODoc

Public module name, subroutine names and derived types

m_unit_conversion
unit conversion module
convert_quantity
subroutine to convert a quantity into its SI/US equivalent
print_quantity
subroutine to print the numerical unit

Example

program test_unit_conversion

  use m_precision, only: dp
  use m_unit_conversion

  implicit none

  character(len=256) , parameter :: unit_in = "kts"
  real(dp)           , parameter :: value_in = 10
  integer , dimension(6)         :: num_unit
  real(dp)                       :: value_out

  call convert_quantity ( value_in , unit_in , num_unit , value_out , "SI" )

  call print_quantity ( value_out , num_unit , "SI" )

end program test_unit_conversion