You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: episodes/06-units_and_quantities.md
+35-4Lines changed: 35 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -55,6 +55,9 @@ When printing the variable the unit information will now be attached:
55
55
```python
56
56
print(length)
57
57
```
58
+
```output
59
+
26.2 m
60
+
```
58
61
59
62
The type of this new variable is an `astropy``Quantity`:
60
63
@@ -93,26 +96,39 @@ distance_end = 23 * u.km
93
96
length = distance_end - distance_start
94
97
print(length)
95
98
```
99
+
```output
100
+
19.99999 km
101
+
```
96
102
97
103
And it also enables the combining of quantities, for example, to calculate a speed:
98
104
99
105
```python
106
+
distance =45* u.km
100
107
time =15* u.minute
101
-
speed =length/ time
108
+
speed =distance/ time
102
109
print(speed)
103
110
```
111
+
```output
112
+
3.0 km / min
113
+
```
104
114
105
115
By default the units library will select units to report for these values based on what the units are of the objects that you have passed it. You can, as before, convert these to the units of your choice:
106
116
107
117
```python
108
118
print(speed.to(u.km/u.s))
109
119
```
120
+
```output
121
+
0.05 km / s
122
+
```
110
123
111
124
You can also convert the units to the base (irreducible) units for the unit system you are using with the `decompose` function (changing the unit system choice will be covered later):
112
125
113
126
```python
114
127
print(speed.decompose())
115
128
```
129
+
```output
130
+
50.0 m / s
131
+
```
116
132
117
133
::::::::::::::::::::::::::::::::::::::::: callout
118
134
@@ -121,10 +137,13 @@ print(speed.decompose())
121
137
If you wish to reduce to different base units you can pass a list of those units to the `bases` parameter when calling the `decompose` function:
122
138
123
139
```python
124
-
print(speed.decompose(bases=['km','s']))
140
+
print(speed.decompose(bases=["km", "s"]))
141
+
```
142
+
```output
143
+
0.05 km / s
125
144
```
126
145
127
-
Note that the order of the values in the list passed to `bases` parameter doesn't matter. However, the base units you choose must be either one of the original units used, or a standard base unit. This function cannot be used to convert from km to cm, for example. Instead it is useful where you only want to reduce select units within the object.
146
+
Note that the order of the values in the list passed to the `bases` parameter doesn't matter. However, the base units you choose must be either one of the original units used, or a standard base unit. This function cannot be used to convert from km to cm, for example. Instead it is useful where you only want to reduce select units within the object.
In this case we use the `spectral` equivalence library, which allows conversions between wavelengths, wave number, frequency, and energy equivalent pairs. There are a number of other built-in equivalency libraries, for commonly used pairings.
0 commit comments