Skip to content

Commit 0c34e97

Browse files
committed
Refactored downloading of index files
1 parent 5580f87 commit 0c34e97

File tree

2 files changed

+71
-47
lines changed

2 files changed

+71
-47
lines changed

auxil/download_index.m

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function success = download_index(file_name, file_type)
2+
% download_index This function is part of the
3+
% MATLAB toolbox for accessing BGC Argo float data.
4+
%
5+
% USAGE:
6+
% success = download_index(file_name, file_type)
7+
%
8+
% DESCRIPTION:
9+
% This function downloads the specified index file from the GDAC if
10+
% necessary. If the gzipped file is downloaded, it will be unzipped.
11+
%
12+
% INPUTS:
13+
% file_name : name of the index file (without path)
14+
% file_type : type of the index file, e.g. 'Sprof', 'prof', or 'meta'
15+
%
16+
% OUTPUTS:
17+
% success : 1 if the file was downloaded successfully or did not
18+
% need to be downloaded; 0 if it could not be downloaded
19+
%
20+
% AUTHORS:
21+
% H. Frenzel, J. Sharp, A. Fassbender (NOAA-PMEL), N. Buzby (UW),
22+
% J. Plant, T. Maurer, Y. Takeshita (MBARI), D. Nicholson (WHOI),
23+
% and A. Gray (UW)
24+
%
25+
% CITATION:
26+
% H. Frenzel*, J. Sharp*, A. Fassbender, N. Buzby, J. Plant, T. Maurer,
27+
% Y. Takeshita, D. Nicholson, A. Gray, 2021. BGC-Argo-Mat: A MATLAB
28+
% toolbox for accessing and visualizing Biogeochemical Argo data.
29+
% Zenodo. https://doi.org/10.5281/zenodo.4971318.
30+
% (*These authors contributed equally to the code.)
31+
%
32+
% LICENSE: bgc_argo_mat_license.m
33+
%
34+
% DATE: FEBRUARY 22, 2022 (Version 1.2)
35+
36+
global Settings;
37+
38+
dest_path = [Settings.index_dir, file_name];
39+
40+
if do_download(dest_path)
41+
if Settings.verbose
42+
fprintf('%s index file will now be downloaded.\n', file_type);
43+
end
44+
if ~try_download(file_name, dest_path)
45+
success = 0;
46+
return;
47+
end
48+
if endsWith(file_name, '.gz')
49+
gunzip(dest_path)
50+
end
51+
else
52+
if endsWith(file_name, '.gz')
53+
gunzip_file = strrep(dest_path, '.gz', '');
54+
if ~exist(gunzip_file, 'file')
55+
gunzip(dest_path)
56+
end
57+
end
58+
end
59+
success = 1;

initialize_argo.m

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -145,29 +145,17 @@ function initialize_argo()
145145
'jma'; 'kma'; 'kordi'; 'meds'};
146146

147147
% Write Sprof index file from GDAC to Index directory
148-
sprof = 'argo_synthetic-profile_index.txt'; % file used locally
149-
sprof_gz = [sprof, '.gz']; % file that is available at GDAC
150-
Settings.dest_path_sprof = [Settings.index_dir, sprof];
151-
dest_path_sprof_gz = [Settings.index_dir, sprof_gz];
152-
if do_download(dest_path_sprof_gz)
153-
if Settings.verbose
154-
disp('Sprof index file will now be downloaded.')
155-
disp('Depending on your internet connection, this may take a while.')
156-
end
157-
if ~try_download(sprof_gz, dest_path_sprof_gz)
158-
error('Sprof index file could not be downloaded')
159-
end
160-
gunzip(dest_path_sprof_gz)
161-
elseif ~exist(Settings.dest_path_sprof, 'file')
162-
gunzip(dest_path_sprof_gz)
148+
sprof = 'argo_synthetic-profile_index.txt'; % file at GDAC
149+
if ~download_index([sprof, '.gz'], 'Sprof')
150+
error('Sprof index file could not be downloaded')
163151
end
164152

