File: sx.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 (1711 lines) | stat: -rw-r--r-- 58,948 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
1710
1711
/*
 *   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 SX_VERSION      "sx : V1.08 Peter Boesecke 2011-09-07"
/*+++------------------------------------------------------------------------
NAME
  sx.c --- 3d orientation parameter transformation

INCLUDE FILES
  # include sx.h

PURPOSE
  Transformation of parameters during change of sx orientation.
  See PUBLIC functions for detail.

AUTHOR
  2011 Peter Boesecke (PB)

HISTORY
  2011-04-27 V1.0  PB
  2011-05-18 V1.01 PB axis types added
  2011-05-25 V1.02 PB sx_tf_img etc.
  2011-06-07 V1.03 PB void * params_in -> const void * params_in
                      sx_tf_img: array_ -> data_, variance_, Usage
                      sx_tf_img: initialize outp after copying input 
  2011-06-16 V1.04 PB double constants marked, e.g. 1->1.0
  2011-07-14 V1.05 PB calculation of bdis does neither require cen nor bcenr,
                      calculation of cen or bcen from bcen or cen requires
                      dis or bdis and rotations.
  2011-07-16 V1.06 PB sx_tf_params, sx_tf_img: parameter rot added,
                      use default rotations, when necessary
  2011-07-26 V1.07 PB sx_init function added
  2011-09-07 V1.08 PB SXPARAMS: space between # and pro removed to 
                      facilitate usage of awk, e.g.
                      sxparams  {<parameters>} |  awk '{print $5}' returns
                      on the first line the parameter name and on the second line
                      its calculated value.
--------------------------------------------------------------------------*/

/****************************************************************************
*  Include                                                                  *
****************************************************************************/
# include "sx.h"

/****************************************************************************
* Definitions                                                               *
****************************************************************************/
# define SXPARAMS "#pro ori axis1 axis2 dim1 dim2 off1 off2 bis1 bis2 \
ras1 ras2 pix1 pix2 cen1 cen2 dis rot1 rot2 rot3 wvl \
bcen1 bcen2 bdis tilt1 tilt2 tilt3"

/****************************************************************************
* Static Variables                                                          *
****************************************************************************/

PRIVATE const double rad2deg = 180.0/NUM_PI;
PRIVATE const double SxEps = 1e-8;
PRIVATE int SxDebug = 0;
PRIVATE int SxLevel = 0;

PRIVATE char SX_Usage[SXBUFLEN];

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

  sx_version --- returns pointer to the version string

SYNOPSIS

  const char *sx_version ( void );

DESCRPTION

  Returns pointer to the version string.

--------------------------------------------------------------------------*/
PUBLIC const char *sx_version ( void )
{ 
  return ( SX_VERSION );
} /* sx_version */

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

  sx_usage2str --- return debug mode usage string

SYNOPSIS

  const char *sx_usage2str( void );

DESCRPTION

  Return debug mode usage string.

--------------------------------------------------------------------------*/
PUBLIC const char *sx_usage2str( void )
{ sprintf(SX_Usage,
    "verbose:0x%x,level:0x%x,showdata:0x%x,showtemp:0x%x,sxdebug:0x%x,sxraster:0x%x",
    SX_VERBOSE, SX_LEVEL, SX_SHOWDATA,
    SX_SHOWTEMP, SX_DEBUG, SX_RASTER_DEBUG);
  return(SX_Usage);
} // roca_usage2str

int fprint_debug( FILE *out )
{ fprintf(out,"debug      = 0x%x\n", SxDebug);
  fprintf(out,"verbose    = %d\n", SxDebug&SX_VERBOSE?1:0);
  fprintf(out,"level      = %d\n", SxLevel);
  fprintf(out,"showdata   = %d\n", SxDebug&SX_SHOWDATA?1:0);
  fprintf(out,"showtemp   = %d\n", SxDebug&SX_SHOWTEMP?1:0);
  fprintf(out,"sxdebug    = %d\n", SxDebug&SX_DEBUG?1:0);
  fprintf(out,"sxraster   = %d\n", SxDebug&SX_RASTER_DEBUG?1:0);
  return(0);
} // fprint_debug

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

  sx_debug_set --- set / reset module sx into debug mode

SYNOPSIS

  int sx_debug_set( int debug );

DESCRPTION

  Set / reset module sx into debug mode.

--------------------------------------------------------------------------*/
PUBLIC int sx_debug_set( int debug )
{ SxDebug = debug;
  SxLevel = (SxDebug&SX_LEVEL)>>1;

  raster_debug ( SxDebug&SX_RASTER_DEBUG?1:0 );

  if (SxDebug&SX_DEBUG) fprint_debug( stdout );
  return(0);

} // sx_debug_set

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

  sx_level --- return debug level 

SYNOPSIS

  int sx_level ( void );

--------------------------------------------------------------------------*/
PUBLIC int sx_level ( void )
{ return( SxLevel );
} // sx_Level

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

  sx_debug --- return debug value 

SYNOPSIS

  int sx_debug ( void );

--------------------------------------------------------------------------*/
PUBLIC int sx_debug ( void )

{ return( SxDebug );
} // sx_debug

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

  sx_init_params --- initializes sx parameters

SYNOPSIS

  SXParams * sx_init_params    ( SXParams * params );

DESCRPTION

  Initializes sx parameters with default values.

--------------------------------------------------------------------------*/
SXParams * sx_init_params    ( SXParams * params )
{
  if (SxDebug&SX_DEBUG) printf( "sx_init_params\n" );
  if ( params ) {
    // projection type as defined in reference.h
    params->pro.V = IO_ProSaxs;  params->pro.I = 0;
    // orientation number (1-16)
    params->ori.V = 1l;  params->ori.I = 0;
    // axis types as defined in reference.h
    params->axis1.V = IO_AxisTypeDistance;  params->axis1.I = 0;
    params->axis2.V = IO_AxisTypeDistance;  params->axis2.I = 0;
    // dimensions of 2d array
    params->dim1.V = 0l; params->dim1.I = 0;
    params->dim2.V = 1l; params->dim2.I = 0;
    // offsets of array coordinates
    params->off1.V = 0.0; params->off1.I = 0;
    params->off2.V = 0.0; params->off2.I = 0;
    // binning sizes
    params->bis1.V = 1.0; params->bis1.I = 0;
    params->bis2.V = 1.0; params->bis2.I = 0;
    // raster region of 2d array
    params->ras1.V = 0.0; params->ras1.I = 0;
    params->ras2.V = 0.0; params->ras2.I = 0;
    // pixel sizes [m]
    params->pix1.V = 1.0; params->pix1.I = 0;
    params->pix2.V = 1.0; params->pix2.I = 0;
    // PONI (point of normal incidence)
    params->cen1.V = 0.0; params->cen1.I = 0;
    params->cen2.V = 0.0; params->cen2.I = 0;
    // distance sample-PONI [m]
    params->dis.V = 1.0; params->dis.I = 0;
    // detector rotations [rad]
    params->rot1.V = 0.0; params->rot1.I = 0;
    params->rot2.V = 0.0; params->rot2.I = 0;
    params->rot3.V = 0.0; params->rot3.I = 0;
    // wavelength [m]
    params->wvl.V = 1.0; params->wvl.I = 0;
    // beam center (alt. cen1, cen2)
    params->bcen1.V = 0.0; params->bcen1.I = 0;
    params->bcen2.V = 0.0; params->bcen2.I = 0;
    // distance sample-bcen [m] (alt. dis)
    params->bdis.V = 1.0; params->bdis.I = 0;
    // detector rotations [rad]
    params->tilt1.V = 0.0; params->tilt1.I = 0;
    params->tilt2.V = 0.0; params->tilt2.I = 0;
    params->tilt3.V = 0.0; params->tilt3.I = 0;
  }

  if (SxDebug&SX_DEBUG) printf( "sx_init_params END\n" );
  return( params );
} // sx_init_params

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

  sx_cp_params --- copies input parameters to output

