File: bslio.c

package info (click to toggle)
spd 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,572 kB
  • sloc: ansic: 25,938; fortran: 10,483; sh: 1,032; makefile: 75
file content (1709 lines) | stat: -rw-r--r-- 52,325 bytes parent folder | download | duplicates (2)
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
/*
 *   Project: The SPD Image correction and azimuthal regrouping
 *                      http://forge.epn-campus.eu/projects/show/azimuthal
 *
 *   Copyright (C) 2005-2010 European Synchrotron Radiation Facility
 *                           Grenoble, France
 *
 *   Principal authors: P. Boesecke (boesecke@esrf.fr)
 *                      R. Wilcke (wilcke@esrf.fr)
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   and the GNU Lesser General Public License  along with this program.
 *   If not, see <http://www.gnu.org/licenses/>.
 */

# define BSLIO_VERSION      "bslio : V0.65 Peter Boesecke 2010-12-18"
/*+++***********************************************************************
NAME

   bslio.c

SYNOPSIS

   #include "edfio.h"

   typedef struct Bsl_Data_Specification {
     char          *BinaryFileName;                 // defined in edfio 
     unsigned long BinaryFilePos;                   // defined in edfio 
     unsigned long BinaryFileLen;                   // defined in edfio
     void          *Data;                           // defined in edfio 
     long          *Dim;                            // defined in edfio 
     int           DataType;                        // defined in edfio
     int           ByteOrder;                       // defined in edfio
     long          RasterConfiguration;             // defined in edfio
     int           Compression;                     // defined in edfio
   } BslDataSpec;

   extern int
      open_bsl_file ( const char * FileName , const char * mode ),
      close_bsl_file ( int stream );
      bsl_memory_range( int stream, long * minmem, long *maxmem ); 
      bsl_frame_range( int stream, long memnum, long * minfra, long * maxfra );

   extern BslDataSpec
      *read_bsl_data_spec ( int stream, long memnum, long franum );

   extern void
      print_bsl_data_spec ( FILE * out, const BslDataSpec * data_spec ),
      print_bsl_filetable ( FILE * out, int level, int verbose ),
      read_bsl_file_headers ( int stream, char **first_header,
                                                 char **second_header );

   extern char
      char *bslio_version ( void );

DESCRIPTION

   Library for access to bsl and otoko files. Public routines are defined
   in "bslio.h"

HISTORY
    22-Mar-1998 Peter Boesecke V0.50 (read only version)
    27-Apr/1998 PB V0.51 (BSL read with HighByteFirst if not a PC)
    17-May-1998 PB       open bsl file with "new", "old", "any" and "read"
    31-Dec-2000 PB V0.53 Dim[3] not used any more
    2000-01-12  PB V0.54 for VisualC++ compatibility:
                         u_long replaced by unsigned long
                         strncmp replaced by STRNCASECMP 
                         still undefined in VCC: getcwd
    2001-01-15  PB V0.55 GETCWD
    2001-01-24  PB V0.56 _getcwd
    2001-02-05  PB V0.57 all unreferenced variables removed
    2001-02-05  PB V0.58 <unistd.h> not included for __MSVC__
    2001-04-11  PB V0.59 __MSVC__ -> WIN32
    2001-07-01  PB V0.60 __MSVC__ -> WIN32
    2004-03-16  PB V0.61 STRNCASECMP now in numio, bslio must be linked 
                         with numio
    2004-03-24  PB V0.62 STRNCASECMP -> num_strncasecmp
    2007-04-19  PB V0.63 sizeof is always first operand,
                         corrected to avoid compiler warnings with -Wall
    2007-11-23  PB V0.64 edf_byteorder() function used
    2010-12-18  PB V0.65 read_bsl_header: (memory_key) is always TRUE

***************************************************************************/

/***************************************************************************
* Private part                                                             *
***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifndef WIN32
# include <unistd.h>
#endif

#include <ctype.h>

#ifdef sun      /* sun specific stuff */
#  include <sys/time.h>
#  include <floatingpoint.h>
#else
#  include <time.h>
#endif

# include "bslio.h"
# include "numio.h"

/*****************************************************************************/
# define MaxBslFiles           20                /* maximum opened bsl files */
# define BslIndicNumber        10     /* minimum size of the indicator array */
# define BslBufferSize        512                  /* size of the I/O buffer */
# define MaxBslLineLen         80       /* maximum length per line in output */
/*****************************************************************************/

typedef struct Bsl_Frame {
  char        *FrameKey;
  BslDataSpec *DataSpec;
  struct Bsl_Frame  *Previous, *Next;            /* previous and next memory */
  struct Bsl_Memory *Memory;                            /* the owning memory */
} BslFrame;

typedef struct Bsl_Memory {
  char       *MemoryKey;
  char       *FileName;                           /* name of the memory file */
  char       *Indicator;
  BslFrame   *FrameList;
  struct Bsl_Memory *Previous, *Next;            /* previous and next memory */
  struct Bsl_File   *File;                                /* the owning file */
} BslMemory;

typedef struct Bsl_File {
  char       *Name;                                   /* file name with path */
  char       *Path;                                             /* path only */
  FILE       *Channel;                                        /* i/o channel */
  char       *Buffer;                                /* pointer to IO Buffer */
  char       *FirstHeader;
  char       *SecondHeader;
  BslMemory  *MemoryList;               /* the list of memories in this file */
} BslFile;

PRIVATE int        InitBslTable = 0;
PRIVATE BslFile    BslTable[MaxBslFiles];
PRIVATE char       *BslNew  = "new", *BslOld = "old", *BslAny = "any",
                   *BslRead = "read";

PRIVATE char       end_of_line[3] = { '\r', '\n', '\0' };

enum BslOpenMode { NewBslFile, OldBslFile, AnyBslFile };

enum BslSortMode { Bsl_CaseSensitive, Bsl_UpperCase, Bsl_Number };

enum IndicIndex  { index_dim_1=1, index_dim_2, index_dim_3, index4, index5, 
                   index6, index7, index8, index9, lastindex, IndicEnd };


/***************************************************************************
* Defines                                                                  *
***************************************************************************/

#ifdef sun
# include <sys/param.h>
# define GETCWD(x,y) getwd (x)
#else
# ifdef WIN32
#  include <direct.h>
#  define GETCWD(x,y) _getcwd(x, y)
# else
#  define GETCWD(x,y) getcwd (x, y)
# endif
#endif

/*****************************************************************************/

