-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbin2txt.c
1427 lines (1144 loc) · 43.4 KB
/
bin2txt.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/////////////////
//
// chooseable:
//
/////////////////
#define HDF (1)
#define V5D (1)
/////////////////
//
// rest NOT chooseable unless you know what you are doing:
//
/////////////////
#ifndef _SWAP_ENDIAN
#define _SWAP_ENDIAN
// Macs and SGIs are Big-Endian; PCs are little endian
// returns TRUE if current machine is little endian
static int IsLittleEndian(void);
/******************************************************************************
FUNCTION: SwapEndian
PURPOSE: Swap the byte order of a structure
EXAMPLE: float F=123.456;; SWAP_FLOAT(F);
******************************************************************************/
#define SWAP_SHORT(Var) Var = *(short*) SwapEndian((void*)&Var, sizeof(short))
#define SWAP_USHORT(Var) Var = *(unsigned short*)SwapEndian((void*)&Var, sizeof(short))
#define SWAP_LONG(Var) Var = *(long*) SwapEndian((void*)&Var, sizeof(long))
#define SWAP_ULONG(Var) Var = *(unsigned long*) SwapEndian((void*)&Var, sizeof(long))
#define SWAP_RGB(Var) Var = *(int*) SwapEndian((void*)&Var, 3)
#define SWAP_FLOAT(Var) Var = *(float*) SwapEndian((void*)&Var, sizeof(float))
#define SWAP_DOUBLE(Var) Var = *(double*) SwapEndian((void*)&Var, sizeof(double))
static void *SwapEndian(void* Addr, const int Nb);
#endif
#define LITTLE_ENDIAN_USER 0
#define BIG_ENDIAN_USER 1
// this program does not convert between data types, only between file formats
// for HDF we have to choose what datatype is read/written, and it's fixed for all output/input types
#if(HDF)
// note, for HDF, dim0,1,2,3,... means the array element array[dim0][dim1][dim2][dim3], which means dimN where N is the dimensionality of the vector, is most quickly iterated.
// so a loop like for(k) for(j) for(i) would be used on array[k][j][i]
// so i is actually dim2, j is dim1, and k is dim0
// SOMETIMES SYSTEM USER OR INSTALLATION TYPE (e.g. ki-rh42,physics-179.umd.edu)
#include "hdf/hdf.h"
#include "hdf/mfhdf.h"
// SOMETIMES USER INSTALLATION TYPE (e.g. ki-rh39)
//#include "hdf.h"
//#include "mfhdf.h"
#define HDFTYPE DFNT_FLOAT32
//#define HDFTYPE DFNT_FLOAT64
// could be DFNT_BYTE DFNT_INT32 or DFNT_FLOAT64 for doubles
#define SDS_NAME "Jonathan McKinney SDS"
#endif
#if(V5D)
#include <vis5d+/v5d.h>
#include <vis5d+/binio.h>
#endif
// note that for binary, should be concerned with big or little endianness
#define BYTE unsigned char
static void swap(BYTE *x, BYTE size);
#define swaporder16(A) ( (((A) & 0xff00) >> 8) | (((A) & 0x00ff) << 8) )
#define swaporder32(A) ( (((A) & 0xff000000) >> 24) | (((A) & 0x00ff0000) >> 8) | (((A) & 0x0000ff00) << 8) | (((A) & 0x000000ff) << 24))
#define DEBUG (0)
// debug compile gcc -O0 -g -Wall -o bin2txt bin2txt.c -lmfhdf -ldf -ljpeg -lz -lv5d
// standard compile on ki-rh42:
// gcc -O2 -Wall -o bin2txt bin2txt.c -I /usr/include/hdf/ -L /usr/lib64/hdf/ -lmfhdf -ldf -ljpeg -lz -lv5d
// standard compile on ki-rh39:
// gcc -O2 -Wall -o bin2txt bin2txt.c -I /usr2/local/include/ -L /usr2/local/lib/ -lmfhdf -ldf -ljpeg -lz -lv5d
// JCM 10-4-01
// JCM 10-30-01 added hdf4 capability
#define BUFFERMAP ((k*N2*N1+j*N1+i)*numcolumns+nextbuf++)
#define BUFFERINIT nextbuf=0
#define MAXTERMS 100
int main(
int argc,
char *argv[],
char *envp[]
)
{
int machineEndianness(void);
int argstep=0;
int NDIM;
int i,j,k;
int numrows,numcolumns,numterms,numheader,pipeheader,realnumheader;
int bytesize,intsize,floatsize,doublesize;
unsigned char dumb;
unsigned short shortfordumb;
int dumi;
float dumf;
double dumlf;
char precision[MAXTERMS];
int group[MAXTERMS];
int N1,N2,N3,TS;
int inputlist,outputlist,input4dblock;
int nextbuf;
FILE * input;
FILE * output;
FILE * inputfilenamelist;
FILE * outputfilenamelist;
char INPUT_NAME[400];
char OUTPUT_NAME[400];
char INPUTLIST_NAME[400];
char OUTPUTLIST_NAME[400];
char V5DHEAD_NAME[400];
int source,dest,ble;
short *arrayb;
int *arrayi;
float *arrayf;
double *arrayd;
void * array;
float *arrayvisf;
void * arrayvis;
float *arrayvisoutput;
#if(HDF)
// hdf
int32 sd_id, sds_id, sd_index,istat;
intn status;
int32 rank;
int32 dim_sizes[4], start[4], edges[4];
#endif
int it;
#if(V5D)
// vis5d
int iv;
int NumTimes; /* number of time steps */
int NumVars; /* number of variables */
int Nr, Nc, Nl[MAXVARS]; /* size of 3-D grids */
char VarName[MAXVARS][MAXVARNAME]; /* names of variables */
int TimeStamp[MAXTIMES]; /* real times for each time step */
int DateStamp[MAXTIMES]; /* real dates for each time step */
int CompressMode; /* number of bytes per grid */
int Projection; /* a projection number */
float ProjArgs[MAXPROJARGS]; /* the projection parameters */
int Vertical; /* a vertical coord system number */
float VertArgs[MAXVERTARGS]; /* the vertical coord sys parameters */
FILE * vis5dheader;
float pos[3+1][2+1];
float minmax[2][MAXVARS];
float a,b;
int ijkvis5d,ijkjon;
#endif
// r8 stuff
char incommand[300],outcommand[300];
int lname;
int inputtype,outputtype;
char ch;
// begin
bytesize=sizeof(unsigned char);
intsize=sizeof(int);
floatsize=sizeof(float);
doublesize=sizeof(double);
if(argc<11){
fprintf(stderr,"non-V5D Usage: bin2txt SOURCE DEST BLE HEADERLINESTOSKIP NDIM N1 N2 N3 TS INPUTNAME OUTPUTNAME <format>\n");
fprintf(stderr," SOURCE/DEST: 0=r8 (.gz) 1=binary 2=text 3=HDF4 4=HDF5 5=vis5d\n");
fprintf(stderr," BLE: same endian (0), big to little or little to big (same process) (1), auto-conversion assuming ROMIO binary (-1)\n");
fprintf(stderr," HEADERLINESTOSKIP: # of header text lines in r8 or text source or binary header (negative values mean pipe text header into new file)\n");
fprintf(stderr," NDIM/N1/N2/N3/TS: # of dimensions and elements in each dimension (e.g. if 2D, N3's value doesn't matter but needs to be there) and # of timesteps to include TS must be >0\n");
fprintf(stderr," INPUTNAME/OUTPUTNAME: input and output file names (not used if TS>1)\n");
fprintf(stderr," <format> type is (for 1-4) b=byte i=integer, f=float, d=double, where you give <type1> #of1 <type2> #of2 ...\n");
fprintf(stderr,"\ne.g. bin2txt 1 2 0 3 32 32 32 1 data32-3.txt data32-3.bin i 3 f 9\n\n");
fprintf(stderr,"\n");
fprintf(stderr,"x=0,1,2 && y=0,1,2 w/ TS>1 then also include:\n");
fprintf(stderr,"Usage (dest=0,1,2 w/ TS=+|TS|): bin2txt SOURCE DEST BLE HEADERLINESTOSKIP NDIM N1 N2 N3 TS INPUTLIST OUTPUTNAME <format>\n");
fprintf(stderr,"Usage (source=0,1,2 w/ TS=-|TS|): bin2txt SOURCE DEST BLE HEADERLINESTOSKIP NDIM N1 N2 N3 TS INPUTNAME OUTPUTLIST <format>\n");
fprintf(stderr,"INTPUTLIST contains: sequence of return delimited input file names for each timeslice\n");
fprintf(stderr,"OUTPUTLIST contains: sequence of return delimited output file names for each timeslice\n");
fprintf(stderr,"e.g., to input list and output single 4D file: bin2txtn x y 0 -1 3 256 128 32 +67 dumplist.txt dump.??? d 6\n");
fprintf(stderr,"e.g., to input 4D file and output many files: bin2txtn x y 0 -1 3 256 128 32 -67 dump4d.??? dumplist.txt d 6\n");
fprintf(stderr,"\n");
fprintf(stderr,"Vis5D with TS=1:\n");
fprintf(stderr,"Usage (dest=5): bin2txt SOURCE DEST BLE HEADERLINESTOSKIP NDIM N1 N2 N3 TS HEADFILE INPUTNAME OUTPUTNAME <format>\n");
fprintf(stderr,"HEADFILE contains return delimited list of names for all variables and their min and max values (min/max only used if source=0) (names MUST BE <=9 characters!!!)\n");
fprintf(stderr,"Last line must have:\nx_start x_finish y_start y_finish z_start z_finish\n");
fprintf(stderr,"e.g. for one variable:\n\ndensity 1E-4 1\n");
fprintf(stderr,"0 1 0 1 0 1\n");
fprintf(stderr,"e.g. for fieldline file with 11 columns:\n");
fprintf(stderr,"density 1E-4 1\n"
"ug 1E-4 1\n"
"negudt 1E-4 1\n"
"mu 1E-4 1\n"
"uut 1E-4 1\n"
"vr 1E-4 1\n"
"vh 1E-4 1\n"
"vph 1E-4 1\n"
"Br 1E-4 1\n"
"Bh 1E-4 1\n"
"Bp 1E-4 1\n"
"0 1 0 1 0 1\n"
);
fprintf(stderr,"vis5d e.g.: ./bin2txt 0 5 0 3 64 64 64 1 vis5d.image.head imx0-0-0-s1-0000.dat.r8.gz imx0-0-0-s1-0000.dat.v5d b 1\n");
fprintf(stderr,"\n");
fprintf(stderr,"V5D w/ TS>1 then also include:\n");
fprintf(stderr,"Usage (dest=5): bin2txt SOURCE DEST BLE HEADERLINESTOSKIP NDIM N1 N2 N3 TS HEADFILE INPUTLIST OUTPUTNAME <format>\n");
fprintf(stderr,"Usage (source=5): bin2txt SOURCE DEST BLE HEADERLINESTOSKIP NDIM N1 N2 N3 TS HEADFILE INPUTNAME OUTPUTLIST <format>\n");
fprintf(stderr,"INTPUTLIST contains: sequence of return delimited input file names for each timeslice\n");
fprintf(stderr,"OUTPUTLIST contains: sequence of return delimited output file names for each timeslice\n");
fprintf(stderr,"vis5d: bin2txtn 2 5 0 7 3 32 32 32 101 vis5d.dump.head dumplist.txt dump.v5d d 9\n");
fprintf(stderr,"vis5d: bin2txtn 2 5 0 0 3 32 32 32 101 vis5d.force.head forcelist.txt forcex.v5d d 8\n");
exit(1);
}
//////////////////
//
// BEGIN Read-in arguments
//
//////////////////
// argv[0] is filename of program
argstep=1;
source=atoi(argv[argstep++]);
dest=atoi(argv[argstep++]);
ble=atoi(argv[argstep++]);
if(ble==-1){
if(machineEndianness()==LITTLE_ENDIAN_USER){
ble=1; // need to convert
}
else{
ble=0; // no need to convert
}
}
// else use user-inputted ble
if(ble!=0 && ble!=1){
fprintf(stderr,"ble=%d undefined\n",ble);
exit(1);
}
numheader=atoi(argv[argstep++]);
if(numheader<0){
pipeheader=1;
realnumheader=-numheader;
}
else{
pipeheader=0;
realnumheader=numheader;
}
NDIM=atoi(argv[argstep++]);
N1=atoi(argv[argstep++]);
N2=atoi(argv[argstep++]);
N3=atoi(argv[argstep++]);
TS=atoi(argv[argstep++]);
if(TS==0){
TS=1;
fprintf(stderr,"Assumed by TS=0 you meant TS=%d really\n",TS);
}
if(TS<0){
TS=-TS;
fprintf(stderr,"Assumed by TS<0 you meant TS=%d but inputting 4D data block\n",TS);
input4dblock=1;
}
else{
input4dblock=0;
fprintf(stderr,"outputting 4D data block\n");
}
fprintf(stderr,"source: %d dest: %d numheader: %d NDIM: %d N1: %d N2: %d N3: %d TS: %d\n",source,dest,numheader,NDIM,N1,N2,N3,TS);
// modify N1,N2,N3 for various dimension
if(NDIM==2){
N3=1; // force
}
else if(NDIM==1){
N2=N3=1; // force
}
else if(NDIM==4){
fprintf(stderr,"Not yet setup for direct 4D input\n");
exit(1);
}
// set number of rows
numrows=N1*N2*N3;
////////////////////////
//
// get input-output file (or list)
//
////////////////////////
if((TS>1)&&(dest==5)){
strcpy(V5DHEAD_NAME,argv[argstep++]); // for vis5d this is the header
strcpy(INPUTLIST_NAME,argv[argstep++]); // for TS>1 this is list of files
strcpy(OUTPUT_NAME,argv[argstep++]); // single output name
fprintf(stderr,"V5DHEAD_NAME: %s INPUTLIST_NAME: %s OUTPUT_NAME: %s\n",V5DHEAD_NAME,INPUTLIST_NAME,OUTPUT_NAME); fflush(stderr);
}
else if((TS>1)&&(source==5)){
strcpy(V5DHEAD_NAME,argv[argstep++]); // for vis5d this is the header
strcpy(INPUT_NAME,argv[argstep++]); // just single input name
strcpy(OUTPUTLIST_NAME,argv[argstep++]); // for TS>1 this is list of files
fprintf(stderr,"V5DHEAD_NAME: %s INPUT_NAME: %s OUTPUTLIST: %s \n",V5DHEAD_NAME,INPUT_NAME,OUTPUTLIST_NAME); fflush(stderr);
}
else if((dest==5)||(source==5)){
strcpy(V5DHEAD_NAME,argv[argstep++]); // for vis5d this is the header
strcpy(INPUT_NAME,argv[argstep++]); // just single input name
strcpy(OUTPUT_NAME,argv[argstep++]); // single output name
fprintf(stderr,"V5DHEAD_NAME: %s INPUT_NAME: %s OUTPUT_NAME: %s \n",V5DHEAD_NAME,INPUT_NAME,OUTPUT_NAME); fflush(stderr);
}
else if((TS>1 && input4dblock==0)&&(dest==0 || dest==1 || dest==2)){
strcpy(INPUTLIST_NAME,argv[argstep++]); // for TS>1 this is list of files
strcpy(OUTPUT_NAME,argv[argstep++]); // single output name
fprintf(stderr,"INPUTLIST_NAME: %s OUTPUT_NAME: %s\n",INPUTLIST_NAME,OUTPUT_NAME); fflush(stderr);
}
else if((TS>1 && input4dblock==1)&&(source==0 || source==1 || source==2)){
strcpy(INPUT_NAME,argv[argstep++]); // just single input name
strcpy(OUTPUTLIST_NAME,argv[argstep++]); // for TS>1 this is list of files
fprintf(stderr,"INPUT_NAME: %s OUTPUTLIST: %s \n",INPUT_NAME,OUTPUTLIST_NAME); fflush(stderr);
}
else{ // then don't need v5d header and don't need multi-file list
strcpy(INPUT_NAME,argv[argstep++]); // just single input name
strcpy(OUTPUT_NAME,argv[argstep++]); // single output name
fprintf(stderr,"INPUT_NAME: %s OUTPUT_NAME: %s\n",INPUT_NAME,OUTPUT_NAME); fflush(stderr);
}
//////////////////
//
// Setup format specifier from command line
//
//////////////////
j=0;
numcolumns=0;
while(argstep<argc){
sscanf(argv[argstep++],"%c",&precision[j]);
sscanf(argv[argstep++],"%d",&group[j]);
numcolumns+=group[j];
j++;
if(j>MAXTERMS){
fprintf(stderr,"too many terms!\n");
exit(1);
}
}
numterms=j;
//////////////////
//
// END Read-in arguments
//
//////////////////
fprintf(stderr,"numrows: %d numcolumns: %d numterms: %d\n",numrows,numcolumns,numterms); fflush(stderr);
/////////////////////////////
//
// vis5d stuff
//
/////////////////////////////
#if(V5D)
// for source==5, don't actually use header, so header can be blank but file name should still be added to command line
if(dest==5){
NumTimes=TS;
NumVars=numcolumns;
Nr=N3; // (whine) Avery said use N3 instead of N1
Nc=N2;
fprintf(stderr,"NumTimes=%d NumVars=%d Nr=%d Nc=%d\n",NumTimes,NumVars,Nr,Nc); fflush(stderr);
for(i=0;i<NumVars;i++){
Nl[i]=N1; // all variables have same # of N3 elements
// (whine) Avery said use N1 instead of N3
fprintf(stderr,"Nl[%d]=%d\n",i,Nl[i]);fflush(stderr);
}
// read special header file to setup vis5d
if( (vis5dheader=fopen(V5DHEAD_NAME,"rt"))==NULL){
fprintf(stderr,"can't open vis5d header file %s\n","vis5d.head");
exit(1);
}
// header has in it:
// list of names for all variables types in sequence (MUST BE <=9 characters!!!)
// list of min max for each of the variables
// x_start x_finish y_start y_finish z_start z_finish
for(i=0;i<NumVars;i++){
fscanf(vis5dheader,"%s %f %f",VarName[i],&minmax[0][i],&minmax[1][i]);
fprintf(stderr,"VarName[%d of %d]=%s min: %g max: %g\n",i,NumVars-1,VarName[i],minmax[0][i],minmax[1][i]); fflush(stderr);
}
fprintf(stderr,"DONE fscanf1\n"); fflush(stderr);
fscanf(vis5dheader,"%f",&pos[1][1]);
fscanf(vis5dheader,"%f",&pos[1][2]);
fscanf(vis5dheader,"%f",&pos[2][1]);
fscanf(vis5dheader,"%f",&pos[2][2]);
fscanf(vis5dheader,"%f",&pos[3][1]);
fscanf(vis5dheader,"%f",&pos[3][2]);
fprintf(stderr,"DONE fscanf2: %f %f %f %f %f %f\n",pos[1][1],pos[1][2],pos[2][1],pos[2][2],pos[3][1],pos[3][2]); fflush(stderr);
while(fgetc(vis5dheader)!='\n'); // skip rest of line
fprintf(stderr,"DONE fgetc\n"); fflush(stderr);
for(i=0;i<NumTimes;i++){
TimeStamp[i]=i%60+((i/60)%60)*100+(i/(60*60))*10000;
//fprintf(stderr,"ts: %06d\n",TimeStamp[i]); fflush(stderr);
DateStamp[i]=99036;
}
}
fprintf(stderr,"DONE fscanfs\n"); fflush(stderr);
if((dest==5)||(source==5)){
if(source==0 || dest==0) CompressMode=1; // 1,2,4 bytes per grid point
else CompressMode=4; // 1,2,4 bytes per grid point (assume want more precision if data)
// in general, CompressMode==2 can be bad for vector tracing when vectors vary alot away from (say) central BH
// CompressMode=1; // override
Projection=0; // 0=linear, rectangular, generic units 1=linear, rectangular,cylindrical-equidistant,2=Lambert Conformal, 3=Stereographic, 4=Rotated
// For Projection=0, comments below are true for each ProjArgs[?]
ProjArgs[0]=pos[1][2]; // North bondary of 3D box
ProjArgs[1]=pos[2][2]; // West boundary of 3D box
ProjArgs[2]=(pos[1][2]-pos[1][1])/(float)(Nr-1); // increment between rows
ProjArgs[3]=(pos[2][2]-pos[2][1])/(float)(Nc-1); // increment between columns
Vertical=0; // 0=equally spaced in generic units 1=equally spaced in km 2=unequally spaced in km 3=unequally spaced in mb
VertArgs[0]=pos[3][1]; // height of bottom level
VertArgs[1]=(pos[3][2]-pos[3][1])/(float)(Nl[0]-1); // spacing between levels
// if use Vertical==3, then do something like:
// for (il=0; il< Nl[il]; il++) VertArgs[il] = 1000.0 - 40.0 * il;
// see convert/test.c in source code
fprintf(stderr,"Args: %g %g : %g %g: %g %g\n",ProjArgs[0],ProjArgs[1],ProjArgs[2],ProjArgs[3],VertArgs[0],VertArgs[1]); fflush(stderr);
if(dest==5){
fclose(vis5dheader); // no longer needed
}
}
fprintf(stderr,"DONE ProJVert\n"); fflush(stderr);
#endif
/////////////////
//
// see if TS>1 and get filename list
//
// after done, input and output names will be formed from these lists if inputlist or outputlist are non-zero
////////////////
outputlist=inputlist=0; //default
if(TS>1){
// then ignore argument file input/output names and get from list
// order is ALL input names in 1 file, ALL output names in another file
if((dest==5 && source!=5) || (input4dblock==0)&&(dest==0 || dest==1 || dest==2 || dest==3 || dest==4)){ // otherwise 1 file
fprintf(stderr,"Opening %s\n",INPUTLIST_NAME);
if( (inputfilenamelist=fopen(INPUTLIST_NAME,"rt"))==NULL){
fprintf(stderr,"can't open input filenamelist header file %s\n",INPUTLIST_NAME);
exit(1);
}
inputlist=1;
outputlist=0;
}
else if((dest!=5 && source==5) || (input4dblock==1)&&(source==0 || source==1 || source==2)){ // otherwise 1 file (can't source 4D from HDF yet)
fprintf(stderr,"Opening %s\n",OUTPUTLIST_NAME);
if( (outputfilenamelist=fopen(OUTPUTLIST_NAME,"rt"))==NULL){
fprintf(stderr,"can't open output filenamelist header file %s\n",OUTPUTLIST_NAME);
exit(1);
}
inputlist=0;
outputlist=1;
}
else{
fprintf(stderr,"Not setup to process TS=%d>1 with source=%d and dest=%d\n",TS,source,dest);
exit(1);
}
}
/////////////////////
//
// HDF stuff
//
/////////////////////
#if(HDF)
if((source==3)||(dest==3)||(source==4)||(dest==4)){ // use maximal holder array (doubles)
if(HDFTYPE==DFNT_UINT8){
arrayi=(int*)malloc(sizeof(int)*numcolumns*N1*N2*N3); // (DIM0->N1, DIM1->N2 where array[DIM0][DIM1])
array=arrayi;
}
if(HDFTYPE==DFNT_INT32){
arrayi=(int32*)malloc(sizeof(int32)*numcolumns*N1*N2*N3); // (DIM0->N1, DIM1->N2 where array[DIM0][DIM1])
array=arrayi;
}
if(HDFTYPE==DFNT_FLOAT32){
arrayf=(float32*)malloc(sizeof(float32)*numcolumns*N1*N2*N3); // (DIM0->N1, DIM1->N2 where array[DIM0][DIM1])
array=arrayf;
}
if(HDFTYPE==DFNT_FLOAT64){
arrayd=(float64*)malloc(sizeof(float64)*numcolumns*N1*N2*N3); // (DIM0->N1, DIM1->N2 where array[DIM0][DIM1])
array=arrayd;
}
if(array==NULL){
fprintf(stderr,"cannot allocate array hdf data\n");
exit(1);
}
if(NDIM==3){
dim_sizes[3] = numcolumns;
dim_sizes[2] = N1;
dim_sizes[1] = N2;
dim_sizes[0] = N3;
rank = 1+NDIM;
edges[3] = numcolumns;
edges[2] = N1;
edges[1] = N2;
edges[0] = N3;
start[0]=start[1]=start[2]=start[3]=0;
}
else if(NDIM==2){
// assum 2d
dim_sizes[2] = numcolumns;
dim_sizes[1] = N1;
dim_sizes[0] = N2;
rank = 1+NDIM;
edges[2] = numcolumns;
edges[1] = N1;
edges[0] = N2;
start[0]=start[1]=start[2]=0;
}
else if(NDIM==1){
// assum 1d
dim_sizes[1] = numcolumns;
dim_sizes[0] = N1;
rank = 1+NDIM;
edges[1] = numcolumns;
edges[0] = N1;
start[0]=start[1]=0;
}
}
#endif
/////////////////////////////
//
// vis5d stuff
//
/////////////////////////////
#if(V5D)
if((source==5)||(dest==5)){
fprintf(stderr,"Allocated memory for source=%d dest=%d\n",source,dest); fflush(stderr);
arrayvisf=(float*)malloc(sizeof(float)*numcolumns*N1*N2*N3); // (DIM0->N1, DIM1->N2 where array[DIM0][DIM1])
arrayvis=arrayvisf;
if(arrayvis==NULL){
fprintf(stderr,"cannot allocate array vis5d data\n");
exit(1);
}
arrayvisoutput=(float*)malloc(sizeof(float)*N1*N2*N3); // (DIM0->N1, DIM1->N2 where array[DIM0][DIM1])
if(arrayvisoutput==NULL){
fprintf(stderr,"cannot allocate array vis5d data: arrayvisoutput\n");
exit(1);
}
}
#endif
///////////
//
//
// BIG LOOP
//
////////////
for (it=0;it<TS;it++) { // loop over timeslices
if(TS>1){ // otherwise already set
if(inputlist) fscanf(inputfilenamelist,"%s",INPUT_NAME); // otherwise 1 file
if(outputlist) fscanf(outputfilenamelist,"%s",OUTPUT_NAME); // otherwise 1 file
fprintf(stderr,"At TS=%d of %d using input file %s and output file %s\n",it,TS,INPUT_NAME,OUTPUT_NAME);
}
fprintf(stderr,"Open source=%d\n",source);
//////////////////////
//
// open source file
//
//////////////////////
if(source==0){
// length of the entire unmodified input file name
lname=strlen(INPUT_NAME);
if( (INPUT_NAME[lname-1]=='z')&&(INPUT_NAME[lname-2]=='g')&&(INPUT_NAME[lname-3]=='.') ){
inputtype=1;
printf("input flagged as gzip\n");
}
else{
inputtype=0;
}
if(inputtype==0){
if( !(input=fopen(INPUT_NAME,"rb"))){
fprintf(stderr,"trouble opening input file: %s\n",INPUT_NAME);
exit(1);
}
}
if(inputtype==1){
sprintf(incommand,"gzip -d < %s",INPUT_NAME);
if( !(input=popen(incommand,"r"))){
fprintf(stderr,"trouble opening input file: %s %s\n",INPUT_NAME,incommand);
exit(1);
}
}
// assume header info provided and # lines of header provided (unlike in r8toras.c and block2tile.c)
}
else if(source==1){
if( (input=fopen(INPUT_NAME,"rb"))==NULL){
fprintf(stderr,"cannot open %s\n",INPUT_NAME);
exit(1);
}
}
else if(source==2){
if( (input=fopen(INPUT_NAME,"rt"))==NULL){
fprintf(stderr,"cannot open %s\n",INPUT_NAME);
exit(1);
}
}
#if(HDF)
else if(source==3){
// open HDF file
sd_id=SDstart(INPUT_NAME,DFACC_READ);
if (sd_id != FAIL) printf ("Reading HDF file with READ access\n");
sd_index = 0;
sds_id = SDselect (sd_id, sd_index);
istat = SDreaddata (sds_id, start, NULL, edges, (VOIDP) array);
}
else if(source==4){
// not yet
fprintf(stderr,"NOT YET\n"); exit(1);
}
#endif
#if(V5D)
else if((source==5)&&(it==it)){ // No longer assume: // assume all input vis5d are multi-timed
// not yet
// fprintf(stderr,"NOT YET\n"); exit(1);
v5dstruct v;
int time,var;
float *data;
float min, max, sum, sumsum;
int missing, good;
// code pulled from v5dstats.c
if (!v5dOpenFile( INPUT_NAME, &v )) {
printf("Error: couldn't open %s for reading\n", INPUT_NAME );
exit(0);
}
if(v.NumTimes>1){
fprintf(stderr,"Not quite setup for multi-timed inputs since need to increase size of array to have time dimension\n");
fflush(stderr);
exit(1);
}
// assume same size for all times and variables
int sizeproblem=0;
for (time=0; time<v.NumTimes; time++) {
for (var=0; var<v.NumVars; var++) {
// v5d data size
int nrncnl;
nrncnl = v.Nr * v.Nc * v.Nl[var];
if(nrncnl!=N1*N2*N3){
fprintf(stderr,"Memory created was per-time per-variable of size %d while v5d file had %d\n",N1*N2*N3,nrncnl);
fflush(stderr);
sizeproblem++;
}
else{
// debug:
// fprintf(stderr,"time=%d var=%d size=%d\n",time,var,nrncnl); fflush(stderr);
// read data into array
// data = (float *) malloc( nrncnl * sizeof(float) );
data=arrayvisoutput;
if (!v5dReadGrid( &v, time, var, data )) {
printf("Error while reading grid (time=%d,var=%s)\n", time+1, v.VarName[var] );
exit(0);
}
// min = MISSING;
// max = -MISSING;
// missing = 0;
// good = 0;
// sum = 0.0;
// sumsum = 0.0;
//
// for (i=0;i<nrncnl;i++) {
// /*
// if (data[i]!=data[i]) {
// printf("bad: %g\n", data[i]);
// }
// */
// if ( IS_MISSING(data[i]) ) {
// missing++;
// }
// else {
// good++;
// if (data[i]<min) {
// min = data[i];
// }
// if (data[i]>max) {
// max = data[i];
// }
// sum += data[i];
// sumsum += data[i]*data[i];
// }
// }
if(dest==0){ // then use min/max conversion for decent legend (at least for fixed scaled data)
// also assumes linear legend!
a=minmax[0][var];
b=minmax[1][var];
}
else{ // to cancel change
a=0.0;
b=255.0;
}
// loop over spatial dimensions // for (i=0;i<nrncnl;i++)
for(k=0;k<N3;k++) for(j=0;j<N2;j++) for(i=0;i<N1;i++){
ijkjon=(i+(j+k*N2)*N1)*(v.NumVars) + var; // so var (columns) is fastest
ijkvis5d=(N3-1-k) + ((j) + (i) * N2) * N3;
arrayvisf[ijkjon] = (arrayvisoutput[ijkvis5d]-a)*255.0/(b-a);
// fprintf(stderr,"k=%d j=%d i=%d : var=%d : ijkjon=%d\n",k,j,i,var,ijkjon);
}
// free( data );
// if (good==0) {
// /* all missing */
// printf("%4d %-8s %-5s all missing values\n",
// time+1, v.VarName[var], v.Units[var] );
// }
// else {
// float mean = sum / good;
// float tmp = (sumsum - sum*sum/good) / (good-1);
// float sd;
// if (tmp<0.0) {
// sd = 0.0;
// }
// else {
// sd = sqrt( tmp );
// }
// printf("%4d %-8s %-5s %13g%13g%13g%13g %4d\n",
// time+1, v.VarName[var], v.Units[var],
// min, max, mean, sd, missing );
// }
}// end else if ok to read i,j,k block of data
}// end loop over reading variables
}// end loop over reading times
if(sizeproblem!=0){
fprintf(stderr,"Size problem: %d\n",sizeproblem);
fflush(stderr);
exit(1);
}
v5dCloseFile( &v );
fprintf(stderr,"Closing vis5d+ file. Done reading vis5d+ file into array\n");
fflush(stderr);
}
#endif
// fprintf(stderr,"Deal with header: pipeheader=%d\n",pipeheader);
//////////////////////
//
// trial open dest file when processing input list out to 1 file
//
//////////////////////
if(it==0 && (it==0 && outputlist==0 && inputlist==1 || outputlist==0 && inputlist==0)){
if( !(output=fopen(OUTPUT_NAME,"wt"))){
fprintf(stderr,"trouble opening output file: %s\n",OUTPUT_NAME);
exit(1);
}
fclose(output);
}
/////////////////////////
//
// deal with header (when doing inputlist, note that this only pipes first input file into any output file)
//
if(pipeheader && (it==0 && outputlist==0 && inputlist==1 || outputlist==0 && inputlist==0)){
if( !(output=fopen(OUTPUT_NAME,"wt"))){
fprintf(stderr,"trouble opening output file: %s\n",OUTPUT_NAME);
exit(1);
}
}
for(i=0;i<realnumheader;i++){
//printf("headerline: %i\n",i);
while((ch=fgetc(input))!='\n'){
if(pipeheader) fputc(ch,output);
//printf("%c",ch);
}
}
// close header part if opened
if(pipeheader && (it==0 && outputlist==0 && inputlist==1 || outputlist==0 && inputlist==0) ){
fprintf(output,"\n");
fclose(output);
}
///////////////////////////
//
// open destination file
//
///////////////////////////
if(dest==0 && ( (it==0 && outputlist==0 && inputlist==1 || outputlist==0 && inputlist==0) || outputlist==1) ){
fprintf(stderr,"Open dest=%d\n",dest);
lname=strlen(OUTPUT_NAME);
if( (OUTPUT_NAME[lname-1]=='z')&&(OUTPUT_NAME[lname-2]=='g')&&(OUTPUT_NAME[lname-3]=='.') ){
outputtype=1;
printf("output flagged as gzip\n");
}
else{
outputtype=0;
}
// open output file
if(outputtype==0){
if( !(output=fopen(OUTPUT_NAME,"ab"))){
fprintf(stderr,"trouble opening output file: %s\n",OUTPUT_NAME);
exit(1);
}
}
if(outputtype==1){
sprintf(outcommand,"gzip > %s",OUTPUT_NAME);
if( !(output=popen(outcommand,"w"))){
fprintf(stderr,"trouble opening output file: %s %s\n",OUTPUT_NAME,outcommand);
exit(1);
}
}
}
else if(dest==1 && ( (it==0 && outputlist==0 && inputlist==1 || outputlist==0 && inputlist==0) || outputlist==1) ){
fprintf(stderr,"Open dest=%d\n",dest);
if( (output=fopen(OUTPUT_NAME,"at"))==NULL){
fprintf(stderr,"cannot open %s\n",OUTPUT_NAME);
exit(1);
}
}
else if(dest==2 && ( (it==0 && outputlist==0 && inputlist==1 || outputlist==0 && inputlist==0) || outputlist==1) ){
if( (output=fopen(OUTPUT_NAME,"at"))==NULL){
fprintf(stderr,"cannot open %s\n",OUTPUT_NAME);
exit(1);
}
}
#if(HDF)
else if(dest==3 && ( (it==0 && outputlist==0 && inputlist==1 || outputlist==0 && inputlist==0) || outputlist==1) ){
fprintf(stderr,"Open dest=%d\n",dest);
// open HDF file
sd_id = SDstart (OUTPUT_NAME, DFACC_CREATE);
// can change to other output types
sds_id = SDcreate (sd_id, SDS_NAME, HDFTYPE, rank, dim_sizes);
start[0]=start[1]=start[2]=start[3]=0;
}
else if(dest==4 && ( (it==0 && outputlist==0 && inputlist==1 || outputlist==0 && inputlist==0) || outputlist==1) ){
fprintf(stderr,"Open dest=%d\n",dest);
// not yet
fprintf(stderr,"NOT YET\n"); exit(1);
}
#endif
#if(V5D)
else if((dest==5)&&(it==0)){ // assume all output vis5d are multi-timed, so only open 1 file for all timeslices
fprintf(stderr,"Open dest=%d\n",dest);
fprintf(stderr,"Opening vis5d file: %s %d %d %d %d %d %s\n",OUTPUT_NAME,NumTimes,NumVars,Nr,Nc,Nl[0],VarName[0]); fflush(stderr);