Пример #1
0
def calculate_julian_century(datetime):
    """
    Given a datetime object return the julian century from the 2000 epoch.

    :param datetime:
        A datetime object containing the date to be converted to a
        Julian centuries since 2000/01/01 12:00.

    :return:
        A floating point value representing the Julian centuries since
        the 2000/01/01 12:00 epoch.
    """

    # Convert the scene timestamp to a julian date
    d = ephem.date(datetime)
    jdate = ephem.julian_date(d)

    # Get the J2000 epoch
    epoch = ephem.date((2000, 1, 1, 12.00))
    j2_epoch = ephem.julian_date(epoch)

    # Note:
    # This differes from online sources such as
    # http://www.pietro.org/astro_util_staticdemo/FDetailDateConversions.htm
    # http://en.wikipedia.org/wiki/Equinox_(celestial_coordinates)
    # which use:
    # 2000 + (jdate - epoch) / 365.25
    century = (jdate - j2_epoch) / 36525

    return century
Пример #2
0
def LunarCalendar(nian, type=1):  # type=1时截止到次年冬至朔,=0时截止到次年冬至朔次月
    dzs = findDZS(nian)
    shuo = dzs  # 计算用朔,date格式
    shuoJD = [ephem.julian_date(dzs)]  # 存储ut+8 JD,起冬至朔
    next_dzsJD = ephem.julian_date(findDZS(nian + 1))  # 次年冬至朔
    i = -1  # 中气序,从0起计
    j = -1  # 计算连续两个冬至月中的合朔次数,从0起计
    zry = 0
    flag = False
    # 查找所在月及判断置闰
    while not DateCompare(shuoJD[j + type], next_dzsJD):  # 从冬至月起查找,截止到次年冬至朔
        i += 1
        j += 1
        shuo = ephem.next_new_moon(shuo)  # 次月朔
        shuoJD.append(ephem.julian_date(shuo))
        # 查找本月中气,若无则置闰
        if j == 0: continue  # 冬至月一定含中气,从次月开始查找
        angle = (-90 + 30 * i) % 360  # 本月应含中气,起冬至
        qJD = SolarTerms(nian, angle)
        # 不判断气在上月而后气在后月的情况,该月起的合朔次数不超过气数,可省去
        if DateCompare(qJD, shuoJD[j + 1]) and flag == False:  # 中气在次月,则本月无中气
            zry = j + 1  # 置闰月
            i -= 1
            flag = True  # 仅第一个无中气月置闰
    # 生成农历月序表
    ymb = []
    for k in range(len(shuoJD)):
        ymb.append(yuefen[(k - 2) % 12])  # 默认月序
        if j + type == 13:  # 仅12次合朔不闰,有闰时修改月名
            if k + 1 == zry:
                ymb[k] = '闰' + yuefen[(k - 1 - 2) % 12]
            elif k + 1 > zry:
                ymb[k] = yuefen[(k - 1 - 2) % 12]
    return ymb, shuoJD  # 月名表,合朔JD日期表
Пример #3
0
    def set_datetime(self, D=1, v=0):
        """ set and compute all times for observatory
            v = 'v' for verbose """

        k = '{:0=+3.0f}'.format(int(D))
        k = int(k)
        t = datetime.date.today() + datetime.timedelta(+k)
        tt = datetime.time(0, 0)
        self.comp_date = datetime.datetime.combine(t, tt)
        self.oadm.date = self.comp_date
        self.Dat1 = self.oadm.next_rising(ep.Sun(), use_center=True)
        self.Dat0 = self.oadm.previous_setting(ep.Sun(), use_center=True)
        self.start_night = self.Dat0.datetime()
        self.end_night = self.Dat1.datetime()
        self.night_len = (self.end_night - self.start_night)
        self.lst = self.oadm.sidereal_time()
        self.utc = ep.now().datetime()
        self.jd0 = ep.julian_date(0)
        self.jd_now = ep.julian_date(ep.now())
        self.jd_comp = ep.julian_date(self.comp_date)
        self.jd_start = ep.julian_date(self.start_night)
        self.jd_end = ep.julian_date(self.end_night)
        if v == 'v':
            D = str(D)
            print '> set_datetime >  d' + D, ' > ', self.oadm.date, '-', self.jd_comp  #self.print_date()
        return
Пример #4
0
def convertToJulianTuple(timestamp):
    """ Convert a list of timestamps into DATE and TIME since julian midnight

    FITS-IDI requires DATE parameter is set to Julian date @ midnight. TIME is
    then indexed against this DATE.
    
    timestamp (float): timestamp of type 'float' is preferred, but this should
                       handle time tuples, strings and datetime objects too.
    """
    
    # Figure out exactly what type of timestamp we've got.
    if type(timestamp) in (str, unicode):
        tt = parse_timestring(timestamp)
        ts = calendar.timegm(tt)
    elif type(timestamp) is float:
        ts = timestamp
    elif type(timestamp) is tuple:
        ts = calendar.timegm(timestamp)
    elif type(timestamp) is type(datetime.now):
        date_str = timestamp.strftime("%Y-%m-%dT%H:%M:%S")
        tt = time.strptime(date_str, "%Y-%m-%dT%H:%M:%S")
        ts = calendar.timegm(tt)
    else:
        raise TypeError("Unknown timestamp type '%s'" % str(type(timestamp)))
        
    # DATE is julian date at midnight that day
    # TIME is in DAYS since midnight
    julian = ephem.julian_date(time.gmtime(ts)[:6])
    # Ephem returns julian date at NOON, we need at MIDNIGHT
    julian_midnight = int(julian) - 0.5

    time_elapsed = ephem.julian_date(time.gmtime(ts)[:6]) - julian_midnight

    return julian_midnight, time_elapsed
Пример #5
0
def get_time_range(start_time,end_time,timestep,time_in_sec,TIME_OFFSET=0):
    if HAS_PYRAP:
      try:
        start_time = qu.quantity(start_time).get_value()
        end_time = qu.quantity(end_time).get_value()
        print ('**** specified start and end time ', start_time, end_time)
        reference_time = start_time * 86400.0 - TIME_OFFSET
        st = reference_time - timestep 
        et = end_time * 86400.0 +  timestep + TIME_OFFSET
        #print "getting string",reference_time
        str_start_time =  obtain_observation_year_month_day_hms(reference_time)
        timerange= [st, et]
      except:
        print ('no time range given')
        print ('exiting')
        return -1,-1,-1
    elif HAS_EPHEM:
      if time_in_sec:
        dublin_start = start_time / 86400.0 -15019.5
        dublin_end = end_time / 86400.0 -15019.5
        start_time = ephem.julian_date(ephem.Date(dublin_start)) - 2400000.5
        end_time = ephem.julian_date(ephem.Date(dublin_end)) - 2400000.5
      else:
        start_time = ephem.julian_date(ephem.Date(start_time)) - 2400000.5
        end_time = ephem.julian_date(ephem.Date(end_time)) - 2400000.5
      print ('ephem start and end time ', start_time, end_time)
      reference_time = start_time * 86400.0 - TIME_OFFSET
      st = reference_time - timestep 
      et = end_time * 86400.0 + timestep + TIME_OFFSET
      str_start_time =  obtain_observation_year_month_day_hms(reference_time)
      timerange= [st, et]
    else:
      print ('unable to get time range so exiting!')
      return -1,-1,-1
    return timerange,str_start_time,reference_time
Пример #6
0
def create_ascii_table(observation_table, outfile):
    """Given a table of observations create an ascii log file for easy parsing.
    Store the result in outfile (could/should be a vospace dataNode)

    observation_table: astropy.votable.array object
    outfile: str (name of the vospace dataNode to store the result to)

    """

    logging.info("writing text log to %s" % outfile)

    stamp = "#\n# Last Updated: " + time.asctime() + "\n#\n"
    header = "| %20s | %20s | %20s | %20s | %20s | %20s | %20s |\n" % (
        "EXPNUM", "OBS-DATE", "FIELD", "EXPTIME(s)", "RA", "DEC", "RUNID")
    bar = "=" * (len(header) - 1) + "\n"

    if outfile[0:4] == "vos:":
        temp_file = tempfile.NamedTemporaryFile(suffix='.txt')
        fout = temp_file
    else:
        fout = open(outfile, 'w')

    t2 = None
    fout.write(bar + stamp + bar + header)

    populated = storage.list_dbimages()
    for i in range(len(observation_table) - 1, -1, -1):
        row = observation_table.data[i]
        if row['dataset_name'] not in populated:
            storage.populate(row['dataset_name'])
        str_date = str(
            ephem.date(row.StartDate + 2400000.5 -
                       ephem.julian_date(ephem.date(0))))[:20]
        t1 = time.strptime(str_date, "%Y/%m/%d %H:%M:%S")
        if t2 is None or math.fabs(time.mktime(t2) -
                                   time.mktime(t1)) > 3 * 3600.0:
            fout.write(bar)
        t2 = t1
        ra = str(ephem.hours(math.radians(row.RA)))
        dec = str(ephem.degrees(math.radians(row.DEC)))
        line = "| %20s | %20s | %20s | %20.1f | %20s | %20s | %20s |\n" % (
            str(row.dataset_name),
            str(
                ephem.date(row.StartDate + 2400000.5 -
                           ephem.julian_date(ephem.date(0))))[:20],
            row.TargetName[:20], row.ExposureTime, ra[:20], dec[:20],
            row.ProposalID[:20])
        fout.write(line)

    fout.write(bar)

    if outfile[0:4] == "vos:":
        fout.flush()
        storage.copy(fout.name, outfile)
    fout.close()

    return