/*---------------------------------------------------------------------------
NAME

   bslio_version --- returns the current version of bslio

SYNOPSIS

   char *bslio_version ( void );

RETURN VALUE

char * version string
---------------------------------------------------------------------------*/
char *bslio_version ( void )
{ return ( BSLIO_VERSION );
} /* bslio_version */

/*---------------------------------------------------------------------------
NAME

   newstr_bsl --- allocate memory and copy a character string into it

SYNOPSIS

   char * newstr_bsl( const char * string );

DESCRIPTION
  Allocates strlen(string)+1 bytes of memory and copies string into it.
  In case of success the pointer to the allocated memory is returned. The
  null pointer is returned in case of an error.

RETURN VALUE
  Returns the pointer to the allocated string or (char *) NULL in case
  of an error.
---------------------------------------------------------------------------*/
char * newstr_bsl( const char * string )
{ char * newstr_bsling;

  if (!(newstr_bsling = (char *) malloc(strlen(string)+1))) return((char *) NULL);
  (void) strcpy(newstr_bsling,string);

  return( newstr_bsling );

} /* newstr_bsl */

/*---------------------------------------------------------------------------
NAME

       trim_bsl --- remove leading and trailing white spaces

SYNOPSIS

       char * trim_bsl ( char * str );

DESCRIPTION
       Leading and trailing characters in str that return true when checked
       with isspace are removed. The string str is actually modified.
       The first non-isspace character is copied to the start of str and 
       the character after the last non-isspace character is set to '\0'.
       removed. If str is the NULL pointer the NULL pointer is returned.

HISTORY
       23-Mar-1998 Peter Boesecke
---------------------------------------------------------------------------*/
char * trim_bsl ( char * str )
{ char *ps1, *ps2;

  if ( !str ) return ( str );

  ps1 = ps2 = str;
  while ( (*ps1) && (isspace(*ps1)) ) ps1++;
  while (*ps1) *ps2++ = *ps1++;
  *ps2--='\0';
  while ( (ps2>=str) && (isspace(*ps2)) ) *ps2--='\0';

  return ( str );
 
} /* trim_bsl */

/*---------------------------------------------------------------------------
NAME

       getpath_bsl --- extract path from filename 

SYNOPSIS

       char *getpath_bsl ( char *buffer, size_t buflen, const char * filename );

DESCRIPTION
       Extract path from filename and copies it into buffer. If the buffer is
       too short or another error has occurred NULL is returned. If filename 
       has no path the current working directory is determined with 
       getcwd and returned.

HISTORY
       23-Mar-1998 Peter Boesecke
---------------------------------------------------------------------------*/
char * getpath_bsl ( char * buffer, size_t buflen, const char * filename )
{ size_t str_len;  
  char *ps;

  str_len = strlen(filename);
  if (str_len>=buflen) return( (char *) NULL );


  if ( (ps = strrchr( filename, (int) '/' )) ) {
    memcpy( buffer, filename, (ps-filename) );
    buffer[(ps-filename)]='\0'; }
  else GETCWD( buffer, buflen );

  str_len = strlen(buffer);
  if (str_len<buflen) { buffer[str_len]='/';buffer[str_len+1]='\0'; }
    else buffer[0] = '\0';

  return ( buffer );

} /* getpath_bsl */

/*---------------------------------------------------------------------------
NAME

   s2long_bsl --- Conversion of a string to long int

SYNOPSIS

   long int s2long_bsl( const char * string );

DESCRIPTION
   Conversion of a string to long int. 
   In case of an error the output value is 0.

RETURN VALUE
   long int   Converted string
---------------------------------------------------------------------------*/
long int s2long_bsl( const char * string )
{ long int value;

  if ( sscanf(string,"%ld", &value  ) < 1 ) return( 0l );

  return(value);

} /* s2long_bsl */

/*---------------------------------------------------------------------------
NAME

   compare_bsl_keys --- compare two strings 

SYNOPSIS

  enum BslSortMode { Bsl_CaseSensitive, Bsl_UpperCase, Bsl_Number };
  int compare_bsl_keys ( const char * s1, const char * s2, SortMode smode );


DESCRIPTION
  Compares the two strings s1 and s2.
  If s1 < s2 the return value is -1
  if s1 == s2 the return value is 0
  if s1 > s2 the return value is 1
  In SortMode SM_UpperCase the sorting is not case sensitive
  In SortMode SM_Number the strings are converted to numbers and the numbers
  are compared. If the conversion fails the comparison is case insensitive.

RETURN VALUE
  int -1, 0, +1 depending on the comparison
---------------------------------------------------------------------------*/
int compare_bsl_keys ( const char * s1, const char * s2, int smode )
{ int comp_result;
  long l1, l2;
  char * err1, *err2;

  switch ( smode ) {
  case      Bsl_CaseSensitive: 
               comp_result = strncmp ( s1, s2, MaxBslLineLen ); 
               break;
  case      Bsl_UpperCase: 
               comp_result = num_strncasecmp ( s1, s2, MaxBslLineLen );
               break;
  case      Bsl_Number:
               l1 = strtol(s1, &err1 ,10);
               l2 = strtol(s2, &err2 ,10);
               if (err1) if (strlen(err1)==0) err1 = (char *) NULL;
               if (err2) if (strlen(err2)==0) err2 = (char *) NULL;
               if (!((err1)||(err2))) {
                 if (l1<l2) comp_result = -1;
                 else if (l1==l2) comp_result = 0;
                 else comp_result = 1;
               } else comp_result = num_strncasecmp ( s1, s2, MaxBslLineLen );
               break;
  default:  printf("compare: Invalid smode\n"); exit(-1);
            break;
  } /* switch */

  return ( comp_result );

} /* compare_bsl_keys */

/*---------------------------------------------------------------------------
NAME

   rmeoln --- removes end of line characters from the end of the line 

SYNOPSIS

   char * rmeoln ( char * line );

DESCRIPTION
   Removes end of line characters from line and replaces them with '\0'.
   Nothing is done when line is the NULL pointer.

ARGUMENTS
   char * line (input and output) 

RETURN VALUE
   char * line (pointer to output)
---------------------------------------------------------------------------*/
char * rmeoln ( char * line )
{ char * pc = line;

  
  if (pc)
    while (*pc) {
      if (strchr( end_of_line, (int) *pc )) *pc='\0';
      pc++;
      }
  return( line );

} /* rmeoln */

/*---------------------------------------------------------------------------
NAME

  init_bsl_file --- initialization of the bsl file structure

---------------------------------------------------------------------------*/
void init_bsl_file ( BslFile * file )
{ file->Name          = (char *) NULL;
  file->Path          = (char *) NULL;
  file->Channel       = (FILE *) NULL;
  file->Buffer        = (char *) NULL;
  file->FirstHeader   = (char *) NULL;
  file->SecondHeader  = (char *) NULL;
  file->MemoryList    = (BslMemory *) NULL;
} /* init_bsl_file */

