util.py 5.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Created by schaffer at 11/6/19

Collection of utility functions specific to GRIS
"""

import datetime
import re
Carl Schaffer's avatar
Carl Schaffer committed
12
from collections import namedtuple
13
from glob import glob
Carl Schaffer's avatar
Carl Schaffer committed
14
from itertools import chain
15
from os.path import basename
Carl Schaffer's avatar
Carl Schaffer committed
16
from pathlib import Path
17

Carl Schaffer's avatar
Carl Schaffer committed
18
import numpy as np
19
import pandas as pd
Carl Schaffer's avatar
Carl Schaffer committed
20
21
22
from astropy.io.fits.hdu.hdulist import fitsopen as fitsopen

from kis_tools.util.util import gris_run_number
23
24
25
26
27
28
29


def get_observers(logfile):
    return extract_names(get_candidates(logfile))


def get_candidates(logfile):
Carl Schaffer's avatar
Carl Schaffer committed
30
    with open(logfile, "r") as f:
31
        text = f.read().strip()
Carl Schaffer's avatar
Carl Schaffer committed
32
    text = "\n".join(text.split("\n")[:5])
33
34
35

    observer_parts = []
    patterns = [
Carl Schaffer's avatar
Carl Schaffer committed
36
37
        r"[a-zA-Z\-]*[ \t\r\f\v]*(?:observer|remote)+\w*[ \t\r\f\v]*:?\s+(.+)\n",
        r"(?:\n|^)*\s*([a-z ,\.]+)\n",
38
39
40
41
42
43
44
45
46
    ]

    for p in patterns:
        res = re.findall(p, text, re.IGNORECASE)
        if res:
            for r in res:
                observer_parts.append(r)
            break

Carl Schaffer's avatar
Carl Schaffer committed
47
    return ",".join(observer_parts)
48
49
50
51


def extract_names(obs_candidates):
    # check for Capitalized names
Carl Schaffer's avatar
Carl Schaffer committed
52
53
    obs_candidates.replace(" and ", ",")
    observers = [c for c in obs_candidates.split(",") if re.search(r"[A-Z][a-z]", c)]
54
55
56
57
    return observers


def observer_data_frame():
Carl Schaffer's avatar
Carl Schaffer committed
58
59
    files = glob("/dat/sdc/gris/*/*.txt")
    logfiles = list(filter(lambda x: re.match(r"\d{8}\.txt", basename(x)), files))
60
61
62
63
64

    observers = []
    dates = []

    for f in logfiles:
Carl Schaffer's avatar
Carl Schaffer committed
65
66
        observers.append(",".join(get_observers(f)))
        date = datetime.datetime.strptime(basename(f), "%Y%m%d.txt")
67
68
69
        dates.append(date)

    df = pd.DataFrame(dict(date=dates, observers=observers))
Carl Schaffer's avatar
Carl Schaffer committed
70
    df = df.set_index("date")
71
72

    print(df.head())
Carl Schaffer's avatar
Carl Schaffer committed
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138


def extract_position_kws_from_manolo(raw_gris_file):
    """
    Extract coordinate information from an observation, possibly spliced into multiple gris files.
    Extracts date and run information from the filename, searches for files of the same observation
    and determines coordinate info. Coordinate info is collected for every step of the first map found.
    The keywords are averaged across the map, resulting in the coordinates of the map center.

    Args:
        raw_gris_file: path or stub of gris file name like 14apr20.001[-01][cc|r]

    Returns:
        pos: dictionary with member access to position values
    """
    # get coord info and bounding box
    folder = Path(raw_gris_file).parent
    assert folder.exists(), f"Input folder {folder} not found!"

    runnumber = gris_run_number(raw_gris_file)

    pattern = f"*.{runnumber:03d}*"
    files = [f for f in folder.glob(pattern) if not str(f).endswith('m')]
    print(files)

    headers = []
    for f in sorted(files):
        with fitsopen(f) as hdu:
            hdu[0].verify("silentfix")
            headers.append(hdu[0].header)

    assert headers, f"Empty list of headers, are there files in {str(folder)}? Pattern was: '{pattern}'."

    pos_kws = [
        "IFILE",
        "SLITORIE",
        "SLITPOSX",
        "SLITPOSY",
        "SLITLATI",
        "SLITLONG",
        "B0ANGLE",
        "P0ANGLE",
        "PARAANGL",
        "AZIMUT",
        "ELEVATIO",
        "L0ANGLE",
        "ROTANGLE",
        "ROTCODE",
        "FILENAME",
        "RA",
        "DEC",
    ]
    PositionInfo = namedtuple("PositionInfo", field_names=pos_kws)

    def info_from_env(env):
        # default for rotcode and angle, they were added in 2016
        defaults = dict(
            ROTCODE=1,
            ROTANGLE=0,
        )

        kwargs = {**defaults, **{k: env[k] for k in pos_kws if k in env}}
        return PositionInfo(**kwargs)

    env = {}
    pos_infos = None
Carl Schaffer's avatar
Carl Schaffer committed
139
    trigger_kw = "ISTEP"
Carl Schaffer's avatar
Carl Schaffer committed
140
    for key, value in chain(*[h.items() for h in headers]):
Carl Schaffer's avatar
Carl Schaffer committed
141
142
        if str(value).strip() == '' and key in env.keys():
            value = env[key]
Carl Schaffer's avatar
Carl Schaffer committed
143
        env[key] = value
Carl Schaffer's avatar
Carl Schaffer committed
144
145
        next_map = key == 'ISERIE' and value == 2
        if next_map:
Carl Schaffer's avatar
Carl Schaffer committed
146
147
            # Stop iteration after first map
            break
Carl Schaffer's avatar
Carl Schaffer committed
148
        if key == trigger_kw and value != 1:
Carl Schaffer's avatar
Carl Schaffer committed
149
150
            if pos_infos is None:
                pos_infos = [None] * env["STEPS"]
Carl Schaffer's avatar
Carl Schaffer committed
151
152
            pos_infos[env["ISTEP"] - 1] = info_from_env(env)
    pos_infos[env["ISTEP"] - 1] = info_from_env(env)
Carl Schaffer's avatar
Carl Schaffer committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    pos_infos = np.array(pos_infos)
    # coords map 1
    indexes = {k: i for i, k in enumerate(pos_kws)}
    pos = dict(
        slitposx=pos_infos[:, indexes["SLITPOSX"]].astype(float).mean(),
        slitposy=pos_infos[:, indexes["SLITPOSY"]].astype(float).mean(),

        slitlati=pos_infos[:, indexes["SLITLATI"]].astype(float).mean(),
        slitlong=pos_infos[:, indexes["SLITLONG"]].astype(float).mean(),

        slitorie=pos_infos[:, indexes["SLITORIE"]].astype(float).mean(),

        l0_angle=pos_infos[:, indexes["L0ANGLE"]].astype(float).mean(),
        b0_angle=pos_infos[:, indexes["B0ANGLE"]].astype(float).mean(),
        p0_angle=pos_infos[:, indexes["P0ANGLE"]].astype(float).mean(),

        parallax_angle=pos_infos[:, indexes["PARAANGL"]].astype(float).mean(),
        azimuth=pos_infos[:, indexes["AZIMUT"]].astype(float).mean(),
        elevation=pos_infos[:, indexes["ELEVATIO"]].astype(float).mean(),
        date_beg=headers[0]["DATE-OBS"] + " " + headers[0]["UT"]
    )

    return pos