Пример #7
0
def create_ascii_table(observation_table, outfile):
    """Given a table of observations create an ascii log file for easy parsing.
    Store the result in outfile (could/should be a vospace dataNode)

    observation_table: astropy.votable.array object
    outfile: str (name of the vospace dataNode to store the result to)

    """

    logging.info("writing text log to %s" % outfile)

    stamp = "#\n# Last Updated: " + time.asctime() + "\n#\n"
    header = "| %20s | %20s | %20s | %20s | %20s | %20s | %20s |\n" % (
        "EXPNUM", "OBS-DATE", "FIELD", "EXPTIME(s)", "RA", "DEC", "RUNID")
    bar = "=" * (len(header) - 1) + "\n"

    if outfile[0:4] == "vos:":
        temp_file = tempfile.NamedTemporaryFile(suffix='.txt')
        fout = temp_file
    else:
        fout = open(outfile, 'w')

    t2 = None
    fout.write(bar + stamp + bar + header)

    populated = storage.list_dbimages()
    for i in range(len(observation_table) - 1, -1, -1):
        row = observation_table.data[i]
        if row['dataset_name'] not in populated:
            storage.populate(row['dataset_name'])
        str_date = str(ephem.date(row.StartDate +
                                  2400000.5 -
                                  ephem.julian_date(ephem.date(0))))[:20]
        t1 = time.strptime(str_date, "%Y/%m/%d %H:%M:%S")
        if t2 is None or math.fabs(time.mktime(t2) - time.mktime(t1)) > 3 * 3600.0:
            fout.write(bar)
        t2 = t1
        ra = str(ephem.hours(math.radians(row.RA)))
        dec = str(ephem.degrees(math.radians(row.DEC)))
        line = "| %20s | %20s | %20s | %20.1f | %20s | %20s | %20s |\n" % (
            str(row.dataset_name),
            str(ephem.date(row.StartDate + 2400000.5 -
                           ephem.julian_date(ephem.date(0))))[:20],
            row.TargetName[:20],
            row.ExposureTime, ra[:20], dec[:20], row.ProposalID[:20])
        fout.write(line)

    fout.write(bar)

    if outfile[0:4] == "vos:":
        fout.flush()
        storage.copy(fout.name, outfile)
    fout.close()

    return
 def record_night(self):
     self.__NightSummary[0] = self.t_start
     self.__NightSummary[1] = self.t_end
     self.__NightSummary[2] = self.init_id
     np.save(
         "Output/Schedule{}.npy".format(int(ephem.julian_date(self.Date))),
         self.__NightOutput)
     np.save(
         "Output/Summary{}.npy".format(int(ephem.julian_date(self.Date))),
         self.__NightSummary)
     np.save("Output/Watch{}.npy".format(int(ephem.julian_date(self.Date))),
             self.tonight_telescope.watch)
Пример #9
0
 def cp_transit(self,
                ob,
                x,
                k='+'):  #TODO: revisar transits next i previous!
     """ returns the JD of previous/next object transit
         ob = coordinate line in ephem format """
     obj = ep.readdb(ob)
     self.Transit = ep.julian_date(self.oadm.previous_transit(obj))
     if self.Transit < self.jd_start:
         self.Transit = ep.julian_date(self.oadm.next_transit(obj))
     if k == '+':
         x['transit'] = self.Transit
     return self.Transit
Пример #10
0
    def cp_obs_time_airm(self, x, ob, t1, t2, k='+'):
        """compute visibility time (from airmass req. A)
            ob = coordinates line in ephem format
            x = {toi} data
            t1,t2 = start/end time to compute
            k = '+' to set values of V,NV,WV periods in {toi}"""
        self.V_period, self.WV_period, self.NV_period = [], [], []
        obj = ep.readdb(ob)
        td = datetime.timedelta(seconds=self.time_acc)
        obsv = []
        period = []
        i = 0
        #t1 = t1-td
        while t1 <= t2:
            self.oadm.date = t1
            obj.compute(self.oadm)
            air0 = self.comp_air(ob, t1)
            if air0 > float(x['airmass_max']) or air0 < float(
                    x['airmass_min']):
                obsv0 = 0
            else:
                obsv0 = 1
            obsv.append([obsv0, t1])
            t1 += td
        period.append([0, obsv[0]])
        period.append([obsv.index(obsv[-1]), obsv[-1]])
        for i in range(len(obsv) - 1):
            if obsv[i][0] != obsv[i + 1][0]:
                period.append([obsv.index(obsv[i]), obsv[i]])
        sort_period = sorted(period)
        for i in range(len(sort_period) - 1):
            kk = [
                sort_period[i + 1][1][0], x['id'],
                ep.julian_date(sort_period[i][1][1]),
                ep.julian_date(sort_period[i + 1][1][1])
            ]
            self.V_period.append(kk)
            if kk[0] == 0:
                self.NV_period.append(kk)
            else:
                self.WV_period.append(kk)
        self.oadm.date = self.comp_date
        if k == '+':
            x['V_period'] = self.V_period
            x['WV_period'] = self.WV_period
            x['NV_period'] = self.NV_period

        return self.V_period, self.WV_period, self.NV_period
Пример #11
0
def _kbos_from_survey_sym_model_input_file(model_file):
    """
    Load a Survey Simulator model file as an array of ephem EllipticalBody objects.
    @param model_file:
    @return:
    """
    lines = storage.open_vos_or_local(model_file).read().split('\n')
    kbos = []
    for line in lines:
        if len(line) == 0 or line[
                0] == '#':  # skip initial column descriptors and the final blank line
            continue
        kbo = ephem.EllipticalBody()
        values = line.split()
        kbo.name = values[8]
        kbo.j = values[9]
        kbo.k = values[10]
        kbo._a = float(values[0])
        kbo._e = float(values[1])
        kbo._inc = float(values[2])
        kbo._Om = float(values[3])
        kbo._om = float(values[4])
        kbo._M = float(values[5])
        kbo._H = float(values[6])
        epoch = ephem.date(2453157.50000 - ephem.julian_date(0))
        kbo._epoch_M = epoch
        kbo._epoch = epoch
        kbos.append(kbo)
    return kbos
Пример #12
0
def test_JulianDate_reduce_datetime(date, expected):
    d = datetime.datetime(*date)
    jd = JulianDate.from_datetime(d, reduced=True)

    assert jd.reduced
    assert np.allclose(jd.jd, expected)
    assert np.allclose(jd.jd, ephem.julian_date(d) - 2400000)
Пример #13
0
def _kbos_from_survey_sym_model_input_file(model_file):
    """
    Load a Survey Simulator model file as an array of ephem EllipticalBody objects.
    @param model_file:
    @return:
    """
    lines = storage.open_vos_or_local(model_file).read().split('\n')
    kbos = []
    for line in lines:
        if len(line) == 0 or line[0] == '#':  # skip initial column descriptors and the final blank line
            continue
        kbo = ephem.EllipticalBody()
        values = line.split()
        kbo.name = values[8]
        kbo.j = values[9]
        kbo.k = values[10]
        kbo._a = float(values[0])
        kbo._e = float(values[1])
        kbo._inc = float(values[2])
        kbo._Om = float(values[3])
        kbo._om = float(values[4])
        kbo._M = float(values[5])
        kbo._H = float(values[6])
        epoch = ephem.date(2453157.50000 - ephem.julian_date(0))
        kbo._epoch_M = epoch
        kbo._epoch = epoch
        kbos.append(kbo)
    return kbos
Пример #14
0
def gen_ha_axis(timestamps, scale, offset, RA):
    """ Return a list of real times from the timestamp vector"""
    # get the timestamps
    timestamps = numpy.array(timestamps, dtype=float)
    t = numpy.zeros(len(timestamps), dtype=numpy.float64)
    t = numpy.array(offset + timestamps / scale,
                    dtype=numpy.float64)  #UNIX times
    gmt_ref = time.gmtime(t[0])  # Start time in UTC
    jd_ref = ephem.julian_date(gmt_ref[0:6])
    ha = numpy.zeros_like(t)
    for i, ti in enumerate(t):
        obs.date = ephem.Date(time.gmtime(ti)[0:6])
        #print "time is", obs.date
        ha[i] = obs.sidereal_time() - RA
        #print obs.sidereal_time(), RA, obs.sidereal_time()-RA
    # Unwrap the phases, otherwise weird things can happen
    # When the lst crosses midnight
    ha = numpy.unwrap(ha)
    ha = numpy.rad2deg(ha)
    if RA == 0:
        scale = 'Local Sidereal Time'
    else:
        scale = 'Hour Angle'
    return {
        'times': ha,
        'scale': scale,
        'ref': RA,
        'gmtref': gmt_ref,
        'jd_ref': jd_ref,
        'unit': ''
    }
