ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
findfile.m
Go to the documentation of this file.
1%> \brief
2%> Return a vector of MATLAB strings containing the
3%> fully-resolved paths matching the input ``pattern``.
4%>
5%> \details
6%> This is a ``Hidden`` method of the class [pm.sampling.Sampler](@ref Sampler).
7%>
8%> \param[in] self : The input parent object of class [pm.sampling.Sampler](@ref Sampler)
9%> which is **implicitly** passed to this dynamic method (not by the user).<br>
10%> \param[in] ftype : The input scalar MATLAB string containing the sampler file type,
11%> which can be one of the following:
12%> <ol>
13%> <li> ``"chain"``
14%> <li> ``"sample"``
15%> <li> ``"report"``
16%> <li> ``"restart"``
17%> <li> ``"progress"``
18%> </ol>
19%>
20%> \param[in] pattern : The input scalar MATLAB string containing either:<br>
21%> <ol>
22%> <li> a pattern to search for paths on the current system.<br>
23%> Wildcards may be used for basenames and for the directory parts.<br>
24%> If pattern contains directory parts, then these will
25%> be included in the output ``fileList``.<br>
26%> Following wildcards can be used within the specified ``pattern``:
27%> ``*`` : match zero or more characters.<br>
28%> <li> a full weblink to download and save locally on the system temporary folder.<br>
29%> (**optional**, default = ``pwd()``)
30%> </ol>
31%>
32%> \return
33%> ``fileList`` : The output vector of MATLAB strings containing the
34%> fully-resolved paths matching the input ``pattern``.<br>
35%>
36%> \interface{findfile}
37%> \code{.m}
38%>
39%> sampler = pm.sampling.Sampler();
40%> fileList = sampler.findfile(ftype)
41%> fileList = sampler.findfile(ftype, [], [])
42%> fileList = sampler.findfile(ftype, [], silent)
43%> fileList = sampler.findfile(ftype, pattern, [])
44%> fileList = sampler.findfile(ftype, pattern, silent)
45%>
46%> \endcode
47%>
48%> \final{findfile}
49%>
50%> \author
51%> \JoshuaOsborne, May 21 2024, 12:14 AM, University of Texas at Arlington<br>
52%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
53%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
54function fileList = findfile(self, ftype, pattern)
55
56 if nargin < 3
57 pattern = [];
58 end
59
60 ftype = string(ftype);
61 suffix = ftype + ".txt";
62 if isempty(pattern)
63 if isempty(self.spec.outputFileName)
64 if pm.array.len(self.input) ~= 0
65 % \todo
66 % This extraction of ``outputFileName`` from the ``input`` file must be automated in future.
67 error ( newline ...
68 + "Apparently, you have set the ``input`` component of the sampler object." + newline ...
69 + "Extract the value of the simulation specification ``outputFileName`` from this ``input``." + newline ...
70 + "Then, assign it to the simulation specification ``spec.outputFileName``." + newline ...
71 + newline ...
72 );
73 end
74 %if ~self.silent
75 % warning ( newline ...
76 % + "The input simulation specification ``outputFileName`` is" + newline ...
77 % + "unset while the input argument ``pattern`` is missing." + newline ...
78 % + "Searching the current working directory:" + newline ...
79 % + newline ...
80 % + pm.io.tab + string(pwd) + newline ...
81 % + newline ...
82 % + "for potential ASCII " + ftype + " files..." + newline ...
83 % + newline ...
84 % );
85 %end
86 pattern = string(pwd());
87 else
88 pattern = self.spec.outputFileName;
89 end
90 end
91
92 if isfile(pattern)
93
94 %%%%
95 %%%% Check if the input path is a full path to a file.
96 %%%%
97
98 fileList = string(pattern);
99 if ~self.silent && ~endsWith(pattern, suffix)
100 warning ( newline ...
101 + "The identified " + ftype + " file:" + newline ...
102 + newline ...
103 + pm.io.tab + pattern + newline ...
104 + newline ...
105 + "does not end with the expected suffix """ + suffix + """." + newline ...
106 );
107 end
108 else
109
110 %%%%
111 %%%% search for files matching the input pattern.
112 %%%%
113
114 if isfolder(pattern) && ~endsWith(pattern, filesep)
115 % Ensure pattern is a directory with an explicit directory separator.
116 pattern = pattern + filesep;
117 end
118 if endsWith(pattern, "*")
119 dirList = dir(pattern);
120 else
121 dirList = dir(pattern + "*");
122 end
123 counter = 0;
124 fileList = [];
125 for ifile = 1 : length(dirList)
126 if contains(dirList(ifile).name, suffix)
127 counter = counter + 1;
128 filePathModified = fullfile(string(dirList(ifile).folder), string(dirList(ifile).name));
129 fileList = [fileList, filePathModified];
130 end
131 end
132
133 %%%%
134 %%%% check if the input path is a url.
135 %%%%
136
137 if isempty(fileList)
138 if pm.web.isurl(pattern)
139 try
140 fileList = [string(websave(fullfile(tempdir, pm.web.basename(pattern)), pattern))];
141 catch me
142 warning ( newline ...
143 + string(me.identifier) + " : " + string(me.message) + newline ...
144 + "Failed to read data from the specified URL:" + newline ...
145 + newline ...
146 );
147 end
148 end
149 end
150
151 if isempty(fileList)
152 msg = newline ...
153 + "Failed to detect any " + ftype + " files with the requested pattern:" + newline ...
154 + newline ...
155 + pm.io.tab + pattern + newline ...
156 + newline ...
157 + "Specify an input ``pattern`` that either" + newline ...
158 + newline ...
159 + pm.io.tab + "1. points to one or more " + ftype + " files, or," + newline ...
160 + pm.io.tab + "2. represents the unique part of the names of a simulation." + newline ...
161 + pm.io.tab + " This unique-name is the common prefix in the names of" + newline ...
162 + pm.io.tab + " the output files of a given sampling simulation." + newline ...
163 + newline ...
164 + "Note that binary files ending with the suffix ""*.bin"" cannot be currently parsed." + newline ...
165 ;
166 if contains(ftype, "restart")
167 msg = msg ...
168 + "For restart files, you can set the simulation specification ``restartFileFormat`` to" + newline ...
169 + newline ...
170 + pm.io.tab + "spec.restartFileFormat = ""ascii"";" + newline ...
171 + newline ...
172 + "to generate ASCII-format MATLAB-readable " + ftype + " files." ...
173 + newline ...
174 ;
175 elseif contains(ftype, "chain")
176 msg = msg ...
177 + "For chain files, you can set the simulation specification ``chainFileFormat`` to" + newline ...
178 + newline ...
179 + pm.io.tab + "spec.chainFileFormat = ""ascii"";" + newline ...
180 + newline ...
181 + "to generate ASCII-format MATLAB-readable " + ftype + " files." ...
182 + newline ...
183 ;
184 end
185 error(msg);
186 end
187 end
188
189 if ~self.silent
190 disp(string(length(fileList)) + " files were detected matching pattern: """ + pattern + """");
191 end
192
193end
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared library directories.
function basename(in url)
Return a scalar MATLAB string containing the basename part of an input url, defined as the segment of...
This is the base class for the ParaMonte sampler routines.
Definition: Sampler.m:21
function detect(in vendor)
Return the MPI image count and current image ID (e.g., MPI rank + 1) and the MPI library name as it a...
function isurl(in url)
Return a scalar MATLAB logical that is true if and only if the input URL string exists in the world w...
function len(in obj)
Return a scalar MATLAB whole-number containing the length of the input scalar or vector object.
function tab()
Return a scalar MATLAB string containing 4 blank characters equivalent to a tab character.
function which(in vendor)
Return the a MATLAB string containing the path to the first mpiexec executable binary found in system...