SYNOPSIS

  SXParams * sx_cp_params ( SXParams * params_out, 
                            const SXParams * params_in );

DESCRPTION

  Copies all params_in to params_out 

--------------------------------------------------------------------------*/
PUBLIC SXParams * sx_cp_params ( SXParams *params_out, 
                                 const SXParams *params_in )
{
  SXParams * out=NULL;

  if (SxDebug&SX_DEBUG) printf( "sx_cp_params\n" );

  if ( params_in && params_out ) {
    out = (SXParams *) memcpy( (void *) params_out, 
                               (const void *) params_in, sizeof( SXParams ));
  }

  if (SxDebug&SX_DEBUG) printf( "sx_cp_params END\n" );

  return( out );

} // sx_cp_params

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

  sx_new --- allocates and initializes new sx parameters

SYNOPSIS

  SXParams * sx_new    ( SXParams * params );

DESCRPTION

  Returns pointer to the successfully allocated and initialized
  parameters, otherwise NULL.

  If params is the NULL pointer, new memory is allocated and initialized,
  otherwise params is only initialized.

--------------------------------------------------------------------------*/
PUBLIC SXParams * sx_new    ( SXParams * params )
{ 
  SXParams * new = NULL;

  if (SxDebug&SX_DEBUG) printf( "sx_new\n" );

  if (!params) {
    if (!(new=malloc ( sizeof(SXParams) ))) 
      goto sx_new_error;
    params=new;
  }

  // initialize params
  if (!sx_init_params( params ))
    goto sx_new_error;

  if (SxDebug&SX_DEBUG) printf( "sx_new END\n" );

  return( params );

sx_new_error:

  if (new) free(new);

  if (SxDebug&SX_DEBUG) printf( "sx_new END (error)\n" );

  return(NULL);

} // sx_new

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

  sx_init --- initializes sx parameters

SYNOPSIS

  SXParams * sx_init    ( SXParams * params );

DESCRPTION

  Returns pointer to the successfully allocated and initialized
  parameters, otherwise NULL. Like sx_new, but without allocation of
  memory.

--------------------------------------------------------------------------*/
PUBLIC SXParams * sx_init    ( SXParams * params )
{
  if (SxDebug&SX_DEBUG) printf( "sx_init\n" );

  if (!params) goto sx_init_error;

  // initialize params
  if (!sx_init_params( params ))
    goto sx_init_error;

  if (SxDebug&SX_DEBUG) printf( "sx_init END\n" );

  return( params );

sx_init_error:

  if (SxDebug&SX_DEBUG) printf( "sx_init END (error)\n" );

  return(NULL);

} // sx_init

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

  sx_free --- releases the memory of sx parameters

SYNOPSIS

  SXParams * sx_free    ( SXParams * params );

DESCRPTION

  Returns NULL if successfully released, otherwise params.

--------------------------------------------------------------------------*/
PUBLIC SXParams * sx_free   ( SXParams * params )
{
  if (SxDebug&SX_DEBUG) printf( "sx_free\n" );
  if ( params ) free( params );
  if (SxDebug&SX_DEBUG) printf( "sx_free END\n" );

  return( NULL );

} // sx_free

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

  sx_pr_params --- print sx parameters

SYNOPSIS

  int sx_pr_params( FILE * out, const SXParams * params );

DESCRPTION

  Prints the value of the structure params.

RETURN VALUE

  0: success; -1: error

--------------------------------------------------------------------------*/
PUBLIC int sx_pr_params( FILE * out, const SXParams * params )
{
  if (SxDebug&SX_DEBUG) printf( "sx_pr_params\n" );

  if ((params)&&(out)) {

    fprintf(out,"  %s pro       = %10d : projection type (%d,%d)\n",
      params->pro.I?"X":" ",params->pro.V,IO_ProSaxs,IO_ProWaxs);
    fprintf(out,"  %s ori       = %10ld : orientation number (1-16)\n",
      params->ori.I?"X":" ",params->ori.V);
    fprintf(out,"  %s axis1     = %10d : type of axis 1 (%d,%d,%d)\n",
      params->axis1.I?"X":" ",params->axis1.V,IO_AxisTypeDistance,
      IO_AxisTypeAngle, IO_AxisTypeNumerator);
    fprintf(out,"  %s axis2     = %10d : type of axis 2 (%d,%d,%d)\n",
      params->axis2.I?"X":" ",params->axis2.V,IO_AxisTypeDistance, 
      IO_AxisTypeAngle, IO_AxisTypeNumerator);
    fprintf(out,"  %s dim1      = %10ld : dimension 1 of 2d array\n",
      params->dim1.I?"X":" ",params->dim1.V);
    fprintf(out,"  %s dim2      = %10ld : dimension 2 of 2d array\n",
      params->dim2.I?"X":" ",params->dim2.V);
    fprintf(out,"  %s off1      = %10lg : offset 1 of array coordinates\n",
      params->off1.I?"X":" ",params->off1.V);
    fprintf(out,"  %s off2      = %10lg : offset 2 of array coordinates\n",
      params->off2.I?"X":" ",params->off2.V);
    fprintf(out,"  %s bis1      = %10lg : binning size 1\n",
      params->bis1.I?"X":" ",params->bis1.V);
    fprintf(out,"  %s bis2      = %10lg : binning size 2\n",
      params->bis2.I?"X":" ",params->bis2.V);
    fprintf(out,"  %s ras1      = %10lg : raster region of axis 1\n",
      params->ras1.I?"X":" ",params->ras1.V);
    fprintf(out,"  %s ras2      = %10lg : raster region of axis 2\n",
      params->ras2.I?"X":" ",params->ras2.V);
    fprintf(out,"  %s pix1      = %10lg : pixel size 1 [m]\n",
      params->pix1.I?"X":" ",params->pix1.V);
    fprintf(out,"  %s pix2      = %10lg : pixel size 2 [m]\n",
      params->pix2.I?"X":" ",params->pix2.V);
    fprintf(out,"  %s cen1      = %10lg : PONI 1 (point of normal incidence)\n",
      params->cen1.I?"X":" ",params->cen1.V);
    fprintf(out,"  %s cen2      = %10lg : PONI 2 (point of normal incidence)\n",
      params->cen2.I?"X":" ",params->cen2.V);
    fprintf(out,"  %s dis       = %10lg : distance sample-PONI [m]\n",
      params->dis.I?"X":" ",params->dis.V);
    fprintf(out,"  %s rot1      = %10lg : detector rotation 1 [rad] (%10lg deg)\n",
      params->rot1.I?"X":" ",params->rot1.V,params->rot1.V*rad2deg);
    fprintf(out,"  %s rot2      = %10lg : detector rotation 2 [rad] (%10lg deg)\n",
      params->rot2.I?"X":" ",params->rot2.V,params->rot2.V*rad2deg);
    fprintf(out,"  %s rot3      = %10lg : detector rotation 3 [rad] (%10lg deg)\n",
      params->rot3.I?"X":" ",params->rot3.V,params->rot3.V*rad2deg);
    fprintf(out,"  %s wvl       = %10lg : wavelength [m]\n",
      params->wvl.I?"X":" ",params->wvl.V);
    fprintf(out,"  %s bcen1     = %10lg : beam center 1\n",
      params->bcen1.I?"X":" ",params->bcen1.V);
    fprintf(out,"  %s bcen2     = %10lg : beam center 2\n",
      params->bcen2.I?"X":" ",params->bcen2.V);
    fprintf(out,"  %s bdis      = %10lg : distance sample-bcen [m]\n",
      params->bdis.I?"X":" ",params->bdis.V);
    fprintf(out,"  %s tilt1      = %10lg : detector tilt 1 [rad] (%10lg deg)\n",
      params->tilt1.I?"X":" ",params->tilt1.V,params->tilt1.V*rad2deg);
    fprintf(out,"  %s tilt2      = %10lg : detector tilt 2 [rad] (%10lg deg)\n",
      params->tilt2.I?"X":" ",params->tilt2.V,params->tilt2.V*rad2deg);
    fprintf(out,"  %s tilt3      = %10lg : detector tilt 3 [rad] (%10lg deg)\n",
      params->tilt3.I?"X":" ",params->tilt3.V,params->tilt3.V*rad2deg);
  }

  if (SxDebug&SX_DEBUG) fprintf( stdout, "sx_pr_params END\n" );

  return(0);

} // sx_pr_params

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

  sx_pr_params_line --- print sx parameters in a single line

