|
import csv |
|
import time |
|
from time import strptime |
|
from datetime import datetime |
|
from pathlib import Path |
|
|
|
|
|
|
|
|
|
|
|
YEARS_LOCATION = "../orig_downloads/csv" |
|
LOCATION_2023 = "../orig_downloads/2023/csv" |
|
|
|
YEARS_PATH = Path(YEARS_LOCATION) |
|
YEARS_PATH_2023 = Path(LOCATION_2023) |
|
|
|
FINAL_BIG_FILE = "../full_years_remove_flawed_rows.csv" |
|
FINAL_BIG_FILE_2023 = "../full_2023_remove_flawed_rows.csv" |
|
|
|
HEADER = "#YY,MM,DD,hh,mm,WDIR,WSPD,GST,WVHT,DPD,APD,MWD,PRES,ATMP,WTMP,DEWP,VIS,TIDE\n" |
|
FINAL_HEADER = ["TSTMP", "#YY","MM","DD", "hh","mm","WDIR","WSPD","GST","WVHT","DPD","APD","MWD","PRES","ATMP","WTMP"] |
|
|
|
|
|
|
|
def standardize(): |
|
for read_path in YEARS_PATH.rglob('*.csv'): |
|
out_file_name = "fixed_" + read_path.name |
|
write_path = str(read_path).replace(read_path.name, out_file_name) |
|
with open(read_path, newline='') as read_file, open(write_path, 'w', newline='\n') as write_file: |
|
year = read_path.name[6:10] |
|
year = int(year) |
|
if year <= 2006: |
|
|
|
read_file.readline() |
|
|
|
write_file.write(HEADER) |
|
for line in read_file: |
|
line = line.strip() |
|
if line[len(line)-1] == ",": |
|
line_array = line[:-1].split(',') |
|
else: |
|
line_array = line.split(',') |
|
|
|
|
|
if year <= 1998: |
|
line_array[0] = "19" + (line_array[0]) |
|
|
|
|
|
if year < 2000: |
|
line_array.append('99.0') |
|
|
|
|
|
if year < 2005: |
|
line_array.insert(4, '0') |
|
|
|
|
|
write_file.write(','.join(line_array) + "\n") |
|
if year > 2006: |
|
|
|
|
|
read_file.readline() |
|
read_file.readline() |
|
|
|
|
|
write_file.write(HEADER) |
|
for line in read_file: |
|
line = line.strip() |
|
if line[len(line)-1] == ",": |
|
line = line[0:-1] |
|
write_file.write(line + "\n") |
|
|
|
|
|
def winnow_down(big_file_name, read_location): |
|
|
|
|
|
nine9_0 = {"WVHT", "WSPD", "GST", "DPD", "APD"} |
|
nine99_0 = {"ATMP", "WTMP"} |
|
nine99 = {"WDIR", "MWD"} |
|
if_all_missing = {"DPD","APD"} |
|
remove_me = {"DEWP", "VIS", "TIDE"} |
|
|
|
|
|
|
|
with open(big_file_name, 'w', newline='') as file: |
|
fieldnames = FINAL_HEADER |
|
output_csvfile = csv.DictWriter(file, fieldnames=fieldnames) |
|
|
|
output_csvfile.writeheader() |
|
for read_path in read_location.rglob('fixed_*.csv'): |
|
print(read_path) |
|
with open(read_path, newline='') as csv_file: |
|
csv_reader = csv.DictReader(csv_file) |
|
|
|
|
|
for row in csv_reader: |
|
|
|
|
|
delete_row = 0.0 |
|
if row["WSPD"] == "99.0": |
|
delete_row = delete_row + 1.0 |
|
if row["WVHT"] == "99.0" or row["WVHT"] == "99.00": |
|
delete_row = delete_row + 1.0 |
|
if row["WTMP"] == "999.0": |
|
delete_row = delete_row + 1.0 |
|
|
|
for key in if_all_missing: |
|
if row[key] == "99.0" or row[key] == "99.00": |
|
delete_row = delete_row + 0.5 |
|
|
|
|
|
if delete_row >= 2.0: |
|
|
|
continue |
|
|
|
|
|
|
|
|
|
for key in nine99: |
|
if row[key] == '999': |
|
row[key] = '' |
|
for key in nine9_0: |
|
if row[key] == '99.0' or row[key] == '99.00': |
|
row[key] = '' |
|
for key in nine99_0: |
|
if row[key] == '999.0': |
|
row[key] = '' |
|
if row["PRES"] == '9999.0': |
|
row["PRES"] = '' |
|
|
|
|
|
for key in remove_me: |
|
del row[key] |
|
|
|
|
|
|
|
timestamp_string = row["#YY"] + "-" + row["MM"] + "-" + row["DD"] + " " + row["hh"] + ":" + row["mm"] + "-" + "-0500" |
|
row["TSTMP"] = datetime.strptime(timestamp_string, "%Y-%m-%d %H:%M-%z") |
|
|
|
|
|
output_csvfile.writerow(row) |
|
|
|
|
|
def standardize2023(): |
|
for read_path in YEARS_PATH_2023.rglob('*.csv'): |
|
out_file_name = "fixed_" + read_path.name |
|
write_path = str(read_path).replace(read_path.name, out_file_name) |
|
with open(read_path, newline='') as read_file, open(write_path, 'w', newline='\n') as write_file: |
|
|
|
read_file.readline() |
|
read_file.readline() |
|
|
|
|
|
write_file.write(HEADER) |
|
for line in read_file: |
|
line = line.strip() |
|
if line[len(line)-1] == ",": |
|
line = line[0:-1] |
|
write_file.write(line + "\n") |
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
print("start") |
|
|
|
winnow_down(FINAL_BIG_FILE, YEARS_PATH) |
|
|
|
winnow_down(FINAL_BIG_FILE_2023, YEARS_PATH_2023) |
|
print("finished") |