Skip to content

Instantly share code, notes, and snippets.

@bnordgren
Created October 25, 2014 20:26
Show Gist options
  • Save bnordgren/7e34c0752ed2a75ebcfb to your computer and use it in GitHub Desktop.
Save bnordgren/7e34c0752ed2a75ebcfb to your computer and use it in GitHub Desktop.
Unit-ed index translator
from abc import ABCMeta, abstractmethod
import numpy as np
import astropy.units as u
import astropy.coordinates as c
class SamplingFunction (object) :
__metaclass__ = ABCMeta
@abstractmethod
def get_index(self, unit_val) :
pass
class LinearSamplingFunction ( SamplingFunction ) :
def __init__(self, scale, offset=0) :
self.scale = scale
self.offset = offset
def get_index(self, unit_val) :
return np.trunc(unit_val*self.scale + self.offset).astype(np.int)
class LongitudeSamplingFunction (SamplingFunction) :
E_ROT = 360*u.deg/(1*u.day)
"""earth angular velocity around own axis (approximate)"""
NOON = c.Angle(180*u.deg)
"""the time-of-day "noon", expressed as angle from midnight"""
def __init__(self, daily_samples) :
self.daily_samples = daily_samples
sample_interval = (1./daily_samples) * u.day
self.time_to_sample = LinearSamplingFunction( 1/sample_interval )
self.noon = np.trunc((12*u.hour) / sample_interval)
def get_index(self, unit_val) :
"""converts longitude to sample"""
lon_noon = c.Angle((-unit_val) + self.NOON).wrap_at(360*u.deg)
return self.time_to_sample.get_index(lon_noon/self.E_ROT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment