cgpickfile.pro
10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
; docformat = 'rst'
;
; NAME:
; cgPickfile
;
; PURPOSE:
; This is a utility program for selecting data files. It is a wrapper for DIALOG_PICKFILE,
; with the additional functionality of being able to "remember" the name and directory of
; the last file selected by the program. It basically allows you to start the next file
; selection from the location of the previous file selection.
;******************************************************************************************;
; ;
; Copyright (c) 2011, by Fanning Software Consulting, Inc. All rights reserved. ;
; ;
; Redistribution and use in source and binary forms, with or without ;
; modification, are permitted provided that the following conditions are met: ;
; ;
; * Redistributions of source code must retain the above copyright ;
; notice, this list of conditions and the following disclaimer. ;
; * Redistributions in binary form must reproduce the above copyright ;
; notice, this list of conditions and the following disclaimer in the ;
; documentation and/or other materials provided with the distribution. ;
; * Neither the name of Fanning Software Consulting, Inc. nor the names of its ;
; contributors may be used to endorse or promote products derived from this ;
; software without specific prior written permission. ;
; ;
; THIS SOFTWARE IS PROVIDED BY FANNING SOFTWARE CONSULTING, INC. ''AS IS'' AND ANY ;
; EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES ;
; OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT ;
; SHALL FANNING SOFTWARE CONSULTING, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, ;
; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED ;
; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; ;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ;
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ;
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS ;
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ;
;******************************************************************************************;
;
;+
; This is a utility program for selecting data files. It is a wrapper for DIALOG_PICKFILE,
; with the additional functionality of being able to "remember" the name and directory of
; the last file selected by the program. It basically allows you to start the next file
; selection from the location of the previous file selection.
;
; :Categories:
; Utility
;
; :Returns:
; The fully-qualified name of the selected file or a null string if the CANCEL
; button was selected.
;
; :Keywords:
; datadir: in, optional, type=string
; Set this keyword to the name of the data directory. If not set, the program
; assumes the data directory is rooted in the directory that holds this program
; file, or the directory directly above it. If it can't find a "data" directory
; in either of these two locations, the current directory is used as the "data"
; directory. The data directory is ONLY used if there is no "current" last
; directory. In other words, it is only used if cgPickfile has not been called
; in the current IDL session.
; demo: in, optional, type=boolean, default=0
; If set, starts in the !DIR/examples/data directory.
; jpeg: in, optional, type=boolean, default=0
; If set, starts in the "jpeg" directory. It assumes the jpeg directory is rooted
; in the "data" directory.
; hdf: in, optional, type=boolean, default=0
; If set, starts in the "hdf" directory. It assumes the hdf directory is rooted
; in the "data" directory.
; last_file: in, optional, type=boolean, default=0
; If set, the name of the last file opened is placed in the filename widget.
; lidar: in, optional, type=boolean, default=0
; If set, starts in the "lidar" directory. It assumes the lidar directory is
; rooted in the "data" directory.
; ncdf: in, optional, type=boolean, default=0
; If set, starts in the "ncdf" directory. It assumes the ncdf directory is
; rooted in the "data" directory.
; png: in, optional, type=boolean, default=0
; If set, starts in the "png" directory. It assumes the png directory is
; rooted in the "data" directory.
; tiff: in, optional, type=boolean, default=0
; If set, starts in the "tiff" directory. It assumes the tiff directory is
; rooted in the "data" directory.
; title: in, optional, type=string, default="Please Select a File"
; The title for the selection dialog window. If the `Write` keyword is set,
; the default title becomes "Please Select a File for Writing".
; write: in, optional, type=boolean, default=0
; Set this keyword to change the default title to "Please Select a File for Writing".
; _ref_extra: in, optional
; Accepts any input keywords to DIALOG_PICKFILE (e.g., FILTER).
;
; :Author:
; FANNING SOFTWARE CONSULTING::
; David W. Fanning
; 1645 Sheely Drive
; Fort Collins, CO 80526 USA
; Phone: 970-221-0438
; E-mail: david@idlcoyote.com
; Coyote's Guide to IDL Programming: http://www.idlcoyote.com
;
; :History:
; Change History::
; Adapted from FSC_PICKFILE to be a Coyote Graphics routine by David W. Fanning, 4 Aug 2011.
; Added keywords TITLE and WRITE to work around a bug in Dialog_Pickfile that clips the
; input filenames. 25 Feb 2012. DWF.
;
; :Copyright:
; Copyright (c) 2011, Fanning Software Consulting, Inc.
;-
FUNCTION cgPickfile, $
DATADIR=datadir, $
DEMO=demo, $
JPEG=jpeg, $
HDF=hdf, $
LAST_FILE=last_file, $
LIDAR=lidar, $
NCDF=ncdf, $
PNG=png, $
TIFF=tiff, $
TITLE=title, $
WRITE=write, $
_REF_EXTRA=extra
On_Error, 2 ; Return to caller.
Compile_Opt idl2
; Check keywords to see if anything is set.
demo = Keyword_Set(demo)
jpeg = Keyword_Set(jpeg)
hdf = Keyword_Set(hdf)
lidar = Keyword_Set(lidar)
ncdf = Keyword_Set(ncdf)
png = Keyword_Set(png)
tiff = Keyword_Set(tiff)
keywordsAreSet = Total(demo+jpeg+hdf+lidar+ncdf+png+tiff) GT 0
; There is a bug in Windows that causes Dialog_Pickfile to clip the
; input filename unless the WRITE keyword is set. So, in this program
; the WRITE keyword is *always* set. But this means we have to pay attention
; to the TITLE, too. This is where we do all of that.
IF N_Elements(title) EQ 0 THEN BEGIN
IF Keyword_Set(write) $
THEN title = "Please Select a File for Writing" $
ELSE title = "Please Select a File"
ENDIF
; Find the "data" subdirectory. If it can't be found, then the
; current directory is the data directory.
IF N_Elements(dataDir) EQ 0 THEN BEGIN
dataDir = ProgramRootDir() + 'data'
IF ~File_Test(dataDir, /DIRECTORY) THEN BEGIN
dataDir = ProgramRootDir(/ONEUP) + 'data'
IF ~File_Test(dataDir, /DIRECTORY) THEN CD, CURRENT=dataDir
ENDIF
ENDIF
; Be sure the dataDir exists.
IF ~File_Test(dataDir, /DIRECTORY) THEN BEGIN
Message, 'Specified data directory cannot be found. Using current directory.', /Informational
CD, CURRENT=dataDir
ENDIF
; If a keyword is set, define the image directory.
IF keywordsAreSet THEN BEGIN
CASE 1 OF
jpeg: imageDir = 'jpeg'
hdf: imageDir = 'hdf'
lidar: imageDir = 'lidar'
ncdf: imageDir = 'netCDF'
png: imageDir = 'png'
tiff: imageDir = 'tiff'
ELSE:
ENDCASE
; If imageDir is defined, use it. Otherwise, use the dataDir.
IF N_Elements(imageDir) NE 0 THEN BEGIN
checkDir = dataDir + Path_Sep() + imageDir
ENDIF ELSE BEGIN
checkDir = dataDir
ENDELSE
; Did the user chose a demo directory?
IF Keyword_Set(demo) THEN BEGIN
checkDir= File_DirName(Filepath(SUBDIRECTORY=['examples','data'], '*'))
ENDIF
IF ~File_Test(checkDir, /DIRECTORY) THEN fileDir = dataDir ELSE fileDir = checkDir
ENDIF ELSE BEGIN
; If the lastDir was saved, use it. Otherwise, the dataDir.
DEFSYSV, '!cgPickfile_LastDir', EXISTS=exists
IF ~exists THEN fileDir = dataDir ELSE fileDir = !cgPickfile_LastDir
ENDELSE
; Should we use the last file?
IF Keyword_Set(last_file) THEN BEGIN
; If the last file was saved, use it. Otherwise, the nothing to use.
DEFSYSV, '!cgPickfile_LastFile', EXISTS=exists
IF exists THEN lastFile = !cgPickfile_LastFile
ENDIF
; Has the fileDir been defined. If not, use the dataDir.
IF N_Elements(fileDir) EQ 0 THEN fileDir = dataDir
file = Dialog_Pickfile(PATH=fileDir, GET_PATH=selectedDir, $
FILE=lastFile, _STRICT_EXTRA=extra, /WRITE, TITLE=title)
; Save the last directory and filename. Make sure you are working
; with a scalar.
IF file[0] NE "" THEN BEGIN
DEFSYSV, '!cgPickfile_LastDir', EXISTS=exists
IF (exists AND (N_Elements(selectedDir) NE 0)) THEN BEGIN
!cgPickfile_LastDir = selectedDir
ENDIF ELSE BEGIN
IF N_Elements(selectedDir) NE 0 THEN DEFSYSV, '!cgPickfile_LastDir', selectedDir
ENDELSE
DEFSYSV, '!cgPickfile_LastFile', EXISTS=exists
IF exists THEN BEGIN
!cgPickfile_LastFile = File_BaseName(file[0])
ENDIF ELSE BEGIN
DEFSYSV, '!cgPickfile_LastFile', File_BaseName(file[0])
ENDELSE
ENDIF
RETURN, file
END