-
Notifications
You must be signed in to change notification settings - Fork 0
/
hist_yearly_counts.py
127 lines (103 loc) · 3.78 KB
/
hist_yearly_counts.py
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
"""
Author: Hana Hourston
Date: Mar. 1, 2023
"""
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
import glob
def get_year_from_filename(infile, dtype):
"""
dtype: "ADCP", "BOT", "CTD", or "CUR"
"""
if dtype in ['ADCP', 'CUR']:
dfin = pd.read_csv(infile, header=0, names=['File path'])
dfout = dfin
func = lambda x: int(os.path.basename(x).split('_')[1][:4])
dfout['Year'] = list(map(func, dfout.iloc[:, 0]))
elif dtype in ['BOT', 'CTD', 'CHE']:
# func = lambda x: int(os.path.basename(x)[:4])
dfin = pd.read_csv(infile)
dfout = dfin.loc[:, ['FILE_URL']]
dfout.rename(columns={'FILE_URL': 'File path'}, inplace=True)
dfout['Year'] = pd.to_datetime(dfin.loc[:, 'START TIME(UTC)']).dt.year
else:
print('Unsupported dtype', dtype)
return
outfile = infile.replace('.csv', '_hasyear.csv')
dfout.to_csv(outfile, index=False)
return
def hist_annual_instrument_count(infile, png_name: str, dtype: str):
# Get data in right format
dfin = pd.read_csv(infile)
num_profs = len(dfin)
num_bins = dfin.Year.max() - dfin.Year.min() + 1
# Manually assign y axis ticks to have only whole number ticks
# num_yticks = dfin.Year.max
# if num_yticks < 10:
# yticks = np.arange(num_yticks + 1)
plt.clf() # Clear any active plots
fig, ax = plt.subplots() # Create a new figure and axis instance
ax.hist(dfin.Year.values, bins=num_bins, align='left',
label='Number of files: {}'.format(num_profs))
# if num_yticks < 10:
# ax.set_yticks(yticks)
ax.minorticks_on()
ax.set_ylabel('Number of Profiles')
plt.legend()
plt.title('Number of {} files per year'.format(dtype))
plt.tight_layout()
plt.savefig(png_name)
plt.close(fig)
return
def scatter_year_counts(fdict, png_name):
plt.clf() # Clear any active plots
fig, ax = plt.subplots() # Create a new figure and axis instance
# plot the data
colours = ['r', 'g', 'b', 'grey']
symbols = ['o', 'v', '.', 'P']
for key, col, sym in zip(fdict.keys(), colours, symbols):
# Load the csv data
df = pd.read_csv(fdict[key])
num_files = str(len(df))
num_bins = df.Year.max() - df.Year.min() + 1
# Use hist function to count number of occurrences,
# or use numpy.unique
hist, bin_edges = np.histogram(df.Year.values, num_bins)
# scatter plot
ax.plot(bin_edges[:-1], hist, color=col, marker=sym,
label=f'{key}: {num_files}')
ax.minorticks_on()
ax.set_ylabel('Number of files')
plt.legend(title='Total file counts')
plt.tight_layout()
plt.savefig(png_name)
plt.close(fig)
return
input_dir = 'C:\\Users\\HourstonH\\Documents\\sopo2023\\lu_poster\\'
# file_lists = glob.glob(input_dir + '*.csv')
# file_lists.sort()
#
# for f in file_lists:
# print(os.path.basename(f))
# # hist_annual_instrument_count()
# dtype = os.path.basename(f).split('_')[-1].split('.')[0]
# print(dtype)
# get_year_from_filename(f, dtype)
file_lists = glob.glob(input_dir + '*hasyear.csv')
file_lists.sort()
file_dict = {}
for f in file_lists:
print(os.path.basename(f))
if 'CTD' in os.path.basename(f):
file_dict['CTD'] = f
elif 'CUR' in os.path.basename(f):
file_dict['CUR'] = f
elif 'ADCP' in os.path.basename(f):
file_dict['ADCP'] = f
elif 'BOT' in os.path.basename(f):
file_dict['BOTTLE'] = f
plot_name = input_dir + 'IOS file counts per year.png'
# hist_annual_instrument_count(f, plot_name, dtype)
scatter_year_counts(file_dict, plot_name)