Skip to content

Commit e2c3f6a

Browse files
committed
- added isabspath to determine if a path is
absolute or relative - this helps with problems stemming from exist, isdir, fopen, etc allowing for relative paths to be from any directory in the path (not just the pwd) - updated many functions that use exist on a filename or directory name to have the full path so there is no ambiguity - many more still to go (need to check for isdir & fopen too) - probably some bugs from this as I've gone cross-eyed fixing this many files
1 parent a09edcf commit e2c3f6a

33 files changed

+329
-119
lines changed

Diff for: docs/seizmo_to_do.txt

+6-13
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,17 @@ sacpzdb
1111
octave installation under 3.8.0
1212

1313
1. exist function problem
14-
- returns true for any directory/file name on path so we need to test
15-
for existence only in the current directory (if not absolute)
16-
- the workaround (use absolute path if relative):
17-
exist([pwd filesep dir],7)
18-
- but how to know if the path is relative or absolute???
19-
- first get the OS: use isunix or ispc
20-
- if "unix" (unix/linux/macosx): '/' is 1st char
21-
- if "pc" (windows): 'X:\' is 1st 3 char
22-
- isabspath will be the new function name
14+
- use isabspath to find rel paths then prepend pwd fs on those
15+
- isdir needs this too!
16+
- BONUS: fix string checking to use isstring ('must be a string!')
2317
2. fix topo functions
2418
3. 2d inversion on a sphere
2519
4. data grabbing codes
26-
grab_events
20+
grab_events (!)
2721
grab_stations
2822
grab_channels
29-
grab_responses
30-
grab_seismograms
31-
23+
grab_responses (!)
24+
grab_seismograms (!)
3225
5 noise array phase and attenuation like in prieto 2009
3326
- noise_stack_delaz
3427
- stacking option

Diff for: misc/create_contents_file.m

+6-2
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,27 @@
2626

2727
% Version History:
2828
% Jan. 3, 2011 - initial version
29+
% Jan. 26, 2014 - abs path exist fix
2930
%
3031
% Written by Garrett Euler (ggeuler at wustl dot edu)
31-
% Last Updated Jan. 3, 2011 at 23:00 GMT
32+
% Last Updated Jan. 26, 2014 at 23:00 GMT
3233

3334
% todo:
3435

3536
% check nargin
3637
error(nargchk(0,4,nargin));
3738

39+
% directory separator
40+
fs=filesep;
41+
3842
% check directory if given
3943
if(nargin>0 && ~isempty(mdir))
4044
% check directory
4145
if(~isstring(mdir))
4246
error('seizmo:create_contents_file:dirNotString',...
4347
'MDIR must be a string!');
4448
end
49+
if(~isabspath(mdir)); mdir=[pwd fs mdir]; end
4550
if(~exist(mdir,'dir'))
4651
error('seizmo:create_contents_file:dirDoesNotExist',...
4752
'Directory: %s\nDoes Not Exist!',mdir);
@@ -98,7 +103,6 @@
98103
if(nargin<2 || isempty(desc)); desc=['Contents of ' mdir ':']; end
99104

