psrdada-sys 0.2.0

Bindgen wrappers for psrdada
Documentation
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
#include "dada_pwc_main.h"
#include "dada_def.h"

#include "ascii_header.h"

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>

// #define _DEBUG 1

/*! Create a new DADA primary write client main loop */
dada_pwc_main_t* dada_pwc_main_create ()
{
  dada_pwc_main_t* pwcm = malloc (sizeof(dada_pwc_main_t));
  assert (pwcm != 0);

  pwcm -> pwc = 0;
  pwcm -> log = 0;

  pwcm -> data_block = 0;
  pwcm -> header_block = 0;

  pwcm -> start_function = 0;
  pwcm -> buffer_function = 0;
  pwcm -> block_function = 0;
  pwcm -> stop_function = 0;
  pwcm -> error_function = 0;
  pwcm -> header_valid_function = 0;
  pwcm -> xfer_pending_function = 0;
  pwcm -> new_xfer_function = 0;

  pwcm -> context = 0;
  pwcm -> header = 0;
  pwcm -> header_size = 0;
  pwcm -> verbose = 0;
  pwcm -> header_valid = 0;

  return pwcm;
}

/*! Destroy a DADA primary write client main loop */
void dada_pwc_main_destroy (dada_pwc_main_t* pwcm)
{
  free (pwcm);
}

/*! prepare for data transfer */
int dada_pwc_main_prepare (dada_pwc_main_t* pwcm);

/*! start the data transfer */
int dada_pwc_main_start_transfer (dada_pwc_main_t* pwcm);

/*! do the data transfer */
int dada_pwc_main_transfer_data (dada_pwc_main_t* pwcm);

/*! do the data transfer, via zero copy block operations */
int dada_pwc_main_transfer_data_block (dada_pwc_main_t* pwcm);

/*! stop the data transfer */
int dada_pwc_main_stop_transfer (dada_pwc_main_t* pwcm);

/*! Run the DADA primary write client main loop */
int dada_pwc_main (dada_pwc_main_t* pwcm)
{
 
  if (!pwcm) {
    fprintf (stderr, "dada_pwc_main no main!\n");
    return -1;
  }

  if (!pwcm->pwc) {
    fprintf (stderr, "dada_pwc_main no PWC command connection\n");
    return -1;
  }

  if (!pwcm->log) {
    fprintf (stderr, "dada_pwc_main no logging facility\n");
    return -1;
  }

  if (!pwcm->start_function) {
    fprintf (stderr, "dada_pwc_main no start function\n");
    return -1;
  }

  if (!pwcm->buffer_function) {
    fprintf (stderr, "dada_pwc_main no buffer function\n");
    return -1;
  }

  if (!pwcm->stop_function) {
    fprintf (stderr, "dada_pwc_main no stop function\n");
    return -1;
  }

  if (!pwcm->pwc->log)
    pwcm->pwc->log = pwcm->log;


  /* the return value can be
   *  0:  no error
   * -1:  soft error   pwc_command will try to restart
   * -2:  hard error   fatal, but data block intact, dada clients ok
   * -3:  fatal error  fatal, data block damaged, dada clients hung
   */
  int rval = 0;

  while (!dada_pwc_quit (pwcm->pwc))
  {
    /* Enter the idle/prepared state. */
    rval = dada_pwc_main_prepare (pwcm);

    if (dada_pwc_quit (pwcm->pwc))
      break;

    if (rval < 0)
      dada_pwc_main_process_error (pwcm, rval);
    else
    {
      /* Start the data transfer. */
      rval = dada_pwc_main_start_transfer (pwcm);
      if (rval < 0) 
        dada_pwc_main_process_error (pwcm, rval);
      else {

        /* Enter the clocking/recording state. */
        if (pwcm->block_function)
          rval = dada_pwc_main_transfer_data_block (pwcm);
        else
          rval = dada_pwc_main_transfer_data (pwcm);
        if (rval < 0) 
          dada_pwc_main_process_error (pwcm, rval); 

      }

      /* Stop the data transfer. */
      rval = dada_pwc_main_stop_transfer (pwcm);
      if (rval < 0) 
        dada_pwc_main_process_error (pwcm, rval);

    } 

    /* If we ever reach a fatal state, try to exit */
    if (pwcm->pwc->state == dada_pwc_fatal_error) 
      pwcm->pwc->quit = 1;

  }

  return rval;

}

/*! The idle and prepared states of the DADA primary write client main loop */
int dada_pwc_main_prepare (dada_pwc_main_t* pwcm)
{
  /* get next available Header Block */
  if (pwcm->header_block) {
    pwcm->header_size = ipcbuf_get_bufsz (pwcm->header_block);
    pwcm->header = ipcbuf_get_next_write (pwcm->header_block);
    if (!pwcm->header) {
      multilog (pwcm->log, LOG_ERR, "Could not get next header block\n");
      return DADA_ERROR_HARD;
    }
  }

  /* ensure that Data Block is closed */
  if (pwcm->data_block && ipcio_is_open (pwcm->data_block)
      && ipcio_close (pwcm->data_block) < 0)
  {
    multilog (pwcm->log, LOG_ERR, "Could not close Data Block\n");
    return DADA_ERROR_HARD;
  }

  while (!dada_pwc_quit (pwcm->pwc))
  {
    pwcm->command = dada_pwc_command_get (pwcm->pwc);

    if (dada_pwc_quit (pwcm->pwc))
      break;

    if (pwcm->command.code == dada_pwc_reset)
    {
      dada_pwc_set_state (pwcm->pwc, dada_pwc_idle, 0);
    }
    
    else if (pwcm->command.code == dada_pwc_header)
    {
#ifdef _DEBUG 
      multilog (pwcm->log, LOG_INFO, 
                "HEADER START\n%s\nHEADER END\n", pwcm->command.header);
#endif

      if (pwcm->header_block)
        strncpy (pwcm->header, pwcm->command.header, pwcm->header_size);

      dada_pwc_set_state (pwcm->pwc, dada_pwc_prepared, 0);
    }

    else if (pwcm->command.code == dada_pwc_clock)
    {
      /* multilog (pwcm->log, LOG_INFO, "Start clocking data\n"); */

      if (pwcm->command.byte_count) {
        multilog (pwcm->log, LOG_ERR, "dada_pwc_main_idle internal error.  "
                 "byte count specified in CLOCK command\n");
        return DADA_ERROR_SOFT;
      }

      /* Open the Data Block in clocking mode */
      if (pwcm->data_block && ipcio_open (pwcm->data_block, 'w') < 0)  {
        multilog (pwcm->log, LOG_ERR, "Could not open data block\n");
        return DADA_ERROR_HARD;
      }

      /* leave the idle state loop */
      return 0;
        
    }
    else if (pwcm->command.code == dada_pwc_start)
    {
#ifdef _DEBUG
      multilog (pwcm->log, LOG_INFO, "Start recording data\n");
#endif

      if (pwcm->command.byte_count)
        multilog (pwcm->log, LOG_INFO,
                  "Will record %"PRIu64" bytes\n", pwcm->command.byte_count);

      /* Open the Data Block in recording mode */
      if (pwcm->data_block && ipcio_open (pwcm->data_block, 'W') < 0)  {
        multilog (pwcm->log, LOG_ERR, "Could not open data block\n");
        return DADA_ERROR_HARD;
      }
      
      /* leave the idle state loop */
      return 0;
    }
    else if (pwcm->command.code == dada_pwc_stop) 
    {
      if (pwcm->pwc->state == dada_pwc_soft_error) 
        multilog (pwcm->log, LOG_WARNING, "Resetting soft_error to idle\n");
      else
        multilog (pwcm->log, LOG_WARNING, "dada_pwc_main_prepare: Unexpected stop command\n");

      dada_pwc_set_state (pwcm->pwc, dada_pwc_idle, 0);
    }
    else
    {
      multilog (pwcm->log, LOG_ERR, "dada_pwc_main_prepare internal error = "
               "unexpected command code %s\n", 
               dada_pwc_cmd_code_string(pwcm->command.code));
      return DADA_ERROR_HARD;
    }

  }

  return 0;
}

