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

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

modification of headers : mainly blanks around = sign for keywords in declaration of function and pro

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