Checkin of new directory structure.
[pspp-builds.git] / doc / data-file-format.texi
1 @node Data File Format, q2c Input Format, Portable File Format, Top
2 @appendix Data File Format
3
4 PSPP necessarily uses the same format for system files as do the
5 products with which it is compatible.  This chapter is a description of
6 that format.
7
8 There are three data types used in system files: 32-bit integers, 64-bit
9 floating points, and 1-byte characters.  In this document these will
10 simply be referred to as @code{int32}, @code{flt64}, and @code{char},
11 the names that are used in the PSPP source code.  Every field of type
12 @code{int32} or @code{flt64} is aligned on a 32-bit boundary.
13
14 The endianness of data in PSPP system files is not specified.  System
15 files output on a computer of a particular endianness will have the
16 endianness of that computer.  However, PSPP can read files of either
17 endianness, regardless of its host computer's endianness.  PSPP
18 translates endianness for both integer and floating point numbers.
19
20 Floating point formats are also not specified.  PSPP does not
21 translate between floating point formats.  This is unlikely to be a
22 problem as all modern computer architectures use IEEE 754 format for
23 floating point representation.
24
25 The PSPP system-missing value is represented by the largest possible
26 negative number in the floating point format; in C, this is most likely
27 @code{-DBL_MAX}.  There are two other important values used in missing
28 values: @code{HIGHEST} and @code{LOWEST}.  These are represented by the
29 largest possible positive number (probably @code{DBL_MAX}) and the
30 second-largest negative number.  The latter must be determined in a
31 system-dependent manner; in IEEE 754 format it is represented by value
32 @code{0xffeffffffffffffe}.
33
34 System files are divided into records.  Each record begins with an
35 @code{int32} giving a numeric record type.  Individual record types are
36 described below:
37
38 @menu
39 * File Header Record::          
40 * Variable Record::             
41 * Value Label Record::          
42 * Value Label Variable Record::  
43 * Document Record::             
44 * Machine int32 Info Record::   
45 * Machine flt64 Info Record::   
46 * Auxilliary Variable Parameter Record::
47 * Long Variable Names Record::
48 * Miscellaneous Informational Records::  
49 * Dictionary Termination Record::  
50 * Data Record::                 
51 @end menu
52
53 @node File Header Record, Variable Record, Data File Format, Data File Format
54 @section File Header Record
55
56 The file header is always the first record in the file.
57
58 @example
59 struct sysfile_header
60   @{
61     char                rec_type[4];
62     char                prod_name[60];
63     int32               layout_code;
64     int32               case_size;
65     int32               compressed;
66     int32               weight_index;
67     int32               ncases;
68     flt64               bias;
69     char                creation_date[9];
70     char                creation_time[8];
71     char                file_label[64];
72     char                padding[3];
73   @};
74 @end example
75
76 @table @code
77 @item char rec_type[4];
78 Record type code.  Always set to @samp{$FL2}.  This is the only record
79 for which the record type is not of type @code{int32}.
80
81 @item char prod_name[60];
82 Product identification string.  This always begins with the characters
83 @samp{@@(#) SPSS DATA FILE}.  PSPP uses the remaining characters to
84 give its version and the operating system name; for example, @samp{GNU
85 pspp 0.1.4 - sparc-sun-solaris2.5.2}.  The string is truncated if it
86 would be longer than 60 characters; otherwise it is padded on the right
87 with spaces.
88
89 @item int32 layout_code;
90 Always set to 2.  PSPP reads this value to determine the
91 file's endianness.
92
93 @item int32 case_size;
94 Number of data elements per case.  This is the number of variables,
95 except that long string variables add extra data elements (one for every
96 8 characters after the first 8).
97 When reading system files, PSPP will use this value unless it is set
98 to -1, in which case it will determine the number of data elements by
99 context.  When writing system files PSPP always uses this value.
100
101 @item int32 compressed;
102 Set to 1 if the data in the file is compressed, 0 otherwise.
103
104 @item int32 weight_index;
105 If one of the variables in the data set is used as a weighting variable,
106 set to the index of that variable.  Otherwise, set to 0.
107
108 @item int32 ncases;
109 Set to the number of cases in the file if it is known, or -1 otherwise.
110
111 In the general case it is not possible to determine the number of cases
112 that will be output to a system file at the time that the header is
113 written.  The way that this is dealt with is by writing the entire
114 system file, including the header, then seeking back to the beginning of
115 the file and writing just the @code{ncases} field.  For `files' in which
116 this is not valid, the seek operation fails.  In this case,
117 @code{ncases} remains -1.
118
119 @item flt64 bias;
120 Compression bias.  Always set to 100.  The significance of this value is
121 that only numbers between @code{(1 - bias)} and @code{(251 - bias)} can
122 be compressed.
123
124 @item char creation_date[9];
125 Set to the date of creation of the system file, in @samp{dd mmm yy}
126 format, with the month as standard English abbreviations, using an
127 initial capital letter and following with lowercase.  If the date is not
128 available then this field is arbitrarily set to @samp{01 Jan 70}.
129
130 @item char creation_time[8];
131 Set to the time of creation of the system file, in @samp{hh:mm:ss}
132 format and using 24-hour time.  If the time is not available then this
133 field is arbitrarily set to @samp{00:00:00}.
134
135 @item char file_label[64];
136 Set the the file label declared by the user, if any.  Padded on the
137 right with spaces.
138
139 @item char padding[3];
140 Ignored padding bytes to make the structure a multiple of 32 bits in
141 length.  Set to zeros.
142 @end table
143
144 @node Variable Record, Value Label Record, File Header Record, Data File Format
145 @section Variable Record
146
147 Immediately following the header must come the variable records.  There
148 must be one variable record for every variable and every 8 characters in
149 a long string beyond the first 8; i.e., there must be exactly as many
150 variable records as the value specified for @code{case_size} in the file
151 header record.
152
153 @example
154 struct sysfile_variable
155   @{
156     int32               rec_type;
157     int32               type;
158     int32               has_var_label;
159     int32               n_missing_values;
160     int32               print;
161     int32               write;
162     char                name[8];
163
164     /* The following two fields are present 
165        only if has_var_label is 1. */
166     int32               label_len;
167     char                label[/* variable length */];
168
169     /* The following field is present only
170        if n_missing_values is not 0. */
171     flt64               missing_values[/* variable length*/];
172   @};
173 @end example
174
175 @table @code
176 @item int32 rec_type;
177 Record type code.  Always set to 2.
178
179 @item int32 type;
180 Variable type code.  Set to 0 for a numeric variable.  For a short
181 string variable or the first part of a long string variable, this is set
182 to the width of the string.  For the second and subsequent parts of a
183 long string variable, set to -1, and the remaining fields in the
184 structure are ignored.
185
186 @item int32 has_var_label;
187 If this variable has a variable label, set to 1; otherwise, set to 0.
188
189 @item int32 n_missing_values;
190 If the variable has no missing values, set to 0.  If the variable has
191 one, two, or three discrete missing values, set to 1, 2, or 3,
192 respectively.  If the variable has a range for missing variables, set to
193 -2; if the variable has a range for missing variables plus a single
194 discrete value, set to -3.
195
196 @item int32 print;
197 Print format for this variable.  See below.
198
199 @item int32 write;
200 Write format for this variable.  See below.
201
202 @item char name[8];
203 Variable name.  The variable name must begin with a capital letter or
204 the at-sign (@samp{@@}).  Subsequent characters may also be octothorpes
205 (@samp{#}), dollar signs (@samp{$}), underscores (@samp{_}), or full
206 stops (@samp{.}).  The variable name is padded on the right with spaces.
207
208 @item int32 label_len;
209 This field is present only if @code{has_var_label} is set to 1.  It is
210 set to the length, in characters, of the variable label, which must be a
211 number between 0 and 120.
212
213 @item char label[/* variable length */];
214 This field is present only if @code{has_var_label} is set to 1.  It has
215 length @code{label_len}, rounded up to the nearest multiple of 32 bits.
216 The first @code{label_len} characters are the variable's variable label.
217
218 @item flt64 missing_values[/* variable length */];
219 This field is present only if @code{n_missing_values} is not 0.  It has
220 the same number of elements as the absolute value of
221 @code{n_missing_values}.  For discrete missing values, each element
222 represents one missing value.  When a range is present, the first
223 element denotes the minimum value in the range, and the second element
224 denotes the maximum value in the range.  When a range plus a value are
225 present, the third element denotes the additional discrete missing
226 value.  HIGHEST and LOWEST are indicated as described in the chapter
227 introduction.
228 @end table
229
230 The @code{print} and @code{write} members of sysfile_variable are output
231 formats coded into @code{int32} types.  The LSB (least-significant byte)
232 of the @code{int32} represents the number of decimal places, and the
233 next two bytes in order of increasing significance represent field width
234 and format type, respectively.  The MSB (most-significant byte) is not
235 used and should be set to zero.
236
237 Format types are defined as follows:
238 @table @asis
239 @item 0
240 Not used.
241 @item 1
242 @code{A}
243 @item 2
244 @code{AHEX}
245 @item 3
246 @code{COMMA}
247 @item 4
248 @code{DOLLAR}
249 @item 5
250 @code{F}
251 @item 6
252 @code{IB}
253 @item 7
254 @code{PIBHEX}
255 @item 8
256 @code{P}
257 @item 9
258 @code{PIB}
259 @item 10
260 @code{PK}
261 @item 11
262 @code{RB}
263 @item 12
264 @code{RBHEX}
265 @item 13
266 Not used.
267 @item 14
268 Not used.
269 @item 15
270 @code{Z}
271 @item 16
272 @code{N}
273 @item 17
274 @code{E}
275 @item 18
276 Not used.
277 @item 19
278 Not used.
279 @item 20
280 @code{DATE}
281 @item 21
282 @code{TIME}
283 @item 22
284 @code{DATETIME}
285 @item 23
286 @code{ADATE}
287 @item 24
288 @code{JDATE}
289 @item 25
290 @code{DTIME}
291 @item 26
292 @code{WKDAY}
293 @item 27
294 @code{MONTH}
295 @item 28
296 @code{MOYR}
297 @item 29
298 @code{QYR}
299 @item 30
300 @code{WKYR}
301 @item 31
302 @code{PCT}
303 @item 32
304 @code{DOT}
305 @item 33
306 @code{CCA}
307 @item 34
308 @code{CCB}
309 @item 35
310 @code{CCC}
311 @item 36
312 @code{CCD}
313 @item 37
314 @code{CCE}
315 @item 38
316 @code{EDATE}
317 @item 39
318 @code{SDATE}
319 @end table
320
321 @node Value Label Record, Value Label Variable Record, Variable Record, Data File Format
322 @section Value Label Record
323
324 Value label records must follow the variable records and must precede
325 the header termination record.  Other than this, they may appear
326 anywhere in the system file.  Every value label record must be
327 immediately followed by a label variable record, described below.
328
329 Value label records begin with @code{rec_type}, an @code{int32} value
330 set to the record type of 3.  This is followed by @code{count}, an
331 @code{int32} value set to the number of value labels present in this
332 record.
333
334 These two fields are followed by a series of @code{count} tuples.  Each
335 tuple is divided into two fields, the value and the label.  The first of
336 these, the value, is composed of a 64-bit value, which is either a
337 @code{flt64} value or up to 8 characters (padded on the right to 8
338 bytes) denoting a short string value.  Whether the value is a
339 @code{flt64} or a character string is not defined inside the value label
340 record.
341
342 The second field in the tuple, the label, has variable length.  The
343 first @code{char} is a count of the number of characters in the value
344 label.  The remainder of the field is the label itself.  The field is
345 padded on the right to a multiple of 64 bits in length.
346
347 @node Value Label Variable Record, Document Record, Value Label Record, Data File Format
348 @section Value Label Variable Record
349
350 Every value label variable record must be immediately preceded by a
351 value label record, described above.
352
353 @example
354 struct sysfile_value_label_variable
355   @{
356      int32              rec_type;
357      int32              count;
358      int32              vars[/* variable length */];
359   @};
360 @end example
361
362 @table @code
363 @item int32 rec_type;
364 Record type.  Always set to 4.
365
366 @item int32 count;
367 Number of variables that the associated value labels from the value
368 label record are to be applied.
369
370 @item int32 vars[/* variable length];
371 A list of variables to which to apply the value labels.  There are
372 @code{count} elements.
373 @end table
374
375 @node Document Record, Machine int32 Info Record, Value Label Variable Record, Data File Format
376 @section Document Record
377
378 There must be no more than one document record per system file.
379 Document records must follow the variable records and precede the
380 dictionary termination record.
381
382 @example
383 struct sysfile_document
384   @{
385     int32               rec_type;
386     int32               n_lines;
387     char                lines[/* variable length */][80];
388   @};
389 @end example
390
391 @table @code
392 @item int32 rec_type;
393 Record type.  Always set to 6.
394
395 @item int32 n_lines;
396 Number of lines of documents present.
397
398 @item char lines[/* variable length */][80];
399 Document lines.  The number of elements is defined by @code{n_lines}.
400 Lines shorter than 80 characters are padded on the right with spaces.
401 @end table
402
403 @node Machine int32 Info Record, Machine flt64 Info Record, Document Record, Data File Format
404 @section Machine @code{int32} Info Record
405
406 There must be no more than one machine @code{int32} info record per
407 system file.  Machine @code{int32} info records must follow the variable
408 records and precede the dictionary termination record.
409
410 @example
411 struct sysfile_machine_int32_info
412   @{
413     /* Header. */
414     int32               rec_type;
415     int32               subtype;
416     int32               size;
417     int32               count;
418
419     /* Data. */
420     int32               version_major;
421     int32               version_minor;
422     int32               version_revision;
423     int32               machine_code;
424     int32               floating_point_rep;
425     int32               compression_code;
426     int32               endianness;
427     int32               character_code;
428   @};
429 @end example
430
431 @table @code
432 @item int32 rec_type;
433 Record type.  Always set to 7.
434
435 @item int32 subtype;
436 Record subtype.  Always set to 3.
437
438 @item int32 size;
439 Size of each piece of data in the data part, in bytes.  Always set to 4.
440
441 @item int32 count;
442 Number of pieces of data in the data part.  Always set to 8.
443
444 @item int32 version_major;
445 PSPP major version number.  In version @var{x}.@var{y}.@var{z}, this
446 is @var{x}.
447
448 @item int32 version_minor;
449 PSPP minor version number.  In version @var{x}.@var{y}.@var{z}, this
450 is @var{y}.
451
452 @item int32 version_revision;
453 PSPP version revision number.  In version @var{x}.@var{y}.@var{z},
454 this is @var{z}.
455
456 @item int32 machine_code;
457 Machine code.  PSPP always set this field to value to -1, but other
458 values may appear.
459
460 @item int32 floating_point_rep;
461 Floating point representation code.  For IEEE 754 systems this is 1.
462 IBM 370 sets this to 2, and DEC VAX E to 3.
463
464 @item int32 compression_code;
465 Compression code.  Always set to 1.
466
467 @item int32 endianness;
468 Machine endianness.  1 indicates big-endian, 2 indicates little-endian.
469
470 @item int32 character_code;
471 Character code.  1 indicates EBCDIC, 2 indicates 7-bit ASCII, 3
472 indicates 8-bit ASCII, 4 indicates DEC Kanji.
473 @end table
474
475 @node Machine flt64 Info Record, Auxilliary Variable Parameter Record, Machine int32 Info Record, Data File Format
476 @section Machine @code{flt64} Info Record
477
478 There must be no more than one machine @code{flt64} info record per
479 system file.  Machine @code{flt64} info records must follow the variable
480 records and precede the dictionary termination record.
481
482 @example
483 struct sysfile_machine_flt64_info
484   @{
485     /* Header. */
486     int32               rec_type;
487     int32               subtype;
488     int32               size;
489     int32               count;
490
491     /* Data. */
492     flt64               sysmis;
493     flt64               highest;
494     flt64               lowest;
495   @};
496 @end example
497
498 @table @code
499 @item int32 rec_type;
500 Record type.  Always set to 7.
501
502 @item int32 subtype;
503 Record subtype.  Always set to 4.
504
505 @item int32 size;
506 Size of each piece of data in the data part, in bytes.  Always set to 4.
507
508 @item int32 count;
509 Number of pieces of data in the data part.  Always set to 3.
510
511 @item flt64 sysmis;
512 The system missing value.
513
514 @item flt64 highest;
515 The value used for HIGHEST in missing values.
516
517 @item flt64 lowest;
518 The value used for LOWEST in missing values.
519 @end table
520
521 @node Auxilliary Variable Parameter Record, Long Variable Names Record, Machine flt64 Info Record, Data File Format
522 @section Auxilliary Variable Parameter Record
523
524 There must be no more than one auxilliary variable parameter record per
525 system file.  This  record must follow the variable
526 records and precede the dictionary termination record.
527
528 @example
529 struct sysfile_aux_var_parameter
530   @{
531     /* Header. */
532     int32               rec_type;
533     int32               subtype;
534     int32               size;
535     int32               count;
536
537     /* Data. */
538     struct aux_params   aux_params[/* variable length */];
539   @};
540 @end example
541
542 @table @code
543 @item int32 rec_type;
544 Record type.  Always set to 7.
545
546 @item int32 subtype;
547 Record subtype.  Always set to 11.
548
549 @item int32 size;
550 The size  @code{int32}. Always set to 4.
551
552 @item int32 count;
553 The total number of bytes in @code{aux_params} divided by 3.
554
555 @item struct aux_params aux_params[];
556 An array of @code{struct aux_params}.   The order of the elements corresponds 
557 to the order of the variables in the Variable Records.  The @code{struct aux_params} type is defined as follows:
558
559 @example
560 struct aux_params
561   @{
562     int32 measure;
563     int32 width;
564     int32 alignment;
565   @};
566 @end example
567
568 @table @code
569 @item int32 measure
570 The measurement type of the variable:  
571 @table @asis
572 @item 0
573 Nominal Scale
574 @item 1
575 Ordinal Scale
576 @item 2
577 Continuous Scale
578 @end table
579
580 @item int32 width
581 The width of the display column for the variable in characters.
582
583 @item int32 alignment 
584 The alignment of the variable for display purposes:
585
586 @table @asis
587 @item 0
588 Left aligned
589 @item 1
590 Right aligned
591 @item 2
592 Centre aligned
593 @end table
594
595 @end table
596
597
598
599 @end table
600
601
602
603 @node Long Variable Names Record, Miscellaneous Informational Records, Auxilliary Variable Parameter Record,  Data File Format
604 @section Long Variable Names Record
605
606 There must be no more than one long variable names record per
607 system file.  This  record must follow the variable
608 records and precede the dictionary termination record.
609
610 @example
611 struct sysfile_long_variable_names
612   @{
613     /* Header. */
614     int32               rec_type;
615     int32               subtype;
616     int32               size;
617     int32               count;
618
619     /* Data. */
620     char                var_name_pairs[/* variable length */];
621   @};
622 @end example
623
624 @table @code
625 @item int32 rec_type;
626 Record type.  Always set to 7.
627
628 @item int32 subtype;
629 Record subtype.  Always set to 13.
630
631 @item int32 size;
632 The size of each element in the @code{var_name_pairs} member. Always set to 1.
633
634 @item int32 count;
635 The total number of bytes in @code{var_name_pairs}.
636
637 @item char var_name_pairs[/* variable length];
638 A list of @var{key}--@var{value} tuples, where @var{key} is the name
639 of a variable, and @var{value} is its long variable name. 
640 The @var{key} field is at most 8 bytes long and must match the
641 name of a variable which appears in the variable record @xref{Variable Record}.
642 The @var{value} field is at most 64 bytes long.
643 The @var{key} and @var{value} fields are separated by a @samp{=} byte.
644 Each tuple is separated by a byte whose value is 09.  There is no
645 trailing separator following the last tuple.
646 The total length is @code{count} bytes.
647 @end table
648
649
650 @node Miscellaneous Informational Records, Dictionary Termination Record, Long Variable Names Record, Data File Format
651 @section Miscellaneous Informational Records
652
653 Miscellaneous informational records must follow the variable records and
654 precede the dictionary termination record.
655
656 Miscellaneous informational records are ignored by PSPP when reading
657 system files.  They are not written by PSPP when writing system files.
658
659 @example
660 struct sysfile_misc_info
661   @{
662     /* Header. */
663     int32               rec_type;
664     int32               subtype;
665     int32               size;
666     int32               count;
667
668     /* Data. */
669     char                data[/* variable length */];
670   @};
671 @end example
672
673 @table @code
674 @item int32 rec_type;
675 Record type.  Always set to 7.
676
677 @item int32 subtype;
678 Record subtype.  May take any value.  According to Aapi
679 H@"am@"al@"ainen, value 5 indicates a set of grouped variables and 6
680 indicates date info (probably related to USE).
681
682 @item int32 size;
683 Size of each piece of data in the data part.  Should have the value 4 or
684 8, for @code{int32} and @code{flt64}, respectively.
685
686 @item int32 count;
687 Number of pieces of data in the data part.
688
689 @item char data[/* variable length */];
690 Arbitrary data.  There must be @code{size} times @code{count} bytes of
691 data.
692 @end table
693
694 @node Dictionary Termination Record, Data Record, Miscellaneous Informational Records, Data File Format
695 @section Dictionary Termination Record
696
697 The dictionary termination record must follow all other records, except
698 for the actual cases, which it must precede.  There must be exactly one
699 dictionary termination record in every system file.
700
701 @example
702 struct sysfile_dict_term
703   @{
704     int32               rec_type;
705     int32               filler;
706   @};
707 @end example
708
709 @table @code
710 @item int32 rec_type;
711 Record type.  Always set to 999.
712
713 @item int32 filler;
714 Ignored padding.  Should be set to 0.
715 @end table
716
717 @node Data Record,  , Dictionary Termination Record, Data File Format
718 @section Data Record
719
720 Data records must follow all other records in the data file.  There must
721 be at least one data record in every system file.
722
723 The format of data records varies depending on whether the data is
724 compressed.  Regardless, the data is arranged in a series of 8-byte
725 elements.
726
727 When data is not compressed, Every case is composed of @code{case_size}
728 of these 8-byte elements, where @code{case_size} comes from the file
729 header record (@pxref{File Header Record}).  Each element corresponds to
730 the variable declared in the respective variable record (@pxref{Variable
731 Record}).  Numeric values are given in @code{flt64} format; string
732 values are literal characters string, padded on the right when
733 necessary.
734
735 Compressed data is arranged in the following manner: the first 8-byte
736 element in the data section is divided into a series of 1-byte command
737 codes.  These codes have meanings as described below:
738
739 @table @asis
740 @item 0
741 Ignored.  If the program writing the system file accumulates compressed
742 data in blocks of fixed length, 0 bytes can be used to pad out extra
743 bytes remaining at the end of a fixed-size block.
744
745 @item 1 through 251
746 These values indicate that the corresponding numeric variable has the
747 value @code{(@var{code} - @var{bias})} for the case being read, where
748 @var{code} is the value of the compression code and @var{bias} is the
749 variable @code{compression_bias} from the file header.  For example,
750 code 105 with bias 100.0 (the normal value) indicates a numeric variable
751 of value 5.
752
753 @item 252
754 End of file.  This code may or may not appear at the end of the data
755 stream.  PSPP always outputs this code but its use is not required.
756
757 @item 253
758 This value indicates that the numeric or string value is not
759 compressible.  The value is stored in the 8-byte element following the
760 current block of command bytes.  If this value appears twice in a block
761 of command bytes, then it indicates the second element following the
762 command bytes, and so on.
763
764 @item 254
765 Used to indicate a string value that is all spaces.
766
767 @item 255
768 Used to indicate the system-missing value.
769 @end table
770
771 When the end of the first 8-byte element of command bytes is reached,
772 any blocks of non-compressible values are skipped, and the next element
773 of command bytes is read and interpreted, until the end of the file is
774 reached.
775 @setfilename ignored