/*! The transit to clocking/recording state */
int dada_pwc_main_start_transfer (dada_pwc_main_t* pwcm)
{
  /* If utc != 0, the start function should attempt to start the data
     transfer at the specified utc.  Otherwise, start as soon as
     possible.  Regardless, the start function should return the UTC
     of the first time sample to be transfered to the Data Block.
  */

#ifdef _DEBUG
  fprintf (stderr, "dada_pwc_main_start_transfer: call start function\n");
#endif

  time_t utc = pwcm->start_function (pwcm, pwcm->command.utc);

  const unsigned int buffer_size = 32;
  char buffer[buffer_size];

  if (utc < 0) {
    multilog (pwcm->log, LOG_ERR, "start_function returned invalid UTC\n");
    return DADA_ERROR_HARD;
  }

  /* If the start function, cannot provide the utc start, 0 
   * indicates that the UTC will be provided by the command
   * interface */
  if (utc == 0)
    sprintf (buffer, "%s", "UNKNOWN");
  else 
  {
    strftime (buffer, buffer_size, DADA_TIMESTR, gmtime (&utc));
  }

  multilog (pwcm->log, LOG_INFO, "UTC_START = %s\n", buffer);

  /* make header available on Header Block */
  if (pwcm->header_block) {

    /* write UTC_START to the header */
    if (ascii_header_set (pwcm->header, "UTC_START", "%s", buffer) < 0) {
      multilog (pwcm->log, LOG_ERR, "failed ascii_header_set UTC_START\n");
      return DADA_ERROR_SOFT;
    }

    /* Set the primary UTC_START in the pwc if we have it */
    if (utc > 0) {

      /* Set the base utc from which all commands are calculated */
      pwcm->pwc->utc_start = utc;
      multilog(pwcm->log, LOG_INFO, "Setting pwcm->pwc->utc_start = %d\n",pwcm->pwc->utc_start);

    }

    /* We can only mark the header filled if we have a start command */
    if (pwcm->command.code == dada_pwc_start) {

      /* only mark header block filled if header is valid */
      if (pwcm->header_valid_function)
        pwcm->header_valid = pwcm->header_valid_function(pwcm);
      else 
        pwcm->header_valid = 1;

      if (pwcm->header_valid) {
#ifdef _DEBUG
          multilog(pwcm->log, LOG_INFO, "dada_pwc_main_start_transfer: marking header filled\n");
#endif
        if ( ipcbuf_mark_filled (pwcm->header_block, pwcm->header_size) < 0)  {
          multilog (pwcm->log, LOG_ERR, "Could not marked header filled or command.code != start\n");
          return DADA_ERROR_HARD;
        }
      }
    }
  } 

#ifdef _DEBUG
  fprintf (stderr, "dada_pwc_main_start_transfer: exit change state\n");
#endif

  ipcbuf_set_soclock_buf ((ipcbuf_t*) pwcm->data_block);

  if (pwcm->command.code == dada_pwc_clock)
    return dada_pwc_set_state (pwcm->pwc, dada_pwc_clocking, utc);

  else if (pwcm->command.code == dada_pwc_start) 
    return dada_pwc_set_state (pwcm->pwc, dada_pwc_recording, utc);
 

  multilog (pwcm->log, LOG_ERR, "dada_pwc_main_start_transfer"
            " internal error = invalid state\n");
  return DADA_ERROR_HARD;
}

/*! Switch from clocking to recording states */
int dada_pwc_main_record_start (dada_pwc_main_t* pwcm)
{

  /* the minimum offset at which recording may start */
  uint64_t minimum_record_start = 0;

  /* the byte to which the UTC_START corresponds */
  uint64_t utc_start_byte = 0;

  /* the byte to which command will correspond */
  uint64_t command_start_byte = 0;

  /* the next header to be written */
  char* header = 0;

  /* Find the earliest byte recording can start on. NB this may
   * have wrapped around if we have been clocking for longer than
   * the data block length... */
  minimum_record_start = ipcio_get_start_minimum (pwcm->data_block);

  /* If the first data received did not occur on buf 0, then
   * we need to know this to correctly calculate the command
   * offset */
  utc_start_byte = ipcio_get_soclock_byte(pwcm->data_block);

  // multilog(pwcm->log, LOG_INFO, "utc_start_byte = %"PRIu64"\n",utc_start_byte);

  /* The actual byte this command corresponds to */
  command_start_byte = utc_start_byte + pwcm->command.byte_count;

  // multilog (pwcm->log, LOG_INFO, "minimum_record_start=%"PRIu64"\n",
  //           minimum_record_start);

  /* If the command is scheduled to occur earlier than is possible, then
   * we must delay that command to the earliest time */
  if (command_start_byte < minimum_record_start) {
    multilog (pwcm->log, LOG_ERR, "Requested start byte=%"PRIu64
              " reset to minimum=%"PRIu64"\n", command_start_byte, 
              minimum_record_start);

    /* This is the byte the command corresponds to */
    command_start_byte = minimum_record_start;

    /* This is the offset of the command from the utc_start (OBS_OFFSET) */
    pwcm->command.byte_count = minimum_record_start - utc_start_byte;
  }

  multilog (pwcm->log, LOG_INFO, "REC_START\n");
  multilog (pwcm->log, LOG_INFO, "pwcm->command.utc = %d\n",pwcm->command.utc);
  multilog (pwcm->log, LOG_INFO, "pwcm->pwc->utc_start = %d\n",pwcm->pwc->utc_start);

  /* Special case for rec_stop/rec_start toggling */
  header = ipcbuf_get_next_write (pwcm->header_block);
  if (header != pwcm->header) {
    /* the case if there is more than one sub-block in Header Block */
    memcpy (header, pwcm->header, pwcm->header_size);
    pwcm->header = header;
  }

  /* a UTC_START may be different for multiple PWC's, but the REC_START will 
   * be uniform, so on a REC_START we will change the UTC_START in the outgoing 
   * header to be UTC_START + OBS_OFFSET, to ensure uniformity across PWCs */

  time_t utc = pwcm->command.utc;
  int buffer_size = 64;
  char buffer[buffer_size];
  strftime (buffer, buffer_size, DADA_TIMESTR, gmtime (&utc));

  multilog (pwcm->log, LOG_INFO, "dada_pwc_main_record_start: UTC_START reset to REC_START = %s\n", buffer);

  if (ascii_header_set (pwcm->header, "UTC_START", "%s", buffer) < 0) {
    multilog (pwcm->log, LOG_ERR, "fail ascii_header_set UTC_START\n");
    return DADA_ERROR_HARD;
  }

  multilog (pwcm->log, LOG_INFO, "dada_pwc_main_record_start: OBS_OFFSET = 0\n");
  if (ascii_header_set (pwcm->header, "OBS_OFFSET", "%"PRIu64, 0) < 0) {
    multilog (pwcm->log, LOG_ERR, "fail ascii_header_set OBS_OFFSET\n");
    return DADA_ERROR_HARD;
  }

  multilog (pwcm->log, LOG_INFO,"command_start_byte = %"PRIu64", command.byte_"
            "count = %"PRIu64"\n",command_start_byte,pwcm->command.byte_count);

  /* start valid data on the Data Block at the requested byte */
  if (ipcio_start (pwcm->data_block, command_start_byte) < 0)  {
    multilog (pwcm->log, LOG_ERR, "Could not start data block"
              " at %"PRIu64"\n", command_start_byte);
    return DADA_ERROR_HARD;
  }

  /* If the header has not yet been made valid */
  if (!(pwcm->header_valid)) {

    if (pwcm->header_valid_function) 
      pwcm->header_valid = pwcm->header_valid_function(pwcm);
    else
      pwcm->header_valid = 1;

    if (pwcm->header_valid) {
#ifdef _DEBUG
        multilog(pwcm->log, LOG_INFO, "dada_pwc_main_record_start: marking header filled\n");
#endif
      if (ipcbuf_mark_filled (pwcm->header_block, pwcm->header_size) < 0)  {
        multilog (pwcm->log, LOG_ERR, "Could not mark filled header\n");
        return DADA_ERROR_HARD;
      }
    } else {
      multilog (pwcm->log, LOG_ERR, "Cannot transit from clocking to recoding "
                "if when header is invalid\n");;
      return DADA_ERROR_HARD;
    }
  }

  return 0;

}

