From 5dc92fc278290efb5f217958962ae87461b3b17f Mon Sep 17 00:00:00 2001 From: Martin Raspaud Date: Mon, 18 Mar 2024 14:41:55 +0100 Subject: [PATCH] Allow overriding which header to use for pod --- pygac/pod_reader.py | 35 +++++++++++++++++++---------------- pygac/reader.py | 3 ++- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/pygac/pod_reader.py b/pygac/pod_reader.py index 5026543..1919a14 100644 --- a/pygac/pod_reader.py +++ b/pygac/pod_reader.py @@ -279,7 +279,7 @@ def read(self, filename, fileobj=None): # choose the right header depending on the date with file_opener(fileobj or filename) as fd_: self.tbm_head, self.head = self.read_header( - filename, fileobj=fd_) + filename, fileobj=fd_, eosip_header=self.eosip_header) if self.tbm_head: tbm_offset = tbm_header.itemsize else: @@ -302,12 +302,13 @@ def read(self, filename, fileobj=None): return self.head, self.scans @classmethod - def read_header(cls, filename, fileobj=None): + def read_header(cls, filename, fileobj=None, eosip_header=False): """Read the file header. Args: filename (str): Path to GAC/LAC file fileobj: An open file object to read from. (optional) + eosip_header: if the format to read is eosip, use only the latest header format, independently of the date. Returns: archive_header (struct): archive header @@ -332,20 +333,22 @@ def read_header(cls, filename, fileobj=None): fd_.seek(0) tbm_head = None tbm_offset = 0 - # read header - head0, = np.frombuffer( - fd_.read(header0.itemsize), - dtype=header0, count=1) - year, jday, _ = cls.decode_timestamps(head0["start_time"]) - start_date = (datetime.date(year, 1, 1) + - datetime.timedelta(days=int(jday) - 1)) - if start_date < datetime.date(1992, 9, 8): - header = header1 - elif start_date <= datetime.date(1994, 11, 15): - header = header2 - else: - header = header3 - fd_.seek(tbm_offset, 0) + header = header3 + if not eosip_header: + # choose the appropriate header + head0, = np.frombuffer( + fd_.read(header0.itemsize), + dtype=header0, count=1) + year, jday, _ = cls.decode_timestamps(head0["start_time"]) + start_date = (datetime.date(year, 1, 1) + + datetime.timedelta(days=int(jday) - 1)) + if start_date < datetime.date(1992, 9, 8): + header = header1 + elif start_date <= datetime.date(1994, 11, 15): + header = header2 + else: + header = header3 + fd_.seek(tbm_offset, 0) # need to copy frombuffer to have write access on head head, = np.frombuffer( fd_.read(header.itemsize), diff --git a/pygac/reader.py b/pygac/reader.py index fefac28..caea7fc 100644 --- a/pygac/reader.py +++ b/pygac/reader.py @@ -95,7 +95,7 @@ class Reader(six.with_metaclass(ABCMeta)): def __init__(self, interpolate_coords=True, adjust_clock_drift=True, tle_dir=None, tle_name=None, tle_thresh=7, creation_site=None, - custom_calibration=None, calibration_file=None): + custom_calibration=None, calibration_file=None, eosip_header=False): """Init the reader. Args: @@ -122,6 +122,7 @@ def __init__(self, interpolate_coords=True, adjust_clock_drift=True, self.creation_site = (creation_site or 'NSS').encode('utf-8') self.custom_calibration = custom_calibration self.calibration_file = calibration_file + self.eosip_header = eosip_header self.head = None self.scans = None self.spacecraft_name = None