Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 13, 2024
1 parent b83b7d0 commit ec3dc56
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 117 deletions.
3 changes: 2 additions & 1 deletion chimerapy/chimera.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Package for Coronal Hole Identification Algorithm"""

import glob
import sys

Expand Down Expand Up @@ -209,7 +210,7 @@ def to_helio(image: np.array):
hg = hpc.transform_to(sunpy.coordinates.frames.HeliographicStonyhurst)
global csys
csys = wcs.WCS(hedb)


to_helio(im171)

Expand Down
114 changes: 64 additions & 50 deletions chimerapy/chimera_copy.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
"""Package for Coronal Hole Identification Algorithm"""
import glob
import sys

import astropy.units as u
import cv2
import mahotas
import matplotlib.pyplot as plt
import numpy as np
import scipy
from sunpy.map import Map
from sunpy.coordinates import frames
from sunpy.coordinates import propagate_with_solar_surface
import scipy.interpolate
import sunpy
import sunpy.map
from astropy import wcs
from astropy.io import fits
from astropy.modeling.models import Gaussian2D
from astropy.visualization import astropy_mpl_style
from skimage.util import img_as_ubyte
from astropy.wcs import WCS
from sunpy.map import all_coordinates_from_map
from sunpy.coordinates import HeliographicStonyhurst
from sunpy.coordinates import (HeliographicStonyhurst,
propagate_with_solar_surface)
from sunpy.map import Map, all_coordinates_from_map


#--noverify
# --noverify

plt.style.use(astropy_mpl_style)

"""Defining the paths for example files used to run program locally"""

file_path = "./"

INPUT_FILES = {"aia171": 'http://jsoc.stanford.edu/data/aia/synoptic/2016/09/22/H1000/AIA20160922_1030_0171.fits',
"aia193": 'http://jsoc.stanford.edu/data/aia/synoptic/2016/09/22/H1000/AIA20160922_1030_0193.fits',
"aia211": 'http://jsoc.stanford.edu/data/aia/synoptic/2016/09/22/H1000/AIA20160922_1030_0211.fits',
"hmi_mag": 'http://jsoc.stanford.edu/data/hmi/fits/2016/09/22/hmi.M_720s.20160922_010000_TAI.fits',
INPUT_FILES = {
"aia171": "http://jsoc.stanford.edu/data/aia/synoptic/2016/09/22/H1000/AIA20160922_1030_0171.fits",
"aia193": "http://jsoc.stanford.edu/data/aia/synoptic/2016/09/22/H1000/AIA20160922_1030_0193.fits",
"aia211": "http://jsoc.stanford.edu/data/aia/synoptic/2016/09/22/H1000/AIA20160922_1030_0211.fits",
"hmi_mag": "http://jsoc.stanford.edu/data/hmi/fits/2016/09/22/hmi.M_720s.20160922_010000_TAI.fits",
}

im171 = Map(INPUT_FILES['aia171'])
im193 = Map(INPUT_FILES['aia193'])
im211 = Map(INPUT_FILES['aia211'])
imhmi = Map(INPUT_FILES['hmi_mag'])
im171 = Map(INPUT_FILES["aia171"])
im193 = Map(INPUT_FILES["aia193"])
im211 = Map(INPUT_FILES["aia211"])
imhmi = Map(INPUT_FILES["hmi_mag"])


def rescale(proj_to: sunpy.map.Map, input_map: sunpy.map.Map):
Expand All @@ -56,23 +48,25 @@ def rescale(proj_to: sunpy.map.Map, input_map: sunpy.map.Map):
Returns
-------
array: 'np.array'
"""
"""
with propagate_with_solar_surface():
map1 = proj_to.reproject_to(input_map.wcs)
new_x_scale = map1.scale[0].to(u.arcsec / u.pixel).value
new_y_scale = map1.scale[1].to(u.arcsec / u.pixel).value
map1.meta['cdelt1'] = new_x_scale
map1.meta['cdelt2'] = new_y_scale
map1.meta['cunit1'] = 'arcsec'
map1.meta['cunit2'] = 'arcsec'
map1.meta["cdelt1"] = new_x_scale
map1.meta["cdelt2"] = new_y_scale
map1.meta["cunit1"] = "arcsec"
map1.meta["cunit2"] = "arcsec"
return map1


im171 = rescale(im171, im171)
im193 = rescale(im171, im193)
im211 = rescale(im171, im211)
imhmi = rescale(im171, imhmi)


def filter(map1: np.array, map2: np.array, map3: np.array):
"""
Defines headers and filters aia arrays to meet header requirements. Removes 0 values from each array.
Expand All @@ -96,18 +90,18 @@ def filter(map1: np.array, map2: np.array, map3: np.array):

