コード例 #1
0
def calculate_julian(year, month, day, hours=None, minutes=None, seconds=None):
    """
    Calculate the julian date value.
    :param year:    the year
    :param month:   the month
    :param day:     the day
    :param hours:   the hours
    :param minutes: the minutes
    :param seconds: the seconds
    :return:        the julian date
    """
    yr = mathutils.to_int(year)
    mth = mathutils.to_int(month)
    day = mathutils.to_int(day)
    hrs = mathutils.to_int(hours)
    mns = mathutils.to_int(minutes)
    scs = mathutils.to_float(seconds)
    if mth <= 2:
        yr -= 1
        mth += 12
    if yr >= 1582:
        A = yr / 100
        B = 2 - A + (A / 4)
    else:
        B = 0
    if yr < 0:
        C = int((365.25 * yr) - 0.75)
    else:
        C = int(365.25 * yr)
    D = int(30.6001 * (mth + 1))
    return 1720994.5 + B + C + D + day + (((
        (mathutils.fix(scs, TIME_SECONDS_PRECISION) / 60.0) + float(mns)) /
                                           60.0) + float(hrs)) / 24.0
コード例 #2
0
def load(file="USCityLatLng.csv"):
    global CITIES
    with open(file, 'r') as fref:
        line = __readline(fref)
        while line:
            parts = line.split(',')
            city = unicode(parts[0])
            region = unicode(parts[1])
            lat = mathutils.to_float(parts[2])
            lng = mathutils.to_float(parts[3])
            key = city.lower()
            entry = (city, region, lat, lng)
            if CITIES.has_key(key):
                CITIES[key].append(entry)
            else:
                CITIES[key] = [entry]
            line = __readline(fref)
コード例 #3
0
 def set_longitude(self, longitude=0.0):
     """
     Set the longitude to be used in date calculations.
     :param longitude:   the longitude to set (- for west, + for east)(-180..180)
     """
     l = mathutils.to_float(longitude, 0.0)
     if l is not None:
         l = mathutils.normalize_degrees(l, -180.0, 180.0)
     self.longitude = l
コード例 #4
0
def dms_from_dd(dd):
    dd = mathutils.to_float(dd, None)
    if dd is not None:
        d = int(dd)
        dm = abs(dd - d) * 60.0
        m = int(dm + 0.0005)
        s = mathutils.fix((dm - m) * 60.0, 2)
        return d, m, s
    return None
コード例 #5
0
def convert(v, fr, to, defaultValue=None):
    v = mathutils.to_float(v, defaultValue)
    if fr == to:
        return v
    if fr not in UNITS:
        raise ValueError("Invalid from type! Unknown type '{}'".format(fr))
    if to not in UNITS:
        raise ValueError("Invalid from type! Unknown type '{}'".format(to))
    name = "{}_to_{}".format(fr, to)
    func = getattr(sys.modules[__name__], name)
    return func(v)
コード例 #6
0
def to_day_of_week_from_julian(jd):
    """
    Converts a julian to day of the week.
    :param jd:  julian
    :return:    day of week
    """
    jul = mathutils.to_float(jd, None)
    if jul is not None:
        a = (jul + 1.5) / 7.0
        day = mathutils.to_int(round((a - mathutils.to_int(a)) * 7.0))
        return day
    return None
コード例 #7
0
def dd_from_dms(d, m, s):
    d = mathutils.to_int(d, None)
    m = mathutils.to_int(m, None)
    s = mathutils.to_float(s, None)
    if (d is not None) and (m is not None) and (s is not None):
        if d < 0:
            sgn = -1
        else:
            sgn = 1
        dd = sgn * ((((s / 60.0) + float(m)) / 60.0) + float(abs(d)))
        return dd
    return None
コード例 #8
0
 def set_seconds(self, seconds=0.0):
     """
     Set the seconds.
     :param seconds:     the seconds to set (0..59.99)
     """
     s = mathutils.to_float(seconds, 0.0)
     if s is not None:
         if s < 0.0:
             s = 0.0
         if s > get_max_seconds():
             s = get_max_seconds()
     self.seconds = s
コード例 #9
0
def hms_from_dh(dh):
    """
    Converts decimal hours to a time tuple.
    :param dh:  decimal hours to convert
    :return:    time tuple (hours, mins, secs)
    """
    dh = mathutils.to_float(dh, None)
    if dh is not None:
        h = int(dh)
        dm = abs(dh - h) * 60.0
        m = int(dm + 0.0005
                )  # add a tiny amount to make sure the minutes round properly
        s = mathutils.fix((dm - m) * 60.0, 2)
        return h, m, s
    return None
