Exemplo n.º 1
0
    def __init__(self, data_file, tzinfo=None):
        self.default_tzinfo = tzinfo
        self.header_lines = []
        self.parameters = []
        self.site_name = ''
        self.file_ext = data_file.split('.')[-1].lower()

        if self.file_ext == 'xls':
            file_buf = open(util.xls_to_csv(data_file), 'rb')
        else:
            file_buf = open(data_file)

        self.read_eureka(file_buf)

        # if the serial number just contains numbers the cell holding
        # it might be formatted as a number, in which case it gets
        # read in with a trailing '.0'
        if hasattr(self, 'serial_number') and \
               self.serial_number.rfind('.0') == len(self.serial_number) - 2:
            self.serial_number = self.serial_number[:-2]

        if tzinfo:
            if hasattr(self, 'setup_time'):
                self.setup_time = self.setup_time.replace(tzinfo=tzinfo)
            if hasattr(self, 'stop_time'):
                self.stop_time = self.stop_time.replace(tzinfo=tzinfo)

            self.dates = [i.replace(tzinfo=tzinfo) for i in self.dates]
Exemplo n.º 2
0
    def __init__(self, data_file, tzinfo=None):
        self.default_tzinfo = tzinfo
        self.header_lines = []
        self.parameters = []
        self.site_name = ''
        self.file_ext = data_file.split('.')[-1].lower()

        if self.file_ext == 'xls':
            file_buf = open(util.xls_to_csv(data_file), 'rb')
        else:
            file_buf = open(data_file)

        self.read_eureka(file_buf)

        # if the serial number just contains numbers the cell holding
        # it might be formatted as a number, in which case it gets
        # read in with a trailing '.0'
        if hasattr(self, 'serial_number') and \
               self.serial_number.rfind('.0') == len(self.serial_number) - 2:
            self.serial_number = self.serial_number[:-2]

        if tzinfo:
            if hasattr(self, 'setup_time'):
                self.setup_time = self.setup_time.replace(tzinfo=tzinfo)
            if hasattr(self, 'stop_time'):
                self.stop_time = self.stop_time.replace(tzinfo=tzinfo)

            self.dates = [i.replace(tzinfo=tzinfo) for i in self.dates]
Exemplo n.º 3
0
    def __init__(self, data_file, tzinfo=None):
        self.default_tzinfo = tzinfo
        self.header_lines = []
        self.parameters = []
        self.site_name = ''
        if type(data_file) == str:
            self.file_name = data_file
        elif type(data_file) == file:
            self.file_name = data_file.name
        self.file_ext = self.file_name.split('.')[-1].lower()

        temp_file_path = None
        if self.file_ext == 'xls':
            temp_file_path, self.xlrd_datemode = util.xls_to_csv(
                self.file_name)
            file_buf = open(temp_file_path, 'rb')
        else:
            if type(data_file) == str:
                file_buf = open(data_file)
            elif type(data_file) == file:
                file_buf = data_file

        try:
            self.read_eureka(file_buf)
        except:
            raise
        finally:
            if type(data_file) == str:
                file_buf.close()
            if temp_file_path:
                os.remove(temp_file_path)

        if tzinfo:
            if hasattr(self, 'setup_time'):
                self.setup_time = self.setup_time.replace(tzinfo=tzinfo)
            if hasattr(self, 'stop_time'):
                self.stop_time = self.stop_time.replace(tzinfo=tzinfo)

            self.dates = [i.replace(tzinfo=tzinfo) for i in self.dates]
Exemplo n.º 4
0
    def __init__(self, data_file, tzinfo=None):
        self.default_tzinfo = tzinfo
        self.header_lines = []
        self.parameters = []
        self.site_name = ''
        if type(data_file) == str:
            self.file_name = data_file
        elif type(data_file) == file:
            self.file_name = data_file.name
        self.file_ext = self.file_name.split('.')[-1].lower()

        temp_file_path = None
        if self.file_ext == 'xls':
            temp_file_path, self.xlrd_datemode = util.xls_to_csv(
                self.file_name)
            file_buf = open(temp_file_path, 'rb')
        else:
            if type(data_file) == str:
                file_buf = open(data_file)
            elif type(data_file) == file:
                file_buf = data_file

        try:
            self.read_eureka(file_buf)
        except:
            raise
        finally:
            if type(data_file) == str:
                file_buf.close()
            if temp_file_path:
                os.remove(temp_file_path)

        if tzinfo:
            if hasattr(self, 'setup_time'):
                self.setup_time = self.setup_time.replace(tzinfo=tzinfo)
            if hasattr(self, 'stop_time'):
                self.stop_time = self.stop_time.replace(tzinfo=tzinfo)

            self.dates = [i.replace(tzinfo=tzinfo) for i in self.dates]