SYNOPSIS

  int sx_pr_params_line( FILE *out, const SXParams *params, int head );

DESCRIPTION

  Prints the value of the structure params. If head is > 0
  a commented head line with the name of all values is written on top.

  The parameters are

  SXI pro;                          // projection (IO_SaxsPro, IO_WaxsPro) 
  SXL ori;                          // orientation number (1-16)
  SXI axis1, axis2;                 // axis type (IO_AxisTypeDistance,
                                    // IO_AxisTypeAngle, IO_AxisTypeNumerator)
  SXL dim1;  SXL dim2;              // dimensions of 2d array 
  SXD off1;  SXD off2;              // offsets of array coordinates
  SXD bis1;  SXD bis2;              // binning sizes
  SXD ras1;  SXD ras2;              // raster region of 2d array 
  SXD pix1;  SXD pix2;              // pixel sizes [m]
  SXD cen1;  SXD cen2;              // PONI (point of normal incidence)
  SXD dis;                          // distance sample-PONI [m]
  SXD rot1;  SXD rot2; SXD rot3;    // detector rotations [rad]
  SXD wvl;                          // wavelength [m]
  SXD bcen1; SXD bcen2;             // beam center (alt. cen1, cen2)
  SXD bdis;                         // distance sample-bcen [m] (alt. dis) 
  SXD tilt1;  SXD tilt2; SXD tilt3; // detector tilts [rad]

RETURN VALUE

  0

---------------------------------------------------------------------------*/
PUBLIC int sx_pr_params_line( FILE *out, const SXParams *params, int head ) 
{
  if (SxDebug&SX_DEBUG) fprintf( stdout, "sx_pr_params_line\n" );

  if (head>0) fprintf(out,"%s\n",SXPARAMS);
  if (params->pro.I) fprintf(out,"%d ",params->pro.V); else fprintf(out,"- ");
  if (params->ori.I) fprintf(out,"%ld ",params->ori.V); else fprintf(out,"- ");
  if (params->axis1.I) fprintf(out,"%d ",params->axis1.V); else fprintf(out,"- ");
  if (params->axis2.I) fprintf(out,"%d ",params->axis2.V); else fprintf(out,"- ");
  if (params->dim1.I) fprintf(out,"%ld ",params->dim1.V); else fprintf(out,"- ");
  if (params->dim2.I) fprintf(out,"%ld ",params->dim2.V); else fprintf(out,"- ");
  if (params->off1.I) fprintf(out,"%lg ",params->off1.V); else fprintf(out,"- ");
  if (params->off2.I) fprintf(out,"%lg ",params->off2.V); else fprintf(out,"- ");
  if (params->bis1.I) fprintf(out,"%lg ",params->bis1.V); else fprintf(out,"- ");
  if (params->bis2.I) fprintf(out,"%lg ",params->bis2.V); else fprintf(out,"- ");
  if (params->ras1.I) fprintf(out,"%lg ",params->ras1.V); else fprintf(out,"- ");
  if (params->ras2.I) fprintf(out,"%lg ",params->ras2.V); else fprintf(out,"- ");
  if (params->pix1.I) fprintf(out,"%lg ",params->pix1.V); else fprintf(out,"- ");
  if (params->pix2.I) fprintf(out,"%lg ",params->pix2.V); else fprintf(out,"- ");
  if (params->cen1.I) fprintf(out,"%lg ",params->cen1.V); else fprintf(out,"- ");
  if (params->cen2.I) fprintf(out,"%lg ",params->cen2.V); else fprintf(out,"- ");
  if (params->dis.I) fprintf(out,"%lg ",params->dis.V); else fprintf(out,"- ");
  if (params->rot1.I) fprintf(out,"%lg ",params->rot1.V); else fprintf(out,"- ");
  if (params->rot2.I) fprintf(out,"%lg ",params->rot2.V); else fprintf(out,"- ");
  if (params->rot3.I) fprintf(out,"%lg ",params->rot3.V); else fprintf(out,"- ");
  if (params->wvl.I) fprintf(out,"%lg ",params->wvl.V); else fprintf(out,"- ");
  if (params->bcen1.I) fprintf(out,"%lg ",params->bcen1.V); else fprintf(out,"- ");
  if (params->bcen2.I) fprintf(out,"%lg ",params->bcen2.V); else fprintf(out,"- ");
  if (params->bdis.I) fprintf(out,"%lg ",params->bdis.V); else fprintf(out,"- ");
  if (params->tilt1.I) fprintf(out,"%lg ",params->tilt1.V); else fprintf(out,"- ");
  if (params->tilt2.I) fprintf(out,"%lg ",params->tilt2.V); else fprintf(out,"- ");
  if (params->tilt3.I) fprintf(out,"%lg ",params->tilt3.V); else fprintf(out,"- ");

  if (SxDebug&SX_DEBUG) fprintf( stdout, "sx_pr_params_line END\n" );

  return(0);

} // sx_pr_params_line

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

  sx_rd_params --- reads the parameters from the string array argv[]  

SYNOPSIS

  SXParams * sx_rd_params ( SXParams * params_out, char *argv[],
                              int * perrval );

DESCRIPTION

  The sx parameters are successively read from argv[0], argv[1], ...
  until the end of argv[], indicated by a NULL pointer, or until 
  all possible parameters have been read.

  If params_out is NULL a new parameter structure is allocated and
  needs, in case of success, to be released by the calling program.

  In case of success the pointer to the params_out or to the alloated
  parameter structure is returned.

RETURN VALUE

  In case of success the pointer to the parameter structure is returned,
  otherwise NULL.

---------------------------------------------------------------------------*/
PUBLIC SXParams * sx_rd_params ( SXParams * params_out, char *argv[], 
                                   int * perrval )

