Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add meta_data dictionary to reader class. #41

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pygac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import logging
import os
import numpy as np

from pygac.version import __version__ # noqa

Expand Down Expand Up @@ -52,3 +53,18 @@ def centered_modulus(array, divisor):
arr = array % divisor
arr[arr > divisor / 2] -= divisor
return arr


def calculate_sun_earth_distance_correction(jday):
"""Calculate the sun earth distance correction.

In 2008 3-4 different equations of ESD were considered.
This one was chosen as it at the time gave reflectances most closely
matching the PATMOS-x data provided then by Andy Heidinger.

Formula might need to be reconsidered if jday is updated to a float.

"""
# Earth-Sun distance correction factor
corr = 1.0 - 0.0334 * np.cos(2.0 * np.pi * (jday - 2) / 365.25)
return corr
10 changes: 5 additions & 5 deletions pygac/gac_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ def save_gac(satellite_name,
bt3, bt4, bt5,
sun_zen, sat_zen, sun_azi, sat_azi, rel_azi,
qual_flags, start_line, end_line,
gac_file, midnight_scanline, miss_lines):
gac_file, meta_data):

midnight_scanline = meta_data['midnight_scanline']
miss_lines = meta_data['missing_scanlines']
corr = meta_data['sun_earth_distance_correction_factor']

last_scan_line_number = qual_flags[-1, 0]

Expand Down Expand Up @@ -199,10 +203,6 @@ def save_gac(satellite_name,
starttime = start.strftime("%H%M%S%f")[:-5]
enddate = end.strftime("%Y%m%d")
endtime = end.strftime("%H%M%S%f")[:-5]
jday = int(start.strftime("%j"))

# Earth-Sun distance correction factor
corr = 1.0 - 0.0334 * np.cos(2.0 * np.pi * (jday - 2) / 365.25)

# Apply scaling & offset
bt3 -= 273.15
Expand Down
3 changes: 1 addition & 2 deletions pygac/gac_klm.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,7 @@ def main(filename, start_line, end_line):
sun_zen, sat_zen, sun_azi, sat_azi, rel_azi,
qual_flags, start_line, end_line,
reader.filename,
reader.get_midnight_scanline(),
reader.get_miss_lines())
reader.meta_data)
LOG.info("pygac took: %s", str(datetime.datetime.now() - tic))


Expand Down
3 changes: 1 addition & 2 deletions pygac/gac_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,7 @@ def main(filename, start_line, end_line):
sun_zen, sat_zen, sun_azi, sat_azi, rel_azi,
qual_flags, start_line, end_line,
reader.filename,
reader.get_midnight_scanline(),
reader.get_miss_lines())
reader.meta_data)
LOG.info("pygac took: %s", str(datetime.datetime.now() - tic))


Expand Down
26 changes: 22 additions & 4 deletions pygac/gac_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import types

from pygac import (CONFIG_FILE, centered_modulus,
calculate_sun_earth_distance_correction,
get_absolute_azimuth_angle_diff)
try:
import ConfigParser
Expand Down Expand Up @@ -64,6 +65,7 @@ def __init__(self, interpolate_coords=True, adjust_clock_drift=True,
tle_thresh: Maximum number of days between observation and nearest
TLE
"""
self.meta_data = {}
self.interpolate_coords = interpolate_coords
self.adjust_clock_drift = adjust_clock_drift
self.tle_dir = tle_dir
Expand Down Expand Up @@ -255,15 +257,31 @@ def compute_lonlat(self, width, utcs=None, clock_drift_adjust=True):

return lons.reshape(-1, width), lats.reshape(-1, width)

def get_sun_earth_distance_correction(self):
"""Get the julian day and the sun-earth distance correction."""
self.get_times()
jday = self.times[0].timetuple().tm_yday
return calculate_sun_earth_distance_correction(jday)

def update_meta_data(self):
"""Add some metd data to the meta_data dicitonary."""
if 'sun_earth_distance_correction_factor' not in self.meta_data:
self.meta_data['sun_earth_distance_correction_factor'] = (
self.get_sun_earth_distance_correction())
if 'midnight_scanline' not in self.meta_data:
self.meta_data['midnight_scanline'] = self.get_midnight_scanline()
if 'missing_scanlines' not in self.meta_data:
self.meta_data['missing_scanlines'] = self.get_miss_lines()

def get_calibrated_channels(self):
"""Calibrate the solar channels."""
channels = self.get_counts()
self.get_times()
self.update_meta_data()
year = self.times[0].year
delta = self.times[0].date() - datetime.date(year, 1, 1)
jday = delta.days + 1
jday = self.times[0].timetuple().tm_yday

# Earth-Sun distance correction factor
corr = 1.0 - 0.0334 * np.cos(2.0 * np.pi * (jday - 2) / 365.25)
corr = self.meta_data['sun_earth_distance_correction_factor']

# how many reflective channels are there ?
tot_ref = channels.shape[2] - 3
Expand Down
5 changes: 3 additions & 2 deletions pygac/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from pygac.tests import (test_calibrate_pod, test_slerp, test_calibrate_klm,
test_pod, test_tsm, test_reader, test_io,
test_angles)
test_angles, test_init)
import unittest


Expand All @@ -34,7 +34,8 @@ def suite():
"""
mysuite = unittest.TestSuite()
tests = (test_slerp, test_calibrate_klm, test_calibrate_pod,
test_pod, test_tsm, test_reader, test_io, test_angles)
test_pod, test_tsm, test_reader, test_io, test_angles,
test_init)
for test in tests:
mysuite.addTests(test.suite())

Expand Down
51 changes: 51 additions & 0 deletions pygac/tests/test_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Copyright (c) 2014-2019 Pytroll Developers

# Author(s):

# Nina Hakansson <nina.hakansson@smhi.se>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

"""Test function for the angle calculation."""

import unittest

import numpy as np

from pygac import calculate_sun_earth_distance_correction


class TestInit(unittest.TestCase):
"""Test function for the angle calculation."""

def test_calculate_sun_earth_distance_correction(self):
"""Test function for the sun distance corretction."""
corr = calculate_sun_earth_distance_correction(3)
np.testing.assert_almost_equal(corr, 0.96660494, decimal=7)


def suite():
"""Test non angle functions in pygac init file."""
loader = unittest.TestLoader()
mysuite = unittest.TestSuite()
mysuite.addTest(loader.loadTestsFromTestCase(TestInit))

return mysuite


if __name__ == '__main__':
unittest.main()
3 changes: 1 addition & 2 deletions pygac/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ def test_save_gac(self, check_user_scanlines, slice_channel, avhrr_gac_io,
rel_azi=mm,
qual_flags=mm,
gac_file=mm,
midnight_scanline=mm,
miss_lines=mm
meta_data=mm
)
slice_channel.return_value = mm, 'miss', 'midnight'
strip_invalid_lat.return_value = 0, 0
Expand Down
9 changes: 9 additions & 0 deletions pygac/tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ def test_correct_times_thresh(self, get_header_timestamp):
self.reader.correct_times_thresh()
numpy.testing.assert_array_equal(self.reader.utcs, utcs_expected)

def test_calculate_sun_earth_distance_correction(self):
"""Test the calculate sun earth distance correction method."""
self.reader.utcs = np.array([315748035469, 315748359969,
315751135469, 315754371969,
315754371969]).astype('datetime64[ms]')
self.reader.times = self.reader.to_datetime(self.reader.utcs)
corr = self.reader.get_sun_earth_distance_correction()
numpy.testing.assert_almost_equal(corr, 0.96660494, decimal=7)


def suite():
"""Test suite for test_reader."""
Expand Down