/*---------------------------------------------------------------------------
NAME

  init_bsl_file_table --- initialization of the bsl file table cells

---------------------------------------------------------------------------*/
void init_bsl_file_table ( BslFile ftb[] )
{ register int i;
  for (i=0;i<MaxBslFiles;i++) init_bsl_file ( &ftb[i] );
  InitBslTable = 1;
} /* init_bsl_file_table */

/*---------------------------------------------------------------------------
NAME

  search_free_bsl_stream --- search an unused file table cell 

DESCRIPTION
Returns the index of the first unused file table cell

RETURN VALUE
success: int stream >= 0 
error:              <0
---------------------------------------------------------------------------*/
int search_free_bsl_stream ( BslFile ftb[] )
{ register int i;
  for (i=0;i<MaxBslFiles;i++) {
    if (ftb[i].Channel==(FILE *) NULL) return(i);
    }
  return(-1);
} /* search_free_bsl_stream */

/*---------------------------------------------------------------------------
NAME

  init_bsl_data --- initialization of the bsl data structure

---------------------------------------------------------------------------*/
void init_bsl_data_spec ( BslDataSpec * data_spec )
{ 
  data_spec->BinaryFileName      = (char *) NULL;
  data_spec->BinaryFilePos       = 0U;
  data_spec->BinaryFileLen       = 0U;
  data_spec->Data                = (void *) NULL;
  data_spec->Dim                 = (long *) NULL;
  data_spec->DataType            = FloatIEEE32;
  data_spec->ByteOrder           = edf_byteorder ();
  data_spec->RasterConfiguration = 1;
  data_spec->Compression         = UnCompressed;

} /* init_bsl_data_spec */

/*---------------------------------------------------------------------------
NAME

  new_bsl_data --- creates a new bsl data structure and initializes it

RETURN VALUE
success:  BslDataSpec * pointer to new bsl data specification
error:    NULL pointer
---------------------------------------------------------------------------*/
BslDataSpec * new_bsl_data_spec ( void )
{ BslDataSpec * newdata_spec;

  if ( (newdata_spec = (BslDataSpec *) malloc( sizeof(BslDataSpec) )) )
     init_bsl_data_spec ( newdata_spec );
  return(newdata_spec);

} /* new_bsl_data_spec */

/*---------------------------------------------------------------------------
NAME

   free_bsl_data_spec --- deallocate data_spec and all its contents

SYNOPSIS

   BslDataSpec * free_bsl_data ( BslDataSpec * data_spec )

DESCRIPTION
The allocated memory of the contents of data_spec is removed and
(BslDataSpec *) NULL is returned. If data_spec is NULL, nothing is done.

RETURN VALUE
(BslDataSpec *) NULL
---------------------------------------------------------------------------*/
BslDataSpec * free_bsl_data_spec ( BslDataSpec * data_spec )
{ 

  if (!data_spec) return ( data_spec );

  if (data_spec->BinaryFileName) free ( data_spec->BinaryFileName );
  if (data_spec->Data)           free ( data_spec->Data );
  if (data_spec->Dim)            free ( data_spec->Dim );
 
  free(data_spec);
 
  return( (BslDataSpec *) NULL );
 
} /* free_bsl_data_spec */

/*---------------------------------------------------------------------------
NAME

  init_bsl_frame --- initialization of the bsl frame structure

---------------------------------------------------------------------------*/
void init_bsl_frame ( BslFrame * frame )
{ frame->FrameKey       = (char *) NULL;
  frame->DataSpec       = (BslDataSpec *) NULL;
  frame->Previous       = (BslFrame *) NULL;
  frame->Next           = (BslFrame *) NULL;
  frame->Memory         = (BslMemory *) NULL;
} /* init_bsl_frame */

/*---------------------------------------------------------------------------
NAME

  new_bsl_frame --- creates a new bsl frame and initializes it 

RETURN VALUE
success:  BslFrame * pointer to new bsl frame
error:    NULL pointer
---------------------------------------------------------------------------*/
BslFrame * new_bsl_frame ( const char * FrameKey )
{ BslFrame * newframe;
 
  if ( (newframe = (BslFrame *) malloc( sizeof(BslFrame) )) ) 
     init_bsl_frame ( newframe );
  newframe->FrameKey = newstr_bsl(FrameKey);
  newframe->DataSpec = new_bsl_data_spec();
  return(newframe);

} /* new_bsl_frame */


/*---------------------------------------------------------------------------
NAME

  search_bsl_frame --- searches the bsl frame in the frame list of memory

SYNOPSIS

  BslFrame * search_bsl_frame ( BslMemory * memory, char * FrameKey )

DESCRIPTION

RETURN VALUE
success BslFrame * frame
error   NULL
---------------------------------------------------------------------------*/
BslFrame * search_bsl_frame ( BslMemory * memory, const char * FrameKey )
{ BslFrame * current = (BslFrame *) NULL;

  if (memory == (BslMemory *) NULL) return( current );

  /* search frame */
  current = memory->FrameList;
  if (current!=(BslFrame *) NULL )
  while( ( current!=(BslFrame *) NULL ) &&
         ( compare_bsl_keys(current->FrameKey,FrameKey,Bsl_Number)!=0 ) ) {
    current = current->Next;
    }
  return( current );

} /* search_bsl_frame */
 
/*---------------------------------------------------------------------------
NAME

  insert_bsl_frame --- insert a bsl frame into the frame list of a bsl memory

SYNOPSIS

  BslFrame  * insert_bsl_frame ( BslMemory * memory, const char * FrameKey )

DESCRIPTION

RETURN VALUE
success:            pointer to inserted frame 
error:              NULL pointer 
---------------------------------------------------------------------------*/
BslFrame  * insert_bsl_frame ( BslMemory * memory, const char * FrameKey )
{ BslFrame *frame, *previous, *next, *newframe;
  int notfound = -1;

  frame = (BslFrame *) NULL;
  previous = (BslFrame *) NULL;

  if ( memory == (BslMemory *) NULL ) return( frame );

  /* search insertion point (insertion before *pnext) */
  next = memory->FrameList;
  while( ( next!=(BslFrame *) NULL ) && (notfound<0) ) {
    notfound = compare_bsl_keys(next->FrameKey,FrameKey,Bsl_Number);
    if (notfound<0) { previous = next; next = next->Next; }
    }

  /* create new frame, if no frame found */
  if ( next==(BslFrame *) NULL  ) {
    /* create new frame */
    newframe = new_bsl_frame ( FrameKey );
    if ( newframe == (BslFrame *) NULL ) return( frame );

    /* insert new frame before *pnext */
    if (next) next->Previous = newframe;
    newframe->Next=next;
    newframe->Previous=previous;
    if (previous) previous->Next=newframe;
      else memory->FrameList=newframe;

    /* link to owning memory */
    newframe->Memory      = memory;

    next = newframe;
    }

  frame = next;

  return( frame );

} /* insert_bsl_frame */

