/
dccnpath.m
218 lines (193 loc) · 7.13 KB
/
dccnpath.m
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
function filename = dccnpath(filename)
% DCCNPATH manages the filename and path for test files. It helps to locate and read
% test file from Linux, Windows or macOS computers both inside and outside the DCCN.
%
% Use as
% filename = dccnpath(filename)
% where the input filename corresponds to the test data on the DCCN cluster and the
% output filename corresponds to the local file including the full path where the
% test data is available.
%
% The test data location on the DCCN cluster is '/home/common/matlab/fieldtrip/data'
% and the specification of the input filename MUST start with this.
%
% This function will search-and-replace the location on the DCCN cluster by the
% location that applies to your computer. If needed, it will replace '/home' by 'H:'
% and will replace forward by backward slashes.
%
% In case you have a local copy of the data, or if you are inside the DCCN and have
% mounted the '/home' drive on another letter than 'H:', you should override the
% default location using
% global ft_default
% ft_default.dccnpath = '/your/copy';
%
% If you do not have a local copy and do not define ft_default.dccnpath manually,
% then dccnpath will automatically use a temporary directory and try to download the
% data.
%
% See also WHICH, WEBSAVE
% Copyright (C) 2012-2023, Donders Centre for Cognitive Neuroimaging, Nijmegen, NL
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
global ft_default
% it must always start with this
assert(startsWith(filename, '/home/common/matlab/fieldtrip/data'));
% alternative0 applies inside the DCCN network when using the standard data location
if ispc
alternative0 = strrep(filename, '/home', 'H:');
alternative0 = strrep(alternative0, '/', '\');
else
alternative0 = strrep(filename, 'H:', '/home');
alternative0 = strrep(alternative0, '\', '/');
end
if exist(alternative0, 'file') || exist(alternative0, 'dir')
ft_notice('using default DCCN path %s', alternative0);
filename = alternative0;
return
end
% the simple alternative1 applies with a local file in the present working directory
% this is often convenient when initially setting up a new test script while the data is not yet uploaded
[p, f, x] = fileparts(alternative0);
alternative1 = [f x];
skip = {
% subdirectories like fieldtrip/xxx
'bin'
'compat'
'connectivity'
'contrib'
'external'
'fileio'
'forward'
'inverse'
'plotting'
'preproc'
'private'
'qsub'
'realtime'
'specest'
'src'
'statfun'
'template'
'test'
'trialfun'
'utilities'
% subdirectories like fieldtrip/template/xxx
'dewar'
'headmodel'
'sourcemodel'
'anatomy'
'electrode'
'layout'
'atlas'
'gradiometer'
'neighbours'
};
if any(strcmp(alternative1, skip))
% this should not be used for subdirectories underneath fieldtrip
warning('the simple alternative1 cannot be used for "%s"', alternative1)
alternative1 = '';
end
if exist(alternative1, 'file') || exist(alternative1, 'dir')
if ~isempty(x) && ~isequal(x, '.ds') % if alternative1 is a file
filenamepath = which(alternative1); % also output the path that alternative1 is located at
ft_notice('using present working directory %s', filenamepath);
filename = filenamepath;
return;
else % if alternative1 is a folder
searchPath = path; % get the MATLAB search path
directories = strsplit(searchPath, pathsep); % split the search path into individual directories
folderPath = '';
for i = 1:numel(directories)
currentFolder = directories{i};
if contains(currentFolder, alternative1)
folderPath = currentFolder;
break
end
end
warning('using present working directory %s', folderPath);
filename = folderPath;
return
end
end
% alternative2 applies when ft_default.dccnpath is specified
% see https://github.com/fieldtrip/fieldtrip/issues/1998
if ~isfield(ft_default, 'dccnpath') || isempty(ft_default.dccnpath)
ft_notice('using a temporary directory')
ft_default.dccnpath = tempdir;
end
% we do not want it to end with a '/' or '\'
ft_default.dccnpath = strip(ft_default.dccnpath, 'right', '/');
ft_default.dccnpath = strip(ft_default.dccnpath, 'right', '\');
% alternative0 is the same as the input filename, but potentially updated for windows
if ~ispc
alternative2 = strrep(alternative0, '/home/common/matlab/fieldtrip/data', ft_default.dccnpath);
else
alternative2 = strrep(alternative0, 'H:\common\matlab\fieldtrip\data', ft_default.dccnpath);
end
if exist(alternative2, 'file')
ft_notice('using local copy %s ', alternative2);
filename = alternative2;
return
elseif isfolder(alternative2) && ~isemptydir(alternative2)
ft_notice('using local copy %s ', alternative2);
filename = alternative2;
return
else
% if the file doesn't exist or the folder is empty, then download test data
% see also UNTAR, UNZIP, GUNZIP, which can download on the fly
if contains(alternative0, 'data/test') || contains(alternative0, 'data\test')
error('the test data are private and can not be downloaded from https://download.fieldtriptoolbox.org')
end
% public data are downloaded from https://download.fieldtriptoolbox.org
% so, we need to find the right path to the HTTPS download server
pattern = 'ftp(.*)';
datadir = regexp(alternative0, pattern, 'tokens', 'once');
if iscell(datadir)
datadir = datadir{1};
end
weblocation = strcat('https://download.fieldtriptoolbox.org', datadir);
weblocation = strrep(weblocation, '\', '/');
urlContent = webread(weblocation, weboptions('ContentType', 'text'));
if contains(urlContent, '<html')
% the URL corresponds to a folder
ft_notice('downloading recursively from %s', weblocation);
recursive_download(weblocation, alternative2);
filename = alternative2;
else
% the URL corresponds to a file
% create the necessary directories if they do not exist
[p, f, x] = fileparts(alternative2);
if ~isfolder(p)
mkdir(p);
end
if isfolder(alternative2)
[p, f, x] = fileparts(alternative0);
alternative2 = fullfile(alternative2, [f x]);
end
ft_notice('downloading recursively from %s', weblocation);
websave(alternative2, weblocation);
filename = alternative2;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% SUBFUNCTION
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function bool = isemptydir(dirName)
dirContents = dir(dirName);
bool = ~isempty(dirContents(~ismember({dirContents.name}, {'.', '..'})));