NOAA-Buoy / buoy-python /YearsLessThan2StdDev.py
lukawskikacper's picture
First version of the dataset
8be024f
raw
history blame
2.12 kB
import csv
from pathlib import Path
import numpy as np
import pandas as pd
from collections import defaultdict
FULL_DATA_SET_STRING = "../full_years_remove_flawed_rows.csv"
FULL_DATA_SET_PATH = Path(FULL_DATA_SET_STRING)
OUT_DATASET_STRING = "../trimmed_full_years_for_db.parquet"
OUT_DATASET_PATH = Path(OUT_DATASET_STRING)
OUT_FULL_DATASET_STRING = "../full_years_remove_flawed.parquet"
OUT_FULL_DATASET_PATH = Path(OUT_FULL_DATASET_STRING)
NUMERIC_FIELDS = ["WSPD","GST","WVHT","DPD","APD","PRES","ATMP","WTMP"]
def load_data(data_path):
print("Loading data")
with open(data_path, newline='') as csv_file:
loaded_np_data = pd.read_csv(csv_file)
print("Writing out the full Parquet file")
loaded_np_data.to_parquet(OUT_FULL_DATASET_PATH)
print("Applying Sin() to the two degrees columns")
loaded_np_data["WDIR"] = np.sin(np.deg2rad(loaded_np_data["WDIR"]))
loaded_np_data["MWD"] = np.sin(np.deg2rad(loaded_np_data["MWD"]))
print("calculating z-scores")
for var in NUMERIC_FIELDS:
var_mean = np.mean(loaded_np_data[var])
var_std = np.std(loaded_np_data[var])
var_zscore = (loaded_np_data[var] - var_mean)/var_std
loaded_np_data[var] = var_zscore
print("finding outlier rows")
# calculate the rows to keep
# for each column, is the z-score larger than 2 = loaded_np_data[NUMERIC_FIELDS].le(2)
# are there less 2 columns meeting the condition above = keep the row
output_np_data = loaded_np_data[loaded_np_data[NUMERIC_FIELDS].gt(2).sum(axis=1).lt(2)]
print("exporting to parquet")
output_np_data.set_index("TSTMP")
output_np_data.to_parquet(OUT_DATASET_PATH)
if __name__ == '__main__':
print("Start")
# Load data
all_data = load_data(FULL_DATA_SET_PATH)
# Calculate mean and std dev for each non-date column
# Going to need to sin(X) for any circular numbers (WDIR & MWD)
# Write out data removing rows
# Probably want to write out the sin(X) for any circular numbers
print("finished")