B
    a]                 @   s4  d dl Z 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 ZG dd deZed	kr0ejd
ejd d dlZd dlmZ ejddZejdddd ejddddd ejddddd ejddddd e ZeejZejZeeZx(ejD ]Zed ef e ee qW dS )!    N)OptionParser)fromorg)Threadc                sR   t   }|  tdd| d  tdd fdd|D  d  d S )N|z | z |c                s   g | ]}t  | qS  )str).0k)dr   */home/dice/stardice/scheduler/scheduler.py
<listcomp>   s    zdict_to_org.<locals>.<listcomp>)listkeyssortprintjoin)r
   lr   )r
   r   dict_to_org   s    r   c               @   sZ   e Zd ZdddZdd Zdd Zddd	Zd
d ZdddZdd Z	dd Z
dd ZdS )	SchedulerNc                s   d_ g _ _|dkr,t jj }n fdd|D } fdd|D _x jD ]\}}|j	g  q\W 
  tj }d|j|j|jf }tj
jjj|_xjD ]\}}t|| qW dS )z
        Creates a scheduler instance that will handle a schedule and in particular
        handle taking the images

        Data will be saved in 
        [config.Output.dir]/year-month-day/
        FNc                s   g | ]}|t  j|fqS r   )getattr
Subsystems)r   name)configr   r   r   &   s    z&Scheduler.__init__.<locals>.<listcomp>c                s&   g | ]\}}|t t| |fqS r   )r   allsubsystems)r   r   comp)r   selfr   r   r   '   s    z%04d-%02d-%02d)abortingcurrent_jobsr   r   r   __dict__items
subsystems	async_runZ	configurer   datetimenowyearmonthdayospathOutput
output_dirsetattr)r   r   Zdevice_listr   sr#   Zoutput_dir_nowsysr   )r   r   r   __init__   s    
zScheduler.__init__c             C   s$   t ||d}| j| |  d S )N)targetargs)r   r   appendstart)r   funcr0   tr   r   r   r!   7   s    zScheduler.async_runc             C   s"   x| j r| j  }|  qW d S )N)r   popr   )r   r4   r   r   r   r   <   s    
zScheduler.joinc             C   s   |dkr| j }tj|d}tt| xTdD ]L}x>| jD ]4\}}| jrPtd|| r:| 	t
||||g q:W |   q.W td|  |j|dS )z
        Shoots an exposure: runs every subprocess before_exposure, exposure and after_exposure
        and dumps the exposure in output_dir.

        If output_dir isn't set, uses the scheduler output_dir
        N)r*   )Zbefore_exposureexposureZafter_exposurezAborted observation programzdumping files -> %s)r*   fitsbuilderZFitsBuilderlogginginfor   r    r   StopIterationr!   r   r   dump)r   stater*   Zfbr3   r   	subsystemr   r   r   shootA   s    
zScheduler.shootc             C   s8   d}x.| j D ]$\}}|d| d 7 }|| 7 }qW |S )zPrint system status
         
z
----------
)r    r<   )r   str   r=   r   r   r   r<   Z   s
    zScheduler.stater   c       
      c   s   ddl m} d| _td|  t|\}}||}x`t||d  |d D ]F\}}d| _x6| jstd|t	|f  | 
||}	|	|fV  q^W qNW d S )Nr   )prog_fieldsFzExecuting program %s   zProcessing exposure %d/%d)Zstardice.obsprogrB   r   r8   r9   r   astype	enumerateZacceptedlenr>   )
r   Zprogram_filer*   	startlinerB   programr	   expr6   filenamer   r   r   executed   s    
 zScheduler.executec             C   s
   d| _ d S )NT)r   )r   r   r   r   abortq   s    zScheduler.abortc             C   sF   | j j| j j }}ttd||f }|dkr<| j|| | j S )z@ Mount synchronisation with the result of last analysis
        z3Do you want set current pointing to %f,%f (y/n)? : y)ZanalysefrafdecevalinputZmountZ	set_radecZ	get_radec)r   rN   rO   ar   r   r   
mount_initt   s
    zScheduler.mount_initc             C   sJ   i }x@| j D ]6\}}d||< x$|jD ]}|j| |d||f < q$W qW |S )NTz%s.%s)r    Zexpected_params)r   resnr,   r	   r   r   r   get_default~   s    zScheduler.get_default)N)N)Nr   )__name__
__module____qualname__r.   r!   r   r>   r<   rK   rL   rS   rV   r   r   r   r   r      s   
 



r   __main__z%(asctime)s %(message)s)formatlevel)configfilesz1Supervise the execution of an observation program)descriptionprograms*zprogram to be executed)nargshelpz-cz--configzscheduler.cfgzName of the configuration file)defaultrb   z-oz--output-dirz./zLPath to the directory where images will be stored. (Created if not existing)z-rz	--releaseFzif True, release the servers at the end of execution; set to False if you want to reuse the scheduler after the first execution.zExecuting program )!r'   r"   r8   optparser   Zstardice.orgfilesr   r7   r   	threadingr   r   objectr   rW   basicConfigDEBUGargparsestardicer]   ArgumentParserparseradd_argument
parse_argsr0   Configr   r*   	schedulerr_   rH   r   rK   r   r   r   r   <module>   sD   p
