
`9Sc           @   s_  d  d l  Td  d l Td  d l m Z d  d l m Z d  d l m Z d  d l Td  d l Td  d l Td  d l	 Td  d l
 Td  d l Td  d l Td  d l Td  d l Td  d l Td  d l Td  d l Td  d l Ty d  d l TWn e k
 r e Z n Xe Z e rd  d l Tn  d g Z d e f d     YZ d   Z d e f d	     YZ d
 e f d     YZ d S(   i(   t   *(   t   gui(   t   txt(   t   utilst   ManualRetrieverWidgetc           B   s   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d   Z d	   Z e rr d
   Z n  d   Z d   Z RS(   sQ   Main window of the manual version of aerosol profile retrieval algorithm
    GUI.c   	      C   s~  t  j |  |  |  j t j  d  |  _ t j   |  _	 t
 rP t j   |  _ n  t   } t   |  _ |  j j j |  j  t   |  _ |  j j t d d  t d d  t d d  g  |  j j t d d  t d d  t d d  t d d  g  |  j j t d d  t d d  t d d  t d d  g  |  j j j |  j  t   } | j |  j  | j |  j d  | j d d d d  t    } | j |  t   |  _ |  j j j |  j  t   |  _  t   } | j |  j  | j |  j   | j d d d d  t    } | j |  t! t j"  |  _# |  j# j$ t%  |  j# j |  |  j# j |  | j |  j#  t&   |  _' | j |  j'  t(   |  _) | j |  j)  t* d  |  _+ |  j+ j, t- j. d	   |  j+ j/ j |  j0  t
 r"t* d
  |  _1 |  j1 j/ j |  j2  n  t* d  |  _3 |  j3 j/ j |  j4  t* d  |  _5 |  j5 j/ j |  j6  t7   } | j |  j+  t
 r| j |  j1  n  | j8   | j |  j3  | j |  j5  | j9 |  |  j |  |  j: d  |  j; t- j. d   xU |  j |  j |  j' |  j |  j  f D]/ } | j< j |  j=  | j> j |  j) j?  q*W|  j@   |  j jA   jB   d  S(   Ng     0v@i    i   g     @g     @i   i   s   &Parameterst
   parameterss   Per&turbance optionss	   &Retrieves   &View outputs    Manual aerosol profile retrievers   institute-of-physics(   i    i   (   i    i   (   i    i   (   i    i   (   i    i   (   i    i   (   i    i   (   i    i   (   i    i   (C   t   QWidgett   __init__t   setAttributet   Qtt   WA_DeleteOnCloset   Nonet   outputViewerWidgett   ManualParamst   loadFromFilet   algorithmParamst#   liricPerturbanceProcessPluginLoadedt   LiricPerturbanceParamst   perturbanceParamst   QVBoxLayoutt   LidarDatabaseWidgett   lidarDatabaseWidgett   dataListChangedt   connectt   onLidarDataListChangedt   LidarTableWidgett   lidarTableWidgett   addAllowedChannelSequencet   LidarChannelInfot   selectionChangedt   onLidarSelectionChangedt	   addWidgett   setContentsMarginst	   setLayoutt   PhotometerDatabaseWidgett   photometerDatabaseWidgett   onPhotometerDataListChangedt   PhotometerTableWidgett   photometerTableWidgett	   QSplittert   Verticalt   splittert   setChildrenCollapsiblet   Falset   RetrievalSelectionWidgett   retrievalSelectionWidgett   StatusLabelWidgett   statusLabelt   QPushButtont   paramsButtont   setIconR   t   loadIcont   clickedt   onParamsButtonClickedt   perturbanceButtont   onPerturbanceButtonClickedt   retrieveButtont   onRetrieveButtonClickedt   viewOutputButtont   onViewOutputButtonClickedt   QHBoxLayoutt
   addStretcht	   addLayoutt   setWindowTitlet   setWindowIcont   statusMessageChangedt   onStatusMessageChangedt   repaintRequestedt   repaintParentWindowt   loadSettingst   getTableWidgett   setFocus(	   t   selft   parentt   layoutt   lidarLayoutt   lidarWidgett   photometerLayoutt   photometerWidgett   buttonBoxLayoutt   widget(    (    s   code\ManualRetrieverWidget.pyR   :   s    		
			
		
			
	
c         C   s   |  j    | j   d  S(   N(   t   saveSettingst   accept(   RH   t   event(    (    s   code\ManualRetrieverWidget.pyt
   closeEvent   s    
c         C   s   t  d t  j  } t j |  |  | j d |  j j    |  j j |  |  j	 j |  |  j
 j |  |  j j |  d  S(   Ns,   settings/interface/ManualRetrieverWidget.init   splitterState(   t	   QSettingst	   IniFormatR   t   saveWindowSettingst   setValueR)   t	   saveStateR   RQ   R   R#   R-   (   RH   t   settings(    (    s   code\ManualRetrieverWidget.pyRQ      s    c         C   s   t  d t  j  } t j |  |  | j d  rS |  j j | j d  j    n  |  j	 j
 |  |  j j
 |  |  j j
 |  |  j j
 |  d  S(   Ns,   settings/interface/ManualRetrieverWidget.iniRU   (   RV   RW   R   t   loadWindowSettingst   containsR)   t   restoreStatet   valuet   toByteArrayR   RE   R   R#   R-   (   RH   R[   (    (    s   code\ManualRetrieverWidget.pyRE      s    	c         C   sy  |  j  j   } |  j j |  t |  d k r5 d St |  \ } } } |  j j | |  } | d k ro d S| |  j j	   k r | |  j j
   k r d S|  j j   } t |   } | j d  | j d t j |  t j |  f  | j d t j |  t j |  t j |  f  | j t j  | j t j t j B | j   t j k ru|  j j | |  n  d S(   s   Populate lidar table with contents of the newly loaded lidar
        database. After that, find the most relevant photometer data subset for
        the loaded lidar database and load it if the user agrees to do that.i    Ns   Selecting photometer datasG   The most relevant Aeronet site for this lidar database is %s (year %s).sK   Would you like to load photometer data file for %s, year %s, data level %s?(   R   t   getDataListR   t   connectDataListt   lent   getLidarLatLongAndYearR#   t   getNearestSiteNameR   t   getSiteNamet   getYeart   getDataLevelt   QMessageBoxR?   t   setTextR   t   quotet   quoteNumbert   setInformativeTextR2   t   Questiont   setStandardButtonst   Yest   Not   exec_t   setSiteNameAndYear(   RH   t   lidarDataListt   latitudet	   longitudet   yeart   siteNamet	   dataLevelt   msgBox(    (    s   code\ManualRetrieverWidget.pyR      s2    	 ,c         C   ss   |  j  j   } t |  d k r0 d } d } n, t d   | D  } t d   | D  } |  j j | |  d S(   s   Update synchronization icons in the photometer table widget
        according to the selected lidar data and update scrolling of the table
        so that the best-matching photometer measurement is displayed at its
        center.i    c         s   s   |  ] } | j    Vq d  S(   N(   t   getStartDateTime(   t   .0t
   lidarInput(    (    s   code\ManualRetrieverWidget.pys	   <genexpr>"  s   c         s   s   |  ] } | j    Vq d  S(   N(   t   getStopDateTime(   R|   R}   (    (    s   code\ManualRetrieverWidget.pys	   <genexpr>$  s   N(   R   t   getSelectedDataRc   R   t   mint   maxR&   t   updateSynchronizationHints(   RH   t   selectedLidarDatat   startDateTimet   stopDateTime(    (    s   code\ManualRetrieverWidget.pyR     s    				c         C   s'   |  j  j |  j j    |  j   d S(   s@   Populate photometer table with the newly loaded photometer data.N(   R&   Rb   R#   Ra   R   (   RH   (    (    s   code\ManualRetrieverWidget.pyR$   *  s    	c         C   s  |  j  j |  j j   d k  |  j |  j |  j |  j |  j f } g  | D] } | j   ^ qJ } t	 d   | D  r |  j
 j |  |  j j t  d S|  j j   } t |  j j | d j  } | t d   | D  k r|  j
 j d  |  j j t  d St |  d k r?d t j d  t j d	  f } nF t |  d
 k sWt  d t j d  t j d  t j d  f } |  j
 j d |  |  j j t  d S(   sd   Update Retrieve button state and show the most relevant error
        message in 'self.statusLabel'.c         s   s   |  ] } | d  k	 Vq d  S(   N(   R   (   R|   t   message(    (    s   code\ManualRetrieverWidget.pys	   <genexpr>E  s    Ni    c         s   s   |  ] } | j  Vq d  S(   N(   t   firstInputIndex(   R|   t   input(    (    s   code\ManualRetrieverWidget.pys	   <genexpr>P  s    sO   Photometer displacement is larger than lower boundary of the lidar measurementsi   s   2 aerosol modes: %s, %st   Finet   Coarsei   s   3 aerosol modes: %s, %s, %ss   Coarse Sphericals   Coarse Spheroids)   The data are ready for the retrieval (%s)(   R:   t
   setEnabledR-   t   getStatusMessageR   R   R#   R   R&   t   anyR/   t   setMostSevereStatusMessageR8   R+   t   getDatat   roundR   t   photometerDisplacementt   gridHeightStepR   t   setErrorRc   R   Rk   t   AssertionErrort   setCompletedt   True(   RH   t
   widgetListRP   t   statusMessagest   lidarInputst   gridDisplacementt   aerosolModes(    (    s   code\ManualRetrieverWidget.pyRB   6  s8    	"	c         C   s*   t  |  |  j  } | j   |  j   d  S(   N(   t   ManualParamsDialogR   Rr   RB   (   RH   t   paramsDialog(    (    s   code\ManualRetrieverWidget.pyR5   g  s    
c         C   s    t  |  |  j  } | j   d  S(   N(   t   LiricPerturbanceParamsDialogR   Rr   (   RH   R   (    (    s   code\ManualRetrieverWidget.pyR7   q  s    c         C   s   |  j  j   |  j j   d k s( t  |  j j   d k sC t  |  j j   } |  j j   } t | | |  j	  } t
 r t | | |  j	 |  j  } n  t |  | |  j j    } | j   d S(   s"   Launch the retrieval modal dialog.N(   R#   t   saveAotCorrFileR   R   R   R   R&   R   t   LiricRetrievalProcessR   R   t   LiricPerturbanceProcessR   t   LiricRetrievalDialogR-   t   getFilePathRr   (   RH   R   t   photometerInputt   retrievalProcesst   progressDialog(    (    s   code\ManualRetrieverWidget.pyR9   w  s    		c         C   s   |  j  d k	 r@ y |  j  j   Wq@ t k
 r< d |  _  q@ Xn  |  j  d k rd t d t  |  _  n  |  j  j |  j j    |  j  j	   |  j  j
   d S(   s   Open the output database in the output viewer window, select the
        last database record and activate the window.

        If there is no output viewer window yet, or if the window has been
        closed, create it.t   skipDatabaseLoadingN(   R   R   t
   objectNamet   RuntimeErrort   OutputViewerWidgetR   t   openLastDatabaseRecordR-   R   t   showt   activateWindow(   RH   (    (    s   code\ManualRetrieverWidget.pyR;     s    	N(   t   __name__t
   __module__t   __doc__R   R   RT   RQ   RE   R   R   R$   RB   R5   R   R7   R9   R;   (    (    (    s   code\ManualRetrieverWidget.pyR   5   s   |				7			1			c         C   s   g  } g  } xq |  D]i } | j  | j | j j f } | | k r] | j |  | j d  q | j |  } | | c d 7<q W| d } | d } xD t d t |   D]- } | | | k r | | } | | } q q W| S(   s  Return a 3-tuple of the most relevant latitude, longitude, and
    measurement year for the given non-empty list of 'PreparedLidarInput'
    instances.

    Normally, geodetic coordinates of a lidar station and measurement year will
    be constant within a single lidar database file, and these constant values
    will be the returned ones. Otherwise, the most frequent combination of
    latitude, longitude and year will be returned.i   i    (   Ru   Rv   t	   startDateRw   t   appendt   indext   rangeRc   (   Rt   t   latLongYearst   latLongYearFrequenciesR   t   latLongYeart   it   bestLatLongYeart   maxFrequency(    (    s   code\ManualRetrieverWidget.pyRd     s     


R,   c           B   sG   e  Z d  Z d d  Z d   Z d   Z d   Z d   Z d   Z	 RS(   s   Widget responsible for selection and initialization of the output
    aerosol profile database in the manual version of aerosol profile retrieval
    algorithm.c         C   s   t  j |  |  t   } t d d d d  |  _ |  j j d d  |  j j j |  j  |  j j	 j |  j
  | j |  j  | j d d d d  |  j |  d  S(   Ns!   Aerosol profile &output database:s"   Microsoft Access databases (*.mdb)s&   Select aerosol profile output databases5   Open an existing aerosol profile output database files5   Select name for a new aerosol profile output databases7   Create a new empty aerosol profile output database filei    (   t   StatusSignalingWidgetR   R   t   FileSelectionWidgett   fileSelectort   showCreateButtont
   fileOpenedR   t   onFileOpenedt   fileCreatedt   onFileCreatedR   R    R!   (   RH   RI   RJ   (    (    s   code\ManualRetrieverWidget.pyR     s    		c         C   s   |  j  j   S(   N(   R   R   (   RH   (    (    s   code\ManualRetrieverWidget.pyR     s    c         C   s0   t  j | d   |  j j | d  Wd  QXd  S(   NR,   t   filePath(   R   t   SettingsGrouperR   RQ   (   RH   R[   (    (    s   code\ManualRetrieverWidget.pyRQ     s    c         C   s0   t  j | d   |  j j | d  Wd  QXd  S(   NR,   R   (   R   R   R   RE   (   RH   R[   (    (    s   code\ManualRetrieverWidget.pyRE     s    c         C   s   |  j  j   } | d k r, |  j d  d Sy! |  j d t  t j |  Wn' t j	 k
 rv } |  j | j
  d SX|  j   d S(   s?   Check format of an existing database file selected by the user.s0   Aerosol profile output database is not specifiedNs+   Checking aerosol profile output database...(   R   R   R   t   setErrorMessaget   setProgressMessageR   t   LiricOutputt   checkDataFormatR   t   Errort   textt   clearStatusMessage(   RH   R   t   e(    (    s   code\ManualRetrieverWidget.pyR     s    
c         C   su   |  j  j   } y! |  j d t  t j |  Wn4 t j k
 rf } |  j | j	  |  j  j
   d SX|  j   d S(   sA   Create a new database file at the file path selected by the user.s+   Creating aerosol profile output database...N(   R   R   R   R   R   t   createDatabaseR   R   R   R   t   invalidateFilePathR   (   RH   R   R   (    (    s   code\ManualRetrieverWidget.pyR     s    
N(
   R   R   R   R   R   R   RQ   RE   R   R   (    (    (    s   code\ManualRetrieverWidget.pyR,     s   				R   c           B   s    e  Z d  Z d   Z d   Z RS(   s6   Graphical user interface for the optimization process.c         C   s   t    S(   N(   t   LiricPlotWidget(   RH   (    (    s   code\ManualRetrieverWidget.pyt   createPlotWidget%  s    c         C   s   d S(   Ns+   settings/interface/LiricRetrievalDialog.ini(    (   RH   (    (    s   code\ManualRetrieverWidget.pyt   getSettingsFilePath(  s    (   R   R   R   R   R   (    (    (    s   code\ManualRetrieverWidget.pyR   !  s   	N(    t   PyQt4.QtCoret   PyQt4.QtGuit   common.utilsR   R   R   t   common.FileSelectionWidgett   common.StatusLabelWidgett   common.StatusSignalingWidgetR   R   R   R   R   R   R"   R%   t   PlotWidgetst   RetrievalProgressDialogR   t   ImportErrorR+   R   R   R   t   __all__R   R   Rd   R   R,   R   (    (    (    s   code\ManualRetrieverWidget.pyt   <module>   s<   















	 x	'N