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

Last change on this file since 77 was 77, checked in by smasson, 18 years ago

tests tst_* work again (brokken since DATA/SRC split)

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