for item in test: if item.endswith(".1"): os.remove(os.path.join(fits_dir, item)) Xdata_dir = workdir + 'Xdata/' if not os.path.exists(Xdata_dir): os.mkdir(Xdata_dir) print("Directory " + Xdata_dir + "does not exist. Creating...") fits_filenames = os.listdir(fits_dir) resizing = [256] for resize in resizing: resize_dir = Xdata_dir + str(resize) if os.path.exists( resize_dir ): #delete any resizing directories matching the new resizes shutil.rmtree(resize_dir) os.makedirs(resize_dir) #creates new resize directories for filename in fits_filenames: #iterates over fits files and converts to a numpy array hmi_map = Map(fits_dir + filename) rotateddata90 = hmi_map.rotate(angle=90 * u.deg, order=0) rotateddata180 = rotateddata90.rotate(angle=90 * u.deg, order=0) data = rotateddata180.data data[np.where(np.isnan(data))] = 0.0 # replacing nans with 0s print('saving ' + filename + ' in sizes' + str(resizing)) for resize in resizing: #resizes and saves numpy array data into given resizes resized_image = np.array( Image.fromarray(data).resize((resize, resize), Image.LANCZOS)) np.save(Xdata_dir + str(resize) + '/' + filename[:26] + '_' + str(resize), resized_image) #saves series,time,and resize
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
# Read fits files and make maps file_list = [] file_list.append(glob.glob(path+'*.i*.fits')) file_list=sorted(file_list) Mfiles=Map(file_list) for mf in Mfiles: # changing 'CRDER1' and 'CRDER2' keywords to avoid annoying warnings mf.meta['CRDER1'] = 0.0 mf.meta['CRDER2'] = 0.0 # Máscara: imask = 10 diff = Mfiles[imask+1].data-Mfiles[imask].data Mdiff = Map(np.nan_to_num(np.abs(diff)),Mfiles[imask].meta) Mdiffrot = Mdiff.rotate(angle=Mdiff.meta['crota2'] * u.deg) mask = Mdiffrot.data > 10 Gdiff = ndimage.gaussian_filter(Mdiffrot.data,40) Gmask = Gdiff > Gdiff.max()*0.5 labels, n = ndimage.label(Gdiff*Gmask) flaremask = (labels==3) # Creating arrays containing time-serie data Int_Inc = [] tiempos = [] for i in range(20): diff = Mfiles[i+1].data-Mfiles[i].data Mdiff = Map(np.nan_to_num(np.abs(diff)),Mfiles[i].meta) Mdiffrot = Mdiff.rotate(angle=Mdiff.meta['crota2'] * u.deg) Int_Inc.append((Mdiffrot.data*flaremask).sum()) tiempos.append(datetime.datetime.strptime(Mdiffrot.meta["date-obs"],'%Y-%m-%dT%H:%M:%S.%f'))
def scale_rotate(amap, target_scale=0.504273, target_factor=0): """ Parameters ---------- amap Returns ------- """ scalex = amap.meta['cdelt1'] scaley = amap.meta['cdelt2'] # Set ratios to 1 if no scalin is done if target_factor == 0: ratio_plate = 1 ratio_dist = 1 new_shape = amap.data.shape[0] else: ratio_plate = target_factor * target_scale / scalex logger.info(np.round(scalex / target_scale) / scalex * target_scale) ratio_dist = amap.meta['dsun_obs'] / amap.meta['dsun_ref'] logger.info(ratio_dist) # Pad image, if necessary new_shape = int(4096 / target_factor) # Reform map to new size if original shape is too small if new_shape > amap.data.shape[0]: new_fov = np.zeros((new_shape, new_shape)) * np.nan new_meta = amap.meta new_meta['crpix1'] = new_meta[ 'crpix1'] - amap.data.shape[0] / 2 + new_fov.shape[0] / 2 new_meta['crpix2'] = new_meta[ 'crpix2'] - amap.data.shape[1] / 2 + new_fov.shape[1] / 2 # Identify the indices for appending the map original FoV i1 = int(new_fov.shape[0] / 2 - amap.data.shape[0] / 2) i2 = int(new_fov.shape[0] / 2 + amap.data.shape[0] / 2) # Insert original image in new field of view new_fov[i1:i2, i1:i2] = amap.data[:, :] # Assemble Sunpy map amap = Map(new_fov, new_meta) # Rotate solar north up rescale rot_map = amap.rotate(scale=ratio_dist / ratio_plate, recenter=True) if target_factor != 0: rot_map.meta['cdelt1'] = target_factor * target_scale rot_map.meta['cdelt2'] = target_factor * target_scale rot_map.meta['rsun_obs'] = rot_map.meta['rsun_obs'] * ratio_dist rot_map.meta['dsun_obs'] = rot_map.meta['dsun_ref'] # # Crop the image to desired shape sz_x_diff = (rot_map.data.shape[0] - new_shape) // 2 sz_y_diff = (rot_map.data.shape[0] - new_shape) // 2 rot_map.meta['crpix1'] = rot_map.meta['crpix1'] - sz_x_diff rot_map.meta['crpix2'] = rot_map.meta['crpix2'] - sz_y_diff rot_map = Map( rot_map.data[sz_x_diff:sz_x_diff + new_shape, sz_y_diff:sz_y_diff + new_shape].copy(), rot_map.meta) print(rot_map.data.shape) return rot_map