
`9Sc           @   s   d  d l  Z  d  d l Z  d  d l Z d  d l Z d  d l Td  d l Td  d l m Z d  d l m Z d  d l	 Td  d l
 Td g Z d e f d     YZ d S(   iN(   t   *(   t   gui(   t   txtt   AerlidToolDialogc           B   s   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z RS(   s  Dialog used to process Dubovik AERONET data files using AERLID tool.

    Processing starts when the dialog's 'exec_' method is called. During the
    run of the program, its output is printed out. If processing completes
    successfully, the dialog would close automatically, and 'getErrorMessage'
    method would return 'None'. Otherwise, the user would have to close the
    dialog manually, and 'getErrorMessage' would return an appropriate error
    message.c   	      C   sB  t  j |  |  t d   | D  s, t  t |  d k sD t  | |  _ | |  _ | |  _ | |  _ t	   |  _
 |  j
 j t	 j  t   } | j d d  |  j
 j |  |  j
 j j |  j  |  j
 j j |  j  |  j
 j j |  j  d	 |  _ t   } t   |  _ | j |  j  t   |  _ | j |  j  t   |  _  | j |  j   t! d  |  _" |  j" j# t$  |  j" j% t$  |  j" j& j |  j'  t(   } | j)   | j |  j"  | j* |  |  j+ |  |  j, d  |  j- t. j/ d   |  j0 |  j1   t2 j3 @ |  j4 j |  j5  t6 j7 d |  j8  d	 S(
   sh  Parameters:
          - 'packageDir': path to the root directory of the AERLID package. The
            program itself has to reside in the 'run' subdirectory.
          - 'outputWavelengths': a list of wavelengths, in nanometers, to
            generate data for (currently, there should be exactly 3 elements in
            the list).
          - 'dubovikFilePath': path to the 'Combined Dubovik Retrievals'
            AERONET data file to be used as input (these files may be
            downloaded from 'http://aeronet.gsfc.nasa.gov').
          - 'aerlidFilePath': path to the file to store AERLID's output in.c         s   s+   |  ]! } d  | k o  d k n Vq d S(   g      i@g     @@N(    (   t   .0t   wl(    (    s   code\AerlidToolDialog.pys	   <genexpr>?   s    i   t    GFORTRAN_UNBUFFERED_PRECONNECTEDt   yt   Closes   Processing AERONET data...s   institute-of-physicsi    N(9   t   QDialogt   __init__t   anyt   AssertionErrort   lent
   packageDirt   outputWavelengthst   dubovikFilePatht   aerlidFilePatht   QProcesst   aerlidProcesst   setProcessChannelModet   MergedChannelst   QProcessEnvironmentt   insertt   setProcessEnvironmentt	   readyReadt   connectt   onOutputAvailablet   errort   onProcessErrort   finishedt   onProcessFinishedt   Nonet   errorMessaget   QVBoxLayoutt   StatusLabelWidgett   statusLabelt	   addWidgett   ConsoleWidgett   outputWindowt   QProgressBart   progressBart   QPushButtont   closeButtont   setAutoDefaultt   Falset
   setVisiblet   clickedt   rejectt   QHBoxLayoutt
   addStretcht	   addLayoutt	   setLayoutt   setWindowTitlet   setWindowIconR   t   loadIcont   setWindowFlagst   windowFlagst   Qtt   WindowContextHelpButtonHintt   rejectedt
   onRejectedt   QTimert
   singleShott   onInitComplete(	   t   selft   parentR   R   R   R   t   processEnvironmentt   layoutt   buttonBoxLayout(    (    s   code\AerlidToolDialog.pyR
   .   sL    									
c         C   s   |  j  S(   s   Return a rich text message describing the failed processing of the
        data or 'None' if the processing has completed successfully.(   R!   (   RA   (    (    s   code\AerlidToolDialog.pyt   getErrorMessage   s    c         C   s^   | |  _  |  j j |  |  j j t  |  j j   |  j j   |  j	 |  j
   d  d  S(   Ns	    (Failed)(   R!   R$   t   setErrorR+   R.   t   Truet   setFocusR'   t   ensureCursorVisibleR5   t   windowTitle(   RA   t   message(    (    s   code\AerlidToolDialog.pyt   setErrorMessage   s    	c         C   s   t  j j |  j d d  S(   Nt   inputs	   input.txt(   t   ost   patht   joinR   (   RA   (    (    s   code\AerlidToolDialog.pyt   getInputFilePath   s    c         C   s   t  j j |  j d d  S(   Nt   outputs
   output.txt(   RO   RP   RQ   R   (   RA   (    (    s   code\AerlidToolDialog.pyt   getOutputFilePath   s    c         C   s   t  j j |  j d d  S(   Nt   runs   aerlid_input.txt(   RO   RP   RQ   R   (   RA   (    (    s   code\AerlidToolDialog.pyt   getParamsFilePath   s    c         C   s   t  j j |  j d d  S(   NRU   s
   aerlid.exe(   RO   RP   RQ   R   (   RA   (    (    s   code\AerlidToolDialog.pyt   getProgramFilePath   s    c         C   s  |  j    } y t |  j  } Wn7 t k
 rX |  j t j d t j |  j    n Xy t | d  } Wn7 t k
 r | j   t j d t j |    n Xd } z yP xI | D]A } | j	 d d  } | j	 d d  } | j
 |  | d	 7} q WWn- t k
 r.t j d
 t j |    n XWd | j   | j   X| d 8} | d k  rt j d t j |  j    n  | S(   s   Create intermediate input data file for the AERLID program and store
        it at 'self.getInputFilePath()'.

        Return the number of measurements stored in the input file, or raise
        'txt.Error' if the file couldn't be created.s2   AERONET data file %s may not be opened for readingt   ws6   Failed to open AERLID input data file (%s) for writingi    s   N/As   -999t   /t   -i   s+   Failed to write AERLID input data file (%s)Ni   s#   %s is not a valid AERONET data file(   RR   t   openR   t   IOErrorRM   R   t   Errort	   quotePatht   closet   replacet   write(   RA   t   inputFilePatht   dubovikFilet	   inputFilet   recordCountt   line(    (    s   code\AerlidToolDialog.pyt   prepareInputFile   s@    
 	

	c         C   s  |  j    } y t | d  } Wn- t k
 rN t j d t j |    n Xz7y| j d  | j d d d  | j d  | j d | d	  | j d
  d } x) |  j D] } | t | d  d 7} q W| j d | d  t	 j
 j |  j    } t	 j
 j |  j    } | j d  | j d | d  | j d | d  Wn- t k
 rt j d t j |    n XWd | j   Xd S(   sf   Create parameters data file for the AERLID program and store it at
        'self.getParamsFilePath()'.RX   s;   Failed to open AERLID parameters data file (%s) for writings   LOGICAL FLAGS ---
s   %-25ss   f t f fs   laprn, llput, ltest, l_f33_f44
s   -----------------
s   %-25ds4   nmeas (number of lines for reading of Aeronet data)
s   0.440 0.675 0.870 1.020  WAVEA
t    g     @@t    s   WAVEL
s   FILENAMES -------
s   input_filename
s   out_filename
s0   Failed to write AERLID parameters data file (%s)N(   RV   R[   R\   R   R]   R^   Ra   R   t   strRO   RP   t   basenameRR   RT   R_   (   RA   Re   t   paramsFilePatht
   paramsFilet   wavelengthStrR   t   inputFileNamet   outputFileName(    (    s   code\AerlidToolDialog.pyt   prepareParamsFile   s<     c         C   s   t  j j |  j  } y& t  j j |  s: t  j |  n  Wn0 t k
 rm t j d t j	 |  j    n Xy t
 j |  j   |  j  Wn0 t k
 r t j d t j	 |  j    n Xd S(   sn   Copy output file generated by AERLID program to its final location
        specified by 'self.aerlidFilePath'.u;   Couldn’t create directory for the AERLID output file (%s)u=   Couldn’t copy AERLID output file to its final location (%s)N(   RO   RP   t   dirnameR   t   isdirt   makedirst   OSErrorR   R]   R^   t   shutilt   copyfileRT   R\   (   RA   t   fileDir(    (    s   code\AerlidToolDialog.pyt   copyOutputFile  s    c         C   s   y |  j    } |  j |  Wn' t j k
 rF } |  j | j  d SX|  j j d t j |  j	   |  j
 j d |  |  j
 j d  |  j j |  j  |  j j |  j    d S(   s0   Prepare input data and start the AERLID program.Ns*   Running AERLID tool for AERONET file %s...i    (   Rg   Rq   R   R]   RM   t   textR$   t   setProgressR^   R   R)   t   setRanget   setValueR   t   setWorkingDirectoryR   t   startRW   (   RA   Re   t   e(    (    s   code\AerlidToolDialog.pyR@   -  s    	c         C   s   xx |  j  j   rz t |  j  j    } |  j j |  t j d |  } | d k	 r |  j	 j
 t | j d    q q Wd S(   s3   Print AERLID program's output to the output window.s   imeas=\s+(\d+)\s+i   N(   R   t   canReadLinet   QStringt   readLineR'   t   appendOutputt   ret   matchR    R)   R}   t   intt   group(   RA   t
   outputLinet   matchObj(    (    s   code\AerlidToolDialog.pyR   E  s    c         C   s$   |  j  d t j |  j     d  S(   Ns   Failed to run AERLID tool (%s)(   RM   R   R^   RW   (   RA   (    (    s   code\AerlidToolDialog.pyR   U  s    	c         C   s   |  j  d  k	 r d  S|  j j   } | d k rC |  j d |  d  Sy |  j   Wn' t j k
 rz } |  j | j  d  SX|  j	   d  S(   Ni    s+   Failed to run AERLID tool (return code: %d)(
   R!   R    R   t   exitCodeRM   Ry   R   R]   Rz   t   accept(   RA   t
   returnCodeR   (    (    s   code\AerlidToolDialog.pyR   Y  s    c         C   s0   |  j  d k r |  j d  n  |  j j   d S(   s-   Terminate the process, if it's still running.s&   Failed to run AERLID tool (user abort)N(   R!   R    RM   R   t   kill(   RA   (    (    s   code\AerlidToolDialog.pyR=   s  s    (   t   __name__t
   __module__t   __doc__R
   RF   RM   RR   RT   RV   RW   Rg   Rq   Ry   R@   R   R   R   R=   (    (    (    s   code\AerlidToolDialog.pyR   #   s    	X							8	2					(   RO   t   os.pathR   Rv   t   PyQt4.QtCoret   PyQt4.QtGuit   common.utilsR   R   t   common.ConsoleWidgett   common.StatusLabelWidgett   __all__R	   R   (    (    (    s   code\AerlidToolDialog.pyt   <module>   s   



	