-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_file_coordinates.py
223 lines (178 loc) · 7.17 KB
/
map_file_coordinates.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
Author: Hana Hourston @hhourston
Date: Mar. 1, 2023
Plot IOS files on a map:
Bottle (BOT and CHE)
CTD
Current meter (CUR)
Acoustic Doppler current profiler (ADCP)
Thermosalinograph (TOB)
Weather station (ANE)
"""
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
DTYPES = ['BOT_CHE', 'CTD', 'ADCP', 'CUR', 'TOB', 'ANE']
ARCTIC_PAC_LAT_CUTOFF = 68
def map_all(wdir, left_lon, bot_lat, right_lon, top_lat):
colours = ['r', 'g', 'b', 'chartreuse', 'm', 'c']
symbols = ['o', 'v', '.', 'P', '^', 's']
# Make the map
# area_thresh=1000 means don't plot coastline features less
# than 1000 km^2 in area.
# m = Basemap(llcrnrlon=left_lon, llcrnrlat=bot_lat,
# urcrnrlon=right_lon, urcrnrlat=top_lat,
# projection='lcc', area_thresh=1000.,
# resolution='c', lat_0=0.5 * (bot_lat + top_lat),
# lon_0=0.5 * (left_lon + right_lon)) # lat_0=53.4, lon_0=-129.0)
# Azimuthal equidistant projection
# width = 28000000; lon_0 = -105; lat_0 = 40
width = 23000000; lon_0 = -140; lat_0 = 20
m = Basemap(width=width,height=width,projection='aeqd',
lat_0=lat_0,lon_0=lon_0)
fig = plt.figure(num=None, figsize=(8, 6), dpi=100)
m.drawcoastlines(linewidth=0.2)
m.drawmapboundary(fill_color='white')
m.fillcontinents(color='0.8')
# m.drawrivers()
# 20 degree graticule.
m.drawparallels(np.arange(-80,81,20))
m.drawmeridians(np.arange(-180,181,20))
# parallels = np.arange(bot_lat, top_lat, 0.5) # parallels = np.arange(48., 54, 0.2), parallels = np.linspace(bot_lat, top_lat, 10)
# m.drawparallels(parallels, labels=[True, False, True, False]) # draw parallel lat lines
# meridians = np.arange(left_lon, right_lon, 0.5)
# m.drawmeridians(meridians, labels=[False, False, False, True])
# Iterate through the dtypes
for dtype, col, sym in zip(DTYPES, colours, symbols):
# for dtype, col, sym in zip(['ADCP'], ['g'], ['.']):
print(dtype)
infile = os.path.join(wdir, f'csv_file_download_list_{dtype}.csv')
dfin = pd.read_csv(infile)
x, y = m(dfin.LON.values, dfin.LAT.values)
if dtype == 'BOT_CHE':
dtype = 'BOTTLE'
# elif dtype == 'TOB':
# dtype = 'TSG'
m.scatter(x, y, marker=sym, color=col, s=5, label=dtype)
plt.legend(loc='lower right')
plt.tight_layout()
# Save the fig and close
plt.savefig(os.path.join(wdir, 'ios-wp-file-map.png'))
plt.close(fig)
return
def pacific_map_bounds():
# left_lon, bot_lat, right_lon, top_lat
return [-180, 20, -115, 70]
def map_pacific():
"""
file_list: contains the input file names, may only be one
"""
left_lon, bot_lat, right_lon, top_lat = pacific_map_bounds()
# Make the map
# area_thresh=1000 means don't plot coastline features less
# than 1000 km^2 in area.
# Use Equidistant Cylindrical Projection
# The simplest projection, just displays the world in
# latitude/longitude coordinates.
m = Basemap(llcrnrlon=left_lon, llcrnrlat=bot_lat,
urcrnrlon=right_lon, urcrnrlat=top_lat,
projection='cyl', # area_thresh=1000.,
resolution='h')
m.drawcoastlines(linewidth=0.2)
m.drawmapboundary(fill_color='white')
m.fillcontinents(color='0.8')
# Graticule.
# labels = [left,right,top,bottom]
m.drawparallels(np.arange(20,71,10), labels=[True,False,False,False])
m.drawmeridians(np.arange(-180,-114,15), labels=[True,False,False,True])
return m
def subset_pacific_data(df):
left_lon, bot_lat, right_lon, top_lat = pacific_map_bounds()
# Restrict the upper lat more to not include arctic
return ((df.LON.values >= left_lon) &
(df.LON.values <= right_lon) &
(df.LAT.values >= bot_lat) &
(df.LAT.values <= ARCTIC_PAC_LAT_CUTOFF))
def subset_arctic_data(df):
return df.LAT.values >= ARCTIC_PAC_LAT_CUTOFF
def map_arctic():
"""
file_list: contains the input file names, may only be one
"""
# Find a good map projection to use
# nplaea: North Polar Lambert Azimuthal Projection
m = Basemap(projection='nplaea', boundinglat=65,
lon_0=230, resolution='h') # , area_thresh=1000.)
m.drawcoastlines(linewidth=0.2)
m.drawmapboundary(fill_color='white')
m.fillcontinents(color='0.8')
# Graticule.
# labels = [left,right,top,bottom]
m.drawparallels(np.arange(60,81,10), labels=[True,False,False,False])
m.drawmeridians(np.arange(-180,181,30), labels=[False,True,False,True])
return m
def do_map(file_list: list, region: str, dtype: str, output_dir: str):
# Initialize figure
plt.clf() # Clear any active plots
fig = plt.figure(num=None, figsize=(8, 6), dpi=100)
# Load the data
dfall = pd.DataFrame()
for f in file_list:
dfin = pd.read_csv(f)
dfall = pd.concat((dfall, dfin))
if region.lower() == 'pacific':
# Make map instance
m = map_pacific()
# Subset the data to the region
subsetter = subset_pacific_data(dfall)
elif region.lower() == 'arctic':
# Make map instance
m = map_arctic()
# Subset the data to the region
subsetter = subset_arctic_data(dfall)
# Scatter the points
start_year = min(pd.to_datetime(
dfall.loc[subsetter, 'START TIME(UTC)']).dt.year)
end_year = max(pd.to_datetime(
dfall.loc[subsetter, 'END TIME(UTC)']).dt.year)
num_files = sum(subsetter)
x, y = m(dfall.LON.values[subsetter], dfall.LAT.values[subsetter])
# alpha defines the opacity
m.scatter(x, y, marker='o', color='r', alpha=0.25)
plt.title(
f'{start_year} - {end_year} {dtype} {region.capitalize()} Files {num_files}')
plt.tight_layout()
# Save the fig and close
plt.savefig(os.path.join(
output_dir, f'all_{region.lower()}_{dtype}_map_hires.png'))
plt.close(fig)
return
def map_regions(wdir):
"""
dtype: 'BOT and CHE' or 'CTD' or 'ADCP and CUR'
region: 'pacific' or 'arctic'
"""
for dtype in ['BOT_CHE', 'CTD', 'ADCP']:
if dtype == 'ADCP':
# Combine with current meter
file_list = [
os.path.join(wdir, f'csv_file_download_list_{dtype}.csv'),
os.path.join(wdir, f'csv_file_download_list_CUR.csv')
]
dtype = 'ADCP + Current Meter'
else:
file_list = [
os.path.join(wdir, f'csv_file_download_list_{dtype}.csv')
]
if dtype == 'BOT_CHE':
dtype = 'Bottle'
print(dtype)
do_map(file_list, 'pacific', dtype, wdir)
do_map(file_list, 'arctic', dtype, wdir)
return
input_dir = 'C:\\Users\\HourstonH\\Documents\\sopo2023\\lu_poster\\'
# left_lon, bot_lat, right_lon, top_lat = [-180, -80, 180, 90]
# map_all(input_dir, left_lon, bot_lat, right_lon, top_lat)
map_regions(input_dir)