³ò
îýIc           @   s£  d  d k  Z  d  d k Z d  d k l Z l Z l Z d  d k l Z l Z l Z l	 Z	 l
 Z
 d  d k l Z l Z l Z l Z l Z l Z l Z l Z d  d k l Z e i d i d ƒ d Z d	 Z d
 „  Z d „  Z d „  Z d „  Z e d j o¨e d d f d d f g ƒ Z e d e  i  ƒ Z! e! d j o–e" e! ƒ Z! e d e  i  ƒ Z# e d e  i  ƒ Z$ e i% ƒ  Z& e i' i e$ ƒ d Z( d Z) e* e$ d e) d ƒ i+ d e e& f ƒ e d d e( d e) e e& e# e! f ƒ e# e j o e i, e! e ƒ nE e d ƒ e i- d e# d e e! f ƒ i. ƒ  Z) e d d e( e) ƒ e d  e  i  ƒ Z/ e	 e/ e0 e ƒ Z e d d j o€e i1 d! ƒ Z2 d Z3 x8 e2 i4 ƒ  D]* Z5 e5 i6 d" ƒ d j o e3 d 7Z3 n q–We" e2 d# ƒ Z7 d Z8 xy e8 e3 j  oY e7 d e3 Z7 d$ e7 Z9 e2 e9 i: d% ƒ o( e2 e9 d% d& j o Pn e8 d 7Z8 n PqÝWe e d d' ƒ Z e d d j o‰e ƒ  Z; e2 e9 e d( <e e ƒ Z e ƒ  e; d) Z; e d d d* d+ e; ƒ d, e d j Z< e< o d e d <n e d d j oe	 e/ e= e ƒ Z e d d j o» e i1 d! ƒ Z2 e2 i: e9 ƒ ok d- e7 e2 d# <e< o d. e2 e9 d% <nA e2 e9 d% d. j o+ d/ e d j o e2 e9 d% d& j n n n d Z7 d- e7 e2 d# <e2 e d! <e
 e/ e ƒ Z n e< o e9 d0 e d e d <n n n n e d d j o
 d% Z) n d1 Z) e* e$ d e) d ƒ i+ e d d2 ƒ e d d e( d e) e e& e# e! f ƒ e d ƒ e# e j o e i, e! e ƒ n; e i- d e# d e e! f ƒ i. ƒ  Z) e d d e( e) ƒ nÁe d3 e d4 e  i  ƒ f d5 e d6 e  i  ƒ f d7 e d8 e  i  ƒ f d9 e d: e  i  ƒ f d; e d< e  i  ƒ f d= e d> e  i  ƒ f d? e d@ e  i  ƒ f dA e dB e  i  ƒ f dC e dD e  i  ƒ f dE e dF e  i  ƒ f dG e dH e  i  ƒ f dI e dJ e  i  ƒ f dK e dL e  i  ƒ f dM e dN e  i  ƒ f dO e dP e  i  ƒ f dQ e dR e  i  ƒ f dS e dT e  i  ƒ f dU e dV e  i  ƒ f dW e dX e  i  ƒ f dY e dZ e  i  ƒ f g ƒ e d( <e e ƒ Z e  i> e d ƒ n d S([   iÿÿÿÿN(   t   sleept   timet   strftime(   t   skyd_statust
   skyd_claimt   skyd_releaset   skyd_read_conft   skyd_write_conf(   t   hide_envt
   dict_entryt   whichR   t   startt   is_theret   sayt   run_cmd(   t   SIGUSR1t   HOSTNAMEt   .i    t   smeidb_skydc      
   C   sU  d |  j p d | j o g  Sn t  d d d g g f d d d g g f d d d g g f d	 d d
 g g f d d
 d g g f d d d g g f d d d g g f g ƒ } | i t ƒ p g  Sn t |  ƒ } t | ƒ } | t } | d j o€ d } xw | t | ƒ j  o_ | | | d j o g  | | | d +qü | | | d j o | | | d <n | d 7} qü Wn | d j o€ d } xw | t | ƒ j  o_ | | | d j  o g  | | | d +q‰| | | d j  o | | | d <n | d 7} q‰Wn xN t t | ƒ ƒ D]: } d | | d | | d <d | | d | | d <qW| S(   Nt   _t   zorroi   iû  t   zeldaiÜ(  s   sun iL;  t   ziggyi-O  t   sidia  t   sethis  t   suni¸ˆ  i    s   %d(   t   dictt   has_keyt   gruntt   intt   lent   range(   t	   min_orbitt	   max_orbitt   local_orbitst   min_orbt   max_orbt   orbitst   i(    (    s   skyd_orbit.pyt   skyd_local_orbits   sH    

 c         C   sT   xM |  D]? } | i  ƒ  } t | ƒ d j o | d | j o PqF q q Wd } | S(   Ni    t    (   t   splitR   (   R%   t   orbnrt   orbitt   tmp(    (    s   skyd_orbit.pyt   skyd_getorbitE   s     c
      	   C   s*  t  |  ƒ }
 | o t t |	 |  ƒ }	 n t t |	 |  ƒ }	 |	 d d j o |	 Sn |	 i d ƒ } | d j o| d j oû t | | ƒ } | d j o  t |	 d d | |
 f ƒ }	 qd	 | j o¨ | o
 d } qt| op | i ƒ  } t | ƒ d
 j  o
 d } qjt	 | ƒ t	 | d ƒ j o
 d } qjt |	 d d | |
 f ƒ }	 qtt |	 d d | |
 f ƒ }	 qd } n‹x‡| D]`} t | ƒ d j o qn | i
 ƒ  o qn | i d ƒ d j o qn | i ƒ  } | d j o$ t | d ƒ t | ƒ j  o qn | d j o$ t | d ƒ t | ƒ j o qn d | j o2 t | ƒ d j o t | d ƒ } n d } Pn d	 | j oU | oJ t | ƒ d j o, t	 | ƒ t	 | d ƒ j o d } Pq×qÛd } PqßqqWt |	 d d | | |
 f ƒ }	 |	 d d j os | i | ƒ } t | i d ƒ | i d ƒ | i d ƒ | i d	 ƒ g ƒ } | d | !d | t d ƒ t f } qªn!| d j oFt | | ƒ } | d j o  t |	 d d | |
 f ƒ }	 nG d | j o3 | i ƒ  d } t |	 d d | | |
 f ƒ }	 n d } |	 d d j o¯ | i | ƒ } | i ƒ  d } t | ƒ d j o t | ƒ } n d } | d 7} d } | | j o( | d | i | ƒ !d t d ƒ } qÙ| d | i | ƒ !d | } qªnÎ| d j oÇt | | ƒ } | d j o  t |	 d d | |
 f ƒ }	 nÒ d | j o¾ | i ƒ  d } | o$ | d | i | ƒ !d } d } q÷| d	 j o# t |	 d d | | |
 f ƒ }	 q÷| d j o# t |	 d d | | |
 f ƒ }	 q÷t |	 d d | | |
 f ƒ }	 n d } |	 d d j o¥ | i | ƒ } | o
 | } n | d j oP t | i ƒ  d ƒ } | d 7} | d | i | ƒ !d t d ƒ d | } q­d } | d | i | ƒ !d | } qªnú | d j oÖ t | | ƒ } | d j o  t |	 d d | |
 f ƒ }	 nG d | j o3 | i ƒ  d } t |	 d d | | |
 f ƒ }	 n d } |	 d d j o? | i | ƒ } | d | i | ƒ !d  t d ƒ d | } qªn t |	 d d! | ƒ }	 |	 d d j oQ | | | <| |	 d <| p t |	 |  ƒ }	 n |	 d d j o | |	 d" <q&n | p t |	 |  ƒ }	 n |	 S(#   s}  
#+
# NAME:
#	skyd_cat
# PURPOSE:
#	Manipulates the orbit catalogue
# CALLING SEQUENCE:
#	status = skyd_cat(filepath,checkver,prog_version,overwrite,ignore_cat,task,orbnr,min_orbit,max_orbit,status)
# INPUTS:
#	filepath	string		file name of user catalogue
#	checkver	integer		0: do not check version number
#					1: check version number
#	prog_version string		smei_skyd version number
#	overwrite	integer		0: do not overwrite existing skymap
#					1: overwrite existing skymap
#	task		string		task to be performed
#					one of 'set_busy','set_make','set_done','set_skip'
#					'set_busy': select an orbit for indexing
#	orb_nr		string		orbit number (as a string!, e.g. '2012'
#					used for task 'set_make', 'set_done' and 'set_skip'
#	min_orbit	string		lowest orbit number to be processed
#	max_orbit	string		highest orbit number to be processed
#	status		dictionary	status dictionary
# OUTPUTS:
#	status		dictionary	status dictionary
#					on success:
#					status['number'] and status['message'] are not modified
#					(should be 0 and blank string, respectively)
#					key 'orbit' contains the full record from the catalogue
#					for the relevant orbit.
#					on failure:
#					status['number' ] is set to 1
#					status['message'] contains error message
#					Possible reasons:
#					- skyd_claim failed to claim the catalogue
#					- invalid task
#					- no orbit left (task 'set_make')
#					- input orbit orb_nr is not marked "busy"
#					- skyd_release failed to release the catalogue
# CALLS:
#	skyd_claim, skyd_release, skyd_status
# MODIFICATION HISTORY:
#	DEC-2005, Paul Hick (UCSD/CASS)
#	DEC-2005, Paul Hick (UCSD/CASS)
#		Modified to skip empty lines and comments (beginning
#		with # character).
#	MAY-2007, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
#		When looking for a "busy" orbit allow also "done" and "make"
#		to pass. If this happens the orbit is set to "make" and hence
#		will be redone.
#-
	t   numberi    t   contentst   set_busyt   0R(   i   s   orbit %s not present, %st   donei   i   s0   orbit %s already done by same/higher version, %ss   orbit %s already done, %st   #t   makei   s   no orbit left in [%s,%s], %st   busyt   skips   busy %d %s %ss   %Y_%j_%H%M%St   set_makei   s*   orbit %s marked "%s" instead of "busy", %ss   pass s   make %dt   set_dones   busy 0s   done t    t   set_skips   skip s   invalid task, R+   (   R   R   t   Falset   Truet   popR-   R   R)   R   t   floatt   isspacet   findR   t   indext   maxR   R   R   (   t   filepatht   checkvert   prog_versiont	   overwritet
   ignore_catt   taskR*   R    R!   t   statust   hidepathR%   R+   t   countR,   t   post   nt   statet   max_tryt   orbit_ignore(    (    s   skyd_orbit.pyt   skyd_catQ   sô    5
 


  
 **		<, #
(& 
###

0& #4

c   /      C   s©  d } t  i d } |  i d ƒ } t | d d ƒ } t | d d ƒ } t | d d	 ƒ } t | d
 d	 ƒ } t | d d	 ƒ } t | d d	 ƒ }	 t | d d	 ƒ }
 t | d d	 ƒ } t | d d	 ƒ } t | d d ƒ } t | d d ƒ } t | d d ƒ } t | d d ƒ } t | d d ƒ } t | d d ƒ } t | d d ƒ } t | d | ƒ } t | d d	 ƒ } t | d d	 ƒ } t | d d	 ƒ } t | ƒ } t | ƒ } t | ƒ } t | ƒ } t | ƒ } t |	 ƒ }	 t |
 ƒ }
 t | ƒ } t | ƒ } t | ƒ } t | ƒ } t | ƒ } t | ƒ } t t ƒ d j o t |  d d t ƒ Sn | d j  p | d  j o t |  d d! | ƒ Sn | d j oI | d j o
 d" } n | d" j o
 d" } n | d  j o
 d } qÙn | d	 j  p | d" j o t |  d d# | ƒ Sn | d j o2 | d  j o% | d$ j o t |  d d% | ƒ Sn | d& j o
 d } n d' | j } | o | i d' ƒ d	 } n t  i	 i
 | ƒ } | o2 t  i	 i | ƒ o t |  d d( t | ƒ ƒ Sn | d j o t |  d d) t | ƒ ƒ Sn t  i	 i
 | ƒ } t  i	 i | ƒ p t |  d d* t | ƒ ƒ Sn t t d+ d	 ƒ i ƒ  } t | ƒ d	 j o t |  d d, t d- ƒ Sn | d	 } | d j oË t | | ƒ } t |  d	 d ƒ }  xÎ | D]t } t | | | | | d. | | d	 | d |  ƒ
 }  |  d/ d	 j o t | d0 d1 |  d2 ƒ Pn t |  d	 d ƒ }  qÌWt | | | | | d. | | | |  ƒ
 }  n( t | | | | | d. | | | |  ƒ
 }  |  d/ d	 j o |  Sn |  i d2 ƒ } | i ƒ  } y4 t | d	 ƒ } t | d ƒ } | d t j } Wn t } n Xt d3 | d	 g }  | o |  i d4 | d d5 g ƒ n |  i d6 | d7 | d8 | g ƒ |  i d9 | | g | ƒ |  i d: ƒ | o |  i d; ƒ n | o |  i d< ƒ n | o |  i d= ƒ n |	 o |  i d> ƒ n |
 o |  i d? ƒ n | d	 j o, | d	 j o |  i d@ | dA | g ƒ n | d j  o |  i dB | ƒ n | d j o |  i dC | ƒ n t | d0 dD dE dF i |  ƒ ƒ dG | }! d dH | g | d	 j }" |! dI d g | d j |" }# t  i	 i | | g | |! dJ | d" dK ƒ }$ t }% y t  i t  i t |  ƒ }& |& d j o- t  i	 i |$ ƒ o d	 }& t }% q­d" }& n; |& d  j o' t  i	 i |$ ƒ o
 d	 }& q­d }& n d" }& Wn d  }& n X|% oQ | oJ t dL |$ dF | dM |$ t ƒ t  i	 i | t  i	 i |$ ƒ d ƒ }$ n |% od| o]t  i dN }' | o | d' |' }' n | t  i	 i |' dO |# ƒ j oD t  i	 i |' dP |# ƒ }( dQ |( dR t  i	 i |' dS |# ƒ dT }) n | }( dU |( dU }) d' |$ j o  d' i |$ i d' ƒ d ƒ }$ n dV |$ dW dX dY |) }  | o |  dZ 7}  n |  GHy* t  i t  i d[ d[ d\ d] |  d^ ƒ }* Wn t | d0 d_ d` ƒ n X| p| t  i	 i |' dO |# ƒ j o$ dU t  i	 i |' da |# ƒ dU }+ n dU | dU }+ t  i	 i |( |! db | d" dK ƒ }, d' |, j o  d' i |, i d' ƒ d ƒ }, n dc |, dW dd dY |+ }  |  GHy* t  i t  i d[ d[ d\ d] |  d^ ƒ }* Wq}t | d0 d_ de ƒ q}Xqn df dg dh dh g |& }- t | | | | | di |- | d	 | | |  ƒ
 }  |  d/ d	 j oÎ |  d2 i ƒ  }. dj |. d	 dk |. d  dl }. |& d j o |. dm t |$ ƒ 7}. n? |& d" j o |. dn t do 7}. n |& d  j o |. dp 7}. n |  dq d j o |. dn |  dq 7}. n t |  d	 |. ƒ }  n |  S(r   s
  
