class CSVConverterSolarAnywhere(CSVConverter): def __init__(self, logger): self.logger = logger self.parser = ParserSolarAnywhere(self.logger) self.pandas_tools = PandasTools(self.parser, self.logger) def input_csv_to_df(self, input_file, date_range, plot_output_path): csv_converter = CSVConverter(self.parser, self.logger) # "0_0.csv" => "0_0" column_str = input_file.filename.split(".")[0] columns = [ column_str + '_' + column for column in ['GHI', 'DNI', 'DHI'] ] csv_converter.input_csv_to_df(input_file, date_range, columns) df = csv_converter.pandas_tools.df.copy() ParserSolarAnywhere.plot(plot_output_path, input_file.filename, df) self.pandas_tools.concat_df(csv_converter.pandas_tools.df) def df_to_output_csv(self, output_path, output_filename): output_file = CSVWriter(output_path, output_filename) output_file.write_row(['DATASET:', self.parser.NAME]) columns = ['Timestamp'] columns.extend(self.pandas_tools.df.columns) output_file.write_row(columns) self.pandas_tools.df_to_csv(output_file) output_file.close()
class CSVConverterADCP(CSVConverter): def __init__(self, logger): self.logger = logger self.parser = ParserADCP(self.logger) self.pandas_tools = PandasTools(self.parser, self.logger) def input_csv_to_df(self, input_file, date_range): csv_converter = CSVConverter(self.parser, self.logger) # "TAO_T0N110W_D_ADCP.ascii" => "T0N110W" column_str = input_file.filename.split("_")[1] columns = self.columns(column_str) csv_converter.input_csv_to_df(input_file, date_range, columns, column_str) self.pandas_tools.concat_df(csv_converter.pandas_tools.df) @classmethod def columns(cls, column_str): cols = [] for depth in range(10, 321, 5): # [10, 15, 20, ..., 310, 315, 320] for component in ['UCUR', 'VCUR', 'WCUR']: col = column_str + "_" + str(int(depth)) + "_" + component cols.append(col) return cols def df_to_output_csv(self, output_path, output_filename): output_file = CSVWriter(output_path, output_filename) output_file.write_row(['DATASET:', self.parser.NAME]) columns = ['Timestamp'] columns.extend(self.pandas_tools.df.columns) output_file.write_row(columns) self.pandas_tools.df_to_csv(output_file) output_file.close()
class DataFrameUtils: def __init__(self, parser, logger): self.parser = parser self.logger = logger self.pandas_tools = PandasTools(self.parser, self.logger) self.current_pandas_tools = None def buoy_start(self, new_buoy_id): print "new buoy", new_buoy_id columns = [ "lat", "long", "ZonWinds", "MerWinds", "Humidity", "AirTemp", "SST" ] columns = ["%s_%s" % (new_buoy_id, column) for column in columns] if new_buoy_id != 1: # this is only False the first time this method is called self.buoy_end() self.current_pandas_tools = PandasTools(self.parser, self.logger) self.current_pandas_tools.new_df(columns, False) def add_row(self, timestamp, row): self.current_pandas_tools.add_row(timestamp, row) def buoy_end(self): self.pandas_tools.concat_df(self.current_pandas_tools.df) def df_to_output_csv(self, output_path, output_filename): # print stats self.pandas_tools.print_stats() # output to file output_file = CSVWriter(output_path, output_filename) output_file.write_row(['DATASET:', self.parser.NAME]) output_file.write_row(['TIME UNIT:', 'hours']) output_file.write_row( ['FIRST TIMESTAMP:', self.pandas_tools.first_timestamp()]) row = ['Time Delta'] row.extend(self.pandas_tools.df.columns) output_file.write_row(row) self.pandas_tools.df_to_csv(output_file, 'hours') output_file.close()
class CSVConverterNOAA(CSVConverter): def __init__(self, logger): self.logger = logger self.parser = ParserNOAA(self.logger) self.pandas_tools = PandasTools(self.parser, self.logger) def input_csv_to_df(self, input_file, date_range): csv_converter = CSVConverter(self.parser, self.logger) # "TAO_T5N140W_D_SST_10min.ascii" => "T5N140W" column = input_file.filename.split("_")[1] csv_converter.input_csv_to_df(input_file, date_range, [column]) self.pandas_tools.concat_df(csv_converter.pandas_tools.df) def df_to_output_csv(self, output_path, output_filename): output_file = CSVWriter(output_path, output_filename) output_file.write_row(['DATASET:', self.parser.NAME]) columns = ['Timestamp'] columns.extend(self.pandas_tools.df.columns) output_file.write_row(columns) self.pandas_tools.df_to_csv(output_file) output_file.close()