/*---------------------------------------------------------------------------
NAME

   remove_bsl_frame --- removes frame from the frame list

SYNOPSIS

   void remove_bsl_frame ( BslFrame * frame )


DESCRIPTION
The allocated memory of the contents of frame is removed. The frame is 
removed from the frame list of the owning memory. If frame was the only 
frame in frame->Memory->FrameList frame->Memory->FrameList is set to 
(BslFrame *) NULL. If frame is NULL, nothing is done.
---------------------------------------------------------------------------*/
void remove_bsl_frame ( BslFrame * frame )
{
  BslFrame **proot, *previous, *next;

  if (frame==(BslFrame*) NULL) return;
 
  proot    = &(frame->Memory->FrameList);
  previous = frame->Previous;
  next     = frame->Next; 

  if (next!=(BslFrame*) NULL)     next->Previous = previous;
  if (previous!=(BslFrame*) NULL) previous->Next = next;
  if (*proot==frame) { *proot = ((BslFrame*) NULL); }


  if (frame->FrameKey)       free(frame->FrameKey); 
  free_bsl_data_spec( frame->DataSpec );

  free(frame); 

  return;

} /* remove_bsl_frame */

/*---------------------------------------------------------------------------
NAME

   remove_bsl_frame_list --- empty the frame list of memory 

SYNOPSIS

   void remove_bsl_frame_list ( BslMemory * memory );

---------------------------------------------------------------------------*/
void remove_bsl_frame_list ( BslMemory * memory )
{
  BslFrame *frame, *next;

  if (memory==(BslMemory*) NULL) return;

  next = memory->FrameList;
  while ( next!=(BslFrame*) NULL ) {
    frame = next;
    next  = next->Next;
    remove_bsl_frame ( frame );
    }
  return;

} /* free_bsl_frame_list */

/*---------------------------------------------------------------------------
NAME

   print_bsl_data_spec --- prints data_spec

SYNOPSIS

   void print_bsl_data_spec ( FILE * out, const BslDataSpec * data_spec );

---------------------------------------------------------------------------*/
void print_bsl_data_spec ( FILE * out, const BslDataSpec * data_spec )
{ const char * SeparationLine =
    "-       -       -       -       -       -       -       -";
      long i_dim;

      if (!data_spec) return;

      fprintf(out,"  %s\n",SeparationLine);

      fprintf(out,"    BinaryFileName      = ");
      if ( data_spec->BinaryFileName )
        fprintf(out,"\"%s\"\n",data_spec->BinaryFileName);
        else fprintf(out,"(no binary file name)\n");
      fprintf(out,"    BinaryFilePos       = %lu\n",data_spec->BinaryFilePos);
      fprintf(out,"    BinaryFileLen       = %lu\n",data_spec->BinaryFileLen);
      fprintf(out,"    Data                = %p\n",data_spec->Data);
      fprintf(out,"    Dim                 = %p\n",data_spec->Dim);
      if (data_spec->Dim) for (i_dim=0;i_dim<=data_spec->Dim[0];i_dim++)
        fprintf(out,"          Dim[%1lu]        = %ld\n", 
                    i_dim,(data_spec->Dim)[i_dim]);
      fprintf(out,"    DataType            = %u\n",data_spec->DataType);
      fprintf(out,"    ByteOrder           = %u\n",data_spec->ByteOrder);
      fprintf(out,"    RasterConfiguration = %lu\n",
                    data_spec->RasterConfiguration);
      fprintf(out,"    Compression         = %u\n",data_spec->Compression);

      fprintf(out,"  %s\n",SeparationLine);

} /* print_bsl_data_spec */

/*---------------------------------------------------------------------------
NAME

   print_bsl_frame_list --- prints frame list contents of memory

SYNOPSIS

   void print_bsl_frame_list ( FILE * out, const BslMemory * memory,
                               int level, int verbose )

---------------------------------------------------------------------------*/
void print_bsl_frame_list ( FILE * out, const BslMemory * memory,
                            int level, int verbose )

{ const char * SeparationLine =
    "-   -   -   -   -   -   -   -   -   -   -   -   -   -   -";

  const BslFrame * frame;

  if (level<1) return;

  frame = memory->FrameList;

  while(frame!=(BslFrame*) NULL) {
    if (verbose) {
      fprintf(out,"  %s\n",SeparationLine);
      fprintf(out,"  FrameKey           = %s\n",frame->FrameKey);
      fprintf(out,"  DataSpec           = %p\n",frame->DataSpec);
      print_bsl_data_spec ( out, frame->DataSpec );

      fprintf(out,"  Previous FrameKey  = ");
      if ((frame->Previous)!=(BslFrame*) NULL)
        fprintf(out,"%s\n", frame->Previous->FrameKey);
        else fprintf(out,"(no previous frame)\n");
      fprintf(out,"  Next FrameKey      = ");
      if ((frame->Next)!=(BslFrame*) NULL)
        fprintf(out,"%s\n", frame->Next->FrameKey);
        else fprintf(out,"(no next frame)\n");
      fprintf(out,"  Owner Memory       = %s\n",frame->Memory->MemoryKey);
      fprintf(out,"  %s\n",SeparationLine); 
      } else {
      fprintf(out,"  FrameKey           = '%s'\n",frame->FrameKey);
      }
    frame=frame->Next;
    }

} /* print_bsl_frame_list */

/*---------------------------------------------------------------------------
NAME

  init_bsl_memory --- initialization of the bsl memory structure

---------------------------------------------------------------------------*/
void init_bsl_memory ( BslMemory * memory )
{ memory->MemoryKey  = (char *) NULL;
  memory->FileName   = (char *) NULL;
  memory->Indicator  = (char *) NULL;
  memory->FrameList  = (BslFrame *) NULL;
  memory->Previous   = (BslMemory *) NULL;
  memory->Next       = (BslMemory *) NULL;
  memory->File       = (BslFile *) NULL;

} /* init_bsl_memory */