/*! The clocking and recording states of the DADA PWC main loop */
int dada_pwc_main_transfer_data (dada_pwc_main_t* pwcm)
{
  /* total number of bytes written to the Data Block */
  uint64_t total_bytes_written = 0;

  /* the byte at which the state will change */
  uint64_t transit_byte = pwcm->command.byte_count;

  /* the number of bytes to write to the Data Block */
  uint64_t bytes_to_write = pwcm->command.byte_count;

  /* the number of bytes to be copied from buffer */
  uint64_t buf_bytes = 0;

  /* the number of bytes written to the Data Block */
  int64_t bytes_written = 0;

  /* pointer to the data buffer */
  char* buffer = 0;

  /* size of the data buffer */
  int64_t buffer_size = 0;

  /* error state due to a 0 byte buffer function return */
  uint64_t data_transfer_error_state = 0;

  char* command_string = 0;

  /* flags and buffers for setting of UTC_START */
  uint64_t utc_start_set = 0;
  int utc_size = 1024;
  char utc_buffer[utc_size];

  int64_t first_byte_of_xfer = 1;

  /* if the pwcm supports xfers, get the first byte of data for the first xfer */
  if (pwcm->new_xfer_function) {
    total_bytes_written = pwcm->new_xfer_function(pwcm);
    if (pwcm->verbose)
      multilog (pwcm->log, LOG_INFO, "transfer_data: first byte expected on %"PRIu64"\n", total_bytes_written);
  }

  /* Check if the UTC_START is valid */
  if (ascii_header_get (pwcm->header, "UTC_START", "%s", utc_buffer) < 0) {
    multilog (pwcm->log, LOG_ERR, "Could not read UTC_START from header\n");
    return DADA_ERROR_HARD;
  }

  if (strcmp(utc_buffer,"UNKNOWN") != 0) 
    utc_start_set = 1;

#ifdef _DEBUG
  fprintf (stderr, "transfer_data: enter main loop\n");
#endif

  while ((!dada_pwc_quit (pwcm->pwc)) && (data_transfer_error_state <= 1)) {

#ifdef _DEBUG
    fprintf (stderr, "transfer_data: check for command\n");
#endif

    /* check to see if a new command has been registered */
    if (dada_pwc_command_check (pwcm->pwc))  {

#ifdef _DEBUG
      fprintf (stderr, "transfer_data: get command\n");
#endif

      pwcm->command = dada_pwc_command_get (pwcm->pwc);

      /* special case for set_utc_start */
      if (pwcm->command.code == dada_pwc_set_utc_start) {

        assert(pwcm->command.utc > 0);

        if (utc_start_set) { 
          multilog (pwcm->log, LOG_WARNING, "WARNING, UTC_START was already "
                    "set. Ignoring set_utc_start command\n");
        } else {

          strftime (utc_buffer, utc_size, DADA_TIMESTR, 
                    (struct tm*) gmtime(&(pwcm->command.utc)));

#ifdef _DEBUG
          fprintf (stderr,"transfer_data: set UTC_START in header to : %s\n",
                   utc_buffer);
#endif
          multilog (pwcm->log, LOG_INFO,"UTC_START = %s\n",utc_buffer);

          // write UTC_START to the header
          if (ascii_header_set (pwcm->header,"UTC_START","%s",utc_buffer) < 0) {
            multilog (pwcm->log,LOG_ERR,"failed ascii_header_set UTC_START\n");
            return DADA_ERROR_HARD;
          }

          /* We only marked the header block as filled if we are already 
           * recording this can only happen via a "start" command. if we
           * are clocking, then the record_start command will mark the 
           * header block as filled */

#ifdef _DEBUG
          fprintf (stderr,"transfer_data: header block filled\n");
#endif
          if (pwcm->pwc->state == dada_pwc_recording) {

            if (pwcm->header_valid_function) 
              pwcm->header_valid = pwcm->header_valid_function(pwcm);
            else
              pwcm->header_valid = 1;

            if (pwcm->header_valid) {
#ifdef _DEBUG
              multilog (pwcm->log, LOG_INFO, "transfer_data: marking header valid\n");
#endif

              if (ipcbuf_mark_filled (pwcm->header_block,pwcm->header_size) < 0) {
                multilog (pwcm->log, LOG_ERR, "Could not mark filled header\n");
                return DADA_ERROR_HARD;
              }
            }
          }
      
          /* Set to true */
          utc_start_set = 1;
        }

        /* Signal the command control interface */
        pthread_mutex_lock (&(pwcm->pwc->mutex));
        pthread_cond_signal (&(pwcm->pwc->cond));
        pthread_mutex_unlock (&(pwcm->pwc->mutex));

      } else {

        if (pwcm->command.code == dada_pwc_record_stop)
          command_string = "recording->clocking";
        else if (pwcm->command.code == dada_pwc_record_start)
          command_string = "clocking->recording";
        else if (pwcm->command.code == dada_pwc_stop)
          command_string = "stopping";
        else {
          multilog (pwcm->log, LOG_ERR, "transfer_data: internal error = "
                    "unexpected command code %d\n", pwcm->command.code);
          return DADA_ERROR_HARD;
        }

        if (pwcm->verbose)
          multilog (pwcm->log, LOG_INFO, "transfer_data: %s byte count = %"PRIu64" bytes\n",
                    command_string, pwcm->command.byte_count);

        if (pwcm->command.byte_count > total_bytes_written) {
          /* command is dated, happens in the future... */
          transit_byte = pwcm->command.byte_count;
          bytes_to_write = pwcm->command.byte_count - total_bytes_written;

          multilog (pwcm->log, LOG_INFO, "%s in %"PRIu64" bytes\n", 
                    command_string, bytes_to_write);

        } else {
          /* command is immediate */
          multilog (pwcm->log, LOG_INFO, "%s immediately\n", command_string);
          if (total_bytes_written == 0)
          {
            multilog (pwcm->log, LOG_INFO, "Received 0 bytes, setting to 1\n");
            total_bytes_written = 1;
          }
          transit_byte = total_bytes_written;
          bytes_to_write = 0;

          if (pwcm->command.byte_count &&
              pwcm->command.byte_count < total_bytes_written &&
              pwcm->verbose)
            multilog (pwcm->log, LOG_INFO,
                      "requested transit byte=%"PRIu64" passed\n",
                      pwcm->command.byte_count);
        }
        
      }

      if (pwcm->verbose) {
        if (pwcm->pwc->state == dada_pwc_recording)
          multilog (pwcm->log, LOG_INFO, "recording\n");
        else if (pwcm->pwc->state == dada_pwc_clocking)
          multilog (pwcm->log, LOG_INFO, "clocking\n");

        if (transit_byte) {

          if (pwcm->command.code ==  dada_pwc_record_stop)
            multilog (pwcm->log, LOG_INFO, "record stop in %"PRIu64" bytes\n",
                      bytes_to_write);
          else if (pwcm->command.code ==  dada_pwc_record_start)
            multilog (pwcm->log, LOG_INFO, "record start in %"PRIu64" bytes\n",
                      bytes_to_write);
          else if (pwcm->command.code ==  dada_pwc_record_stop)
            multilog (pwcm->log, LOG_INFO, "stop in %"PRIu64" bytes\n",
                      bytes_to_write);
          else
            ;

        }
      }
    }

    if (!transit_byte || bytes_to_write) {

#ifdef _DEBUG
      fprintf (stderr, "transfer_data: call buffer function\n");
#endif

      /* get the next data buffer */
      buffer = pwcm->buffer_function (pwcm, &buffer_size);

      /* If the buffer_function had an error */
      if (buffer_size < 0) 
        return buffer_size;

      if ((data_transfer_error_state) && (buffer_size != 0)) {
        data_transfer_error_state = 0;
        if (total_bytes_written) 
          multilog (pwcm->log, LOG_WARNING, "pwc buffer_function "
                    "recovered from error state\n");
      }


      if (buffer_size == 0) {

        // if the pwc supports multiple xfers within the 1 observation
        if (pwcm->xfer_pending_function) 
        {
          if (pwcm->verbose)       
            multilog (pwcm->log, LOG_INFO, "transfer_data: buffer_size=0, checking xfer status\n");
          first_byte_of_xfer = dada_pwc_main_process_xfer (pwcm, 0, 0);
          if (first_byte_of_xfer == -2) 
          {
            multilog (pwcm->log, LOG_ERR, "transfer_data: process_xfer failed\n");
            return DADA_ERROR_HARD;
          } 
          else if (first_byte_of_xfer == -1) 
          {
            if (pwcm->verbose)
              multilog (pwcm->log, LOG_INFO, "transfer_data: process_xfer indicates end of observation\n");
            return 0;
          }
          else if (first_byte_of_xfer == 0) 
          {
            if (pwcm->verbose)
              multilog (pwcm->log, LOG_INFO, "transfer_data: process_xfer indicates waiting for SOD on xfer\n");
          }
          else
          {
            if (pwcm->verbose)
              multilog (pwcm->log, LOG_INFO, "transfer_data: process_xfer indicates next byte is %"PRIi64"\n", first_byte_of_xfer);
            total_bytes_written = (uint64_t) first_byte_of_xfer;
          }

        } else {

          /* If we are experiencing an read error that we may be able to 
           * recover from, run the error_function, if it returns:
           * 0, we have recovered from error 
           * 1, we keep trying to recover
           * 2, we will not be able to recover from error, exit gracefully */
          if (pwcm->error_function) {
            /* If defined */
            data_transfer_error_state = pwcm->error_function(pwcm);
          } else {
            /* give up */        
            data_transfer_error_state = 2;
          }

          if (data_transfer_error_state == 2) {
            multilog (pwcm->log, LOG_ERR, "pwc buffer_function returned 0 bytes."
                                          " Stopping\n");

            /* Ensure that the ipcio_write function is called with 1 bytes
             * so that a reader may acknwowledge the SoD and subsequent EoD */
            bytes_written = ipcio_write (pwcm->data_block, buffer, 1);

            /* If we are clocking, then its not the end of the world, signal
             * a soft error */
            return DADA_ERROR_HARD;
          }

          if ((data_transfer_error_state == 1) && total_bytes_written) {
            multilog (pwcm->log, LOG_INFO, "PWC buffer function "
                      "returned 0 bytes, trying to continue\n");
          }
        }
      }
      
      if (!buffer) {
        multilog (pwcm->log, LOG_ERR, "buffer function error\n");
        return DADA_ERROR_HARD;   
        //TODO CHECK WHAT buffer_function actually returns...
      }

      /* Check if we need to mark the header as valid after this - not possible
       * if we are clocking */
      if ((!pwcm->header_valid) && (pwcm->pwc->state == dada_pwc_recording)) {
        pwcm->header_valid = pwcm->header_valid_function(pwcm);
      
        /* If the header is NOW valid, flag the header as filled */  
        if (pwcm->header_valid) {
#ifdef _DEBUG
            multilog (pwcm->log, LOG_INFO,"transfer_data: marking header filled\n");
#endif
          if (ipcbuf_mark_filled (pwcm->header_block,pwcm->header_size) < 0) {
            multilog (pwcm->log, LOG_ERR, "Could not mark filled header\n");
            return DADA_ERROR_HARD;
          }
        }
      }

      /* If the transit_byte is set, do not write more than the requested
         amount of data to the Data Block */
      buf_bytes = buffer_size;
      
      if (transit_byte && buf_bytes > bytes_to_write)
        buf_bytes = bytes_to_write;
     
      /* write the bytes to the Data Block */
      if (pwcm->data_block) {

#ifdef _DEBUG
        fprintf (stderr, "transfer_data: write to data block buffer=%p bytes=%"
                         PRIu64"\n", buffer, buf_bytes);
#endif

        /* If we run out of empty buffers and don't yet have a UTC_START, 
         * fail */
        if ((!utc_start_set) && (ipcio_space_left (pwcm->data_block) < buf_bytes)) {
          multilog (pwcm->log, LOG_ERR, "Data block full and UTC_START not "
                    "set.\n");
          return DADA_ERROR_FATAL;
        }
        
        bytes_written = ipcio_write (pwcm->data_block, buffer, buf_bytes);

#ifdef _DEBUG
        fprintf (stderr, "transfer_data: return from write\n");
#endif

        if (bytes_written < 0 || bytes_written < buf_bytes) {
          multilog (pwcm->log, LOG_ERR, "Cannot write %"PRIu64
                    " bytes to Data Block\n", buf_bytes);
          return DADA_ERROR_FATAL;
        }

      }

      total_bytes_written += buf_bytes;

      if (bytes_to_write)
        bytes_to_write -= buf_bytes;

      if (pwcm->verbose)
        multilog (pwcm->log, LOG_INFO, "Written %"PRIu64" bytes\n",
                  total_bytes_written);

    }

    if (transit_byte && pwcm->verbose)
      multilog (pwcm->log, LOG_INFO, "transit=%"PRIu64" total=%"PRIu64"\n",
                transit_byte, total_bytes_written);

    // total_bytes_written can jump ahead of a transit byte (with XFERS)
    if (transit_byte && (transit_byte <= total_bytes_written)) {

#ifdef _DEBUG
      fprintf (stderr, "transfer_data: transit state\n");
#endif

      /* The transit_byte has been reached, it is now time to change state */
      /* reset the transit_byte */ 
      transit_byte = 0;

      if (pwcm->command.code ==  dada_pwc_record_stop) {

        multilog (pwcm->log, LOG_INFO, "record stop\n");

        /* stop valid data on the Data Block at the last byte written */
        if (pwcm->data_block && ipcio_stop (pwcm->data_block) < 0){
          multilog (pwcm->log, LOG_ERR, "Could not stop data block\n");
          return DADA_ERROR_FATAL;
        }
        
        dada_pwc_set_state (pwcm->pwc, dada_pwc_clocking, 0);
        
      }

      else if (pwcm->command.code ==  dada_pwc_record_start) {

        multilog (pwcm->log, LOG_INFO, "record start\n");

        if (dada_pwc_main_record_start (pwcm) < 0)
          return DADA_ERROR_HARD;
        
        dada_pwc_set_state (pwcm->pwc, dada_pwc_recording, 0);

      } 

      else if (pwcm->command.code == dada_pwc_stop) {

        if (pwcm->xfer_pending_function) 
        {
          // CASPSR CHANGE
          // If we haven't stopped already due to a packet timeout, we must
          // write the additional 1 byte transfer and set END_OF_OBS=-1
          dada_pwc_main_process_xfer(pwcm, 2, 0);
        }

#ifdef _DEBUG
        multilog (pwcm->log, LOG_INFO, "stopping... entering idle state\n");
#endif
        return 0;
      }
      
      else if (pwcm->command.code ==  dada_pwc_set_utc_start) {

        if (total_bytes_written) {
          multilog (pwcm->log, LOG_ERR, "Error. unexpected set_utc_start\n");
          return DADA_ERROR_HARD;
        }

      } else if (pwcm->command.code == dada_pwc_clock) {

        if (total_bytes_written) {
          multilog (pwcm->log, LOG_ERR, "Error. unexpected clock command\n");
          return DADA_ERROR_HARD;
        }

      } else if (pwcm->command.code == dada_pwc_start) {

        if (total_bytes_written) {
          multilog (pwcm->log, LOG_ERR, "Error. unexpected start command\n");
          return DADA_ERROR_HARD;
        }
        
      } else  {
        /* enter the idle state */
        multilog (pwcm->log, LOG_ERR, "Error. unpected command: %d\n",
                  pwcm->command.code);
        return DADA_ERROR_HARD;

      }

      /* When changing between clocking and recording states, there
         may remain unwritten data in the buffer.  Write this data to
         the Data Block */

      /* offset the buffer pointer by the amount already written */
      buffer += buf_bytes;
      /* set the number of remaining samples */
      buf_bytes = buffer_size - buf_bytes;

      if (buf_bytes && pwcm->data_block) {

        // If we run out of empty buffers and don't yet have a UTC_START, fail
        if (ipcio_space_left(pwcm->data_block) < buf_bytes) 
        {
          if (!utc_start_set) {
            multilog (pwcm->log, LOG_ERR, "Data block full and UTC_START not set.\n");
            return DADA_ERROR_FATAL;
          }
          else
            multilog(pwcm->log, LOG_WARNING, "Data block full, waiting for space\n");
        }

        // write the bytes to the Data Block
        if (ipcio_write (pwcm->data_block, buffer, buf_bytes) < buf_bytes) {

          multilog (pwcm->log, LOG_ERR, "Cannot write %"PRIu64
                    " bytes to Data Block\n", buf_bytes);
          return DADA_ERROR_FATAL;

        }

        total_bytes_written += buf_bytes;

      }

    }

  }

  return 0;

}

