Added functions for converting to/from wavelength and wavenumbers.
							parent
							
								
									e2d0d21236
								
							
						
					
					
						commit
						c00fcc54a9
					
				| @ -0,0 +1,26 @@ | ||||
| ################################################## | ||||
| ################## CountRods ##################### | ||||
| ################################################## | ||||
| CountRods <- function() { | ||||
|    ## Description: | ||||
|    ##    | ||||
|    ## Usage: | ||||
|    ##    | ||||
|    ## Arguments: | ||||
|    ##    | ||||
|    ##    | ||||
|    ##    | ||||
|    ##    | ||||
|    ##    | ||||
|    ##    | ||||
|    ##    | ||||
|    ##    | ||||
|    ##    | ||||
|    ##    | ||||
|    ##    | ||||
|    ## Return value: | ||||
|    ##   | ||||
|    # | ||||
|     | ||||
|    return() | ||||
| } | ||||
| @ -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) | ||||
| } | ||||
|  | ||||
| @ -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) | ||||
| } | ||||
|  | ||||
| @ -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 <return>. | ||||
| # !...............................................   | ||||
| 
 | ||||
| 
 | ||||
| # 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)) | ||||
| } | ||||
|     | ||||
|     | ||||
|     | ||||
|     | ||||
|     | ||||
|     | ||||
|     | ||||
|     | ||||
|     | ||||
|     | ||||
|     | ||||
|     | ||||
|     | ||||
| @ -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) | ||||
| } | ||||
| @ -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) | ||||
| } | ||||
					Loading…
					
					
				
		Reference in New Issue