/*---------------------------------------------------------------------------
NAME

  new_bsl_memory --- creates a new bsl memory with name MemoryKey 

RETURN VALUE
success:  BslMemory * pointer to new bsl memory 
error:    NULL pointer
---------------------------------------------------------------------------*/
BslMemory * new_bsl_memory ( const char * MemoryKey )
{ BslMemory * newmemory;

  if ( (newmemory = (BslMemory*) malloc( sizeof(BslMemory) )) )
     init_bsl_memory ( newmemory );
  newmemory->MemoryKey = newstr_bsl(MemoryKey);
  return(newmemory);

} /* new_bsl_memory */

/*---------------------------------------------------------------------------
NAME

  search_bsl_memory --- searches the bsl memory in the memory list of file

SYNOPSIS

  BslMemory * search_bsl_memory ( BslFile * file, char * MemoryKey )

DESCRIPTION

RETURN VALUE
success BslMemory * found memory 
error   NULL
---------------------------------------------------------------------------*/
BslMemory * search_bsl_memory ( BslFile * file, const char * MemoryKey )
{ BslMemory * current = (BslMemory *) NULL;

  if (file == (BslFile *) NULL) return( current );

  /* search memory */
  current = file->MemoryList;
  if (current!=(BslMemory *) NULL )
  while( ( current!=(BslMemory *) NULL ) &&
         ( compare_bsl_keys(current->MemoryKey,MemoryKey,Bsl_UpperCase)!=0 ) ) {
    current = current->Next;
    }
  return( current );

} /* search_bsl_memory */

/*---------------------------------------------------------------------------
NAME

  insert_bsl_memory --- insert a bsl memory into the memory list of a bsl file 

SYNOPSIS

  BslMemory  * insert_bsl_memory ( BslFile * file, const char * MemoryKey )

DESCRIPTION

RETURN VALUE
success:            pointer to inserted memory 
error:              NULL pointer
---------------------------------------------------------------------------*/
BslMemory  * insert_bsl_memory ( BslFile * file, const char * MemoryKey )
{ BslMemory *memory, *previous, *next, *newmemory;
  int notfound = -1;

  memory   = (BslMemory *) NULL;
  previous = (BslMemory *) NULL;

  if ( file == (BslFile *) NULL ) return( memory );

  /* search insertion point (insertion before *pnext) */
  next = file->MemoryList;
  while( ( next!=(BslMemory *) NULL ) && (notfound<0) ) {
    notfound = compare_bsl_keys(next->MemoryKey,MemoryKey,Bsl_UpperCase);
    if (notfound<0) { previous = next; next = next->Next; }
    }

  /* create new memory, if no memory found */
  if ( next==(BslMemory *) NULL  ) {
    /* create new memory */
    newmemory = new_bsl_memory ( MemoryKey );
    if ( newmemory == (BslMemory *) NULL ) return( memory );

    /* insert new memory before *pnext */
    if (next) next->Previous = newmemory;
    newmemory->Next=next;
    newmemory->Previous=previous; 
    if (previous) previous->Next=newmemory;
      else file->MemoryList=newmemory;

    /* link to owning file */
    newmemory->File      = file;

    next = newmemory;
    }

  memory = next;

  return( memory );

} /* insert_bsl_memory */

/*---------------------------------------------------------------------------
NAME

   remove_bsl_memory --- removes memory from the memory list

SYNOPSIS

   void remove_bsl_memory ( BslMemory * memory );

DESCRIPTION
The allocated memory of the contents of memory is removed. memory
is removed from the memory list of the owning file. If memory was the 
only memory in memory->File->MemoryList memory->File->MemoryList is 
set to (BslMemory *) NULL. If memory is NULL, nothing is done. 
---------------------------------------------------------------------------*/
void remove_bsl_memory ( BslMemory * memory )
{
  BslMemory **proot, *previous, *next;

  if (memory==(BslMemory*) NULL) return; 

  proot      = &(memory->File->MemoryList);
  previous   = memory->Previous;
  next       = memory->Next; 

  remove_bsl_frame_list ( memory );

  if (next!=(BslMemory*) NULL)       next->Previous = previous;
  if (previous!=(BslMemory*) NULL)   previous->Next = next;
  if (*proot==memory) { *proot = ((BslMemory*) NULL); }

  if (memory->MemoryKey)  free(memory->MemoryKey);
  if (memory->FileName)   free(memory->FileName);
  if (memory->Indicator)  free(memory->Indicator);
  free(memory);

  return;

} /* remove_bsl_memory */

/*---------------------------------------------------------------------------
NAME

   remove_bsl_memory_list --- empty the memory list of file

SYNOPSIS

   void remove_bsl_memory_list ( BslFile * file )

---------------------------------------------------------------------------*/
void remove_bsl_memory_list ( BslFile * file )
{
  BslMemory *memory, *next;

  if (file==(BslFile*) NULL) return;

  next = file->MemoryList;
  while ( next!=(BslMemory*) NULL ) {
    memory = next;
    next=next->Next;
    remove_bsl_memory ( memory );
    }
  return;

} /* remove_bsl_memory_list */

/*---------------------------------------------------------------------------
NAME

   print_bsl_memory_list --- prints bsl memory contents

SYNOPSIS

   void print_bsl_memory_list ( FILE * out, const BslFile * file,
                               int level, int verbose );

---------------------------------------------------------------------------*/
void print_bsl_memory_list ( FILE * out, const BslFile * file, 
                             int level, int verbose )
{ const char * SeparationLine =
    "- - - - - - - - - - - - - - - - - - - - - - - - - - - - -";

  BslMemory * memory;

  if (level<1) return;

  memory = file->MemoryList;

  while(memory!=(BslMemory*) NULL) {
    if (verbose) {
      fprintf(out," %s\n",SeparationLine);
      fprintf(out," MemoryKey           = %s\n",memory->MemoryKey);
      fprintf(out," FileName            = \"%s\"\n",memory->FileName);

      fprintf(out," Indicator string    = \n\"%s\"\n",memory->Indicator); 

      fprintf(out," Previous MemoryKey   = ");
      if ((memory->Previous)!=(BslMemory*) NULL)
        fprintf(out,"%s\n", memory->Previous->MemoryKey);
        else fprintf(out,"(no previous memory)\n");
      fprintf(out," Next MemoryKey       = ");
      if ((memory->Next)!=(BslMemory*) NULL)
        fprintf(out,"%s\n", memory->Next->MemoryKey);
        else fprintf(out,"(no next memory)\n");
//      fprintf(out," Owner file          = %#x\n",memory->File->Channel);
      fprintf(out," Owner file          = %p\n",memory->File->Channel);
      fprintf(out," FrameList           = %p\n",memory->FrameList);
      print_bsl_frame_list( out, memory, level-1, verbose );
      fprintf(out," %s\n",SeparationLine); 
      } else { /* short */
      fprintf(out," MemoryKey           = '%s'\n",memory->MemoryKey);
      print_bsl_frame_list( out, memory, level-1, verbose );

      }
    memory=memory->Next;
    }

} /* print_bsl_memory_list */

