source: trunk/SRC/Utilities/find.pro @ 238

Last change on this file since 238 was 238, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
1;+
2;
3; @file_comments
4; based on <proidl>file_search</proidl>, but it is possible to specify
5; a set of possibles names and a different set of
6; possibles directories names.
7; By default look for files included in !path
8;
9; all file_search keywords can be used.
10;
11; @categories
12; find a file
13;
14; @param FILEIN {in}{required}
15; A scalar or array variable of string type, containing
16; file names to match. Input names specifications may contain
17; wildcard characters, enabling them to match multiple files
18; (see file_search for more informations). By default and if
19; necessary, find is looking for filename and also for filename
20; completed with '.pro'
21;
22; @keyword FIRSTFOUND
23; activate this keyword to stop looking for the file as soon as we found one.
24;
25; @keyword IODIRECTORY {default=['.',!path]}
26; A scalar or array variable of string type, containing
27; directories names where we are looking for the file.
28; Different directories can be separated by
29; path_sep(/search_path) (':' on unix type machine) as it is done
30; to define !path.
31; Note that if filename's dirname is different from '.', this
32; keyword is not taken into account.
33;
34; @keyword LOOKALLDIR
35; activate to look for the file (with a recursive search if needed)
36; in . iodir, homedir, !path + the DATA:TestsData directory if it exists.
37;
38; @keyword NOPRO
39; activate to avoid the automatic search of filename completed with '.pro'
40;
41; @keyword ONLYPRO
42; force to look only at file ending with .pro
43;
44; @keyword ONLYNC
45; force to look only at file ending with .nc
46;
47; @keyword RECURSIVE
48; performs recursive searching of directory hierarchies.
49; In a recursive search, find looks recursively for any and all
50; subdirectories in the file hierarchy rooted at the IODIRECTORY argument.
51;
52; @keyword REPERTOIRE
53; obsolete. keep for compatibility, use directory keyword
54;
55; @keyword UNIQUE
56; activate to make sure that each element of the output vector is unique.
57;
58; @keyword TRYFIND
59; if the file was not found and this keyword is activated, find will call
60; itself with the keywords /LOOKALLDIR and /FIRSTFOUND to try to find
61; the file we are looking for. Note that if the file was found at the
62; first try this keyword as no effect (which is not the case with LOOKALLDIR)
63;
64; @keyword _EXTRA
65; Used to pass keywords
66;
67; @returns
68; A scalar or array variable of string type, containing the
69; name (with the full path of the matching files. If no files
70; exist with names matching the input arguments, find returns
71; the scalar string : 'NOT FOUND'
72;
73; @examples
74;
75; IDL> print, find('*loadct')
76;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
77;   /usr/local/rsi/idl_6.0/lib/loadct.pro
78; IDL> print, find('*loadct', iodir=!dir,/recursive)
79;   /usr/local/rsi/idl_6.0/lib/loadct.pro
80;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
81; IDL> print, find('*loadct.pro')
82;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
83;   /usr/local/rsi/idl_6.0/lib/loadct.pro
84; IDL> print, find('*loadct',/nopro)
85;   NOT FOUND
86; IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib')
87;   /usr/local/rsi/idl_6.0/lib/loadct.pro
88; IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib', /test_write)
89;   NOT FOUND
90; IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib', /recursive)
91;   /usr/local/rsi/idl_6.0/lib/loadct.pro
92;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
93; IDL> print, find('mesh*', iodirectory = [iodir, !path])
94;   /Users/sebastie/DATA/ORCA2/meshmaskORCA2closea.nc
95;   /Users/sebastie/IDL/meshmaskclosesea.pro
96;   /Users/sebastie/IDL/meshmaskclosesea.pro~
97;   /Users/sebastie/SAXO_RD/Obsolete/meshlec.pro
98;   /usr/local/rsi/idl_6.0/lib/mesh_obj.pro
99;
100; @history
101; Sebastien Masson (smasson\@lodyc.jussieu.fr)
102;                       28/4/1999
103;                       6/7/1999: compatibility mac and windows
104; June 2005: Sebastien Masson: cleaning, use for file_* functions
105;
106; @version
107; $Id$
108;-
109;
110FUNCTION find, filein, IODIRECTORY = iodirectory, RECURSIVE = recursive $
111               , REPERTOIRE = repertoire, NOPRO = nopro, ONLYPRO = onlypro $
112               , ONLYNC = onlync, UNIQUE = unique, FIRSTFOUND = firstfound $
113               , LOOKALLDIR = LOOKALLDIR, TRYFIND = tryfind, _EXTRA = ex
114; define where we look for the file
115;
116  compile_opt idl2, strictarrsubs
117;
118  CASE 1 OF
119    keyword_set(lookalldir):BEGIN
120@cm_general
121      dirnames = ['./', iodir, homedir, !path]
122      tstdtadir= file_dirname(find('find', /onlypro), /mark_directory)
123      tstdtadir = (file_search(tstdtadir+'../../DATA/TestsData'))[0]
124      IF tstdtadir NE '' THEN dirnames = [tstdtadir, dirnames]
125    END
126    keyword_set(iodirectory): dirnames = iodirectory
127    keyword_set(repertoire): dirnames = repertoire
128    ELSE: dirnames = ['./', !path]
129  ENDCASE
130  tmp = dirnames
131  dirnames = 'dummy'
132  FOR i = 0, n_elements(tmp)-1 DO $
133    dirnames = [dirnames, strsplit(tmp[i], path_sep(/search_path), /extract)]
134  dirnames = dirnames[1:*]
135;
136  fileout = 'dummy'
137  FOR i = 0, n_elements(filein)-1 DO BEGIN
138    dir = file_dirname(filein[i])
139    base = file_basename(filein[i])
140; try to complete the file name with .pro or .nc if needed...
141    CASE 1 OF
142      keyword_set(onlypro):BEGIN
143        promiss = strpos(base, '.pro', /reverse_search)
144        promiss = promiss - (strlen(base) - 4)
145        bad = where(promiss NE 0 OR strlen(base) LE 4, cnt)
146        IF cnt NE 0 THEN base[bad] = base[bad] + '.pro'
147      end
148      keyword_set(onlync):BEGIN
149        ncmiss = strpos(base, '.nc', /reverse_search)
150        ncmiss = ncmiss - (strlen(base) - 3)
151        bad = where(ncmiss NE 0 OR strlen(base) LE 3, cnt)
152        IF cnt NE 0 THEN base[bad] = base[bad] + '.nc'
153      END
154      ELSE:if strmid(base, 0, 1, /reverse_offset) NE '*' $
155        AND NOT keyword_set(nopro) THEN base = base + '{.pro,}'
156    ENDCASE
157; use dirnames only if dir eq '.'
158    IF dir EQ  '.' THEN BEGIN
159      if keyword_set(recursive) THEN $
160        found = file_search(dirnames, base, _extra = ex) $
161        ELSE found = file_search(dirnames + '/' + base, _extra = ex)
162    ENDIF ELSE found = file_search(dir + '/' + base, _extra = ex)
163    IF found[0] NE '' THEN BEGIN
164      IF keyword_set(firstfound) THEN BEGIN
165        IF keyword_set(unique) THEN return, found[uniq(found, sort(found))] $
166        ELSE return, found
167      ENDIF
168      fileout = [fileout, found]
169    ENDIF
170  ENDFOR
171
172  IF n_elements(fileout) EQ 1 THEN fileout = 'NOT FOUND' $
173  ELSE fileout = fileout[1:*]
174;
175  IF n_elements(fileout) GT 1 THEN BEGIN
176    IF keyword_set(unique) THEN fileout = fileout[uniq(fileout, sort(fileout))]
177  ENDIF ELSE fileout = fileout[0]
178;
179  IF keyword_set(lookalldir) AND fileout[0] EQ 'NOT FOUND' $
180     AND NOT keyword_set(recursive) THEN $
181        filout = find(file_basename(filein[0]), /lookalldir $
182                      , /recursive, _extra = ex)
183;
184  IF keyword_set(tryfind) AND fileout[0] EQ 'NOT FOUND' THEN BEGIN
185    fileout = find(file_basename(filein[0]), /lookalldir, /firstfound, _extra = ex)
186    fileout = fileout[0]
187  ENDIF
188;
189  RETURN, fileout
190END
Note: See TracBrowser for help on using the repository browser.