def test_solar_semidiameter_angular_size(): assert_quantity_allclose(sun.solar_semidiameter_angular_size("2012/11/11"), 968.604 * u.arcsec, atol=1e-3 * u.arcsec) assert_quantity_allclose(sun.solar_semidiameter_angular_size("2043/03/01"), 968.059 * u.arcsec, atol=1e-3 * u.arcsec) assert_quantity_allclose(sun.solar_semidiameter_angular_size("2001/07/21"), 943.778 * u.arcsec, atol=1e-3 * u.arcsec)
def test_solar_semidiameter_angular_size(): assert_quantity_allclose(sun.solar_semidiameter_angular_size("2012/11/11"), 968.871294 * u.arcsec, atol=1e-3 * u.arcsec) assert_quantity_allclose(sun.solar_semidiameter_angular_size("2043/03/01"), 968.326347 * u.arcsec, atol=1e-3 * u.arcsec) assert_quantity_allclose(sun.solar_semidiameter_angular_size("2001/07/21"), 944.039007 * u.arcsec, atol=1e-3 * u.arcsec)
def rsun_arcseconds(self): """Radius of the sun in arcseconds""" rsun_arcseconds = self.meta.get('rsun_obs', self.meta.get('solar_r', self.meta.get('radius', None))) if rsun_arcseconds is None: warnings.warn_explicit("Missing metadata for solar radius: assuming photospheric limb as seen from Earth", Warning, __file__, inspect.currentframe().f_back.f_lineno) rsun_arcseconds = sun.solar_semidiameter_angular_size(self.date).value return rsun_arcseconds
def rsun_obs(self): """Radius of the Sun.""" rsun_arcseconds = self.meta.get("rsun_obs", self.meta.get("solar_r", self.meta.get("radius", None))) if rsun_arcseconds is None: warnings.warn_explicit( "Missing metadata for solar radius: assuming photospheric limb as seen from Earth", Warning, __file__, inspect.currentframe().f_back.f_lineno, ) rsun_arcseconds = sun.solar_semidiameter_angular_size(self.date).to("arcsec").value return u.Quantity(rsun_arcseconds, "arcsec")
def _dsunAtSoho(date, rad_d, rad_1au = None): """Determines the distance to the Sun from SOhO following d_{\sun,Object} = D_{\sun\earth} \frac{\tan(radius_{1au}[rad])}{\tan(radius_{d}[rad])} though tan x ~ x for x << 1 d_{\sun,Object} = D_{\sun\eart} \frac{radius_{1au}[rad]}{radius_{d}[rad]} since radius_{1au} and radius_{d} are dividing each other we can use [arcsec] instead. --- TODO: Does this apply just to observations on the same Earth-Sun line? If not it can be moved outside here. """ if not rad_1au: rad_1au = sun.solar_semidiameter_angular_size(date) return sun.sunearth_distance(date) * constants.au * (rad_1au / rad_d)
def _dsunAtSoho(date, rad_d, rad_1au=None): """Determines the distance to the Sun from SOhO following d_{\sun,Object} = D_{\sun\earth} \frac{\tan(radius_{1au}[rad])}{\tan(radius_{d}[rad])} though tan x ~ x for x << 1 d_{\sun,Object} = D_{\sun\eart} \frac{radius_{1au}[rad]}{radius_{d}[rad]} since radius_{1au} and radius_{d} are dividing each other we can use [arcsec] instead. --- TODO: Does this apply just to observations on the same Earth-Sun line? If not it can be moved outside here. """ if not rad_1au: rad_1au = sun.solar_semidiameter_angular_size(date) dsun = sun.sunearth_distance(date) * constants.au * (rad_1au / rad_d) # return scalar value not astropy.quantity return dsun.value
def test_rsun_missing(): """Tests output if 'rsun' is missing""" euvi_no_rsun = Map(fitspath) euvi_no_rsun.meta['rsun'] = None assert euvi_no_rsun.rsun_obs.value == sun.solar_semidiameter_angular_size(euvi.date).to('arcsec').value
def backprojection(calibrated_event_list, pixel_size=(1., 1.) * u.arcsec, image_dim=(64, 64) * u.pix): """ Given a stacked calibrated event list fits file create a back projection image. .. warning:: The image is not in the right orientation! Parameters ---------- calibrated_event_list : string filename of a RHESSI calibrated event list pixel_size : `~astropy.units.Quantity` instance the size of the pixels in arcseconds. Default is (1,1). image_dim : `~astropy.units.Quantity` instance the size of the output image in number of pixels Returns ------- out : RHESSImap Return a backprojection map. Examples -------- This example is broken. >>> import sunpy.data >>> import sunpy.data.sample # doctest: +SKIP >>> import sunpy.instr.rhessi as rhessi >>> map = rhessi.backprojection(sunpy.data.sample.RHESSI_IMAGE) # doctest: +SKIP >>> map.peek() # doctest: +SKIP """ pixel_size = pixel_size.to(u.arcsec) image_dim = np.array(image_dim.to(u.pix).value, dtype=int) afits = sunpy.io.read_file(calibrated_event_list) info_parameters = afits[2] xyoffset = info_parameters.data.field('USED_XYOFFSET')[0] time_range = TimeRange(info_parameters.data.field('ABSOLUTE_TIME_RANGE')[0]) image = np.zeros(image_dim) # find out what detectors were used det_index_mask = afits[1].data.field('det_index_mask')[0] detector_list = (np.arange(9)+1) * np.array(det_index_mask) for detector in detector_list: if detector > 0: image = image + _backproject(calibrated_event_list, detector=detector, pixel_size=pixel_size.value, image_dim=image_dim) dict_header = { "DATE-OBS": time_range.center.strftime("%Y-%m-%d %H:%M:%S"), "CDELT1": pixel_size[0], "NAXIS1": image_dim[0], "CRVAL1": xyoffset[0], "CRPIX1": image_dim[0]/2 + 0.5, "CUNIT1": "arcsec", "CTYPE1": "HPLN-TAN", "CDELT2": pixel_size[1], "NAXIS2": image_dim[1], "CRVAL2": xyoffset[1], "CRPIX2": image_dim[0]/2 + 0.5, "CUNIT2": "arcsec", "CTYPE2": "HPLT-TAN", "HGLT_OBS": 0, "HGLN_OBS": 0, "RSUN_OBS": solar_semidiameter_angular_size(time_range.center).value, "RSUN_REF": sunpy.sun.constants.radius.value, "DSUN_OBS": get_sunearth_distance(time_range.center).value * sunpy.sun.constants.au.value } result_map = sunpy.map.Map(image, dict_header) return result_map
def backprojection(calibrated_event_list, pixel_size: u.arcsec=(1., 1.) * u.arcsec, image_dim: u.pix=(64, 64) * u.pix): """ Given a stacked calibrated event list fits file create a back projection image. .. warning:: The image is not in the right orientation! Parameters ---------- calibrated_event_list : str filename of a RHESSI calibrated event list pixel_size : `~astropy.units.Quantity` instance the size of the pixels in arcseconds. Default is (1,1). image_dim : `~astropy.units.Quantity` instance the size of the output image in number of pixels Returns ------- out : RHESSImap Return a backprojection map. Examples -------- This example is broken. >>> import sunpy.data >>> import sunpy.data.sample # doctest: +REMOTE_DATA >>> import sunpy.instr.rhessi as rhessi >>> map = rhessi.backprojection(sunpy.data.sample.RHESSI_EVENT_LIST) # doctest: +SKIP >>> map.peek() # doctest: +SKIP """ # import sunpy.map in here so that net and timeseries don't end up importing map import sunpy.map pixel_size = pixel_size.to(u.arcsec) image_dim = np.array(image_dim.to(u.pix).value, dtype=int) afits = sunpy.io.read_file(calibrated_event_list) info_parameters = afits[2] xyoffset = info_parameters.data.field('USED_XYOFFSET')[0] time_range = TimeRange(info_parameters.data.field('ABSOLUTE_TIME_RANGE')[0], format='utime') image = np.zeros(image_dim) # find out what detectors were used det_index_mask = afits[1].data.field('det_index_mask')[0] detector_list = (np.arange(9)+1) * np.array(det_index_mask) for detector in detector_list: if detector > 0: image = image + _backproject(calibrated_event_list, detector=detector, pixel_size=pixel_size.value, image_dim=image_dim) dict_header = { "DATE-OBS": time_range.center.strftime("%Y-%m-%d %H:%M:%S"), "CDELT1": pixel_size[0], "NAXIS1": image_dim[0], "CRVAL1": xyoffset[0], "CRPIX1": image_dim[0]/2 + 0.5, "CUNIT1": "arcsec", "CTYPE1": "HPLN-TAN", "CDELT2": pixel_size[1], "NAXIS2": image_dim[1], "CRVAL2": xyoffset[1], "CRPIX2": image_dim[0]/2 + 0.5, "CUNIT2": "arcsec", "CTYPE2": "HPLT-TAN", "HGLT_OBS": 0, "HGLN_OBS": 0, "RSUN_OBS": solar_semidiameter_angular_size(time_range.center).value, "RSUN_REF": sunpy.sun.constants.radius.value, "DSUN_OBS": get_sunearth_distance(time_range.center).value * sunpy.sun.constants.au.value } result_map = sunpy.map.Map(image, dict_header) return result_map
def backprojection(calibrated_event_list, pixel_size=(1.,1.), image_dim=(64,64)): """Given a stacked calibrated event list fits file create a back projection image. .. warning:: The image is not in the right orientation! Parameters ---------- calibrated_event_list : string filename of a RHESSI calibrated event list detector : int the detector number pixel_size : 2-tuple the size of the pixels in arcseconds. Default is (1,1). image_dim : 2-tuple the size of the output image in number of pixels Returns ------- out : RHESSImap Return a backprojection map. Examples -------- >>> import sunpy.instr.rhessi as rhessi >>> map = rhessi.backprojection(sunpy.RHESSI_EVENT_LIST) >>> map.show() """ calibrated_event_list = sunpy.RHESSI_EVENT_LIST fits = pyfits.open(calibrated_event_list) info_parameters = fits[2] xyoffset = info_parameters.data.field('USED_XYOFFSET')[0] time_range = TimeRange(info_parameters.data.field('ABSOLUTE_TIME_RANGE')[0]) image = np.zeros(image_dim) #find out what detectors were used det_index_mask = fits[1].data.field('det_index_mask')[0] detector_list = (np.arange(9)+1) * np.array(det_index_mask) for detector in detector_list: if detector > 0: image = image + _backproject(calibrated_event_list, detector=detector, pixel_size=pixel_size, image_dim=image_dim) dict_header = { "DATE-OBS": time_range.center().strftime("%Y-%m-%d %H:%M:%S"), "CDELT1": pixel_size[0], "NAXIS1": image_dim[0], "CRVAL1": xyoffset[0], "CRPIX1": image_dim[0]/2 + 0.5, "CUNIT1": "arcsec", "CTYPE1": "HPLN-TAN", "CDELT2": pixel_size[1], "NAXIS2": image_dim[1], "CRVAL2": xyoffset[1], "CRPIX2": image_dim[0]/2 + 0.5, "CUNIT2": "arcsec", "CTYPE2": "HPLT-TAN", "HGLT_OBS": 0, "HGLN_OBS": 0, "RSUN_OBS": solar_semidiameter_angular_size(time_range.center()), "RSUN_REF": sun.radius, "DSUN_OBS": sunearth_distance(time_range.center()) * sunpy.sun.constants.au } header = sunpy.map.MapHeader(dict_header) result_map = sunpy.map.Map(image, header) return result_map
def backprojection(calibrated_event_list, pixel_size=(1., 1.) * u.arcsec, image_dim=(64, 64) * u.pix): """ Given a stacked calibrated event list fits file create a back projection image. .. warning:: The image is not in the right orientation! Parameters ---------- calibrated_event_list : string filename of a RHESSI calibrated event list pixel_size : `~astropy.units.Quantity` instance the size of the pixels in arcseconds. Default is (1,1). image_dim : `~astropy.units.Quantity` instance the size of the output image in number of pixels Returns ------- out : RHESSImap Return a backprojection map. Examples -------- >>> import sunpy.data >>> import sunpy.data.sample >>> import sunpy.instr.rhessi as rhessi >>> sunpy.data.download_sample_data(overwrite=False) # doctest: +SKIP >>> map = rhessi.backprojection(sunpy.data.sample.RHESSI_EVENT_LIST) # doctest: +SKIP >>> map.peek() # doctest: +SKIP """ if not isinstance(pixel_size, u.Quantity): raise ValueError("Must be astropy Quantity in arcseconds") try: pixel_size = pixel_size.to(u.arcsec) except: raise ValueError("'{0}' is not a valid pixel_size unit".format( pixel_size.unit)) if not (isinstance(image_dim, u.Quantity) and image_dim.unit == 'pix'): raise ValueError("Must be astropy Quantity in pixels") try: import sunpy.data.sample except ImportError: import sunpy.data sunpy.data.download_sample() # This may need to be moved up to data from sample calibrated_event_list = sunpy.data.sample.RHESSI_EVENT_LIST afits = fits.open(calibrated_event_list) info_parameters = afits[2] xyoffset = info_parameters.data.field('USED_XYOFFSET')[0] time_range = TimeRange( info_parameters.data.field('ABSOLUTE_TIME_RANGE')[0]) image = np.zeros(image_dim.value) # find out what detectors were used det_index_mask = afits[1].data.field('det_index_mask')[0] detector_list = (np.arange(9) + 1) * np.array(det_index_mask) for detector in detector_list: if detector > 0: image = image + _backproject(calibrated_event_list, detector=detector, pixel_size=pixel_size.value, image_dim=image_dim.value) dict_header = { "DATE-OBS": time_range.center().strftime("%Y-%m-%d %H:%M:%S"), "CDELT1": pixel_size[0], "NAXIS1": image_dim[0], "CRVAL1": xyoffset[0], "CRPIX1": image_dim[0].value / 2 + 0.5, "CUNIT1": "arcsec", "CTYPE1": "HPLN-TAN", "CDELT2": pixel_size[1], "NAXIS2": image_dim[1], "CRVAL2": xyoffset[1], "CRPIX2": image_dim[0].value / 2 + 0.5, "CUNIT2": "arcsec", "CTYPE2": "HPLT-TAN", "HGLT_OBS": 0, "HGLN_OBS": 0, "RSUN_OBS": solar_semidiameter_angular_size(time_range.center()).value, "RSUN_REF": sunpy.sun.constants.radius.value, "DSUN_OBS": sunearth_distance(time_range.center()) * sunpy.sun.constants.au.value } header = sunpy.map.MapMeta(dict_header) result_map = sunpy.map.Map(image, header) return result_map
def backprojection(calibrated_event_list, pixel_size=(1., 1.), image_dim=(64, 64)): """ Given a stacked calibrated event list fits file create a back projection image. .. warning:: The image is not in the right orientation! Parameters ---------- calibrated_event_list : string filename of a RHESSI calibrated event list detector : int the detector number pixel_size : 2-tuple the size of the pixels in arcseconds. Default is (1,1). image_dim : 2-tuple the size of the output image in number of pixels Returns ------- out : RHESSImap Return a backprojection map. Examples -------- >>> import sunpy.instr.rhessi as rhessi >>> map = rhessi.backprojection(sunpy.RHESSI_EVENT_LIST) >>> map.show() """ calibrated_event_list = sunpy.RHESSI_EVENT_LIST afits = fits.open(calibrated_event_list) info_parameters = afits[2] xyoffset = info_parameters.data.field('USED_XYOFFSET')[0] time_range = TimeRange( info_parameters.data.field('ABSOLUTE_TIME_RANGE')[0]) image = np.zeros(image_dim) #find out what detectors were used det_index_mask = afits[1].data.field('det_index_mask')[0] detector_list = (np.arange(9) + 1) * np.array(det_index_mask) for detector in detector_list: if detector > 0: image = image + _backproject(calibrated_event_list, detector=detector, pixel_size=pixel_size, image_dim=image_dim) dict_header = { "DATE-OBS": time_range.center().strftime("%Y-%m-%d %H:%M:%S"), "CDELT1": pixel_size[0], "NAXIS1": image_dim[0], "CRVAL1": xyoffset[0], "CRPIX1": image_dim[0] / 2 + 0.5, "CUNIT1": "arcsec", "CTYPE1": "HPLN-TAN", "CDELT2": pixel_size[1], "NAXIS2": image_dim[1], "CRVAL2": xyoffset[1], "CRPIX2": image_dim[0] / 2 + 0.5, "CUNIT2": "arcsec", "CTYPE2": "HPLT-TAN", "HGLT_OBS": 0, "HGLN_OBS": 0, "RSUN_OBS": solar_semidiameter_angular_size(time_range.center()), "RSUN_REF": sun.radius, "DSUN_OBS": sunearth_distance(time_range.center()) * sunpy.sun.constants.au } header = sunpy.map.MapHeader(dict_header) result_map = sunpy.map.Map(image, header) return result_map
def backprojection(calibrated_event_list, pixel_size=(1.,1.) * u.arcsec, image_dim=(64,64) * u.pix): """ Given a stacked calibrated event list fits file create a back projection image. .. warning:: The image is not in the right orientation! Parameters ---------- calibrated_event_list : string filename of a RHESSI calibrated event list detector : int the detector number pixel_size : `~astropy.units.Quantity` instance the size of the pixels in arcseconds. Default is (1,1). image_dim : `~astropy.units.Quantity` instance the size of the output image in number of pixels Returns ------- out : RHESSImap Return a backprojection map. Examples -------- >>> import sunpy.instr.rhessi as rhessi >>> map = rhessi.backprojection(sunpy.RHESSI_EVENT_LIST) >>> map.peek() """ if not isinstance(pixel_size, u.Quantity): raise ValueError("Must be astropy Quantity in arcseconds") try: pixel_size = pixel_size.to(u.arcsec) except: raise ValueError("'{0}' is not a valid pixel_size unit".format(pixel_size.unit)) if not (isinstance(image_dim, u.Quantity) and image_dim.unit == 'pix'): raise ValueError("Must be astropy Quantity in pixels") calibrated_event_list = sunpy.RHESSI_EVENT_LIST afits = fits.open(calibrated_event_list) info_parameters = afits[2] xyoffset = info_parameters.data.field('USED_XYOFFSET')[0] time_range = TimeRange(info_parameters.data.field('ABSOLUTE_TIME_RANGE')[0]) image = np.zeros(image_dim.value) #find out what detectors were used det_index_mask = afits[1].data.field('det_index_mask')[0] detector_list = (np.arange(9)+1) * np.array(det_index_mask) for detector in detector_list: if detector > 0: image = image + _backproject(calibrated_event_list, detector=detector, pixel_size=pixel_size.value , image_dim=image_dim.value) dict_header = { "DATE-OBS": time_range.center().strftime("%Y-%m-%d %H:%M:%S"), "CDELT1": pixel_size[0], "NAXIS1": image_dim[0], "CRVAL1": xyoffset[0], "CRPIX1": image_dim[0].value/2 + 0.5, "CUNIT1": "arcsec", "CTYPE1": "HPLN-TAN", "CDELT2": pixel_size[1], "NAXIS2": image_dim[1], "CRVAL2": xyoffset[1], "CRPIX2": image_dim[0].value/2 + 0.5, "CUNIT2": "arcsec", "CTYPE2": "HPLT-TAN", "HGLT_OBS": 0, "HGLN_OBS": 0, "RSUN_OBS": solar_semidiameter_angular_size(time_range.center()).value, "RSUN_REF": sunpy.sun.constants.radius.value, "DSUN_OBS": sunearth_distance(time_range.center()) * sunpy.sun.constants.au.value } header = sunpy.map.MapMeta(dict_header) result_map = sunpy.map.Map(image, header) return result_map
def test_rsun_missing(): """Tests output if 'rsun' is missing""" euvi_no_rsun = Map(fitspath) euvi_no_rsun.meta['rsun'] = None assert euvi_no_rsun.rsun_obs.value == sun.solar_semidiameter_angular_size( euvi.date).to('arcsec').value
def test_solar_semidiameter_angular_size(): assert_array_almost_equal(sun.solar_semidiameter_angular_size("2012/11/11"), 968.383 * u.arcsec, decimal=3) assert_array_almost_equal(sun.solar_semidiameter_angular_size("2043/03/01"), 968.274 * u.arcsec, decimal=3) assert_array_almost_equal(sun.solar_semidiameter_angular_size("2001/07/21"), 943.706 * u.arcsec, decimal=3)