From c00fcc54a9d1c91a661cc41a76242e2fcb5f9901 Mon Sep 17 00:00:00 2001 From: Taha Ahmed Date: Fri, 26 Aug 2016 11:58:55 +0200 Subject: [PATCH] Added functions for converting to/from wavelength and wavenumbers. --- .gitignore | 0 AVS2SHE.R | 0 Celsius2Kelvin.R | 0 ConvertRefPot.R | 6 +-- ConvertRefPot.tex | 0 ConvertRefPotEC.R | 0 ConvertRefPotEC.tex | 0 CountRods.R | 26 ++++++++++++ GenericXtableSetAttributes.R | 0 Kelvin2Celsius.R | 0 LoadRData2Variable.R | 0 LongtableXtableHeader.R | 0 ProvideSampleId.R | 0 ProvideSampleId.tex | 0 SHE2AVS.R | 0 SubfigureGenerator.R | 2 +- SubstrateHistory.R | 2 +- TabularXtableHeader.R | 0 as.degrees.R | 0 as.radians.R | 0 capitalize.R | 0 eV2nm.R | 11 +++-- hms2seconds.R | 0 int2padstr.R | 0 is.wholenumber.R | 0 molarity2mass.R | 0 nm2eV.R | 11 +++-- numbers2words.R | 0 roundup.R | 0 siunitx-exp-uncert.R | 81 ++++++++++++++++++++++++++++++++++++ thth2d.R | 2 +- trapz.R | 0 wavelength2num.R | 13 ++++++ wavenum2length.R | 13 ++++++ 34 files changed, 153 insertions(+), 14 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 AVS2SHE.R mode change 100644 => 100755 Celsius2Kelvin.R mode change 100644 => 100755 ConvertRefPot.R mode change 100644 => 100755 ConvertRefPot.tex mode change 100644 => 100755 ConvertRefPotEC.R mode change 100644 => 100755 ConvertRefPotEC.tex create mode 100755 CountRods.R mode change 100644 => 100755 GenericXtableSetAttributes.R mode change 100644 => 100755 Kelvin2Celsius.R mode change 100644 => 100755 LoadRData2Variable.R mode change 100644 => 100755 LongtableXtableHeader.R mode change 100644 => 100755 ProvideSampleId.R mode change 100644 => 100755 ProvideSampleId.tex mode change 100644 => 100755 SHE2AVS.R mode change 100644 => 100755 SubfigureGenerator.R mode change 100644 => 100755 SubstrateHistory.R mode change 100644 => 100755 TabularXtableHeader.R mode change 100644 => 100755 as.degrees.R mode change 100644 => 100755 as.radians.R mode change 100644 => 100755 capitalize.R mode change 100644 => 100755 eV2nm.R mode change 100644 => 100755 hms2seconds.R mode change 100644 => 100755 int2padstr.R mode change 100644 => 100755 is.wholenumber.R mode change 100644 => 100755 molarity2mass.R mode change 100644 => 100755 nm2eV.R mode change 100644 => 100755 numbers2words.R mode change 100644 => 100755 roundup.R create mode 100755 siunitx-exp-uncert.R mode change 100644 => 100755 thth2d.R mode change 100644 => 100755 trapz.R create mode 100755 wavelength2num.R create mode 100755 wavenum2length.R diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/AVS2SHE.R b/AVS2SHE.R old mode 100644 new mode 100755 diff --git a/Celsius2Kelvin.R b/Celsius2Kelvin.R old mode 100644 new mode 100755 diff --git a/ConvertRefPot.R b/ConvertRefPot.R old mode 100644 new mode 100755 index f8f1427..46ab5a2 --- a/ConvertRefPot.R +++ b/ConvertRefPot.R @@ -1,6 +1,6 @@ -source("/home/taha/chepec/chetex/common/R/common/SHE2AVS.R") -source("/home/taha/chepec/chetex/common/R/common/AVS2SHE.R") -source("/home/taha/chepec/chetex/common/R/common/ConvertRefPotEC.R") +source(HomeByHost("/home/taha/chepec/chetex/common/R/common/SHE2AVS.R")) +source(HomeByHost("/home/taha/chepec/chetex/common/R/common/AVS2SHE.R")) +source(HomeByHost("/home/taha/chepec/chetex/common/R/common/ConvertRefPotEC.R")) ################################################## ################# ConvertRefPot ################## diff --git a/ConvertRefPot.tex b/ConvertRefPot.tex old mode 100644 new mode 100755 diff --git a/ConvertRefPotEC.R b/ConvertRefPotEC.R old mode 100644 new mode 100755 diff --git a/ConvertRefPotEC.tex b/ConvertRefPotEC.tex old mode 100644 new mode 100755 diff --git a/CountRods.R b/CountRods.R new file mode 100755 index 0000000..f6bfa0b --- /dev/null +++ b/CountRods.R @@ -0,0 +1,26 @@ +################################################## +################## CountRods ##################### +################################################## +CountRods <- function() { + ## Description: + ## + ## Usage: + ## + ## Arguments: + ## + ## + ## + ## + ## + ## + ## + ## + ## + ## + ## + ## Return value: + ## + # + + return() +} diff --git a/GenericXtableSetAttributes.R b/GenericXtableSetAttributes.R old mode 100644 new mode 100755 diff --git a/Kelvin2Celsius.R b/Kelvin2Celsius.R old mode 100644 new mode 100755 diff --git a/LoadRData2Variable.R b/LoadRData2Variable.R old mode 100644 new mode 100755 diff --git a/LongtableXtableHeader.R b/LongtableXtableHeader.R old mode 100644 new mode 100755 diff --git a/ProvideSampleId.R b/ProvideSampleId.R old mode 100644 new mode 100755 diff --git a/ProvideSampleId.tex b/ProvideSampleId.tex old mode 100644 new mode 100755 diff --git a/SHE2AVS.R b/SHE2AVS.R old mode 100644 new mode 100755 diff --git a/SubfigureGenerator.R b/SubfigureGenerator.R old mode 100644 new mode 100755 index b2ce9a7..f35941d --- a/SubfigureGenerator.R +++ b/SubfigureGenerator.R @@ -47,7 +47,7 @@ SubfigureGenerator <- function(images, if (landscape == TRUE) { cat("\\begin{sidewaysfigure}\\centering\n", file = zz) } else { - cat("\\begin{figure}[tb]\\centering\n", file = zz) + cat("\\begin{figure}[hb]\\centering\n", file = zz) } # display images in a X-by-Y grid diff --git a/SubstrateHistory.R b/SubstrateHistory.R old mode 100644 new mode 100755 index 4f121f3..86c6419 --- a/SubstrateHistory.R +++ b/SubstrateHistory.R @@ -1,4 +1,4 @@ -source("/home/taha/chepec/chetex/common/R/common/LoadRData2Variable.R") +source(HomeByHost("/home/taha/chepec/chetex/common/R/common/LoadRData2Variable.R")) ################################################## ############## SubstrateHistory ################## diff --git a/TabularXtableHeader.R b/TabularXtableHeader.R old mode 100644 new mode 100755 diff --git a/as.degrees.R b/as.degrees.R old mode 100644 new mode 100755 diff --git a/as.radians.R b/as.radians.R old mode 100644 new mode 100755 diff --git a/capitalize.R b/capitalize.R old mode 100644 new mode 100755 diff --git a/eV2nm.R b/eV2nm.R old mode 100644 new mode 100755 index b1c29db..474d380 --- a/eV2nm.R +++ b/eV2nm.R @@ -1,13 +1,16 @@ ################################################## #################### eV2nm ####################### ################################################## +source(HomeByHost("/home/taha/chepec/chetex/common/R/sunlight/solarconstants.R")) + eV2nm <- function(eV) { # Converts energy in eV to wavelength in nm # - # Define some constants needed for the calculations - Plancks.constant <- 4.135667516E-15 # \electron\volt\per\second - speed.of.light <- 299792458 # \meter\per\second + sun.constants <- solar.constants() + + nm <- + sun.constants["h.eV", "value"] * + 1E9 * sun.constants["c", "value"] / eV - nm <- Plancks.constant * 1E9 * speed.of.light / eV return(nm) } diff --git a/hms2seconds.R b/hms2seconds.R old mode 100644 new mode 100755 diff --git a/int2padstr.R b/int2padstr.R old mode 100644 new mode 100755 diff --git a/is.wholenumber.R b/is.wholenumber.R old mode 100644 new mode 100755 diff --git a/molarity2mass.R b/molarity2mass.R old mode 100644 new mode 100755 diff --git a/nm2eV.R b/nm2eV.R old mode 100644 new mode 100755 index c1b4f90..e4a379d --- a/nm2eV.R +++ b/nm2eV.R @@ -1,13 +1,16 @@ ################################################## #################### nm2eV ####################### ################################################## +source(HomeByHost("/home/taha/chepec/chetex/common/R/sunlight/solarconstants.R")) + nm2eV <- function(nm) { # Converts wavelength in nm to energy in eV # - # Define some constants needed for the calculations - Plancks.constant <- 4.135667516E-15 # \electron\volt\per\second - speed.of.light <- 299792458 # \meter\per\second + sun.constants <- solar.constants() + + eV <- + sun.constants["h.eV", "value"] * + 1E9 * sun.constants["c", "value"] / nm - eV <- Plancks.constant * 1E9 * speed.of.light / nm return(eV) } diff --git a/numbers2words.R b/numbers2words.R old mode 100644 new mode 100755 diff --git a/roundup.R b/roundup.R old mode 100644 new mode 100755 diff --git a/siunitx-exp-uncert.R b/siunitx-exp-uncert.R new file mode 100755 index 0000000..43b43a6 --- /dev/null +++ b/siunitx-exp-uncert.R @@ -0,0 +1,81 @@ +# Writing number with exponent (such as scientific notation) with uncertainty using siunitx + +# Origin of problem, this kind of code: +# \SI[separate-uncertainty=true]{\Sexpr{formatC(cd.flux, format = "e", digits = 2)} \pm \Sexpr{formatC(cd.flux.error, format = "e", digits = 2)}}{\milli\coulomb\per\square\cm\per\second} +# makes siunitx throw the error: +# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +# ! +# ! siunitx error: "misplaced-sign-token" +# ! +# ! Misplaced sign token '\pm '. +# ! +# ! See the siunitx documentation for further information. +# ! +# ! For immediate help type H . +# !............................................... + + +# This is status-by-design according to Joseph Wright. +# http://tex.stackexchange.com/questions/123771/exponent-notation-in-siunitx + +# But if the two numbers (number and uncertainty) have the same exponent, that is ok. +# So here we try to write a function that accepts two numbers, +# and returns them written in a common exponent (as strings). + +siunitx.uncertainty <- function(quantity, uncertainty, digits = 6) { + # both arguments should be numeric + + # how to find common exponent for two numbers? + + # find exponent of quantity (tiopotensen for kvantiteten) + quantity.exponent <- floor(log(abs(quantity), 10)) + + # find coefficient of quantity + # warning, numeric-to-string-to-numeric conversion ... + quantity.coefficient <- + as.numeric(strsplit(formatC(quantity, format="e", digits=digits), "[Ee]-")[[1]][1]) + + # construct return quantity string + rquantity.string <- + paste0(formatC(quantity.coefficient, format="f", digits=digits), "e", quantity.exponent) + + + + # find exponent of uncertainty (tiopotensen for the uncertainty) + uncertainty.exponent <- floor(log(abs(uncertainty), 10)) + + # find coefficient of uncertainty + # warning, numeric-to-string-to-numeric conversion ... + uncertainty.coefficient <- + as.numeric(strsplit(formatC(uncertainty, format="e", digits=digits), "[Ee]-")[[1]][1]) + + # adjust uncertainty to the same exponent as the quantity + # express uncertainty with the same exponent as quantity + # (adjust number of uncertainty accordingly) + runcertainty.exponent <- quantity.exponent + runcertainty.coefficient <- uncertainty.coefficient * 10^(uncertainty.exponent - quantity.exponent) + + runcertainty.string <- + paste0(formatC(runcertainty.coefficient, format="f", digits=digits), "e", runcertainty.exponent) + + + # create a string directly suitable for the siunitx \num{} command + siunitx.string <- paste(quantity.coefficient, "\\pm", runcertainty.string) + + return(c(quantity = rquantity.string, + uncertainty = runcertainty.string, + siunitx = siunitx.string)) +} + + + + + + + + + + + + + \ No newline at end of file diff --git a/thth2d.R b/thth2d.R old mode 100644 new mode 100755 index 1e556b7..19cd832 --- a/thth2d.R +++ b/thth2d.R @@ -33,6 +33,6 @@ thth2d <- function(thth, wavelength = 1.540562) { # Zn-Kb1 wavelength=1.295250 # Usage: # thth : vector with thth values in degrees - # wavelength : radiation wavelength in Ångström + # wavelength : radiation wavelength in Angstrom return (wavelength / (2 * sin(as.radians(thth)))) } \ No newline at end of file diff --git a/trapz.R b/trapz.R old mode 100644 new mode 100755 diff --git a/wavelength2num.R b/wavelength2num.R new file mode 100755 index 0000000..d1afc0a --- /dev/null +++ b/wavelength2num.R @@ -0,0 +1,13 @@ +################################################## +################ wavelength2num ################## +################################################## + +wavelength2num <- function(wavelength) { + # Converts wavelength (nm) to wavenumber (cm-1) + # Only valid for absolute wavelengths, + # NOT delta wavelengths (ranges) + # http://www.powerstream.com/inverse-cm.htm + wavenumber <- + 10E6 / wavelength + return(wavenumber) +} diff --git a/wavenum2length.R b/wavenum2length.R new file mode 100755 index 0000000..ad022e3 --- /dev/null +++ b/wavenum2length.R @@ -0,0 +1,13 @@ +################################################## +################ wavenum2length ################## +################################################## + +wavenum2length <- function(wavenumber) { + # Converts wavenumber (cm-1) to wavelength (nm) + # Only valid for absolute wavenumbers, + # NOT delta wavenumbers (ranges) + # http://www.powerstream.com/inverse-cm.htm + wavelength <- + 10E6 / wavenumber + return(wavelength) +}