开发者

Radiation level converter

开发者 https://www.devze.com 2023-03-04 10:03 出处:网络
I\'m working on something, I want to 开发者_Python百科be able to have a converter like this website: Radiation - Dose Equivalent - Sievert Conversion Factors unfortunately there is no source on this k

I'm working on something, I want to 开发者_Python百科be able to have a converter like this website: Radiation - Dose Equivalent - Sievert Conversion Factors unfortunately there is no source on this kind of converter and I'm in the need of this.

Could somebody point me in the right direction or provide a source code of this radiation unit converter.

I'm willing to give 350 bounty points for a correct and perfect source once I have found a correct answer (when a few days has passed that allows me to set a bounty).

Thank you.

EDIT:

Since now a bounty has been started. I need this script to be coded in PHP that will allow to convert inputted values to another radiation unit by selection.


The easiest way to manage conversions between multiple formats like this is to just maintain a table of conversions for one type, and do a two-part conversion to get the desired format. There is some loss of precision in doing this, but you really should have plenty of precision for the needs of something like this (you can always increase the precision by adding some more digits to the roentgen type).

There are many ways to do this, but I created a RadiationConverter class to keep everything tidy, and I did a bit of chaining just to keep it clean. You get the idea, though.

<?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;
 }


}

Now all you need is a couple <select> lists with option values being the various types to convert from/to, then an <input> field for an amount. You could use it like this:

$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');

If you want to create a table like on that site:

$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 />";
}


It looks like it's mostly javascript. You can do a view source on the page and see the code that they used and interpret for your site.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号