[2] | 1 | ;------------------------------------------------------------ |
---|
| 2 | ;------------------------------------------------------------ |
---|
| 3 | ;------------------------------------------------------------ |
---|
| 4 | ;+ |
---|
[128] | 5 | ; @file_comments |
---|
| 6 | ; same as find.pro except that as long as the file is 'NOT FOUND', |
---|
| 7 | ; isafile calls dialog_pickfile, to ask the user to select a file. |
---|
[2] | 8 | ; |
---|
[93] | 9 | ; @categories io |
---|
[2] | 10 | ; |
---|
[224] | 11 | ; @param FILEIN {in}{optional} |
---|
| 12 | ; a proposed name. If neither filein input parameter of filename keyword are |
---|
[136] | 13 | ; defined, the ask the user to choose a file. |
---|
[2] | 14 | ; |
---|
[224] | 15 | ; @keyword FILENAME |
---|
[136] | 16 | ; a proposed filename. |
---|
[2] | 17 | ; |
---|
[224] | 18 | ; @keyword IODIRECTORY |
---|
[136] | 19 | ; a directory where we look for the file. this |
---|
| 20 | ; keyword is taken into account only if the dirname |
---|
| 21 | ; of filein or filename is '.' |
---|
[11] | 22 | ; |
---|
[224] | 23 | ; @keyword NEW |
---|
| 24 | ; to specify that filename is a new file and that we should check only its |
---|
[136] | 25 | ; path |
---|
[11] | 26 | ; |
---|
[224] | 27 | ; @keyword ONLYPRO |
---|
[136] | 28 | ; force to look only at file ending with .pro |
---|
[69] | 29 | ; |
---|
[224] | 30 | ; @keyword ONLYNC |
---|
[136] | 31 | ; force to look only at file ending with .nc |
---|
[69] | 32 | ; |
---|
[224] | 33 | ; @keyword RECURSIVE |
---|
[136] | 34 | ; performs recursive searching of directory hierarchies. |
---|
| 35 | ; In a recursive search, find looks recursively for any and all |
---|
| 36 | ; subdirectories in the file hierarchy rooted at the IODIRECTORY argument. |
---|
[11] | 37 | ; |
---|
[224] | 38 | ; @keyword _EXTRA |
---|
[136] | 39 | ; used to pass your keywords |
---|
[118] | 40 | ; |
---|
[136] | 41 | ; all find, file_search and dialog_pickfile keywords (like title) can be used |
---|
[11] | 42 | ; |
---|
[224] | 43 | ; @returns |
---|
[136] | 44 | ; the filename with its path |
---|
[11] | 45 | ; |
---|
[128] | 46 | ; @examples |
---|
[2] | 47 | ; |
---|
[128] | 48 | ; IDL> print, isafile('/Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro') |
---|
[11] | 49 | ; /Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro |
---|
[128] | 50 | ; IDL> print, isafile('cm_4mesh.pro', iodir = '/Users/sebastie/SAXO_RD/Commons') |
---|
[11] | 51 | ; /Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro |
---|
[128] | 52 | ; IDL> print, isafile('cm_4mesh.pro', iodir = !path) |
---|
[11] | 53 | ; /Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro |
---|
[128] | 54 | ; IDL> print, isafile('cm_4mesh.pro', iodir = '/Users/sebastie/SAXO_RD', /recursive) |
---|
[11] | 55 | ; /Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro |
---|
[128] | 56 | ; IDL> print, isafile('cm_4mesh.pro', iodir = getenv('HOME'), /recursive) |
---|
[11] | 57 | ; /Users/sebastie/SAXO_RD/Commons/cm_4mesh.pro |
---|
[128] | 58 | ; IDL> print, isafile('fake_file.pro') |
---|
[11] | 59 | ; |
---|
[224] | 60 | ; @history |
---|
| 61 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[2] | 62 | ; 11/2/2000 |
---|
[11] | 63 | ; June 2005: Sebastien Masson: cleaning, use for file_* functions |
---|
[118] | 64 | ; |
---|
[224] | 65 | ; @version |
---|
| 66 | ; $Id$ |
---|
[2] | 67 | ;- |
---|
| 68 | ;------------------------------------------------------------ |
---|
| 69 | ;------------------------------------------------------------ |
---|
| 70 | ;------------------------------------------------------------ |
---|
[11] | 71 | FUNCTION isafile, filein, FILENAME = filename, IODIRECTORY = iodirectory $ |
---|
[69] | 72 | , NEW = new, RECURSIVE = RECURSIVE, ONLYPRO = onlypro $ |
---|
| 73 | , ONLYNC = onlync, _extra = ex |
---|
[2] | 74 | ;------------------------------------------------------------ |
---|
[114] | 75 | ; |
---|
| 76 | compile_opt idl2, strictarrsubs |
---|
| 77 | ; |
---|
[11] | 78 | CASE 1 OF |
---|
| 79 | (size(filein, /type))[0] EQ 7:fileout = filein |
---|
[114] | 80 | keyword_set(filename):fileout = filename[0] |
---|
[11] | 81 | ELSE:fileout = 'file that is not existing' |
---|
| 82 | ENDCASE |
---|
| 83 | if size(fileout, /type) NE 7 THEN return, -1 |
---|
| 84 | ; |
---|
[69] | 85 | CASE 1 OF |
---|
| 86 | keyword_set(onlypro): filter = '*.pro' |
---|
| 87 | keyword_set(onlync): filter = '*.nc' |
---|
| 88 | else: filter = '*' |
---|
| 89 | ENDCASE |
---|
| 90 | ; |
---|
[11] | 91 | basename = file_basename(fileout) |
---|
| 92 | dirname = file_dirname(fileout) |
---|
| 93 | ; should we redefine dirname? |
---|
| 94 | if keyword_set(iodirectory) AND dirname EQ '.' then dirname = iodirectory |
---|
| 95 | ; |
---|
| 96 | if keyword_set(new) then return, dirname + path_sep() + basename |
---|
| 97 | ; |
---|
| 98 | fileout = find(basename, iodirectory = dirname $ |
---|
[69] | 99 | , recursive = recursive, /unique, /firstfound, ONLYPRO = onlypro $ |
---|
| 100 | , ONLYNC = onlync, _extra = ex) |
---|
[11] | 101 | WHILE fileout[0] EQ 'NOT FOUND' DO BEGIN |
---|
[69] | 102 | fileout = dialog_pickfile(path = dirname[0], filter = filter, _extra = ex) |
---|
[11] | 103 | if fileout EQ '' THEN RETURN, report('check/find file canceled') |
---|
| 104 | ; check again everything... |
---|
| 105 | basename = file_basename(fileout) |
---|
| 106 | dirname = file_dirname(fileout) |
---|
| 107 | ; check if the name of the dirname is ok |
---|
| 108 | dirname = isadirectory(dirname, title = 'choose a directory for the file ' $ |
---|
| 109 | + basename) |
---|
| 110 | ; if we cancel the check |
---|
| 111 | IF size(dirname, /type) NE 7 THEN return, report('check/find file canceled') |
---|
| 112 | fileout = find(basename, iodirectory = dirname $ |
---|
[69] | 113 | , recursive = recursive, /unique, /firstfound, ONLYPRO = onlypro $ |
---|
| 114 | , ONLYNC = onlync, _extra = ex) |
---|
[11] | 115 | ENDWHILE |
---|
| 116 | ; |
---|
| 117 | RETURN, fileout |
---|
| 118 | END |
---|