/*! The clocking and recording states for zero copy of the DADA PWC main loop */
int dada_pwc_main_transfer_data_block (dada_pwc_main_t* pwcm)
{
  /* total number of bytes written to the Data Block */
  uint64_t total_bytes_written = 0;

  /* the byte at which the state will change */
  uint64_t transit_byte = pwcm->command.byte_count;

  /* the number of bytes to write to the Data Block */
  uint64_t bytes_to_write = pwcm->command.byte_count;

  /* the number of bytes to be copied from buffer */
  uint64_t buf_bytes = 0;

  /* the number of bytes written to the Data Block */
  int64_t bytes_written = 0;

  /* the number of bytes written this xfer */
  uint64_t bytes_written_this_xfer = 0;

  /* pointer to the data buffer */
  char* buffer = 0;

  /* error state due to a 0 byte buffer function return */
  uint64_t data_transfer_error_state = 0;

  /* block id of the current block */
  uint64_t block_id = 0;

  /* size of the buffer in the data block */
  uint64_t block_size = ipcbuf_get_bufsz ((ipcbuf_t*) pwcm->data_block);

  char* command_string = 0;

  /* flags and buffers for setting of UTC_START */
  uint64_t utc_start_set = 0;
  int utc_size = 1024;
  char utc_buffer[utc_size];

  int64_t first_byte_of_xfer = 1;

  /* if the pwcm supports xfers, get the first byte of data for the first xfer */
  if (pwcm->new_xfer_function) {
    total_bytes_written = pwcm->new_xfer_function(pwcm);
    if (pwcm->verbose)
      multilog (pwcm->log, LOG_INFO, "first byte expected on %"PRIu64"\n", total_bytes_written);
  }

  /* Check if the UTC_START is valid */
  if (ascii_header_get (pwcm->header, "UTC_START", "%s", utc_buffer) < 0) {
    multilog (pwcm->log, LOG_ERR, "Could not read UTC_START from header\n");
    return DADA_ERROR_HARD;
  }

  if (strcmp(utc_buffer,"UNKNOWN") != 0) 
    utc_start_set = 1;

#ifdef _DEBUG
  fprintf (stderr, "transfer_data_block: enter main loop\n");
#endif

  while ((!dada_pwc_quit (pwcm->pwc)) && (data_transfer_error_state <= 1)) {

#ifdef _DEBUG
    fprintf (stderr, "transfer_data_block: check for command\n");
#endif

    /* check to see if a new command has been registered */
    if (dada_pwc_command_check (pwcm->pwc))  {

#ifdef _DEBUG
      fprintf (stderr, "transfer_data_block: get command\n");
#endif

      pwcm->command = dada_pwc_command_get (pwcm->pwc);

#ifdef _DEBUG
      multilog (pwcm->log, LOG_INFO, "pwcm->command.utc = %d\n",pwcm->command.utc);
      multilog (pwcm->log, LOG_INFO, "pwcm->pwc->utc_start = %d\n",pwcm->pwc->utc_start);
#endif

      /* special case for set_utc_start */
      if (pwcm->command.code == dada_pwc_set_utc_start) {
#ifdef _DEBUG
        multilog (pwcm->log, LOG_INFO, "pwcm->command.code = dada_pwc_set_utc_start\n");
        multilog (pwcm->log, LOG_INFO, "pwcm->command.utc = %d\n", pwcm->command.utc);
        multilog (pwcm->log, LOG_INFO, "pwcm->command.byte_count = %"PRIu64"\n", pwcm->command.byte_count);
#endif

        assert(pwcm->command.utc > 0);

        if (utc_start_set) { 
          multilog (pwcm->log, LOG_WARNING, "WARNING, UTC_START was already "
                    "set. Ignoring set_utc_start command\n");
        } else {

          strftime (utc_buffer, utc_size, DADA_TIMESTR, 
                    (struct tm*) gmtime(&(pwcm->command.utc)));

#ifdef _DEBUG
          fprintf (stderr,"transfer_data_block: set UTC_START in "
                          "header to : %s\n",utc_buffer);
#endif
          multilog (pwcm->log, LOG_INFO,"UTC_START = %s\n",utc_buffer);

          // write UTC_START to the header
          if (ascii_header_set (pwcm->header,"UTC_START","%s",utc_buffer) < 0) {
            multilog (pwcm->log,LOG_ERR,"failed ascii_header_set UTC_START\n");
            return DADA_ERROR_HARD;
          }

          /* We only marked the header block as filled if we are already 
           * recording this can only happen via a "start" command. if we
           * are clocking, then the record_start command will mark the 
           * header block as filled */

#ifdef _DEBUG
          fprintf (stderr,"transfer_data_block: header block filled\n");
#endif
          if (pwcm->pwc->state == dada_pwc_recording) {

            if (pwcm->header_valid_function) 
              pwcm->header_valid = pwcm->header_valid_function(pwcm);
            else
              pwcm->header_valid = 1;

            if (pwcm->header_valid) {
#ifdef _DEBUG
              multilog (pwcm->log, LOG_INFO, "transfer_data_block: marking header valid\n");
#endif

              if (ipcbuf_mark_filled (pwcm->header_block,pwcm->header_size) < 0) {
                multilog (pwcm->log, LOG_ERR, "Could not mark filled header\n");
                return DADA_ERROR_HARD;
              }
            }
          }
      
          /* Set to true */
          utc_start_set = 1;
        }

        /* Signal the command control interface */
        pthread_mutex_lock (&(pwcm->pwc->mutex));
        pthread_cond_signal (&(pwcm->pwc->cond));
        pthread_mutex_unlock (&(pwcm->pwc->mutex));

      } else {

        if (pwcm->command.code == dada_pwc_record_stop)
          command_string = "recording->clocking";
        else if (pwcm->command.code == dada_pwc_record_start)
          command_string = "clocking->recording";
        else if (pwcm->command.code == dada_pwc_stop)
          command_string = "stopping";
        else {
          multilog (pwcm->log, LOG_ERR,
                    "dada_pwc_main_transfer data internal error = "
                    "unexpected command code %d\n", pwcm->command.code);
          return DADA_ERROR_HARD;
        }

        if (pwcm->verbose)
          multilog (pwcm->log, LOG_INFO, "%s byte count = %"PRIu64" bytes\n",
                    command_string, pwcm->command.byte_count);

        if (pwcm->command.byte_count > total_bytes_written) {
          /* command is dated, happens in the future... */
          transit_byte = pwcm->command.byte_count;
          bytes_to_write = pwcm->command.byte_count - total_bytes_written;

          multilog (pwcm->log, LOG_INFO, "%s in %"PRIu64" bytes\n", 
                    command_string, bytes_to_write);

        } else {
          /* command is immediate */
          multilog (pwcm->log, LOG_INFO, "%s immediately\n", command_string);
          transit_byte = total_bytes_written;
          bytes_to_write = 0;

          if (pwcm->command.byte_count &&
              pwcm->command.byte_count < total_bytes_written)
            multilog (pwcm->log, LOG_NOTICE,
                      "requested transit byte=%"PRIu64" passed\n",
                      pwcm->command.byte_count);
        }
        
      }

      if (pwcm->verbose) {
        if (pwcm->pwc->state == dada_pwc_recording)
          multilog (pwcm->log, LOG_INFO, "recording\n");
        else if (pwcm->pwc->state == dada_pwc_clocking)
          multilog (pwcm->log, LOG_INFO, "clocking\n");

        if (transit_byte) {

          if (pwcm->command.code ==  dada_pwc_record_stop)
            multilog (pwcm->log, LOG_INFO, "record stop in %"PRIu64" bytes\n",
                      bytes_to_write);
          else if (pwcm->command.code ==  dada_pwc_record_start)
            multilog (pwcm->log, LOG_INFO, "record start in %"PRIu64" bytes\n",
                      bytes_to_write);
          else if (pwcm->command.code ==  dada_pwc_record_stop)
            multilog (pwcm->log, LOG_INFO, "stop in %"PRIu64" bytes\n",
                      bytes_to_write);
          else
            ;

        }
      }
    }

    buf_bytes = 0;

    // if we dont have a specific byte to stop on or we have some bytes to write
    if (!transit_byte || bytes_to_write) 
    {

      // get the pointer to the next empty block and its ID
      if (pwcm->verbose)
        multilog (pwcm->log, LOG_INFO, "transfer_data_block: ipcio_open_block_write()\n");
      buffer = ipcio_open_block_write (pwcm->data_block, &block_id);
      if (!buffer) 
      {
        multilog (pwcm->log, LOG_ERR, "ipcio_open_block_write error %s\n", strerror(errno));
        return DADA_ERROR_FATAL;
      }

      if (pwcm->verbose)
        multilog (pwcm->log, LOG_INFO, "transfer_data_block: opened block id=%"PRIu64"\n", block_id);

      // call the block function to fill the buffer with data
      if (pwcm->verbose)
        multilog (pwcm->log, LOG_INFO, "transfer_data_block: block_function(%"PRIu64", %"PRIu64")\n", block_size, block_id);
      bytes_written = pwcm->block_function (pwcm, buffer, block_size, block_id);

      // check the result of the block function
      if (bytes_written < 0)
      {
        multilog (pwcm->log, LOG_ERR, "transfer_data_block: block_function failed\n");
        ipcio_close_block_write (pwcm->data_block, 0);
        return DADA_ERROR_FATAL;
      }

      // check that we did not write more than the block size
      if (bytes_written > block_size)
      {
        multilog (pwcm->log, LOG_ERR, "transfer_data_block: block_function wrote %"PRIu64
                                      " > block_size %"PRIu64"\n", bytes_written, block_size);
        ipcio_close_block_write (pwcm->data_block, block_size);
        return DADA_ERROR_FATAL;
      }

      // If the transit_byte is set, do not write more than the requested 
      // amount of data to the Data Block
      buf_bytes = bytes_written;
      if (transit_byte && bytes_written > bytes_to_write)
        buf_bytes = bytes_to_write;

      if (bytes_written == 0)
      {
        multilog (pwcm->log, LOG_INFO, "transfer_data_block: block_function returned 0 bytes, "
                                       "buf_bytes=%"PRIu64" bytes_written_this_xfer=%"PRIu64"\n", 
                                       buf_bytes, bytes_written_this_xfer);
      }

      // update counters
      total_bytes_written += buf_bytes;
      if (bytes_to_write)
        bytes_to_write -= buf_bytes;

      if (pwcm->verbose)
        multilog (pwcm->log, LOG_INFO, "Written %"PRIu64" bytes\n", total_bytes_written);

      // if we do not support multiple XFERS within the one observation, just close the data block
      if (!pwcm->xfer_pending_function)
      {
        if (pwcm->verbose)
          multilog (pwcm->log, LOG_INFO, "transfer_data_block: ipcio_close_block_write(%"PRIu64")\n", (uint64_t) buf_bytes);
        if (ipcio_close_block_write (pwcm->data_block, (uint64_t) buf_bytes) < 0) {
          multilog (pwcm->log, LOG_ERR, "transfer_data_block: ipcio_close_block_write error\n");
          return DADA_ERROR_FATAL;
        }
      }
      else
      {
        bytes_written_this_xfer += buf_bytes;

        // if we support XFERS, have transit byte and its equal to total bytes written
        // run the block function once more to finalize the transfer process
        if (transit_byte && (transit_byte == total_bytes_written))
        {
          if (pwcm->verbose)
            multilog (pwcm->log, LOG_INFO, "transfer_data_block: transit_byte == total_bytes_written\n");
          void * ending_buffer = 0;
          uint64_t ending_block_size = 0;
          pwcm->block_function (pwcm, ending_buffer, ending_block_size, block_id);
        }

        // check on the status of the XFER
        first_byte_of_xfer = dada_pwc_main_process_xfer (pwcm, buf_bytes, bytes_written_this_xfer);
  
        if (first_byte_of_xfer == 0)
        {
          if (pwcm->verbose)
            multilog (pwcm->log, LOG_INFO, "transfer_data_block: process_xfer reports xfer continuing\n");
        }
        else if (first_byte_of_xfer < 0)
        {
          multilog (pwcm->log, LOG_ERR, "dada_pwc_main_process_xfer failed\n");
          return DADA_ERROR_HARD;
        }
        else
        {
          if (pwcm->verbose)
            multilog (pwcm->log, LOG_INFO, "transfer_data_block: new XFER, next byte=%"PRIi64"\n", first_byte_of_xfer);
          total_bytes_written = (uint64_t) first_byte_of_xfer;
          bytes_written_this_xfer = 0;
        }
      }

      // Check if we need to mark the header as valid after this - not possible if we are clocking
      if ((!pwcm->header_valid) && (pwcm->pwc->state == dada_pwc_recording)) {
        pwcm->header_valid = pwcm->header_valid_function(pwcm);
      
        // If the header is NOW valid, flag the header as filled
        if (pwcm->header_valid) {
          multilog (pwcm->log, LOG_INFO,"transfer_data_block: marking header filled\n");
          if (ipcbuf_mark_filled (pwcm->header_block,pwcm->header_size) < 0) {
            multilog (pwcm->log, LOG_ERR, "Could not mark filled header\n");
            return DADA_ERROR_HARD;
          }
        }
      }

    } else {
      if (pwcm->verbose)
        multilog (pwcm->log, LOG_INFO, "transfer_data_block: transit byte was set, block_function ignored\n");
    }

    if (pwcm->verbose)
      multilog (pwcm->log, LOG_INFO, "transit=%"PRIu64" total=%"PRIu64"\n",
                transit_byte, total_bytes_written);

    // total_bytes_written can jump ahead of a transit byte
    if (transit_byte && (transit_byte <= total_bytes_written)) {

#ifdef _DEBUG
      fprintf (stderr, "transfer_data_block: transit state\n");
#endif

      /* The transit_byte has been reached, it is now time to change state */
      /* reset the transit_byte */ 
      transit_byte = 0;

      if (pwcm->command.code ==  dada_pwc_record_stop) {

        multilog (pwcm->log, LOG_INFO, "record stop\n");

        /* stop valid data on the Data Block at the last byte written */
        if (pwcm->data_block && ipcio_stop (pwcm->data_block) < 0){
          multilog (pwcm->log, LOG_ERR, "Could not stop data block\n");
          return DADA_ERROR_FATAL;
        }
        
        dada_pwc_set_state (pwcm->pwc, dada_pwc_clocking, 0);
        
      }

      else if (pwcm->command.code ==  dada_pwc_record_start) {

        multilog (pwcm->log, LOG_INFO, "record start\n");

        if (dada_pwc_main_record_start (pwcm) < 0)
          return DADA_ERROR_HARD;
        
        dada_pwc_set_state (pwcm->pwc, dada_pwc_recording, 0);

      } 

      else if (pwcm->command.code == dada_pwc_stop) {

        if (pwcm->xfer_pending_function)
        {
          if (pwcm->verbose)
            multilog (pwcm->log, LOG_INFO, "stopping, but calling process_xfer\n");
          int64_t result = dada_pwc_main_process_xfer (pwcm, buf_bytes, bytes_written_this_xfer);
          multilog (pwcm->log, LOG_INFO, "process_xfer returned %"PRIi64"\n", result);
        }

        multilog (pwcm->log, LOG_INFO, "stopping... entering idle state\n");
        return 0;
      }
      
      else if (pwcm->command.code ==  dada_pwc_set_utc_start) {

        if (total_bytes_written) {
          multilog (pwcm->log, LOG_ERR, "Error. unexpected set_utc_start\n");
          return DADA_ERROR_HARD;
        }

      } else if (pwcm->command.code == dada_pwc_clock) {

        if (total_bytes_written) {
          multilog (pwcm->log, LOG_ERR, "Error. unexpected clock command\n");
          return DADA_ERROR_HARD;
        }

      } else if (pwcm->command.code == dada_pwc_start) {

        if (total_bytes_written) {
          multilog (pwcm->log, LOG_ERR, "Error. unexpected start command\n");
          return DADA_ERROR_HARD;
        }
        
      } else  {
        /* enter the idle state */
        multilog (pwcm->log, LOG_ERR, "Error. unpected command: %d\n",
                  pwcm->command.code);
        return DADA_ERROR_HARD;

      }
    }
  }
  return 0;
}