Пример #15
0
def gen_time_axis(timestamps, scale, offset):
    """ Return a list of real times from the timestamp vector"""
    # get the timestamps
    timestamps = numpy.array(timestamps, dtype=float)
    t = numpy.zeros(len(timestamps), dtype=numpy.float64)
    t = numpy.array(offset + timestamps / scale,
                    dtype=numpy.float64)  #UNIX times
    t_range = t[-1] - t[0]
    gmt_ref = time.gmtime(t[0])  # Start time in UTC
    jd_ref = ephem.julian_date(gmt_ref[0:6])

    if t_range < 300:
        scale = 'Seconds since %.4d/%.2d/%.2d %.2d:%.2d:%.2d UTC' % (
            gmt_ref[0:6])
        t = (t - t[0])
    elif t_range < 60 * 60 * 3:
        scale = 'Minutes since %.2d/%.2d/%.2d %.2d:%.2d:%.2d UTC' % (
            gmt_ref[0:6])
        t = (t - t[0]) / 60
    else:
        scale = 'Hours since %.2d/%.2d/%.2d %.2d:%.2d:%.2d UTC' % (
            gmt_ref[0:6])
        t = (t - t[0]) / 60 / 60
    return {
        'times': t,
        'scale': scale,
        'ref': t[0],
        'gmtref': gmt_ref,
        'jd_ref': jd_ref,
        'unit': ''
    }
Пример #16
0
def calculate_airmass(file_name):
    """
    Calculates AIRMASS for the FITS file and appends respective details in the header of the file 'file_name'
    Args:
        file_name : FITS file whose header has to be edited
    Returns:
        None
    """
    hdulist = fits.open(file_name, mode='update')
    file_header = hdulist[0].header
    date_obs = file_header[str(date_keyword)]
    time_start = file_header[str(time_start_keyword)]

    if str(RA_keyword) in file_header.keys():
        object_ra = file_header[str(RA_keyword)]
    else:
        object_ra = RA_object

    if str(DEC_keyword) in file_header.keys():
        object_dec = file_header[str(DEC_keyword)]
    else:
        object_dec = DEC_object

    time_utc = str(datetime.timedelta(seconds=int(time_start)))
    datetime_utc = str(date_obs) + ' ' + str(time_utc)
    julian_day = ephem.julian_date(datetime_utc)

    telescope = ephem.Observer()
    telescope.lon = OBS_LONG
    telescope.lat = OBS_LAT
    telescope.elevation = OBS_ALT
    telescope.pressure = 0
    telescope.epoch = ephem.J2000
    telescope.date = datetime_utc

    obj_pos = ephem.FixedBody()
    obj_pos._ra = object_ra
    obj_pos._dec = object_dec
    obj_pos._epoch = ephem.J2000
    obj_pos.compute(telescope)

    time_sidereal = telescope.sidereal_time()
    object_alt = Angle(str(obj_pos.alt) + ' degrees').degree
    airmass = 1 / math.cos(math.radians(90 - object_alt))

    list_keywords = ['OBSERVAT', 'OBS_LAT', 'OBS_LONG', 'OBS_ALT', 'TIMEZONE', 'DATE_OBS', 'UT', 'JD', 'ST', 'RA',
                     'DEC', 'ALT', 'AZ', 'AIRMASS']

    dict_header = {'OBSERVAT': str(OBS_NAME), 'OBS_LAT': str(OBS_LAT), 'OBS_LONG': str(OBS_LONG),
                   'OBS_ALT': str(OBS_ALT), 'TIMEZONE': str(OBS_TIMEZONE), 'DATE_OBS': str(date_obs),
                   'UT': str(time_utc), 'JD': str(julian_day), 'ST': str(time_sidereal), 'RA': str(object_ra),
                   'DEC': str(object_dec), 'ALT': str(obj_pos.alt), 'AZ': str(obj_pos.az), 'AIRMASS': str(airmass)}

    for keyword in list_keywords:
        if keyword in file_header.keys():
            file_header.remove(str(keyword), remove_all=True)
        file_header.append(card=(keyword, dict_header[keyword]))

    hdulist.flush()
    hdulist.close()
Пример #17
0
def update_local_googledex(intime,googledex_file="googledex.dat", observed_file="observed_targets"):
    """
        Update the local copy of the googledex with the last observed star time.
        update_local_googledex(time,googledex_file="googledex.dat", observed_file="observed_targets")

        opens googledex_file and inputs date of last observation from observed_file
        in principle can use timestamps as well as scriptobs uth and utm values
    """
    names, times = ObservedLog.getObserved(observed_file)

    try:
        g = open(googledex_file, 'rb')
        full_codex = pickle.load(g)
        g.close()
    except IOError:
        apflog("googledex file did not exist, so can't be updated",echo=True)
        return names,times
    except EOFError:
        apflog("googledex file corrupt, so can't be updated",echo=True)
        return names,times


    codex_cols = full_codex[0]

    starNameIdx = codex_cols.index("Star Name")
    lastObsIdx = codex_cols.index("lastobs")
    try:
        nObsIdx = codex_cols.index("nObsIdx")
    except:
        nObsIdx = -1
    
    for i in range(1, len(full_codex)):
        row = full_codex[i]
        if row[starNameIdx] in names:
            # We have observed this star, so lets update the last obs field
            obstime = times[names.index(row[starNameIdx])]
            if isinstance(obstime,float):
                t = datetime.utcfromtimestamp(obstime)
            else:
                hr, min = obstime
                if type(intime) != datetime:
                    ctime = datetime.now()
                    td = timedelta(0,3600.*7)
                    intime = ctime + td
                t = datetime(intime.year, intime.month, intime.day, hr, min)

            # This keeps the JD precision to one decimal point. There is no real reason for this other than
            # the googledex currently only stores the lastObs field to one decimal precision. Legacy styles FTW.
            jd = round(float(ephem.julian_date(t)), 2) 
            apflog( "Updating local googledex star %s from time %s to %s" % (row[starNameIdx], row[lastObsIdx], str(jd)),echo=True)
            row[lastObsIdx] = str(jd)
            if nObsIdx > 0:
                row[nObsIdx] = row[nObsIdx] + 1
            full_codex[i] = row

    with open(googledex_file, 'wb') as f:
        pickle.dump(full_codex, f)
    f.close()
    
    return names, times
Пример #18
0
 def gmst(self):
     if self._gmst is None:
         jd = ephem.julian_date(self.ut)
         T = (jd - 2451545.0)/36525.0
         gmstdeg = 280.46061837+(360.98564736629*(jd-2451545.0))+(0.000387933*T*T)-(T*T*T/38710000.0)
         self._gmst = ephem.degrees(gmstdeg*numpy.pi/180.0)
     return self._gmst
Пример #19
0
def synthetic_model_kbos(date, maglimit=24.5, kbotype='resonant'):
    ra = []
    dist = []
    hlat = []
    for line in open('L7SyntheticModel-v09.txt'):
        if line[0]=='#':
            continue
        kbo = ephem.EllipticalBody()
        values = line.split()
        kbo._a = float(values[0])
        kbo._e = float(values[1])
        kbo._inc = float(values[2])
        kbo._Om = float(values[3])
        kbo._om = float(values[4])
        kbo._M = float(values[5])
        kbo._H = float(values[6])
        kbo._epoch_M = ephem.date(2453157.50000 - ephem.julian_date(0))
        kbo._epoch = kbo._epoch_M
        kbo.name = values[8]  # values[9] and [10] encode the type of resonance eg. 2:1 - add that if wanted

        kbo.compute(ephem.date(date))
        if (kbo.mag < maglimit):# and (kbo.name == kbotype):
            ra.append(kbo.ra)
            dist.append(kbo.sun_distance)
            hlat.append(kbo.hlat)

    return ra, dist, hlat
 def init_night(self):
     # Reset Nights outputs
     self.__NightOutput = np.zeros(
         (1200, ),
         dtype=[('Field_id', np.int), ('ephemDate', np.float),
                ('Filter', np.int), ('n_ton', np.int), ('n_last', np.int),
                ('Cost', np.float), ('Slew_t', np.float),
                ('t_since_v_ton', np.float), ('t_since_v_last', np.float),
                ('Alt', np.float), ('HA', np.float),
                ('t_to_invis', np.float), ('Sky_bri', np.float),
                ('Temp_coverage', np.int)])  # at most 1200 visits per night
     self.__NightSummary = np.zeros(3)  # t_start and t_end for now
     # Reset time
     self.clock(0, True)
     # Reset fields' state
     self.reset_fields_state()
     # Reset telescope
     init_state = self.init_state(self.manual_init_state, False)
     init_filter = self.init_filter()
     init_state.update_visit_var(self.__t)
     self.tonight_telescope.update(self.t_start, self.n_start, self.__step,
                                   init_state, init_filter)
     self.minimum_cost = 0.
     self.reset_feedback()
     # Record initial condition
     self.op_log = open(
         "Output/log{}.lis".format(int(ephem.julian_date(self.Date))), "w")
     self.record_visit()
 def epochString(self):
     if self.epoch == Epoch.J2000:
         return "J2000.0"
     elif self.epoch == Epoch.B1950:
         return "B1950.0"
     else:
         return "J%.2f" % (2000.0 + (ephem.julian_date() - 2451545.0) / 365.25)
