Stimfit @PACKAGE_VERSION@
Loading...
Searching...
No Matches
AxoGraph_ReadWrite.h
Go to the documentation of this file.
1#ifndef AXOGRAPH_READEWRITE_H
2#define AXOGRAPH_READEWRITE_H
3
4/* ----------------------------------------------------------------------------------
5
6 AxoGraph_ReadWrite : functions for reading and writing AxoGraph data files.
7
8 See also : the example programs which uses these functions, Demo_AxoGraph_ReadWrite
9 and Simple_AxoGraph_ReadWrite
10
11 This source code and the AxoGraph data file format are in the public domain.
12
13 To run on little endian hardware (Intel, etc.) __LITTLE_ENDIAN__ must be defined
14 This is done automatically under OS X / XCode
15
16 2008-12-14: Some modifications to avoid unfreed mallocs using std::vector
17 C. Schmidt-Hieber
18 ----------------------------------------------------------------------------------
19
20 The three AxoGraph file formats are descibed here for completeness, but this
21 information is not needed in order to use the supplied AxoGraph file functions
22 to read and write binary data. For information about reading and writing graph
23 display information, see the end of the section on the AxoGraph X file format.
24
25
26 AxoGraph Data File Format
27 =========================
28
29 Header
30 ------
31 Byte Type Contents
32 0 char[4] AxoGraph file header identifier = 'AxGr' - same as document type ID
33 4 short AxoGraph graph file format ID = 1
34 6 short Number of columns to follow
35
36
37 Each column
38 -----------
39 Byte Type Contents
40 0 long Number of points in the column ( columnPoints )
41 4 char[80] Column title (Pascal 'String' format) - S.I. units should be in brackets e.g. 'Current (pA)'
42 84 float 1st Data point
43 88 float 2nd Data point
44 .. .. ....
45 .. .. etc.
46
47
48 ----------------------------------------------------------------------------------
49
50 AxoGraph Digitized Data File Format
51 ===================================
52
53 Header
54 ------
55 Byte Type Contents
56 0 char[4] AxoGraph file header identifier = 'AxGr' - same as document type ID
57 4 short AxoGraph file format ID = 2
58 6 short Number of columns to follow
59
60
61 Each column
62 ----------------------
63 Byte Type Contents
64 0 long Number of points in the column ( columnPoints )
65 4 long Data type
66 8 char[80] Column title (Pascal 'String' format) - S.I. units should be in brackets e.g. 'Current (pA)'
67 84 float Scaling Factor
68 88 short 1st Data point
69 90 short 2nd Data point
70 .. ... ....
71 .. ... etc.
72
73
74 ----------------------------------------------------------------------------------
75
76 AxoGraph X Data File Format
77 ===================================
78
79 Header
80 ------
81 Byte Type Contents
82 0 char[4] AxoGraph file header identifier = 'AxGx' - same as filename extension
83 4 long AxoGraph X file format ID = a number between 3 (earliest version) and 6 (latest version)
84 8 long Number of columns to follow
85
86
87 Each column
88 ----------------------
89 Byte Type Contents
90 0 long Number of points in the column ( columnPoints )
91 4 long Column type
92 8 long Length of column title in bytes (Unicode - 2 bytes per character)
93 12 char* Column title (Unicode 2 byte per char) - S.I. units should be in brackets e.g. 'Current (pA)'
94 ?? ?? Byte offset depends on length of column title string.
95 .. ... Numeric type and layout depend on the column type
96 .. ... ....
97 .. ... etc.
98
99
100 Six column types are supported...
101 4: short
102 5: long
103 6: float
104 7: double
105 9: 'series'
106 10: 'scaled short'
107
108 In the first four column types, data is stored as a simple array of the corresponding type.
109 The 'scaled short' column type stores data as a 'double' scaling factor and offset, and a 'short' array.
110 The 'series' column type stores data as a 'double' first value and a 'double' increment.
111
112 Prior to AxoGraph X, all graph display information was stored in the 'resource fork' of the file,
113 and the resource fork format was not documented. In contrast, AxoGraph X has a 'flat' format
114 with all display information stored immediately following the data columns.
115 It is safe to simply leave out this information. AxoGraph X will use default parameters
116 when the file is read in. For greater control of graph appearance when creating a file
117 it may be necessary to add display format information. When reading in a file,
118 it may be necessary to access the 'Notes' string. The following is a preliminary description
119 of the file format used to store important elements of graph display information.
120 Reading 'notes' and graph format info is not supported in the AxoGraph_ReadWrite example functions.
121
122 The Comment and Notes strings are stored immediately after the last data column.
123 Both are stored in Unicode string format..
124
125
126 Unicode string format
127 ----------------------
128 long Length of string in bytes
129 char* Notes string (Unicode 2 byte per char)
130 For Latin1 strings, every second byte is an ASCII character code
131
132 Each trace consists of a pair of columns. The trace header specifies the
133 X and Y column numbers, and other trace-specific information.
134 'bool' header fields are stored as long int: false = 0, true = 1
135
136 The number of traces is stored immediately after the comment and notes strings.
137
138 long Number of trace headers to follow
139
140 Header for each trace
141 ----------------------
142 long header version number (currently = 2)
143 long X column number
144 long Y column number
145 long Error bar column number or -1 if no error bars
146 long Negative error bar column number or -1 if no negative error bars
147
148 long Group number that this column belongs to
149 bool Trace shown? False if trace is hidden
150
151 double Minimum X data point in this trace
152 double Maximum X data point in this trace (if both are zero, they will be recalculated)
153 double Minimum positive X data point in this trace (used in log-axis format)
154
155 bool True if X axis data is regularly spaced
156 bool True if X axis data is monotonic (each point > previous point)
157 double Interval between points for regular X axis data
158
159 double Minimum Y data point in this trace
160 double Maximum Y data point in this trace (if both are zero, they will be recalculated)
161 double Minimum positive Y data point in this trace (used in log-axis format)
162
163 long Trace color with RGB values serialized into a long int
164
165 bool True if a line plot joining the data points is displayed
166 double Thickness of the line plot (can be less than 1.0 for fine lines)
167 long Pen style (zero for solid line, non zero for dashed lines)
168
169 bool True if symbols are displayed
170 long Symbol type
171 long Symbol size (radius in pixels)
172
173 bool True if some symbols are to be skipped
174 bool True if symbols are to be skipped by distance instead of number of points
175 long Minimum separation of symbols in pixels is previous parameter is true
176
177 bool True for a histogram plot
178 long Type of histogram (zero for standard solid fill)
179 long Separation between adjacent histogram bars expressed as a percentage of bar width
180
181 bool True if error bars are displayed
182 bool True if a positive error bar is displayed
183 bool True if a negative error bar is displayed
184 long Error bar width in pixels
185
186 ---------------------------------------------------------------------------------- */
187
188// uncomment the following line to run on little endian hardware ( byte swaps data before reading or writing )
189#ifdef __APPLE__
190 #include <machine/endian.h>
191#elif defined(__MINGW32__)
192 #define __LITTLE_ENDIAN__
193#elif !defined(_MSC_VER)
194 #include <endian.h>
195#else
196 #define __LITTLE_ENDIAN__
197#endif
198
199#ifdef __LITTLE_ENDIAN
200 #define __LITTLE_ENDIAN__
201#endif
202
203#include "longdef.h"
204#include "fileUtils.h"
205#include "./../stfio.h"
206
207// errors numbers
208const short kAG_MemoryErr = -21;
209const short kAG_FormatErr = -23;
210const short kAG_VersionErr = -24;
211
212// file format id's
215const short kAxoGraph_X_Format = 6;
217
218const axgchar kAxoGraph4DocType[4] = { 'A', 'x', 'G', 'r' };
219const axgchar kAxoGraphXDocType[4] = { 'a', 'x', 'g', 'x' };
220
221// column header for AxoGraph graph files
227
228// x-axis column header for AxoGraph digitized files
236
237// y-axis column header for AxoGraph digitized files
244
245// column header for AxoGraph X files
252
254{
255 long nHeaderVersion; // header version number (currently = 2)
256 long nColumnX; // X column number
257 long nColumnY; // Y column number
258 long nError; // Error bar column number or -1 if no error bars
259 long nErrorBarColumn; // Negative error bar column number or -1 if no negative error bars
260
261 long nGroup; // Group number that this column belongs to
262 bool isShown; // Trace shown? False if trace is hidden
263
264 double dMinX; // Minimum X data point in this trace
265 double dMaxX; // Maximum X data point in this trace (if both are zero, they will be recalculated)
266 double dMinPosX; // Minimum positive X data point in this trace (used in log-axis format)
267
268 bool bXEqSpaced; // True if X axis data is regularly spaced
269 bool bXMonotonic; // True if X axis data is monotonic (each point > previous point)
270 double dXInterval; // Interval between points for regular X axis data
271
272 double dMinY; // Minimum Y data point in this trace
273 double dMaxY; // Maximum Y data point in this trace (if both are zero, they will be recalculated)
274 double dMinPosY; // Minimum positive Y data point in this trace (used in log-axis format)
275
276 long nRGB; // Trace color with RGB values serialized into a long int
277
278 bool bLinejoin; // True if a line plot joining the data points is displayed
279 double dLineThickness; // Thickness of the line plot (can be less than 1.0 for fine lines)
280 long nLineStyle; // Pen style (zero for solid line, non zero for dashed lines)
281
282 bool bSymbol; // True if symbols are displayed
283 long nSymbolType; // Symbol type
284 long nSymbolSize; // Symbol size (radius in pixels)
285
286 bool bSymbolSkip; // True if some symbols are to be skipped
287 bool bSymbolSkipDist; // True if symbols are to be skipped by distance instead of number of points
288 long nSympolDist; // Minimum separation of symbols in pixes is previous parameter is true
289
290 bool bHisto; // True for a histogram plot
291 long nHistoType; // Type of histogram (zero for standard solid fill)
292 long nHistoDist; // Separation between adjacent histogram bars expressed as a percentage of bar width
293
294 bool bError; // True if error bars are displayed
295 bool bErrorPos; // True if a positive error bar is displayed
296 bool bErrorNeg; // True if a negative error bar is displayed
297 long nErrorWidth; // Error bar width in pixels
298
299};
300
301//============= ColumnData structure ======================
302
303// This enum is copied from AxoGraph X source code
304// The only types used for data file columns are...
305// ShortArrayType = 4 IntArrayType = 5
306// FloatArrayType = 6 DoubleArrayType = 7
307// SeriesArrayType = 9 ScaledShortArrayType = 10
323
326 double increment;
327};
328
330 double scale;
331 double offset;
332 std::vector<short> shortArray;
333};
334
335
348
349
350// ----------- AxoGraph Read and Write functions -------------
351
352int AG_GetFileFormat( filehandle refNum, int *fileFormat );
353
354// Check that the file referenced by refNum is an AxoGraph data file
355// and read in the file format. Legal values are 1, 2, or 3,
356// corresponding to AxoGraph Graph, Digitized, or AxoGraph X formats.
357// Called once per file. Returns 0 if all goes well.
358// If an error occurs, returns the result from the file access functions,
359// or kAG_FormatErr if file is not in AxoGraph format,
360// or kAG_VersionErr if the file is of a more recent version than supported by this code.
361
362
363int AG_GetNumberOfColumns( filehandle refNum, const int fileFormat, AXGLONG *numberOfColumns );
364
365// Read in the number of columns to follow in this file.
366// Called once per file. Returns 0 if all goes well.
367// If an error occurs, returns the result from the file access functions,
368
369
370int AG_ReadColumn( filehandle refNum, const int fileFormat, const int columnNumber, ColumnData *columnData );
371
372// Read in a column from any AxoGraph data file.
373// Called once for each column in the file.
374// Returns data in a pointer in structure that contains the number of points,
375// the column title, and the column data.
376// This function allocates new pointers of the appropriate size, reads the data into
377// them and returns it in columnData.
378
379std::string AG_ReadComment( filehandle refNum );
380
381// Read in comment from an AxoGraph X data file.
382
383std::string AG_ReadNotes( filehandle refNum );
384
385// Read in notes from an AxoGraph X data file.
386
387std::string AG_ReadTraceHeaders( filehandle refNum );
388
389// Read in trace headers from an AxoGraph X data file.
390
391std::string AG_ParseDate( const std::string& notes );
392std::string AG_ParseTime( const std::string& notes );
393
394int AG_ReadFloatColumn( filehandle refNum, const int fileFormat, const int columnNumber, ColumnData *columnData );
395
396// Read in a column from any AxoGraph data file.
397// Convert the column data to a float array, regardless of the input column format
398// Called once for each column in the file.
399// Returns data in a pointer in structure that contains the number of points,
400// the column title, and the column data.
401// This function allocates new pointers of the appropriate size, reads the data into
402// them and returns it in columnData.
403
404#endif
405
@ IntArrayType
@ BoolArrayType
@ ReferenceType
@ StringType
@ ShortArrayType
@ FloatArrayType
@ DoubleArrayType
@ StringArrayType
@ DoubleType
@ SeriesArrayType
@ ScaledShortArrayType
int AG_GetNumberOfColumns(filehandle refNum, const int fileFormat, AXGLONG *numberOfColumns)
std::string AG_ReadTraceHeaders(filehandle refNum)
std::string AG_ReadNotes(filehandle refNum)
std::string AG_ReadComment(filehandle refNum)
int AG_GetFileFormat(filehandle refNum, int *fileFormat)
const axgchar kAxoGraphXDocType[4]
const short kAG_MemoryErr
std::string AG_ParseDate(const std::string &notes)
const short kAxoGraph_X_Digitized_Format
std::string AG_ParseTime(const std::string &notes)
const short kAxoGraph_X_Format
const short kAG_VersionErr
int AG_ReadFloatColumn(filehandle refNum, const int fileFormat, const int columnNumber, ColumnData *columnData)
const axgchar kAxoGraph4DocType[4]
const short kAxoGraph_Digitized_Format
int AG_ReadColumn(filehandle refNum, const int fileFormat, const int columnNumber, ColumnData *columnData)
const short kAxoGraph_Graph_Format
const short kAG_FormatErr
std::vector< float > Vector_float
Definition core.h:56
std::vector< double > Vector_double
Definition core.h:55
HANDLE filehandle
Definition fileUtils.h:20
#define AXGLONG
Definition longdef.h:11
header file for libstfio
unsigned char axgchar
Definition stringUtils.h:7
std::vector< int > intArray
std::vector< short > shortArray
Vector_double doubleArray
ScaledShortArray scaledShortArray
SeriesArray seriesArray
Vector_float floatArray
std::string title
std::vector< short > shortArray