/*! The transit to idle/prepared state */
int dada_pwc_main_stop_transfer (dada_pwc_main_t* pwcm)
{

  // Reset the header so that it is not valid anymore
  pwcm->header_valid = 0;

  if (pwcm->verbose)
    multilog (pwcm->log, LOG_INFO, "stop_transfer: calling stop_function()\n");
  if (pwcm->stop_function (pwcm) < 0)  {
    multilog (pwcm->log, LOG_ERR, "dada_pwc_main_stop_transfer"
              " stop function returned error code\n");
    return DADA_ERROR_HARD;
  }

  if (pwcm->verbose)
    multilog (pwcm->log, LOG_INFO, "stop_transfer: closing data_block()\n");
  if (pwcm->data_block && ipcio_close (pwcm->data_block) < 0)  {
    multilog (pwcm->log, LOG_ERR, "Could not close Data Block\n");
    return DADA_ERROR_FATAL;
  }

  if (pwcm->verbose)
    multilog (pwcm->log, LOG_INFO, "stop_transfer: setting state\n");

  if (pwcm->pwc->state != dada_pwc_soft_error &&
      pwcm->pwc->state != dada_pwc_hard_error &&
      pwcm->pwc->state != dada_pwc_fatal_error)  
    dada_pwc_set_state (pwcm->pwc, dada_pwc_idle, 0);

  return 0;
}