Пример #22
0
def getGST(year, month, day, hour, minute, secunde):
    """
	Function calculates the GST0 in degrees for a current year. This is a constans
	needed for eci2ecef function.
	"""

    stream = open("inputs.yaml", 'r')
    inputs = yaml.load(stream)
    longitude = float(inputs.get('input lon'))

    julian_date = ephem.julian_date(
        str(year) + '/' + str(month) + '/' + str(day))
    julian_centuries = (julian_date - 2451545) / 36525
    GST0 = 100.4606184 + (36000.77005361 * julian_centuries) + \
     (0.00038793 * julian_centuries**2) - (2.583*10**-8 * julian_centuries**3)
    GST0 = GST0 % 360
    #print "Julian date: ", julian_date
    #print "GMST0: ", GST0

    #Greenwich sidereal time
    GST = GST0 + 0.25068447733746215 * (hour * 60 + minute + secunde / 60)
    GST = GST % 360
    #print "GST: ", GST

    #Local sidereal time
    LST = GST + longitude
    LST = LST % 360
    #print "LST: ", LST

    return GST
Пример #23
0
def test_JulianDate_reduce_datetime(date, expected):
    d = datetime.datetime(*date)
    jd = JulianDate.from_datetime(d, reduced=True)

    assert jd.reduced
    assert np.allclose(jd.jd, expected)
    assert np.allclose(jd.jd, ephem.julian_date(d) - 2400000)
Пример #24
0
 def calc_GMST(self, date):
     """Compute Greenwich Mean Sidereal Time"""
     jd = ephem.julian_date(date)
     T = (jd - 2451545.0)/36525.0
     gmstdeg = 280.46061837+(360.98564736629*(jd-2451545.0))+(0.000387933*T*T)-(T*T*T/38710000.0)
     gmst = ephem.degrees(gmstdeg*numpy.pi/180.0)
     return gmst
Пример #25
0
 def epochString(self):
     if self.epoch == Epoch.J2000:
         return "J2000.0"
     elif self.epoch == Epoch.B1950:
         return "B1950.0"
     else:
         return "J%.2f" % (2000.0 + (ephem.julian_date() - 2451545.0) / 365.25)
Пример #26
0
def import_lastrun_data(opt):
    mjd_yesterday = ephem.date(ephem.julian_date(ephem.date(
        opt.date))) - 2400000.5

    data_to_date = query_for_observations(mjd_yesterday, opt.cal, OSSOS_RUNIDS)
    for row in data_to_date:
        storage.populate(row['dataset_name'])
Пример #27
0
def calculate_airmass(textlist_files):
    """
    Calculates AIRMASS for the list of all FITS files and appends respective details in the headers.
    Args:
        textlist_files : List of all FITS files whose headers have to be edited
    Returns:
        None
    """
    list_files = text_list_to_python_list(textlist_files)

    for file_name in list_files:
        hdulist = fits.open(file_name, mode='update')
        file_header = hdulist[0].header
        date_obs = file_header[DATE_keyword]

        if RA_keyword in file_header.keys():
            object_ra = file_header[RA_keyword]
        else:
            object_ra = OBJECT_RA

        if DEC_keyword in file_header.keys():
            object_dec = file_header[DEC_keyword]
        else:
            object_dec = OBJECT_DEC

        datetime_utc = date_obs + '00:00:00'
        jd = ephem.julian_date(datetime_utc)

        telescope = ephem.Observer()
        telescope.lon = OBS_LONG
        telescope.lat = OBS_LAT
        telescope.elevation = OBS_ALT
        telescope.pressure = 0
        telescope.epoch = ephem.J2000
        telescope.date = datetime_utc

        obj_pos = ephem.FixedBody()
        obj_pos._ra = object_ra
        obj_pos._dec = object_dec
        obj_pos._epoch = ephem.J2000
        obj_pos.compute(telescope)

        time_sidereal = telescope.sidereal_time()
        object_alt = Angle(str(obj_pos.alt) + ' degrees').degree
        airmass = 1 / math.cos(math.radians(90 - object_alt))

        list_keywords = ['OBSERVAT', 'OBS_LAT', 'OBS_LONG', 'OBS_ALT', 'TIMEZONE', 'DATE_OBS', 'JD', 'ST', 'RA',
                         'DEC', 'ALT', 'AZ', 'AIRMASS']

        dict_header = {'OBSERVAT': OBS_NAME, 'OBS_LAT': OBS_LAT, 'OBS_LONG': OBS_LONG, 'OBS_ALT': OBS_ALT,
                       'TIMEZONE': OBS_TIMEZONE, 'DATE_OBS': date_obs, 'JD': jd, 'ST': time_sidereal,
                       'RA': object_ra, 'DEC': object_dec, 'ALT': obj_pos.alt, 'AZ': obj_pos.az, 'AIRMASS': airmass}

        for keyword in list_keywords:
            if keyword in file_header.keys():
                file_header.remove(keyword, remove_all=True)
            file_header.append(card=(keyword, dict_header[keyword]))

        hdulist.flush()
        hdulist.close()
Пример #28
0
def gen_test_dates(obs):
    """Generate test dates"""
    greenwich = ephem.Observer()
    greenwich.lat = '51:28:38'

    str_dates = [
        '1978/11/13 04:34:00',
        '1984/05/30 02:20:00',
        '2000/01/01 00:00:00',
        '2003/05/06 08:03:01',
        '2004/02/29 12:59:11',
        '2014/12/31 16:10:22',
        '2017/12/11 22:02:33',
        '2050/05/11 20:01:44',
    ]

    dates = []
    for str_date in str_dates:
        greenwich.date = str_date
        obs.date = str_date
        date = {
            'str': str_date,
            'jd': ephem.julian_date(str_date),
            'gmst': greenwich.sidereal_time(),
            'lst': obs.sidereal_time(),
        }
        dates.append(date)
    return dates
Пример #29
0
    def __init__(self, julianOffset=None):
        '''
        Initialise class

        options:
            julianOffset: set to report Julian day with an offset subtracted
                          Format, 'YYYY/M/D' e.g. '2012/1/1'
        '''
        # use the python library ephem
        try:
            import ephem
        except:
            import pyephem as ephem
        self.gatech = ephem.Observer()
        if julianOffset != None:
            self.julianOffset = ephem.julian_date(julianOffset)
        else:
            self.julianOffset = 0.

        # http://acrim.com/TSI%20Monitoring.htm
        self.solar = 1361  #W/m2

        # PPFD is measured in micromoles/m2/sec
        # (Photosynthetic Photon Flux Density)

        # astronomical unit
        # http://neo.jpl.nasa.gov/glossary/au.html
        # we dont need this, but its interesting to know
        self.AU = 149597870.691 * 1e3  # m

        # energy content of PAR quanta
        self.EPAR = 220.e-3  # MJmol-1
Пример #30
0
    def run(self):
        self.transit_dict = {}
        for src in self.srclist:
            self.transit_dict.update({src: []})
        for index, filein in enumerate(self.files):
            with h5.File(filein, 'r') as data:
                self.obser.date = ephem.Date(
                    data.attrs['obstime']) - 8 * ephem.hour
                tstart = data.attrs['sec1970']  # + 8*3600.
                #                print('=========================================')
                #                print('observer time: %s'%data.attrs['obstime'])
                #                print('start time: %s'%datetime.utcfromtimestamp(tstart))

                tend = tstart + (data['vis'].shape[0] -
                                 1) * data.attrs['inttime']
                #                print('end time: %s'%datetime.utcfromtimestamp(tend))
                tend = ephem.julian_date(datetime.utcfromtimestamp(tend))
                #                tephem = aipy.phs.juldate2ephem(tend)
                #                print(ephem.Date(tephem))
                for src, fb in self.srcdict.items():
                    next_tran = self.obser.next_transit(fb)
                    if index == 1:
                        print(src + ':', ephem.Date(next_tran))
                    next_tran = aipy.phs.ephem2juldate(next_tran)
                    if next_tran < tend:
                        self.transit_dict[src].append((index, filein))
