
`9Sc           @   sa   d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l Td g Z d e f d     YZ d S(   iN(   t   txt(   t   *t
   LidarInputc           B   s9  e  Z d  Z d Z e d d d  e d d d  e d d	 d  e d
 d d  e d d d  e d d d  e d d d  e d d d  e d d d d e e d d d  e d d d  e d d d  e d  d! d  e d" d# d  e d$ d% d  e d& d' d(  e d) d* d  e d+ d, d  e d- d. d  e d/ d0 d  e d1 d2 d3 e j  e d4 d5 d3 e j d e e d6 d7 d3 e j d e e d8 d9 d3 e j d e e d: d; d3 e j d e g Z d< Z	 d= Z
 d> Z d?   Z d@   Z dA   Z dB   Z dC   Z dD   Z dE   Z dF   Z dG   Z dH   Z RS(I   s  Input data from a single unprocessed lidar measurement.

    Attributes:
      - 'localId': a text that presumedly uniquely identifies the measurement.
      - 'latitude', 'longitude', 'altitude': geodetic coordinates of the
        measurement site, in degrees and meters above sea level.
      - 'startDate', 'stopDate', 'startTime', 'stopTime': time boundaries for
        the series of lidar shots that constitute the measurement.

      - 'regNumber': registration number of the lidar channel, denoting its
        wavelength, polarization, aperture etc. in a uniform way.
      - 'wavelength': nominal lidar signal wavelength in nanometers.
      - 'polarization': '0' for unpolarized signal, '1' for Raman signal, '2'
        and '3' for cross and parallel recievers for a linearly polarized
        signal.

      - 'gridStep': (uniform) step of the data grid, in meters.
      - 'gridZenithAngle': zenith angle of the data grid, in degrees. Real
        height step is 'gridStep * cos(gridZenithAngle)'. Index zero of the
        grid corresponds to an along-track segment that has 'gridStep' length
        and starts at the measurement point. If 'originalZenithAgnles' array
        is 'None', this will correspond to the actual zenith angle of the lidar
        measurement; otherwise 'originalZenithAngles' should be used for that.
      - 'firstDataIndex', 'lastDataIndex': first and last indices of the data
        grid for which data is present in the arrays. Add 'firstDataIndex' to
        physical indices of the data arrays to obtain indices of the data grid.

      - 'accumulation': number of lidar shots combined in this measurement.
      - 'background': background signal or 0.0 if this signal is a joined one.
      - 'nonlinearity': estimation of error coefficient describing nonlinearity
        of the receiving channel.
      - 'syncNoise': estimation of error coefficient describing the receiver's
        synchronous noise.
      - 'nonSyncNoise': estimation of error coefficient describing the
        receiver's nonsynchronous noise.

      - 'signal': Numpy array holding the raw lidar signal profile, including
        the background (unless this signal is a joined one, in which case
        background signal would had been subtracted during a join operation,
        whereas portions of the signal itself may had been multiplied by some
        appropriate factors in order to make the final profile smooth).

      - 'originalZenithAngles': Numpy array holding zenith angles of the lidar
        measurements, in degrees, at the given height, before a signal joining
        operation, or 'None' if this signal is not a joined one.
      - 'originalSignal': Numpy array holding raw lidar signal values before a
        signal joining operation, or 'None' if this signal is not a joined one.
      - 'originalAccumulations': Numpy array holding 'accumulaion's of the
        original lidar signals, or 'None' if this signal is not a joined one.
      - 'originalBackgrounds': Numpy array holding 'background's of the
        original lidar signals, or 'None' if this signal is not a joined
        one.

    Attributes that are calculated manually in 'onDataLoaded':
      - 'gridHeightStep': height step of the data grid (see 'gridStep' and
        'gridZenithAngle').t   Specifict   localIdt   IDLocalt   VARCHARt   latitudet   Latitudet   SINGLEt	   longitudet	   Longitudet   altitudet   Altitudet	   startDatet	   StartDatet   DATEt   stopDatet   StopDatet	   startTimet	   StartTimet   TIMEt   stopTimet   StopTimet	   regNumbers
   Reg Numbert   requiredt
   wavelengtht
   Wavelengtht   polarizationt   Polarizationt   SMALLINTt   gridStept   Stept   gridZenithAnglet   Zenitht   firstDataIndext   N1t   lastDataIndext   N2t   accumulationt   Accumulationt   INTt
   backgroundt
   Backgroundt   nonlinearityt	   Nonlineart	   syncNoiset   Synchront   nonSyncNoiset   NonSynchront   signalt   OLEDatat
   LONGBINARYt   originalZenithAnglest   ZenithArrayt   originalSignalt   OriginalSignalArrayt   originalAccumulationst   AccumulationArrayt   originalBackgroundst   BackgroundArraysP   Lidar input file is not a valid Access database or may not be opened for readings8   Lidar input: failed to query data from the database files$   Lidar input file format is invalid: c         C   s?   |  j  d  k s |  j d  k r" d  St j j |  j  |  j  Sd  S(   N(   R   t   NoneR   t   datetimet   combine(   t   self(    (    s   code\LidarInput.pyt   getStartDateTime   s    c         C   s?   |  j  d  k s |  j d  k r" d  St j j |  j  |  j  Sd  S(   N(   R   R=   R   R>   R?   (   R@   (    (    s   code\LidarInput.pyt   getStopDateTime   s    c         C   s   |  j  |  j |  j  S(   s@   Return actual zenith angles for the original lidar measurements.(   t   getOriginalProfileR5   R!   (   R@   (    (    s   code\LidarInput.pyt   getZenithAngles   s    c         C   s   |  j  |  j |  j  S(   s@   Return actual accumulations for the original lidar measurements.(   RC   R9   R'   (   R@   (    (    s   code\LidarInput.pyt   getAccumulations   s    c         C   s   |  j  |  j |  j  S(   s>   Return actual backgrounds for the original lidar measurements.(   RC   R;   R*   (   R@   (    (    s   code\LidarInput.pyt   getBackgrounds   s    c         C   s!   |  j  d k	 r |  j  S|  j Sd S(   s=   Return raw signal values for the original lidar measurements.N(   R7   R=   R2   (   R@   (    (    s   code\LidarInput.pyt   getRawSignal   s    c      	   C   s   |  j  d k r" |  j d k r" d S|  j  d k rD |  j d k rD d S|  j  d k rf |  j d k rf d	 S|  j  d
 k r |  j d k r d S|  j  d k r |  j d k r d S|  j  d k r |  j d k r d S|  j  d
 k r |  j d k r d Sd S(   s   Return the lidar channel's textual identifier for a retrieval
        algorithm or 'None' if this lidar data may not be used with any of the
        retrieval algorithms.g     0v@i    i   t   355i   t   355Cg     0x@i   t   387Rg     @t   532g     @t   607Rg     @t   1064t   532CN(   i    i   (   i    i   (   i    i   (   R   R   (   R@   (    (    s   code\LidarInput.pyt   getChannelId   s    c         C   s  xC |  j    D]5 } t |  |  d( k r d t j |  j |   Sq W|  j   |  j   k r d t j |  j d   t j |  j d   t j |  j d   t j |  j d   f Sd |  j k o d k n s d	 t j |  j d
   Sd |  j	 k od k n s,d	 t j |  j d   S|  j
 d k rUd t j |  j d   S|  j d) k r~d t j |  j d   S|  j d k rd t j |  j d   Sd |  j k od k  n sd	 t j |  j d   Sd |  j k o|  j k  n s4d t j |  j d   t j |  j d   f S|  j d k r]d t j |  j d   Sx= d* D]5 } t |  |  d k  rdd  t j |  j |   SqdW|  j |  j d } x d+ D] } t |  |  } | d( k	 rt |  | k rt |  | j } | | j } d& t j |  j |   t j |  t j |  f SqW|  j d( k	 rt j |  j d k   st j |  j d k  rd' t j |  j d"   Sn  d( S(,   s}   Return an error message describing the malformed data or 'None' if
        the attribute values are plausible and consistent.s"   %s database field contains no datas6   %s/%s and %s/%s database field values are inconsistentR   R   R   R   g     Vg     V@s'   %s database field value is out of rangeR   g     fg     f@R
   g        s5   %s database field value is less than or equal to zeroR   i    i   i   i   s+   %s database field has an inadmissible valueR   R   R!   s0   %s and %s database field values are inconsistentR#   R%   R'   R,   R.   R0   s)   %s database field value is less than zeroR2   R5   R7   R9   R;   s7   size of the %s database field is %s bytes instead of %ss0   one of the items of the %s array is out of rangeN(   i    i   i   i   (   R,   R.   R0   (   s   signals   originalZenithAngless   originalSignals   originalAccumulationss   originalBackgrounds(   t   getRequiredAttributeNamest   getattrR=   R    t   quotet   getFieldNameRA   RB   R   R
   R   R   R   R!   R#   R%   R'   t   lent   itemsizet   quoteNumberR5   t   numpyt   any(   R@   t   namet   dataArrayLengtht
   arrayValuet
   valueBytest   requiredBytes(    (    s   code\LidarInput.pyt   getErrorMessage   sn    "
c         C   sp   |  j  d  k s |  j d  k r* d  |  _ n' |  j  t j |  j t j d  |  _ |  j d  k rl d |  _ n  d  S(   Ng     f@t    (	   R   R=   R!   t   gridHeigthStept   matht   cost   pit   gridHeightStepR   (   R@   (    (    s   code\LidarInput.pyt   onDataLoaded  s    !c         C   sf   | d k	 r | S| d k s: |  j d k s: |  j d k r> d S|  j |  j d } t j | g |  S(   s\  Return a Numpy array holding original signal data regardless of
        whether 'self' is a raw lidar signal or a joined one. 'originalProfile'
        has to be the array used to store the data in joined signals, and
        'scalarValue' has to be the value of the scalar database field used to
        store the same data in normal measurements.i   N(   R=   R#   R%   RW   t   array(   R@   t   originalProfilet   scalarValueRZ   (    (    s   code\LidarInput.pyRC   )  s    (   t   __name__t
   __module__t   __doc__t	   tableNamet	   DataFieldt   FalseRW   t   float32t   fieldst   openDatabaseErrorMessaget   queryFailureErrorMessaget   invalidFormatErrorPrefixRA   RB   RD   RE   RF   RG   RO   R^   Re   RC   (    (    (    s   code\LidarInput.pyR      sX   8								W	(	   R>   Ra   RW   t   common.utilsR    t   common.DataRecordt   __all__t
   DataRecordR   (    (    (    s   code\LidarInput.pyt   <module>   s   
	