forked from nanograv/PINT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkingWithFlags.py
More file actions
116 lines (86 loc) · 2.46 KB
/
WorkingWithFlags.py
File metadata and controls
116 lines (86 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.14.7
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %% [markdown]
# # Working With TOA Flags
#
# ``PINT`` provides methods for working conveniently with TOA flags. You can add, delete, and modify flags, and use them to select TOAs.
#
# %%
from pint.toa import get_TOAs
import pint.config
import pint.logging
# %%
pint.logging.setup("WARNING")
# %% [markdown]
# Get a test dataset. This file has no flags to start with.
# %%
t = get_TOAs(pint.config.examplefile("NGC6440E.tim"), ephem="DE440")
print(t)
# %% [markdown]
# The TOAs are stored internally as an ``astropy.Table``:
# %%
print(t.table)
# %% [markdown]
# You can look at the flags directly (note that the leading ``-`` from a file has been stripped for internal storage). The initial file did not have flags, so these (``format``, ``ddm``, and ``clkcorr``) were added by ``PINT`` when reading:
# %%
print(t["flags"])
# %% [markdown]
# Which is just looking at the ``'flags'`` column in the TOA table:
# %%
print(t.table["flags"])
# %% [markdown]
# To extract the values of a flag, you can just treat it like an array slice
# %%
print(t["ddm"])
# %% [markdown]
# However, flags are stored as strings, so you might use a function that also allows type conversions:
# %%
ddm, _ = t.get_flag_value("ddm", as_type=float)
print(ddm)
# %% [markdown]
# It's also easy to add flags. In this case we will just add to the first 10 TOAs:
# %%
t[:10, "fish"] = "carp"
print(t["flags"])
# %% [markdown]
# If we now try to get those values:
# %%
print(t["fish"])
# %% [markdown]
# it will return the flag value or an empty string when it is not set. On the other hand:
# %%
fish, idx = t.get_flag_value("fish")
print(fish)
print(idx)
# %% [markdown]
# Will also return an array of indices when the flag is set.
# Now let's set some other fish:
# %%
t[10:15, "fish"] = "bass"
# %% [markdown]
# and we can select only the TOAs that meet some criteria:
# %%
basstoas = t[t["fish"] == "bass"]
print(len(basstoas))
# %% [markdown]
# Which can be combined with other selection criteria:
# %%
print(t[(t["fish"] == "carp") & (t["mjd_float"] > 53679)])
# %% [markdown]
# You can set the value of a flag to an empty string, which will delete it:
# %%
t["fish"] = ""
print(t["flags"])
# %%