/*---------------------------------------------------------------------------
NAME

   print_bsl_filetable --- prints bsl file contents

SYNOPSIS

   void print_bsl_filetable ( FILE * out, int level, int verbose )

---------------------------------------------------------------------------*/
void print_bsl_filetable ( FILE * out, int level, int verbose )
{ 
  const char * SeparationLine =
    "=========================================================";

  int stream;

  if (level<1) return;
  if (!InitBslTable) { printf("Bsl file table is not initialized\n");return;}

  for (stream=0;stream<MaxBslFiles;stream++)
    if (BslTable[stream].Channel) {
      if (verbose) {
        fprintf(out,"%s\n",SeparationLine);
        fprintf(out,"Stream               = %d\n",stream);
        fprintf(out,"Name                 = \"%s\"\n",BslTable[stream].Name);
	fprintf(out,"Path                 = \"%s\"\n",BslTable[stream].Path);
//        fprintf(out,"Channel              = %#x\n",BslTable[stream].Channel);
        fprintf(out,"Channel              = %p\n",BslTable[stream].Channel);
        fprintf(out,"Buffer               = %p\n",BslTable[stream].Buffer);
        fprintf(out,"FirstHeader          = \n\"%s\"\n",
                                             BslTable[stream].FirstHeader);
        fprintf(out,"SecondHeader         = \n\"%s\"\n",
                                             BslTable[stream].SecondHeader);
        fprintf(out,"MemoryList           = %p\n",BslTable[stream].MemoryList);
        print_bsl_memory_list(out,&BslTable[stream],level-1,verbose);
        fprintf(out,"%s\n",SeparationLine); 
        } else {
        fprintf(out,"Stream               = '%d'\n",stream);
        print_bsl_memory_list(out,&BslTable[stream],level-1,verbose);
        }
      }

} /* print_bsl_filetable */

/*===file_io===============================================================*/

/*---------------------------------------------------------------------------
NAME

   strisspace --- checks, whether the string contains only white spaces. 

SYNOPSIS

   int strisspace ( const char * str );

DESCRIPTION
   Scans str with isspace and returns 1 if all characters are white spaces.

ARGUMENTS
   const char * str     string to be scanned

RETURN VALUE
   int  1  str contains only white spaces (isspace) 
   int  0  str contains also other characters
---------------------------------------------------------------------------*/
int strisspace ( const char * str ) 
{ 
  while (*str) if (!isspace((int) *str++)) return(0);

  return(1);
  
} /* strisspace */

/*---------------------------------------------------------------------------
NAME

   indicator2table --- create indicator table from indicator string

SYNOPSIS

   long * indicator2table ( const char * indicator );

DESCRIPTION
   Creates a table of values from indicator string. The length of the 
   table is written in element 0. 

ARGUMENTS
 const char * indicator    string with indicator numbers
 
RETURN VALUE
 Success:         long * indic_table  pointer to the indicator table 
 error:           long * NULL pointer
---------------------------------------------------------------------------*/
long * indicator2table ( const char * indicator )
{ char *rest, *previous;
  long int index, *indic_table = (long int *) NULL;
  int i, table_len;

  /* get length of table */ 
  table_len = 0;
  rest = (char *) indicator; /*sorry!*/
  do { table_len++;
       previous = rest;
       index = strtol(previous, &rest, 10);
   } while (( *rest ) && ( previous<rest ));

  if (strisspace(previous)) table_len--;  

  /* allocate memory for indic_table */
  indic_table = (long int *) malloc ( sizeof( long int) * (table_len+1) );
  if (!indic_table) return(indic_table); 
  indic_table[0] = table_len;

  /* convert */
  rest = (char *) indicator;
  for (i=1;i<=table_len;i++) indic_table[i] = strtol(rest, &rest, 10);
     
  return( indic_table );

} /* indicator2table */

/*---------------------------------------------------------------------------
NAME

   get_bsl_dim --- extract dimension from indicators

SYNOPSIS

   long * get_bsl_dim ( const BslMemory * memory );

DESCRIPTION
   Reads the dimensions from the indicator string memory->Indicator.

ARGUMENTS
 const BslMemory * memory  input memory

RETURN VALUE
  long *
  error   : (long *) NULL
  success : Dim
              Dim[0]  : 2 = number of dimensions
              Dim[1]  : number of elements in first dimension
              Dim[2]  : number of elements in nn-th dimension
---------------------------------------------------------------------------*/
long * get_bsl_dim ( const BslMemory * memory )
{ long * dim = (long *) NULL;
  long * indic;

  if (!memory) return( dim );
  indic = indicator2table ( memory->Indicator );
  if (!indic) return( dim );
  if (indic[0]<BslIndicNumber) { free(indic); return( dim ); }

  dim = (long *) malloc ( sizeof (long int) * 4 );
  if (!dim) { free(indic); return ( dim ); }

  dim[0] = 2;
  dim[1] = indic[1];
  dim[2] = indic[2];

  free ( indic );

  return ( dim );

} /* get_bsl_dim */

/*---------------------------------------------------------------------------
NAME

   get_bsl_number --- extract number of frames from indicators

SYNOPSIS

   long get_bsl_number ( const BslMemory * memory );

DESCRIPTION
   Reads the number of frames from the indicator string memory->Indicator. 

ARGUMENTS
 const BslMemory * memory  input memory

RETURN VALUE
  error   : long int   <0 
  success : long int   number of frames
---------------------------------------------------------------------------*/
long get_bsl_number ( const BslMemory * memory )
{ 
  long * indic, number = -1l;

  if (!memory) return( number );
  indic = indicator2table ( memory->Indicator );
  if (!indic) return( number );
  if (indic[0]<BslIndicNumber) { free(indic); return( number ); }

  number = indic[3];
  free ( indic );

  return ( number ); 

} /* get_bsl_number*/