Пример #31
0
def obs_params(longitude_deg, latitude_deg, mid_azimuth_deg, mid_elevation_deg,
               mid_date_string, obs_length_sec):
    """Evaluate the observation parameters (RA, Dec, MJD start) for the
    requested azimuth, elevation and time.

    Args:
        longitude_deg (float):     Telescope longitude, in degrees.
        latitude_deg (float):      Telescope latitude, in degrees.
        mid_azimuth_deg (float):   Azimuth at mid-point, in degrees.
        mid_elevation_deg (float): Elevation at mid-point, in degrees.
        mid_date_string (str):     Date and time of mid-point, as a string.
        obs_length_sec (float):    Target observation length, in seconds.

    Returns:
        Tuple containing RA, Dec and MJD start for the given parameters.
    """
    obs = ephem.Observer()
    obs.lon, obs.lat, obs.elevation = \
        math.radians(longitude_deg), math.radians(latitude_deg), 0.0
    obs.date = mid_date_string
    ra, dec = obs.radec_of(math.radians(mid_azimuth_deg),
                           math.radians(mid_elevation_deg))
    ra, dec = math.degrees(ra), math.degrees(dec)
    mjd_mid = ephem.julian_date(obs.date) - 2400000.5
    mjd_start = mjd_mid - obs_length_sec / (2 * 86400.0)
    return ra, dec, mjd_start
Пример #32
0
def calculate_airmass(file_name):
    """
    Calculates AIRMASS for the FITS file and appends respective details in the header of the file 'file_name'
    Args:
        file_name : FITS file whose header has to be edited
    Returns:
        None
    """
    for file_name in list_files:
        hdulist = fits.open(file_name, mode='update')
        file_header = hdulist[0].header

        if str(RA_keyword) in file_header.keys():
            object_ra = file_header[str(RA_keyword)]
        else:
            object_ra = OBJECT_RA

        if str(DEC_keyword) in file_header.keys():
            object_dec = file_header[str(DEC_keyword)]
        else:
            object_dec = OBJECT_DEC

        date_avg = file_header[str(DATEAVG_keyword)]
        date_obs, time_utc = date_avg.split('T')

        datetime_utc = str(date_obs) + ' ' + str(time_utc)
        julian_day = ephem.julian_date(datetime_utc)

        telescope = ephem.Observer()
        telescope.lon = OBS_LONG
        telescope.lat = OBS_LAT
        telescope.elevation = OBS_ALT
        telescope.pressure = 0
        telescope.epoch = ephem.J2000
        telescope.date = datetime_utc
        time_sidereal = telescope.sidereal_time()

        object_pos = ephem.FixedBody()
        object_pos._ra = object_ra
        object_pos._dec = object_dec
        object_pos._epoch = ephem.J2000
        object_pos.compute(telescope)

        object_alt = Angle(str(object_pos.alt) + ' degrees').degree
        airmass = 1 / math.cos(math.radians(90 - object_alt))
        list_keywords = ['LAT', 'LONG', 'ALT', 'TIMEZONE', RA_keyword, DEC_keyword, UT_keyword,
                         DATE_keyword, 'JD', 'ST', 'ELE', 'AZ', 'AIRMASS']
        dict_header = {'LAT': str(OBS_LAT), 'LONG': str(OBS_LONG), 'ALT': str(OBS_ALT), 'TIMEZONE': str(OBS_TIMEZONE),
                       RA_keyword: str(object_ra), DEC_keyword: str(object_dec), DATE_keyword: str(date_obs),
                       UT_keyword: str(time_utc), 'JD': str(julian_day), 'ST': str(time_sidereal),
                       'ELE': str(object_pos.alt), 'AZ': str(object_pos.az), 'AIRMASS': str(airmass)}

        for keyword in list_keywords:
            if keyword in file_header.keys():
                file_header.remove(keyword, remove_all=True)
            file_header.append(card=(keyword, dict_header[keyword]))

        hdulist.flush()
        hdulist.close()
Пример #33
0
 def gmst(self):
     if self._gmst is None:
         jd = ephem.julian_date(self.ut)
         T = (jd - 2451545.0) / 36525.0
         gmstdeg = 280.46061837 + (360.98564736629 * (jd - 2451545.0)) + (
             0.000387933 * T * T) - (T * T * T / 38710000.0)
         self._gmst = ephem.degrees(gmstdeg * numpy.pi / 180.0)
     return self._gmst
Пример #34
0
def dzs_search(year):  # 寻找年前冬至月朔日
    if year == 1: year -= 1  # 公元0改为公元前1
    dz = ephem.next_solstice((year - 1, 12))  # 年前冬至
    jd = ephem.julian_date(dz)
    # 可能的三种朔日
    date1 = ephem.next_new_moon(ephem.Date(jd - 2415020 - 0))
    jd1 = ephem.julian_date(date1)
    date2 = ephem.next_new_moon(ephem.Date(jd - 2415020 - 29))
    jd2 = ephem.julian_date(date2)
    date3 = ephem.next_new_moon(ephem.Date(jd - 2415020 - 31))
    jd3 = ephem.julian_date(date3)
    if DateCompare(jd, jd1):  # 冬至合朔在同一日或下月
        return date1
    elif DateCompare(jd, jd2) and (not DateCompare(jd, jd1)):
        return date2
    elif DateCompare(jd, jd3):  # 冬至在上月
        return date3
Пример #35
0
def test_jd_conversions(jd):
    """Test JulianDate.to_datetime and JulianDate.from_datetime are reversible."""
    assert np.allclose(jd,
                       JulianDate(jd).jd)
    assert np.allclose(JulianDate(jd).jd,
                       JulianDate.from_datetime(JulianDate(jd).to_datetime()).jd)
    assert np.allclose(ephem.julian_date(JulianDate(jd).to_datetime()),
                       JulianDate.from_datetime(JulianDate(jd).to_datetime()).jd)
Пример #36
0
def findDZS(year):  # 寻找年前冬至月朔日
    if year == 1: year -= 1  # 公元元年前冬至在公元前1年
    dz = ephem.next_solstice((year - 1, 12))  # 年前冬至
    jd = ephem.julian_date(dz)
    # 可能的三种朔日
    date1 = ephem.next_new_moon(JD2date(jd - 0))
    jd1 = ephem.julian_date(date1)
    date2 = ephem.next_new_moon(JD2date(jd - 29))
    jd2 = ephem.julian_date(date2)
    date3 = ephem.next_new_moon(JD2date(jd - 31))
    jd3 = ephem.julian_date(date3)
    if DateCompare(jd, jd1):  # 冬至合朔在同一日或下月
        return date1
    elif DateCompare(jd, jd2) and (not DateCompare(jd, jd1)):
        return date2
    elif DateCompare(jd, jd3):  # 冬至在上月
        return date3
Пример #37
0
def jd2pyephemdate( jd ):
    """
    Converts a Julian date to a pyephem date object by first
    subtracting the difference between the pyephem zero date
    and the JD zero date.
    """
    pyephem_zero = ephem.julian_date( ephem.Date( 0 ) )
    return  ephem.Date( jd - pyephem_zero )
Пример #38
0
 def GMST(self, date):
     """Compute Greenwich Mean Sidereal Time"""
     jd = E.julian_date(date)
     T = (jd - 2451545.0) / 36525.0
     gmstdeg = 280.46061837 + (360.98564736629 * (jd - 2451545.0)) + (
         0.000387933 * T * T) - (T * T * T / 38710000.0)
     gmst = E.degrees(gmstdeg * N.pi / 180.0)
     return gmst