コード例 #10
0
 def add_seconds(self, ds):
     """
     Add a seconds delta to the date.
     :param ds:  seconds delta
     """
     if self.seconds is not None:
         self.seconds += mathutils.to_float(ds, 0.0)
         dm = 0
         while self.seconds < 1:
             self.seconds += 60.0
             dm -= 1
         while self.seconds >= 60.0:
             self.seconds -= 60.0
             dm += 1
         self.add_minutes(dm)
コード例 #11
0
def validate_time(hours, minutes, seconds, mode=None):
    """
    Validate the time.
    :return:    (hours, minutes, seconds[, mode]) or None
    """
    hrs = mathutils.to_int(hours, None)
    mns = mathutils.to_int(minutes, None)
    scs = mathutils.to_float(seconds, None)
    if (hrs is not None) and (mns is not None) and (scs is not None):
        if (hrs >= 0) and (hrs < 24):
            if (mns >= 0) and (mns < 60):
                if (scs >= 0) and (scs < 60):
                    if mode is not None:
                        if mode in TIME_MODES:
                            return hrs, mns, scs, mode
                    else:
                        return hrs, mns, scs
    return None
コード例 #12
0
def dh_from_hms(hours, minutes, seconds):
    """
    Converts a time to decimal hours.
    :param hours:   hours
    :param minutes: minutes
    :param seconds: seconds
    :return:        dh
    """
    h = mathutils.to_int(hours, None)
    m = mathutils.to_int(minutes, None)
    s = mathutils.to_float(seconds, None)
    if (h is not None) and (m is not None) and (s is not None):
        if h < 0:
            sgn = -1
        else:
            sgn = 1
        return sgn * ((((s / 60.0) + m) / 60.0) + abs(h))
    return None
コード例 #13
0
 def set_with_julian(self, jul, mode=TIME_MODE_UTC):
     """
     Set date from a julian day.
     :param jul:     julian
     :param mode:    the time mode (default: 'utc')
     """
     jul = mathutils.to_float(jul, None)
     if jul is not None:
         jd = 0.5 + jul
         I = mathutils.to_int(jd)
         F = jd - I
         if I > 2229160:
             A = mathutils.to_int((I - 1867216.25) / 36524.25)
             B = I + 1 + A - mathutils.to_int(A / 4.0)
         else:
             B = I
         C = B + 1524
         D = mathutils.to_int((C - 122.1) / 365.25)
         E = mathutils.to_int(365.25 * D)
         G = mathutils.to_int((C - E) / 30.6001)
         d = C - E + F - mathutils.to_int(30.6001 * G)
         if G < 13.5:
             mth = G - 1
         else:
             mth = G - 13
         if mth > 2.5:
             yr = D - 4716
         else:
             yr = D - 4715
         day = mathutils.to_int(d)
         h = (d - day) * 24
         hrs, mns, scs = hms_from_dh(h)
         self.set_year(yr)
         self.set_month(mth)
         self.set_day(day)
         self.set_hours(hrs)
         self.set_minutes(mns)
         self.set_seconds(scs)
         self.set_mode(mode)
コード例 #14
0
 def set_sec2(self, sec2):
     s = 0
     if sec2 is not None:
         s = mathutils.to_float(sec2, 0.0)
     self.sec2 = s
コード例 #15
0
 def set_sec1(self, sec1):
     s = 0
     if sec1 is not None:
         s = mathutils.to_float(sec1, 0.0)
     self.sec1 = s
コード例 #16
0
 def set_latitude(self, latitude):
     l = mathutils.to_float(latitude, 0.0)
     if l is not None:
         l = mathutils.normalize_degrees(l, -90, 90.0)
     self.latitude = l
コード例 #17
0
def validate_hours(h, m, s):
    if (h is not None) and (h == mathutils.to_int(h)) and (h > -24.0) and (h <= 24.0):
        if (m is not None) and (m == mathutils.to_int(m)) and (m >= 0.0) and (m <= 59.0):
            if (s is not None) and (s == mathutils.to_float(s)) and (s >= 0.0) and (s < 60.0):
                return True
    return False
コード例 #18
0
def validate_degrees(d, m, s):
    if (d is not None) and (d == mathutils.to_int(d)) and (d >= -360.0) and (d <= 360.0):
        if (m is not None) and (m == mathutils.to_int(m)) and (m >= 0.0) and (m <= 59.0):
            if (s is not None) and (s == mathutils.to_float(s)) and (s >= 0.0) and (s < 60.0):
                return True
    return False