예제 #1
0
def read_csv(filepath, has_headers = True, delim = ','):
    """ simple reader of a delimited file. Does not read fixed-width """
    
    with open(filepath,'r') as f:

        data = []

        if has_headers:
            heads = next(f).replace("\n","").split(delim)
        else:
            heads = None

        for line in f:
            entry = line.replace("\n","").split(delim)
            data.append(entry)
            
        f.close()
            
    print("Loaded data from '{0}'".format(filepath))

    # assemble the text data object and return it
    tdo = text_data( text_filepath   = filepath,
                            headers         = heads,
                            row_data        = data)
    
    return tdo
예제 #2
0
def read_DS3505(filepath, has_headers=True):
    """
    text reader for DS3505 data (space delimited) with some fixes
    
    Weather data downloaded from the following website has a peculiarity
    [http://gis.ncdc.noaa.gov/map/viewer/#app=cdo&cfg=cdo&theme=hourly&layers=1&node=gi]
    in that it has some upercase T's that are rarely needed, but ruin otherwise
    uniform space formatting.

    This function returns a properly structured text_data object
    """

    with open(filepath, 'r') as f:

        data = []

        if has_headers:
            headers = next(f).replace('\n', '').split(' ')
            headers = [x for x in headers if x != ""]
        else:
            headers = None

        for line in f:
            entry = line.replace("T", " ").replace("\n", "").split(' ')
            entry = [x for x in entry if x != ""]  # remove emptys
            data.append(entry)
        f.close()

    print("Loaded data from '{0}'".format(filepath))

    # assemble the text data object and return it
    tdo = text_data(text_filepath=filepath, headers=headers, row_data=data)

    return tdo
예제 #3
0
    def to_csv(self, csv_path):
        """
        Writes the row data of this time_series to a csv file.

        """

        # disallow overwriting the csv used as input. Added by request
        if os.path.abspath(self.infilepath) == os.path.abspath(csv_path):
            csv_path = csv_path.replace(".csv", "_out.csv")

        print("Saved time series '{0}' with {1} rows and {2} columns".format(
                                    self.name, len(self.row_data), len(self.col_data)))

        tdo = textio.text_data( text_filepath   = csv_path,
                                headers         = self.headers,
                                row_data        = self.row_data)

        tdo.write_csv()
        return
예제 #4
0
    def to_csv(self, csv_path):
        """
        Writes the row data of this time_series to a csv file.

        """

        # disallow overwriting the csv used as input. Added by request
        if os.path.abspath(self.infilepath) == os.path.abspath(csv_path):
            csv_path = csv_path.replace(".csv", "_out.csv")

        print("Saved time series '{0}' with {1} rows and {2} columns".format(
            self.name, len(self.row_data), len(self.col_data)))

        tdo = textio.text_data(text_filepath=csv_path,
                               headers=self.headers,
                               row_data=self.row_data)

        tdo.write_csv()
        return
예제 #5
0
def read_DS3505(filepath, has_headers = True):
    """
    text reader for DS3505 data (space delimited) with some fixes
    
    Weather data downloaded from the following website has a peculiarity
    [http://gis.ncdc.noaa.gov/map/viewer/#app=cdo&cfg=cdo&theme=hourly&layers=1&node=gi]
    in that it has some upercase T's that are rarely needed, but ruin otherwise
    uniform space formatting.

    This function returns a properly structured text_data object
    """
    
    with open(filepath,'r') as f:

        data = []

        if has_headers:
            headers = next(f).replace('\n','').split(' ')
            headers = [x for x in headers if x != ""]
        else:
            headers = None

        for line in f:
            entry = line.replace("T"," ").replace("\n","").split(' ')
            entry = [x for x in entry if x!= ""] # remove emptys
            data.append(entry)
        f.close()
            
    print("Loaded data from '{0}'".format(filepath))

    # assemble the text data object and return it
    tdo = text_data( text_filepath   = filepath,
                    headers         = headers,
                    row_data        = data)
    
    return tdo
예제 #6
0
def read_csv(filepath, has_headers=True, delim=","):
    """ simple reader of a delimited file. Does not read fixed-width """

    with open(filepath, "r") as f:

        data = []

        if has_headers:
            heads = next(f).replace("\n", "").split(delim)
        else:
            heads = None

        for line in f:
            entry = line.replace("\n", "").split(delim)
            data.append(entry)

        f.close()

    print("Loaded data from '{0}'".format(filepath))

    # assemble the text data object and return it
    tdo = text_data(text_filepath=filepath, headers=heads, row_data=data)

    return tdo