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

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

correction of some *.pro using aspell list; introduction of default idldoc syntax

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