Function distance_converter,data_in,unit_in=unit_in,unit_out=unit_out
;+
;NAME:
; distance_converter
;PURPOSE:
; converter distance between different unit
;CALLING SEQUENCE:
; result=distance_converter(data,unit_in=unit_in,unit_out=unit_out)
;INPUT:
; data_in --- input data, scalar or vector
;OPTIONAL KEYWORD INPUT:
; unit_in --- Unit of input data, string scalar
;OUTPUT:
; data_out --- output data, scalar or vector
;OPTIONAL KEYWORD OUTPUT:
; unit_out --- Unit of output data, string scalar
;
;EXAMPLE:
; IDL> print,distance_converter(1.0,unit_in='pc',unit_out='cm')
; 3.0856100e+18
; IDL> print,distance_converter(1.0,unit_in='pc',unit_out='m')
; 3.0856100e+16
; IDL> distance_converter(1.0,unit_in='pc',unit_out='AU')
; 206260.26
; IDL> print,distance_converter(1.0,unit_in='ly',unit_out='AU')
; 63239.527
;
;REVISION HISTORY:
; Original by DL.Wang,Aug-31-2007
;-
;###########################
;#### Fundmental Unit
Astronomical_unit = 1.49597892D+13 ;cm
Parsec = 3.08561D+18 ;cm
Light_year = 9.4605D+17 ;cm
;###################################
;####### Unit converter
cm_cm = 1.0D+0
cm_au = 1.0D+0/Astronomical_unit
cm_A = 1.0D+8
cm_m = 1.0D-2
cm_pc = 1.0D+0/Parsec
cm_kpc = 1.0D-3/Parsec
cm_mpc = 1.0D-6/Parsec
cm_ly = 1.0D+0/Light_year
au_cm = Astronomical_unit
A_cm = 1.0D-8
m_cm = 1.0D+2
pc_cm = Parsec
kpc_cm = 1.0D+3*Parsec
mpc_cm = 1.0D+6*Parsec
ly_cm = Light_year
;########################################
;##### distance converter #######
;########################################
;
;converter input unit to cm
;
case unit_in of
'cm':data_in=data_in*cm_cm
'm':data_in=data_in*m_cm
'A':data_in=data_in*A_cm
'AU':data_in=data_in*au_cm
'pc':data_in=data_in*pc_cm
'kpc':data_in=data_in*kpc_cm
'Mpc':data_in=data_in*mpc_cm
'ly':data_in=data_in*ly_cm
endcase
;
;converter cm to out unit
;
case unit_out of
'cm':data_out=data_in*cm_cm
'm':data_out=data_in*cm_m
'A':data_out=data_in*cm_A
'AU':data_out=data_in*cm_au
'pc':data_out=data_in*cm_pc
'kpc':data_out=data_in*cm_kpc
'Mpc':data_out=data_in*cm_mpc
'ly':data_out=data_in*cm_ly
endcase
return,data_out
End
https://blog.sciencenet.cn/blog-456360-658888.html
上一篇:
能量不同单位表达的转化(IDL程序)下一篇:
用x射线的积分流量计算单色流量(IDL程序)