void dada_pwc_main_process_error (dada_pwc_main_t* pwcm, int rval) 
{

  int new_state = pwcm->pwc->state;

  switch (rval) 
  {
    case DADA_ERROR_SOFT:
      if ( (pwcm->pwc->state != dada_pwc_hard_error) && 
           (pwcm->pwc->state != dada_pwc_fatal_error) )
        new_state = dada_pwc_soft_error;
      break;

    case DADA_ERROR_HARD:
      if (pwcm->pwc->state != dada_pwc_fatal_error)
        new_state = dada_pwc_hard_error;
      break;

    case DADA_ERROR_FATAL:
      new_state = dada_pwc_fatal_error;
      break;

    default:
      multilog (pwcm->log, LOG_ERR, "Unknown error state: %d\n",rval);
      new_state = dada_pwc_fatal_error;
  }

  multilog(pwcm->log, LOG_WARNING, "PWC entering error state: %s\n",
           dada_pwc_state_to_string(new_state));

  if (dada_pwc_set_state (pwcm->pwc, new_state, 0) < 0) 
    multilog(pwcm->log, LOG_ERR, "Failed to change state from %s to %s\n",
             dada_pwc_state_to_string(pwcm->pwc->state),
             dada_pwc_state_to_string(new_state));


}

/*
 * dada_pwc_main_process_xfer
 *
 * check the current xfer status of the pwc. If required, move to a new transfer.
 * Return values:
 *   -2 error
 *   -1 at end of observation
 *    0 at beginning of observation and waiting for Start of Data
 *   >0 first byte of the next xfer
 *
 */
