forked from hayesla/NOAA-DRAP-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_drap_functions.py
150 lines (114 loc) · 3.38 KB
/
plot_drap_functions.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
import numpy as np
import os
import re
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import urllib
def get_data(date, filepath='/Users/laurahayes/Documents/drap'):
"""
Function to download the DRAP zip files for a certain date.
Parameters
----------
date : `~datetime.datetime`
Returns
-------
The filepath if downloaded.
"""
url = 'https://www.ngdc.noaa.gov/stp/drap/data/' + date.strftime('%Y/%m/')
try:
file = 'SWX_DRAP20_C_SWPC_'+ date.strftime('%Y%m%d') + '.zip'
urlpath = os.path.join(url, file)
urllib.request.urlretrieve(urlpath, os.path.join(filepath, file))
except:
file = 'SWX_DRAP20_C_SWPC_'+ date.strftime('%Y%m%d') + '.tar.gz'
urlpath = os.path.join(url, file)
urllib.request.urlretrieve(urlpath, os.path.join(filepath, file))
if os.path.exists(os.path.join(filepath, file)):
print (os.path.join(filepath, file))
else:
print ('didn\'t download')
return
return os.path.join(filepath, file)
def read_global_files(file_path):
"""
Function to read the global DRAP txt files and return lat, lon and map data
Parameters
----------
filepath : `~str`
path to txt file
Returns
-------
lats : `~ndarray`
2D np.array of latitudes
lons : `ndarray`
2D np.array of longitudes
full_map : `ndarray
2D np.array - map data (degraded freq) at each lat lon
"""
data = []
for lines in open(file_path):
if not lines.startswith('#'):
data.append(lines)
#first line contains the latitute info
lat = [float(x) for x in data[0].split()]
#get rest of data
lon = []
map_values = []
for dat in data[2:]:
#collecting longitude values
lon.append(float(dat.split()[0]))
#collecting map values - sometimes **** so need to check
try:
val = [float(x) for x in dat.split()[2:]]
except:
val = []
for v in dat.split()[2:]:
if v == '****':
val.append(100)
else:
val.append(float(v))
map_values.append(val)
lon.reverse()
map_values.reverse()
full_map = np.array(map_values).T
lats, lons = np.meshgrid(np.array(lon), np.array(lat))
return lats, lons, full_map
def plot_global_map(lats, lons, full_map, title="DRAP Model", save_plot=None,):
"""
Function to plot map after of DRAP model.
Parameters
----------
outputs from read_global_files
lats : `ndarray`
2D array of latitude values
lons : `ndarray`
2D array of longitude values
full_map : `ndarray`
2D array of DRAP degraded frequencies.
title : `~str`, optional
title of plot, defaults "DRAP Model"
save_plot : `~str`, optional
path name of file to save, default None (i.e. doesn't save)
"""
fig=plt.figure(figsize=(10, 5))
ax = fig.add_subplot()
m = Basemap(projection='cyl',
llcrnrlat=-89,urcrnrlat=89,
llcrnrlon=-178,urcrnrlon=178,
resolution='l', ax=ax)
levels = np.arange(0, 35,0.1)
CS2 = m.contourf(lons,lats,full_map,levels,cmap='magma_r', extend='both',latlon=True)
cbar = m.colorbar(CS2,location='right', size="2%")
cbar.set_label('Degraded Frequency (MHz)')
cbar.set_ticks([0, 5, 10, 15, 20, 25, 30])
m.drawcoastlines()
m.drawmapboundary()
m.drawparallels(np.linspace(-90, 90, 10), linewidth = 0.5, labels = [1,0,0,0])
m.drawmeridians(np.linspace(-180, 180, 10), linewidth = 0.5, labels = [0,0,0,1])
ax.set_xlabel("Longitude", labelpad=20)
ax.set_ylabel("Latitude", labelpad=40)
ax.set_title(title)
plt.tight_layout()
if save_plot != None:
plt.savefig(save_plot+'.png', dpi=200)
plt.show()