util.py 7.23 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

18
import astropy.units as u
Carl Schaffer's avatar
Carl Schaffer committed
19
import numpy as np
20
import pandas as pd
21
22
23
import pytz
from astroplan import Observer
from astropy.coordinates import SkyCoord, GCRS, get_sun
Carl Schaffer's avatar
Carl Schaffer committed
24
from astropy.io.fits.hdu.hdulist import fitsopen as fitsopen
25
from sunpy.coordinates import Helioprojective
Carl Schaffer's avatar
Carl Schaffer committed
26

27
from kis_tools.util.calculations import rotate_around_origin
28
from kis_tools.util.constants import gregor_coords
Carl Schaffer's avatar
Carl Schaffer committed
29
from kis_tools.util.util import gris_run_number
30
31
32
33
34
35
36


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


def get_candidates(logfile):
Carl Schaffer's avatar
Carl Schaffer committed
37
    with open(logfile, "r") as f:
38
        text = f.read().strip()
Carl Schaffer's avatar
Carl Schaffer committed
39
    text = "\n".join(text.split("\n")[:5])
40
41
42

    observer_parts = []
    patterns = [
Carl Schaffer's avatar
Carl Schaffer committed
43
44
        r"[a-zA-Z\-]*[ \t\r\f\v]*(?:observer|remote)+\w*[ \t\r\f\v]*:?\s+(.+)\n",
        r"(?:\n|^)*\s*([a-z ,\.]+)\n",
45
46
47
48
49
50
51
52
53
    ]

    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
54
    return ",".join(observer_parts)
55
56
57
58


def extract_names(obs_candidates):
    # check for Capitalized names
Carl Schaffer's avatar
Carl Schaffer committed
59
60
    obs_candidates.replace(" and ", ",")
    observers = [c for c in obs_candidates.split(",") if re.search(r"[A-Z][a-z]", c)]
61
62
63
64
    return observers


def observer_data_frame():
Carl Schaffer's avatar
Carl Schaffer committed
65
66
    files = glob("/dat/sdc/gris/*/*.txt")
    logfiles = list(filter(lambda x: re.match(r"\d{8}\.txt", basename(x)), files))
67
68
69
70
71

    observers = []
    dates = []

    for f in logfiles:
Carl Schaffer's avatar
Carl Schaffer committed
72
73
        observers.append(",".join(get_observers(f)))
        date = datetime.datetime.strptime(basename(f), "%Y%m%d.txt")
74
75
76
        dates.append(date)

    df = pd.DataFrame(dict(date=dates, observers=observers))
Carl Schaffer's avatar
Carl Schaffer committed
77
    df = df.set_index("date")
78
79

    print(df.head())
Carl Schaffer's avatar
Carl Schaffer committed
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
139
140
141
142
143
144
145


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
146
    trigger_kw = "ISTEP"
Carl Schaffer's avatar
Carl Schaffer committed
147
    step = 0
Carl Schaffer's avatar
Carl Schaffer committed
148
    for key, value in chain(*[h.items() for h in headers]):
Carl Schaffer's avatar
Carl Schaffer committed
149
150
        if str(value).strip() == '' and key in env.keys():
            value = env[key]
Carl Schaffer's avatar
Carl Schaffer committed
151
        env[key] = value
Carl Schaffer's avatar
Carl Schaffer committed
152
153
        next_map = key == 'ISERIE' and value == 2
        if next_map:
Carl Schaffer's avatar
Carl Schaffer committed
154
155
            # Stop iteration after first map
            break
Carl Schaffer's avatar
Carl Schaffer committed
156
        if key == trigger_kw and value != 1:
Carl Schaffer's avatar
Carl Schaffer committed
157
158
            if pos_infos is None:
                pos_infos = [None] * env["STEPS"]
Carl Schaffer's avatar
Carl Schaffer committed
159
160
161
            pos_infos[step] = info_from_env(env)
            step += 1
    pos_infos[step] = info_from_env(env)
Carl Schaffer's avatar
Carl Schaffer committed
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
    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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214


def gregor_parallactic_angle(time, hpc_x=None, hpc_y=None):
    """
    Calculates parallactic angle for GREGOR based on a time and helioprojective coordinates on the sun.
    Args:
        time: time of observation
        hpc_x: helioprojective x (defaults to sun center)
        hpc_y: helioprojective y (defaults to sun center)

    Returns:
        angle: parallactic angle in degrees

    """
    # Define observer and add timezone (assume tenerife time)
    gregor_observer = Observer(location=gregor_coords)
    date_tz = pytz.utc.localize(time)
    time = gregor_observer.datetime_to_astropy_time(date_tz)

    # Build Sky Coord object, default to sun center at time
    if hpc_x and hpc_y:
        sun_coords = SkyCoord(hpc_x * u.arcsec, hpc_y * u.arcsec, frame=Helioprojective, observer='earth', obstime=time)
        sun_coords.representation_type = 'spherical'
        sun_coords = sun_coords.transform_to(GCRS)
    else:
        sun_coords = get_sun(time)

    angle = gregor_observer.parallactic_angle(time, sun_coords)
    angle = angle.to('degree').value
    return angle
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234


def telescope_to_hpc(slitposx=None, slitposy=None, p0_angle=None, xcenter=None, ycenter=None):
    """
    Transform GRIS header coordinate data to HPC coordinates
    Args:
        slitposx: float SLITPOSX in arcsec
        slitposy: float SLITPOSY in arcsec
        p0_angle:  float p0_angle in degrees
        xcenter: float X_center in arcsec (stored in GDBS upon centering the telescope)
        ycenter: float Y_center in arcsec (stored in GDBS upon centering the telescope)

    Returns:
        hpc_x, hpc_y tuple of helioprojective coordinates in arcseconds

    """
    xi = slitposx - xcenter
    yi = slitposy - ycenter
    rotated = rotate_around_origin((xi, yi), np.radians(p0_angle))
    return rotated