int64_t dada_pwc_main_process_xfer (dada_pwc_main_t* pwcm, uint64_t buf_bytes, uint64_t bytes_this_xfer)
{

  if (pwcm->verbose)
    multilog (pwcm->log, LOG_INFO, "process_xfer(%"PRIu64", %"PRIu64")\n", buf_bytes, bytes_this_xfer);

  int xfer_status = 0;

  int64_t first_byte = 0;

  // check the status of the XFER
  xfer_status = pwcm->xfer_pending_function(pwcm);
  if (pwcm->verbose)
    multilog (pwcm->log, LOG_INFO, "process_xfer: xfer_status=%d\n", xfer_status);

  // if we in the middle of an XFER, update the current open buffer 
  if (xfer_status == DADA_XFER_NORMAL)
  {
    if (pwcm->verbose)
       multilog (pwcm->log, LOG_INFO, "process_xfer: status=XFER_NORMAL, ipcio_close_block_write(%"PRIu64")\n", buf_bytes);
    int rval = ipcio_close_block_write (pwcm->data_block, buf_bytes);
    if (rval < 0) {
      multilog (pwcm->log, LOG_ERR, "process_xfer: ipcio_close_block_write error [%d]\n", rval);
      return DADA_ERROR_FATAL;
    }
    return 0;
  }
  // we are at the end of OBS, dont need to do anything
  else if (xfer_status == DADA_OBS_ENDING) 
  {
    if (pwcm->verbose)
       multilog (pwcm->log, LOG_INFO, "process_xfer: status=OBS_ENDING\n");
    return 0;
  }
  // we are at the end of an XFER (and perhaps the final XFER in an OBS - see bottom)
  else
  {
    if (pwcm->verbose)
      multilog (pwcm->log, LOG_INFO, "process_xfer: status=%d, ipcio_update_block_write(%"PRIu64")\n", xfer_status, buf_bytes);
    if (ipcio_update_block_write (pwcm->data_block, (uint64_t) buf_bytes) < 0)
    {
      multilog (pwcm->log, LOG_ERR, "process_xfer: ipcio_update_block_write error\n");
      return DADA_ERROR_FATAL;
    }

    char * last_header = (char *) malloc (sizeof(char) * pwcm->header_size);
    if (!last_header)
    {
      multilog (pwcm->log, LOG_ERR, "process_xfer: malloc failed for last_header\n");
      return -2;
    }

    // copy the current header
    memcpy(last_header, pwcm->header, pwcm->header_size);

    // close the current data/header block
    if (pwcm->verbose)
      multilog (pwcm->log, LOG_INFO, "process_xfer: closing header/data block\n");

    if (ipcio_close (pwcm->data_block) < 0) {
      multilog (pwcm->log, LOG_ERR, "Could not unlock Data Block write\n");
      return -2;
    }
    if (ipcbuf_unlock_write (pwcm->header_block) < 0) {
      multilog (pwcm->log, LOG_ERR, "Could not unlock Header Block write\n");
      return -2;
    }

    // re-open the data/header block
    if (pwcm->verbose)
      multilog (pwcm->log, LOG_INFO, "process_xfer: opening header/data block\n");

    if (ipcbuf_lock_write (pwcm->header_block) < 0) {
      multilog (pwcm->log, LOG_ERR, "Could not lock Header Block for writing\n");
      return -2;
    }
    if (ipcio_open (pwcm->data_block, 'W') < 0) {
      multilog (pwcm->log, LOG_ERR, "Could not lock Data Block for writing\n");
      return -2;
    }

    // get the next header block 
    pwcm->header = ipcbuf_get_next_write (pwcm->header_block);

    // copy the header_buf to the header block
    memcpy(pwcm->header, last_header, pwcm->header_size);

    // call new xfer function. This will set the OBS_XFER count, the OBS_OFFSET 
    // and return the first byte expected of the next XFER
    first_byte = (int64_t) pwcm->new_xfer_function (pwcm);
    if (pwcm->verbose)
      multilog(pwcm->log, LOG_INFO, "process_xfer: next XFER begins on byte=%"PRIu64"\n", first_byte);

    // marked the header as filled
    if (pwcm->verbose)
      multilog (pwcm->log, LOG_INFO, "process_xfer: marking header block filled\n");
    if (ipcbuf_mark_filled (pwcm->header_block, pwcm->header_size) < 0) {
      multilog (pwcm->log, LOG_ERR, "process_xfer: Could not mark header block filled\n");
      return -2;
    }

    // check the xfer status on the new xfer
    xfer_status = pwcm->xfer_pending_function(pwcm);
    if (pwcm->verbose)
      multilog (pwcm->log, LOG_INFO, "process_xfer: xfer_status=%d\n", xfer_status);

    if (xfer_status == DADA_OBS_ENDING)
    {
      if (pwcm->verbose)
        multilog (pwcm->log, LOG_INFO, "process_xfer: OBS_ENDING, write 1 byte to empty DB\n");
      char zerod_char = '0';
      if (ipcio_write(pwcm->data_block, &zerod_char, 1) != 1)
      {
        multilog(pwcm->log, LOG_ERR, "process_xfer: failed to write 1 byte to empty datablock at end of OBS\n");
        return -2;
      }
    }

    return first_byte;
  }

}