-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgaddes.c
2300 lines (1946 loc) · 61 KB
/
gaddes.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
/* Copyright (c) 1987-1995 by Brian Doty. All Rights Reserved.
See file COPYRIGHT for liability information.
Permission is granted for usage of source code on an individual
basis only. You are in violation of copyright law if you have
possession of this source code without specific permission from
Brian Doty. This source code may not be redistributed in any way,
in whole or in part. You may use this source code to:
- understand GrADs functionality
- make minor enhancements to the GrADS program
- correct bugs in the GrADs program
All other uses ARE PROHIBITED, without specific written permission
for such use. You agree that if you make any modifications to the
source code, you will provide such modifications to Brian Doty for
possible inclusion into the distributed version of GrADS (attribution
will be made if requested). */
#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <malloc.h>
#include <string.h>
#include "grads.h"
/*mf 961205 --- expose Mike Fiorino's global struct to these routines for 365 day calandars mf*/
extern struct gamfcmn mfcmn;
/*mf 961205 --- expose Mike Fiorino's global struct to these routines for 365 day calandars mf*/
static char pout[512];
FILE *descr; /* File descriptor pointer */
FILE *pdfi; /* File descriptor for pdef file */
void ll2eg (int , int, float *, float, float, float *, float *, float *) ;
void ll2pse(int, int, float *, float, float, float *, float *);
void ll2ops (float *, float , float , float *, float *) ;
/* Read a GrADS data descriptor file and fill in the information
into a gafile structure. The gafile structure should be
allocated and must be initialized by getpfi. If this routine
returns an error, release the pfi structure and allocated
storage via frepfi. mflag indicates whether to read the
stnmap/index file; if this routine is being called to
preprocess the dd file then this flag should be 0. */
int gaddes (char *name, struct gafile *pfi, int mflag) {
struct gavar *pvar;
struct dt tdef,tdefi,dt1,dt2;
struct gaindx *pindx;
float *vals;
int size,rc,len,swpflg,cnt;
char rec[512], mrec[512], *ch, *dn, *pos, *sname ; /*mf mf*/
int flgs[8],cflg,i,j,err,hdrb,trlb,mflflg,crayflg=0;
int mcnt,maxlv,maxct;
int levs,acum,acumvz,fpos,recacm;
float temp,v1,v2;
FILE *mfile;
char pdefnm[256];
int pdefop1, pdefop2;
/*mf .... mf*/
int nzstride=0,first=1;
int levsua=0,acumstride=0;
unsigned char vermap;
int idum;
float fdum;
unsigned char urec[4];
int diag=0;
/*mf .... mf*/
static char *errs[9] = {"XDEF","YDEF","ZDEF","TDEF","UNDEF",
"DSET","VARS","TITLE","DTYPE"};
#if GRADS_CDUNIF == 1
mfcmn.fullyear=1; /*mf --- define here vice grads.c for cdunif.c mf*/
#endif
hdrb = 0;
trlb = 0;
pdfi = NULL;
mflflg = 0; /* map file not open */
pfi->mfile = NULL;
mcnt = -1;
descr = fopen (name, "r");
/* default suffix of .ctl */
sname=NULL;
if (descr == NULL) {
sname = (char *)malloc(strlen(name)+5);
if(sname == NULL) {
gaprnt(0,"malloc error in creating date descriptor file name\n");
return(1);
}
for(i=0;i<=strlen(name);i++) *(sname+i)=*(name+i);
strcat(sname,".ctl");
descr = fopen (sname, "r");
}
if (descr==NULL) {
gaprnt (0,"Open Error: Can't open description file\n");
return(1);
}
if(sname !=NULL) {
getwrd (pfi->dnam,sname,256);
free(sname);
} else {
getwrd (pfi->dnam,name,256);
}
/* initialize error flags */
for (i=0;i<8;i++) flgs[i] = 1;
/*mf initialize the calendar to standard and cray_ieee to 0 mf*/
pfi->calendar=0;
pfi->cray_ieee=0;
/*mf---------mf*/
/* parse the dd file */
while (fgets(rec,512,descr)!=NULL) {
strcpy (mrec,rec);
lowcas(rec);
if ( (cmpwrd("*",rec)) || !isalnum(rec[0]) ) {
cflg = 1;
} else if (cmpwrd("byteswapped",rec)) {
pfi->bswap = 1;
} else if (cmpwrd("fileheader",rec)) {
if ( (ch=nxtwrd(rec))==NULL ) {
gaprnt (1,"Description file warning: Missing fileheader length\n");
} else {
ch = intprs(ch,&(pfi->fhdr));
if (ch==NULL) {
gaprnt (1,"Fileheader record invalid\n");
pfi->fhdr = 0;
}
}
} else if (cmpwrd("xyheader",rec)) {
if ( (ch=nxtwrd(rec))==NULL ) {
gaprnt (1,"Description file warning: Missing xy grid header length\n");
} else {
ch = intprs(ch,&(pfi->xyhdr));
if (ch==NULL) {
gaprnt (1,"xy grid header length invalid\n");
pfi->xyhdr = 0;
} else {
pfi->xyhdr = pfi->xyhdr / sizeof(float);
}
}
} else if (cmpwrd("theader",rec)) {
if ( (ch=nxtwrd(rec))==NULL ) {
gaprnt (1,"Description file warning: Missing time chunk header length\n");
} else {
ch = intprs(ch,&(pfi->thdr));
if (ch==NULL) {
gaprnt (1,"time chunk grid header length invalid\n");
pfi->thdr = 0;
} else {
pfi->thdr = pfi->thdr / sizeof(float);
}
}
} else if (cmpwrd("format",rec) || cmpwrd("options",rec)) {
if ( (ch=nxtwrd(rec))==NULL ) {
gaprnt (1,"Description file warning: Missing options keyword\n");
} else {
while (ch!=NULL) {
if (cmpwrd("sequential",ch)) pfi->seqflg = 1;
else if (cmpwrd("yrev",ch)) pfi->yrflg = 1;
else if (cmpwrd("zrev",ch)) pfi->zrflg = 1;
else if (cmpwrd("template",ch)) pfi->tmplat = 1;
else if (cmpwrd("byteswapped",ch)) pfi->bswap = 1;
else if (cmpwrd("cray_32bit_ieee",ch) && GRADS_CRAY) pfi->cray_ieee = 1;
else if (cmpwrd("cray_32bit_ieee",ch) && !GRADS_CRAY) pfi->cray_ieee = 0;
else if (cmpwrd("365_day_calendar",ch)) pfi->calendar=1;
else if (cmpwrd("big_endian",ch)) {
if (!BYTEORDER) pfi->bswap = 1;
}
else if (cmpwrd("little_endian",ch)) {
if (BYTEORDER) pfi->bswap = 1;
}
else {
gaprnt (0,"Open Error: Data file type invalid\n");
goto err9;
}
ch = nxtwrd(ch);
}
}
} else if (cmpwrd("trailerbytes",rec)) {
if ( (ch=nxtwrd(rec))==NULL ) {
gaprnt (1,"Trailerbytes record invalid\n");
} else {
ch = intprs(ch,&trlb);
if (ch==NULL) {
gaprnt (1,"Trailerbytes record invalid\n");
trlb = 0;
} else {
trlb = trlb/4;
}
}
} else if (cmpwrd("headerbytes",rec)) {
if ( (ch=nxtwrd(rec))==NULL ) {
gaprnt (1,"Headerbytes record invalid\n");
} else {
ch = intprs(ch,&hdrb);
if (ch==NULL) {
gaprnt (1,"Headerbytes record invalid\n");
hdrb = 0;
} else {
hdrb = hdrb/4;
}
}
} else if (cmpwrd("dtype",rec)) {
if ( (ch=nxtwrd(rec))==NULL ) {
gaprnt (1,"Description file warning: Missing data type\n");
gaprnt (1," Assuming data file type is grid\n");
}
if (cmpwrd("station",ch)) {
pfi->type = 2;
flgs[0] = 0;
flgs[1] = 0;
flgs[2] = 0;
} else if (cmpwrd("grib",ch)) {
pfi->idxflg = 1;
if ( (ch=nxtwrd(ch))!=NULL ) {
if ( intprs(ch,&(pfi->grbgrd))==NULL) {
gaprnt (1,"Description file warning: Invalid GRIB option\n");
pfi->grbgrd = -999;
}
}
} else {
gaprnt (0,"Open Error: Data file type invalid\n");
goto err9;
}
} else if (cmpwrd("title",rec)) {
if ( (ch=nxtwrd(mrec))==NULL ) {
gaprnt (1,"Description file warning: Missing title string\n");
} else {
getstr (pfi->title,ch,79);
flgs[7] = 0;
}
} else if (cmpwrd("dset",rec)) {
ch = nxtwrd(mrec);
if (ch==NULL) {
gaprnt (0,"Open Error: Data file name is missing\n");
goto err9;
}
if (*ch=='^' || *ch=='$') {
fnmexp (pfi->name,ch,name);
} else {
getwrd (pfi->name,ch,256);
}
flgs[5] = 0;
} else if (cmpwrd("stnmap",rec) || cmpwrd("index",rec)) {
/*mf -----
map file processing -- gribmap
-----mf*/
if (cmpwrd("index",rec)) pfi->idxflg = 1;
ch = nxtwrd(mrec);
if (ch==NULL) {
gaprnt (0,"Open Error: Station or Index Map file name is missing\n");
goto err9;
}
if (*ch=='^' || *ch=='$') {
fnmexp (pout, ch, name);
} else {
getwrd (pout,ch,100);
}
len = 0;
while (*(pout+len)) len++;
pfi->mnam = (char *)malloc(len+3);
if (pfi->mnam==NULL) goto err8;
strcpy (pfi->mnam,pout);
if (mflag) {
mfile = fopen (pout, "rb");
if (mfile==NULL) {
gaprnt (0,"Open Error: Can't open Station/Index map file ");
gaprnt (0,pout);
gaprnt (0,"\n");
goto err9;
}
mflflg = 1;
swpflg = 0;
/*---
GRIB data - load the map
---*/
if (pfi->idxflg) {
pindx = (struct gaindx *)malloc(sizeof(struct gaindx));
if (pindx==NULL) goto err8;
pfi->pindx = pindx;
/*
check the version number
*/
fseek(mfile,1,0);
fread(&vermap,sizeof(unsigned char),1,mfile);
if(diag) printf("ddd gribmap vermap =%d\n",vermap);
if(vermap == 2) {
fseek(mfile,2,0);
fread(mrec,sizeof(unsigned char),4,mfile);
pindx->hinum=gagby(mrec,0,4);
if(diag) printf("ddd hinum = %d\n",pindx->hinum);
fread(mrec,sizeof(unsigned char),4,mfile);
pindx->hfnum=gagby(mrec,0,4);
if(diag) printf("ddd hfnum = %d\n",pindx->hfnum);
fread(mrec,sizeof(unsigned char),4,mfile);
pindx->intnum=gagby(mrec,0,4);
if(diag) printf("ddd intnum = %d\n",pindx->intnum);
fread(mrec,sizeof(unsigned char),4,mfile);
pindx->fltnum=gagby(mrec,0,4);
if(diag) printf("ddd fltnum = %d\n",pindx->fltnum);
/* skip the begining time struct info */
fread(mrec,sizeof(unsigned char),7,mfile);
pindx->hipnt = NULL;
pindx->hfpnt = NULL;
pindx->intpnt = NULL;
pindx->fltpnt = NULL;
if (pindx->hinum>0) {
pindx->hipnt = (int *)malloc(sizeof(int)*pindx->hinum);
if (pindx->hipnt==NULL) goto err8;
for(i=0;i<pindx->hinum;i++) {
fread(mrec,sizeof(unsigned char),4,mfile);
idum=gagby(mrec,0,4);
if(gagbb(mrec,0,1)) idum=-idum;
*(pindx->hipnt+i)=idum;
if(diag) printf("ddd 1 i = %d pindx->hipnt = %d\n",i,idum);
}
}
if (pindx->hfnum>0) {
pindx->hfpnt = (float *)malloc(sizeof(float)*pindx->hfnum);
if (pindx->hfpnt==NULL) goto err8;
fread (pindx->hfpnt,sizeof(float),pindx->hfnum,mfile);
}
if (pindx->intnum>0) {
pindx->intpnt = (int *)malloc(sizeof(int)*pindx->intnum);
if (pindx->intpnt==NULL) goto err8;
for(i=0;i<pindx->intnum;i++) {
fread(mrec,sizeof(unsigned char),4,mfile);
idum=gagby(mrec,0,4);
if(gagbb(mrec,0,1)) idum=-gagbb(mrec,1,31);
*(pindx->intpnt+i)=idum;
if(diag) printf("ddd 2 i = %d pindx->intpnt = %d\n",i,idum);
}
}
if (pindx->fltnum>0) {
pindx->fltpnt = (float *)malloc(sizeof(float)*pindx->fltnum);
if (pindx->fltpnt==NULL) goto err8;
for(i=0;i<pindx->fltnum;i++) {
fread(urec,sizeof(unsigned char),4,mfile);
fdum=ibm2flt(urec);
*(pindx->fltpnt+i)=fdum;
if(diag) printf("ddd 3 i = %d pindx->fltpnt = %g\n",i,fdum);
}
}
} else {
fseek (mfile,0L,0);
fread (pindx,sizeof(struct gaindx),1,mfile);
if (pindx->type>>24 > 0) swpflg=1;
if (swpflg) gabswp((float *)pindx,5);
pindx->hipnt = NULL;
pindx->hfpnt = NULL;
pindx->intpnt = NULL;
pindx->fltpnt = NULL;
if (pindx->hinum>0) {
pindx->hipnt = (int *)malloc(sizeof(int)*pindx->hinum);
if (pindx->hipnt==NULL) goto err8;
fread (pindx->hipnt,sizeof(int),pindx->hinum,mfile);
if (swpflg) gabswp((float *)(pindx->hipnt),pindx->hinum);
}
if (pindx->hfnum>0) {
pindx->hfpnt = (float *)malloc(sizeof(float)*pindx->hfnum);
if (pindx->hfpnt==NULL) goto err8;
fread (pindx->hfpnt,sizeof(float),pindx->hfnum,mfile);
if (swpflg) gabswp(pindx->hfpnt,pindx->hfnum);
}
if (pindx->intnum>0) {
pindx->intpnt = (int *)malloc(sizeof(int)*pindx->intnum);
if (pindx->intpnt==NULL) goto err8;
fread (pindx->intpnt,sizeof(int),pindx->intnum,mfile);
if (swpflg) gabswp((float *)(pindx->intpnt),pindx->intnum);
}
if (pindx->fltnum>0) {
pindx->fltpnt = (float *)malloc(sizeof(float)*pindx->fltnum);
if (pindx->fltpnt==NULL) goto err8;
fread (pindx->fltpnt,sizeof(float),pindx->fltnum,mfile);
if (swpflg) gabswp(pindx->fltpnt,pindx->fltnum);
}
}
/*mf -----
map file processing -- stnmap
-----mf*/
} else {
/*mf version detection mf*/
fread(rec,1,16,mfile); /* minimum map file is 16 bytes */
vermap=1;
if(strncmp(rec,"GrADS_stnmapV002",16) == 0) {
vermap=2;
}
if(vermap == 2) {
fread(mrec,sizeof(unsigned char),4,mfile);
idum=gagby(mrec,0,4);
if(gagbb(mrec,0,1)) idum=-idum;
mcnt=idum;
fread(mrec,sizeof(unsigned char),4,mfile);
idum=gagby(mrec,0,4);
if(gagbb(mrec,0,1)) idum=-idum;
maxlv=idum;
pfi->tstrt = (int *)malloc(sizeof(int)*mcnt);
pfi->tcnt = (int *)malloc(sizeof(int)*mcnt);
for(i=0;i<mcnt;i++) {
fread(mrec,sizeof(unsigned char),4,mfile);
idum=gagby(mrec,0,4);
if(gagbb(mrec,0,1)) idum=-idum;
*(pfi->tstrt+i)=idum;
}
for(i=0;i<mcnt;i++) {
fread(mrec,sizeof(unsigned char),4,mfile);
idum=gagby(mrec,0,4);
if(gagbb(mrec,0,1)) idum=-idum;
*(pfi->tcnt+i)=idum;
}
} else if(vermap ==1) {
/* reposition and read two local ints for version 1 map*/
fseek (mfile,0L,0);
fread (&mcnt,sizeof(int),1,mfile);
fread (&maxlv,sizeof(int),1,mfile);
if (maxlv>>24 > 0) swpflg = 1;
if (swpflg) {
gabswp((float *)(&mcnt),1);
gabswp((float *)(&maxlv),1);
}
pfi->tstrt = (int *)malloc(sizeof(int)*mcnt);
pfi->tcnt = (int *)malloc(sizeof(int)*mcnt);
fread (pfi->tstrt,sizeof(int),mcnt,mfile);
fread (pfi->tcnt,sizeof(int),mcnt,mfile);
if (swpflg) {
gabswp((float *)pfi->tstrt,1);
gabswp((float *)pfi->tcnt,1);
}
pfi->mtype = 1;
}
fclose (mfile);
mflflg = 0;
}
} /* END OF mpfile check */
} else if (cmpwrd("toff",rec)) {
ch = nxtwrd(rec);
if (ch==NULL) {
gaprnt (0,"Open Error: Missing toff value\n");
goto err9;
}
pos = intprs(ch,&(pfi->tlpst));
if (pos==NULL || pfi->tlpst>=pfi->dnum[3]) {
gaprnt (0,"Open Error: Invalid toff value\n");
goto err9;
}
pfi->tlpflg = 1;
} else if (cmpwrd("undef",rec)) {
ch = nxtwrd(rec);
if (ch==NULL) {
gaprnt (0,"Open Error: Missing undef value\n");
goto err9;
}
pos = valprs(ch,&(pfi->undef));
if (pos==NULL) {
gaprnt (0,"Open Error: Invalid undef value\n");
goto err9;
}
if (SETMISS) {
pfi->ulow = fabs(pfi->undef/EPSILON);
pfi->uhi = pfi->undef + pfi->ulow;
pfi->ulow = pfi->undef - pfi->ulow;
}
flgs[4] = 0;
} else if (cmpwrd("pdef",rec)) {
if ( (ch = nxtwrd(rec)) == NULL) goto errm;
if ( (pos = intprs(ch,&(pfi->ppisiz)))==NULL) goto errm;
if ( (ch = nxtwrd(ch)) == NULL) goto errm;
if ( (pos = intprs(ch,&(pfi->ppjsiz)))==NULL) goto errm;
if ( (ch = nxtwrd(ch)) == NULL) goto errm;
if (cmpwrd("nps",ch)) {pfi->ppflag=1; pfi->ppwrot=1; cnt=4;}
else if (cmpwrd("sps",ch)) {pfi->ppflag=2; pfi->ppwrot=1; cnt=4;}
else if (cmpwrd("lcc",ch)) {pfi->ppflag=3; pfi->ppwrot=0; cnt=9;}
else if (cmpwrd("eta.u",ch)) {pfi->ppflag=4; pfi->ppwrot=1; cnt=4;}
else if (cmpwrd("pse",ch)) {pfi->ppflag=5; pfi->ppwrot=0; cnt=7;}
else if (cmpwrd("ops",ch)) {pfi->ppflag=6; pfi->ppwrot=0; cnt=8;}
else if (cmpwrd("bilin",ch)) {pfi->ppflag=7; pfi->ppwrot=1; cnt=0;}
else if (cmpwrd("file",ch)) {pfi->ppflag=8; pfi->ppwrot=1; cnt=1;}
else goto errm;
for (i=0; i<cnt; i++) {
if ( (ch = nxtwrd(ch)) == NULL) goto errm;
if ( (pos = valprs(ch,&(pfi->ppvals[i])))==NULL) goto errm;
}
pdefop1 = 0; pdefop2 = 0;
if (pfi->ppflag==8) {
i = (int)(pfi->ppvals[0]+0.1);
if (i<1 || i>8) goto errm;
}
if (pfi->ppflag==7 || pfi->ppflag==8) {
if ( (ch = nxtwrd(ch)) == NULL) goto errm;
if (cmpwrd("stream",ch)) pdefop1 = 1;
else if (cmpwrd("sequential",ch)) pdefop1 = 2;
else goto errm;
if ( (ch = nxtwrd(ch)) == NULL) goto errm;
if (cmpwrd("binary",ch)) pdefop2 = 1;
if (cmpwrd("binary-big",ch)) pdefop2 = 2;
if (cmpwrd("binary-little",ch)) pdefop2 = 3;
else if (cmpwrd("packed",ch)) pdefop2 = 4;
else goto errm;
if ( (ch = nxtwrd(ch)) == NULL) goto errm;
if (*ch=='^' || *ch=='$') {
fnmexp (pdefnm,ch,name);
} else {
getwrd (pdefnm,ch,256);
}
pdfi = fopen(pdefnm,"rb");
if (pdfi==NULL) {
gaprnt (0,"Error opening pdef file\n");
sprintf (pout, " File name is: %s\n",pdefnm);
gaprnt (0,pout);
goto errm;
}
}
} else if (cmpwrd("xdef",rec)) {
if (pfi->type == 2) continue;
if ( (ch = nxtwrd(rec)) == NULL) goto err1;
if ( (pos = intprs(ch,&(pfi->dnum[0])))==NULL) goto err1;
if (*pos!=' ') goto err1;
if ( (ch = nxtwrd(ch))==NULL) goto err2;
if (cmpwrd("linear",ch)) {
rc = deflin(ch, pfi, 0, 0);
if (rc==-1) goto err8;
if (rc) goto err9;
v2 = *(pfi->grvals[0]);
v1 = *(pfi->grvals[0]+1) + v2;
temp = v1+((float)(pfi->dnum[0]))*v2;
temp=temp-360.0;
if (fabs(temp-v1)<0.01) pfi->wrap = 1;
}
else if (cmpwrd("levels",ch)) {
if (pfi->dnum[0]<1) goto err7;
rc = deflev (ch, rec, pfi, 0);
if (rc==-1) goto err8;
if (rc) goto err9;
} else goto err2;
flgs[0] = 0;
} else if (cmpwrd("ydef",rec)) {
if (pfi->type == 2) continue;
if ( (ch = nxtwrd(rec)) == NULL) goto err1;
if ( (pos = intprs(ch,&(pfi->dnum[1])))==NULL) goto err1;
if (*pos!=' ') goto err1;
if ( (ch = nxtwrd(ch))==NULL) goto err2;
if (cmpwrd("linear",ch)) {
rc = deflin(ch, pfi, 1, 0);
if (rc==-1) goto err8;
if (rc) goto err9;
} else if (cmpwrd("levels",ch)) {
if (pfi->dnum[1]<1) goto err7;
rc = deflev (ch, rec, pfi, 1);
if (rc==-1) goto err8;
if (rc) goto err9;
} else if (cmpwrd("gausr40",ch)) {
if ( (ch = nxtwrd(ch))==NULL) goto err3;
if ( (pos = intprs(ch,&i))==NULL) goto err3;
pfi->grvals[1] = gagaus(i,pfi->dnum[1]);
if (pfi->grvals[1]==NULL) goto err9;
pfi->abvals[1] = pfi->grvals[1];
pfi->ab2gr[1] = lev2gr;
pfi->gr2ab[1] = gr2lev;
pfi->linear[1] = 0;
} else if (cmpwrd("mom32",ch)) {
if ( (ch = nxtwrd(ch))==NULL) goto err3;
if ( (pos = intprs(ch,&i))==NULL) goto err3;
pfi->grvals[1] = gamo32(i,pfi->dnum[1]);
if (pfi->grvals[1]==NULL) goto err9;
pfi->abvals[1] = pfi->grvals[1];
pfi->ab2gr[1] = lev2gr;
pfi->gr2ab[1] = gr2lev;
pfi->linear[1] = 0;
} else if (cmpwrd("gausr30",ch)) {
if ( (ch = nxtwrd(ch))==NULL) goto err3;
if ( (pos = intprs(ch,&i))==NULL) goto err3;
pfi->grvals[1] = gags30(i,pfi->dnum[1]);
if (pfi->grvals[1]==NULL) goto err9;
pfi->abvals[1] = pfi->grvals[1];
pfi->ab2gr[1] = lev2gr;
pfi->gr2ab[1] = gr2lev;
pfi->linear[1] = 0;
} else if (cmpwrd("gausr20",ch)) {
if ( (ch = nxtwrd(ch))==NULL) goto err3;
if ( (pos = intprs(ch,&i))==NULL) goto err3;
pfi->grvals[1] = gags20(i,pfi->dnum[1]);
if (pfi->grvals[1]==NULL) goto err9;
pfi->abvals[1] = pfi->grvals[1];
pfi->ab2gr[1] = lev2gr;
pfi->gr2ab[1] = gr2lev;
pfi->linear[1] = 0;
} else if (cmpwrd("gausr15",ch)) {
if ( (ch = nxtwrd(ch))==NULL) goto err3;
if ( (pos = intprs(ch,&i))==NULL) goto err3;
pfi->grvals[1] = gags15(i,pfi->dnum[1]);
if (pfi->grvals[1]==NULL) goto err9;
pfi->abvals[1] = pfi->grvals[1];
pfi->ab2gr[1] = lev2gr;
pfi->gr2ab[1] = gr2lev;
pfi->linear[1] = 0;
} else goto err2;
flgs[1] = 0;
} else if (cmpwrd("zdef",rec)) {
if (pfi->type == 2) continue;
if ( (ch = nxtwrd(rec)) == NULL) goto err1;
if ( (pos = intprs(ch,&(pfi->dnum[2])))==NULL) goto err1;
if (*pos!=' ') goto err1;
if ( (ch = nxtwrd(ch))==NULL) goto err2;
if (cmpwrd("linear",ch)) {
rc = deflin(ch, pfi, 2, 0);
if (rc==-1) goto err8;
if (rc) goto err9;
}
else if (cmpwrd("levels",ch)) {
if (pfi->dnum[2]<1) goto err7;
rc = deflev (ch, rec, pfi, 2);
if (rc==-1) goto err8;
if (rc) goto err9;
} else goto err2;
flgs[2] = 0;
} else if (cmpwrd("tdef",rec)) {
if ( (ch = nxtwrd(rec)) == NULL) goto err1;
if ( (pos = intprs(ch,&(pfi->dnum[3])))==NULL) goto err1;
if (*pos!=' ') goto err1;
if ( (ch = nxtwrd(ch))==NULL) goto err2;
if (cmpwrd("linear",ch)) {
if ( (ch = nxtwrd(ch))==NULL) goto err3;
tdef.yr = -1000;
tdef.mo = -1000;
tdef.dy = -1000;
if ( (pos = adtprs(ch,&tdef,&dt1))==NULL) goto err3;
if (*pos!=' ' || dt1.yr == -1000 || dt1.mo == -1000.0 ||
dt1.dy == -1000) goto err3;
if ( (ch = nxtwrd(ch))==NULL) goto err4;
if ( (pos = rdtprs(ch,&dt2))==NULL) goto err4;
v1 = (dt2.yr * 12) + dt2.mo;
v2 = (dt2.dy * 1440) + (dt2.hr * 60) + dt2.mn;
/*mf --- check if 0 dt ---mf*/
if( (v1 == 0) && (v2 == 0) ) goto err4a;
vals = (float *)malloc(sizeof(float)*8);
if (vals==NULL) goto err8;
*(vals) = dt1.yr;
*(vals+1) = dt1.mo;
*(vals+2) = dt1.dy;
*(vals+3) = dt1.hr;
*(vals+4) = dt1.mn;
*(vals+5) = v1;
*(vals+6) = v2;
*(vals+7) = -999.9;
pfi->grvals[3] = vals;
pfi->abvals[3] = vals;
pfi->linear[3] = 1;
} else goto err2;
flgs[3] = 0;
} else if (cmpwrd("vars",rec)) {
if ( (ch = nxtwrd(rec)) == NULL) goto err5;
if ( (pos = intprs(ch,&(pfi->vnum)))==NULL) goto err5;
size = pfi->vnum * (sizeof(struct gavar) + 7 );
pvar = (struct gavar *)malloc(size);
pfi->pvar1 = pvar;
i = 0;
while (i<pfi->vnum) {
if (fgets(rec,256,descr)==NULL) {
gaprnt (0,"Open Error: Unexpected EOF reading variables\n");
sprintf (pout, "Was expecting %i records. Found %i.\n",
pfi->vnum, i);
gaprnt (2,pout);
goto retrn;
}
strcpy (mrec,rec);
lowcas(rec);
if (cmpwrd("endvars",rec)) {
gaprnt (0,"Open Error: Unexpected ENDVARS record\n");
sprintf (pout, "Was expecting %i records. Found %i.\n",
pfi->vnum, i);
gaprnt (2,pout);
goto err9;
}
getwrd (pvar->abbrv,rec,15);
if ( (ch=nxtwrd(rec))==NULL) goto err6;
if ( (pos=intprs(ch,&(pvar->levels)))==NULL ) goto err6;
if ( (ch=nxtwrd(ch))==NULL) goto err6;
for (j=0;j<4;j++) pvar->units[j] = -999;
j = 0;
while (1) {
if ( (ch=intprs(ch,&(pvar->units[j])))==NULL ) goto err6;
while (*ch==' ') ch++;
if (*ch=='\0' || *ch=='\n') goto err6;
if (*ch!=',') break;
ch++;
while (*ch==' ') ch++;
if (*ch=='\0' || *ch=='\n') goto err6;
j++;
if (j>3) goto err6;
}
getstr (pvar->varnm,mrec+(ch-rec),127);
/*mf...........mf*/
/* initialize the var_z counter for NASA GLA format */
pvar->var_z = 1;
if(pvar->units[0] == -1 && pvar->units[1] == 10 ) {
nzstride++;
}
/* var_t is for DRS var-t transforms */
pvar->var_t = 0;
if(pvar->units[0] == -1 && pvar->units[1] == 20 ) pvar->var_t = 1;
/* x-y transpose for lat/lon vice lon/lat data VERY INEFFICIENT!!! */
pvar->y_x = 0;
if(pvar->units[0] == -1 && pvar->units[1] == 30) pvar->y_x = 1;
/*mf 961126 - integer data types mf*/
pvar->dfrm = 0;
if( (pvar->units[0] == -1 && pvar->units[1] == 40 ) &&
(pvar->units[2] >=1 && pvar->units[2] <= 4 ) ) {
pvar->dfrm= pvar->units[2];
if ( pvar->units[2] == 2 ) pvar->dfrm=2;
if ( pvar->units[2] == 2 && pvar->units[3] == -1) pvar->dfrm=-2;
}
/* 32-bit big endian ieee to cray float */
#if GRADS_CRAY == 1
if( (pvar->units[0]==-1 && pvar->units[1]==50) || crayflg ) pvar->dfrm=8;
#endif
/*mf...........mf*/
i++; pvar++;
}
if (fgets(rec,256,descr)==NULL) {
gaprnt (0,"Open Error: Missing ENDVARS statement.\n");
goto retrn;
}
lowcas(rec);
if (!cmpwrd("endvars",rec)) {
gaprnt (0,"Open Error: Looking for ENDVARS statement\n");
gaprnt (0," Instead, found: \n");
goto err9;
}
flgs[6] = 0;
/*
parse error of .ctl file
*/
} else {
gaprnt (0,"Open Error: Unknown keyword in description file\n");
goto err9;
}
}
err=0;
for (i=0; i<7; i++) {
if (flgs[i]) {
sprintf (pout,"Open Error: missing %s record \n",errs[i]);
gaprnt (0,pout);
err=1;
}
}
if (err) goto retrn;
if (pfi->type>1 && mflag) {
if (mcnt==-1) {
gaprnt (0,"Open Error: missing STNMAP record\n");
err=1;
} else if (mcnt != pfi->dnum[3]) {
gaprnt (0,"Open Error: Inconsistent time count\n");
sprintf (pout," Count in station map file = %li\n",mcnt);
gaprnt (0,pout);
sprintf (pout," Count in descriptor file = %i\n",pfi->dnum[3]);
gaprnt (0,pout);
err=1;
}
}
if (err) goto retrn;
/* Figure out locations of variables within a time group */
pvar = pfi->pvar1;
/*mf------------------mf*/
/*
Grid data
*/
if (pfi->type==1) {
pfi->gsiz = pfi->dnum[0] * pfi->dnum[1];
if (pfi->ppflag) pfi->gsiz = pfi->ppisiz * pfi->ppjsiz;
/*mf
add a constant xy header
mf*/
if (pfi->xyhdr) {
pfi->gsiz = pfi->gsiz + pfi->xyhdr;
}
if (pfi->seqflg) {
pfi->gsiz+=2;
if (hdrb>0) hdrb+=2;
pvar->offset = 1+hdrb;
acum = 1+hdrb;
} else {
pvar->offset = hdrb;
acum = hdrb;
}
levs = pvar->levels;
if (levs==0) levs=1;
pvar->recoff = 0;
recacm = 0;
pvar++;
acumvz=acum;
for (i=1; i<pfi->vnum; i++) {
/* NASA GLA FORMAT CHECKS */
/* upper air fields which var and z are transposed*/
if( (pvar->units[0]==-1) &&
(pvar->units[1]==10) &&
(pvar->units[2]==1) ) {
levsua = pvar->levels;
acum = acum + pfi->gsiz;
pvar->var_z = nzstride;
/* diagnstotic fields AFTER upper air fields which are in GrADS normal order */
} else if( (pvar->units[0]==-1) &&
(pvar->units[1]==10) &&
(pvar->units[2]==2) ) {
if(first) {
acumstride = acumstride + nzstride*levsua*pfi->gsiz;
acum = acumstride + (pvar->levels*pfi->gsiz) ;
first = 0 ;
} else {
acum = acum + (levs*pfi->gsiz);
}
} else if(pvar->var_t) { /* DRS transposition of var and t */
if(pfi->tmplat) { /* time template, read the third unit param
for the size of each chunk in a file */
if(pvar->units[2] != -999) {
acum = acum + levs*(pfi->gsiz)*(pvar->units[1]);
} else {
gaprnt (0,"Using time templat and 4-D variables, # times / file not specified\n");
gaprnt (0,"Defaulting to the number of times in the .ctl file\n");
acum = acum + levs*(pfi->gsiz)*(pfi->dnum[3]);
}
} else {
acum = acum + levs*(pfi->gsiz)*(pfi->dnum[3]);
}
} else { /* simple GrADS */
acum = acum + (levs*pfi->gsiz);
acumstride = acum ;
}
recacm += levs;
pvar->offset = acum;
pvar->recoff = recacm;
levs = pvar->levels;
if (levs==0) levs=1;
pvar++;
}
recacm += levs;
/*mf 960514 correct for case where the last variable is a NASA UA */
pvar--;
if( (pvar->units[0]==-1) &&
(pvar->units[1]==10) &&
(pvar->units[2]==1) ) {
acum = acumvz + recacm*pfi->gsiz;
}else {
acum = acum + (levs*pfi->gsiz);
}
/*mf --------------mf*/
/*
time chunk header; the default is 0
*/
if(pfi->seqflg && pfi->thdr>0) {
pfi->thdr+=2;
}
pfi->tsiz = acum + pfi->thdr;
/*mf --------------mf*/