cmsvread.pro
7.38 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
;+
; NAME:
; CMSVREAD
;
; AUTHOR:
; Craig B. Markwardt, NASA/GSFC Code 662, Greenbelt, MD 20770
; craigm@lheamail.gsfc.nasa.gov
;
; PURPOSE:
; Read a single variable from an open SAVE file
;
; CALLING SEQUENCE:
;
; CMSVREAD, UNIT, DATA [, NAME=NAME, /NO_DATA, VERSION=VERSION,
; TIMESTAMP=TIMESTAMP ]
;
; DESCRIPTION:
;
; CMSVREAD reads a single IDL variable from an open IDL SAVE file.
; The file should already have been opened as a normal file using
; OPENR.
;
; CMSVREAD is a simplified version of the CMSVLIB package, and as
; such is not capable of reading heap data (pointers) or object
; data. Strings, structures, and all array types are supported.
;
; This procedure is part of the CMSVLIB SAVE library for IDL by
; Craig Markwardt. You must have the full CMSVLIB core package
; installed in order for this procedure to function properly.
;
; ==================================================================
; Research Systems, Inc. has issued a separate license intended
; to resolve any potential conflict between this software and the
; IDL End User License Agreement. The text of that license
; can be found in the file LICENSE.RSI, included with this
; software library.
; ==================================================================
;
; INPUTS:
;
; UNIT - the open file unit.
;
; DATA - a named variable, into which the new data is to be read.
;
; KEYWORDS:
;
; NAME - upon output, the name of the saved variable is returned in
; this keyword. If a failure or end of file condition
; occurs, name will be undefined upon return.
;
; STRUCTURE_NAME - if the data to be read is a structure, upon
; output, this keyword will contain the name of the
; structure. A value of '' indicates an anonymous
; structure.
;
; SIZE - upon output, the SIZE type of the data is returned in this
; keyword.
;
; NO_DATA - if set, no data is read from the file, only the variable
; name and type.
;
; TIMESTAMP - after the first call to CMSVREAD on a newly opened
; file, this keyword will contain the file timestamp
; structure.
;
; VERSION - after the first call to CMSVREAD on a newly opened file,
; this keyword will contain the file version information,
; if available.
;
; QUIET - if set, error messages are not printed.
; Default: an error causes errors to be printed with MESSAGE
;
; STATUS - upon return, this keyword will contain 1 for success and
; 0 for failure.
;
; ERRMSG - upon return with a failure, this keyword will contain the
; error condition as a string.
;
; EXAMPLE:
;
; Read all variables from a file, and print help on them.
;
; openr, 50, 'test.sav'
; name = ''
; while n_elements(name) GT 0 do begin ;; EOF signalled by NAME undefined
; cmsvread, 50, data, name=name
; help, name, data
; end
; close, 50
;
; SEE ALSO:
;
; CMSVWRITE, CMRESTORE, CMSAVE, RESTORE, CMSVLIB
;
; MODIFICATION HISTORY:
; Written and documented, 11 Jan 2001, CM
; Added notification about RSI License, 13 May 2002, CM
; Remove support for undocumented AUTOPROMOTE64 keyword,
; 11 Jan 2010, CM
; NOTE: remember to modify CMSVLIB.PRO when changing library!
;
; $Id: cmsvread.pro,v 1.9 2010/01/11 08:58:13 craigm Exp $
;
;-
; Copyright (C) 2001, Craig Markwardt
; This software is provided as is without any warranty whatsoever.
; Permission to use, copy, modify, and distribute modified or
; unmodified copies is granted, provided this copyright and disclaimer
; are included unchanged.
;-
pro cmsvread, unit0, data, timestamp=tstamp, version=ver, $
name=name, size=sz, no_data=nodata, structure_name=stname, $
promote64=promote64, $
quiet=quiet, status=status, errmsg=errmsg
status = 0
catch, catcherr
if catcherr EQ 0 then lib = cmsvlib(/query) else lib = 0
catch, /cancel
if lib EQ 0 then begin
errmsg = 'ERROR: The CMSVLIB library must be in your IDL path.'
if keyword_set(quiet) then return else message, errmsg
endif
name = 0 & dummy = temporary(name)
data = 0 & dummy = temporary(data)
sz = 0 & dummy = temporary(sz)
tp = 0 & dummy = temporary(tp)
stname = 0 & dummy = temporary(stname)
if n_elements(unit0) EQ 0 then begin
errmsg = 'ERROR: UNIT is not defined'
if keyword_set(quiet) then return else message, errmsg
endif
unit = floor(unit0(0))
stat = fstat(unit)
if stat.read EQ 0 OR stat.open EQ 0 then begin
errmsg = 'ERROR: UNIT is not open for reading'
if keyword_set(quiet) then return else message, errmsg
endif
;; Thanks to Liam Gumley to show that one can check the file pointer
;; to see if we are at the start.
;; We are at the beginning of the file, make sure this is a proper
;; IDL save file.
if stat.cur_ptr EQ 0 then begin
cmsv_open, unit, 'FILENAME', pointer, access='R', /reopen, $
status=status, errmsg=errmsg
if status EQ 0 then begin
if keyword_set(quiet) then return else message, errmsg
endif
endif
done = 0
while NOT done do begin
block = 0 & dummy = temporary(block)
cmsv_rrec, block, p1, bdata, unit=unit, next_block=pnext, /init, $
block_type=bt, block_name=bn, status=status, errmsg=errmsg, $
promote64=promote64
if status EQ 0 then begin
if keyword_set(quiet) then return else message, errmsg
endif
case bn of
'END_MARKER': begin
done = 1
end
'TIMESTAMP': begin
tstamp = bdata
end
'VERSION': begin
ver = bdata
end
'VARIABLE': begin
sysvar = 0
DO_VARIABLE:
tp = 0
cmsv_rvtype, block, p1, name, sz, status=status, template=tp, $
unit=unit, system=sysvar, errmsg=errmsg, $
structure_name=stname
if status EQ 0 OR name EQ '' then begin
status = 0
if errmsg EQ '' then $
errmsg = 'ERROR: could not read variable name'
if keyword_set(quiet) then return else message, errmsg
endif
if NOT keyword_set(nodata) then begin
tp1 = sz(sz(0)+1)
if tp1 EQ 0 OR tp1 EQ 10 OR tp1 EQ 11 then begin
status = 0
if tp1 EQ 0 then $
errmsg = 'ERROR: variable type is undefined' $
else if tp1 EQ 10 OR tp1 EQ 11 then $
errmsg = 'ERROR: CMSVREAD cannot read heap or objects'
if keyword_set(quiet) then return else message, errmsg
endif
cmsv_rdata, block, p1, sz, data, template=tp, unit=unit, $
status=status, errmsg=errmsg
tp = 0
if status EQ 0 then begin
errmsg = 'ERROR: could not read data'
if keyword_set(quiet) then return else message, errmsg
endif
endif
done = 1
end
'SYSTEM_VARIABLE': begin
sysvar = 1
goto, DO_VARIABLE
end
ELSE: dummy = 1
endcase
point_lun, unit, pnext
endwhile
status = 1
return
end