Skip to content

Commit

Permalink
Eliminate prints and add Filter classes
Browse files Browse the repository at this point in the history
  • Loading branch information
gnrgomes committed Dec 7, 2023
1 parent ecab145 commit c3a4a04
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
1 change: 0 additions & 1 deletion src/lisfloodutilities/gridding/generate_grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ def run(config_filename: str, infolder: str, output_file: str, processing_dates_
outfilepath = filename.with_suffix('.tiff')
output_writer_tiff.open(outfilepath)
grid_data = grid_utils.generate_grid(filename)
print('orig: ', grid_data[1519][2250])
output_writer_netcdf.write(grid_data, file_timestamp)
if output_tiff:
output_writer_tiff.write(grid_data, file_timestamp)
Expand Down
40 changes: 40 additions & 0 deletions src/lisfloodutilities/gridding/lib/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from pathlib import Path
import pandas as pd

class KiwisFilter():

def __init__(self, filter_args: dict):
self.args = filter_args

def filter(self, kiwis_files: array) -> array:
filtered_data_frames = []
for file_path in kiwis_files:
df_kiwis = pd.read_csv(file_path, sep="\t")
df_kiwis = self.__apply_filter(df_kiwis)
filtered_data_frames.append(df_kiwis)
return filtered_data_frames

def __apply_filter(self, df: pd.DataFrame) -> pd.DataFrame:
# Get the code to filter kiwis leaving only the rows to be used for point file creation
return df

# KIWIS_FILTER_PLUGIN_CLASSES = {'DowgradedObservationsKiwisFilter': {'1295': 1.0}, 'ObservationsKiwisFilter': {'1303': 100.0}}

class DowgradedObservationsKiwisFilter(KiwisFilter):

def __init__(self, filter_args: dict):
super().__init__(filter_args)

def __apply_filter(self, df: pd.DataFrame) -> pd.DataFrame:
df = super().__apply_filter(df)
return df

class ObservationsKiwisFilter(KiwisFilter):

def __init__(self, filter_args: dict):
super().__init__(filter_args)

def __apply_filter(self, df: pd.DataFrame) -> pd.DataFrame:
df = super().__apply_filter(df)
return df

4 changes: 0 additions & 4 deletions src/lisfloodutilities/gridding/lib/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,12 @@ def write_timestep(self, grid: np.ndarray, timestep: int = -1):
raise Exception("netCDF Dataset was not initialized. If file already exists, use --force flag to append.")
timestep_idx = int(timestep / self.time_frequency)
self.nf.variables[self.netcdf_var_time][timestep_idx] = timestep
print('ncdf f(grid): ', grid[1519][2250])
values = self.setNaN(copy.deepcopy(grid))
values[values < self.conf.value_min_packed] = np.nan
values[values > self.conf.value_max_packed] = np.nan
values[values != self.conf.VALUE_NAN] *= self.conf.scale_factor
values[values != self.conf.VALUE_NAN] += self.conf.add_offset
values[np.isnan(values)] = self.conf.VALUE_NAN * self.conf.scale_factor + self.conf.add_offset
print('ncdf i(grid): ', values[1519][2250])
self.nf.variables[self.var_code][timestep_idx, :, :] = values

def opened(self) -> bool:
Expand Down Expand Up @@ -290,8 +288,6 @@ def write_timestep(self, grid: np.ndarray, timestep: int = -1):
ds.GetRasterBand(1).SetScale(self.conf.scale_factor)
ds.GetRasterBand(1).SetOffset(self.conf.add_offset)
ds = self.setup_dataset_metadata(ds)
print('tiff f(grid): ', grid[1519][2250])
print('tiff i(grid): ', grid.astype(np.int16)[1519][2250])
ds.GetRasterBand(1).WriteArray(grid.astype(np.int16))
ds.FlushCache()
ds = None
Expand Down

0 comments on commit c3a4a04

Please sign in to comment.