100105
% graphical file selections
101-
fs=filesep;
102106
if(nargin<3 || isempty(file))
103107
[file,path]=uiputfile(...
104108
{'*.m' 'M Files (*.m)';

Diff for: misc/isabspath.m

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
function [lgc]=isabspath(path,iswindows)
2+
%ISABSPATH Determines if a path is an absolute path or not
3+
%
4+
% Usage: lgc=isabspath(path)
5+
% lgc=isabspath(path,iswindows)
6+
%
7+
% Description:
8+
% LGC=ISABSPATH(PATH) checks if the path(s) in PATH are relative or
9+
% absolute and returns TRUE for those that are absolute paths. PATHS
10+
% may be a string, char array or a cell string array. LGC is a logical
11+
% array with one element per path in PATH. This is useful to find
12+
% relative paths so you can convert them to absolute paths for
13+
% functions like EXIST. The determination is done by discovering the
14+
% OS type of the current system using ISPC.
15+
%
16+
% LGC=ISABSPATH(PATH,ISWINDOWS) allows setting the OS type for
17+
% determining if the paths are absolute or not when the paths are not
18+
% valid paths for the current machine. For instance, set ISWINDOWS to
19+
% FALSE for Unix, Linux or MACOSX paths when you are using MicrosoftTM
20+
% WindowsTM. ISWINDOWS must be TRUE or FALSE (scalar only).
21+
%
22+
% Notes:
23+
% - The path is not required to exist or even to be valid! This just
24+
% does a simple test on each path given the OS (e.g., is the first
25+
% character a '/' for unix).
26+
%
27+
% Examples:
28+
% % Test a few relative paths:
29+
% isabspath('./somedir')
30+
% isabspath('../somedir')
31+
% isabspath('~/somedir')
32+
% isabspath('..\somewindir')
33+
%
34+
% % Test a few absolute paths:
35+
% isabspath('/home')
36+
% isabspath('/usr/share/../bin')
37+
% isabspath('c:\Programs')
38+
%
39+
% % And a few invalid ones:
40+
% isabspath('/\') % absolute path to the '\' dir?
41+
% isabspath('somedir\c:/somewhere') % win drive in a unix dir under pwd
42+
% isabspath('\\someserver\somedir') % maybe you can add this feature...
43+
%
44+
% See also: ISPC, ISUNIX
45+
46+
% Version History:
47+
% Jan. 27, 2014 - initial version
48+
%
49+
% Written by Garrett Euler (ggeuler at wustl dot edu)
50+
% Last Updated Jan. 27, 2014 at 11:15 GMT
51+
52+
% todo:
53+
54+
% check number of inputs
55+
error(nargchk(1,2,nargin));
56+
57+
% check/fix path
58+
if(ischar(path))
59+
path=cellstr(path);
60+
elseif(~iscellstr(path))
61+
error('seizmo:isabspath:badInput',...
62+
'PATH must be a string, char array or a cellstr array!');
63+
end
64+
65+
% check/default os
66+
if(nargin<2 || isempty(iswindows)); iswindows=ispc; end
67+
if(~islogical(iswindows) || ~isscalar(iswindows))
68+
error('seizmo:isabspath:badInput',...
69+
'ISWINDOWS must be TRUE or FALSE!');
70+
end
71+
72+
% preallocate output as all relative paths
73+
lgc=false(size(path));
74+
75+
% act by os
76+
if(iswindows) % windows
77+
for i=1:numel(path)
78+
% require drive char to be a-z,A-Z
79+
if(isempty(path{i})); continue; end
80+
drive=double(upper(path{i}(1)));
81+
lgc(i)=drive>=65 && drive<=90 && strcmp(path{i}(2:3),':\');
82+
end
83+
else % unix, linux, macosx
84+
for i=1:numel(path)
85+
if(isempty(path{i})); continue; end
86+
lgc(i)=strcmp(path{i}(1),'/');
87+
end
88+
end
89+
90+
end

Diff for: misc/readcsv.m

+8-3
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@
3838
% Jan. 28, 2011 - handle empty entries (,,), allow alternate
3939
% field delimiter, require nonempty field name
4040
% Feb. 28, 2012 - output is scalar struct now
41+
% Jan. 26, 2014 - abs path exist fix
4142
%
4243
% Written by Garrett Euler (ggeuler at wustl dot edu)
43-
% Last Updated Feb. 28, 2012 at 17:25 GMT
44+
% Last Updated Jan. 26, 2014 at 17:25 GMT
4445

4546
% todo:
4647
% - text delimiter
@@ -53,6 +54,9 @@
5354
% check nargin
5455
error(nargchk(0,2,nargin));
5556

57+
% directory separator
58+
fs=filesep;
59+
5660
% graphical selection
5761
if(nargin<1 || isempty(file))
5862
[file,path]=uigetfile(...
@@ -63,13 +67,14 @@
6367
error('seizmo:readcsv:noFileSelected',...
6468
'No input file selected!');
6569
end
66-
file=strcat(path,filesep,file);
70+
file=[path fs file];
6771
else
6872
% check file
69-
if(~ischar(file))
73+
if(~isstring(file))
7074
error('seizmo:readcsv:fileNotString',...
7175
'FILE must be a string!');
7276
end
77+
if(~isabspath(file)); file=[pwd fs file]; end
7378
if(~exist(file,'file'))
7479
error('seizmo:readcsv:fileDoesNotExist',...
7580
'CSV File: %s\nDoes Not Exist!',file);

Diff for: misc/readtxt.m

+8-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@
3333
% July 30, 2010 - nargchk fix
3434
% Aug. 10, 2010 - filterspec option added
3535
% Nov. 1, 2011 - doc update
36+
% Jan. 26, 2014 - abs path exist fix
3637
%
3738
% Written by Garrett Euler (ggeuler at wustl dot edu)
38-
% Last Updated Nov. 1, 2011 at 13:00 GMT
39+
% Last Updated Jan. 26, 2014 at 13:00 GMT
3940

4041
% todo:
4142

4243
% check nargin
4344
error(nargchk(0,2,nargin));
4445

46+
% directory separator
47+
fs=filesep;
48+
4549
% default/check filterspec
4650
if(nargin<2 || isempty(filterspec))
4751
filterspec={'*.txt;*.TXT' 'TXT Files (*.txt,*.TXT)';
@@ -58,13 +62,14 @@
5862
if(isequal(0,file))
5963
error('seizmo:readtxt:noFileSelected','No input file selected!');
6064
end
61-
file=strcat(path,filesep,file);
65+
file=[path fs file];
6266
else
6367
% check file
64-
if(~ischar(file))
68+
if(~isstring(file))
6569
error('seizmo:readtxt:fileNotString',...
6670
'FILE must be a string!');
6771
end
72+
if(~isabspath(file)); file=[pwd fs file]; end
6873
if(~exist(file,'file'))
6974
error('seizmo:readtxt:fileDoesNotExist',...
7075
'File: %s\nDoes Not Exist!',file);

Diff for: misc/unixcompressavi.m

+6-1
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@
7777
% Apr. 3, 2012 - minor doc update
7878
% Apr. 26, 2012 - add LD_LIBRARY_PATH="" to unix calls to avoid
7979
% linker issues (glibcxx_3.4.11 no found...)
80+
% Jan. 26, 2014 - abs path exist fix
8081
%
8182
% Written by Garrett Euler (ggeuler at wustl dot edu)
82-
% Last Updated Apr. 26, 2012 at 17:45 GMT
83+
% Last Updated Jan. 26, 2014 at 17:45 GMT
8384

8485
% todo
8586
% - options for other codecs?
@@ -103,6 +104,9 @@
103104
% check nargin
104105
error(nargchk(1,4,nargin));
105106

107+
% directory separator
108+
fs=filesep;
109+
106110
% default codec
107111
if(nargin==1 || isempty(fileout)); fileout=filein; end
108112
if(nargin<3 || isempty(codec)); codec='lavc'; end
@@ -140,6 +144,7 @@
140144

141145
% loop over filein checking each exists
142146
for i=1:n
147+
if(~isabspath(filein{i})); filein{i}=[pwd fs filein{i}]; end
143148
if(~exist(filein{i},'file'))
144149
error('seizmo:unixcompressavi:fileNotFound',...
145150
'Could not locate file: %s',filein{i});

Diff for: misc/writecsv.m

+8-3
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,19 @@
4949
% Feb. 5, 2010 - add check for overwrite flag
5050
% Feb. 11, 2011 - mass nargchk fix, use fprintf
5151
% Feb. 28, 2012 - input is scalar struct now, delimiter input
52+
% Jan. 26, 2014 - abs path exist fix
5253
%
5354
% Written by Garrett Euler (ggeuler at wustl dot edu)
54-
% Last Updated Feb. 28, 2012 at 15:05 GMT
55+
% Last Updated Jan. 26, 2014 at 15:05 GMT
5556

5657
% todo:
5758

5859
% check nargin
5960
error(nargchk(2,4,nargin));
6061

62+
% directory separator
63+
fs=filesep;
64+
6165
% defaults
6266
if(nargin<3 || isempty(delimiter)); delimiter=', '; end
6367
if(nargin<4 || isempty(overwrite)); overwrite=false; end
@@ -84,13 +88,14 @@
8488
if(isequal(0,file))
8589
error('seizmo:writecsv:noFileSelected','No output file selected!');
8690
end
87-
file=strcat(path,filesep,file);
91+
file=[path fs file];
8892
else
8993
% check file
90-
if(~ischar(file) || ~isvector(file))
94+
if(~isstring(file))
9195
error('seizmo:writecsv:fileNotString',...
9296
'FILE must be a string!');
9397
end
98+
if(~isabspath(file)); file=[pwd fs file]; end
9499
if(exist(file,'file'))
95100
if(exist(file,'dir'))
96101
error('seizmo:writecsv:dirConflict',...

Diff for: models/read_cu_mod.m

+8-3
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,23 @@
3131
% figure; imagesc(model.vsh(:,:,3));
3232
% figure; imagesc(model.vsv(:,:,3));
3333
%
34-
% See also:
34+
% See also: READCRUST2, READCRUST10
3535

3636
% Version History:
3737
% Jan. 21, 2011 - initial version
38+
% Jan. 26, 2014 - abs path exist fix
3839
%
3940
% Written by Garrett Euler (ggeuler at wustl dot edu)
40-
% Last Updated Jan. 21, 2011 at 10:35 GMT
41+
% Last Updated Jan. 26, 2014 at 10:35 GMT
4142

4243
% todo
4344

4445
% check nargin
4546
error(nargchk(0,1,nargin));
4647

48+
% directory separator
49+
fs=filesep;
50+
4751
% file input
4852
filterspec={
4953
'*.mod;*.MOD' 'MOD Files (*.mod,*.MOD)';
@@ -55,13 +59,14 @@
5559
error('seizmo:read_cu_mod:noFileSelected',...
5660
'No input file selected!');
5761
end
58-
file=strcat(path,filesep,file);
62+
file=[path fs file];
5963
else
6064
% check file
6165
if(~isstring(file))
6266
error('seizmo:read_cu_mod:fileNotString',...
6367
'FILE must be a string!');
6468
end
69+
if(~isabspath(file)); file=[pwd fs file]; end
6570
if(~exist(file,'file'))
6671
error('seizmo:read_cu_mod:fileDoesNotExist',...
6772
'File: %s\nDoes Not Exist!',file);

Diff for: models/write_1dmodel_nd.m

+8-3
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,19 @@
4949
% Sep. 18, 2010 - initial version
5050
% Sep. 19, 2010 - support for inf Q output as 0
5151
% Feb. 21, 2012 - noheader flag
52+
% Jan. 26, 2014 - abs path exist fix
5253
%
5354
% Written by Garrett Euler (ggeuler at wustl dot edu)
54-
% Last Updated Feb. 21, 2012 at 10:35 GMT
55+
% Last Updated Jan. 26, 2014 at 10:35 GMT
5556

5657
% todo
5758

5859
% check nargin
5960
error(nargchk(2,4,nargin));
6061

62+
% directory separator
63+
fs=filesep;
64+
6165
% default overwrite to false
6266
if(nargin<3 || isempty(overwrite)); overwrite=false; end
6367
if(nargin<4 || isempty(noheader)); noheader=false; end
@@ -88,13 +92,14 @@
8892
error('seizmo:write_1dmodel_nd:noFileSelected',...
8993
'No output file selected!');
9094
end
91-
file=strcat(path,filesep,file);
95+
file=[path fs file];
9296
else
9397
% check file
94-
if(~ischar(file) || ~isvector(file))
98+
if(~isstring(file))
9599
error('seizmo:write_1dmodel_nd:fileNotString',...
96100
'FILE must be a string!');
97101
end
102+
if(~isabspath(file)); file=[pwd fs file]; end
98103
if(exist(file,'file'))
99104
if(exist(file,'dir'))
100105
error('seizmo:write_1dmodel_nd:dirConflict',...

0 commit comments

Comments
 (0)