def save(self): date = sunpy.time.parse_time(self.date) if not path.exists(self.maps_dir): makedirs(self.maps_dir) fname = path.join(self.maps_dir, '{:%Y-%m-%dT%H_%M_%S}.fits'.format(date)) alldata = np.zeros((self.shape[0], self.shape[1], self.n_params+1)) alldata[..., 0] = self.data if self.n_params != 1: fname = fname.replace('.fits', '_full.fits') alldata[..., 1] = self.dem_width alldata[..., 2] = self.emission_measure alldata[..., -1] = self.goodness_of_fit outmap = Map(alldata, self.meta.copy()) outmap.save(fname, clobber=True)
def synop_reproject(dtime, shape_out): synop_map_path = synoptic_map_path(dtime) if not synop_map_path.exists(): m = load_start_of_day_map(dtime) # Reproject print(f'Reprojecting {synop_map_path}') header = synop_header(shape_out, m.date) with np.errstate(invalid='ignore'): array, footprint = reproject_interp(m, WCS(header), shape_out=shape_out) # Save some memory array = np.int16(array) for key in m.meta: if key not in header: header[key] = m.meta[key] new_map = Map((array, header)) new_map.save(str(synop_map_path)) print(f'Loading {synop_map_path}') new_map = Map(synop_map_path) return new_map
def synop_reproject(m, shape_out): """ Reproject a helioprojective map into a synoptic map. """ synop_map_path = synoptic_map_path(m.date.to_datetime()) if not synop_map_path.exists(): m.meta['rsun_ref'] = sunpy.sun.constants.radius.to_value(u.m) header = synop_header(shape_out, m.date) array, footprint = reproject_interp(m, WCS(header), shape_out=shape_out) new_map = Map((array, header)) new_map.save(str(synop_map_path)) print(f'Loading {synop_map_path}') new_map = Map(synop_map_path) new_map.plot_settings = m.plot_settings for key in m.meta: if key not in new_map.meta: new_map.meta[key] = m.meta[key] return new_map
meta['instrume'] = 'AIA' meta['detector'] = 'AIA' meta['waveunit'] = 'angstrom' meta['wavelnth'] = 193 synop_map = Map((data, meta)) synop_map.plot_settings = aia_synop_map.plot_settings synop_map.plot_settings['vmin'] = 0 synop_map.plot_settings['vmax'] = 2000 # synop_map.meta['crln_new'] = aia_map.meta['crln_obs'] return synop_map def aia_fov(dtime): l0 = sunpy.coordinates.sun.L0(dtime) bounds = Longitude([l0 - 90 * u.deg, l0 + 90 * u.deg]) return bounds if __name__ == '__main__': dtime = datetime.now() dtime = datetime(2020, 2, 1) map = create_synoptic_map(dtime) # Norm the data data = map.data data = map.plot_settings['norm'](data) map = Map((data, map.meta)) datestr = dtime.strftime('%Y%m%d') map.save(f'aia193_synoptic_{datestr}.fits')
def aia_prepping_script(image_files, save_files, verbose=False, as_npz=False): warnings.simplefilter('ignore', category=AstropyDeprecationWarning) fits_files = glob.glob(os.path.join(os.path.abspath(image_files), '**/*.fits'), recursive=True) eff_area = aia_area() pointing_table = calibrate.util.get_pointing_table( parse_time(fits.getval(fits_files[0], 'DATE-OBS', 1)), parse_time(fits.getval(fits_files[-1], 'DATE-OBS', 1))) bad_files = [] for image in fits_files: if verbose: print(image) try: savepath = os.path.join( os.path.abspath(save_files), os.path.dirname(image.split(image_files)[1])[1:]) os.makedirs(savepath, exist_ok=True) savename = os.path.join( savepath, os.path.basename(image).replace('lev1', 'lev15')) cal_map = Map(image) cal_map = calibrate.meta.fix_observer_location(cal_map) cal_map = calibrate.meta.update_pointing( cal_map, pointing_table=pointing_table) try: cal_map = calibrate.prep.normalize_exposure(cal_map) cal_map = aiaprep(cal_map) temp_data = cal_map.data / eff_area.effective_area_ratio( cal_map.fits_header['WAVELNTH'] * u.angstrom, parse_time(cal_map.fits_header['DATE-OBS']).to_datetime()) cal_map.data[:] = temp_data[:] if as_npz: np.savez_compressed(savename.replace('.fits', '.npz'), cal_map.data) else: try: cal_map.save(savename, overwrite=True, hdu_type=fits.CompImageHDU) except: bad_files += [image] print( f'FITS image write error... Skipping: {savename}') except ValueError: print(f'AIA Image has no integration time. Skipping: {image}') try: os.remove(os.path.abspath(savename)) print('Cleaning up bad files...') bad_files += [image] except OSError: pass except (ValueError, OSError): print(f'{image} is not valid. Flagging for follow up.') bad_files += [image] os.rename(image, image.replace('aia.', 'CHECK.aia.')) return bad_files
aia_synop_map = synop_reproject(euvi_map, shape) weights = synop_weights(aia_synop_map) aia_data = aia_synop_map.data aia_data[np.isnan(aia_data)] = 0 data += (aia_data * weights) weight_sum += weights weight_sum[weight_sum == 0] = np.nan data /= weight_sum meta = aia_synop_map.meta meta['date-obs'] = dtime.strftime('%Y-%m-%dT%H:%M:%S') synop_map = Map((data, meta)) synop_map.plot_settings = aia_synop_map.plot_settings synop_map.meta['crln_new'] = euvi_map.meta['crln_obs'] return synop_map if __name__ == '__main__': map = create_synoptic_map(datetime.now()) # Add a fudge factor so we get the same ballpark as AIA data data = map.data / 3 + (71 - 959 / 3) data[data <= 0] = 0 # Apply the same normalisation as an AIA map data = ImageNormalize(stretch=AsinhStretch(0.01), clip=False)(data) map = Map((data, map.meta)) datestr = datetime.now().strftime('%Y%m%d') map.save(f'euvi195_synoptic_latest_{datestr}.fits')
def prepData(files, base_dir, prefix, custom_keywords={}, plot=False): diffs = {'center_x': [], 'center_y': [], 'radius': [], 'scale': []} os.makedirs(os.path.join(base_dir, 'level1'), exist_ok=True) os.makedirs(os.path.join(base_dir, 'level1_5'), exist_ok=True) with warnings.catch_warnings(): warnings.simplefilter("ignore") for file in tqdm(files): try: # load existing file hdul = fits.open(file) hdu = hdul[0] hdu.verify('fix') d, h = hdu.data, hdu.header # set custom keywords h.update(custom_keywords) # evaluate center and radius imsave("demo.jpg", d) myCmd = os.popen( '/home/rja/PythonProjects/SpringProject/spring/limbcenter/sunlimb demo.jpg' ).read() center_x, center_y, radius, d_radius = map( float, myCmd.splitlines()) if "EXPTIME" in h: h['EXP_TIME'] = h['EXPTIME'] del h['EXPTIME'] if 'TIME-OBS' in h: obs_date = datetime.strptime( h['DATE-OBS'] + 'T' + h['TIME-OBS'], "%m/%d/1%yT%H:%M:%S") h['DATE-OBS'] = obs_date.isoformat() del h["TIME-OBS"] if 'TIME' in h: obs_date = datetime.strptime( h['DATE-OBS'] + 'T' + h['TIME'], "%d/%m/%YT%H:%M:%S") h['DATE-OBS'] = obs_date.isoformat() del h["TIME"] obs_time = parse(h["DATE-OBS"]) rsun = angular_radius(obs_time) b0_angle = sun.B0(obs_time) l0 = sun.L0(obs_time) p_angle = sun.P(obs_time) filename = "%s_%s_fi_%s.fits" % ( prefix, h["OBS_TYPE"].lower(), obs_time.strftime("%Y%m%d_%H%M%S")) # prepare existing header information if "ANGLE" not in h: h["ANGLE"] = p_angle.value scale = rsun / (radius * u.pix) coord = SkyCoord(0 * u.arcsec, 0 * u.arcsec, obstime=obs_time, observer='earth', frame=frames.Helioprojective) # create WCS header info header = header_helper.make_fitswcs_header( d, coord, rotation_angle=h["ANGLE"] * u.deg, reference_pixel=u.Quantity([center_x, center_y] * u.pixel), scale=u.Quantity([scale, scale]), instrument=h["INSTRUME"], telescope=h["TELESCOP"], observatory=h["OBSVTRY"], exposure=h["EXP_TIME"] * u.ms, wavelength=h["WAVELNTH"] * u.angstrom) header["KEYCOMMENTS"] = { "EXPTIME": "[s] exposure time in seconds", "DATE": "file creation date (YYYY-MM-DDThh:mm:ss UT)", "DATE-OBS": "date of observation", "WAVELNTH": "[Angstrom] wavelength", "BANDPASS": "******", "WAVEMIN": "[Angstrom] minimum wavelength", "WAVEMAX": "[Angstrom] maximum wavelength", "BZERO": "offset data range to that of unsigned short", "CDELT1": "[arcsec/pix]", "CDELT2": "[arcsec/pix]", "SOLAR_R": "[pix]", "DSUN_OBS": "[m]", "RSUN_REF": "[m]", "RSUN_ARC": "[%s]" % rsun.unit, "ANGLE": "[deg]", "SOLAR_P": "[%s]" % p_angle.unit, "SOLAR_L0": "[%s]" % l0.unit, "SOLAR_B0": "[%s]" % b0_angle.unit, 'SIMPLE': 'file does conform to FITS standard', 'BITPIX': 'number of bits per data pixel', 'CUNIT1': '[arcsec]', 'CUNIT2': '[arcsec]', 'CRVAL1': 'coordinate system value at reference pixel', 'CRVAL2': 'coordinate system value at reference pixel', 'CTYPE1': 'name of the coordinate axis', 'CTYPE2': 'name of the coordinate axis', 'INSTRUME': 'name of instrument', 'TELESCOP': 'name of telescope', 'OBSVTRY': 'name of observatory', } # set constants and default values header["FILENAME"] = filename header["DATE"] = datetime.now().strftime("%Y-%m-%dT%H:%M:%S") header["SOLAR_R"] = radius header["RSUN_ARC"] = rsun.value header["SOLAR_P"] = p_angle.value header["SOLAR_L0"] = l0.value header["SOLAR_B0"] = b0_angle.value header["DATAMIN"] = np.min(d) header["DATAMEAN"] = np.mean(d) header["DATAMAX"] = np.max(d) # copy existing keys for key, value in h.items(): if key not in header: header[key] = value # copy comments for key, value in zip(list(h.keys()), list(h.comments)): if key not in header["KEYCOMMENTS"]: header["KEYCOMMENTS"][key] = value # LEVEL 1 s_map = Map(d.astype(np.float32), header) level1_path = os.path.join(base_dir, 'level1', filename) h.add_history("unified FITS header") s_map.meta["HISTORY"] = h["HISTORY"] s_map.meta["LVL_NUM"] = "1.0" s_map = Map(s_map.data.astype(np.float32), s_map.meta) s_map.save(level1_path, overwrite=True) # LEVEL 1.5 scale = s_map.scale[0].value s_map = padScale(s_map) s_map = s_map.rotate( recenter=True, scale=scale, missing=s_map.min(), ) center = np.floor(s_map.meta['crpix1']) range_side = (center + np.array([-1, 1]) * 2048 / 2) * u.pix s_map = s_map.submap( u.Quantity([range_side[0], range_side[0]]), u.Quantity([range_side[1], range_side[1]])) level1_5_path = os.path.join(base_dir, 'level1_5', filename) h.add_history("recentered and derotated") s_map.meta["HISTORY"] = h["HISTORY"] s_map.meta["LVL_NUM"] = "1.5" s_map = Map(s_map.data.astype(np.float32), s_map.meta) s_map.save(level1_5_path, overwrite=True) if plot: s_map.plot() s_map.draw_grid() plt.savefig(level1_5_path.replace(".fits", ".jpg")) plt.close() # check header hdul = fits.open(level1_5_path) hdu = hdul[0] hdu.verify('exception') # evaluate difference if 'center_x' in h and not isinstance(h["center_x"], str): diffs['center_x'].append( np.abs(h['center_x'] - header['crpix1'])) if 'center_y' in h and not isinstance(h["center_y"], str): diffs['center_y'].append( np.abs(h['center_y'] - header['crpix2'])) if 'SOLAR_R' in h and not isinstance(h["SOLAR_R"], str): diffs['radius'].append( np.abs(h['SOLAR_R'] - header['SOLAR_R'])) if 'cdelt1' in h and not isinstance(h["cdelt1"], str): diffs['scale'].append( np.abs(h['cdelt1'] - header['cdelt1'])) except Exception as ex: print("INVALID FILE", file) print("ERROR:", ex) return diffs
emiss = Map(emission[wl, :, w, :].copy(), mapmeta) emiss.cmap = sunpy.cm.get_cmap('sdoaia{}'.format(wlength)) emiss.meta['naxis1'] = emiss.shape[1] emiss.meta['naxis2'] = emiss.shape[0] #emiss.meta['cdelt1'] = widths[1] - widths[0] emiss.meta['cdelt1'] = heights[1] - heights[0] #np.log10(heights[1]) - np.log10(heights[0]) emiss.meta['cdelt2'] = temps[1] - temps[0] #emiss.meta['crval1'] = widths[0] emiss.meta['crval1'] = heights[0] #np.log10(heights[0]) emiss.meta['crval2'] = temps[0] emiss.meta['crpix1'] = 0.5 emiss.meta['crpix2'] = 0.5 if wlength == '94': wlength = '094' fits_dir = path.join(CThome, 'data', 'synthetic', wlength) if not path.exists(fits_dir): makedirs(fits_dir) emiss.save(path.join(fits_dir, 'model.fits'), clobber=True) #print '----', emission[2, :, :, :].min(), emission[2, :, :, :].max() #print '------', emission[2, :, w, :].min(), emission[2, :, w, :].max() emiss.data /= emission[2, :, w, :] #print '--------', emiss.min(), emiss.max() ax = fig.add_subplot(1, 6, wl+1) emiss.plot(aspect='auto', vmin=emiss.min(), vmax=emiss.max()) plt.title('{}'.format(wlength)) plt.xlabel('Input EM') plt.ylabel('Input log(T)') plt.colorbar() #fig.gca().add_artist(rect) plt.axvline(20.0, color='white') plt.axvline(35.0, color='white') plt.axhline(5.6, color='white') plt.axhline(7.0, color='white')