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 if nargin < 3
56 pattern = [];
57 end
58 ftype = string(ftype);
59 suffix = ftype + ".txt";
60 if isempty(pattern)
61 if isempty(self.spec.outputFileName)
62 if pm.array.len(self.input) ~= 0
63 % \todo
64 % This extraction of ``outputFileName`` from the ``input`` file must be automated in future.
65 error ( newline ...
66 + "Apparently, you have set the ``input`` component of the sampler object." + newline ...
67 + "Extract the value of the simulation specification ``outputFileName`` from this ``input``." + newline ...
68 + "Then, assign it to the simulation specification ``spec.outputFileName``." + newline ...
69 + newline ...
70 );
71 end
72 %if ~self.silent
73 % warning ( newline ...
74 % + "The input simulation specification ``outputFileName`` is" + newline ...
75 % + "unset while the input argument ``pattern`` is missing." + newline ...
76 % + "Searching the current working directory:" + newline ...
77 % + newline ...
78 % + pm.io.tab + string(pwd) + newline ...
79 % + newline ...
80 % + "for potential ASCII " + ftype + " files..." + newline ...
81 % + newline ...
82 % );
83 %end
84 pattern = string(pwd());
85 else
86 pattern = self.spec.outputFileName;
87 end
88 end
89 if isfile(pattern)
90 % Check if the input path is a full path to a file.
91 fileList = string(pattern);
92 if ~self.silent && ~endsWith(pattern, suffix)
93 warning ( newline ...
94 + "The identified " + ftype + " file:" + newline ...
95 + newline ...
96 + pm.io.tab + pattern + newline ...
97 + newline ...
98 + "does not end with the expected suffix """ + suffix + """." + newline ...
99 );
100 end
101 else
102 % search for files matching the input pattern.
103 if isfolder(pattern) && ~endsWith(pattern, filesep)
104 % Ensure pattern is a directory with an explicit directory separator.
105 pattern = pattern + filesep;
106 end
107 if endsWith(pattern, "*")
108 dirList = dir(pattern);
109 else
110 dirList = dir(pattern + "*");
111 end
112 counter = 0;
113 fileList = [];
114 for ifile = 1 : length(dirList)
115 if contains(dirList(ifile).name, suffix)
116 counter = counter + 1;
117 filePathModified = fullfile(string(dirList(ifile).folder), string(dirList(ifile).name));
118 fileList = [fileList, filePathModified];
119 end
120 end
121 if isempty(fileList)
122 % check if the input path is a url.
123 if pm.web.isurl(pattern)
124 try
125 fileList = [string(websave(fullfile(tempdir, pm.web.basename(pattern)), pattern))];
126 catch me
127 warning ( newline ...
128 + string(me.identifier) + " : " + string(me.message) + newline ...
129 + "Failed to read data from the specified URL:" + newline ...
130 + pm.io.tab + newline ...
131 + newline ...
132 );
133 end
134 end
135 end
136 if isempty(fileList)
137 msg = newline ...
138 + "Failed to detect any " + ftype + " files with the requested pattern:" + newline ...
139 + newline ...
140 + pm.io.tab + pattern + newline ...
141 + newline ...
142 + "Specify an input ``pattern`` that either" + newline ...
143 + newline ...
144 + pm.io.tab + "1. points to one or more " + ftype + " files, or," + newline ...
145 + pm.io.tab + "2. represents the unique part of the names of a simulation." + newline ...
146 + pm.io.tab + " This unique-name is the common prefix in the names of" + newline ...
147 + pm.io.tab + " the output files of a given sampling simulation." + newline ...
148 + newline ...
149 + "Note that binary files ending with the suffix ""*.bin"" cannot be currently parsed." + newline ...
150 ;
151 if contains(ftype, "restart")
152 msg = msg ...
153 + "For restart files, you can set the simulation specification ``restartFileFormat`` to" + newline ...
154 + newline ...
155 + pm.io.tab + "spec.restartFileFormat = ""ascii"";" + newline ...
156 + newline ...
157 + "to generate ASCII-format MATLAB-readable " + ftype + " files." ...
158 + newline ...
159 ;
160 elseif contains(ftype, "chain")
161 msg = msg ...
162 + "For chain files, you can set the simulation specification ``chainFileFormat`` to" + newline ...
163 + newline ...
164 + pm.io.tab + "spec.chainFileFormat = ""ascii"";" + newline ...
165 + newline ...
166 + "to generate ASCII-format MATLAB-readable " + ftype + " files." ...
167 + newline ...
168 ;
169 end
170 error(msg);
171 end
172 end
173 if ~self.silent
174 disp(string(length(fileList)) + " files were detected matching pattern: """ + pattern + """");
175 end
176end
function name(in vendor)
Return the MPI library name as used in naming the ParaMonte MATLAB shared libraries.
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 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...