def test_is_time(): assert time.is_time(datetime.utcnow()) is True assert time.is_time('2017-02-14 08:08:12.999', "%Y-%m-%d %H:%M:%S.%f") is True assert time.is_time(None) is False assert time.is_time('2016-14-14 19:08', "%Y-%b-%d %H:%M:%S") is False
def get_properties(cls, header): """Parses a map header and determines default properties.""" if is_time(header.get('date-obs',[])): # Hack! check FITS standard is a time date = header.get('date-obs') # Check commonly used but non-standard FITS keyword for observation time is a time elif is_time(header.get('date_obs',[])): # Horrible [] hack date = header.get('date_obs') else: date = None return { "cmap": cm.gray, # @UndefinedVariable "date": parse_time(date) if date is not None else 'N/A', "detector": header.get('detector', ''), "dsun": header.get('dsun_obs', constants.au), "exposure_time": header.get('exptime', 0.), "instrument": header.get('instrume', ''), "measurement": header.get('wavelnth', ''), "observatory": header.get('telescop', ''), "name": header.get('telescop', '') + " " + str(header.get('wavelnth', '')), "nickname": header.get('detector', ''), "rsun_meters": header.get('rsun_ref', constants.radius), "rsun_arcseconds": header.get('rsun_obs', header.get('solar_r', header.get('radius', constants.average_angular_size))), "coordinate_system": { 'x': header.get('ctype1', 'HPLN-TAN'), 'y': header.get('ctype2', 'HPLT-TAN') }, "carrington_longitude": header.get('crln_obs', 0.), "heliographic_latitude": header.get('hglt_obs', header.get('crlt_obs', header.get('solar_b0', 0.))), "heliographic_longitude": header.get('hgln_obs', 0.), "reference_coordinate": { 'x': header.get('crval1', 0.), 'y': header.get('crval2', 0.), }, "reference_pixel": { 'x': header.get('crpix1', (header.get('naxis1') + 1) / 2.), 'y': header.get('crpix2', (header.get('naxis2') + 1) / 2.) }, "scale": { 'x': header.get('cdelt1', 1.), 'y': header.get('cdelt2', 1.), }, "units": { 'x': header.get('cunit1', 'arcsec'), 'y': header.get('cunit2', 'arcsec') }, "rotation_angle": { 'x': header.get('crota1', 0.), 'y': header.get('crota2', 0.) } }
def _validate(self, params): """Filters out images that are known to have problems using information in their metadata""" # Make sure the time can be understood if not is_time(params['date']): raise BadImage("DATE") # AIA if params['detector'] == "AIA": if params['header'].get("IMG_TYPE") == "DARK": raise BadImage("DARK") if float(params['header'].get('PERCENTD')) < 50: raise BadImage("PERCENTD") if str(params['header'].get('WAVE_STR')).endswith("_OPEN"): raise BadImage("WAVE_STR") # LASCO if params['instrument'] == "LASCO": hcomp_sf = params['header'].get('hcomp_sf') if ((params['detector'] == "C2" and hcomp_sf == 32) or (params['detector'] == "C3" and hcomp_sf == 64)): raise BadImage("WrongMask")
def _fix_date(self): # Check commonly used but non-standard FITS keyword for observation time # and correct the keyword if we can. Keep updating old one for # backwards compatibility. if is_time(self.meta.get('date_obs', None)): self.meta['date-obs'] = self.meta['date_obs']
def time_is_time(self): t = is_time('1995-12-31 23:59:60')
def mem_is_time(self): t = is_time('1995-11-31 23:59:60') return t
# Because this has *args, **kwargs as its signature we need to disable the # check of ConditionalDispatch that makes sure the function and the # conditional need to have the same signature - but they still do have to. LightCurve._cond_dispatch.add( run_cls("from_time"), lambda cls, time: sunpy.time.is_time(time), # type is here because the class parameter is a class, # i.e. an instance of type (which is the base meta-class). [type, (basestring, datetime, tuple)], False ) LightCurve._cond_dispatch.add( run_cls("from_range"), lambda cls, time, **kwargs: is_time(time), [type, (basestring, datetime, tuple)], False ) LightCurve._cond_dispatch.add( run_cls("from_timerange"), lambda cls, timerange, **kwargs: True, [type, TimeRange], False ) LightCurve._cond_dispatch.add( run_cls("from_file"), lambda cls, filename: os.path.exists(os.path.expanduser(filename)), [type, basestring],
# What's happening here is the following: The ConditionalDispatch is just an # unbound callable object, that is, it does not know which class it is attached # to. What we do against that is return a wrapper and make that a classmethod - # thus we get the class as whose member it is called as as the first argument, # this is why in the type signature we always have type as the first type. # We then use run_cls, which just returns a wrapper that interprets the first # argument as the class the function should be called of. So, # x = run_cls("foo") returns something that turns x(cls, 1) into cls.foo(1). # Because this has *args, **kwargs as its signature we need to disable the # check of ConditionalDispatch that makes sure the function and the # conditional need to have the same signature - but they still do have to. LightCurve._cond_dispatch.add( run_cls("from_time"), lambda cls, time: is_time(time), # type is here because the class parameter is a class, # i.e. an instance of type (which is the base meta-class). [type, (basestring, datetime, tuple)], False, ) LightCurve._cond_dispatch.add( run_cls("from_range"), lambda cls, time1, time2, **kwargs: is_time(time1) and is_time(time2), [type, (basestring, datetime, tuple), (basestring, datetime, tuple)], False, ) LightCurve._cond_dispatch.add( run_cls("from_timerange"), lambda cls, timerange, **kwargs: True, [type, TimeRange], False
# What's happening here is the following: The ConditionalDispatch is just an # unbound callable object, that is, it does not know which class it is attached # to. What we do against that is return a wrapper and make that a classmethod - # thus we get the class as whose member it is called as as the first argument, # this is why in the type signature we always have type as the first type. # We then use run_cls, which just returns a wrapper that interprets the first # argument as the class the function should be called of. So, # x = run_cls("foo") returns something that turns x(cls, 1) into cls.foo(1). # Because this has *args, **kwargs as its signature we need to disable the # check of ConditionalDispatch that makes sure the function and the # conditional need to have the same signature - but they still do have to. LightCurve._cond_dispatch.add( run_cls("from_time"), lambda cls, time, **kwargs: is_time(time), # type is here because the class parameter is a class, # i.e. an instance of type (which is the base meta-class). [type, (basestring, datetime, tuple)], False) LightCurve._cond_dispatch.add( run_cls("from_range"), lambda cls, time1, time2, **kwargs: is_time(time1) and is_time(time2), [type, (basestring, datetime, tuple), (basestring, datetime, tuple)], False) LightCurve._cond_dispatch.add(run_cls("from_timerange"), lambda cls, timerange, **kwargs: True, [type, TimeRange], False)
# What's happening here is the following: The ConditionalDispatch is just an # unbound callable object, that is, it does not know which class it is attached # to. What we do against that is return a wrapper and make that a classmethod - # thus we get the class as whose member it is called as as the first argument, # this is why in the type signature we always have type as the first type. # We then use run_cls, which just returns a wrapper that interprets the first # argument as the class the function should be called of. So, # x = run_cls("foo") returns something that turns x(cls, 1) into cls.foo(1). # Because this has *args, **kwargs as its signature we need to disable the # check of ConditionalDispatch that makes sure the function and the # conditional need to have the same signature - but they still do have to. LightCurve._cond_dispatch.add( run_cls("from_time"), lambda cls, time: is_time(time), # type is here because the class parameter is a class, # i.e. an instance of type (which is the base meta-class). [type, (basestring, datetime, tuple)], False) LightCurve._cond_dispatch.add(run_cls("from_range"), lambda cls, time, **kwargs: is_time(time), [type, (basestring, datetime, tuple)], False) LightCurve._cond_dispatch.add(run_cls("from_timerange"), lambda cls, timerange, **kwargs: True, [type, TimeRange], False) LightCurve._cond_dispatch.add( run_cls("from_file"),
def get_properties(cls, header): """Parses a map header and determines default properties.""" if is_time(header.get('date-obs', [])): # Hack! check FITS standard is a time date = header.get('date-obs') # Check commonly used but non-standard FITS keyword for observation time is a time elif is_time(header.get('date_obs', [])): # Horrible [] hack date = header.get('date_obs') else: date = None return { "cmap": cm.gray, # @UndefinedVariable "date": parse_time(date) if date is not None else 'N/A', "detector": header.get('detector', ''), "dsun": header.get('dsun_obs', constants.au), "exposure_time": header.get('exptime', 0.), "instrument": header.get('instrume', ''), "measurement": header.get('wavelnth', ''), "observatory": header.get('telescop', ''), "name": header.get('telescop', '') + " " + str(header.get('wavelnth', '')), "nickname": header.get('detector', ''), "rsun_meters": header.get('rsun_ref', constants.radius), "rsun_arcseconds": header.get( 'rsun_obs', header.get( 'solar_r', header.get('radius', constants.average_angular_size))), "coordinate_system": { 'x': header.get('ctype1', 'HPLN-TAN'), 'y': header.get('ctype2', 'HPLT-TAN') }, "carrington_longitude": header.get('crln_obs', 0.), "heliographic_latitude": header.get('hglt_obs', header.get('crlt_obs', header.get('solar_b0', 0.))), "heliographic_longitude": header.get('hgln_obs', 0.), "reference_coordinate": { 'x': header.get('crval1', 0.), 'y': header.get('crval2', 0.), }, "reference_pixel": { 'x': header.get('crpix1', (header.get('naxis1') + 1) / 2.), 'y': header.get('crpix2', (header.get('naxis2') + 1) / 2.) }, "scale": { 'x': header.get('cdelt1', 1.), 'y': header.get('cdelt2', 1.), }, "units": { 'x': header.get('cunit1', 'arcsec'), 'y': header.get('cunit2', 'arcsec') }, "rotation_angle": { 'x': header.get('crota1', 0.), 'y': header.get('crota2', 0.) } }