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