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

Last change on this file since 133 was 128, checked in by pinsard, 18 years ago

improvements of Utilities/*.pro header

  • Property svn:keywords set to Id
File size: 6.5 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} 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 activate this keyword to stop looking for the file as
23;        soon as we found one.
24;
25; @keyword IODIRECTORY {default=!path} 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 activate to look for the file with a recursive search
34;        in iodir, homedir, !path + the DATA:TestsData directory if it exists.
35;
36; @keyword NOPRO activate to avoid the automatic search of filename
37;        completed with '.pro'
38;
39; @keyword ONLYPRO force to look only at file ending with .pro
40;
41; @keyword ONLYNC force to look only at file ending with .nc
42;
43; @keyword RECURSIVE performs recursive searching of directory hierarchies.
44;        In a recursive search, find looks recursively for any and all
45;        subdirectories in the file hierarchy rooted at the IODIRECTORY
46;        argument.
47;
48; @keyword REPERTOIRE obsolete. keep for compatibility, use directory keyword
49;
50; @keyword UNIQUE activate to make sure that each element of the output
51;        vector is unique.
52;
53; @keyword _EXTRA used to pass your keywords
54;
55;
56; @returns A scalar or array variable of string type, containing the
57;       name (with the full path of the matching files. If no files
58;       exist with names matching the input arguments, find returns
59;       the scalar string : 'NOT FOUND'
60;
61; @examples
62;
63; IDL> print, find('*loadct')
64;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
65;   /usr/local/rsi/idl_6.0/lib/loadct.pro
66; IDL> print, find('*loadct', iodir=!dir,/recursive)
67;   /usr/local/rsi/idl_6.0/lib/loadct.pro
68;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
69; IDL> print, find('*loadct.pro')
70;   /usr/local/rsi/idl_6.0/lib/utilities/xloadct.pro
71;   /usr/local/rsi/idl_6.0/lib/loadct.pro
72; IDL> print, find('*loadct',/nopro)
73;   NOT FOUND
74; IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib')
75;   /usr/local/rsi/idl_6.0/lib/loadct.pro
76; IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib', /test_write)
77;   NOT FOUND
78; IDL> print, find('*loadct', iodir = '/usr/local/rsi/idl_6.0/lib', /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('mesh*', iodirectory = [iodir, !path])
82;   /Users/sebastie/DATA/ORCA2/meshmaskORCA2closea.nc
83;   /Users/sebastie/IDL/meshmaskclosesea.pro
84;   /Users/sebastie/IDL/meshmaskclosesea.pro~
85;   /Users/sebastie/SAXO_RD/Obsolete/meshlec.pro
86;   /usr/local/rsi/idl_6.0/lib/mesh_obj.pro
87;
88; @history Sebastien Masson (smasson\@lodyc.jussieu.fr)
89;                       28/4/1999
90;                       6/7/1999: compatibility mac and windows
91; June 2005: Sebastien Masson: cleaning, use for file_* functions
92;
93; @version $Id$
94;-
95;------------------------------------------------------------
96;------------------------------------------------------------
97;------------------------------------------------------------
98FUNCTION find, filein, IODIRECTORY = iodirectory, RECURSIVE = recursive $
99               , REPERTOIRE = repertoire, NOPRO = nopro, ONLYPRO = onlypro $
100               , ONLYNC = onlync, UNIQUE = unique, FIRSTFOUND = firstfound $
101               , LOOKALLDIR = LOOKALLDIR, _extra = ex
102; define where we look for the file
103;
104  compile_opt idl2, strictarrsubs
105;
106  CASE 1 OF
107    keyword_set(lookalldir):BEGIN
108@cm_general
109      dirnames = [iodir, homedir, !path]
110      tstdtadir= file_dirname(find('find', /onlypro), /mark_directory)
111      tstdtadir = (file_search(tstdtadir+'../../DATA/TestsData'))[0]
112      IF tstdtadir NE '' THEN dirnames = [tstdtadir, dirnames]
113    END
114    keyword_set(iodirectory): dirnames = iodirectory
115    keyword_set(repertoire): dirnames = repertoire
116    ELSE: dirnames = !path
117  ENDCASE
118  tmp = dirnames
119  dirnames = 'dummy'
120  FOR i = 0, n_elements(tmp)-1 DO $
121    dirnames = [dirnames, strsplit(tmp[i], path_sep(/search_path), /extract)]
122  dirnames = dirnames[1:*]
123;
124  fileout = 'dummy'
125  FOR i = 0, n_elements(filein)-1 DO BEGIN
126    dir = file_dirname(filein[i])
127    base = file_basename(filein[i])
128; try to complete the file name with .pro or .nc if needed...
129    CASE 1 OF
130      keyword_set(onlypro):BEGIN
131        promiss = strpos(base, '.pro', /reverse_search)
132        promiss = promiss - (strlen(base) - 4)
133        bad = where(promiss NE 0 OR strlen(base) LE 4, cnt)
134        IF cnt NE 0 THEN base[bad] = base[bad] + '.pro'
135      end
136      keyword_set(onlync):BEGIN
137        ncmiss = strpos(base, '.nc', /reverse_search)
138        ncmiss = ncmiss - (strlen(base) - 3)
139        bad = where(ncmiss NE 0 OR strlen(base) LE 3, cnt)
140        IF cnt NE 0 THEN base[bad] = base[bad] + '.nc'
141      END
142      ELSE:if strmid(base, 0, 1, /reverse_offset) NE '*' $
143        AND NOT keyword_set(nopro) THEN base = base + '{.pro,}'
144    ENDCASE
145; use dirnames only if dir eq '.'
146    IF dir EQ  '.' THEN BEGIN
147      if keyword_set(recursive) THEN $
148        found = file_search(dirnames, base, _extra = ex) $
149        ELSE found = file_search(dirnames + '/' + base, _extra = ex)
150    ENDIF ELSE found = file_search(dir + '/' + base, _extra = ex)
151    IF found[0] NE '' THEN BEGIN
152      IF keyword_set(firstfound) THEN BEGIN
153        IF keyword_set(unique) THEN return, found[uniq(found, sort(found))] $
154        ELSE return, found
155      ENDIF
156      fileout = [fileout, found]
157    ENDIF
158  ENDFOR
159  IF n_elements(fileout) EQ 1 THEN fileout = 'NOT FOUND' $
160  ELSE fileout = fileout[1:*]
161;
162  IF n_elements(fileout) GT 1 THEN BEGIN
163    IF keyword_set(unique) THEN fileout = fileout[uniq(fileout, sort(fileout))]
164  ENDIF ELSE fileout = fileout[0]
165;
166  RETURN, fileout
167END
Note: See TracBrowser for help on using the repository browser.