/*---------------------------------------------------------------------------
NAME

   indicator2frame_list --- create a frame list from the indicator string

SYNOPSIS

   int indicator2frame_list ( BslMemory * memory );

DESCRIPTION
 Creates to the indicator string of memory a frame list. Eventually 
 previously allocated memory for memory->FrameList is released.

ARGUMENTS
 BslMemory * memory  memory 

RETURN VALUE
 Success:         int 0  
 error:           int <0
---------------------------------------------------------------------------*/
int indicator2frame_list ( BslMemory * memory )
{ long        number;  /* number of frames */
  BslFrame    *frame;  /* inserted frame */
  char        frame_key[MaxBslLineLen+1]; 
  BslDataSpec *data_spec;   /* data specification */
  char        fullname[BslBufferSize];

  BslFile     *file;
  
  int        i;

  if (!memory) return(-1);
 
  file = memory->File;
 
  number = get_bsl_number ( memory );
  if (number<0) return (-1);

  for (i=1;i<=number;i++) {
    sprintf(frame_key,"%d",i);    
    frame = insert_bsl_frame ( memory, frame_key ); if (!frame) return(-1);
    data_spec = frame->DataSpec;
    data_spec->Dim = get_bsl_dim ( memory ); if (!data_spec->Dim) return(-1);
    sprintf( fullname, "%s%s", file->Path, memory->FileName );
      data_spec->BinaryFileName = newstr_bsl ( fullname );
      if (!data_spec->BinaryFileName) return(-1);
    data_spec->BinaryFileLen  = 
      (unsigned long) (sizeof(float)*data_spec->Dim[1]*data_spec->Dim[2]);
    data_spec->BinaryFilePos  = 
      (unsigned long) (sizeof(float)*data_spec->Dim[1]*data_spec->Dim[2]*(i-1));
    }

  return(0);

} /* indicator2frame_list */

/*---------------------------------------------------------------------------
NAME

   read_bsl_line --- read a single line of an bsl header

SYNOPSIS

   char * read_bsl_line ( BslFile * file, int * io_error );

DESCRIPTION
  Reads a single line from file, allocates memory for it and returns it.

ARGUMENTS
 BslFile * file   opened input file
 int * io_error   returned error value from ferror

RETURN VALUE
 Success: char *  allocated string
 error:           NULL pointer
---------------------------------------------------------------------------*/
char * read_bsl_line ( BslFile * file, int * io_error )
{ const int buflen = BslBufferSize;
  static char buffer[BslBufferSize];
  char * str = (char *) NULL;

  FILE * channel = file->Channel;

  fgets (buffer, buflen, channel);

  if (!feof( channel ))
    if (!(*io_error=ferror( channel ))) str = newstr_bsl(buffer);

  return( rmeoln(str) );

} /* read_bsl_line */

/*---------------------------------------------------------------------------
NAME

   read_bsl_header --- read a bsl header file

SYNOPSIS

   int read_bsl_header ( BslFile * file );

DESCRIPTION
  Reads the bsl header from file. The bsl header has the following
  structure:

  *       line 1. Header with up to 80 alphanumeric characters.
  *       line 2. Header with up to 80 alphanumeric characters.
  *       line 3. Integer indicators for the first binary file

  indicator(1)    = number of pixels        (dim_1)
  indicator(2)    = number of rasters       (dim_2)
  indicator(3)    = number of time frames   (dim_3)
  indicator(4)    =
  indicator(5)    =
  indicator(6)    =
  indicator(7)    =
  indicator(8)    =
  indicator(9)    =
  indicator(10)   = 1, except for the last binary file,  when  it
                    is zero

  *       line 4. File name of the binary file

The lines 3 and 4 are repeated for subsequent binary files.
The panel below shows an example of a header file with two binary
files.


RETURN VALUE
 Success: int     0
 error:   int    <0
---------------------------------------------------------------------------*/
int read_bsl_header ( BslFile * file )
{ BslMemory * memory;
 
  char *indicator, *file_name; 
  char memory_key[MaxBslLineLen+1];
  long int memory_number=1;
  int io_error;

  if (!(file->FirstHeader =trim_bsl(read_bsl_line(file,&io_error)))) return(-1);
  if (!(file->SecondHeader=trim_bsl(read_bsl_line(file,&io_error)))) return(-1);

  do {
    if ( (indicator  = read_bsl_line( file, &io_error )) )
      if ( (file_name = read_bsl_line( file, &io_error )) ) {
        sprintf(memory_key,"%ld",memory_number++);
        memory = insert_bsl_memory ( file, memory_key );
        if (!(memory)) return(-1);
        memory->FileName  = trim_bsl(file_name);
        memory->Indicator = trim_bsl(indicator);

        if (indicator2frame_list ( memory )) return(-1);
      }
//++++++++{+  } while (( memory_key ) && ( indicator )); // always true
   } while ( indicator );
  if (io_error) return(-1);

  return(0);

} /* read_bsl_header */

/*---------------------------------------------------------------------------
NAME

   free_bsl_file --- deallocates and reinitializes a bsl file.

SYNOPSIS

   int free_bsl_file ( BslFile * file );

DESCRIPTION
Deallocates all memory of a bsl file. If it is already done nothing happens.

RETURN VALUE
success:   int 0
error:     int -1
---------------------------------------------------------------------------*/
int free_bsl_file ( BslFile * file )
{
  /* remove memory list */
  remove_bsl_memory_list( file );

  /* close file */
  if (fclose(file->Channel)) return(-1);
  file->Channel = (FILE *) NULL;

  /* release IO-buffer */
  free(file->Buffer);
  file->Buffer = (char *) NULL;

  /* release file name */
  free(file->Name);
  file->Name = (char *) NULL;

  /* release path */
  free(file->Path);
  file->Path = (char *) NULL;

  /* free first and second header */
  if (file->FirstHeader) free ( file->FirstHeader );
  if (file->SecondHeader) free ( file->SecondHeader );

  init_bsl_file ( file );

  return(0);

} /* free_bsl_file */

