def get_sun_image(time, wavelength, image_size = 1023):
    try:
        time_str = time.strftime("%Y/%m/%d/%H%M.fits")
        if wavelength == 'hmi':
            filename = "/work1/t2g-16IAS/hmi/" + time_str
        else:
            filename = "/work1/t2g-16IAS/aia{:04}/".format(wavelength) + time_str
        aia_image = fits.open(filename)

        aia_image.verify("fix")
        if wavelength == 'hmi':
            exptime = 1
        else:
            exptime = aia_image[1].header['EXPTIME']
            if exptime <= 0:
                print(time, "non-positive exposure",file=sys.stderr)
                return None

        quality = aia_image[1].header['QUALITY']
        if quality !=0:
            print(time, "bad quality",file=sys.stderr)
            return None

        original_width = aia_image[1].data.shape[0]

        return interpolation.zoom(np.nan_to_num(aia_image[1].data), image_size / float(original_width)) / exptime
    except Exception as e:
        print(e,file=sys.stderr)
        return None
Esempio n. 2
0
def day_of_year(time_string):
    """
    Returns the (fractional) day of year.

    Note: This function takes into account leap seconds.

    Parameters
    ----------
    time_string : str
        A parse_time compatible string

    Returns
    -------
    out : float
        The fractional day of year (where Jan 1st is 1).

    Examples
    --------
    >>> import sunpy.time
    >>> sunpy.time.day_of_year('2012/01/01')
    1.0
    >>> sunpy.time.day_of_year('2012/08/01')
    214.00001157407408
    >>> sunpy.time.day_of_year('2005-08-04T00:18:02.000Z')
    216.01252314814815

    """
    SECONDS_IN_DAY = 60 * 60 * 24.0
    time = parse_time(time_string)
    time_diff = time - Time.strptime(time.strftime('%Y'), '%Y')
    return time_diff.jd + 1
Esempio n. 3
0
def day_of_year(time_string):
    """
    Returns the (fractional) day of year.

    Note: This function takes into account leap seconds.

    Parameters
    ----------
    time_string : str
        A parse_time compatible string

    Returns
    -------
    out : float
        The fractional day of year (where Jan 1st is 1).

    Examples
    --------
    >>> import sunpy.time
    >>> sunpy.time.day_of_year('2012/01/01')
    1.0
    >>> sunpy.time.day_of_year('2012/08/01')
    214.00001157407408
    >>> sunpy.time.day_of_year('2005-08-04T00:18:02.000Z')
    216.01252314814815

    """
    SECONDS_IN_DAY = 60 * 60 * 24.0
    time = parse_time(time_string)
    time_diff = time - Time.strptime(time.strftime('%Y'), '%Y')
    return time_diff.jd + 1
Esempio n. 4
0
def utc(unix_t=None, fmt='%a %b %d %X %Y'):
    '''Return (current) UTC time as a string.
    Parameters
    ----------
    unix_t : seconds since the Epoch, default=now
    fmt    : format string (see time.strftime), default produces 
             "Mon Jan 23 14:56:59 2018"

    Returns
    -------
    t : string, e.g. "Mon Jan 23 14:56:59 2018"'''
    gmt = time.gmtime(unix_t)
    return time.strftime(fmt, gmt)
Esempio n. 5
0
    def generate_positions(self, times, observing_body, frame,
                           abcorr=None):
        """
        Generate positions from a spice kernel.

        Parameters
        ----------
        times : time like
            An object that can be parsed by `~astropy.time.Time`.
        observing_body : str or int
            The observing body. Output position vectors are given relative to
            the position of this body. See
            https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/req/naif_ids.html
            for a list of bodies.
        frame : str
            The coordinate system to return the positions in. See
            https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/req/frames.html
            for a list of frames.
        abcorr : str, optional
            By default no aberration correciton is performed.
            See the documentaiton at
            https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/spkezr_c.html
            for allowable values and their effects.
        """
        times = time.Time(times)
        # Spice needs a funny set of times
        fmt = '%Y %b %d, %H:%M:%S'
        spice_times = [sp.str2et(time.strftime(fmt)) for time in times]
        self._abcorr = str(abcorr)

        # Do the calculation
        pos_vel, lightTimes = sp.spkezr(
            self.target.name, spice_times, frame, self._abcorr,
            observing_body)

        positions = np.array(pos_vel)[:, :3] * u.km
        velocities = np.array(pos_vel)[:, 3:] * u.km / u.s
        self.light_times=np.array(lightTimes)

        self._frame = frame
        self._times = time.Time(times)
        self._velocities = velocities
        self._x = positions[:, 0]
        self._y = positions[:, 1]
        self._z = positions[:, 2]
        self._vx = velocities[:, 0]
        self._vy = velocities[:, 1]
        self._vz = velocities[:, 2]
        self._generated = True
        self._observing_body = Body(observing_body)
