辐射水平转换器

时间:2011-05-06 18:23:06

标签: php units-of-measurement

我正在做一些事情,我希望能够拥有像这个网站这样的转换器:Radiation - Dose Equivalent - Sievert Conversion Factors遗憾的是这种转换器没有来源,我需要这个。

有人可以指出我正确的方向或提供这个辐射单位转换器的源代码。

一旦我找到了正确的答案,我愿意给出350点赏金积分以获得正确而完美的答案(当几天过去让我设定赏金时)。

谢谢。

编辑:

从现在开始了赏金。我需要在PHP中编写此脚本,以便通过选择将输入值转换为另一个辐射单元。

2 个答案:

答案 0 :(得分:4)

管理多种格式之间转化的最简单方法是仅维护一种类型的转化表,并进行两部分转换以获得所需的格式。这样做会有一些精度的损失,但你真的应该有足够的精度来满足这样的需求(你可以通过在伦琴类型中添加更多的数字来提高精度)。

有很多方法可以做到这一点,但我创建了一个RadiationConverter类来保持一切整洁,我做了一些链接只是为了保持干净。但是你明白了。

<?php

class RadiationConverter {

 // these are the keys we'll reference when sending a type to the methods, 
 // along with a value for what we'll display each type as:
 private $conversion_display = array ( 
     'sievert' => 'sievert',
     'millisievert' =>  'millisievert',
     'microsievert' => 'microsievert',
     'j/kg'=> 'joule/kilogram', 
     'm2/s2' => 'square meter/square second',   
     'rem' => 'Roentgen eq. man',   
     'millirem' => 'millirem',  
     'imillicurie' => 'intensity millicurie',   
     'electrons'=> 'gray (Wr=1, X-ray, gamma ray, electrons)',  
     'alpha' => 'gray (Wr=20, alpha particles)',
     'roentgen'=> 'Roentgen/hour (numerical equivalent)');

 // this is a conversion table for sieverts to anything else
 // (note the conversion factor of 1 for sievert)
 private $conversion_table = array (
    'sievert'   =>  1,
    'millisievert'  =>  0.001,
    'microsievert'  =>  0.000001,
    'j/kg'  =>  1,  
    'm2/s2' =>  1,  
    'rem'   =>  0.01,   
    'millirem'  =>  0.00001,    
    'imillicurie'   =>  1,  
    'electrons' =>  1,  
    'alpha' =>  20,
    'roentgen'  =>  0.119331742243,
);

 private $type;
 private $amount;
 private $base_type; // this is what our conversion table is based on

 public function __construct($from_type='sievert', $amount = 0) {

   $from_type = strtolower($from_type);
   if(!array_key_exists($from_type,$this->conversion_display)) {
      throw new Exception ("Radiation type does not exist.");
   }
   $this->type = $from_type;
   $this->amount = $amount;
   $this->base_type = 'sievert'; // for reference only, really

 }

 public function amount($amount=null) {
  if(empty($amount)) return $this->amount;
  $this->amount = $amount;
  return $this;
 }

 public function types() {
    return $this->conversion_display;
 }

 public function display_name($type=null) {
    if (empty($type)) $type=$this->type;
    return $this->conversion_display[$type];
 }

 public function display_as($type){
   return $this->convert_to($type) . ' '.$this->display_name($type);
 }

 public function convert_to($type) {
  $type = strtolower($type);
  if(!array_key_exists($type,$this->conversion_display)) {
      throw new Exception ("Radiation conversion type does not exist.");
  }
  // first we convert to our base type, then convert that to the new type:
  return ($this->amount*$this->conversion_table[$this->type])/$this->conversion_table[$type];
 }

 public function base_type(){
    return $this->base_type;
 }


}

现在您只需要几个<select>列表,其中选项值是要转换为/的各种类型,然后是一个<input>字段。您可以像这样使用它:

$converter = new RadiationConverter($_POST['convert_from']); 

// return just the number
echo $converter->amount($_POST['amount'])->convert_to($_POST['convert_to']);

// or return the number with the new type
echo $converter->amount($_POST['amount'])->display_as($_POST['convert_to']);

// you can include the amount in the construct if you want like this:
$converter = new RadiationConverter('rem', 100);
echo $converter->display_as('electrons');

如果您想在该网站上创建一个表格:

$display_amount = $_POST['amount'] . " " . $converter->display_name();

// just take the convert_from variable and cycle through all the types:
foreach($converter->types() as $type => $display) {
  echo  "$display_amount = " . $converter->display_as($type) . "<br />";
}

答案 1 :(得分:-2)

看起来它主要是javascript。您可以在页面上执行查看源,并查看他们使用的代码并为您的网站进行解释。