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

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

modification of headers : mainly blanks around = sign for keywords in declaration of function and pro

  • Property svn:keywords set to Id
File size: 6.9 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 <proidl>FILE_SEARCH</proidl> keywords can be used.
10;
11; @categories
12; find a file
13;
14; @param FILEIN {in}{required} {type=scalar string or array of strings}
15; File name[s] to match. Input names specifications may contain
16; wildcard characters, enabling them to match multiple files
17; (see <proidl>FILE_SEARCH</proidl> for more informations). By default and if
18; necessary, <pro>find</pro> 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
23; found one. Return a scalar string containing the first file found
24;
25; @keyword IODIRECTORY {type=scalar string or array of strings} {default=['.',!path]}
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, <pro>find</pro> 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 IODIRECTORY 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, <pro>find</pro>
59; 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, <pro>find</pro> returns
71; the scalar string : 'NOT FOUND'
72;
73; @examples
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;
114  compile_opt idl2, strictarrsubs
115;
116; define where we look for the file
117  cd, current = current
118  current = current + path_sep()
119  CASE 1 OF
120    keyword_set(lookalldir):BEGIN
121@cm_general
122      dirnames = [current, iodir, homedir, !path]
123      tstdtadir= file_dirname(find('find', /onlypro), /mark_directory)
124      parent = path_sep(/parent_directory)+path_sep()
125      tstdtadir = (file_search(tstdtadir+parent+parent+'DATA/TestsData'))[0]
126      IF tstdtadir NE '' THEN dirnames = [tstdtadir, dirnames]
127    END
128    keyword_set(iodirectory): dirnames = iodirectory
129    keyword_set(repertoire): dirnames = repertoire
130    ELSE: dirnames = [current, !path]
131  ENDCASE
132  tmp = dirnames
133  dirnames = 'dummy'
134  FOR i = 0, n_elements(tmp)-1 DO $
135    dirnames = [dirnames, strsplit(tmp[i], path_sep(/search_path), /extract)]
136  dirnames = dirnames[1:*]
137;
138  fileout = 'dummy'
139  FOR i = 0, n_elements(filein)-1 DO BEGIN
140    dir = file_dirname(filein[i])
141    base = file_basename(filein[i])
142; try to complete the file name with .pro or .nc if needed...
143    CASE 1 OF
144      keyword_set(onlypro):BEGIN
145        promiss = strpos(base, '.pro', /reverse_search)
146        promiss = promiss - (strlen(base) - 4)
147        bad = where(promiss NE 0 OR strlen(base) LE 4, cnt)
148        IF cnt NE 0 THEN base[bad] = base[bad] + '.pro'
149      end
150      keyword_set(onlync):BEGIN
151        ncmiss = strpos(base, '.nc', /reverse_search)
152        ncmiss = ncmiss - (strlen(base) - 3)
153        bad = where(ncmiss NE 0 OR strlen(base) LE 3, cnt)
154        IF cnt NE 0 THEN base[bad] = base[bad] + '.nc'
155      END
156      ELSE:if strmid(base, 0, 1, /reverse_offset) NE '*' $
157        AND NOT keyword_set(nopro) THEN base = base + '{.pro,}'
158    ENDCASE
159; use dirnames only if dir eq '.'
160    IF dir EQ  '.' THEN BEGIN
161      if keyword_set(recursive) THEN $
162        found = file_search(dirnames, base, _extra = ex) $
163        ELSE found = file_search(dirnames + '/' + base, _extra = ex)
164    ENDIF ELSE found = file_search(dir + '/' + base, _extra = ex)
165    IF found[0] NE '' THEN BEGIN
166      IF keyword_set(firstfound) THEN return, found[0]
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.