return map1, map2, map3


im171, im193, im211 = filter(im171, im193, im211)

"""defines the shape of the arrays as "s" and "rs" as the solar radius"""
s = np.shape(im171.data)
#do we want the solar radius in arcsec or pixels?
# do we want the solar radius in arcsec or pixels?
rs = im171.rsun_obs
rs_pixels = im171.rsun_obs/im171.scale[0]

rs_pixels = im171.rsun_obs / im171.scale[0]


def pix_arc(map: sunpy.map.Map):
'''
"""
Calculates the conversion value of pixel to arcsec
Parameters
Expand All @@ -117,7 +111,7 @@ def pix_arc(map: sunpy.map.Map):
Returns
'''
"""
dattoarc = map.scale[0].value
conver = ((s[0]) / 2) * dattoarc / map.meta["cdelt1"] - (s[1] / 2)
convermul = dattoarc / map.meta["cdelt1"]
Expand Down Expand Up @@ -149,6 +143,7 @@ def to_helio(map: sunpy.map.Map):
csys = wcs.WCS(dict(map.meta))
return hpc, hg, csys


hpc, hg, csys = to_helio(im171)

"""Setting up arrays to be used in later processing"""
Expand All @@ -166,7 +161,13 @@ def to_helio(map: sunpy.map.Map):
w = np.where((xgrid - center[0]) ** 2 + (ygrid - center[1]) ** 2 > r**2)
y, x = np.mgrid[0:1024, 0:1024]
pix_size = (2000 * u.arcsec).value
garr = Gaussian2D(1, im171.reference_pixel.x.value, im171.reference_pixel.y.value, pix_size / im171.scale[0].value, pix_size / im171.scale[0].value)(x, y)
garr = Gaussian2D(
1,
im171.reference_pixel.x.value,
im171.reference_pixel.y.value,
pix_size / im171.scale[0].value,
pix_size / im171.scale[0].value,
)(x, y)
garr[w] = 1.0

"""creates sub-arrays of props to isolate column of index 0 and column of index 1"""
Expand Down Expand Up @@ -229,8 +230,10 @@ def to_helio(map: sunpy.map.Map):
)

"""define threshold values in log space"""


def log_dat(map1: sunpy.map.Map, map2: sunpy.map.Map, map3: sunpy.map.Map):
'''
"""
Takes the log base-10 of all sunpy map data
Parameters
Expand All @@ -244,13 +247,14 @@ def log_dat(map1: sunpy.map.Map, map2: sunpy.map.Map, map3: sunpy.map.Map):
t0: 'np.array'
t1: 'np.array'
t2: 'np.array'
'''
"""
with np.errstate(divide="ignore"):
t0 = np.log10(map1.data)
t1 = np.log10(map2.data)
t2 = np.log10(map3.data)
return t0, t1, t2


t0, t1, t2 = log_dat(im171, im193, im211)


Expand All @@ -276,7 +280,8 @@ def new_s(self, new_slope):
t1b = Bounds(1.4, 3.0, 255)
t2b = Bounds(1.2, 3.9, 255)

#set to also take in boundaries

# set to also take in boundaries
def set_contour(t0: np.array, t1: np.array, t2: np.array):
"""
Threshold arrays based on desired boundaries and sets contours.
Expand All @@ -295,15 +300,15 @@ def set_contour(t0: np.array, t1: np.array, t2: np.array):
"""
if t0 is not None and t1 is not None and t2 is not None:
#set the threshold and contours for t0
# set the threshold and contours for t0
t0[np.where(t0 < t0b.upper)] = t0b.upper
t0[np.where(t0 > t0b.lower)] = t0b.lower
t0 = np.array(((t0 - t0b.upper) / (t0b.lower - t0b.upper)) * t0b.slope, dtype=np.float32)
#set the threshold and contours for t1
# set the threshold and contours for t1
t1[np.where(t1 < t1b.upper)] = t1b.upper
t1[np.where(t1 > t1b.lower)] = t2b.lower
t1 = np.array(((t1 - t1b.upper) / (t1b.lower - t1b.upper)) * t1b.slope, dtype=np.float32)
#set the threshold and contours for t2
# set the threshold and contours for t2
t2[np.where(t2 < t2b.upper)] = t2b.upper
t2[np.where(t2 > t2b.lower)] = t2b.lower
t2 = np.array(((t2 - t2b.upper) / (t2b.lower - t2b.upper)) * t2b.slope, dtype=np.float32)
Expand All @@ -314,7 +319,10 @@ def set_contour(t0: np.array, t1: np.array, t2: np.array):

t0, t1, t2 = set_contour(t0, t1, t2)

def create_mask(tm1: np.array, tm2: np.array, tm3: np.array, map1: sunpy.map.Map, map2: sunpy.map.Map, map3: sunpy.map.Map):

def create_mask(
tm1: np.array, tm2: np.array, tm3: np.array, map1: sunpy.map.Map, map2: sunpy.map.Map, map3: sunpy.map.Map
):
"""
Creates 3 segmented bitmasks
Expand All @@ -326,7 +334,7 @@ def create_mask(tm1: np.array, tm2: np.array, tm3: np.array, map1: sunpy.map.Map
map1: 'sunpy.map.Map'
map2: 'sunpy.map.Map'
map3: 'sunpy.map.Map'
Returns
-------
bmmix: 'np.array'
Expand All @@ -343,9 +351,10 @@ def create_mask(tm1: np.array, tm2: np.array, tm3: np.array, map1: sunpy.map.Map

bmmix, bmhot, bmcool = create_mask(t0, t1, t2, im171, im193, im211)

#conjunction of 3 bitmasks
# conjunction of 3 bitmasks
cand = bmcool * bmmix * bmhot


def misid(can: np.array, cir: np.array, xgir: np.array, ygir: np.array, thresh_rad: int):
"""
Removes off-detector mis-identification
Expand All @@ -362,7 +371,7 @@ def misid(can: np.array, cir: np.array, xgir: np.array, ygir: np.array, thresh_r
'np.array'
"""
#make r a function argument, give name and unit
# make r a function argument, give name and unit
r = thresh_rad
w = np.where((xgir - center[0]) ** 2 + (ygir - center[1]) ** 2 <= thresh_rad**2)
cir[w] = 1.0
Expand Down Expand Up @@ -396,16 +405,18 @@ def on_off(cir: np.array, can: np.array):
cir[outside] = 1.0
can = can * cir
plt.figure()
plt.imshow(cand, cmap='viridis')
plt.imshow(cand, cmap="viridis")
plt.show
return can


cand = on_off(circ, cand)


def contour_data(cand: np.array):
"""
Contours the identified datapoints
Parameters
----------
cand: 'np.array'
Expand All @@ -419,15 +430,17 @@ def contour_data(cand: np.array):
"""
cand = np.array(cand, dtype=np.uint8)
cont, heir = cv2.findContours(cand, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#I think cont might be the x-y coordinates in pixels?
# I think cont might be the x-y coordinates in pixels?
return cand, cont, heir


cand, cont, heir = contour_data(cand)


def sort(cont: tuple):
"""
Sorts the contours by size
Parameters
----------
cont: 'tuple'
Expand Down Expand Up @@ -476,7 +489,7 @@ def extent(map: sunpy.map.Map, cont: tuple):
"""

coord_hpc = map.world2pix(cont)
maxlat = coord_hpc.transform_to(HeliographicStonyhurst).lat.max()
maxlat = coord_hpc.transform_to(HeliographicStonyhurst).lat.max()
maxlon = coord_hpc.transform_to(HeliographicStonyhurst).lon.max()
minlat = coord_hpc.transform_to(HeliographicStonyhurst).lat.min()
minlon = coord_hpc.transform_to(HeliographicStonyhurst).lat.min()
Expand All @@ -488,7 +501,6 @@ def extent(map: sunpy.map.Map, cont: tuple):
return maxlat, maxlon, minlat, minlon, centlat, centlon



def coords(i, csys, cont):
"""
Finds coordinates of CH boundaries
Expand Down Expand Up @@ -921,4 +933,6 @@ def plot_mask(slate=slate):
plot_mask()

if __name__ == "__main__":
import_functions(INPUT_FILES['aia171'], INPUT_FILES['aia193'], INPUT_FILES['aia211'], INPUT_FILES['hmi_mag'])
import_functions(
INPUT_FILES["aia171"], INPUT_FILES["aia193"], INPUT_FILES["aia211"], INPUT_FILES["hmi_mag"]
)
5 changes: 1 addition & 4 deletions chimerapy/chimera_legacy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"""
"""

""" """

import glob
import sys
Expand Down
Loading

0 comments on commit ec3dc56

Please sign in to comment.