-
Notifications
You must be signed in to change notification settings - Fork 6
/
Main_workshop.R
412 lines (303 loc) · 14.4 KB
/
Main_workshop.R
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# Main_workshop.R
# Driver routine for the GO-BGC workshop R tutorial
# June 28-30, 2021 (https://www.youtube.com/watch?v=w_6pEGNXQQ4)
# Demonstrates the downloading of BGC-Argo float data with sample plots,
# a discussion of available data, quality control flags etc.
#
# UPDATE RECORD:
# Version 1 & 2: June 2021
# Version 2.1: January 2022
# Version 3.0: May 2022
#
# CITATION:
# M. Cornec (LOV, now at NOAA-PMEL), Y. Huang (NOAA-PMEL), Q. Jutard (OSU ECCE TERRA), R. Sauzede (IMEV) and
# C. Schmechtig (OSU ECCE TERRA), 2021.
# BGC-Argo-R: A R toolbox for accessing and visualizing Biogeochemical Argo data.
# Zenodo. http://doi.org/10.5281/zenodo.5028138
# Close figures, clean up workspace, clear command window
cat("\014")
rm(list = ls())
# Fill here the path to the code directory, you can instead set the code
# directory as the working directory with setwd()
path_code = ""
# Load the functions and libraries--------------------------------
setwd(path_code)
func.sources = list.files(path_code,pattern="*.R")
func.sources = func.sources[which(func.sources %in% c('Main_workshop.R',
"bgc_argo_workshop_R_license.R")==F)]
if(length(grep("Rproj",func.sources))!=0){
func.sources = func.sources[-grep("Rproj",func.sources)]
}
invisible(sapply(paste0(func.sources),source,.GlobalEnv))
aux.func.sources = list.files(paste0(path_code,"/auxil"),pattern="*.R")
invisible(sapply(paste0(path_code,"/auxil/",aux.func.sources),source,.GlobalEnv))
# Exercise 0: Initialize --------------------------------------------------
# This function defines standard settings and paths and creates Index
# and Profiles folders in your current path. It also downloads the Sprof
# index file from the GDAC to your Index folder. The Sprof index is
# referenced when downloading and subsetting float data based on user
# specified criteria in other functions.
initialize_argo() # Take some minutes to download the global Index
do_pause()
## Examine global structures
# These global structures contain a variety of useful variables for
# downloading and manipulating float data. 'Sprof' contains fields
# with information for each profile, 'Float' contains fields with
# information for each float, 'Settings' contains settings to be used in
# the backgroud during plotting, etc. Variables in the global structures
# can be altered within the initialize_argo.m file.
# Example: Look at the profile ID numbers and available sensors for the
# profiles that have been executed by new GO-BGC float #5906439.
float_idx <-which(Float$wmoid=='5906439') # float IDs for float #5906439 in the S_file index
float_idx
prof_ids = c(Float$prof_idx1[float_idx]:Float$prof_idx2[float_idx]) # profile IDs for float #5906439 in the S_file index
prof_ids
dates = Sprof$date[prof_ids] # dates of each profile from float #5906439
dates
sensors = unique(Sprof$sens[prof_ids]) # sensors available for float #5906439
sensors
do_pause()
rm (list= c("float_idx","prof_ids","sensors","dates")) # clean up workspace
# Exercise 1: SOCCOM float ------------------------------------------------
# In this exercise, we download the NetCDF file for a Southern Ocean
# BGC float, inspect its contents, show the trajectory, plot profiles
# for unadjusted and adjusted data, and show the effect of adjustments
# made to the nitrate concentrations.
# Download NetCDF file for float #5904183, a SOCCOM float with multiple seasons under ice
WMO = 5904859
success = download_float(WMO)
# Display attributes, dimensions, and variables available in the NetCDF
float_file = nc_open(paste0("Profiles/", WMO,"_Sprof.nc"))
float_file
# Extract informational data from the NetCDF
names (float_file$var)
# Close the file
nc_close(float_file)
do_pause()
# We see that NITRATE is available, so load it (along with TEMP and PSAL) from the NetCDF
data = load_float_data( float_ids= WMO, # specify WMO number
variables=c('PSAL','TEMP','NITRATE') # specify variables
)
names(data$Data[[paste0('F', WMO)]]) # show data that has been loaded into R
do_pause()
# Load the float data in the R with the format of data frame if "format" is specificed
data_df = load_float_data( float_ids= WMO, # specify WMO number
variables=c('PSAL','TEMP','NITRATE'), # specify variables,
format="dataframe" # specify format;
)
colnames(data_df) # show data that has been loaded into R
# Show the trajectory of the downloaded float
show_trajectories(float_ids=WMO,
return_ggplot="True" # return the plot to ggplot panel
)
do_pause()
# Show all profiles for salinity and nitrate from the downloaded float
# this plots the raw, unadjusted data, and includes multiple profiles
# compromised by biofouling that has affected the optics.
show_profiles( float_ids=WMO,
variables=c('PSAL','NITRATE'),
obs='on', # 'on' shows points on the profile at which each measurement was made
raw="yes" # show the unadjusted data ,
)
# this plots the adjusted data.
show_profiles(float_ids=WMO,
variables=c('PSAL','NITRATE'),
obs='on', # 'on' shows points on the profile at which each measurement was made
raw="no",
)
# this plots the adjusted, good (qc flag 1) and probably-good (qc flag 2) data.
show_profiles(float_ids = WMO,
variables=c('PSAL','NITRATE'),
obs='on', # 'on' shows points on the profile at which each measurement was made
qc_flags =c(1:2) # tells the function to plot good and probably-good data
)
do_pause()
# Show sections for nitrate
# this shows the raw, unadjusted data (pcolor plot)
# mixed layer depth is shown based on the temperature threshold
# (set the value to 2 after 'mld' to use the density threshold instead)
show_sections(float_ids=WMO,
variables= c('NITRATE'),
plot_mld=1, # tells the function to plot mixed layer depth
raw="yes") # tells the function to plot raw data
show_sections( float_ids=WMO,
variables= c('NITRATE'),
plot_mld=1, #tells the function to plot mixed layer depth
raw="no") # tells the function to plot adjusted data (that is the default and could be left out in this call)
show_sections( float_ids=WMO ,
variables= c('NITRATE'),
plot_mld=1, #tells the function to plot mixed layer depth
raw="no",
qc=c(1:2) # tells the function to plot good and probably-good data
)
do_pause()
## Clean up the workspace
cat("\014")
rm (list= c("data","float_file","success","WMO","data_df"))
# Exercise 2: Ocean Station Papa floats -----------------------------------
# In this exercise, we define a region in the Northeast Pacific along with
# a duration of time, and identify the float profiles matching that
# criteria. We show the trajectories of all the matching floats and plot
# profiles that match the criteria for one of the floats.
# Set limits near Ocean Station Papa from 2008 to 2018
lat_lim=c(45, 60)
lon_lim=c(-150, -135)
start_date="2008-01-01"
end_date="2018-12-31"
# Select profiles based on those limits with specified sensor (NITRATE)
OSP_data= select_profiles ( lon_lim,
lat_lim,
start_date,
end_date,
sensor=c('NITRATE'), # this selects only floats with nitrate sensors
outside="both" # All floats that cross into the time/space limits
) # are identified from the Sprof index. The optional
# 'outside' argument allows the user to specify
# whether to retain profiles from those floats that
# lie outside the space limits ('space'), time
# limits ('time'), both time and space limits
# ('both'), or to exclude all profiles that fall
# outside the limits ('none'). The default is 'none'
# Display the number of matching floats and profiles
print(paste('# of matching profiles:',sum(lengths(OSP_data$float_profs))))
print(paste('# of matching floats:',length(OSP_data$float_ids)))
# Load the data for the matching float with format of data frame
data_OSP_df= load_float_data( float_ids= OSP_data$float_ids, # specify WMO number
float_profs=OSP_data$float_profs, # specify selected profiles
variables="ALL", # load all the variables
format="dataframe" # specify format;
)
# Show trajectories for the matching floats
# This function downloads the specified floats from the GDAC (unless the
# files have already been downloaded) and then loads the data for plotting.
# Adding the optional input pair 'color','multiple' will plot different
# floats in different colors
trajectory = show_trajectories(float_ids = OSP_data$float_ids,
return_ggplot = TRUE #do not plot and return a ggplot object
) # this plots different floats in different colors
x11()# create a new window
plot(trajectory) # plot the ggplot object
# show domain of interest
trajectory = trajectory + geom_rect( aes(xmin = lon_lim[1],
xmax = lon_lim[2],
ymin = lat_lim[1],
ymax = lat_lim[2]),
color="black",fill=NA
)
x11()# create a new window
plot(trajectory) # plot the ggplot object
do_pause()
# Show profile plots for the first of these matching floats
# Case #1: all profiles from one float (1)
show_profiles(float_ids=OSP_data$float_ids[1],
variables=c('PSAL','DOXY'),
obs='on',# 'on' shows points on the profile at which
# each measurement was made
)
# Case #2: mean and standard deviation of all profiles from one float (1)
show_profiles(float_ids=OSP_data$float_ids[1],
variables=c('PSAL','DOXY'),
obs='on', # 'on' shows points on the profile at which
# each measurement was made
method="mean" # this tells the function to just plot the mean profile
)
do_pause()