-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmovie_bergs_traj.py
executable file
·283 lines (241 loc) · 7.61 KB
/
movie_bergs_traj.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python
def bergs_yearday(bergs):
bergs=bergs_read(bergs,'year')
bergs=bergs_read(bergs,'day')
for b in xrange (0,len(bergs.berg)):
bergs.berg[b].yearday=bergs.berg[b].year+(bergs.berg[b].day/372)
#Note I have not included all the warnings about dimensions already existing.
return bergs
def bergs_read(bergs,varname):
nc=bergs.nc
bergs_list=bergs.berg
var = nc.variables[varname][:]
for b in xrange (0,len(bergs.berg)):
k=[]
js=bergs.berg[b].js
je=bergs.berg[b].je
for l in xrange (0,len(js)):
temp=range(js[l],je[l]+1)
for i in xrange (0, len(temp)):
k.append(temp[i])
setattr(bergs.berg[b],varname,var[k])
return bergs
def bergs_hash(year, day, x, y, m):
ilat=(90.+y)/180
ilon=(360 % x)/360;
iyear=np.floor(year);
iday=(day/372);
im=math.log(m,10)-7.9;
h=ilon+1e3*ilat+1e6*iday+1e9*iyear+1e12*im;
return h
def bergs_open(filename):
#Creating a classes
class berg_handle:
def __init__(self,berg,nc):
self.berg=berg
self.nc=nc
class berg_type:
def __init__(self,hash):
self.hash=hash
self.year0=[]
self.day0=[]
self.lon0=[]
self.lat0=[]
self.mass0=[]
self.js=[]
self.je=[]
self.width=[]
self.lon=[]
self.lat=[]
self.year=[]
self.day=[]
self.uvel=[]
self.vvel=[]
self.uo=[]
self.vo=[]
self.ui=[]
self.ua=[]
self.vu=[]
self.mass_of_bits=[]
self.heat_density=[]
self.thickness=[]
self.length=[]
self.ssh_x=[]
self.ssh_y=[]
self.sst=[]
self.cn=[]
self.hi=[]
self.yearday=[]
self.iceberg_num=[]
#Importing the data
nc = Dataset(filename, mode='r')
lat = nc.variables['lat'][:]
N=len(lat)
js=np.where(lat>91)[0]
je=np.concatenate([js[1:]-1, np.array([N-1])])
lon = nc.variables['lon'][:]
year = nc.variables['year'][:]
day = nc.variables['day'][:]
#mass = nc.variables['mass'][:]
iceberg_num = nc.variables['iceberg_num'][:]
N=len(js)
h=np.zeros(N)
# Making a list of identifying hashes
k=0
for j in js+1:
#h[k]=bergs_hash(year[j], day[j],lon[j],lat[j],mass[j])
h[k]=iceberg_num[j]
k=k+1
#Creates a sorted array of unique elements of h
h=sorted(list(set(h)))
#Create a list of bergs, named by their hash values, h
berg=[]
for i in xrange (0,len(h)):
berg.append(berg_type(h[i]))
for i in xrange(0, len(js)):
j=js[i]+1
#l=np.where(h==bergs_hash(year[j], day[j],lon[j],lat[j],mass[j]))[0]
l=np.where(h==iceberg_num[j])[0]
berg[l].year0.append(year[j])
berg[l].day0.append(day[j])
berg[l].lon0.append(lon[j])
berg[l].lat0.append(lat[j])
#berg[l].mass0.append(mass[j])
berg[l].js.append(js[i]+2)
berg[l].je.append(je[i])
#Sort each segment
for i in xrange (0,len(h)):
yd=year[berg[i].js]+(day[berg[i].js]/373)
#Sort according to descending yd, and then pull out indicies for js, and je.
berg[i].js=[berg[i].js[n] for n,_ in sorted(list(enumerate(yd)),reverse=True)]
berg[i].je=[berg[i].je[n] for n,_ in sorted(list(enumerate(yd)),reverse=True)]
bergs_handle=berg_handle(berg,nc)
return bergs_handle
###############################################################################################
################################# Beginning of Script #######################################
###############################################################################################
from netCDF4 import Dataset
import numpy as np
#from PIL import *
import math
import os
import matplotlib
matplotlib.use("GTKAgg")
import matplotlib.pyplot as plt
from pylab import *
import pdb
import argparse
#This combination allows you to access the varibale imputted from the command line.
import sys
#Clear screen
#os.system('clear')
#This is a command which puts you into testing mode so that you can troubleshoot (like a breakpoint in matlab)
#pdb.set_trace()
#If no files are given at runtime, then use the files selected here:
if len(sys.argv)==1:
Number_of_files=2 # How many files would you like to view?
#Choosing which files to use
#Weddel Sea Version
fileroot='/home/aas/Iceberg_Project/Weddell_Sea/'
dirname1='one_iceberg_testing_Verlet';
dirname2='one_iceberg_testing_RK';
filename1=fileroot+dirname1+'/iceberg_trajectories.nc'
filename2=fileroot+dirname2+'/iceberg_trajectories.nc'
#Other files
#filename1='/home/aas/Iceberg_Project/Test_data1/Quick_Global_RK15/iceberg_trajectories.nc';
#filename1= '/home/aas/Iceberg_Project/Weddell_Sea/one_iceberg_testing/iceberg_trajectories.nc'
else:
Number_of_files=len(sys.argv)-1
#Imput parameters
berg_num=0 #index of ic
#Flags and on/off switches
plot_timeseries=0
plot_trajectory=1
plot_bergsize=1
fix_horizontal_scale=0
plot_all_bergs=1 #plots all the icebergs and not just the one given by n
plot_initial_size=0
plot_size_at_start_and_finish_only=1
short_form=1 #only looks up the lat / lon
#Creating and filling up the bergs
for loop in xrange(0,Number_of_files):
if len(sys.argv)==1:
if loop==0: filename=filename1
if loop==1: filename=filename2
else:
filename=sys.argv[loop+1]
print filename
berg=[]; del berg
bergs=bergs_open(filename)
bergs=bergs_read(bergs,'width')
bergs=bergs_read(bergs,'length')
bergs=bergs_read(bergs,'lat')
bergs=bergs_read(bergs,'lon')
bergs=bergs_yearday(bergs)
N=len(bergs.berg)
if plot_all_bergs==0:
N=1
for n in xrange (0, N):
if plot_all_bergs==0:
n=berg_num
#Loading variables
lon=bergs.berg[n].lon[:]
lat=bergs.berg[n].lat[:]
width=bergs.berg[n].width[:]
length=bergs.berg[n].length[:]
yearday=bergs.berg[n].yearday[:]
#Finding the index of the initial time
initial_time_ind=sorted(list(enumerate(yearday)),key=lambda x: x[0])[-1][0]
final_time_ind=sorted(list(enumerate(yearday)),key=lambda x: x[0])[0][0]
########################### Begining plotting #################################
if plot_trajectory==1:
#plt.subplot(2,1,1)
if loop==0:
if n==1:
plt.plot(lon,lat,'ko-')
if n!=1:
plt.plot(lon,lat,'go-')
else:
plt.plot(lon,lat,'bo-')
plt.xlabel('longitude (deg)')
plt.ylabel('latitude (deg)')
plt.title('Iceberg trajectory')
plt.grid(True)
#savefig("test.png")
if plot_bergsize==1:
Radius_earth=6378.135*1000;
circ_ind=np.linspace(0,2*pi,100);
for k in xrange (0 ,len(lat)):
L_eff=sqrt(((width[k]*length[k])/pi));
plt.plot(lon[k],lat[k],'bo',linewidth=5)
#d_lat=(L_eff/Radius_earth)*(180/pi);
#d_lon=L_eff/(111.320*(10**(3))*cos(lat[k]*pi/180));
d_lat=(L_eff/Radius_earth)*(180/pi);
d_lon=(L_eff/(Radius_earth*cos(lat[k]*pi/180)))*(180/pi);
if plot_size_at_start_and_finish_only==0:
plt.plot(lon[k]+(d_lon*cos(circ_ind)),lat[k]+(d_lat*sin(circ_ind)),'b');
if k==initial_time_ind and plot_initial_size==1:
plt.plot(lon[k]+(d_lon*cos(circ_ind)),lat[k]+(d_lat*sin(circ_ind)),'r');
if k==final_time_ind:
plt.plot(lon[k]+(d_lon*cos(circ_ind)),lat[k]+(d_lat*sin(circ_ind)),'r');
#Setting axes to have equal distance
if fix_horizontal_scale==1:
y_scale=(max(lat)-min(lat))*(Radius_earth*pi/180)
x_scale=(max(lon)-min(lon))*(111.320*(10**(3))*cos(np.mean(lat)/180*pi))
d=0.6*max(x_scale,y_scale)
d_lat=d*(180/(Radius_earth*pi))
d_lon=d/(111.320*(10**(3))*cos(np.mean(lat)*pi/180))
plt.xlim([((min(lon)+max(lon))/2)-d_lon,((min(lon)+max(lon))/2)+d_lon])
plt.ylim([((min(lat)+max(lat))/2)-d_lat,((min(lat)+max(lat))/2)+d_lat])
plt.plot(lon[initial_time_ind],lat[initial_time_ind],'ro')
plt.show()
if plot_timeseries==1:
n=0
#plt.subplot(2,1,2)
plt.plot(yearday,lon,'ko-')
plt.xlabel('yearday')
plt.ylabel('lon (deg)')
plt.grid(True)
plt.savefig("test7.png")
plt.show()
print 'Script complete'