Пример #39
0
    def cp_obs_time_skybright(self, x, ob, t1, t2, k='+'):
        """compute visibility time (from skybrightness req. S)
            ob = coordinates line in ephem format
            x = {toi} data
            t1,t2 = start/end time to compute
            k = '+' to set values of V,NV,WV periods in {toi}"""
        self.SkyV_period = []
        sky = x['skybright'].split(',')
        obj = ep.readdb(ob)
        obsv = []
        period = []
        i = 0
        skybright = 0
        td = datetime.timedelta(seconds=self.time_acc)
        #t1 = t1 - td
        while t1 <= t2:
            self.oadm.date = t1
            obj.compute(self.oadm)
            moon = ep.Moon(self.oadm)
            if moon.phase < float(sky[1]):
                skybright = 1
            elif float(sky[2]) <= moon.alt * 180 / ep.pi <= float(sky[3]):
                skybright = 1
            else:
                skybright = 0
            obsv.append([skybright, t1])
            t1 += td
        period.append([0, obsv[0]])
        period.append([obsv.index(obsv[-1]), obsv[-1]])
        for i in range(len(obsv) - 1):
            if obsv[i][0] != obsv[i + 1][0]:
                period.append([obsv.index(obsv[i]), obsv[i]])
        sort_period = sorted(period)
        for i in range(len(sort_period) - 1):
            kk = [
                sort_period[i + 1][1][0], x['id'],
                ep.julian_date(sort_period[i][1][1]),
                ep.julian_date(sort_period[i + 1][1][1])
            ]
            if kk[0] != 0:
                self.SkyV_period.append(kk)
        self.oadm.date = self.comp_date
        if k == '+':
            x['SkyV_period'] = self.SkyV_period

        return self.SkyV_period
    def __init__(self, date, site):
        self.Date = date
        self.Site = site
        ''' Predictable data '''
        # 3 by n_fields matrix of ID, RA, Dec
        self.all_fields = np.loadtxt("NightDataInLIS/Constants/fieldID.lis",
                                     dtype="i4, f8, f8",
                                     unpack=True)
        self.time_slots = np.loadtxt("NightDataInLIS/TimeSlots{}.lis".format(
            int(ephem.julian_date(self.Date))),
                                     unpack=True)
        self.altitudes = np.loadtxt("NightDataInLIS/Altitudes{}.lis".format(
            int(ephem.julian_date(self.Date))),
                                    unpack=True)
        self.hour_angs = np.loadtxt("NightDataInLIS/HourAngs{}.lis".format(
            int(ephem.julian_date(self.Date))),
                                    unpack=True)
        #self.Moon_seps = np.loadtxt("MoonSeps{}.lis".format(int(ephem.julian_date(self.Date))), unpack = True)
        self.amass_cstr = np.loadtxt(
            "NightDataInLIS/AirmassConstraints{}.lis".format(
                int(ephem.julian_date(self.Date))),
            unpack=True)
        self.all_n_tot_visits = np.loadtxt(
            "NightDataInLIS/tot_N_visit{}.lis".format(
                int(ephem.julian_date(self.Date))),
            dtype="i4",
            unpack=True)
        self.t_last_v_last = np.loadtxt(
            "NightDataInLIS/t_last_visit{}.lis".format(
                int(ephem.julian_date(self.Date))),
            unpack=True)
        self.coad_depth = self.all_n_tot_visits / (
            np.max(self.all_n_tot_visits) + 1
        )  #!!!!! temporarily!!!!!!!! # TODO Add coadded depth module instead of visit count
        self.vis_of_year = np.zeros(
            len(self.all_fields[0])
        )  #!!!!! temporarily!!!!!!!! # TODO Visibility of the year is currently all zero
        self.sci_prog = np.zeros(
            len(self.all_fields[0]), dtype='int'
        )  #!!!!! temporarily!!!!!!!! # TODO Science program is not considered yet
        self.moon_sep = np.loadtxt("NightDataInLIS/MoonSeps{}.lis".format(
            int(ephem.julian_date(self.Date))),
                                   unpack=True)
        # n_fields by n_fields symmetric matrix, slew time from field i to j
        self.slew_t = np.loadtxt("NightDataInLIS/Constants/slewMatrix.dat",
                                 unpack=True) * ephem.second

        self.n_all_fields = len(self.all_fields[0])
        self.n_time_slots = len(self.time_slots)
        self.t_start = self.time_slots[0]
        self.t_end = self.time_slots[self.n_time_slots - 1]
        self.n_start = find_n(self.t_start, self.t_start, self.t_end,
                              self.n_time_slots, self.time_slots)
        ''' Unpredictable data '''
        self.sky_brightness = np.zeros(
            len(self.all_fields[0]), dtype='int'
        )  #!!!!! temporarily!!!!!!!!    # current sky brightness
        self.temp_coverage = np.zeros(
            len(self.all_fields[0]), dtype='int'
        )  #!!!!! temporarily!!!!!!!!    # temporary 0/1 coverage of the sky including clouds
Пример #41
0
def julian_date(date=None):
    """
    This function converts seconds since the epoch to a julian date.

    Input:

    - ``date``: Date in seconds since the Epoch (Jan 1, 1970 UTC).
    """
    return _ephem.julian_date(ephem_time(date))
Пример #42
0
def julian_date(date=None):
    """
    This function converts seconds since the epoch to a julian date.

    Input:

    - ``date``: Date in seconds since the Epoch (Jan 1, 1970 UTC).
    """
    return _ephem.julian_date(ephem_time(date))
Пример #43
0
def jd2lst(jd):
    """
    Calculates local sideral time in radians from right ascension and julian date
    jd : julian date     
    """
    j0 = ephem.julian_date(0)
    hera.date = jd - j0
    lst = hera.sidereal_time()
    return lst
Пример #44
0
def update_googledex_lastobs(filename, sheetns=["2018B"],ctime=None,certificate='UCSC Dynamic Scheduler-5b98d1283a95.json'):
    """
        Update the online googledex lastobs column assuming things in filename have been observed.
        update_googledex_lastobs(filename, sheetn="The Googledex",time=None,certificate='UCSC Dynamic Scheduler-5b98d1283a95.json')

        filename - where the observations are logged
    """
    names, times = ObservedLog.getObserved(filename)
    if len(names) == 0:
        return
    if ctime is None:
        ctime = datetime.utcfromtimestamp(int(time.time()))
    

    for sheetn in sheetns:
        ws = get_spreadsheet(sheetn=sheetn,certificate=certificate)
        if ws:
            vals = ws.get_all_values()
        else:
            next
        col = vals[0].index("lastobs") 
        nobscol = vals[0].index("Nobs")
    
        for i, v in enumerate(vals):
            # Did we observe this target tonight?
            if v[0] in names:
                # We observed this target, so update the cell in the worksheet
                # update_cell(row, col, val) - col and row are 1 indexed
                otime = times[names.index(v[0])]
                if isinstance(otime,float):
                    t = datetime.utcfromtimestamp(otime)
                else:
                    hr, mn = otime
                    t = datetime(ctime.year, ctime.month, ctime.day, hr, mn)
                jd = float(ephem.julian_date(t))
                try:
                    pastdate = float(v[col])
                    try:
                        n = int(v[nobscol])
                    except:
                        n = 0
                    if jd > pastdate:
                        ws.update_cell(i+1, col+1, round(jd, 2) )
                        ws.update_cell(i+1, nobscol+1, n + 1 )

                except:
                    print (v[0], v[col])
                    ws.update_cell(i+1, col+1, round(jd,2) )
                
            apflog( "Updated %s" % (sheetn),echo=True)

    return
Пример #45
0
def do_objs(kbos):
    """Draw the actual plot"""

    import orbfit, ephem, math
    import re
    re_string=w.FilterVar.get()
    vlist=[]
    for name in kbos:
        if not re.search(re_string,name):
            continue
        vlist.append(name)
        if type(kbos[name])==type(ephem.EllipticalBody()):
	    kbos[name].compute(w.date.get())
	    ra=kbos[name].ra
	    dec=kbos[name].dec
	    a=math.radians(10.0/3600.0)
	    b=a
	    ang=0.0
	    color='blue'
            yoffset=+10
	    xoffset=+10
        else:
            yoffset=-10
            xoffset=-10
            file=kbos[name]
	    jdate=ephem.julian_date(w.date.get())
	    obs=568
 	    try:
	        position=orbfit.predict(file,jdate,obs)
	    except:
		continue
	    ra=math.radians(position[0])
	    dec=math.radians(position[1])
	    a=math.radians(position[2]/3600.0)
	    b=math.radians(position[3]/3600.0)
	    ang=math.radians(position[4])
	    if ( a> math.radians(1.0) ):
	        color='green'
            else:
	        color='black'
	if w.show_ellipse.get()==1 :
            if ( a < math.radians(5.0) ): 
	        w.create_ellipse(ra,dec,a,b,ang)
        if ( a < math.radians(1.0) ): 
            w.create_point(ra,dec,size=2,color=color)
	if w.show_labels.get()==1:
	    w.label(ra,dec,name,offset=[xoffset,yoffset])
    vlist.sort()
    for v in vlist:
        w.objList.insert(END,v)
    w.plot_pointings()
Пример #46
0
    def __getitem__(self, name):
        if name not in self.satellites:
            q = callhorizons.query(name, smallbody=False)
            q.set_discreteepochs(ephem.julian_date(self.epoch))
            try:
                sats = q.export2pyephem()
            except ValueError as e:
                if e.message.startswith('Unknown target'):
                    raise KeyError('%s not found in %s' % (name, repr(self)))
                raise

            self.satellites[name] = sats[0]

        return self.satellites[name]
Пример #47
0
def getLST(date, longitude):
    """Take a datetime and longitude and calculate the Local Sidereal Time."""
    # Assumes date is a datetime object, and that the longitude is formatted as in PyEphem 

    ll = [float(v) for v in longitude.split(':')]
    if ll[0] > 0:
        sign = 1
    else:
        sign = -1
    ut = date.hour + date.minute/60. + date.second/3600.
    lng = ll[0] + sign*ll[1]/60. + sign*ll[2]/3600.
    d  = ephem.julian_date() - 2451545.0
    lst = 100.46 + 0.985647 * d + lng + 15*ut
    return lst % 360.
Пример #48
0
    def relocate(self):
        """Move to the postion of self.SearchVar"""

        name=self.SearchVar.get()
        if kbos.has_key(name):
	    import orbfit,ephem,math

	    jdate=ephem.julian_date(w.date.get())
 	    try:
	        (ra,dec,a,b,ang)=orbfit.predict(kbos[name],jdate,568)
	    except:
		return
	    ra=math.radians(ra)
	    dec=math.radians(dec)
	elif mpc_objs.has_key(name):
	    ra=mpc_objs[name].ra
	    dec=mpc_objs[name].dec
	self.recenter(ra,dec)
	self.create_point(ra,dec,color='blue',size=4)