165153
% Extract information from Sprof index file
166154
% NOTE that some quantities will be kept per float (struct Float):
167155
% file_path, file_name, dac, params, wmoid, update
168156
% Others will be kept per profile (struct Sprof):
169157
% date, lat, lon, sens(ors), data_mode
170-
fid = fopen(Settings.dest_path_sprof);
158+
fid = fopen([Settings.index_dir, sprof]);
171159
H = textscan(fid,'%s %s %f %f %s %d %s %s %s %s','headerlines',9,...
172160
'delimiter',',','whitespace','');
173161
fclose(fid);
@@ -207,39 +195,22 @@ function initialize_argo()
207195
Sprof.wmo = regexp(sprof_urls,'\d{7}','once','match');
208196
[uwmo_sprof,ia] = unique(Sprof.wmo,'stable'); % keep list order
209197
Sprof.wmo = str2double(Sprof.wmo);
210-
ulist = sprof_urls(ia);
211-
dacs = regexp(ulist(:,1),'^\w+','once','match');
212-
Sprof_fnames = regexprep(uwmo_sprof,'\d{7}','$0_Sprof.nc');
213-
tmp = regexprep(ulist(:,1),'profiles.+','');
214-
Sprof_fp = strcat(tmp,Sprof_fnames);
215198
ia(end+1) = length(sprof_urls) + 1;
216199
bgc_prof_idx1 = ia(1:end-1);
217200
bgc_prof_idx2 = ia(2:end) - 1;
218201

219202
% Write prof index file from GDAC to Index directory
220-
prof = 'ar_index_global_prof.txt'; % file used locally
221-
prof_gz = [prof, '.gz']; % gzipped file that is available at GDAC
222-
Settings.dest_path_prof = [Settings.index_dir, prof];
223-
dest_path_prof_gz = [Settings.index_dir, prof_gz];
224-
if do_download(dest_path_prof_gz)
225-
if Settings.verbose
226-
disp('prof index file will now be downloaded.')
227-
disp('Depending on your internet connection, this may take a while.')
228-
end
229-
if ~try_download(prof_gz, dest_path_prof_gz)
230-
error('prof index file could not be downloaded')
231-
end
232-
gunzip(dest_path_prof_gz)
233-
elseif ~exist(Settings.dest_path_prof, 'file')
234-
gunzip(dest_path_prof_gz)
203+
prof = 'ar_index_global_prof.txt'; % file at GDAC
204+
if ~download_index([prof, '.gz'], 'prof')
205+
error('prof index file could not be downloaded')
235206
end
236207

237208
% Extract information from prof index file
238209
% NOTE that some quantities will be kept per float (struct Float):
239210
% file_path, file_name, dac, params, wmoid, update
240211
% Others will be kept per profile (struct Prof):
241212
% date, lat, lon, sens(ors), data_mode
242-
fid = fopen(Settings.dest_path_prof);
213+
fid = fopen([Settings.index_dir, prof]);
243214
H = textscan(fid,'%s %s %f %f %s %d %s %s','headerlines',9,...
244215
'delimiter',',','whitespace','');
245216
fclose(fid);
@@ -362,20 +333,14 @@ function initialize_argo()
362333
Sprof.date_update(bgc_prof_idx2(is_true_bgc==1));
363334

364335
% Write meta index file from GDAC to Index directory
365-
% Since it is rather small, download the uncompressed file
336+
% Since it is rather small, download the uncompressed file directly
366337
meta = 'ar_index_global_meta.txt';
367-
Settings.dest_path_meta = [Settings.index_dir, meta];
368-
if do_download(Settings.dest_path_meta)
369-
if Settings.verbose
370-
disp('meta index file will now be downloaded.')
371-
end
372-
if ~try_download(meta, Settings.dest_path_meta)
373-
error('meta index file could not be downloaded')
374-
end
338+
if ~download_index(meta, 'meta')
339+
error('meta index file could not be downloaded')
375340
end
376341

377342
% Extract information from meta index file
378-
fid = fopen(Settings.dest_path_meta);
343+
fid = fopen([Settings.index_dir, meta]);
379344
H = textscan(fid,'%s %s %s %s','headerlines',9,...
380345
'delimiter',',','whitespace','');
381346
fclose(fid);

0 commit comments

Comments
 (0)