Esempio n. 6
0
    def generate_positions(self, times, observing_body, frame):
        """
        Generate positions from a spice kernel.

        Parameters
        ----------
        times : time like
            An object that can be parsed by `~astropy.time.Time`.
        observing_body : str or int
            The observing body. Output position vectors are given relative to
            the position of this body. See
            https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/req/naif_ids.html
            for a list of bodies.
        frame : str
            The coordinate system to return the positions in. See
            https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/req/frames.html
            for a list of frames.
        """
        times = time.Time(times)
        # Spice needs a funny set of times
        fmt = '%Y %b %d, %H:%M:%S'
        spice_times = [spiceypy.str2et(time.strftime(fmt)) for time in times]
        light_travel_correction = 'None'

        # Do the calculation
        pos_vel, lightTimes = spiceypy.spkezr(self.target, spice_times, frame,
                                              light_travel_correction,
                                              observing_body)

        positions = np.array(pos_vel)[:, :3] * u.km
        velocities = np.array(pos_vel)[:, 3:] * u.km / u.s

        self._frame = frame
        self._times = time.Time(times)
        self._velocities = velocities
        self._x = positions[:, 0]
        self._y = positions[:, 1]
        self._z = positions[:, 2]
        self._vx = velocities[:, 0]
        self._vy = velocities[:, 1]
        self._vz = velocities[:, 2]
        self._generated = True
        self._observing_body = observing_body
Esempio n. 7
0
def get_time_now(mytz):
    """
    Get current time in this time zone.  Also supports LST, JD, and MJD
    
    Args:
        mytz: abbrevation e.g. CLT
    """
    if mytz.upper() == 'LST':
        return get_lst(gemini_longitude) + " (Gemini South)"
    
    if mytz.upper() == 'JD'or mytz.upper() == 'MJD':
        return get_jd(modified=('M' in mytz.upper()))
    
    tz_from = get_timezone(mytz)
    if tz_from is None:
        return None
        
    time = datetime.now(tz_from)

    fmt = "%I:%M %p (UT%z)"
    return time.strftime(fmt)
Esempio n. 8
0
    def generate_positions(self, times, observing_body, frame):
        """
        Generate positions from a spice kernel.

        Parameters
        ----------
        times : iterable of `datetime`
            An iterable (e.g. a `list`) of `datetime` objects at which the
            positions are calculated.
        observing_body : str or int
            The observing body. Output position vectors are given relative to
            the position of this body. See
            https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/req/naif_ids.html
            for a list of bodies.
        frame : str
            The coordinate system to return the positions in. See
            https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/req/frames.html
            for a list of frames.
        """
        # Spice needs a funny set of times
        fmt = '%Y %b %d, %H:%M:%S'
        spice_times = [spiceypy.str2et(time.strftime(fmt)) for time in times]
        # 'None' specifies no light-travel time correction
        pos_vel, lightTimes = spiceypy.spkezr(self.target, spice_times, frame,
                                              'None', observing_body)
        positions = np.array(pos_vel)[:, :3] * u.km
        velocities = np.array(pos_vel)[:, 3:] * u.km / u.s

        self._frame = frame
        self._times = time.Time(times)
        self._velocities = velocities
        self._x = positions[:, 0]
        self._y = positions[:, 1]
        self._z = positions[:, 2]
        self._vx = velocities[:, 0]
        self._vy = velocities[:, 1]
        self._vz = velocities[:, 2]
        self._generated = True
        self._observing_body = observing_body
