forked from vosegaard/simpson
-
Notifications
You must be signed in to change notification settings - Fork 2
/
iodata.c
1943 lines (1726 loc) · 51.5 KB
/
iodata.c
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
/*
Data format manipulation routines
Copyright (C) 1999 Mads Bak, 2001 Thomas Vosegaard
This file is part of the SIMPSON General NMR Simulation Package
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Routines for internal manipulation with the SIMPSON format.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <tcl.h>
#include <sys/types.h>
#include <unistd.h>
#include "iodata.h"
/* NMRpipe specific inclusion */
#include "fdatap.h"
#ifdef __APPLE__
# ifndef __i386__
# define REVERSEBYTES 1
# endif
#endif
typedef union {
char bits[4];
#ifdef WORDS_BIGENDIAN
struct {
unsigned int negative:1;
unsigned int exponent:8;
unsigned int mantissa:23;
} f;
#else
struct {
unsigned int mantissa:23;
unsigned int exponent:8;
unsigned int negative:1;
} f;
#endif
} floatbits;
#define FLOATBITS_EXPADD 0x7f
#define FLOATBITS_MANT 23
void double_to_bits(double fd, char *bits) {
float f = 0;
if ((fd > __FLT_MIN__ && fd < __FLT_MAX__) || (-fd > __FLT_MIN__ && -fd < __FLT_MAX__)) {
f = (float)fd;
}
float_to_bits(f, bits);
}
void float_to_bits(float f,char* bits)
{
int e;
double m;
floatbits* fb=(floatbits*)bits;
m=frexp(f,&e);
fb->f.negative = (f < 0);
fb->f.exponent = e + FLOATBITS_EXPADD;
fb->f.mantissa = fabs(m)*(double)(1<<FLOATBITS_MANT);
#ifdef WORDS_BIGENDIAN
{
char tmp;
tmp=bits[3];
bits[3]=bits[0];
bits[0]=tmp;
tmp=bits[2];
bits[2]=bits[1];
bits[1]=tmp;
}
#endif
}
float bits_to_float(char* bits)
{
int e;
double m;
#ifdef WORDS_BIGENDIAN
char bits_rev[4];
floatbits* fb=(floatbits*)bits_rev;
bits_rev[0]=bits[3];
bits_rev[1]=bits[2];
bits_rev[2]=bits[1];
bits_rev[3]=bits[0];
#else
floatbits* fb=(floatbits*)bits;
#endif
e = fb->f.exponent - FLOATBITS_EXPADD;
m = (double)fb->f.mantissa/(double)(1<<FLOATBITS_MANT);
if (fb->f.negative) {
return -ldexp(m,e);
}
return ldexp(m,e);
}
#define FIRST(f,x) ((x) & ~(~0 << f))
#define LAST(f,x) ((x) & (~0 << (8-f)))
/* remove spaces */
#define BASE 33
int _pack_newline;
int _pack_nbuf = -1;
unsigned char _pack_buf[4];
void pack_begin(FILE *fp)
{
if (_pack_nbuf != -1) {
fprintf(stderr,"error: pack_begin(): previous session was not ended with pack_end()\n");
exit(-1);
}
_pack_nbuf=0;
_pack_newline=0;
}
int pack_putc(int c,FILE* fp)
{
_pack_buf[_pack_nbuf++] = c;
if (_pack_nbuf == 3) {
putc(FIRST(6,_pack_buf[0]) + BASE,fp);
putc(((LAST(2,_pack_buf[0]) >> 2) | FIRST(4,_pack_buf[1]))+BASE,fp);
putc(((LAST(4,_pack_buf[1]) >> 2) | FIRST(2,_pack_buf[2]))+BASE,fp);
if (putc((LAST(6,_pack_buf[2]) >> 2)+BASE,fp) == EOF) {
_pack_nbuf= -1;
return EOF;
}
_pack_newline++;
if (_pack_newline == 16) {
putc('\n',fp);
_pack_newline=0;
}
_pack_nbuf=0;
}
return 1;
}
int pack_end_putc(FILE* fp)
{
if (_pack_nbuf > 0) {
int i;
for (i=_pack_nbuf;i<3;i++) {
if (pack_putc(0,fp) == EOF) return EOF;
}
if (_pack_newline != 0)
putc('\n',fp);
}
_pack_nbuf=-1;
return 1;
}
int pack_getc(FILE* fp)
{
if (_pack_nbuf == 0) {
int c;
unsigned char c0,c1,c2,c3;
c=getc(fp);
if (c == '\n')
c=getc(fp);
c0=c-BASE;
c1=getc(fp)-BASE;
c2=getc(fp)-BASE;
if ((c=getc(fp)) == EOF) {
_pack_nbuf= -1;
return EOF;
}
c3=c-BASE;
_pack_buf[0]=FIRST(6,c0) | LAST(2,c1 << 2);
_pack_buf[1]=FIRST(4,c1) | LAST(4,c2 << 2);
_pack_buf[2]=FIRST(2,c2) | LAST(6,c3 << 2);
_pack_nbuf=3;
}
return _pack_buf[3-_pack_nbuf--];
}
int pack_end_getc(FILE* fp)
{
_pack_nbuf=-1;
return 1;
}
/*
Data Format Conventions:
Arrays are 1 offset
*/
/*
This is a test of
the new dataformat.:
SIMP
NP=4
SW=10000
REF=100
NI=2
SW1=20000
REF1=200
DATA
1 0
2.3 0
3.1 0
4.01 0
1 0
2.3 0
3.1 0
4.01 0
END
*/
void FD_write(char* fname,FD* fd,int format,int prec)
{
int i,ntot;
FILE* fp;
char bits[4];
if (*fname != '-') {
fp=fopen(fname,"w");
} else {
fp=stdout;
}
if (!fp) {
fprintf(stderr,"FD_write: unable to create file '%s'\n",fname);
exit(1);
}
fprintf(fp,"SIMP\n");
if (fd->np <= 0) {
fprintf(stderr,"FD_write: number of points with must be larger"
" than zero when writing file '%s'\n",fname);
exit(1);
}
fprintf(fp,"NP=%d\n",fd->np);
if (fd->sw <= 0.0) {
fprintf(stderr,"FD_write: spectral-width must be larger"
" than zero when writing file '%s'\n",fname);
exit(1);
}
fprintf(fp,"SW=%.12g\n",fd->sw);
if (fd->ref != 0.0)
fprintf(fp,"REF=%.12g\n",fd->ref);
if (fd->ni != 0) {
fprintf(fp,"NI=%d\n",fd->ni);
}
if (fd->sw1 != 0.0)
fprintf(fp,"SW1=%.12g\n",fd->sw1);
if (fd->ref1 != 0.0)
fprintf(fp,"REF1=%.12g\n",fd->ref1);
if (fd->nelem != 1)
fprintf(fp,"NELEM=%d\n",fd->nelem);
if (fd->type == FD_TYPE_FID)
fprintf(fp,"TYPE=FID\n");
else
fprintf(fp,"TYPE=SPE\n");
fd->format=format;
if (fd->format == FD_FORMAT_BINARY) {
fprintf(fp,"FORMAT=BINARY\n");
fd->prec=prec;
if (fd->prec == FD_PREC_DOUBLE)
fprintf(fp,"PREC=DOUBLE\n");
}
fprintf(fp,"DATA\n");
ntot=fd->np*(fd->ni > 1 ? fd->ni : 1)*fd->nelem;
if (fd->format == FD_FORMAT_BINARY) {
if (fd->prec == FD_PREC_SINGLE) {
int i,doubles;
double* data;
doubles=ntot*2;
data = &(fd->data)[1].re;
pack_begin(fp);
for (i=0;i<doubles;i++) {
int j;
double_to_bits(data[i],bits);
for (j=0;j<4;j++) {
if (pack_putc(bits[j],fp) == EOF) {
fprintf(stderr,"error: FD_write: cannot write float %d\n",i);
exit(1);
}
}
}
if (pack_end_putc(fp) == EOF) {
fprintf(stderr,"error: FD_write: cannot write last float\n");
exit(1);
}
} else {
}
} else {
for (i=1;i<=ntot;i++)
fprintf(fp,"%.9g %.9g\n",fd->data[i].re,fd->data[i].im);
}
fprintf(fp,"END\n");
if (*fname != '-') {
fclose(fp);
}
}
int FD_alloc1ddata(FD* fd)
{
int ntot;
ntot=fd->np*(fd->ni > 1 ? fd->ni : 1)*fd->nelem;
fd->data=(complx*)malloc(sizeof(complx)*(ntot+1));
if (!fd->data) {
fprintf(stderr,"FD_alloc1ddata: unable to allocate 2x%d doubles\n",ntot);
return 0;
}
return 1;
}
void FD_defvalues(FD* fd)
{
if (!fd) {
fprintf(stderr,"FD_defvalues: called with NULL argument\n");
exit(1);
}
fd->data=NULL;
fd->np=0;
fd->ni=0;
fd->nelem = 1;
fd->type=FD_TYPE_FID;
fd->format=FD_FORMAT_TEXT;
fd->prec=FD_PREC_SINGLE;
fd->ref=0.0;
fd->sw=0.0;
fd->ref1=0.0;
fd->sw1=0.0;
}
FD* FD_alloc()
{
FD* fd;
fd = (FD*)malloc(sizeof(FD));
if (!fd) {
fprintf(stderr,"FD_alloc: unable to allocate FD data structure\n");
exit(1);
}
FD_defvalues(fd);
return fd;
}
void FD_free(FD* fd)
{
if (!fd) {
fprintf(stderr,"FD_free: called with NULL argument\n");
exit(1);
}
if (!fd->data) {
fprintf(stderr,"FD_free: FD structure contained no data !\n");
exit(1);
}
free(fd->data);
free(fd);
}
void FD_zero(FD* fd)
{
if (!fd) {
fprintf(stderr,"FD_zero: called with NULL argument\n");
exit(1);
}
if (!fd->data) {
fprintf(stderr,"FD_zero: FD structure contained no data !\n");
exit(1);
}
memset(fd->data,0,sizeof(complx)*(fd->np*(fd->ni > 1 ? fd->ni : 1)*fd->nelem+1));
}
FD* FD_read(char* fname)
{
int got_np=0,got_sw=0,got_data=0;
FILE* fp;
FD* fd;
int i,lineno,ntot;
char buf[256],*p,*q;
char com[256];
char val[256];
char bits[4];
fd=FD_alloc();
if (!fd){
fprintf(stderr,"FD_read: unable to allocate fd datatype\n");
exit(1);
}
if (!fname) {
fprintf(stderr,"FD_read: filename pointer is null\n");
exit(1);
}
fp=fopen(fname,"r");
if (!fp) {
fprintf(stderr,"FD_read: unable to open file '%s'\n",fname);
exit(1);
}
lineno=1;
if (!fgets(buf,sizeof(buf),fp)) {
fprintf(stderr,"FD_read: unable to read line %d from file '%s'\n",lineno,fname);
exit(1);
}
// this should fix mixed line-ending errors
if (strcmp(buf,"SIMP\n") != 0 && strcmp(buf,"SIMP\r\n") != 0) {
fprintf(stderr,"FD_read: invalid type of file '%s' (expected 'SIMP'), in file '%s'\n",buf,fname);
exit(1);
}
while (!feof(fp) && !got_data) {
lineno++;
if (!fgets(buf,sizeof(buf),fp)) {
if (got_sw && got_data && got_np) break;
buf[0]=0;
if (!got_sw) {
strcat(buf," SW=<spectralwidth>,");
}
if (!got_data) {
strcat(buf," datasection,");
}
if (!got_np) {
strcat(buf," NP=<number of points>,");
}
fprintf(stderr,"FD_read: missing%s in file '%s'\n",buf,fname);
exit(1);
}
p=buf;
q=p+strlen(buf)-1;
if (*q == '\n') *q=0;
while (isspace(*p)) p++;
if (*p == 0) continue;
q=(char*)strchr(p,'=');
if (q) {
*q=0; q++;
while (isspace(*p)) p++;
strcpy(com,p);
strcpy(val,q);
if (!strcmp(com,"NP")) {
fd->np=atoi(val);
if (fd->np <= 0) {
fprintf(stderr,"FD_read: number of points NP must be larger than zero\n");
exit(1);
}
got_np=1;
} else if (!strcmp(com,"NI")) {
fd->ni=atoi(val);
if (fd->ni <= 0) {
fprintf(stderr,"FD_read: number of points NI must be larger than zero\n");
exit(1);
}
} else if (!strcmp(com,"NELEM")) {
fd->nelem=atoi(val);
if (fd->nelem <= 0) {
fprintf(stderr,"FD_read: number of elements NELEM must be larger than zero\n");
exit(1);
}
} else if (!strcmp(com,"SW")) {
if (sscanf(val,"%lg",&fd->sw) != 1) {
fprintf(stderr,"FD_read: unable to convert '%s' to double in file '%s'\n",val,fname);
exit(1);
}
got_sw=1;
} else if (!strcmp(com,"SW1")) {
if (sscanf(val,"%lg",&fd->sw1) != 1) {
fprintf(stderr,"FD_read: unable to convert '%s' to double in file '%s'\n",val,fname);
exit(1);
}
} else if (!strcmp(com,"TYPE")) {
if (!strcmp(val,"FID")) {
fd->type=FD_TYPE_FID;
} else if (!strcmp(val,"SPE")) {
fd->type=FD_TYPE_SPE;
} else {
fprintf(stderr,"FD_read: unable to convert '%s' to SPE or FID in file '%s'\n",val,fname);
exit(1);
}
} else if (!strcmp(com,"FORMAT")) {
if (!strcmp(val,"TEXT")) {
fd->format=FD_FORMAT_TEXT;
} else if (!strcmp(val,"BINARY")) {
fd->format=FD_FORMAT_BINARY;
} else {
fprintf(stderr,"FD_read: unable to convert '%s' to TEXT or BINARY in file '%s'\n",val,fname);
exit(1);
}
} else if (!strcmp(com,"PREC")) {
if (!strcmp(val,"DOUBLE")) {
fd->prec=FD_PREC_DOUBLE;
} else if (!strcmp(val,"SINGLE")) {
fd->prec=FD_PREC_SINGLE;
} else {
fprintf(stderr,"FD_read: unable to convert '%s' to DOUBLE or SINGLE in file '%s'\n",val,fname);
exit(1);
}
} else if (!strcmp(com,"REF")) {
if (sscanf(val,"%lg",&fd->ref) != 1) {
fprintf(stderr,"FD_read: unable to convert '%s' to double in file '%s'\n",val,fname);
exit(1);
}
} else if (!strcmp(com,"REF1")) {
if (sscanf(val,"%lg",&fd->ref1) != 1) {
fprintf(stderr,"FD_read: unable to convert '%s' to double in file '%s'\n",val,fname);
exit(1);
}
} else {
/*
fprintf(stderr,"FD_read: unknown command '%s' in file '%s'\n",com,fname);
exit(1);
*/
}
} else {
if (strcmp(buf,"DATA")) {
fprintf(stderr,"FD_read: data section must start with DATA and not '%s' in file '%s'\n",buf,fname);
exit(1);
}
FD_alloc1ddata(fd);
ntot=fd->np*(fd->ni > 1 ? fd->ni : 1)*fd->nelem;
if (fd->format == FD_FORMAT_TEXT) {
for (i=1;i<=ntot;i++) {
if (fscanf(fp,"%lg%lg",&(fd->data[i].re),&(fd->data[i].im)) != 2) {
fprintf(stderr,"FD_read: unable to read data point number %d from file '%s'\n",i,fname);
exit(1);
}
}
} else {
if (fd->prec == FD_PREC_SINGLE) {
int i,doubles,c;
double* data;
doubles=ntot*2;
data = &(fd->data)[1].re;
pack_begin(fp);
for (i=0;i<doubles;i++) {
int j;
for (j=0;j<4;j++) {
if ((c=pack_getc(fp)) == EOF) {
fprintf(stderr,"error: FD_read: cannot read float %d\n",i);
exit(1);
}
bits[j]=c;
}
data[i]=bits_to_float(bits);
}
pack_end_getc(fp);
} else {
int nb;
if ((nb=fread(&(fd->data[1]),sizeof(complx),ntot,fp)) != ntot) {
fprintf(stderr,"FD_read: read %d but needed %d complex numbers"
" from file '%s'\n",nb,ntot,fname);
exit(1);
}
}
}
strcpy(buf,"");
if (!fscanf(fp,"%s",buf)) {
fprintf(stderr,"FD_read: must read a string (\"END\") at the end of data\n");
exit(1);
}
if (strcmp(buf,"END")) {
fprintf(stderr,"FD_read: data section must end with END and not '%s' in file '%s'\n",buf,fname);
/* exit(1); */
}
got_data=1;
}
}
fclose(fp);
return fd;
}
char *sgets(char *str, int n, char *data) {
int i=0;
while (data[i] != 0 && data[i] != '\n' && i < n) {
str[i] = data[i];
i++;
}
if (i < n) str[i] = 0;
if (i==0) return NULL;
return &data[i+1];
}
int seof(char *data) {
if (*data == 0) return 1;
return 0;
}
FD* FD_readstr(char* d)
{
int got_np=0,got_sw=0,got_data=0;
FD* fd;
int i,lineno,ntot,j;
char buf[256],*p,*q;
char com[256];
char val[256];
char bits[4];
char *dd = d;
fd=FD_alloc();
if (!fd){
fprintf(stderr,"FD_readstr: unable to allocate fd datatype\n");
exit(1);
}
if (!dd) {
fprintf(stderr,"FD_readstr: data pointer is null\n");
exit(1);
}
lineno=1;
if ((dd = sgets(buf, sizeof(buf), dd)) == NULL) {
fprintf(stderr,"FD_readstr: unable to read line %d\n",lineno);
exit(1);
}
if (strncmp(buf,"SIMP",4)) {
fprintf(stderr,"FD_readstr: invalid type of file '%s' (expected 'SIMP')\n",buf);
exit(1);
}
while (!seof(dd) && !got_data) {
lineno++;
if ((dd = sgets(buf,sizeof(buf),dd)) == NULL) {
if (got_sw && got_data && got_np) break;
buf[0]=0;
if (!got_sw) {
strcat(buf," SW=<spectralwidth>,");
}
if (!got_data) {
strcat(buf," datasection,");
}
if (!got_np) {
strcat(buf," NP=<number of points>,");
}
fprintf(stderr,"FD_readstr: missing%s\n",buf);
exit(1);
}
p=buf;
q=p+strlen(buf)-1;
if (*q == '\n') *q=0;
while (isspace(*p)) p++;
if (*p == 0) continue;
q=(char*)strchr(p,'=');
if (q) {
*q=0; q++;
while (isspace(*p)) p++;
strcpy(com,p);
strcpy(val,q);
if (!strcmp(com,"NP")) {
fd->np=atoi(val);
if (fd->np <= 0) {
fprintf(stderr,"FD_readstr: number of points must be larger than zero\n");
exit(1);
}
got_np=1;
} else if (!strcmp(com,"NI")) {
fd->ni=atoi(val);
if (fd->ni <= 0) {
fprintf(stderr,"FD_readstr: number of points must be larger than zero\n");
exit(1);
}
} else if (!strcmp(com,"NELEM")) {
fd->nelem=atoi(val);
if (fd->nelem <= 0) {
fprintf(stderr,"FD_readstr: number of elements NELEM must be larger than zero\n");
exit(1);
}
} else if (!strcmp(com,"SW")) {
if (sscanf(val,"%lg",&fd->sw) != 1) {
fprintf(stderr,"FD_readstr: unable to convert '%s' to double\n",val);
exit(1);
}
got_sw=1;
} else if (!strcmp(com,"SW1")) {
if (sscanf(val,"%lg",&fd->sw1) != 1) {
fprintf(stderr,"FD_readstr: unable to convert '%s' to double\n",val);
exit(1);
}
} else if (!strcmp(com,"TYPE")) {
if (!strcmp(val,"FID")) {
fd->type=FD_TYPE_FID;
} else if (!strcmp(val,"SPE")) {
fd->type=FD_TYPE_SPE;
} else {
fprintf(stderr,"FD_readstr: unable to convert '%s' to SPE or FID\n",val);
exit(1);
}
} else if (!strcmp(com,"FORMAT")) {
if (!strcmp(val,"TEXT")) {
fd->format=FD_FORMAT_TEXT;
} else if (!strcmp(val,"BINARY")) {
fd->format=FD_FORMAT_BINARY;
} else {
fprintf(stderr,"FD_readstr: unable to convert '%s' to TEXT or BINARY\n",val);
exit(1);
}
} else if (!strcmp(com,"PREC")) {
if (!strcmp(val,"DOUBLE")) {
fd->prec=FD_PREC_DOUBLE;
} else if (!strcmp(val,"SINGLE")) {
fd->prec=FD_PREC_SINGLE;
} else {
fprintf(stderr,"FD_readstr: unable to convert '%s' to DOUBLE or SINGLE\n",val);
exit(1);
}
} else if (!strcmp(com,"REF")) {
if (sscanf(val,"%lg",&fd->ref) != 1) {
fprintf(stderr,"FD_readstr: unable to convert '%s' to double\n",val);
exit(1);
}
} else if (!strcmp(com,"REF1")) {
if (sscanf(val,"%lg",&fd->ref1) != 1) {
fprintf(stderr,"FD_readstr: unable to convert '%s' to double\n",val);
exit(1);
}
}
} else {
if (strcmp(buf,"DATA")) {
fprintf(stderr,"FD_readstr: data section must start with DATA and not '%s'\n",buf);
exit(1);
}
FD_alloc1ddata(fd);
ntot=fd->np*(fd->ni > 1 ? fd->ni : 1)*fd->nelem;
if (fd->format == FD_FORMAT_TEXT) {
for (i=1;i<=ntot;i++) {
if (sscanf(dd,"%lg%lg%n",&(fd->data[i].re),&(fd->data[i].im),&j) != 2) {
fprintf(stderr,"FD_read: unable to read data point number %d\n",i);
exit(1);
}
dd += j;
if (*dd == '\n') dd++;
}
} else {
if (fd->prec == FD_PREC_SINGLE) {
int i,doubles,c;
double* data;
doubles=ntot*2;
data = &(fd->data)[1].re;
_pack_nbuf=0;
_pack_newline=0;
for (i=0;i<doubles;i++) {
for (j=0;j<4;j++) {
if (_pack_nbuf==0) {
unsigned char c0,c1,c2,c3;
if (*dd == '\n') dd++;
c0=(unsigned char)*dd-BASE;dd++;
c1=(unsigned char)*dd-BASE;dd++;
c2=(unsigned char)*dd-BASE;dd++;
c3=(unsigned char)*dd-BASE;dd++;
_pack_buf[0]=FIRST(6,c0) | LAST(2,c1 << 2);
_pack_buf[1]=FIRST(4,c1) | LAST(4,c2 << 2);
_pack_buf[2]=FIRST(2,c2) | LAST(6,c3 << 2);
_pack_nbuf=3;
}
c = (int)_pack_buf[3-_pack_nbuf--];
bits[j]=c;
}
data[i]=bits_to_float(bits);
}
_pack_nbuf=-1;
while (*dd == '\n') dd++;
} else {
int nb;
if (strlen(dd) < ntot*sizeof(complx)) {
fprintf(stderr,"FD_read: read %d but needed %d complex numbers\n",nb,ntot);
exit(1);
} else {
memcpy(&fd->data[1], dd, ntot*sizeof(complx));
}
}
}
strcpy(buf,"");
if ((dd = sgets(buf,sizeof(buf),dd)) == NULL) {
fprintf(stderr,"FD_readstr: must read a string (\"END\") at the end of data\n");
exit(1);
}
if (strcmp(buf,"END")) {
fprintf(stderr,"FD_readstr: data section must end with END and not '%s'\n",buf);
exit(1);
}
got_data=1;
}
}
return fd;
}
FD* FD_dup(FD* fd)
{
FD* to;
if (!fd) {
fprintf(stderr,"FD_dup: called with NULL argument\n");
exit(1);
}
to=FD_alloc();
to->np=fd->np;
to->ni=fd->ni;
to->nelem=fd->nelem;
to->type=fd->type;
to->format=fd->format;
to->prec=fd->prec;
to->ref=fd->ref;
to->ref1=fd->ref1;
to->sw=fd->sw;
to->sw1=fd->sw1;
FD_alloc1ddata(to);
memcpy(to->data,fd->data,sizeof(complx)*(to->np*(to->ni > 1 ? to->ni : 1)*to->nelem+1));
return to;
}
FD* FD_data2fd(char* file,complx* data,int np,int ni,double sw,double sw1, int nelem)
{
FD* fd;
fd=FD_alloc();
fd->np=np;
fd->ni=ni;
fd->nelem=nelem;
fd->sw=sw;
fd->sw1=sw1;
FD_alloc1ddata(fd);
memcpy(fd->data,data,sizeof(complx)*(fd->np*(fd->ni > 1 ? fd->ni : 1)*fd->nelem+1));
return fd;
}
/* Routines inserted by Thomas Vosegaard, 2001 */
void mac2unix(char *in)
{
int i = 0;
while (in[i]) {
if (in[i] == 13) in[i] = 10;
i++;
}
}
void unix2mac(char *in)
{
int i = 0;
while (in[i]) {
if (in[i] == 10) in[i] = 13;
i++;
}
}
void swap(char *ptr, int size)
{
int i;
char *p, *rp, swp;
for (i = 0; i < size/2; i++) {
p = ptr + i;
rp = ptr + (size-1) - i;
swp = *p;
*p = *rp;
*rp = swp;
}
}
size_t freadreversedbyteorder(void *ptr, size_t itemsize, size_t nitem, FILE *file)
{
int i, tmp;
char *cptr = ptr;
tmp = fread(ptr, itemsize, nitem, file);
if (itemsize > 1)
for (i = 0; i < nitem; i++) swap(cptr+i*itemsize, itemsize);
return (size_t)tmp;
}
size_t fwritereversedbyteorder(const void *ptr, size_t itemsize, size_t nitem, FILE *file)
{
int i, tmp;
char *p, *cptr = (char *)ptr;
if (itemsize == 1) {
tmp = fwrite(ptr, 1, nitem, file);
} else {
p = malloc(itemsize*nitem);
for (i = 0; i < nitem*itemsize; i++) p[i] = cptr[i];
for (i = 0; i < nitem; i++) swap((char *)(p+i*itemsize), itemsize);
tmp = fwrite(p, itemsize, nitem, file);
free(p);
}
return (size_t)tmp;
}
#ifdef REVERSEBYTES
# define freadreversed freadreversedbyteorder
# define fwritereversed fwritereversedbyteorder
#else
# define freadreversed fread
# define fwritereversed fwrite
#endif
void FD_write_rmn(char *fname, FD* fd)
{
int i,ntot;
char version, title[512],c;
double dd;
float df;
FILE* fp;
if (fd->nelem != 1) {
fprintf(stderr,"Error: FD_write_rmn detected multiple elements in fid/spc - feature not supported here\n");
exit(1);
}
fp=fopen(fname,"w");
if (!fp) {
fprintf(stderr,"FD_write_rmn: unable to create file '%s'\n",fname);
exit(1);
}
if (fd->ni == 0) {
/*
if (fd->type == FD_TYPE_FID) {
printf("Saving 1D fid\n");
} else {
printf("Saving 1D spec\n");
}
*/
version=2;
} else {
/*
if (fd->type == FD_TYPE_FID) {
printf("Saving 2D fid\n");
} else {
printf("Saving 2D spec\n");
}
*/
version=4;
}
if (!fwritereversed(&version,1,1,fp)) {
fprintf(stderr,"FD_write_rmn: unable to write version"
" to file '%s'\n",fname);
exit(1);
}
if (fd->np <= 0) {
fprintf(stderr,"FD_write_rmn: number of points with must be larger"
" than zero when writing file '%s'\n",fname);
exit(1);
}
i = fd->np;
if (!fwritereversed(&i, sizeof(int), 1, fp)) {
fprintf(stderr,"FD_write_rmn: unable to write number of points"
" to file '%s'\n",fname);
exit(1);
}
if (fd->sw <= 0.0) {
fprintf(stderr,"FD_write: spectral-width must be larger"
" than zero when writing file '%s'\n",fname);
exit(1);
}
dd = 1.0/fd->sw;
if (!fwritereversed(&dd, sizeof(double), 1, fp)) {
fprintf(stderr,"FD_write_rmn: unable to write dwell-time"
" to file '%s'\n",fname);