{
  char *nul = (char *) NULL;
  int errval=0;
  char **pargv=NULL;
  SXParams * params=NULL;
  long N=3;

  if (SxDebug&SX_DEBUG) printf( "sx_rd_params\n" );

  if (argv) pargv=&(argv[0]);
  else goto sx_rd_params_error;

  // initialize params_out (if NULL allocate and initialize)
  if ( !(params=sx_new ( params_out )) )
    goto sx_rd_params_error;

  if (*pargv==nul) goto sx_rd_params_end;

      // read params from argument list
      // projection (IO_ProSaxs, IO_ProWaxs) 
      if (sx_debug()&SX_DEBUG) 
        printf( "reading pro from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        strlib_tolower(*pargv);
        if ( (strcmp( *pargv,"saxs" ) == 0)||
             (strcmp( *pargv,"s" ) == 0)||
             (strncmp( *pargv,"no", 2 ) == 0) ) 
           params->pro.V = IO_ProSaxs; // no projection
        else if ( (strcmp( *pargv,"waxs" ) == 0)||
                  (strcmp( *pargv,"sp" ) == 0)||
                  (strncmp( *pargv,"ewa",3 ) == 0) ) 
           params->pro.V = IO_ProWaxs; // ewald sphere projection
        else {
          params->pro.V = (int) num_str2long ( *pargv, NULL, &errval );
          if (errval) goto sx_rd_params_error;
        }
        params->pro.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // ori orientation number (1-16)
      if (sx_debug()&SX_DEBUG) 
        printf( "reading ori from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->ori.V = raster_str2number( N, *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->ori.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // axis type (IO_AxisTypeDistance, IO_AxisTypeAngle, IO_AxisTypeNumerator)
      if (sx_debug()&SX_DEBUG)
        printf( "reading axis1 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        strlib_tolower(*pargv);
        if ( (strcmp( *pargv,"angle" ) == 0) )
           params->axis1.V = IO_AxisTypeDistance; // distance 
        else if ( (strcmp( *pargv,"angle" ) == 0) )
           params->axis1.V = IO_AxisTypeAngle; // angle 
        else if ( (strcmp( *pargv,"numerator" ) == 0) )
           params->axis1.V = IO_AxisTypeNumerator; // numerator 
        else {
          params->axis1.V = (int) num_str2long ( *pargv, NULL, &errval );
          if (errval) goto sx_rd_params_error;
        }
        params->axis1.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      if (sx_debug()&SX_DEBUG)
        printf( "reading axis2 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        strlib_tolower(*pargv);
        if ( (strcmp( *pargv,"angle" ) == 0) )
           params->axis2.V = IO_AxisTypeDistance; // distance
        else if ( (strcmp( *pargv,"angle" ) == 0) )
           params->axis2.V = IO_AxisTypeAngle; // angle
        else if ( (strcmp( *pargv,"numerator" ) == 0) )
           params->axis2.V = IO_AxisTypeNumerator; // numerator
        else {
          params->axis2.V = (int) num_str2long ( *pargv, NULL, &errval );
          if (errval) goto sx_rd_params_error;
        }
        params->axis2.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // dim1 dimensions of 2d array
      if (sx_debug()&SX_DEBUG) 
        printf( "reading dim1 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->dim1.V = num_str2long ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->dim1.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // dim2 dimensions of 2d array
      if (sx_debug()&SX_DEBUG) 
        printf( "reading dim2 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->dim2.V = num_str2long ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->dim2.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // off1 offsets of array coordinates
      if (sx_debug()&SX_DEBUG) 
        printf( "reading off1 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->off1.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->off1.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // off2 offsets of array coordinates
      if (sx_debug()&SX_DEBUG) 
        printf( "reading off2 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->off2.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
      params->off2.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // bis1 binning sizes
      if (sx_debug()&SX_DEBUG) 
        printf( "reading bis1 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->bis1.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->bis1.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // bis2 binning sizes
      if (sx_debug()&SX_DEBUG) 
        printf( "reading bis2 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->bis2.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->bis2.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // ras1 raster region of 2d array
      if (sx_debug()&SX_DEBUG) 
        printf( "reading ras1 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->ras1.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->ras1.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // ras2 raster region of 2d array
      if (sx_debug()&SX_DEBUG) 
        printf( "reading ras2 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->ras2.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->ras2.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // pix1 pixel sizes [m]
      if (sx_debug()&SX_DEBUG) 
        printf( "reading pix1 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->pix1.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->pix1.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // pix2 pixel sizes [m]
      if (sx_debug()&SX_DEBUG) 
        printf( "reading pix2 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->pix2.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->pix2.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // cen1 PONI (point of normal incidence)
      if (sx_debug()&SX_DEBUG) 
        printf( "reading cen1 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->cen1.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->cen1.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // cen2 PONI (point of normal incidence)
      if (sx_debug()&SX_DEBUG) 
        printf( "reading cen2 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->cen2.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->cen2.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // dis distance sample-PONI [m]
      if (sx_debug()&SX_DEBUG) 
        printf( "reading dis from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->dis.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->dis.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // rot1 detector rotations [rad]
      if (sx_debug()&SX_DEBUG) 
        printf( "reading rot1 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->rot1.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->rot1.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // rot2 detector rotations [rad]
      if (sx_debug()&SX_DEBUG) 
        printf( "reading rot2 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->rot2.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->rot2.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // rot3 detector rotations [rad]
      if (sx_debug()&SX_DEBUG) 
        printf( "reading rot3 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->rot3.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->rot3.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // wvl wavelength [m]
      if (sx_debug()&SX_DEBUG) 
        printf( "reading wvl from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->wvl.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->wvl.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // bcen1 beam center (alt. cen1, cen2)
      if (sx_debug()&SX_DEBUG) 
        printf( "reading bcen1 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->bcen1.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->bcen1.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // bcen2 beam center (alt. cen1, cen2)
      if (sx_debug()&SX_DEBUG) 
        printf( "reading bcen2 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->bcen2.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->bcen2.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // bdis distance sample-bcen [m] (alt. dis)
      if (sx_debug()&SX_DEBUG) 
        printf( "reading bdis from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->bdis.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->bdis.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // tilt1 detector tilts [rad]
      if (sx_debug()&SX_DEBUG) 
        printf( "reading tilt1 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->tilt1.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->tilt1.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // tilt2 detector tilt [rad]
      if (sx_debug()&SX_DEBUG) 
        printf( "reading tilt2 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->tilt2.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->tilt2.I = 1;
      }

      if (*(++pargv)==nul) goto sx_rd_params_end;
      // tilt3 detector tilts [rad]
      if (sx_debug()&SX_DEBUG) 
        printf( "reading tilt3 from >>%s<<\n", *pargv );
      if (strlib_is_no_skip(*pargv)) {
        params->tilt3.V = num_str2double ( *pargv, NULL, &errval );
        if (errval) goto sx_rd_params_error;
        params->tilt3.I = 1;
      }

sx_rd_params_end:

  if (perrval) *perrval = errval;

  if (SxDebug&SX_DEBUG) printf( "sx_rd_params END\n" );

  return(params);

sx_rd_params_error:

  if ((!params_out)&&(params)) sx_free(params);

  if (perrval) *perrval = errval;

  if (SxDebug&SX_DEBUG) 
    printf( "sx_rd_params END (errval=%d)\n",errval );

  return(NULL);

} // sx_rd_params

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

  sx_tf_params --- transforms sx parameters to orientation ori

SYNOPSIS

  SXParams *sx_tf_params ( SXParams * params_out, const SXParams * params_in,
                           long ori, int rot, int *perrval );

DESCRPTION

  Returns a pointer to the successfully transformed sx input parameters, 
  otherwise NULL. If the output pointer out is NULL memory is allocated 
  and must be released by the calling program, otherwise out is used. 
  The input (*in) and output (*buffer) buffers can be identical.

  params_in->pro.V: input projection (if 0, IO_ProSaxs is used)
  params_in->ori.V: input orientation (if 0, orientation 1 is used)
  long ori: output orientation (if 0, in->ori.V is used)
  int rot: use default rotations, when necessary

--------------------------------------------------------------------------*/
PUBLIC SXParams *sx_tf_params ( SXParams * params_out, 
                                const SXParams * params_in,
                                long ori, int rot, int *perrval )
{ SXParams *new=NULL, *outp=NULL;
  SXParams in_buffer, *inp;

  long t_ori, inv_t_ori;
  long *t_order=NULL, *inv_t_order=NULL;
  double *T=NULL, *inv_T=NULL;
  long omod2, omod4, omod8, omod16;

  int N=3;
  double R[3][3], RT[3][3], TRT[3][3];
  double Angle[3], TAngle[3];
  double Tilt[3], TTilt[3];

  int tmpX;
  long tmpL;
  double tmpV;
  int tmpI;
  int RotI=0;

  int errval=0;

  if (SxDebug&SX_DEBUG) printf( "sx_tf_params BEGIN\n" );

  if (params_in) {

    // copy input parameters to an internal buffer
    if (!(inp=sx_cp_params ( &in_buffer, params_in ))) {
      errval=SX_COPY_ERROR;
      goto sx_tf_params_error;
    }

    // normalize input projection
    if ( inp->pro.V == 0 ) inp->pro.V = IO_ProSaxs;

    // normalize input orientation
    if ( inp->ori.V == 0l ) inp->ori.V=1l; // default
    else if ( inp->ori.V < 0l ) inp->ori.V = raster_inversion( -inp->ori.V );

    // normalize axis types
    if ( inp->axis1.V == 0 ) inp->axis1.V = IO_AxisTypeDistance;
    if ( inp->axis2.V == 0 ) inp->axis2.V = IO_AxisTypeDistance;

    if (SxDebug&SX_SHOWDATA) {
      printf( "  Input parameters\n");
      sx_pr_params( stdout, inp );
    }

    if ( !(( inp->pro.V == IO_ProSaxs )||( inp->pro.V == IO_ProWaxs )) ) {
      errval=SX_INVALID_PROJECTION;
      goto sx_tf_params_error;
    }

    if ( inp->ori.V > 16l ) {
      errval=SX_INVALID_ORIENTATION;
      goto sx_tf_params_error;
    }

    if ( !(( inp->axis1.V == IO_AxisTypeDistance )||\
           ( inp->axis1.V == IO_AxisTypeAngle )||\
           ( inp->axis1.V == IO_AxisTypeNumerator )) ) {
      errval=SX_INVALID_AXISTYPE;
      goto sx_tf_params_error;
    }

    if ( !(( inp->axis2.V == IO_AxisTypeDistance )||\
           ( inp->axis2.V == IO_AxisTypeAngle )||\
           ( inp->axis2.V == IO_AxisTypeNumerator )) ) {
      errval=SX_INVALID_AXISTYPE;
      goto sx_tf_params_error;
    }

    // set output orientation default
    if ( ori == 0l ) ori = inp->ori.V;
    else if ( ori < 0l ) ori = raster_inversion( -ori );

    if (SxDebug&SX_SHOWDATA)
      printf( "  Output orientation = %ld\n",ori);

    if ( ori > 16l ) {
      errval=SX_INVALID_ORIENTATION;
      goto sx_tf_params_error;
    }

    // calculate tilts from rotations
    Angle[0] = inp->rot1.V;
    Angle[1] = inp->rot2.V;
    Angle[2] = inp->rot3.V;

    if (rot3d_matrix(Angle, R)) {
      errval=SX_MATRIX_CALCULATION_ERROR;
      goto sx_tf_params_error;
    }
    if (tilt3d_angles(Tilt, R)) {
      errval=SX_ANGLE_CALCULATION_ERROR;
      goto sx_tf_params_error;
    }

    // update tilt1, tilt2, tilt3
    if (inp->tilt1.I) Tilt[0] = inp->tilt1.V;
    if (inp->tilt2.I) Tilt[1] = inp->tilt2.V;
    if (inp->tilt3.I) Tilt[2] = inp->tilt3.V;

    if (tilt3d_matrix(Tilt, R)) {
      errval=SX_MATRIX_CALCULATION_ERROR;
      goto sx_tf_params_error;
    }
    RotI=((RotI)||(inp->tilt1.I)||(inp->tilt2.I)||(inp->tilt3.I))?1:0;

    // recalculate rot1, rot2, rot3
    if (rot3d_angles(Angle, R)) {
      errval=SX_ANGLE_CALCULATION_ERROR;
      goto sx_tf_params_error;
    }

    // update rotations
    if (inp->rot1.I) Angle[0] = inp->rot1.V;
    if (inp->rot2.I) Angle[1] = inp->rot2.V;
    if (inp->rot3.I) Angle[2] = inp->rot3.V;
    if (SxDebug&SX_SHOWTEMP) // display Angle
      raster_fprint_matrix( stdout, N, 1, Angle, "Angle[3]" );

    // calculation rotation matrix
    if (rot3d_matrix(Angle, R)) {
      errval=SX_MATRIX_CALCULATION_ERROR;
      goto sx_tf_params_error;
    }
    RotI=((RotI)||(inp->rot1.I)||(inp->rot2.I)||(inp->rot3.I))?1:0;

    if (SxDebug&SX_SHOWTEMP) // display R[3][3]
      raster_fprint_matrix( stdout, N, N, (double*) R, "R[3][3]" );

    // update beam parameters, if possible
    if (SxDebug&SX_SHOWTEMP) // fabs( R[2][2] )
      printf("R[2][2] = %lg, SxEps = %lg\n", R[2][2],SxEps);
    if ( fabs( R[2][2] ) > SxEps ) {
      // the detector normal is not perpendicular to the beam
      // update bcen and bdis if not set (to allow default values)
      //  SXD bcen1; SXD bcen2;       // beam center (alt. cen1, cen2)
      //  SXD bdis;                   // distance sample-bcen [m] (alt. dis) 
      if ( inp->pro.V==IO_ProSaxs ) {
        if (!inp->bdis.I) {
          if (r2t_bdis ( &(inp->bdis.V),inp->dis.V,R )) {
            errval=SX_BEAMDISTANCE_CALCULATION_ERROR;
            goto sx_tf_params_error;
          }
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->bdis.V = %lg (updated)\n", inp->bdis.V);
        }
        if (!inp->bcen1.I) {
          if (r2t_bcen1( &(inp->bcen1.V),inp->pix1.V,inp->cen1.V,inp->dis.V,R ) ) {
            errval=SX_BEAMCENTER_CALCULATION_ERROR;
            goto sx_tf_params_error;
          }
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->bcen1.V = %lg (updated)\n", inp->bcen1.V);
        }
        if (!inp->bcen2.I) {
          if (r2t_bcen2( &(inp->bcen2.V),inp->pix2.V,inp->cen2.V,inp->dis.V,R ) ) {
            errval=SX_BEAMCENTER_CALCULATION_ERROR;
            goto sx_tf_params_error;
          }
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->bcen2.V = %lg (updated)\n", inp->bcen2.V);
        }
      } else { // IO_ProWaxs
        if (!inp->bcen1.I) {
          inp->bcen1.V = inp->cen1.V;
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->bcen1.V = %lg (updated)\n", inp->bcen1.V);
        }
        if (!inp->bcen2.I) {
          inp->bcen2.V = inp->cen2.V;
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->bcen2.V = %lg (updated)\n", inp->bcen2.V);
        }
        if (!inp->bdis.I) {
          inp->bdis.V = inp->dis.V;
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->bdis.V = %lg (updated)\n", inp->bdis.V);
        }
      }

      // update dis, cen1, cen2 if not set
      //  SXD bcen1; SXD bcen2;       // beam center (alt. cen1, cen2)
      //  SXD bdis;                    // distance sample-bcen [m] (alt. dis) 
      if ( inp->pro.V==IO_ProSaxs ) {
        if (rot) RotI=((inp->bdis.I))?1:RotI;
        if (!inp->dis.I) {
          if (r2t_dis  ( &(inp->dis.V),inp->bdis.V,R )) {
            errval=SX_DISTANCE_CALCULATION_ERROR;
            goto sx_tf_params_error;
          }
          inp->dis.I=((RotI)&&(inp->bdis.I))?1:0;
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->dis.V = %lg (updated)\n", inp->dis.V);
        }
        if (rot) RotI=((inp->bcen1.I)&&(inp->pix1.I)&&
                      ((inp->bdis.I)||(inp->dis.I)))?1:RotI;
        if (!inp->cen1.I) {
          if (r2t_cen1 ( &(inp->cen1.V),inp->pix1.V,inp->bcen1.V,inp->bdis.V,R )) {
            errval=SX_CENTER_CALCULATION_ERROR;
            goto sx_tf_params_error;
          }
          inp->cen1.I=((RotI)&&(inp->bcen1.I)&&(inp->pix1.I)&&(inp->bdis.I))?1:0;
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->cen1.V = %lg (updated)\n", inp->cen1.V);
        }
        if (rot) RotI=((inp->bcen2.I)&&(inp->pix2.I)&&
                      ((inp->bdis.I)||(inp->dis.I)))?1:RotI;
        if (!inp->cen2.I) {
          if (r2t_cen2 ( &(inp->cen2.V),inp->pix2.V,inp->bcen2.V,inp->bdis.V,R )) {
            errval=SX_CENTER_CALCULATION_ERROR;
            goto sx_tf_params_error;
          }
          inp->cen2.I=((RotI)&&(inp->bcen2.I)&&(inp->pix2.I)&&(inp->bdis.I))?1:0;
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->cen2.V = %lg (updated)\n", inp->cen2.V);
        }
      } else { // IO_ProWaxs
        if (!inp->dis.I) {
          inp->dis.V=inp->bdis.V;
          inp->dis.I=(inp->bdis.I)?1:0;
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->dis.V = %lg (updated)\n", inp->dis.V);
        }
        if (!inp->cen1.I) {
          inp->cen1.V=inp->bcen1.V;
          inp->cen1.I=(inp->bcen1.I)?1:0;
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->cen1.V = %lg (updated)\n", inp->cen1.V);
        }
        if (!inp->cen2.I) {
          inp->cen2.V=inp->bcen2.V;
          inp->cen2.I=(inp->bcen1.I)?1:0;
          if (SxDebug&SX_SHOWTEMP)
            printf("inp->cen2.V = %lg (updated)\n", inp->cen2.V);
        }
      }
    } // if ( fabs( R[2][2] ) > SxEps ) 

    // initialize output buffer
    if (!params_out) {
      if (!(new=sx_new( NULL ))) {
        errval=SX_MEMORY_ALLOCATION_ERROR;
        goto sx_tf_params_error;
      }
      outp = new;
    } else if (!(outp=sx_new( params_out ))) {
      errval=SX_MEMORY_ALLOCATION_ERROR;
      goto sx_tf_params_error;
    }

    // copy input parameters to output parameters
    if (!sx_cp_params ( outp, inp )) {
      errval=SX_COPY_ERROR;
      goto sx_tf_params_error;
    }

    outp->ori.V = ori; outp->ori.I = 1;

    // calculate relative transformation t_ori from inp->ori.V to outp->ori.V
    if (SxDebug&SX_SHOWTEMP) // display outp->ori.V and inp->ori.V
      printf( "  outp->ori.V = %ld, inp->ori.V = %ld\n",outp->ori.V,inp->ori.V);
    t_ori = raster_multiplication( outp->ori.V, raster_inversion( inp->ori.V ) );
    if (SxDebug&SX_SHOWTEMP) // display t_ori
      printf( "  t_ori = %ld\n",t_ori);

    t_order = raster_number2order ( NULL, 0, 3, t_ori );
    if (!t_order) {
      errval=SX_ORDER_CALCULATION_ERROR;
      goto sx_tf_params_error;
    }

    inv_t_ori = raster_inversion ( t_ori );
    if (SxDebug&SX_SHOWTEMP) // display inv_t_ori
      printf( "  inv_t_ori = %ld\n",inv_t_ori);

    inv_t_order = raster_number2order ( NULL, 0, 3, inv_t_ori );
    if (!inv_t_order) {
      errval=SX_ORDER_CALCULATION_ERROR; 
      goto sx_tf_params_error;
    }

    T = raster_order2matrix ( NULL, 0, t_order );
    if (!T) {
      errval=SX_MATRIX_CALCULATION_ERROR;
      goto sx_tf_params_error;
    }
    if (SxDebug&SX_SHOWTEMP) // display T
      raster_fprint_matrix( stdout, N, N, T, "T[3][3]" );

    inv_T = raster_order2matrix ( NULL, 0, inv_t_order );
    if (!inv_T) {
      errval=SX_MATRIX_CALCULATION_ERROR;
      goto sx_tf_params_error;
    }
    if (SxDebug&SX_SHOWTEMP) // display inv_T
      raster_fprint_matrix( stdout, N, N, inv_T, "inv_T[3][3]" );

    omod2  = (t_ori-1l) % 2l;
    omod4  = (t_ori-1l) % 4l;
    omod8  = (t_ori-1l) % 8l;
    omod16 = (t_ori-1l) % 16l;

    if ( omod2 >= 1l ) { // invert first coordinate
      if (SxDebug&SX_DEBUG)
        printf("  invert first coordinate\n");
      if (inp->ras1.I) {
        outp->off1.V = OSWAP2(inp->ras1.V,inp->bis1.V,inp->off1.V,inp->dim1.V);
        outp->off1.I = 1;
      }
      outp->cen1.V = CSWAP2(outp->off1.V,inp->cen1.V,inp->off1.V,inp->dim1.V);
      outp->cen1.I = inp->cen1.I;
    }

    if ( omod4 >= 2l ) { // invert second coordinate
      if (SxDebug&SX_DEBUG)
        printf("  invert second coordinate\n");
      if (inp->ras2.I) {
        outp->off2.V = OSWAP2(inp->ras2.V,inp->bis2.V,inp->off2.V,inp->dim2.V);
        outp->off2.I = 1;
      }
      outp->cen2.V = CSWAP2(outp->off2.V,inp->cen2.V,inp->off2.V,inp->dim2.V);
      outp->cen2.I = inp->cen2.I;
    }

    if ( omod8 >= 4l ) { // swap coordinates
      if (SxDebug&SX_DEBUG)
        printf("  swap first and second coordinates\n");

      tmpX = outp->axis1.V; tmpI = outp->dim1.I;
      outp->axis1.V = outp->axis2.V; outp->axis1.I = outp->axis2.I;
      outp->axis2.V = tmpX; outp->axis2.I = tmpI;

      tmpL = outp->dim1.V; tmpI = outp->dim1.I;
      outp->dim1.V = outp->dim2.V; outp->dim1.I = outp->dim2.I;
      outp->dim2.V = tmpL; outp->dim2.I = tmpI;

      tmpV = outp->off1.V; tmpI = outp->off1.I;
      outp->off1.V = outp->off2.V; outp->off1.I = outp->off2.I;
      outp->off2.V = tmpV; outp->off2.I = tmpI;

      tmpV = outp->cen1.V; tmpI = outp->cen1.I;
      outp->cen1.V = outp->cen2.V; outp->cen1.I = outp->cen2.I;
      outp->cen2.V = tmpV; outp->cen2.I = tmpI;

      tmpV = outp->bis1.V; tmpI = outp->bis1.I;
      outp->bis1.V = outp->bis2.V; outp->bis1.I = outp->bis2.I;
      outp->bis2.V = tmpV; outp->bis2.I = tmpI;

      tmpV = outp->pix1.V; tmpI = outp->pix1.I;
      outp->pix1.V = outp->pix2.V; outp->pix1.I = outp->pix2.I;
      outp->pix2.V = tmpV; outp->pix2.I = tmpI;

      tmpV = outp->ras1.V; tmpI = outp->ras1.I;
      outp->ras1.V = outp->ras2.V; outp->ras1.I = outp->ras2.I;
      outp->ras2.V = tmpV; outp->ras2.I = tmpI;
    }

    outp->dis.V = inp->dis.V; outp->dis.I = inp->dis.I;
    outp->wvl.V = inp->wvl.V; outp->wvl.I = inp->wvl.I;

//  SXD rot1;  SXD rot2; SXD rot3; // detector rotations [rad]

    raster_matrix_product ( (double*) RT, N*N, (double *) R, T, N, N, N );
    if (SxDebug&SX_SHOWTEMP)// display RT
      raster_fprint_matrix( stdout, N, N, (double*) RT, "RT[3][3]" );
    raster_matrix_product ( (double*) TRT, N*N, inv_T, (double *) RT, N, N, N );
    if (SxDebug&SX_SHOWTEMP)// display TRT
      raster_fprint_matrix( stdout, N, N, (double*) TRT, "TRT[3][3]" );
    if (rot3d_angles(TAngle, TRT)) {
      errval=SX_ANGLE_CALCULATION_ERROR;
      goto sx_tf_params_error;
    }
    if (SxDebug&SX_SHOWTEMP)// display TAngle
      raster_fprint_matrix( stdout, N, 1, TAngle, "TAngle[3]" );

    outp->rot1.V = TAngle[0];
    outp->rot2.V = TAngle[1];
    outp->rot3.V = TAngle[2];

    outp->rot1.I = RotI;
    outp->rot2.I = RotI;
    outp->rot3.I = RotI;

    // update tilt1, tilt2, tilt3
    if (tilt3d_angles(TTilt, TRT)) {
      errval=SX_ANGLE_CALCULATION_ERROR;
      goto sx_tf_params_error;
    }
    if (SxDebug&SX_SHOWTEMP)// display TTilts
      raster_fprint_matrix( stdout, N, 1, TTilt, "TTilts[3]" );

    outp->tilt1.V = TTilt[0];
    outp->tilt2.V = TTilt[1];
    outp->tilt3.V = TTilt[2];

    outp->tilt1.I = RotI;
    outp->tilt2.I = RotI;
    outp->tilt3.I = RotI;

    // update beam parameters if detector plane is not perpendicular to beam
    if (SxDebug&SX_SHOWTEMP) // fabs( TRT[2][2] )
      printf("TRT[2][2] = %lg, SxEps = %lg\n", TRT[2][2],SxEps);
    if ( fabs( TRT[2][2] ) > SxEps ) {
      // update bcen and bdis
      //  SXD bcen1; SXD bcen2;      // beam center (alt. cen1, cen2)
      //  SXD bdis;                   // distance sample-bcen [m] (alt. dis) 
      if ( outp->pro.V==IO_ProSaxs ) {
        if (r2t_bdis ( &(outp->bdis.V),outp->dis.V,TRT )) {
          errval=SX_BEAMDISTANCE_CALCULATION_ERROR;
          goto sx_tf_params_error;
        }
        outp->bdis.I=((outp->dis.I)&&(RotI))?1:0;
        if (SxDebug&SX_SHOWTEMP)
          printf("outp->bdis.V = %lg (updated)\n", outp->bdis.V);
        if (r2t_bcen1( &(outp->bcen1.V),outp->pix1.V,outp->cen1.V,outp->dis.V,TRT )){
          errval=SX_BEAMCENTER_CALCULATION_ERROR;
          goto sx_tf_params_error;
        }
        outp->bcen1.I=((outp->cen1.I)&&(RotI)&&(outp->pix1.I))?1:0;
        if (SxDebug&SX_SHOWTEMP)
          printf("outp->bcen1.V = %lg (updated)\n", outp->bcen1.V);
        if (r2t_bcen2( &(outp->bcen2.V),outp->pix2.V,outp->cen2.V,outp->dis.V,TRT )){
          errval=SX_BEAMCENTER_CALCULATION_ERROR;
          goto sx_tf_params_error;
        }
        outp->bcen2.I=((outp->cen2.I)&&(RotI)&&(outp->pix2.I))?1:0;
        if (SxDebug&SX_SHOWTEMP)
          printf("outp->bcen2.V = %lg (updated)\n", outp->bcen2.V);
      } else { // IO_ProWaxs
        outp->bdis.V=outp->dis.V; 
        outp->bdis.I=((outp->dis.I)&&(RotI))?1:0;
        if (SxDebug&SX_SHOWTEMP)
          printf("outp->bdis.V = %lg (updated)\n", outp->bdis.V);
        outp->bcen1.V=outp->cen1.V; 
        outp->bcen1.I=((outp->cen1.I)&&(RotI))?1:0;
        if (SxDebug&SX_SHOWTEMP)
          printf("outp->bcen1.V = %lg (updated)\n", outp->bcen1.V);
        outp->bcen2.V=outp->cen2.V; 
        outp->bcen2.I=((outp->cen2.I)&&(RotI))?1:0;
        if (SxDebug&SX_SHOWTEMP)
          printf("outp->bcen2.V = %lg (updated)\n", outp->bcen2.V);
      }
    } // if ( fabs( TRT[2][2] ) > SxEps )

    if (inv_T) free(inv_T);
    if (T) free(T);
    if (inv_t_order) free(inv_t_order);
    if (t_order) free(t_order);

  }

  errval = SX_SUCCESS;

  if (SxDebug&SX_SHOWDATA) {
     printf("  Output parameters\n");
     sx_pr_params( stdout, outp );
  }

  if (perrval) *perrval=errval;

  if (SxDebug&SX_DEBUG) printf( "sx_tf_params END\n" );

  return( outp );

sx_tf_params_error:

  if (inv_T) free(inv_T);
  if (T) free(T);
  if (inv_t_order) free(inv_t_order);
  if (t_order) free(t_order);
  if (new) free(new);

  if (perrval) *perrval=errval;

  if (SxDebug&SX_DEBUG) 
    printf( "sx_tf_params END (error=%d)\n",errval );

  return( NULL );

} // sx_tf_params

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

  sx_tf_img --- transforms an image to a different orientation

SYNOPSIS

  int sx_tf_img ( SXParams *params_out,  
              void *data_out, void *variance_out, size_t item_number,
              const SXParams *params_in, 
              const void *data_in, const void *variance_in, size_t item_size,
              long ori, int rot, int *perrval );

ARGUMENTS

   SXParams *params_out (o) : output sx params (must be allocated)
   void *data_out       (o) : output data array (must be allocated)
   size_t item_number   (i) : allocated number of array elements (all arrays)
   const SXParams *params_in (i) : input sx params
   const void *data_in  (i) : input data array 
   size_t item_size     (i) : size of a single array element (both arrays)
   long ori             (i) : output raster orientation
   int rot              (i) : use default rotations, if they are not supplied
   int * perrval        (o) : error message

DESCRPTION

  params_in contains the image parameters, data_in the pixel data.
  ori is the orientation of the output image.
  The transformed parameters are written to params_out and the pixel data 
  to data_out.  params_out and data_out must be sufficiently large.
  If data_in or data_out is the NULL pointer, only the parameter are 
  converted. 
  Input (params_in, data_in) and output (params_out, data_out) parameters 
  can be identical.

RETURN VALUE

   0 in case of success
  -1 otherwise

--------------------------------------------------------------------------*/
PUBLIC int
  sx_tf_img ( SXParams *params_out, 
              void *data_out, void *variance_out, size_t item_number,
              const SXParams *params_in, 
              const void *data_in, const void *variance_in, size_t item_size,
              long ori, int rot, int *perrval )

{ int errval;
  long order_in[4], order_out[4], order_inv[4], order_tf[4];
  long data_dim_in[4];
  size_t used_number;
  SXParams params; // copy of *params_in

  if (SxDebug&SX_DEBUG) 
    printf( "sx_tf_img BEGIN\n" );

  if (!params_out) {
    errval=SX_NULL_POINTER;
    goto sx_tf_img_error;
  }

  /* make a copy of params_in */
  if ( !(sx_cp_params ( &params, params_in )) ) {
    errval=SX_COPY_ERROR;
    goto sx_tf_img_error;
  }

  if ( !(sx_tf_params (params_out, &params, ori, rot, &errval)) )
    goto sx_tf_img_error;

  /* reorder arrays */
  if ( ((data_in)&&(data_out))||((variance_in)&&(variance_out)) ) {

    if (SxDebug&SX_DEBUG)
      printf( "  reorder arrays: orientation %ld -> %ld\n",
        params.ori.V, params_out->ori.V );

    if ( !(raster_number2order ( order_in, 4, 3, params.ori.V )) ) {
      errval=SX_INVALID_ORIENTATION;
      goto sx_tf_img_error;
    }

    if (SxDebug&SX_DEBUG)
      printf("    order_in=%ld\n",raster_order2number(order_in));

    if ( (!raster_number2order ( order_out, 4, 3, params_out->ori.V )) ) {
      errval=SX_INVALID_ORIENTATION;
      goto sx_tf_img_error;
    }
    if (SxDebug&SX_DEBUG)
      printf("    order_out=%ld\n",raster_order2number(order_out));

    if ( (!raster_order_inversion ( order_inv, 4, order_out )) ) {
      errval=SX_INVALID_ORIENTATION;
      goto sx_tf_img_error;
    }
    if (SxDebug&SX_DEBUG)
      printf("    order_out_inv=%ld\n",raster_order2number(order_inv));

    if ( (!raster_order_multiplication(order_tf, 4, order_inv, order_in)) ) {
      errval=SX_INVALID_ORIENTATION;
      goto sx_tf_img_error;
    }
    if (SxDebug&SX_DEBUG)
      printf("    order_tf=%ld\n",raster_order2number(order_tf));

    data_dim_in[0]=3l;
    data_dim_in[1]=params.dim1.V;
    data_dim_in[2]=params.dim2.V;
    data_dim_in[3]=1l; // third dimension is 1

    /* check, that item_number is sufficiently large */
    used_number = (unsigned long) ( data_dim_in[1]*data_dim_in[2] );
    if (item_number<used_number) {
      errval=SX_ARRAY_TOOSMALL;
      goto sx_tf_img_error;
    }

    if ( (data_in)&&(data_out) ) {
      if (SxDebug&SX_DEBUG)
        printf( "  raster_order_normalization\n" );
      if ( raster_order_normalization ( data_out, data_in, data_dim_in, 
                                        order_tf, item_size, &errval) ) {
        errval+=SX_RASTER_ERROR;
        goto sx_tf_img_error;
      }
    }

    if ( (variance_in)&&(variance_out) ) {
      if ( raster_order_normalization ( variance_out, variance_in, data_dim_in,
                                        order_tf, item_size, &errval) ) {
        errval+=SX_RASTER_ERROR;
        goto sx_tf_img_error;
      }
    }
  }

  errval = SX_SUCCESS;

  if (perrval) *perrval=errval;

  if (SxDebug&SX_DEBUG) printf( "sx_tf_img END\n" );

  return(0);

sx_tf_img_error:

  if (perrval) *perrval=errval;

  if (SxDebug&SX_DEBUG)
    printf( "sx_tf_img END (error=%d)\n",errval );

  return( -1 );

} // sx_tf_img

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

  sx_errval2str --- writes error message into a buffer

SYNOPSIS

  char *sx_errval2str( char buffer[], size_t buflen, int errval );

ARGUMENTS

   char buffer[]        (i) : output buffer for message
   size_t buflen        (i) : length of buffer (including terminating NULL)
   int errval           (i) : error message to write
   return char *        (o) : pointer to buffer 

DESCRPTION

  Writes an error message into the buffer corresponding to errval.

  SX_INVALID_PROJECTION, SX_INVALID_ORIENTATION, 
  SX_MEMORY_ALLOCATION_ERROR, SX_ORDER_CALCULATION_ERROR, 
  SX_MATRIX_CALCULATION_ERROR, SX_ANGLE_CALCULATION_ERROR,
  SX_BEAMCENTER_CALCULATION_ERROR, SX_BEAMDISTANCE_CALCULATION_ERROR,
  SX_CENTER_CALCULATION_ERROR, SX_DISTANCE_CALCULATION_ERROR,
  SX_COPY_ERROR

--------------------------------------------------------------------------*/
PUBLIC char *sx_errval2str( char buffer[], size_t buflen, int errval )
{
  char * value;

  value=buffer;

  if ( (buffer)&&(buflen>0) ) {
    switch (errval) {
      case SX_SUCCESS:
             strncpy(buffer,"success",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_NULL_POINTER:
             strncpy(buffer,"NULL pointer",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_INVALID_PROJECTION:
             strncpy(buffer,"invalid projection",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_INVALID_ORIENTATION:
             strncpy(buffer,"invalid orientation",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_INVALID_AXISTYPE:
             strncpy(buffer,"invalid axis type",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_MEMORY_ALLOCATION_ERROR:
             strncpy(buffer,"memory allocation error",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_ORDER_CALCULATION_ERROR:
             strncpy(buffer,"order calculation error",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_MATRIX_CALCULATION_ERROR:
             strncpy(buffer,"matrix calculation error",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_ANGLE_CALCULATION_ERROR:
             strncpy(buffer,"angle calculation error",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_BEAMCENTER_CALCULATION_ERROR:
             strncpy(buffer,"beam center calculation error",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_BEAMDISTANCE_CALCULATION_ERROR:
             strncpy(buffer,"beam distance calculation error",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_CENTER_CALCULATION_ERROR:
             strncpy(buffer,"center calculation error",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_DISTANCE_CALCULATION_ERROR:
             strncpy(buffer,"distance calculation error",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_COPY_ERROR:
             strncpy(buffer,"copying error",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_ARRAY_TOOSMALL:
             strncpy(buffer,"array size too small",buflen-1);
             buffer[buflen-1]='\0';
             break;
      case SX_RASTER_ERROR:
             strncpy(buffer,"raster error",buflen-1);
             buffer[buflen-1]='\0';
             break;
      default:
             strncpy(buffer,"error value",buflen-1);
             buffer[buflen-1]='\0';

    } // switch
  } else value=NULL;

  return( value );

} // sx_errval2str

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