
&cSc           @   s   d  Z  d d l Td d l Td d l Z d d l Td d l Z d d  Z d d d  Z d d	 d
 d  Z	 d   Z
 d d d  Z d d d d d	 d  Z d S(   sh   
This module contains functions to read and write from ascii files, as well as to/from MySQL databases.
i(   t   *Nc         C   s"  t  j |  d   } | j   } Wd QXg  } d } x | D] } | j   } t |  s^ q: n  g  | j t |   D] } t |  ^ qt } t |  }	 | d k	 r |	 | k r t	 d   n  | j
 |  q: Wt |  } | j \ }
 } |
 d k s| d k rt |
 | g  f | _ n  | S(   s  
    Load ASCII data from fname into an array and return the array.

    The data must be regular, same number of values in every row

    fname must be a filename.
    
    optional argument sep must be set if the separators are not some kind of white space.

    well suited for GRASS ascii site data.

    Example usage::

    x,y = load('test.dat')  # data in two columns

    X = load('test.dat')    # a matrix of data

    x = load('test.dat')    # a single column of data
    
    X = load('test.dat, sep = ',') # file is csv

    t   rNs-   All rows must have the same number of columnsi   (   t   codecst   opent	   readlinest   Nonet   stript   lent   splitt   strt   floatt
   ValueErrort   appendt   arrayt   shapet   max(   t   fnamet   sept   ft   linelistt   Xt   numColst   linet   valt   rowt   thisLenR   t   c(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/data_io.pyt   load   s$     .t   ,s   utf-8c   	      C   s   t  j |  d d d } | j   } | j   g  } xU | D]M } | j   } g  | j t |   D] } | j   ^ qc } | j |  q; W| S(   sW   
    Loads from ascii files with separated values
    and returns a list of lists.
    R   t   encodingt   utf8(   R   R   R   t   closeR   R   R	   R   (	   R   R   R   R   R   t   sitelistR   t   elmtR   (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/data_io.pyt   loadData8   s    
.t	   localhosti  t    c         C   s  d d l  } | j d | d | d |  d | d |  } | j   }	 |	 j d |  |	 j   }
 g  } x |
 D] } | j | d	  ql W| d
 k r d | } n  |	 j |  |	 j   } | j   g  } x= | D]5 } | j t g  t | |  D] } | ^ q   q W| S(   s   
    Fetch the contents of a table on a MySQL database
    sql,usr,passw,db,table and host must be strings.
    returns a tuple of tuples and a list of dictionaries.
    iNt   hostt   portt   usert   passwdt   dbs   show columns from i    R$   s   SELECT * FROM (	   t   MySQLdbt   connectt   cursort   executet   fetchallR   R   t   dictt   zip(   t   usrt   passwR)   t   tableR%   R&   t   sqlR*   t   cont   Cursort   coutt   cnamest   it   resultst   rest   x(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/data_io.pyt   queryDbN   s"    *
3c         C   s  d }  t  |  d  } t d d d d  } | d j d  | d j d	  x_t d
 t |   D]H} | | d
 j   } | | d } | | d j   } | | d } g  | D]& } | d | k r | d j   ^ q }	 g  | D] } | d | k r | ^ q }
 t | |	 d  } | s4| | j d  nw g  |
 D]* } | d j   | d k r;| d ^ q;} y | | j | d  Wn$ t k
 rd G| GHt j	   n Xg  | D]& } | d | k r| d j   ^ q} g  | D] } | d | k r| ^ q} t | | d  } | s3| | j d  qb g  | D]* } | d j   | d k r:| d ^ q:} y | | j | d  Wqb t k
 rd G| GHt j	   qb Xqb Wt
 d d  } x= | D]5 } x | D] } | j | d  qW| j d  qW| j   | S(   s   
    s   antt2002TODOS.csvR   t   roott   mysqlt   epigrasst   localidadesi    t   codigo1t   codigo2i   i   i   i   t   UFt   NM_NOMEi   t   NAt	   GEOCODIGOs   No match for city name s   edgesout.csvt   ws   
(   R"   R=   R   t   xrangeR   t   lowert   get_close_matchest
   IndexErrort   syst   exitR   t   writeR   (   R   t   dadost   dicCityR9   t   cidade1t   UF1t   cidade2t   UF2R<   t   ln1t   ld1t   matt   geoc1t   ln2t   ld2t   mat2t   geoc2t   foutt   j(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/data_io.pyt   loadEdgeDataj   sN    3)7	3)7	
c         C   sh  |  s d } n |  } t  | d  } | j   } t d d d d  } t d d d d  } d g } d	 } x | D] }	 xI | D]A }
 |
 d
 t |	  k r} |
 d d |
 d d |
 d } q} q} WxO | D]G } | d |	 d  k r | d t | d  d t |	  d } q q W| j |  qp W| j   | s>d } n  t  | d  } | j |  | j   d S(   sk   
    Search the locality and census databases tables for codes in a file and creates a
    sites file.
    s
   scodes.csvR   R>   R?   R@   RA   t   universos7   latitude,longitude,localidade,populacao urb.,geocodigo
i    RG   t
   MD_LATITUDR   t
   MD_LONGITURE   t   ID_i   t   V04s   
s
   sitios.csvRH   N(   R   R   R=   R   R	   R   R   t
   writelines(   t   finR^   R   R   t   codesRQ   t   dicTodost   sitiosR<   R9   R_   R   t   kt   outf(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/data_io.pyt   getSiteFromGeo   s.    		)1
	R@   R>   R?   c         C   s   d d l  } z | j d | d | d | d | d |  } | j   }	 d | }
 |	 j |
  t |  d	  } | j   } | j   d
 | d } xL | D]D } t g  | j d  D] } t	 |  ^ q  } |	 j | |  q WWd | j   Xd S(   sG   
    Creates a site table on a mysql db from a sites file (fname).
    iNR%   R&   R'   R(   R)   s   CREATE TABLE %s(
        `latitude` VARCHAR( 12 ) DEFAULT '0' NOT NULL ,
        `longitude` VARCHAR( 12 ) DEFAULT '' NOT NULL,
        `locality` VARCHAR(64),
        `population` INT(11),
        `geocodigo` INT(9)
        );
        R   s   INSERT INTO %ss    VALUES(%s,%s,%s,%s,%s)R   (
   R*   R+   R,   R-   R   R   R   t   tupleR   R   (   R   R3   R)   R1   R2   R%   R&   R*   R5   R6   R4   R   t	   listsitest   sql2t   siteR9   t   values(    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/data_io.pyt	   sitesToDb   s    *

.(   t   __doc__t   numpyt   stringR   t   difflibRM   R   R   R"   R=   R`   Rm   Rs   (    (    (    sM   /home/fccoelho/Documentos/Projects_Software/epigrass-code/Epigrass/data_io.pyt   <module>   s   


,	3"