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

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

improvements/corrections of some *.pro headers

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