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

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