Exemplo n.º 5
0
def autodetect(data_file, filename=None):
    """
    returns file_format string if successfully able to detect file
    format, returns False otherwise.

    data_file can be either a filename string or a file-like object.
    If it is a file-like object you can pass a file_name string if the
    file-like object doesn't have a name or filename attribute
    containing the filename.
    """

    if not filename:
        if type(data_file) == str:
            filename = data_file
        elif hasattr(data_file, 'name'):
            filename = data_file.name
        elif hasattr(data_file, 'filename'):
            filename = data_file.filename

    file_ext = filename.split('.')[-1].lower()

    if file_ext and file_ext == 'xls':
        temp_csv_path, xls_read_mode = util.xls_to_csv(data_file)
        fid = open(temp_csv_path, 'rb')
        lines = [fid.readline() for i in range(3)]
        fid.close()
        os.remove(temp_csv_path)

    else:
        if type(data_file) == str:
            fid = open(data_file, 'r')
        else:
            fid = data_file

        file_initial_location = fid.tell()
        fid.seek(0)
        lines = [fid.readline() for i in range(3)]
        fid.seek(file_initial_location)


    if lines[0].lower().find('greenspan') != -1:
        return 'greenspan'
    if lines[0].lower().find('macroctd') != -1:
        return 'macroctd'
    if lines[0].lower().find('minisonde4a') != -1:
        return 'hydrotech'
    if lines[0].lower().find('data file for datalogger.') != -1:
        return 'solinst'
    if lines[0].find('Serial_number:')!= -1 and lines[2].find('Project ID:')!= -1:
        return 'solinst'
    if lines[0].lower().find('log file name') != -1:
        return 'hydrolab'
    if lines[0].lower().find('pysonde csv format') != -1:
        return 'generic'
        # possible binary junk in first line of hydrotech file
    if lines[1].lower().find('log file name') != -1:
        return 'hydrotech'
    if lines[0].lower().find('the following data have been') != -1:
        return 'lcra'

    # ascii files for ysi in brazos riv.
    if lines[0].find('espey') != -1:
        return 'espey'

    #check for ysi:
    # binary
    if lines[0][0] == 'A':
        return 'ysi_binary'
    # txt file
    if lines[0].find('=') != -1:
        return 'ysi_text'
    if lines[0].find('##YSI ASCII Datafile=') != -1:
        return 'ysi_ascii'
    # cdf file
    if file_ext and file_ext == 'cdf':
        return 'ysi_cdf'
    if lines[0].find("Date") > -1 and lines[1].find("M/D/Y") > -1:
        return 'ysi_csv'

    #eureka try and detect degree symbol
    if lines[1].find('\xb0') > -1 or lines[2].find('Manta') > -1 or \
           lines[0].find('Start time : ') > -1:
        return 'eureka'

    # files from various intruments processed by an old script.
    if lines[0].lower().find('request date') != -1:
        return 'midgewater'
    else:
        return False
