sot / kadi

Chandra commands and events
https://sot.github.io/kadi
BSD 3-Clause "New" or "Revised" License
5 stars 3 forks source link

Add starcat_date to observation parameters + fix caching and minor issues #221

Closed taldcroft closed 2 years ago

taldcroft commented 2 years ago

Description

Add starcat_date to the parameters stored in observations.

Other things along the way:

Also includes #225 and #226.

Fixes #220

Interface impacts

Running the 5.9.2 unit tests with the new data files gives 3 failures, all expected:

=================================================== short test summary info ====================================================
FAILED kadi/commands/tests/test_commands.py::test_commands_create_archive_regress[2] - AssertionError: assert False
FAILED kadi/commands/tests/test_commands.py::test_get_observations_by_obsid_single - AssertionError: assert [{'manvr_star...2...
FAILED kadi/commands/tests/test_commands.py::test_get_observations_by_obsid_multi - AssertionError: assert [{'manvr_star...00...
======================================== 3 failed, 50 passed, 103 deselected in 31.06s =========================================

Testing

Unit tests

Independent check of unit tests by Jean

Functional tests

Jean functionally confirmed the unique option to get_starcats_as_table with this query that gets the two catalogs (same catalog) of an ACIS undercover series.

In [5]: get_starcats_as_table(start='2020:366:17:04:20.000', stop='2020:366:23:00:00.000')
Out[5]: 
<Table length=22>
obsid      starcat_date      slot  idx      id    type  sz         mag          maxmag          yang                zang         dim   res  halfw
int64         str21         int64 int64   int64   str3 str3      float64       float64        float64             float64       int64 int64 int64
----- --------------------- ----- ----- --------- ---- ---- ----------------- --------- ------------------- ------------------- ----- ----- -----
24882 2020:366:17:04:20.000     0     1         1  FID  8x8               7.0       8.0 -1174.9607093061636  -468.1481719926585     1     1    25
24882 2020:366:17:04:20.000     1     2         3  FID  8x8               7.0       8.0 -1177.6232024219992   561.4018012757298     1     1    25
24882 2020:366:17:04:20.000     2     3         4  FID  8x8               7.0       8.0  1223.4393109616512   563.8018390366224     1     1    25
24882 2020:366:17:04:20.000     3     4 552075520  BOT  6x6 8.885130882263184 10.390625  -912.6106246537174  -703.8856678520224     8     1    60
24882 2020:366:17:04:20.000     4     5 552086952  BOT  6x6 8.897590637207031  10.40625 -1510.7731283644346  -2392.854408253782     8     1    60
24882 2020:366:17:04:20.000     5     6 552206400  BOT  6x6 8.648664474487305  10.15625  -84.90442138289295  -2367.635642290802    12     1    80
24882 2020:366:17:04:20.000     6     7 552206936  BOT  6x6 6.875301837921143     8.375  2104.7955129097486  1092.1642810692815    16     1   100
24882 2020:366:17:04:20.000     7     8 552213664  BOT  6x6  8.73717975616455 10.234375    1073.65803292352  -944.4668937469213    12     1    80
24882 2020:366:17:04:20.000     0     9 552217088  ACQ  6x6 8.083139419555664   9.59375  168.79558020035054 -2223.1544736547244    12     1    80
24882 2020:366:17:04:20.000     1    10 552081976  ACQ  6x6  9.25229549407959     10.75 -1094.5981762913334  1597.2393211928293     8     1    60
24882 2020:366:17:04:20.000     2    11 552084112  ACQ  6x6  9.63385009765625 11.140625  -782.1356873895911  -292.7544209467494     8     1    60
62647 2020:366:17:04:20.000     0     1         1  FID  8x8               7.0       8.0 -1174.9607093061636  -468.1481719926585     1     1    25
62647 2020:366:17:04:20.000     1     2         3  FID  8x8               7.0       8.0 -1177.6232024219992   561.4018012757298     1     1    25
62647 2020:366:17:04:20.000     2     3         4  FID  8x8               7.0       8.0  1223.4393109616512   563.8018390366224     1     1    25
62647 2020:366:17:04:20.000     3     4 552075520  BOT  6x6 8.885130882263184 10.390625  -912.6106246537174  -703.8856678520224     8     1    60
62647 2020:366:17:04:20.000     4     5 552086952  BOT  6x6 8.897590637207031  10.40625 -1510.7731283644346  -2392.854408253782     8     1    60
62647 2020:366:17:04:20.000     5     6 552206400  BOT  6x6 8.648664474487305  10.15625  -84.90442138289295  -2367.635642290802    12     1    80
62647 2020:366:17:04:20.000     6     7 552206936  BOT  6x6 6.875301837921143     8.375  2104.7955129097486  1092.1642810692815    16     1   100
62647 2020:366:17:04:20.000     7     8 552213664  BOT  6x6  8.73717975616455 10.234375    1073.65803292352  -944.4668937469213    12     1    80
62647 2020:366:17:04:20.000     0     9 552217088  ACQ  6x6 8.083139419555664   9.59375  168.79558020035054 -2223.1544736547244    12     1    80
62647 2020:366:17:04:20.000     1    10 552081976  ACQ  6x6  9.25229549407959     10.75 -1094.5981762913334  1597.2393211928293     8     1    60
62647 2020:366:17:04:20.000     2    11 552084112  ACQ  6x6  9.63385009765625 11.140625  -782.1356873895911  -292.7544209467494     8     1    60

In [6]: get_starcats_as_table(start='2020:366:17:04:20.000', stop='2020:366:23:00:00.000', unique=True)
Out[6]: 
<Table length=11>
obsid      starcat_date      slot  idx      id    type  sz         mag          maxmag          yang                zang         dim   res  halfw
int64         str21         int64 int64   int64   str3 str3      float64       float64        float64             float64       int64 int64 int64
----- --------------------- ----- ----- --------- ---- ---- ----------------- --------- ------------------- ------------------- ----- ----- -----
24882 2020:366:17:04:20.000     0     1         1  FID  8x8               7.0       8.0 -1174.9607093061636  -468.1481719926585     1     1    25
24882 2020:366:17:04:20.000     1     2         3  FID  8x8               7.0       8.0 -1177.6232024219992   561.4018012757298     1     1    25
24882 2020:366:17:04:20.000     2     3         4  FID  8x8               7.0       8.0  1223.4393109616512   563.8018390366224     1     1    25
24882 2020:366:17:04:20.000     3     4 552075520  BOT  6x6 8.885130882263184 10.390625  -912.6106246537174  -703.8856678520224     8     1    60
24882 2020:366:17:04:20.000     4     5 552086952  BOT  6x6 8.897590637207031  10.40625 -1510.7731283644346  -2392.854408253782     8     1    60
24882 2020:366:17:04:20.000     5     6 552206400  BOT  6x6 8.648664474487305  10.15625  -84.90442138289295  -2367.635642290802    12     1    80
24882 2020:366:17:04:20.000     6     7 552206936  BOT  6x6 6.875301837921143     8.375  2104.7955129097486  1092.1642810692815    16     1   100
24882 2020:366:17:04:20.000     7     8 552213664  BOT  6x6  8.73717975616455 10.234375    1073.65803292352  -944.4668937469213    12     1    80
24882 2020:366:17:04:20.000     0     9 552217088  ACQ  6x6 8.083139419555664   9.59375  168.79558020035054 -2223.1544736547244    12     1    80
24882 2020:366:17:04:20.000     1    10 552081976  ACQ  6x6  9.25229549407959     10.75 -1094.5981762913334  1597.2393211928293     8     1    60
24882 2020:366:17:04:20.000     2    11 552084112  ACQ  6x6  9.63385009765625 11.140625  -782.1356873895911  -292.7544209467494     8     1    60

Tom generated new commands archive data files.

taldcroft commented 2 years ago

Although the updated data files are compatible with the code, some unit tests in the release will fail with the new files. For testing I put updated files in ~aldcroft/tmp/kadi/ on HEAD.

javierggt commented 2 years ago

Also, it is not clear to me how this is supposed to replace the use of timelines in the agasc supplement (this might not be related to this P, so perhaps we discuss it elsewhere). In the agasc supplement one needs a table of star-observations, which in an ideal world be something like:

observations = Table(commands.get_observations())
catalogs = commands.get_starcats()
join(observations, starcats, keys=['obsid'])

That does not work exactly, because this is a list of ACATable. I suppose I can iterate over these, creating a table, adding obsid column, vstack and then join. Still, just pointing out that I'd rather have a single table.

taldcroft commented 2 years ago

@javierggt - for testing you need to copy the new cmds2.* files I referenced above into a local directory and then point kadi to them via export KADI=<local-kadi-dir>. If you are using events then you need to link/copy events3.db3 as well.

You might have noticed that it seems to work for recent obsids, which is because those are being created in memory on-the-fly and thus had the starcat_date key included.

javierggt commented 2 years ago

I started thinking how to make functional tests. For that purpose I created a branch of agasc that includes a function get_star_observations in agasc.supplement.magnitudes.star_obs_catalogs. See here.

Using the commands-v2 branch of agasc and the branch in this PR, I did the following:

from agasc.supplement.magnitudes import star_obs_catalogs
star_obs_catalogs.load()
stars_obs_v1 = star_obs_catalogs.STARS_OBS
stars_obs_v2 = star_obs_catalogs.get_star_observations()

These star-observations should be equivalent or close enough... A simple comparison of OBSIDs and agasc IDs in both:

len(stars_obs_v1): 207219
len(stars_obs_v2): 376708

len(obsid_v1): 32754
len(obsid_v2): 34617
+---------------------|-----------------|---------------------+
|                     | in obsid_v1     | not in obsid_v1     |
+---------------------|-----------------|---------------------+
|     in obsid_v2     |           32584 |                2033 |
| not in obsid_v2     |             170 |                     |
+---------------------|-----------------|---------------------+

len(agasc_id_v1): 87587
len(agasc_id_v2): 90263
+---------------------|-----------------|---------------------+
|                     | in agasc_id_v1  | not in agasc_id_v1  |
+---------------------|-----------------|---------------------+
|     in agasc_id_v2  |           87561 |                2702 |
| not in agasc_id_v2  |              26 |                     |
+---------------------|-----------------|---------------------+

I conclude that the code runs and the starcat_date seem to agree. The commands-v2 version seems to produce more star-observations, which is not specific to this PR.

NOTE: these numbers seem not right to me yet. I am taking a closer look and will update once I know better.

javierggt commented 2 years ago

Independently of the numbers above, when I run the following:

from astropy.table import Table
from kadi import commands
commands.conf.commands_version = '2'
observations = Table(commands.get_observations())
np.count_nonzero(observations['starcat_date'].mask)

I see 836 observations out of 40232 that have no starcat_date. Would that be expected?

taldcroft commented 2 years ago

I see 836 observations out of 40232 that have no starcat_date. Would that be expected?

This seems reasonable to me. There are normal reasons for this to happen:

It's good to look into these corner cases to find problems. I suspect that with deep digging that there are some oddball cases that are just wrong, but investigating many of them is very time consuming.

One thing is this:

(ska3) ➜  ~ find ~/ska/data/mpcrit1/mplogs/*/*/ofls/ -name 'starcheck.txt' | xargs grep 'No star catalog for obsid' | wc
     993   13527  142509

So the 836 you found seems in the right ballpark. I suspect my grep includes obsids that were never run, hence the overcount.

taldcroft commented 2 years ago

I just updated the description to note that #225 and #226 are included.

taldcroft commented 2 years ago

@javierggt - I did the comparison of the AGASC star observations to what comes from kadi commands v2 and found not too much actual difference. This comparison uses the MP_STARCAT time as the key. There is a lot of difference in obsids because AGASC uses the originally commanded obsid while kadi uses the as-run obsid, which is different after SCS-107 runs. The takeaway is reinforcing that obsid is not useful in this context since there are different possible definitions.

Also, kadi starts around 2002:001 while AGASC starts in 2003:001. That 2003:001 was arbitrary and conservative, but I think we can push it back to the 2002 date in kadi.

See https://gist.github.com/taldcroft/d870443430466a96a8d5b2cbe5c1faf3. I find just 8 star catalogs that are in kadi v2 and not in AGASC, and zero the other way round.

jeanconn commented 2 years ago

Regarding the 836 without star catalogs, they look to all have npnt_enab == False, which at least seems consistent. Regarding

Segmented maneuvers, which I'm guessing is represented here: https://icxc.harvard.edu/mp/mplogs/2019/DEC0219/oflsa/starcheck.html#obsid47655

Those were actually planned gyro holds (I'm forgetting why but from the load review notes)

· 3 Gyro Holds comprised of 5 attitudes each of which has an earth boresight violation. They start at approximately: o 2019:335:22:00 o 2019:338:13:00 o 2019:341:04:30

I more strict "segmented maneuver" can be seen at something like:

https://icxc.harvard.edu/mp/mplogs/2002/APR0302/oflsc/starcheck.html#obsid2118

And 2118 has what I would expect in the kadi observations (one no-starcat observation, one starcat observation, each with right attitude and timing that seems reasonable).

taldcroft commented 2 years ago

I rebased to fix the merge conflict. This was just silly, I had accidentally put an empty line in states.py and this ended up getting committed.

taldcroft commented 2 years ago

Thanks again for the reviews!