def write_file(c,n,times,dat,swp):
 fmt="%m-%d-%Y_%H-%M-%S"
 t0=times[0]
 t0_str = time.strftime(fmt,time.localtime(t0))
 print 'writing obs_dat %s.uv....' % t0_str
 sys.stdout.flush()
 uv = aipy.miriad.UV('obs_dat_%s.uv' % t0_str,'new')
 uv['history'] = 'this observation started at %s ' % t0_str

 pols = [-5]
 uv._wrhd('obstype','auto-cross')
 #uv._wrhd('history','MDLVIS: created file.\nMDLVIS: ' + ' '.join(sys.argv) + '\n')
 uv.add_var('telescop','a'); uv['telescop'] = 'AIPY'
 uv.add_var('operator','a'); uv['operator'] = 'AIPY'
 uv.add_var('version' ,'a'); uv['version'] = '0.0.1'
 uv.add_var('epoch'   ,'r'); uv['epoch'] = 2000.
 uv.add_var('source'  ,'a'); uv['source'] = 'zenith'
 uv.add_var('latitud' ,'d'); uv['latitud'] = 0.0
 uv.add_var('dec'     ,'d'); uv['dec'] = 0.0
 uv.add_var('obsdec'  ,'d'); uv['obsdec'] = 0.0
 uv.add_var('longitu' ,'d'); uv['longitu'] = 0.0
 uv.add_var('npol'    ,'i'); uv['npol'] = len(pols)
 uv.add_var('nspect'  ,'i'); uv['nspect'] = 1
 uv.add_var('nants'   ,'i'); uv['nants'] = 2
 uv.add_var('antpos'  ,'d')
 antpos = np.array(([0,0,0],[0,0,1]), dtype=np.double)
 uv['antpos'] = antpos.transpose().flatten()
 uv.add_var('sfreq'   ,'d'); uv['sfreq'] = 0.08
 uv.add_var('freq'    ,'d'); uv['freq'] = 0.16
 uv.add_var('restfreq','d'); uv['restfreq'] = 0.
 uv.add_var('sdf'     ,'d'); uv['sdf'] = .00015625
 uv.add_var('nchan'   ,'i'); uv['nchan'] = 512
 uv.add_var('nschan'  ,'i'); uv['nschan'] = 0
 uv.add_var('inttime' ,'r'); uv['inttime'] = 0.
 # These variables just set to dummy values
 uv.add_var('vsource' ,'r'); uv['vsource'] = 0.
 uv.add_var('ischan'  ,'i'); uv['ischan'] = 1
 uv.add_var('tscale'  ,'r'); uv['tscale'] = 0.
 uv.add_var('veldop'  ,'r'); uv['veldop'] = 0.
 # These variables will get updated every spectrum
 #uv.add_var('coord'   ,'d')
 uv.add_var('time'    ,'d')
 uv.add_var('lst'     ,'d')
 uv.add_var('ra'      ,'d')
 uv.add_var('obsra'   ,'d')
 #uv.add_var('baseline','r')
 uv.add_var('pol'     ,'i')

 uv.add_var('switch','i');

 c2=float(c)/1024
 uvw=np.array([(c*0.5*(n-1)),c2,(c*0.5*n)],dtype=np.double)

 for i in range(len(times)):
  uv['pol']=pols[0]
  uv['lst']=0.
  uv['ra']=0.
  uv['obsra']=0.

  for key in dat[i].keys():
    if key == 'xx':
     preamble = (uvw,return_jd(times[i]),(0,0))
     if (n%2)==0:
      dat[i]['xx']=dat[i]['xx'][::-1]
     data=np.ma.array(dat[i]['xx'],mask=np.array(5*[1]+(len(dat[i]['xx'])-10)*[0]+5*[1]))
     uv.write(preamble,data)
    elif key == 'xy':
     preamble = (uvw,return_jd(times[i]),(0,1))
     if (n%2)==0:
      dat[i]['xy']=dat[i]['xy'][::-1]
     #data=np.ma.array(dat[i]['xy'],mask=np.zeros(len(dat[i]['xy'])))
     data=np.ma.array(dat[i]['xy'],mask=np.array(5*[1]+(len(dat[i]['xy'])-10)*[0]+5*[1]))
     uv.write(preamble,data)
    else:
     preamble = (uvw,return_jd(times[i]),(1,1))
     if (n%2)==0:
      dat[i]['yy']=dat[i]['yy'][::-1]
     #data=np.ma.array(dat[i]['yy'],mask=np.zeros(len(dat[i]['yy'])))
     data=np.ma.array(dat[i]['yy'],mask=np.array(5*[1]+(len(dat[i]['yy'])-10)*[0]+5*[1]))
     uv.write(preamble,data)
 del(uv)

 time.sleep(0.5)
 t1=times[-1]
 t_d=(t1-t0)
 print 'done in %.2f seconds' % t_d