Exemplo n.º 6
0
Arquivo: sonde.py Projeto: twdb/sonde
def autodetect(data_file, filename=None):
    """
    returns file_format string if successfully able to detect file
    format, returns False otherwise.

    data_file can be either a filename string or a file-like object.
    If it is a file-like object you can pass a file_name string if the
    file-like object doesn't have a name or filename attribute
    containing the filename.
    """

    if not filename:
        if type(data_file) == str:
            filename = data_file
        elif hasattr(data_file, 'name'):
            filename = data_file.name
        elif hasattr(data_file, 'filename'):
            filename = data_file.filename

    file_ext = filename.split('.')[-1].lower()

    if file_ext and file_ext == 'xls':
        temp_csv_path, xls_read_mode = util.xls_to_csv(data_file)
        fid = open(temp_csv_path, 'rb')
        lines = [fid.readline() for i in range(3)]
        fid.close()
        os.remove(temp_csv_path)

    else:
        if type(data_file) == str:
            fid = open(data_file, 'r')
        else:
            fid = data_file

        file_initial_location = fid.tell()
        fid.seek(0)
        lines = [fid.readline() for i in range(3)]
        fid.seek(file_initial_location)

    if lines[0].lower().find('greenspan') != -1:
        return 'greenspan'
    if lines[0].lower().find('macroctd') != -1:
        return 'macroctd'
    if lines[0].lower().find('minisonde4a') != -1:
        return 'hydrotech'
    if lines[0].lower().find('data file for datalogger.') != -1:
        return 'solinst'
    if lines[0].find('Serial_number:') != -1 and lines[2].find(
            'Project ID:') != -1:
        return 'solinst'
    if lines[0].lower().find('log file name') != -1:
        return 'hydrolab'
    if lines[0].lower().find('pysonde csv format') != -1:
        return 'generic'
        # possible binary junk in first line of hydrotech file
    if lines[1].lower().find('log file name') != -1:
        return 'hydrotech'
    if lines[0].lower().find('the following data have been') != -1:
        return 'lcra'

    # ascii files for ysi in brazos riv.
    if lines[0].find('espey') != -1:
        return 'espey'

    #check for ysi:
    # binary
    if lines[0][0] == 'A':
        return 'ysi_binary'
    # txt file
    if lines[0].find('=') != -1:
        return 'ysi_text'
    if lines[0].find('##YSI ASCII Datafile=') != -1:
        return 'ysi_ascii'
    # cdf file
    if file_ext and file_ext == 'cdf':
        return 'ysi_cdf'
    if lines[0].find("Date") > -1 and lines[1].find("M/D/Y") > -1:
        return 'ysi_csv'

    #eureka try and detect degree symbol
    if lines[1].find('\xb0') > -1 or lines[2].find('Manta') > -1 or \
           lines[0].find('Start time : ') > -1:
        return 'eureka'

    # files from various intruments processed by an old script.
    if lines[0].lower().find('request date') != -1:
        return 'midgewater'
    else:
        return False
Exemplo n.º 7
0
def autodetect(data_file, filename=None):
    """
    autodetect file_format based on file
    return file_format string if successful or
    False if unable to determine format

    data_file can be either a filename string or a file-like object,
    if it is a file-like object, you can pass a file_name string if
    the file-like object doesn't have a name or filename attribute
    containing the filename
    """

    if type(data_file) == str:
        fid = open(data_file, 'r')
    else:
        fid = data_file

    if not filename:
        if type(data_file) == str:
            filename = data_file
        elif hasattr(data_file, 'name'):
            filename = data_file.name
        elif hasattr(data_file, 'filename'):
            filename = data_file.filename

    file_ext = filename.split('.')[-1].lower()

    if file_ext and file_ext == 'xls':
        fid = util.xls_to_csv(data_file)

    fid.seek(0)

    #read first line
    line1 = fid.readline()

    if line1.lower().find('greenspan') != -1:
        return 'greenspan'
    if line1.lower().find('macrocdt') != -1:
        return 'macroctd'
    if line1.lower().find('minisonde4a') != -1:
        return 'hydrotech'
    if line1.lower().find('data file for datalogger.') != -1:
        return 'solinst'
    if line1.lower().find('log file name') != -1:
        return 'hydrolab'
    if line1.lower().find('pysonde csv format') != -1:
        return 'generic'

    #read second line
    line2 = fid.readline()
    if line2.lower().find('log file name') != -1:  # binary junk in first line
        return 'hydrotech'

    #check for ysi
    if line1[0] == 'A':
        return 'ysi_binary'  # binary
    if line1.find('=') != -1:
        return 'ysi_text'  # txt file
    if file_ext and file_ext == 'cdf':
        return 'ysi_cdf'  # cdf file

    #eureka try and detect degree symbol
    if line2.find('\xb0') != -1:
        return 'eureka'
    else:
        return False