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