/*---------------------------------------------------------------------------
NAME

   open_bsl_file --- opens a bsl file 

SYNOPSIS

   int open_bsl_file ( const char * FileName , const char * mode );

DESCRIPTION
Opens a bsl header file and analyzes the structure. If the contents of 
the file does not contain in line 2+n*2+1 at least BslIndicNumber 
long integer values separated by white space, the reading of the file 
is stopped, all memory is released and a negative value is returned.

ARGUMENTS
const char * FileName        name of the file that should be opened
const char * mode            opening mode: "old", "new", "any"

RETURN VALUE
success:  int      stream >= 0  file stream
error:    int      stream < 0   
---------------------------------------------------------------------------*/
int open_bsl_file ( const char * FileName , const char * mode )
{ int       stream;
  BslFile   *file;
  int       buflen = BslBufferSize; 
  char      buffer[BslBufferSize];

  if (!InitBslTable) init_bsl_file_table ( BslTable );
  stream =  search_free_bsl_stream ( BslTable );
  if (stream<0) return( stream ); 

  file = &BslTable[stream];

  init_bsl_file ( file );

  if (strcmp(mode,BslOld)==0) { /* open old file */
    file->Channel = fopen( FileName,"rb+"); } else
  if (strcmp(mode,BslNew)==0) { /* open new file */
    file->Channel = fopen( FileName,"wb+"); } else
  if (strcmp(mode,BslAny)==0) { /* open old file or create new file */
    if ( (file->Channel = fopen( FileName,"rb+")) ) {
      } else {
      file->Channel = fopen( FileName,"wb+"); } } else
  if (strcmp(mode,BslRead)==0) { /* open old file read-only */
    file->Channel = fopen( FileName,"rb"); } 

  if (file->Channel == (FILE *) NULL) { free_bsl_file( file ); return(-1); }

  /* allocate IO-buffer */
  file->Buffer = (char *) malloc(sizeof(char)*BslBufferSize);
  if ( file->Buffer == (char *) NULL ) { free_bsl_file( file ); return(-1); }
  if ( setvbuf(file->Channel, file->Buffer, _IOFBF, BslBufferSize) ) {
    free_bsl_file( file ); return(-1); }

  /* file structure parameters */
  file->Name = newstr_bsl( FileName );
  file->Path = newstr_bsl( getpath_bsl( buffer, buflen, FileName ));

  /* read header(s) and locate data */
  if (read_bsl_header ( file )) { free_bsl_file( file ); return(-1); } 

  return( stream );

} /* open_bsl_file */

/*---------------------------------------------------------------------------
NAME

   close_bsl_file --- closes a bsl file

SYNOPSIS

   int close_bsl_file ( int stream );

DESCRIPTION
Closes a bsl file. If it is already closed nothing happens.

RETURN VALUE
success:   int 0
error:     int -1
---------------------------------------------------------------------------*/
int close_bsl_file ( int stream )
{
  return(free_bsl_file(&BslTable[stream]));

} /* close_bsl_file */

/*---------------------------------------------------------------------------
NAME

   bsl_memory_range --- returns the number of memories in the bsl file 

SYNOPSIS

   int bsl_memory_range( int stream, long * minmem, long *maxmem );    

DESCRIPTION
Returns the minimum memory number (*minmem) and the maximum memory number
(*maxmem) of the bsl.

RETURN VALUE
success:   int 0
error:     int -1
---------------------------------------------------------------------------*/
int bsl_memory_range( int stream, long * minmem, long *maxmem )
{ BslFile * file;
  BslMemory * memory;
  long memnum;

  file = &BslTable[stream];
  if (!(file->Channel)) return(-1); /* file not opened */

  memory = file->MemoryList;
  if (!memory) return(-1); /* file is empty */

  *minmem = *maxmem = s2long_bsl( memory->MemoryKey );

  while (memory) {
    memnum = s2long_bsl( memory->MemoryKey );
    if (memnum<*minmem) *minmem = memnum;
    else if (memnum>*maxmem) *maxmem = memnum;  
    memory = memory->Next;
   } 

  return(0);

} /* bsl_memory_range */

/*---------------------------------------------------------------------------
NAME

   bsl_frame_range --- returns the number of frames in a bsl memory 

SYNOPSIS

   int bsl_frame_range( int stream, long memnum, long * minfra, long * maxfra );

DESCRIPTION
Returns the minimum frame number (*minfra) and the maximum memory number
(*maxfra) of the bsl memory with number memnum.

RETURN VALUE
success:   int 0
error:     int -1
---------------------------------------------------------------------------*/
int bsl_frame_range( int stream, long memnum, long * minfra, long * maxfra )
{ BslFile * file;
  BslMemory * memory;
  BslFrame * frame;
  char memory_key[MaxBslLineLen+1];
  long franum;

  file = &BslTable[stream];
  if (!(file->Channel)) return(-1); /* file not opened */

  sprintf(memory_key,"%ld",memnum); 
  memory = search_bsl_memory ( file, memory_key ); 
  if (!memory) return(-1); /* file is empty */

  frame = memory->FrameList;
  if (!frame) return(-1); /* memory is empty */

  *minfra = *maxfra = s2long_bsl( frame->FrameKey );

  while (frame) {
    franum = s2long_bsl( frame->FrameKey );
    if (franum<*minfra) *minfra = franum;
    else if (franum>*maxfra) *maxfra = franum;
    frame = frame->Next;
   }

  return(0);

} /* bsl_frame_range */

/*---------------------------------------------------------------------------
NAME

   read_bsl_file_headers --- read the first and second header 

SYNOPSIS

   # include "edfio.h"
   void read_bsl_file_headers ( int stream, char **first_header, 
                                            char **second_header );

DESCRIPTION
Pointer to the first and second file header lines are returned.
These pointers remain valid until the bsl file is closed.
---------------------------------------------------------------------------*/
void read_bsl_file_headers ( int stream, char **first_header,  
                                         char **second_header )
{ BslFile   * file;

  *first_header  = (char *) NULL;
  *second_header = (char *) NULL;

  file = &(BslTable[stream]);
  if (!(file->Channel)) return; /* file not opened */

  *first_header  = file->FirstHeader;
  *second_header = file->SecondHeader;

} /* read_bsl_file_headers */

/*---------------------------------------------------------------------------
NAME

   read_bsl_data_spec --- read data spec of frame franum in memory memnum

SYNOPSIS

   # include "edfio.h"
   BslDataSpec * read_bsl_data_spec ( int stream, long memnum, long franum );

DESCRIPTION

RETURN VALUE
success BslDataSpec * pointer to data spec of frame franum in memory memnum 
error   NULL pointer
---------------------------------------------------------------------------*/
BslDataSpec * read_bsl_data_spec ( int stream, long memnum, long franum )
{ 
  BslFile   * file;
  BslMemory * memory;
  BslFrame  * frame;

  char      memory_key[MaxBslLineLen+1];
  char      frame_key[MaxBslLineLen+1];
  
  file = &(BslTable[stream]);
  if (!(file->Channel)) return((BslDataSpec *) NULL); /* file not opened */

  sprintf(memory_key,"%ld",memnum);
  memory = search_bsl_memory ( file, memory_key );
  if (!memory) return((BslDataSpec *) NULL); /* memory not found */

  sprintf(frame_key,"%ld",franum);
  frame = search_bsl_frame ( memory, frame_key );
  if (!frame) return((BslDataSpec *) NULL); /* frame not found */

  return( frame->DataSpec );

} /* read_bsl_data_spec */

/******************************************************************************/