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

Last change on this file since 163 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

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