Пример #49
0
def readin_lastobs(filename,ctime):
    codex = False
    try:
        fp = open(filename,'rb')
        full_codex = pickle.load(fp)
        fp.close()
        codex = True
        colhead = full_codex[0]
        codex = full_codex[1:]
        # These are the columns we need for scheduling
        req_cols = ["Star Name", "lastobs", "Template", "Nobs"]
        didx = findColumns(colhead,req_cols)
        
    except :
        codex = False
        names, times = ObservedLog.getObserved(filename)
        if len(names) == 0:
            return
        if ctime is None:
            ctime = datetime.utcfromtimestamp(int(time.time()))
        

    lastjds = []
    fnames = []
    nobs = []
    if codex:
            
        for cline in codex:
            lastjds.append(float(cline[didx['lastobs']]))
            fnames.append(cline[didx['Star Name']])
            nobs.append(int(cline[didx['Nobs']]))
    else:
        for i in range(0,len(names)):
            fnames.append(names[i])
            otime = times[i]
            if isinstance(otime,float):
                t = datetime.utcfromtimestamp(otime)
            else:
                hr, mn = otime
                t = datetime(ctime.year, ctime.month, ctime.day, hr, mn)
            lastjds.append(float(ephem.julian_date(t)))
            
    return fnames, lastjds
Пример #50
0
def gen_time_axis(timestamps,scale,offset):
    """ Return a list of real times from the timestamp vector"""
    # get the timestamps
    timestamps = numpy.array(timestamps, dtype=float)
    t = numpy.zeros(len(timestamps),dtype=numpy.float64)
    t = numpy.array(offset + timestamps/scale,dtype=numpy.float64) #UNIX times
    t_range = t[-1] - t[0]
    gmt_ref = time.gmtime(t[0]) # Start time in UTC
    jd_ref = ephem.julian_date(gmt_ref[0:6])

    if t_range < 300:
        scale = 'Seconds since %.4d/%.2d/%.2d %.2d:%.2d:%.2d UTC' %(gmt_ref[0:6])
        t = (t-t[0])
    elif t_range < 60*60*3:
        scale = 'Minutes since %.2d/%.2d/%.2d %.2d:%.2d:%.2d UTC' %(gmt_ref[0:6])
        t = (t-t[0])/60
    else:
        scale = 'Hours since %.2d/%.2d/%.2d %.2d:%.2d:%.2d UTC' %(gmt_ref[0:6])
        t = (t-t[0])/60/60
    return {'times':t, 'scale':scale, 'ref':t[0], 'gmtref':gmt_ref, 'jd_ref':jd_ref, 'unit':''}
Пример #51
0
def opposition(coordinate):
    ec = ephem.Ecliptic(0, 0)
    ra = ephem.hours(coordinate.ra.radians)
    dec = ephem.degrees(coordinate.dec.radians)
    ec.from_radec(ra, dec)

    opp_angle = math.pi
    opp_date = -1

    start_date = ephem.date('2013/01/01')
    sun = ephem.Sun()
    for day in range(365):
        d = ephem.date(start_date + day)
        sun.compute(d)
        opp = math.fabs(ephem.degrees(ec.lon - sun.hlon))
        if opp < opp_angle:
            opp_angle = opp
            opp_date = ephem.date(start_date + day)
    sun.compute(opp_date)
    print sun.ra, coordinate.ra, opp_date   
    return ephem.julian_date(opp_date)
Пример #52
0
def get_juldate(local_time, tzone='UTC+08h'):
    """Convert `local_time` to Julian date.

    Parameters
    ----------
    local_time : string, python date or datetime object, etc.
        Local time that can be passed to ephem.Date function.
        Refer to http://rhodesmill.org/pyephem/date.html
    tzone : string, optional
        Time zone in format 'UTC[+/-]xxh'. Defaut: UTC+08h.

    Returns
    -------
    julian_date : float
        A float number representation of a Julian date.

    See Also
    --------
    `get_ephdate`
    """

    return ephem.julian_date(get_ephdate(local_time, tzone))
Пример #53
0
def cur_sidereal(longitude,val):
    global doephem
    if doephem == False:
        return (("12:00:00","9999999999"))
    longstr = "%02d" % int(longitude)
    longstr = longstr + ":"
    longitude = abs(longitude)
    frac = longitude - int(longitude)
    frac *= 60
    mins = int(frac)
    longstr += "%02d" % mins
    longstr += ":00"
    x = ephem.Observer()
    x.date = ephem.now()
    x.long = longstr
    jdate = ephem.julian_date(x)
    tokens=str(x.sidereal_time()).split(":")
    hours=int(tokens[0])
    minutes=int(tokens[1])
    seconds=int(float(tokens[2]))
    sidt = "%02d:%02d:%02d" % (hours, minutes, seconds)
    return ((sidt,jdate))
def updateHeaderTime(infile,outfile):
        '''
        outputs a file containing the corrected times based on the start time 
        of the observation and not the time the file was written
        v1.0 Kieran O'Brien - Aug 2011
        '''
        inh5file=openFile(infile,mode='r')
        inh5file.copyFile(outfile,overwrite=True)
        inh5file.close()
        outh5file=openFile(outfile,mode='a')
#        try:
        timestamp=int(outh5file.root.r0.p0._v_children.keys()[0].strip('t'))
        dt=datetime.datetime.utcfromtimestamp(timestamp)
        jd=np.float64(ephem.julian_date(dt))
        table=outh5file.root.header.header
        table.cols.jd[0]=jd
        table.cols.ut[0]=timestamp
        table.flush()    
#            outfile.copyNode(infile.root.header,newparent=outfile.root,recursive=True)  
#        except:
#            print 'no header'
#            pass
        outh5file.close()        
Пример #55
0
def gen_ha_axis(timestamps,scale,offset,RA):
    """ Return a list of real times from the timestamp vector"""
    # get the timestamps
    timestamps = numpy.array(timestamps, dtype=float)
    t = numpy.zeros(len(timestamps),dtype=numpy.float64)
    t = numpy.array(offset + timestamps/scale,dtype=numpy.float64) #UNIX times
    gmt_ref = time.gmtime(t[0]) # Start time in UTC
    jd_ref = ephem.julian_date(gmt_ref[0:6])
    ha = numpy.zeros_like(t)
    for i,ti in enumerate(t):
        obs.date = ephem.Date(time.gmtime(ti)[0:6])
        #print "time is", obs.date
        ha[i] = obs.sidereal_time() - RA
        #print obs.sidereal_time(), RA, obs.sidereal_time()-RA
    # Unwrap the phases, otherwise weird things can happen
    # When the lst crosses midnight
    ha = numpy.unwrap(ha)
    ha = numpy.rad2deg(ha)
    if RA == 0:
        scale = 'Local Sidereal Time'
    else:
        scale = 'Hour Angle'
    return {'times':ha, 'scale':scale, 'ref':RA, 'gmtref':gmt_ref, 'jd_ref':jd_ref, 'unit':''}
Пример #56
0
def main():
	
	obsfile_name = "/nfs/eor-00/h1/rbyrne/sidelobe_survey_obsIDs.txt"
	obsfile = open(obsfile_name, "r")
	obsids = [line.split( ) for line in obsfile.readlines()]
	obsids = [obs[0] for obs in obsids]
	obsfile.close()

	#t = Time([int(obsid) for obsid in obsids], format="gps", scale="utc")
	t = Time([1130772904], format = "gps", scale = "utc")
	jdates = t.jd
	jdate = jdates[0]

	az = np.radians(198.435)
	el = np.radians(67.98)

	#approximate values, fix these:
	lon = np.radians(116.6)
	lat = np.radians(-26.75)
	alt = 378

	jdate0 = ephem.julian_date(0)

	observer = ephem.Observer()
	observer.lon = lon
	observer.lat = lat
	observer.elevation = alt
	observer.date = jdate - jdate0

	lst = observer.sidereal_time()
	lst_hrs = float(lst)*23.9344699/(2*math.pi)
	print lst_hrs
	print lst
	ra,dec = observer.radec_of(az, el)

	print "EPHEM: %f %f" % (np.degrees(ra),np.degrees(dec))
