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

Last change on this file since 229 was 224, checked in by pinsard, 17 years ago

improvements/corrections of some *.pro headers

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