File size: 6,928 Bytes
8be024f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import csv
import time
from time import strptime
from datetime import datetime
from pathlib import Path

# UGLY - the non 2023 functions should be more generic given a certain start location - that way we don't have to repeat
# logic

# Function for Years
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"]


# Deal with the difference between files and get them standardized
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:
                # First write the new header line
                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(',')

                    # pre 1999 we need to make the year 4 digits
                    if year <= 1998:
                        line_array[0] = "19" + (line_array[0])

                    # Add tide with a value of 99.00 for all years pre 2000
                    if year < 2000:
                        line_array.append('99.0')

                    # Add 0 in for mm pre 2005 (header and values)
                    if year < 2005:
                        line_array.insert(4, '0')

                    # Changes are done, write the line
                    write_file.write(','.join(line_array) + "\n")
            if year > 2006:

                # Remove second header line from 2007 onwards
                read_file.readline()
                read_file.readline()

                # Add the first line back and just write the rest of the lines
                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")

# Now remove the columns we don't want and erase rows with a lot of missing values in columns we care about
def winnow_down(big_file_name, read_location):

    # need to be become missing data
    nine9_0 = {"WVHT", "WSPD", "GST", "DPD", "APD"}
    nine99_0 = {"ATMP", "WTMP"}
    nine99 = {"WDIR", "MWD"}
    if_all_missing = {"DPD","APD"}
    remove_me = {"DEWP", "VIS", "TIDE"}


    # Set up the file to write to
    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)

                # row is not an ordered dict
                for row in csv_reader:

                    # Check to see if we are missing key data - if so delete the row and move along
                    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
                    # if DPD and APD are missing along with any of the above then we remove
                    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:
                        # Two strikes you are out and we go on to the next row
                        continue

                    # Remove observations at least 2 of these columns with null values in wspd (99.0)  wvht (99.0) and wtmp (999.0)
                    # WD MWD = 999, GST DPD APD = 99.0, PRES = 9999.0, ATMP WTMP = 999.0
                    # For those left we need to convert these to missing(just a blank)
                    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"] = ''

                    # remove columns DEMP, VIS, TIDE
                    for key in remove_me:
                        del row[key]

                    # Finally we need to convert Y, M, D, m into a timestamp and that will be the key
                    # Buoy 42002 is in Lousiana, UTC -5
                    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")

                    # Ok we are ready to write a new row to our database
                    output_csvfile.writerow(row)

# Function for 2023
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:
            # Remove second header line from 2007 onwards
            read_file.readline()
            read_file.readline()

            # Add the first line back and just write the rest of the lines
            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")
    #standardize()
    winnow_down(FINAL_BIG_FILE, YEARS_PATH)
    #standardize2023()
    winnow_down(FINAL_BIG_FILE_2023, YEARS_PATH_2023)
    print("finished")