Пример #57
0
def synthetic_model_kbos(coverage, input_date=newMoons['Oct13']):
    ## build a list of KBOs that will be in the discovery fields.
    ra=[]
    dec=[]
    kbos = []
    for line in open('L7SyntheticModel-v09.txt'):
       if line[0]=='#':
        continue
       kbo = ephem.EllipticalBody()
       values = line.split()
       kbo._a = float(values[0])
       kbo._e = float(values[1])
       kbo._inc = float(values[2])
       kbo._Om = float(values[3])
       kbo._om = float(values[4])
       kbo._M = float(values[5])
       kbo._H = float(values[6])
       kbo._epoch_M = ephem.date(2453157.50000 - ephem.julian_date(0))
       kbo._epoch = kbo._epoch_M
       kbo.name = values[8]
       date = ephem.date(input_date)
       kbo.compute(date)

       ### only keep objects that are brighter than limit
       if kbo.mag > 25.0:
         continue
       ra.append(math.degrees(float(kbo.ra)))
       dec.append(math.degrees(float(kbo.dec)))

       ## keep a list of KBOs that are in the discovery pointings
       for field in coverage:
         if field.isInside(ra[-1],dec[-1]):
           kbos.append(kbo)
           break

    return ra, dec, kbos
Пример #58
0
def compute_simulation(curtime,result,star,apf_obs,slowdowns,fwhms,star_tab,owner):
    actel,actaz = ns.compute_el(curtime,star,apf_obs)
    actslow, actfwhm = ns.rand_obs_sample(slowdowns,fwhms)
    if actslow < 0.3:
        actslow = 0.3
    actfwhm = ns.gen_seeing_el(actfwhm,actel)
    lastfwhm = actfwhm
    lastslow = actslow
    metersig = np.random.randn(1)
    specsig = np.random.randn(1)
    if abs(specsig) > 3:
        specsig = 3.
    if abs(metersig) > 3:
        metersig = 3.
    
    meterrate = ec.getEXPMeter_Rate(result['VMAG'],result['BV'],actel,actfwhm,result['DECKER'])
    meterrate *= 1 + 0.11*metersig
    meterrate /= actslow
    specrate = ec.getSpec_Rate(result['VMAG'],result['BV'],actel,actfwhm,result['DECKER'])
    specrate *= 1 + 0.11*specsig
    specrate /= actslow
    metertime = result['COUNTS'] / meterrate
    exp_time = result['EXP_TIME']
    barycentertime = curtime
    if metertime < exp_time:
        fexptime = metertime
    else:
        fexptime = exp_time
        
    curtime += (fexptime+40.)/86400
    barycentertime += fexptime/(2.*86400)
    totcounts = fexptime * specrate

    outstr = "%s %s %.5f %.1f %.1f %.2f %.2f %.2f %.2f %s" %(result['NAME'] , ephem.Date(curtime), ephem.julian_date(ephem.Date(barycentertime)), fexptime, totcounts,  actel,actaz, actfwhm, actslow, owner)

    return curtime, lastfwhm, lastslow, outstr
Пример #59
0
    # # line1 contains commented fermi data
    # # line2 is an xephem database entry
    if re.match("#",line1) == None: continue

    error = float(re.search('(\d+.\d\d)\n$',line1).group(1))
    # if the error is greater than 10 degrees, throw away
    if error > 10: continue

    noqsi.date = re.search(r"..../../.. ..:..:......",line1).group()

    # if the date is before 2010/4/1, quit 
    # since we don't have measurements as early as that
    if noqsi.date < ephem.date('2010/4/1'):
        exit()

    line2 = sys.stdin.readline()

    entry = ephem.readdb(line2)
    entry.name = re.search(r"^..(\w+)",line1).group(1)
    entry.compute(noqsi)
    entry_alt_degrees = entry.alt/pi*180

    sun = ephem.Sun()
    sun.compute(noqsi)
    sun_alt_degrees = sun.alt/pi*180

    if 0 < entry.alt: # if the entry is above the horizon, print
       print "%s\t%s\t%f\t%d\t%f\t%f\t%f" % \
             (entry.name, noqsi.date, ephem.julian_date(noqsi),\
              ephem2unix(noqsi.date), sun_alt_degrees, entry_alt_degrees, error)
Пример #60
0
def config_uv_data(h5fh, tbl_uv_data, antpos, obs, src, ti, verbose=False):
    
    if verbose:
        print('\nGenerating file metadata')
        print('--------------------------')
  
    # Data is stored in multidimensional array called xeng_raw0
    # time, channels, baselines, polarisation, then data=(real, imaginary) 
    (t_len, chan_len, bl_len, pol_len, ri_len) = h5fh['xeng_raw0'].shape
    int_time = h5fh.attrs['int_time']
    h5data = h5fh['xeng_raw0'][ti]
    
    timestamps = []
    baselines = []
    #weights = [1  in range(0,chan_len*2)]
    
    if verbose: print('Retrieving timestamps...')
    timestamps = h5fh['timestamp0'][ti]
  
    # Date and time
    # Date is julian date at midnight that day
    # The time is DAYS since midnight
    firststamp = timestamps[0]
    julian = ephem.julian_date(time.gmtime(firststamp)[:6])
    midnight = int(firststamp)
    
    # Ephem returns julian date at NOON, we need at MIDNIGHT
    julian_midnight = int(julian)+1
  
    elapsed = []
    for timestamp in timestamps:
        elapsed.append((ephem.julian_date(time.gmtime(timestamp)[:6]) - julian_midnight))
 
    if verbose: print('Creating baseline IDs...')
    bl_order = h5fh['bl_order'].value
    #WARNING: hardcore the antenna position frequency (GHz)
    cfreq=.408
    for bl in range(0,bl_len):
        # Baseline is in stupid 256*baseline1 + baseline2 format
        ant1, ant2 = bl_order[bl][0], bl_order[bl][1] 
        bl_id = 256*ant1 + ant2
        
        # Generate the XYZ vectors too
        # From CASA measurement set definition
        # uvw coordinates for the baseline from ANTENNE2 to ANTENNA1, 
        # i.e. the baseline is equal to the difference POSITION2 - POSITION1. 
        bl_vector = (antpos[ant2] - antpos[ant1])*cfreq
        #print bl_vector, antpos[ant2], antpos[ant1]
        baselines.append((bl_id,bl_vector))  
      
    if verbose: print('Computing UVW coordinates...\n')
    # Extract the timestamps and use these to make source our phase centre
    uvws = []
    for timestamp in timestamps:
        t = datetime.datetime.utcfromtimestamp(timestamp)
        if verbose: print t
        obs.date=t
        src.compute(obs)
        
        for baseline in baselines:
            vector = baseline[1]
            H, d = (obs.sidereal_time() - src._ra, src._dec)
            uvws.append(computeUVW(vector,H,d))
  
    # This array has shape t_len, num_ants, 3
    # and units of SECONDS
    uvws = np.array(uvws)
    uvws = uvws.reshape(uvws.size/bl_len/3,bl_len,3) / ephem.c
  
    if verbose:
        print('\nReformatting HDF5 format -> FITS IDI UV_DATA')
        print('--------------------------------------------')
     
    # The actual data matrix is stored per row as a multidimensional matrix
    # with the following mandatory axes:
    # COMPLEX     Real, imaginary, (weight)
    # STOKES      Stokes parameter
    # FREQ        Frequency (spectral channel)
    # RA          Right ascension of the phase center
    # DEC         Declination of the phase center 
    flux = np.ndarray(shape=(chan_len,1,ri_len))
     
    # This step takes ages.
    # I imagine there is some way to massage the hdf5 array
    # to do this a lot quicker than iterating over the indexes 
    if verbose: print('\nCreating multidimensional UV matrix...')
    for t in range(0,len(ti)):
        if verbose: print('processing time sample set %i/%i'%(t+1,len(ti)))
        # The time is seconds since midnight
        tbl_uv_data.data['TIME'][t*bl_len:(t+1)*bl_len] = np.ones(bl_len)*elapsed[t]
        tbl_uv_data.data['DATE'][t*bl_len:(t+1)*bl_len] = julian_midnight
        tbl_uv_data.data['INTTIM'][t*bl_len:(t+1)*bl_len] = np.ones(bl_len)*int_time
        #tbl_uv_data.data['SOURCE'][t*bl_len:(t+1)*bl_len]= np.ones(bl_len,dtype=np.int32)*1
        tbl_uv_data.data['SOURCE_ID'][t*bl_len:(t+1)*bl_len]= np.ones(bl_len,dtype=np.int32)*1
        tbl_uv_data.data['FREQID'][t*bl_len:(t+1)*bl_len]   = np.ones(bl_len,dtype=np.int32)*1
        tbl_uv_data.data['UU'][t*bl_len:(t+1)*bl_len] = uvws[t,:,0]
        tbl_uv_data.data['VV'][t*bl_len:(t+1)*bl_len] = uvws[t,:,1]
        tbl_uv_data.data['WW'][t*bl_len:(t+1)*bl_len] = uvws[t,:,2]
        for bl in range(0,bl_len):
            # Create a 1D index for the uv_data table
            i = t*bl_len + bl
            
            # Swap real and imaginary
            flux[:,0,0] = h5data[t,:,bl,0,1]
            flux[:,0,1] = h5data[t,:,bl,0,0]
            
            tbl_uv_data.data[i]['FLUX']     = flux.ravel()
            tbl_uv_data.data[i]['BASELINE'] = baselines[bl][0]
    
    if verbose:
        print('\nData reformatting complete')
        print('DONE.')
    return tbl_uv_data