Edit custom units

EML has a set list of units that can be added to an EML file. These can be seen by using the following code:

standardUnits <- EML::get_unitList()
View(standardUnits$units)

If you have units that are not in the standard EML unit list, you will need to build a custom unit list. A unit typically consists of the following fields:

  • id: The unit id (ids are camelCased)
  • unitType: The unitType (run View(standardUnits$unitTypes) to see standard unitTypes)
  • parentSI: The parentSI unit (e.g. for kilometer parentSI = “meter”)
  • multiplierToSI: Multiplier to the parentSI unit (e.g. for kilometer multiplierToSI = 1000)
  • name: Unit abbreviation (e.g. for kilometer name = “km”)
  • description: Text defining the unit (e.g. for kilometer description = “1000 meters”)

To manually generate the custom units list, create a dataframe with the fields mentioned above. An example is provided below that can be used as a template:

custom_units <- data.frame(
    
  id = c('partsPerThousand', 'decibar', 'wattsPerSquareMeter', 'micromolesPerGram', 'practicalSalinityUnit'),
  unitType = c('dimensionless', 'pressure', 'power', 'amountOfSubstanceWeight''dimensionless'),
  parentSI = c(NA, 'pascal', 'watt', 'molesPerKilogram'NA),
  multiplierToSI = c(NA, '10000', '1', '1000000000', NA),
  abbreviation = c('ppt', 'decibar', 'W/m^2', 'umol/g', 'PSU'),
  description = c('parts per thousand', 'decibar', 'watts per square meter', 'micro moles per gram', 'used to describe the concentration of dissolved salts in water, the UNESCO Practical Salinity Scale of 1978 (PSS78) defines salinity in terms of a conductivity ratio'))

Using EML::get_unit_id for custom units will also generate valid EML unit ids.

Custom units are then added to additionalMetadata using the following command:

unitlist <- set_unitList(custom_units, as_metadata = TRUE)
doc$additionalMetadata <- unitlist

If units that should be standardUnit are added as customUnit, you can use the following code to fix this issue:

# add standard unit
doc$dataset$dataTable[[i]]$attributeList$attribute[[i]]$measurementScale$ratio$unit$standardUnit <- "your standard unit"

# get rid of custom unit
doc$dataset$dataTable[[i]]$attributeList$attribute[[i]]$measurementScale$ratio$unit$customUnit <- NULL

If you want to find all of the positions of a certain custom unit that should be standard, try this code:

ls <- purrr::map(doc$dataset$dataTable[[i]]$attributeList$attribute, 
                  ~str_detect(.x$measurementScale$ratio$unit$customUnit, "your standard unit"))
                  
which(ls == TRUE)