-
Notifications
You must be signed in to change notification settings - Fork 1
/
ST_Mbaysalt.m
781 lines (734 loc) · 30.1 KB
/
ST_Mbaysalt.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
function ST_Mbaysalt(varargin)
% To setup path of Mbaysalt
% =================================================================================================================
% Parameters:
% varargin: (optional)
% add: add all path || required: False|| type: Text || format: 'add'
% rm: remove all path || required: False|| type: Text || format: 'rm'
% cd: cd here || required: False|| type: Text || format: 'cd'
% noclone: Not add new pkgs || required: False|| type: flag || format: 'noclone'
% init: Initialize || required: False|| type: flag || format: 'init'
% *.json: INSTALL JsonFile || required: False|| type: positional || format: './INSTALL.json'
% =================================================================================================================
% Returns:
% =================================================================================================================
% Updates:
% ****-**-**: See Mainpath.m
% 2024-04-26: Code Refactoring, by Christmas;
% 2024-05-12: Added check git:mirror, by Christmas;
% 2024-05-12: Added improve:minmax, submodule gitclone:irfu_matlab, by Christmas;
% 2024-05-12: Judged init first, by Christmas;
% 2024-05-16: Added improve:m_etopo2, by Christmas;
% 2024-06-15: Added improve:matFigure, by Christmas;
% =================================================================================================================
% Examples:
% ST_Mbaysalt % Add all path
% ST_Mbaysalt('add') % Add all path
% ST_Mbaysalt('rm') % Remove all path
% ST_Mbaysalt('noclone') % Add all path without clone git
% ST_Mbaysalt('cd') % Change current directory to the path of this function
% ST_Mbaysalt('init') % Initialize
% ST_Mbaysalt('add','init') % Initialize and add all path
% ST_Mbaysalt('add','./INSTALL.json') % Initialize and add all path
% =================================================================================================================
arguments(Input, Repeating)
varargin
end
cmd = 'add'; % 默认是 add
for i = 1: length(varargin)
switch lower(varargin{i})
case {'add','rm','noclone'}
cmd = convertStringsToChars(varargin{i});
varargin(i) = [];
break
case {'cd'} % cd to the path of this function
cd(fileparts(mfilename("fullpath")))
return
end
end
if ispref('Mbaysalt','init')
init = getpref('Mbaysalt','init');
if strcmp(init,'DONE')
init = false;
end
else
init = true;
end
for i = 1 : length(varargin)
switch lower(varargin{i})
case 'init'
init = true;
otherwise
if endsWith(varargin{i},'.json')
Jfile = varargin{i};
end
end
end
clear i
PATH.basepath = fileparts(mfilename("fullpath"));
if ~exist("Jfile","var")
if exist('./INSTALL.json','file')
Jfile = './INSTALL.json';
else
Jfile = fullfile(PATH.basepath,'Configurefiles/INSTALL.json');
end
end
Jstruct = jsondecode(fileread(Jfile)); Jstruct.FILEPATH = Jfile;
PATH.modules = fullfile(PATH.basepath,Jstruct.packages.modules.PATH); % modules
PATH.builtin = fullfile(PATH.basepath,Jstruct.packages.builtin.PATH); % builtin
STATUS = 0;
switch lower(cmd)
case 'add'
% addpath
addpath(strjoin(PATH.modules, pathsep)); % cellfun(@addpath, PATH.module); 慢
addpath(strjoin(PATH.builtin, pathsep));
Jstruct.git.TF = true;
[~, ~] = install_pkgs(PATH, Jstruct, 'add'); % install_pkgs
if init
STATUS = Fixed_functions(Jstruct);
end
Javaaddpath(Jstruct)
case 'rm'
% rmpath
[~, PATH] = install_pkgs(PATH, Jstruct, 'rm');
Crmpath(PATH.modules) % rmpath(strjoin(PATH.modules, pathsep))
Crmpath(PATH.builtin) % rmpath(strjoin(PATH.builtin, pathsep))
Crmpath(PATH.exfunctions.download)
Crmpath(PATH.exfunctions.gitclone)
case 'noclone'
addpath(strjoin(PATH.modules, pathsep));
addpath(strjoin(PATH.builtin, pathsep));
Jstruct.git.TF = false;
[~, ~] = install_pkgs(PATH, Jstruct, 'noclone');
if init
STATUS = Fixed_functions(Jstruct);
end
Javaaddpath(Jstruct)
otherwise
error('parameter error');
end
if STATUS
print_info()
end
if init
if ispref('Mbaysalt','PATH_toolbox') % Fixed Mainpath
rmpref('Mbaysalt');
end
setpref('Mbaysalt','init','DONE')
end
end
function Crmpath(Path)
% currentPaths = strsplit(path, pathsep);
% cellfun(@rmpath, Path(cellfun(@(x) ismember(x, currentPaths), Path)));
% exception = warning('on','last');
% identifier = exception.identifier;
identifier = 'MATLAB:rmpath:DirNotFound';
warning('off',identifier);
% FunD = cellfun(@genpath2,FunD,repmat({'.git'},length(FunD),1),'UniformOutput', false);
FunD = cellfun(@genpath2,Path,repmat({{'.git', '.svn'}},length(Path),1),'UniformOutput', false);
FunD (cellfun(@isempty,FunD))= [];
rmpath(strjoin(FunD))
warning('on',identifier);
end
function Javaaddpath(Jstruct)
if exist('setup_nctoolbox_java','file') == 2 && Jstruct.packages.gitclone.nctoolbox.SETPATH
setup_nctoolbox_java()
end
end
function STATUS = Fixed_functions(Jstruct)
% 修正一些函数在高版本matlab中的报错
STATUS_list = [];
if Jstruct.improve.t_tide
STATUS_list = [STATUS_list,fixed_t_tide(Jstruct)]; % 添加输出ref基准面的信息
end
if Jstruct.improve.setup_nctoolbox_java
STATUS_list = [STATUS_list,fixed_setup_nctoolbox_java(Jstruct)]; % 关闭setup_nctoolbox_java的一行,MATLAB高版本会报错
end
if Jstruct.improve.matFVCOM
STATUS_list = [STATUS_list,supplement_matFVCOM(Jstruct)];
end
if Jstruct.improve.mexcdf
STATUS_list = [STATUS_list,fixed_mexcdf(Jstruct)];
end
if Jstruct.improve.m_gshhs
STATUS_list = [STATUS_list,fixed_m_gshhs(Jstruct)];
end
if Jstruct.improve.m_etopo2
STATUS_list = [STATUS_list,fixed_m_etopo2(Jstruct)];
end
if Jstruct.improve.ann_wrapper
STATUS_list = [STATUS_list,install_ann_wrapper(Jstruct)];
end
if Jstruct.improve.DHIMIKE
STATUS_list = [STATUS_list,install_DHIMIKE(Jstruct)];
end
if Jstruct.improve.irfu_matlab
STATUS_list = [STATUS_list,install_irfu_matlab(Jstruct)];
end
if Jstruct.improve.matFigure
STATUS_list = [STATUS_list,supplement_matFigure(Jstruct)];
end
STATUS = any(STATUS_list);
end
function STATUS = fixed_t_tide(Jstruct)
% 为t_tide工具包的t_tide.m文件添加ref参数
m_filepath = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.download.t_tide.PATH,'t_tide.m'); % which('t_tide.m');
STATUS = 0;
if ~exist(m_filepath,"file")
return
end
% 备份源文件
path_DIR = fileparts(m_filepath);
m_filecopy = fullfile(path_DIR,'t_tide_origin.m');
if ~ exist(m_filecopy,'file')
copyfile(m_filepath,m_filecopy);
end
fileContent = fileread(m_filepath); % 读取文件内容
searchStr = "nameu = struct('name',nameu,'freq',fu,'tidecon',tidecon,'type',ltype);"; % 定义要查找的字符串
replaceStr = "nameu = struct('name',nameu,'freq',fu,'tidecon',tidecon,'type',ltype,'ref',z0);"; % 定义替换后的字符串
if ~contains(fileContent, searchStr) % 不需要替换
return
end
newContent = strrep(fileContent, searchStr, replaceStr); % 执行替换操作
fOWC(m_filepath, 'w', newContent);
STATUS = 1;
end
function STATUS = fixed_setup_nctoolbox_java(Jstruct)
% 修正nctoolbox工具包的setup_nctoolbox_java.m函数在高版本matlab中的报错
m_filepath = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.gitclone.nctoolbox.PATH,'java/setup_nctoolbox_java.m'); % which('setup_nctoolbox_java.m');
STATUS = 0;
if ~exist(m_filepath,"file")
return
end
% 备份源文件
path_DIR = fileparts(m_filepath);
m_filecopy = fullfile(path_DIR,'setup_nctoolbox_java_origin.m');
if ~exist(m_filecopy,'file')
copyfile(m_filepath,m_filecopy);
end
fileContent = fileread(m_filepath); % 读取文件内容
if contains(fileContent, '% root.addAppender')
return
end
pattern = '(?m)^(?<!%)(root\.addAppender\(org\.apache\.log4j\.ConsoleAppender\(org\.apache\.log4j\.PatternLayout\(''%d\{ISO8601\} \[\%t\] %-5p %c %x - %m%n''\)\)\);)';
replacement = '% $1';
newContent = regexprep(fileContent, pattern, replacement);
fOWC(m_filepath, 'w', newContent);
STATUS = 1;
end
function STATUS = supplement_matFVCOM(Jstruct)
% 为matFVCOM添加Contents.m和functionSignatures.json
m_filepath = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.gitclone.matFVCOM.PATH,'f_load_grid.m'); % which('f_load_grid.m');
if ~exist(m_filepath,"file")
STATUS = 0;
return
end
basepath = fileparts(fileparts(Jstruct.FILEPATH));
path_in = fullfile(basepath,Jstruct.supplement.matFVCOM.PATH_IN);
path_out = fullfile(basepath,Jstruct.supplement.matFVCOM.PATH_OUT);
files_in = fullfile(path_in,string(Jstruct.supplement.matFVCOM.FILES));
files_out = fullfile(path_out,string(Jstruct.supplement.matFVCOM.FILES));
STATUS_list = zeros(length(files_in),1);
for i = 1 : length(files_in)
file_in = files_in(i);
file_out = files_out(i);
% file_basename = replace(file_out,strcat(fileparts(file_out),filesep),'');
% file_basename = Jstruct.supplement.matFVCOM.FILES{i};
if ~exist(file_out,"file")
if strcmp(computer("arch"),"maca64") && strcmp(getHome(),'/Users/christmas')
% only for MacBook test
str = sprintf('ln -sf %s %s', file_in, file_out);
system(str,"-echo");
else
copyfile(file_in,file_out);
STATUS_list(i) = 1;
end
else
if ~readlink(file_out)
file_out_bak = strcat(file_out,'_bak');
if ~exist(file_out_bak,"file") % backup
copyfile(file_out,file_out_bak);
end
if ~strcmp(fileread(file_in), fileread(file_out))
copyfile(file_in,file_out);
STATUS_list(i) = 1;
else
STATUS_list(i) = 0;
end
end
end
end
STATUS = any(STATUS_list);
% T = validateFunctionSignaturesJSON(fullfile(path_matFVCOM,'functionSignatures.json'));
% if ~isempty(T)
% validateFunctionSignaturesJSON(fullfile(path_matFVCOM,'functionSignatures.json'));
% end
end
function STATUS = supplement_matFigure(Jstruct)
% 为matFigure添加Contents.m和functionSignatures.json
m_filepath = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.gitclone.matFigure.PATH,'cm_disp.m'); % which('cm_disp.m');
if ~exist(m_filepath,"file")
STATUS = 0;
return
end
basepath = fileparts(fileparts(Jstruct.FILEPATH));
path_in = fullfile(basepath,Jstruct.supplement.matFigure.PATH_IN);
path_out = fullfile(basepath,Jstruct.supplement.matFigure.PATH_OUT);
files_in = fullfile(path_in,string(Jstruct.supplement.matFigure.FILES));
files_out = fullfile(path_out,string(Jstruct.supplement.matFigure.FILES));
STATUS_list = zeros(length(files_in),1);
for i = 1 : length(files_in)
file_in = files_in(i);
file_out = files_out(i);
% file_basename = replace(file_out,strcat(fileparts(file_out),filesep),'');
% file_basename = Jstruct.supplement.matFigure.FILES{i};
if ~exist(file_out,"file")
if strcmp(computer("arch"),"maca64") && strcmp(getHome(),'/Users/christmas')
% only for MacBook test
str = sprintf('ln -sf %s %s', file_in, file_out);
system(str,"-echo");
else
copyfile(file_in,file_out);
STATUS_list(i) = 1;
end
else
if ~readlink(file_out)
file_out_bak = strcat(file_out,'_bak');
if ~exist(file_out_bak,"file") % backup
copyfile(file_out,file_out_bak);
end
if ~strcmp(fileread(file_in), fileread(file_out))
copyfile(file_in,file_out);
STATUS_list(i) = 1;
else
STATUS_list(i) = 0;
end
end
end
end
STATUS = any(STATUS_list);
% T = validateFunctionSignaturesJSON(fullfile(path_matFVCOM,'functionSignatures.json'));
% if ~isempty(T)
% validateFunctionSignaturesJSON(fullfile(path_matFVCOM,'functionSignatures.json'));
% end
end
function STATUS = fixed_mexcdf(Jstruct)
% 修复mexcdf工具包的ncmex.m在MATLAB高版本的报错
m_filepath = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.download.mexcdf.PATH,'netcdf_toolbox/netcdf/ncutility/ncmex.m'); % which('ncmex.m');
if ~exist(m_filepath,"file")
STATUS = 0;
return
end
% 备份源文件
path_DIR = fileparts(m_filepath);
m_filecopy = fullfile(path_DIR,'ncmex_origin.m');
if ~exist(m_filecopy,'file')
copyfile(m_filepath,m_filecopy);
end
fileContent = fileread(m_filepath); % 读取文件内容
searchStr = "error(' ## Unrecognized Matlab version.')"; % 定义要查找的字符串
replaceStr = "fcn = 'mexcdf53';"; % 定义替换后的字符串
if ~contains(fileContent, searchStr) % 不需要替换
return
end
newContent = strrep(fileContent, searchStr, replaceStr); % 执行替换操作
fOWC(m_filepath, 'w', newContent);
STATUS = 1;
end
function STATUS = fixed_m_gshhs(Jstruct)
% 更改m_map工具包的m_gshhs.m文件的FILNAME
m_filepath = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.download.m_map.PATH,'m_gshhs.m'); % which('m_gshhs.m');
gshhsc_path = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.download.gshhs.PATH,'gshhs_c.b');
gshhs_filepath = [fileparts(gshhsc_path) filesep];
if ~(exist(m_filepath,"file") && exist(gshhsc_path,"file"))
STATUS = 0;
return
end
% 备份源文件
path_DIR = fileparts(m_filepath);
m_filecopy = fullfile(path_DIR,'m_gshhs_origin.m');
if ~exist(m_filecopy,'file')
copyfile(m_filepath,m_filecopy);
end
fileContent = fileread(m_filepath); % 读取文件内容
if isempty(grep(m_filepath,gshhs_filepath))
searchStr = "FILNAME=[fileparts(which('m_gshhs.m')) '/data/'];"; % 定义要查找的字符串
replaceStr = sprintf("%% %s \nFILNAME = '%s';",searchStr, gshhs_filepath); % 定义替换后的字符串
newContent = strrep(fileContent, searchStr, replaceStr); % 执行替换操作
fOWC(m_filepath, 'w', newContent);
end
STATUS = 1;
end
function STATUS = fixed_m_etopo2(Jstruct)
% 更改m_map工具包的m_etopo2.m文件的FILNAME
m_filepath = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.download.m_map.PATH,'m_etopo2.m'); % which('m_etopo2.m');
etopo2_path = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.download.etopo1.PATH,'etopo1_ice_g_i2.bin');
etopo2_filepath = [fileparts(etopo2_path) filesep];
if ~(exist(m_filepath,"file") && exist(etopo2_path,"file"))
STATUS = 0;
return
end
% 备份源文件
path_DIR = fileparts(m_filepath);
m_filecopy = fullfile(path_DIR,'m_etopo2_origin.m');
if ~exist(m_filecopy,'file')
copyfile(m_filepath,m_filecopy);
end
fileContent = fileread(m_filepath); % 读取文件内容
if isempty(grep(m_filepath,etopo2_filepath))
searchStr = 'PATHNAME=''/ocean/rich/more/mmapbase/etopo2v2/''; % Be sure to end the path with a "/" or'; % 定义要查找的字符串
replaceStr = sprintf("%% %s \n PATHNAME = '%s';",searchStr, etopo2_filepath); % 定义替换后的字符串
newContent = strrep(fileContent, searchStr, replaceStr); % 执行替换操作
fOWC(m_filepath, 'w', newContent);
end
STATUS = 1;
end
function STATUS = install_ann_wrapper(Jstruct)
% 安装 ann_wrapper
m_filepath = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.gitclone.ann_wrapper.PATH,'ann_class_compile.m'); % which('ann_class_compile.m');
if ~exist(m_filepath,"file")
STATUS = 0;
else
PWD = pwd();
cd(fileparts(m_filepath))
ann_class_compile()
cd(PWD)
STATUS = 1;
end
return
end
function STATUS = install_DHIMIKE(Jsruct) %#ok<INUSD>
% https://github.com/DHI/DHI-MATLAB-Toolbox
STATUS = 0;
warning("INSTALL.json:improve:%s do not take effect. \n" + ...
"%s will be installed by INSTALL.json:gitclone:%s", 'DHIMIKE','DHIMIKE','DHIMIKE')
if ispc
%{
1. wget https://dist.nuget.org/win-x86-commandline/latest/nuget.exe
2. Change InstallPackages.bat line 1 with nuget.exe path
3. Run InstallPackages.bat
4. Run BuildBin.bat
5. cd MatlabDfsUtil
6. Run MatlabDfsUtilBuild.bat
7. cp MatlabDfsUtil.dll mbin/
8. Run CreateZip.bat (optional)
%}
end
end
function STATUS = install_irfu_matlab(Jstruct)
% 安装 ann_wrapper
m_filepath = fullfile(fileparts(fileparts(Jstruct.FILEPATH)),Jstruct.packages.gitclone.irfu_matlab.PATH,'irf.m'); % which('irf.m');
if ~exist(m_filepath,"file")
STATUS = 0;
else
PWD = pwd();
cd(fileparts(m_filepath))
irf();
cd(PWD)
STATUS = 1;
end
return
end
function [STATUS, PATH] = install_pkgs(PATH, Jstruct, control)
% packages.gitclone --> START
fields_gitclone = fieldnames(Jstruct.packages.gitclone)';
Git = Jstruct.git;
Git = get_git_method(Git); %get git是CMD还是MATLAB自带 | add .CHECK
CLONES = struct('');
PATH.exfunctions.gitclone = [];
if ~Git.CHECK
warning("git is not installed, functions at packages:gitclone will not be installed, please install git or set in 'INSTALL.json' and run %s again.", mfilename);
end
for field_cell = fields_gitclone
field = field_cell{1};
STATUS1.(field) = 0;
pkg = Jstruct.packages.gitclone.(field);
pkg_path = fullfile(PATH.basepath, pkg.PATH);
PATH.exfunctions.gitclone = [PATH.exfunctions.gitclone, convertCharsToStrings(pkg_path)];
if isequal(control, 'rm')
continue
elseif isequal(control, 'noclone')
if pkg.SETPATH
addpath(genpath2(pkg_path,{'.git', '.svn', '.github'}))
end
continue
end
if pkg.SETPATH
addpath(genpath2(pkg_path,{'.git', '.svn', '.github'}))
end
if Git.TF && Git.CHECK
if pkg.INSTALL
if ~(exist(pkg.CHECK{1},pkg.CHECK{2}) == str2double(pkg.CHECK{3})) && pkg.SETPATH % 如果不同时判断pkg.SETPATH 当pkg.INSATLL && ~pkg.SETPATH 由于不在路径中检测不到会重复下载
if isfield(Git,'mirror') && ~isempty(Git.mirror)
pkg_url = replace(pkg.URL,'https://github.com',del_filesep(Git.mirror)); % mirror
else
pkg_url = pkg.URL;
end
fprintf('---------> Cloning %s toolbox into %s\n', field, pkg.PATH)
CLONES(1).(field) = git_clone(Git, pkg_url, pkg_path, pkg);
STATUS1.(field) = 1;
end
clearvars txt field field_cell
end
if pkg.SETPATH
addpath(genpath2(pkg_path,{'.git', '.svn', '.github'}))
end
end
clearvars pkg pkg_url pkg_path
end
STATUS_gitclone = any(struct2array(STATUS1));
clearvars STATUS1
% END <-- packages.gitclone
% packages.download --> START
fields_download = fieldnames(Jstruct.packages.download)';
PATH.exfunctions.download = [];
for field_cell = fields_download
field = field_cell{1};
STATUS1.(field) = 0;
pkg = Jstruct.packages.download.(field);
pkg_path = fullfile(PATH.basepath, pkg.PATH);
PATH.exfunctions.download = [PATH.exfunctions.download, convertCharsToStrings(pkg_path)];
if isequal(control, 'rm')
continue
elseif isequal(control, 'noclone')
if pkg.SETPATH
addpath(genpath2(pkg_path,{'.git', '.svn', '.github'}))
end
continue
end
if pkg.SETPATH
addpath(genpath2(pkg_path,{'.git', '.svn', '.github'}))
end
if ~(exist(pkg.CHECK{1},pkg.CHECK{2}) == str2double(pkg.CHECK{3})) && pkg.INSTALL && pkg.SETPATH % 如果不同时判断pkg.SETPATH 当pkg.INSATLL && ~pkg.SETPATH 由于不在路径中检测不到会重复下载
pkg_localfile = fullfile(PATH.basepath, pkg.LOCALFILE);
if ~(exist(pkg_localfile, 'file') == 2) % No cache will download.
pkg_url = pkg.URL;
fprintf('---------> Downloading %s toolbox into %s\n', field, pkg.LOCALFILE)
download_urlfile(pkg_url, pkg_localfile, Jstruct.down.thread)
end
if ~(exist(pkg_localfile, 'file') == 2)
error('%s is not downloaded, please download it manually!',pkg.LOCALFILE);
else
switch field
case {'m_map', 'mexcdf', 'dace'}
STAUS_unzip = unzip_file(pkg_localfile, fileparts(pkg_localfile)); % Exfunctions/
case {'DHIMIKE', 't_tide', 'GSW', 'seawater', 'WindRose', 'gshhs', 'etopo1'}
STAUS_unzip = unzip_file(pkg_localfile, pkg_path); % Exfunctions/t_tide
otherwise
error('packages %s : unzip is not defined !!!' )
end
if ~STAUS_unzip
warning_state = warning("query").state;
warning('on')
warning(['%s downloaded failed, please download it manually!\n' ...
'Then rerun %s !'], pkg.LOCALFILE, mfilename());
warning(warning_state)
end
if isequal(field, 'mexcdf')
[pathstr, name] = fileparts(pkg_localfile);
branch_path = fullfile(pathstr,name);
move_mexcdf_branch(branch_path, pkg_path);
% rmfiles(branch_path)
end
% delete(local_file);
if pkg.SETPATH
addpath(genpath2(pkg_path,{'.git', '.svn', '.github'}))
end
end
STATUS1.(field) = 1;
end
clearvars field field_cell pkg local_file pkg_url pkg_localfile
end
STATUS_download = any(struct2array(STATUS1));
clearvars STATUS1
% END <-- packages.download
STATUS = or(STATUS_gitclone,STATUS_download);
clearvars STATUS_gitclone STATUS_download
PATH.exfunctions.gitclone = cellstr(PATH.exfunctions.gitclone)';
PATH.exfunctions.download = cellstr(PATH.exfunctions.download)';
return
end
function STATUS = move_mexcdf_branch(Afolder, Ufolder)
% STATUS = 0;
Dir1 = {'mexnc', 'netcdf_toolbox', 'snctools'};
makedirs(Ufolder) % [Edir, 'mexcdf']
for d = Dir1
copyfile(fullfile(Afolder, d{1}, '/trunk/*'), fullfile(Ufolder, d{1})) % Afolder --> [Edir, 'mexcdf-svn-r4054']
end
rmfiles(Afolder)
clear d
STATUS = 1;
end
function TF = check_command(command)
switch computer('arch')
case {'win32','win64'}
command = ['where ' command];
case {'glnxa64','maci64','maca64'}
command = ['which ' command];
otherwise
error('platform error')
end
[status,~] = system(command);
if status == 0
TF = true;
else
TF = false;
end
end
function download_urlfile(urlin, fileOut, thread)
if check_command('axel')
txt = ['axel -a -v -n ', num2str(thread) ,' ', urlin, ' -o ', fileOut];
% txt = ['axel -a -v -n 4 ', url, ' -o ', Edir, 't_tide_v1.5beta.zip'];
disp(txt);
system(txt);
elseif check_command('wget')
txt = ['wget ', urlin, ' -O ', fileOut];
% txt = ['wget ', url, ' -O ', Edir, 't_tide_v1.5beta.zip'];
disp(txt);
system(txt);
elseif check_command('curl')
txt = ['curl -L ', urlin, ' -o ', fileOut];
% txt = ['curl ', url, ' -o ', Edir, 't_tide_v1.5beta.zip'];
disp(txt);
system(txt);
else
websave(fileOut, url);
% warning('wget and curl are not installed, t_tide will not be installed');
end
end
function STATUS = unzip_file(fileIn, dirOut)
STATUS = 0;
if check_command('unzip')
txt = ['unzip ', fileIn, ' -d ', dirOut];
% txt = ['unzip ', Edir, 'm_map/data/gshhg-bin-2.3.7.zip -d ', Edir, 'm_map/data/'];
disp(txt);
[~, cmdout] = system(txt);
if ~contains(cmdout,'cannot find')
STATUS = 1;
end
else
try
unzip(fileIn, dirOut);
STATUS = 1;
catch ME1
if strcmp(ME1.identifier, 'MATLAB:io:archive:unzip:invalidZipFile')
STATUS = 0;
end
end
% unzip([Edir, 'm_map/data/gshhg-bin-2.3.7.zip'], [Edir, 'm_map/data/']);
end
end
function CLONE = git_clone(Git, pkg_url, pkg_path, pkg)
CLONE = '';
method = Git.method;
username = Git.username;
password = Git.password;
Depth = pkg.DEPTH;
Branch = pkg.BRANCH;
switch lower(method)
case {'cmd'}
if Depth == 0
Depth = 99999999;
end
if ~strcmp(username,'None') && ~strcmp(password,'None')
% https://blog.csdn.net/qq_45859054/article/details/108036754
username = replace(username,'@','%40');
password = replace(password,'@','%40');
pkg_url = replace(pkg_url,'://',sprintf('://%s:%s@',username,password));
end
txt = sprintf('git clone -b %s --depth %d %s %s', Branch, Depth, pkg_url, pkg_path);
disp(txt)
system(txt);
case {'matlab'}
CLONE = gitclone(pkg_url, pkg_path, 'Depth', Depth);
end
end
function Git = get_git_method(Git)
if isfield(Git, 'method') && ~isempty(Git.method) && isa(Git.method,"char")
switch lower(Git.method)
case {'cmd'}
if isfield(Git, 'path') && ~isempty(Git.path) && isa(Git.path,"char")
setenv('PATH', [Git.path, pathsep, getenv('PATH')]);
end
case {'auto'}
if ~isMATLABReleaseOlderThan("R2023b")
Git.method = 'MATLAB';
else
Git.method = 'cmd';
end
case {'matlab'}
if ~isMATLABReleaseOlderThan("R2023b")
else
[list_content,line_id] = grep(Jstruct.FILEPATH, '"method"');
error([' MATLAB version less than R2023b! \n ' ...
'Please set "method" at line %d, in "INSTALL.json" to "AUTO" or "CMD"! \n ' ...
'You set --> %s'],line_id(1),char(list_content))
end
end
end
switch lower(Git.method)
case{'cmd'}
Git.CHECK = check_command('git');
case{'matlab'}
Git.CHECK = true;
end
end
function fOWC(filename, mode, content)
fid = fopen(filename, mode); % 将修改后的内容写回文件
fwrite(fid, content);
fclose(fid);
end
function print_info()
fprintf('\n')
fprintf('=====================================================================\n')
fprintf('As adding files, if it does not take efect, please restart MATLAB \n')
fprintf('=====================================================================\n')
fprintf('\n')
end
function p = genpath2(d, pattern)
p = genpath(d);
% Return if missing or empty input argument
if nargin < 2 || isempty(pattern)
return
end
% Find folders that match the pattern
splitP = split(p, pathsep);
pattern = strcat(filesep, pattern);
hasPattern = contains(splitP, pattern);
% Index out folders with pattern
cleanP = splitP(~hasPattern);
% Return as list in genpath format
p = char(strjoin(cleanP, pathsep));
end % function-genpath2
function fun = ABANDON()
fun.read_start = @read_start;
fun.save_clones = @save_clones;
function S2 = read_start(structIn, prefix)
% 从struct中读取以prefix_开头的变量,将变量写入到PATH结构体中
% eg: 将struct中的Git_path写入到Git.path中
S2 = struct('');
key = fieldnames(structIn);
pattern = sprintf('^%s_', prefix); % ^Git_
for i = 1 : length(key)
if ~isempty(regexp(key{i},pattern,'once'))
S2(1).(key{i}(length(pattern):end)) = structIn.(key{i});
end
end
end
function save_clones(clones)
path__ = mfilename("fullpath");
[path,~]=fileparts(path__);
Sdir = fullfile(path, 'Data');
Sfile = fullfile(Sdir, 'GitRepository.mat');
keys = fieldnames(clones);
for i = 1 : length(keys)
if exist(Sfile,'file')
load(Sfile,'GitRepository')
GitRepository.(keys{i}) = clones.(keys{i});
else
GitRepository = clones;
end
save(Sfile,'GitRepository',"GitRepository","-mat",'-v7.3')
end
end
end