#+
# NAME:
#	skyd_orbit
# PURPOSE:
#	Set up call to the indexing program
# CALLING SEQUENCE:
#	status = skyd_orbit(status)
# CALLS:
#	skyd_status, skyd_cat, dict_entry, say
# EXAMPLE:
#	To call skyd_orbit.py directly from command line use a call like this:
#		skyd_orbit.py -orbit=25182 -camera=1 -mode=2 -source=SMEIDB?
#			-avoidsun -avoidmoon
#			-destination=$SMEISKY0/sky/c1 -overwrite -alltheway
#			-catalogue=$SKYD/list/skyd_c1m2.txt -level=3
# MODIFICATION HISTORY:
#	DEC-2005, Paul Hick (UCSD/CASS)
#	DEC-2007, Paul Hick (UCSD/CASS)
#		Fixed bug in main section: added check for label
#		cur_label after skyd_orbit finishes to make sure
#		it still is present in the conf file.
#	SEP-2008, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
#		Added argument sdark=<-1,3,10>
#-
	t
   skyd_orbitt   TUBt   argst   camerai   t   modeiÿÿÿÿt	   keepglarei    t   checkversionRF   t   avoidsunt	   avoidmoont   nped_mint	   ndark_mint   sdarkt   leveli   t	   cur_orbitR1   R    R!   t	   catalogueR(   t   sources   SMEIDC?t   destinationt	   allthewayt	   keepbkgndRG   s   executable not found, i   s#   camera %d is invalid (use 1,2 or 3)i   s!   mode %d is invalid (use 0,1 or 2)i
   s!   sdark %d is invalid (use -1,3,10)s   SMEIDB?t   :s&   destination directory does not exist, s   no user catalogue specified, s   user catalogue does not exist, s    -dumpversions   could not determine s    versionR0   R.   t   It   localR+   s   -start=s   -stop=s   -oneskys
   -camera=%ds   -mode=%ds   -source=s   -destination=s	   -silent=3s
   -keepglares   -checkversions
   -overwrites	   -avoidsuns
   -avoidmoons   -nped_min=%ds   -ndark_min=%ds	   -level=%ds	   -sdark=%dt   cmds   
R9   s   c%ds   m%dt   st   sky_s   .fts.gzs   scp s   ; rm -v t   SMEISKY0t   skyt   equs   [\"s   \",\"t   pnts   \"]s   \"s   smei_star_remove,\"s   \",s3   /cleanedge,/use_weights,/auto_wing,/force,silent=2,s   destination=s   ,/keepbkgndt   bashs   -cs!   IDL_DEV=Z; export IDL_DEV; echo "s   " | idl -quiett   idls!   exception during star subtractiont   eclt   equ_s   smei_zld_remove,\"s   /force,/silent,s-   exception during zodiacal light model removalR2   R6   R4   t   set_s   orbit s	    marked "t   "s   , no s   , s    errors   , exceptiont   message(   t   ost   environR=   R	   R   R
   t   progR   R)   t   patht
   expandvarst   isdirR   t   isfileR   R   R'   RQ   R   t   orbit2R;   t   extendt   appendt   joint   spawnvpt   P_WAITR<   t   spawnlp(/   RI   t   csayt   tubRT   RU   RV   RW   RD   RF   RY   RZ   R[   R\   R]   R^   R+   R    R!   R`   Ra   t   destRc   Rd   RG   t   make_locallyt   hostRE   R"   t   orbt   orbit0t   orbit1t   oneskyRh   t   camstrt   modestrt   dirstrt   sky_filet   new_skymap_madet   rtnt   sky0t   star_dirt	   star_destt   errt   zld_destt	   star_fileRN   R,   (    (    s   skyd_orbit.pyRR   j  sf   '

 /+'
" 
/





!), *$% */t   __main__R.   Ru   R(   s   -pid=s   -boss=s   -report=i   t   runsR   t   ws   %s pid is %d
RR   Rf   s   %s, "%s" pid %d -> "%s" pid %di   s   ssh s    "kill -%d %d"s   -cffile=t   conft   group_t	   cur_groups   group_%dR2   R1   s   all orbits processedRT   g      N@s   time lapseds   is %.3f minutess   no orbit left ins   %dt   1s   marked "make"R9   t   kills   
RU   s   -camera=RV   s   -mode=RW   s
   -keepglareRF   s
   -overwriteRX   s   -checkversionRY   s	   -avoidsunRZ   s
   -avoidmoonRG   s   -ignore_catR[   s
   -nped_min=R\   s   -ndark_min=R]   s   -sdark=R^   s   -level=R_   s   -orbit=R    s   -min_orbit=R!   s   -max_orbit=R`   s   -catalogue=Ra   s   -source=Rb   s   -destination=Rc   s
   -allthewayRd   s
   -keepbkgnd(?   t   sysRv   R   R    R   t	   skyd_funcR   R   R   R   R   t   tinyR   R	   R
   R   R   R   R   t   signalR   Rw   R)   R   Rx   R'   R-   RQ   RR   t   __name__R   RI   t   argvt   boss_pidR   t   bosst   reportt   getpidt	   grunt_pidRy   t   wmarkt   resultt   opent   writeR    t   popent   readt   cffileR;   R=   Rœ   t	   max_groupt   keyst   keyR@   Rž   t	   fnd_groupt	   cur_labelR   t   lapsed_timeR2   R<   t   exit(    (    (    s   skyd_orbit.pys   <module>   sÚ   (:	8		ÿ 	ÿ W(&
' 
	

&
&&
'"