a
    L9f)                     @   st   d dl Z d dlZd dlmZ d dlmZ d dlZd dl	Z
d dlmZ d dlmZ d dlT d dlZG dd deZdS )    N)HydraHeadApp)SkyCoord)frames)*c                   @   s   e Zd ZdddZdd ZdS )	SolarMach c                 K   s   | j | || _d S )N)__dict__updatetitle)selfr
   kwargs r   G/Users/putuwistika/Documents/TRILAK/hydralit-example/apps/solar_mach.py__init__   s    zSolarMach.__init__c                    s  t d t d t jddd t d t j Z t jdtj	 tj
dd	 }t jd
tdd}tj||d}W d    n1 s0    Y  t j t t jd t jjddd}t jjddd}t jjddd}t jjddd}t jjddd}t jjd|d t jdddgdd}	|	dkrZt dddd}
t d d!d"d}|	dkrt dd#d$d}
t d d!d"d}t|
tj |tj tj|d%}|tjd&d'}|jj}
|jj}dd l}ttt  d(d)}W d    n1 s0    Y  |du rd }
d }W d    n1 s(0    Y  t jd* t j  t jj!d+d,d-d.}t jj!d/d0d-d.|"d1 "d1 fd2d3t#t$ D  d}zfd4d3t#t$D W n t%y   d}Y n0 t& }|j'dd5 d6gt$| |_(t j)|d7  t jd8 W d    n1 s,0    Y  |rNt *d9 t +  t$ t$kr\t,| |
|}d:tj||d; d< }|j-|||||d= d:tj||d; }t./ }t0j1|d>d?d@ | j2|3 |d< dAddB}|j4}|dC |_(|j5dCgdD}|6d}|j7dEdFdGdHdIdJdKdLdMdNdOdPdD}t )|j8 | j2|j4|dQ dRddB}n t *dSt$  dTt$ dU t dV t dW t dX t 9dY\}}|dZ |jd[dd t jd\dd d S )]Nz
Solar-MACHa(  Source for this great app is from the Streamlit gallery [Solar-MACH](https://github.com/jgieseler/Solar-MACH). An example of how easy it is to convert an existing application and use within a Hydralit multi-page application, see the secret saurce [here] (https://github.com/TangleSpace/hydralit).z<br><br>T)unsafe_allow_htmlz6## Multi-spacecraft longitudinal configuration plotterzSelect date   )dayszSelect time      z%Y-%m-%d %H:%M:%SzPlot options:zParker spiral for each body)valuezStraight line from Sun to bodyzAdd Earth-aligned coord. systemFzTransparent backgroundzPlot reference (e.g. flare)z"Reference coordinates (e.g. flare))expandedzCoordinate system:Z
CarringtonZ
Stonyhurstr   )indexz
Longitude:ih     z	Latitude:iZ   iL   )frameZobstimeSun)ZobserverzSolar wind speed for referencei  z7Choose bodies/spacecraft and measured solar wind speedsz0Bodies/spacecraft (scroll down for example list)z6STEREO A, Earth, BepiColombo, PSP, Solar Orbiter, Mars2   )heightz.Solar wind speed per body/SC (mind the order!)z400, 400, 400, 400, 400, 400,c                    s   g | ]} |   qS r   )strip.0i)	body_listr   r   
<listcomp>K       z!SolarMach.run.<locals>.<listcomp>c                    s   g | ]}t  |  qS r   )intr    r!   )vsw_listr   r   r%   N   r&   )inplacer   KeyzY[Complete list of available bodies](https://ssd.jpl.nasa.gov/horizons.cgi?s_target=1#top)z`ERROR: There is something wrong in the solar wind speed list! Maybe some missing or wrong comma?zSolar-MACH_z%Y-%m-%d_%H-%M-%Sz.png)plot_spiralsplot_sun_body_lineshow_earth_centered_coordreference_vswtransparentpngtight)formatbbox_incheszDownload figure as .png file)	pickle_itSpacecraft/Body)columnszSpacecraft / bodyzCarrington longitudezCarrington latitudezHeliocent. distancez'Longitud. separation to Earth longitudez%Latitud. separation to Earth latitudezSolar wind speedz'Magnetic footpoint Carrington longitudez)Longitud. separation bw. body & referencez>Longitud. separation bw. body's magnetic footpoint & referencez+Latitudinal separation bw. body & reference)r5   u   Carrington Longitude (°)u   Latitude (°)zHeliocentric Distance (AU)z,Longitudinal separation to Earth's longitudez*Latitudinal separation to Earth's latitudeZVswz)Magnetic footpoint longitude (Carrington)z7Longitudinal separation between body and reference_longzLLongitudinal separation between body's mangetic footpoint and reference_longz5Latitudinal separation between body and reference_latz.csvzDownload table as .csv filezMERROR: Number of elements in the bodies/spacecraft list                     (z) and solar wind speed list (z})                     don't match! Please verify that for each body there is a solar                     wind speed provided!z---a   The *Solar MAgnetic Connection Haus* (Solar-MACH) tool is a                     multi-spacecraft longitudinal configuration plotter. It was                     originally developed at the University of Kiel, Germany, and further                     discussed within the [ESA Heliophysics Archives USer (HAUS)]                    (https://www.cosmos.esa.int/web/esdc/archives-user-groups/heliophysics)                     group. It is now opened to everyone ([original code]                    (https://github.com/esdc-esac-esa-int/Solar-MACH)).z[Forked and modified](https://github.com/jgieseler/Solar-MACH) by                     [J. Gieseler](https://jgieseler.github.io) (University of Turku, Finland).                     [**Get in contact**](mailto:jan.gieseler@utu.fi?subject=Solar-MACH).)   r   zThe development of the online tool has received funding from the                     European Union's Horizon 2020 research and innovation programme                     under grant agreement No 101004159 (SERPENTINE).z[<img src="https://serpentine-h2020.eu/wp-content/uploads/2021/02/SERPENTINE_logo_new.png"                         height="80">](https://serpentine-h2020.eu)a  Powered by:                     [<img src="https://matplotlib.org/stable/_static/logo2_compressed.svg" height="25">](https://matplotlib.org)                     [<img src="https://streamlit.io/images/brand/streamlit-logo-secondary-colormark-darktext.svg" height="30">](https://streamlit.io)                     [<img src="https://raw.githubusercontent.com/sunpy/sunpy-logo/master/generated/sunpy_logo_landscape.svg" height="30">](https://sunpy.org)):str
   	subheadermarkdownsidebar	container
date_inputdatetimedatetoday	timedelta
time_inputtimecombinestrftimecheckboxexpanderradiosliderr   udegr   ZHeliographicStonyhurstZtransform_toZHeliographicCarringtonlonr   latmathr'   float
text_input	text_areasplitrangelen
ValueErrorZprint_body_listreset_indexr   tableerrorstopZHeliosphericConstellationplotioBytesIOpltsavefigdownload_buttongetvalueZcoord_tabledroproundrenameTr6   )r   dtr?   r+   r,   r-   r/   Zplot_referenceZreference_sysZreference_longZreference_latcoordrN   r.   Zfull_body_listZ	wrong_vswZ
all_bodiescZ	plot_filefilenameZplot2Zdownload_button_strdfcol1col2r   )r$   r(   r   run   s    


 2

4
$



,









zSolarMach.runN)r   )__name__
__module____qualname__r   rm   r   r   r   r   r      s   
r   )r>   r[   hydralitr   Zastropy.unitsunitsrJ   pandaspd	streamlitr8   Zastropy.coordinatesr   Zsunpy.coordinatesr   Zapps.extras.backmappinghydralit_componentshcr   r   r   r   r   <module>   s   