source: trunk/Utilities/find.pro @ 69

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

debug + new xxx

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