Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2024/11/23 14:01:49.216 (台灣標準時)

Pint モジュールを使った単位を含む数値の取り扱い

Astropy にも似たような機能があるが、 Pint という Python モジュールにも、 単位を含む数値を使った計算や単位の変換ができるようだ。

使い方の一例。

#!/usr/pkg/bin/python3.9

# Time-stamp: <2022/04/10 10:39:00 (CST) daisuke>

import pint

#
# units
#

ur       = pint.UnitRegistry ()
u_deg    = ur.degree
u_rad    = ur.radian
u_arcmin = ur.arcmin
u_arcsec = ur.arcsec
u_mas    = ur.mas
u_pc     = ur.pc
u_au     = ur.au
u_m      = ur.metre

# angle a

angle_a = 180.0 * u_deg
angle_a_rad = angle_a.to (u_rad)
print ("angle_a = %s = %s" % (angle_a, angle_a_rad) )

# angle b

angle_b        = 1.0 * u_deg
angle_b_deg    = angle_b.to (u_deg)
angle_b_arcmin = angle_b.to (u_arcmin)
angle_b_arcsec = angle_b.to (u_arcsec)
print ("angle_b = %s = %s = %s" % (angle_b, angle_b_arcmin, angle_b_arcsec) )

# distance to Sirius
parallax = 374.5 * u_mas
distance = 1.0 / parallax.to (u_arcsec) * u_pc * u_arcsec
print ("distance to Sirius = %s" % distance)
print ("                   = %s" % distance.to (u_au) )
print ("                   = %s" % distance.to (u_m) )

実行結果は以下の通り。

% ./pint_00.py
angle_a = 180.0 degree = 3.141592653589793 radian
angle_b = 1.0 degree = 60.0 arcminute = 3599.9999999999995 arcsecond
distance to Sirius = 2.67022696929239 parsec
                   = 550773.8484525508 astronomical_unit
                   = 8.239459496574611e+16 meter

fig_202204/unit_0.png
fig_202204/unit_1.png



Frequently accessed files

  1. Computer___Python/20220518_0.html
  2. Computer___Network/20230726_00.html
  3. Misc___Taiwan/20240207_00.html
  4. Computer___Network/20230516_00.html
  5. Computer___FreeBSD/20220621_0.html
  6. Computer___Python/20220715_0.html
  7. Computer___Network/20230508_00.html
  8. Food___Taiwan/20220429_0.html
  9. Computer___NetBSD/20220817_3.html
  10. Computer___Python/20220410_0.html
  11. Computer___Network/20240416_00.html
  12. Computer___Network/20240130_00.html
  13. Computer___Debian/20210223_1.html
  14. Computer___NetBSD/20230119_00.html
  15. Computer___Python/20210124_0.html
  16. Computer___Python/20221013_0.html
  17. Computer___NetBSD/20220818_1.html
  18. Computer___NetBSD/20220428_0.html
  19. Science___Math/20220420_0.html
  20. Computer___NetBSD/20240101_02.html
  21. Computer___NetBSD/20220808_0.html
  22. Computer___TeX/20230503_00.html
  23. Computer___NetBSD/20230515_00.html
  24. Science___Astronomy/20220503_0.html
  25. Computer___NetBSD/20210127_0.html
  26. Computer___Python/20240101_00.html
  27. Computer___Network/20220413_1.html
  28. Computer___Python/20220816_1.html
  29. Computer___NetBSD/20210204_0.html
  30. Travel___Taiwan/20220809_2.html


HTML file generated by Kinoshita Daisuke.