ó
ÚaÎ]c           @   s´  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 „  Z d e f d „  ƒ  YZ e d k r°e j d	 d
 d e j ƒ d  d l Z d  d l m Z e j d d ƒ Z e j d d d d d ƒe j d d d d d d ƒe j d d d d d d ƒe j d d d e d d ƒe j ƒ  Z e j e j ƒ Z e j Z e e ƒ Z x/ e j D]! Z d  e f GHe j  e e ƒ qˆWn  d S(!   iÿÿÿÿN(   t   OptionParser(   t   fromorg(   t   Threadc         C   si   t  |  j ƒ  ƒ } | j ƒ  d d j | ƒ d GHd d j g  | D] } t |  | ƒ ^ qB ƒ d GHd  S(   Nt   |s    | s    |(   t   listt   keyst   sortt   joint   str(   t   dt   lt   k(    (    s*   /home/dice/stardice/scheduler/scheduler.pyt   dict_to_org   s    
t	   Schedulerc           B   se   e  Z d
 d  „ Z d „  Z d „  Z d
 d „ Z d „  Z d
 d d „ Z d „  Z	 d „  Z
 d	 „  Z RS(   c   	      C   s[  t  |  _ g  |  _ | |  _ | d k rB t | j j j ƒ  ƒ } n+ g  | D] } | t	 | j | ƒ f ^ qI } g  | D]- \ } } | t	 t
 | ƒ | | |  ƒ f ^ qt |  _ x* |  j D] \ } } |  j | j g  ƒ q´ W|  j ƒ  t j j ƒ  } d | j | j | j f } t j j |  j j j | ƒ |  _ x' |  j D] \ } } t |  | | ƒ q7Wd S(   sÉ   
        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/
        s   %04d-%02d-%02dN(   t   Falset   abortingt   current_jobst   configt   NoneR   t
   Subsystemst   __dict__t   itemst   getattrt   allsubsystemst
   subsystemst	   async_runt	   configureR   t   datetimet   nowt   yeart   montht   dayt   ost   patht   Outputt
   output_dirt   setattr(	   t   selfR   t   device_listt   namet   compt   sR   t   output_dir_nowt   sys(    (    s*   /home/dice/stardice/scheduler/scheduler.pyt   __init__   s    			+=
!c         C   s3   t  d | d | ƒ } |  j j | ƒ | j ƒ  d  S(   Nt   targett   args(   R   R   t   appendt   start(   R%   t   funcR.   t   t(    (    s*   /home/dice/stardice/scheduler/scheduler.pyR   7   s    c         C   s-   x& |  j  r( |  j  j ƒ  } | j ƒ  q Wd  S(   N(   R   t   popR   (   R%   R2   (    (    s*   /home/dice/stardice/scheduler/scheduler.pyR   <   s    c         C   sà   | d k r |  j } n  t j d | ƒ } t j t | ƒ ƒ x d d d g D]n } x[ |  j D]P \ } } |  j r t	 d ƒ ‚ n  | | r] |  j
 t | | ƒ | | g ƒ q] q] W|  j ƒ  qM Wt j d | ƒ | j d | ƒ S(   sÖ   
        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
        R#   t   before_exposuret   exposuret   after_exposures   Aborted observation programs   dumping files -> %sN(   R   R#   t   fitsbuildert   FitsBuildert   loggingt   infoR   R   R   t   StopIterationR   R   R   t   dump(   R%   t   stateR#   t   fbR1   R'   t	   subsystem(    (    s*   /home/dice/stardice/scheduler/scheduler.pyt   shootA   s    	
&c         C   sF   d } x9 |  j  D]. \ } } | d | d 7} | | j ƒ  7} q W| S(   s   Print system status
        t    s   
s   
----------
(   R   R=   (   R%   t   stR'   R?   (    (    s*   /home/dice/stardice/scheduler/scheduler.pyR=   Z   s
    i    c   
      c   sÊ   d d l  m } t |  _ t j d | ƒ t | ƒ \ } } | j | ƒ } xx t | | | d ƒ D]_ \ } } t |  _	 xG |  j	 sÁ t j d | t
 | ƒ f ƒ |  j | | ƒ }	 |	 | f Vq{ Wqc Wd  S(   Niÿÿÿÿ(   t   prog_fieldss   Executing program %si   s   Processing exposure %d/%d(   t   stardice.obsprogRC   R   R   R9   R:   R   t   astypet	   enumeratet   acceptedt   lenR@   (
   R%   t   program_fileR#   t	   startlineRC   t   programR   t   expR5   t   filename(    (    s*   /home/dice/stardice/scheduler/scheduler.pyt   executed   s    	$	c         C   s   t  |  _ d  S(   N(   t   TrueR   (   R%   (    (    s*   /home/dice/stardice/scheduler/scheduler.pyt   abortq   s    c         C   sd   |  j  j |  j  j } } t t d | | f ƒ ƒ } | d k rW |  j j | | ƒ n  |  j j ƒ  S(   s@    Mount synchronisation with the result of last analysis
        s3   Do you want set current pointing to %f,%f (y/n)? : t   y(   t   analyset   frat   fdect   evalt   inputt   mountt	   set_radect	   get_radec(   R%   RS   RT   t   a(    (    s*   /home/dice/stardice/scheduler/scheduler.pyt
   mount_initt   s
    c         C   s]   i  } xP |  j  D]E \ } } t | | <x, | j D]! } | j | | d | | f <q0 Wq W| S(   Ns   %s.%s(   R   RO   t   expected_params(   R%   t   rest   nR)   R   (    (    s*   /home/dice/stardice/scheduler/scheduler.pyt   get_default~   s    
#N(   t   __name__t
   __module__R   R,   R   R   R@   R=   RN   RP   R[   R_   (    (    (    s*   /home/dice/stardice/scheduler/scheduler.pyR      s    			
		
t   __main__t   formats   %(asctime)s %(message)st   level(   t   configfilest   descriptions1   Supervise the execution of an observation programt   programst   nargst   *t   helps   program to be executeds   -cs   --configt   defaults   scheduler.cfgs   Name of the configuration files   -os   --output-dirs   ./sL   Path to the directory where images will be stored. (Created if not existing)s   -rs	   --releases€   if True, release the servers at the end of execution; set to False if you want to reuse the scheduler after the first execution.s   Executing program (!   R    R   R9   t   optparseR    t   stardice.orgfilesR   R7   R   t	   threadingR   R   t   objectR   R`   t   basicConfigt   DEBUGt   argparset   stardiceRe   t   ArgumentParsert   parsert   add_argumentR   t
   parse_argsR.   t   ConfigR   R#   t	   schedulerRg   RK   RN   (    (    (    s*   /home/dice/stardice/scheduler/scheduler.pyt   <module>   sD   	p			