ó
ÜªE^c           @   sÖ   d  d l  m Z m Z d  d l m Z m Z d  d l m Z d  d l Td  d l	 m
 Z
 d  d l m Z d  d l Z d  d l Z d  d l Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   rendert   redirect(   t   HttpResponset   HttpResponseRedirect(   t   serializers(   t   *(   t   EventSet(   t   settingsNc         C   sa   t  j j ƒ  j d ƒ } |  j ƒ  r@ t j d | ƒ } t | ƒ Si | d 6} t |  d | ƒ Sd  S(   Ns   -created_att   jsont   eventset_lists   events/eventsets.html(	   R   t   objectst   allt   order_byt   is_ajaxR   t	   serializeR   R    (   t   requestR	   t   datat   context(    (    s+   /home/yuhw/wc/wire-cell-bee/events/views.pyt	   eventsets   s    

c         C   sF   |  j  ƒ  r0 t |  ƒ }  t j j d |  ƒ } n t ƒ  } |  | _ | S(   Nt   pk(   t   isdigitt   intR   R
   t   gett   alias(   t   set_idt   eventset(    (    s+   /home/yuhw/wc/wire-cell-bee/events/views.pyt   get_eventset   s    		c         C   sy  i | d 6g  d 6} y2d t  j | f } t t j | ƒ ƒ } x| D]û } t j | d | ƒ } d } d | k r| d | d } t j j | ƒ rÒ t | ƒ �( }	 t j	 |	 ƒ }
 t
 |
 j ƒ  ƒ } Wd  QXn  | d j i | d 6| d	 6d
 | d | d d 6ƒ qF | d j i | d 6d d	 6d | d | d d 6ƒ qF WWn  t k
 rht d | d ƒ SXt |  d | ƒ S(   Nt   collection_idt   setss   %s/%st   /t   unknownR   s   /data/summary.jsont   namet   nEventss   set/s   /event/list/t   urls   [folder]s   collection/s   Collection s    does not exist.s   events/collection.html(   R   t
   MEDIA_ROOTt   sortedt   ost   listdirt   patht   existst   openR   t   loadt   lent   keyst   appendt   OSErrorR   R    (   R   R   R   t   collection_roott   subdirst   subdirR   R    t   summary_filet	   json_filet   info(    (    s+   /home/yuhw/wc/wire-cell-bee/events/views.pyt
   collection"   s4    
#c         C   sÀ   y t  | ƒ } Wn  t k
 r2 t d | d ƒ SXi | d 6g  d 6} | j ƒ  } | r° | j ƒ  } | j d t ƒ x8 | D]- } | d j | | ƒ | | d d d <q| Wn  t |  d | ƒ S(	   Ns   Event set for s    does not exist.R   t
   event_listt   keyiÿÿÿÿt   ids   events/event_list.html(	   R   t   ObjectDoesNotExistR   t   summaryR+   t   sortR   R,   R    (   R   R   R   R   R9   t   sorted_keysR6   (    (    s+   /home/yuhw/wc/wire-cell-bee/events/views.pyR5   G   s    
c            sZ  y t  | ƒ } Wn  t k
 r2 t d | d ƒ SXi | d 6| d 6| d 6} d „  } ‡  f d †  ‰  | j t | ƒ ƒ } t | ƒ d k r— t d	 ƒ Si | j ƒ  d
 6t | ƒ d 6| j | ƒ d 6| j t | ƒ ƒ d 6| j	 t | ƒ ƒ d 6| j
 t | ƒ ƒ d 6| d 6i  d 6} | |  ƒ | d <|  j ƒ  rFt t j | ƒ ƒ St |  d | ƒ Sd  S(   Ns   Event set for s    does not exist.R   R   t   event_idc         S   sÙ   i  } |  j  } xÃ | j ƒ  D]µ \ } } y t | ƒ } Wn t k
 rQ | } n X| d k rg t } n | d k r| t } n  | j d ƒ d k rÇ | j d ƒ \ } } | j | i  ƒ | | | | <q | | | <q W| S(   s    only works for two nested levelst   truet   falset   .i    (	   t   GETt	   iteritemst   floatt
   ValueErrort   Truet   Falset   findt   splitt
   setdefault(   R   t   optionst   qR6   t   valuet   value_cleant   key1t   key2(    (    s+   /home/yuhw/wc/wire-cell-bee/events/views.pyt   queryToOptionsm   s"    	
		c            s_   xX | j  ƒ  D]J \ } } t | t j ƒ rM ˆ  |  j | i  ƒ | ƒ |  | <q | |  | <q W|  S(   N(   RA   t
   isinstancet   collectionst   MappingR   (   t   dt   ut   kt   v(   t   update(    s+   /home/yuhw/wc/wire-cell-bee/events/views.pyRW   ƒ   s
    "i    s   Sorry, no data found.R    R7   t
   experimentt   hasMCt   hasOPt   hasDeadAreat   sstt   configs   events/event.html(   R   R8   R   t
   recon_listR   R*   t   event_countt   geomt   has_MCt   has_OPt   has_DeadAreaR   R   t   dumpsR    (   R   R   R<   R   R   RO   t   sst_listRI   (    (   RW   s+   /home/yuhw/wc/wire-cell-bee/events/views.pyt   event`   s4    
	

c         C   s  t  | ƒ } | j ƒ  d | d | } | d k rA | d 7} n� | d k rZ | d 7} nv | d k rs | d 7} n] | d k rŒ | d	 7} nD | d
 k r¥ | d 7} n+ | d k r¾ | d 7} n | d | d 7} y  t | ƒ j ƒ  } t | ƒ SWn t k
 rt | d ƒ SXd S(   s   only for ajaxR   t   mcs   -mc.jsont   ops   -op.jsons   WireCell-charges   -rec_charge_blob.jsons   WireCell-simples   -rec_simple.jsons   WireCell-deblobs   -rec_charge_cell.jsont   truths   -truth.jsont   -s   .jsons    does not existN(   R   t   data_dirR(   t   readR   t   IOError(   R   R   R<   R   R   t   filenameR   (    (    s+   /home/yuhw/wc/wire-cell-bee/events/views.pyR   ¬   s(    c         C   s3   d t  j | | f } i | d 6} t |  d | ƒ S(   Ns   %smedia/%s/plots/%s/t   base_plots_urls   events/evd_2D.html(   R   t
   STATIC_URLR    (   R   R   R<   RS   R   (    (    s+   /home/yuhw/wc/wire-cell-bee/events/views.pyt   evd_2DÈ   s    
c         C   sL  d d l  } d d l } |  j d k r>|  j d } t | j ƒ  ƒ } t j | d } t | d ƒ �+ } x! | j	 ƒ  D] } | j
 | ƒ qv WWd QXd | d } yB | j | d	 t ƒ}	 |	 j d
 ƒ rÐ d GHn d G|	 Gt d ƒ SWn | j k
 r t d ƒ SXt j | }
 d | |
 |
 f } | j | d	 t ƒt | ƒ St d ƒ Sd S(   s   file uploadiÿÿÿÿNt   POSTt   files   .zips   wb+s	   unzip -l s+    | head -n 5 | tail -n 2 | awk '{print $4}'t   shells   data/
data/s   Good Format!s   Bad Format!t   DataNotValids!   unzip %s -d %s && chmod -R g+w %ss   No Get, Please POST(   t   uuidt
   subprocesst   methodt   FILESt   strt   uuid4R   R"   R(   t   chunkst   writet   check_outputRD   t
   startswithR   t   CalledProcessErrort   call(   R   Rv   Rw   t   new_filet   unique_namet   new_filenamet   destinationt   chunkt   cmdt   outputt   extract_dir(    (    s+   /home/yuhw/wc/wire-cell-bee/events/views.pyt   uploadÒ   s.    
(   t   django.shortcutsR    R   t   django.httpR   R   t   django.coreR   t   django.core.exceptionst   events.modelsR   t   beeR   R$   R   RQ   R   R   R4   R5   Rf   R   Rq   RŠ   (    (    (    s+   /home/yuhw/wc/wire-cell-bee/events/views.pyt   <module>   s   
$				%		L		
