-
Notifications
You must be signed in to change notification settings - Fork 8
Add Experimental GUI Tool for Quick XRT Data Preview #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ntrueba
wants to merge
16
commits into
main
Choose a base branch
from
metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
98db678
this is my first attempt at using git
681de8b
testing line of code
2a57a01
Example notebook for FOV preview tool
844c82b
FOV tool code
1e44740
Merge branch 'main' into metadata
joyvelasquez b20725b
FOV tool code
56e45be
Merge branch 'main' of https://github.com/HinodeXRT/xrtpy into metadata
e19efec
Merge branch 'metadata' of https://github.com/HinodeXRT/xrtpy into me…
2d558db
Update to code, added animations and other options
c6b7d20
cleaned up things like removed white borders from figures
043ac9e
added ipywidgets dependancy
42ddeee
Merge branch 'main' into metadata
joyvelasquez adda0fa
Merge branch 'main' into metadata
joyvelasquez 93123a2
Merge branch 'main' into metadata
joyvelasquez 5f6a833
Merge branch 'main' into metadata
joyvelasquez 8681495
Merge branch 'main' into metadata
joyvelasquez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| """ | ||
| ============================= | ||
| FOV tool test | ||
| ============================= | ||
|
|
||
| We are just testing stuff here | ||
| """ | ||
|
|
||
| import time as timer | ||
| import matplotlib.pyplot as plt | ||
| from astropy.io import fits | ||
| import astropy.time | ||
| import astropy.units as u | ||
|
|
||
| import sunpy | ||
| import ipywidgets as widgets | ||
| from ipywidgets import Layout, interact, IntSlider,IntProgress, RadioButtons, FloatSlider,FloatRangeSlider | ||
| import numpy as np | ||
| import matplotlib.pyplot as plt | ||
| from ipywidgets import interact | ||
| import numpy as np | ||
| from sunpy.net import Fido | ||
| from sunpy.net import attrs as a | ||
| import matplotlib.dates as mdates | ||
|
|
||
| import sys | ||
| sys.path.append('/Users/ntrueba/SOLAR/code/GIT/xrtpy/xrtpy/visualization/fov/') # | ||
| import metadata_manager as ObsMeta | ||
|
|
||
| ############################################################################## | ||
| # FIDO SEARCH | ||
| # The tool is meant to preview metadata within the fido ecosystem - this means that fido is the slowest part of the code unless you are looking at a very small time frame | ||
|
|
||
| # Define the time range of interest for solar observations | ||
| time_range_1 = a.Time("2011-06-07 06:00:00", "2011-06-07 06:45:54") | ||
| time_range_2 = a.Time("2007-12-17 10:40:00", "2007-12-17 13:00:54") | ||
|
|
||
| # Specify the instrument as 'xrt' to search for Hinode X-Ray Telescope data | ||
| instrument = a.Instrument("xrt") | ||
|
|
||
|
|
||
| # This will return a catalog of available XRT data during t≠he specified period | ||
| xrt_downloaded_files_1 = Fido.search(time_range_1, instrument) | ||
| xrt_downloaded_files_2 = Fido.search(time_range_2, instrument) | ||
|
|
||
|
|
||
|
|
||
| ############################################################################## | ||
| #Metadata extraction and the XRT_meta structure | ||
| #The first function accepts the output of the fido search as an input and retrieves the corresponding metadata without having to download the data. | ||
| #By default, it downloads the level0 metadata (same as SSWIDL) which is very quick when dealing with many files. If you want the level 1 metadata, the syntax is (.., fast_bool = False) - this is only recommended for short observations (< 1hr), as it can retrieve metadata at a rate 5 obs/second | ||
| #The second function creates a handy metadata object for all the observations | ||
| #it contains a list of headers (xmeta.head_lis) | ||
| #and a dictonary (xmeta.metadata) containing important filter-separated quantities | ||
| #This could be super handy, as you get much more information than what fido gives you, so you can download observations that meet very specific conditions - let me know if you want something specific included here | ||
|
|
||
|
|
||
|
|
||
| xrt_dset1 = ObsMeta.DatasetMetaManager(xrt_downloaded_files_1) | ||
| xrt_dset2 = ObsMeta.DatasetMetaManager(xrt_downloaded_files_2)## | ||
|
|
||
| ############################################################################## | ||
| #Plotting backend | ||
| #The %matplotlib inline works best when using notebooks to avoid flickering. Ipywidgets is not available with HTML, but you can uncomment this line when you download the notebook. | ||
| #%matplotlib inline | ||
| ### | ||
| ani = xrt_dset2.plot_preview(ani_bool = False, d_mode=False) | ||
| plt.show() | ||
|
|
||
| ############################################################################## | ||
| ### | ||
| #We can also do nightmode | ||
|
|
||
| ani = xrt_dset1.plot_preview(ani_bool = False, d_mode=True, vertical_plot=True)## | ||
| plt.show() | ||
|
|
||
| ############################################################################## | ||
| ### | ||
| #We can also do a horizontal version | ||
|
|
||
| ani = xrt_dset2.plot_preview(ani_bool = False, d_mode=True, vertical_plot=False) | ||
| plt.show() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| from astropy.io import fits | ||
| import astropy.units as u | ||
|
|
||
| import numpy as np | ||
|
|
||
| from astropy.coordinates import SkyCoord | ||
|
|
||
| from sunpy.coordinates import frames | ||
| from astropy.time import Time | ||
| import scipy.io as sio | ||
| from scipy.io import readsav | ||
| import urllib.request | ||
|
|
||
|
|
||
| def download_metadata(xrt_downloaded_files, filen, overwrite=False): | ||
| url_lis = xrt_downloaded_files[0][:]['fileid'] | ||
| url_str_lis = [] | ||
| for i in range(len(url_lis)): | ||
| url_str_lis.append(url_lis[i]) | ||
| primary_hdu = fits.PrimaryHDU(data=np.ones((3, 3))) | ||
| c1 = fits.Column(name='URL', array=url_str_lis, format='100A') | ||
| c2 = fits.Column(name='header_int', array=np.asarray(range(len(url_str_lis)))+2, format='J') | ||
| table_hdu = fits.BinTableHDU.from_columns([c1, c2]) | ||
|
|
||
| hdul2 = fits.HDUList([primary_hdu, table_hdu]) | ||
| #hlis = [] | ||
|
|
||
| for i in range(len(url_str_lis)): | ||
| if (i%10 == 0): | ||
| print(int(1000.0*i/len(url_str_lis))/10.0,'%') | ||
| fsspec_kwargs = {"block_size": 100_000, "cache_type": "bytes"} | ||
| with fits.open(url_lis[i], use_fsspec=True, fsspec_kwargs=fsspec_kwargs) as hdul: | ||
| #hlis.append(hdul[0].header) | ||
| # Download a single header | ||
| t_header = hdul[0].header | ||
| image_hdu = fits.ImageHDU(data=np.ones((100, 100)), header=t_header,name="header"+str(i)) | ||
| hdul2.append(image_hdu) | ||
| return hdul2 | ||
|
|
||
| #hdul2.writeto(filen,overwrite=overwrite) | ||
|
|
||
| def date_to_meta(xrt_download_list): | ||
| time_lis = xrt_download_list[0]['Start Time'] | ||
| year_lis = xrt_download_list[0]['Start Time'].ymdhms.year | ||
| month_lis = xrt_download_list[0]['Start Time'].ymdhms.month | ||
| day_lis = xrt_download_list[0]['Start Time'].ymdhms.day | ||
| new_date = [] | ||
| file_lis = [] | ||
| for i in range(len(time_lis)): | ||
| year_str = str(year_lis[i]) | ||
| month_str = str(month_lis[i]) | ||
| day_str = str(day_lis[i]) | ||
| if len(day_str) < 2: | ||
| day_str = '0'+day_str | ||
| if len(month_str) < 2: | ||
| month_str = '0'+month_str | ||
| ndatei = year_str + month_str+ day_str | ||
| if ndatei in file_lis: | ||
| new_date.append(file_lis.index(ndatei)) | ||
| #new_date[file_lis.index(ndatei)].append(ndatei) | ||
| else: | ||
| file_lis.append(ndatei) | ||
| new_date.append(file_lis.index(ndatei)) | ||
| #print(ndatei,file_lis.index(ndatei)) | ||
| return file_lis, new_date | ||
|
|
||
| def get_urls(file_n_lis, ggg): | ||
| nfile = len(file_n_lis) | ||
| geny_lis = [] | ||
| for i in range(nfile): | ||
| find_url = 'xrt'+file_n_lis[i] | ||
| findex = ggg.find(find_url) | ||
| gen_fn = ggg[findex:findex+35] | ||
| findex2 = gen_fn.find('geny') | ||
| gen_fn = gen_fn[:findex2+4] | ||
| geny_lis.append(gen_fn) | ||
| return geny_lis | ||
|
|
||
| def get_metafile(geny_lis): | ||
| url_start = 'https://sot.lmsal.com/data/sot/metadata/sswdb/hinode/xrt/xrt_genxcat/' | ||
| ngeny = len(geny_lis) | ||
| meta_lis = [] | ||
| for i in range(ngeny): | ||
| print(i) | ||
| gen_fn = geny_lis[i] | ||
| f, h = urllib.request.urlretrieve(url_start + gen_fn) | ||
| print(i) | ||
| data2 = readsav(f)["p0"] | ||
| data_dict2 = {k : data2[k] for k in data2.dtype.names} | ||
| meta_lis.append(data_dict2) | ||
| return meta_lis | ||
|
|
||
| #def mk_meta_header(meta_lis): | ||
| #print(data_dict2['DATE_OBS']) | ||
|
|
||
| def meta_to_dict(data_dict, di): | ||
| dkeys = data_dict.keys() | ||
| hdict = {} | ||
| for dki in dkeys: | ||
| try: | ||
| hdict[dki] = data_dict[dki][di].decode('ascii') | ||
| except: | ||
| hdict[dki] = data_dict[dki][di] | ||
| return hdict | ||
|
|
||
| def match_vso_to_cat(data_dict_lis, cat_fi, xrt_download): | ||
|
|
||
| n_dict = len(data_dict_lis) | ||
| cat_time_lis = [] | ||
| for i in range(n_dict): | ||
| data_dict = data_dict_lis[i] | ||
| date_obs_cat = data_dict['DATE_OBS'] | ||
| cat_len = len(date_obs_cat) | ||
| cat_str = [] | ||
| for cat_bin in date_obs_cat: | ||
| cat_str.append(cat_bin.decode('ascii')) | ||
| cat_time = Time(np.asarray(cat_str), format='isot', scale='utc') | ||
| cat_time_lis.append(cat_time) | ||
| #print('yo') | ||
| min_ti_lis = [] | ||
| delt_lis = [] | ||
| delt_lisp = [] | ||
| delt_lism = [] | ||
| header_lis = [] | ||
| for i in range(len(xrt_download[0]['Start Time'])): | ||
| cat_time = cat_time_lis[cat_fi[i]] | ||
| stime = xrt_download[0]['Start Time'][i] | ||
| delt = cat_time - stime | ||
| delt = delt.value*24.0*3600.0 | ||
| min_ti = np.argmin(np.abs(delt)) | ||
| min_ti_lis.append(min_ti) | ||
| delt_lis.append(delt[min_ti]) | ||
| try: | ||
| delt_lisp.append(delt[min_ti+1]) | ||
| delt_lism.append(delt[min_ti-1]) | ||
| except: | ||
| print() | ||
| header_lis.append(meta_to_dict(data_dict_lis[cat_fi[i]],min_ti)) | ||
| return header_lis | ||
|
|
||
| def get_html_lis(): | ||
| url_start = 'https://sot.lmsal.com/data/sot/metadata/sswdb/hinode/xrt/xrt_genxcat/' | ||
| with urllib.request.urlopen(url_start) as response: | ||
|
|
||
| html = response.read() | ||
| #print(response.info()) | ||
| #print() | ||
| ggg = html.decode('utf-8') | ||
| return ggg | ||
|
|
||
| def download_metadata_fast(xrt_downloaded_files, ggg=None): | ||
| if (ggg == None): | ||
| ggg = get_html_lis() | ||
| file_lis, new_date = date_to_meta(xrt_downloaded_files) | ||
| genyl = get_urls(file_lis, ggg) | ||
| print('downloading') | ||
| tmeta_lis = get_metafile(genyl) | ||
| print('done') | ||
| hlis3 = match_vso_to_cat(tmeta_lis, new_date, xrt_downloaded_files) | ||
| return hlis3 | ||
|
|
||
| def fetch_metadata(xrt_downloaded_files, fast_bool = True): | ||
| if fast_bool: | ||
| print('Fast Metadata (Level 0)') | ||
| return download_metadata_fast(xrt_downloaded_files, ggg=None) | ||
| else: | ||
| print('Slow Metadata (Level 1)') | ||
| hdul = download_metadata(xrt_downloaded_files,'') | ||
| hlis = [] | ||
| for i in range(len(xrt_downloaded_files[0])): | ||
| hlis.append(hdul[i+2].header) | ||
| return hlis | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's a function that is not intended for end users, then we can start the name with an underscore to indicate that it is private.