-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlatsgrib.c
1515 lines (1217 loc) · 36.1 KB
/
latsgrib.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
/* -*-Mode: C;-*-
* Module: LATS GRIB functions
*
* Copyright: 1996, Regents of the University of California
* This software may not be distributed to others without
* permission of the author.
*
* Authors: Bob Drach, Lawrence Livermore National Laboratory
*
* Mike Fiorino, Lawrence Livermore National Laboratory
*
* Version: $Id: latsgrib.c,v 1.21 1996/12/18 22:06:22 fiorino Exp $
*
* Revision History:
*
* $Log: latsgrib.c,v $
* Revision 1.21 1996/12/18 22:06:22 fiorino
* initialize fltpnt in gagmap.c to cover c90 compiler problem
* added support for climatological and 365-day calendars in GrADS
*
* Revision 1.20 1996/12/12 18:39:45 fiorino
* 961212
*
* Mike's changes to parm table
* GraDS updated source
* and improvements in GrADS output to handle yearly data and 365-day calendars
* added gaprnt routine in latsfort.c to aid link with straight GraDS source
*
* Revision 1.19 1996/10/22 19:05:07 fiorino
* latsgrib bug in .ctl creator
*
* Revision 1.18 1996/10/16 22:10:00 drach
* - Added automatic gribmap generation
* - Restricted LATS_GRADS_GRIB convention to one grid per file
*
* Revision 1.17 1996/10/10 23:15:44 drach
* - lats_create filetype changed to convention, with options LATS_PCMDI,
* LATS_GRADS_GRIB, and LATS_COARDS.
* - monthly data defaults to 16-bit compression
* - LATS_MONTHLY_TABLE_COMP option added to override 16-bit compression
* - AMIP II standard parameter file
* - parameter file incorporates GRIB center and subcenter
* - if time delta is positive, check that (new_time - old_time)=integer*delta
*
* Revision 1.16 1996/09/30 18:57:26 drach
* - Relax test for GrADS/GRIB equal-spacing
*
* Revision 1.15 1996/09/25 23:16:04 fiorino
* added grib map code
* corrected convertions of lat/lon dx/dy to ints in the GDS
*
* Revision 1.14 1996/08/20 18:34:08 drach
* - lats_create has a new argument: calendar
* - lats_grid: longitude, latitude dimension vectors are now double
* precision (double, C).
* - lats_vert_dim: vector of levels is now double precision (double,
* C). lats_vert_dim need not be called for single-value/surface
* dimensions, if defined in the parameter table. Multi-valued vertical
* dimensions, such as pressure levels, must be defined with
* lats_vert_dim.
* - lats_var: set level ID to 0 for implicitly defined surface
* dimension.
* - lats_write: level value is double precision (double, C).
* - lats_parmtab: replaces routine lats_vartab.
* - FORTRAN latserropt added: allows program to set error handling
* options.
* - The parameter file format changed.
*
* Revision 1.13 1996/08/12 19:19:47 drach
* - various bug fixes (fiorino)
*
* Revision 1.12 1996/07/20 01:23:26 drach
* - Restored compatible amip2.parms
* - Provide default file comments for GrADS control file title
*
* Revision 1.11 1996/07/17 18:17:26 fiorino
* outout long_name and units to var description in .ctl file
*
* Revision 1.10 1996/07/17 01:04:33 fiorino
* bug in .ctl file for ydef
*
* Revision 1.9 1996/07/12 00:36:25 drach
* - (GRIB) use undefined flag only when set via lats_miss_XX
* - (GRIB) use delta when checking for missing data
* - (GRIB) define maximum and default precision
* - fixed lats_vartab to work correctly.
* - Added report of routine names, vertical dimension types
*
* Revision 1.8 1996/06/27 01:12:27 drach
* - Added setting timestat entry (removed from latsint.c)
*
* Revision 1.7 1996/06/11 21:43:33 fiorino
* version 0.1 -- first version for Bob D to test
*
* Revision 1.6 1996/05/31 18:58:06 fiorino
* v0.0
* before the new stuff from Bob
*
* Revision 1.5 1996/05/10 23:06:22 fiorino
* - test of commit
*
* Revision 1.4 1996/05/10 22:44:40 drach
* - Initial version before GRIB driver added:
* - Made grids, vertical dimensions file-independent
*
* Revision 1.3 1996/05/03 18:59:24 drach
* - Moved vertical dimension definition from lats_var to lats_vert_dim
* - Changed lats_miss_double to lats_miss_float
* - Made time dimension file-dependent, revised lats_write accordingly
* - Added lats_var_nc, lats_vert_dim_nc
* - Allow GRIB-only compilation
* - Added FORTRAN interface
*
* Revision 1.2 1996/04/25 23:32:05 drach
* - Added checks for correct number of times, levels written
* - Stubbed in statistics routines
*
* Revision 1.1 1996/04/25 00:53:00 drach
* Initial repository version
*
*
*/
#define _POSIX_SOURCE 1
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include "fgrib.h"
#include "fgrib_init.h"
#include "latsint.h"
#include "grads.h"
#define GRADS_CLIM_YEAR 2
/*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*/
#define LATS_VERT_UNDEF 99
typedef struct latsgribfile {
FILE *gfi;
FILE *cfi;
latsCompTime time; /* beginning time */
int gbmode;
char ctlname[256];
} latsGribFile;
/*---
prototypes
---*/
int lats_pds_set(latsFile *file, latsVar *var,
int levindex, int timeindex, latsCompTime time, grib_pds *);
int lats_gds_set(latsFile *file, latsVar *var,
int levindex, int timeindex, latsCompTime time, grib_gds_ll *);
void lats_flt2int(float *, int *, int);
/* Generate a time statistic for a GRIB file. Return a pointer to the entry on success,
* 0 on failure.
*/
latsTimeStatEntry* latsTimeStatLookup(latsTimeFreq frequency, int delta, latsTimeStat statistic){
latsTimeStatEntry *stat;
if((stat = (latsTimeStatEntry *)malloc(sizeof(latsTimeStatEntry)))==(latsTimeStatEntry *)0)
return 0;
stat->frequency = frequency;
stat->delta = delta;
stat->stat = statistic;
switch(frequency){
case LATS_YEARLY:
stat->grib_unit = 4;
break;
case LATS_MONTHLY:
stat->grib_unit = 3;
break;
case LATS_WEEKLY:
stat->grib_unit = 2;
break;
case LATS_DAILY:
stat->grib_unit = 2;
break;
case LATS_HOURLY:
stat->grib_unit = 1;
break;
case LATS_FORECAST_HOURLY:
stat->grib_unit = 1;
break;
case LATS_FIXED:
stat->grib_unit = 1;
break;
default:
latsError("GRIB (latsgrib.c) --> Invalid frequency: %d",frequency);
return 0;
}
stat->grib_p3 = stat->grib_p2 = stat->grib_p1 = 0;
switch(statistic){
case LATS_AVERAGE:
stat->grib_p2 = delta;
stat->grib_timerange = 3;
break;
case LATS_INSTANT:
stat->grib_timerange = 0;
break;
case LATS_ACCUM:
stat->grib_p2 = delta;
stat->grib_timerange = 4;
break;
case LATS_OTHER_TIME_STAT:
default:
latsError("GRIB (latsgrib.c) --> Invalid time statistic: %d", statistic);
return 0;
}
if(frequency == LATS_WEEKLY) stat->grib_p2 *= 7;
if(frequency == LATS_FORECAST_HOURLY) stat->grib_timerange=10;
return stat;
}
/*-------------
Close a GRIB file. Returns 1 on success, 0 on failure.
--------------*/
int lats_close_grib(latsFile *file){
FILE *cfi;
FILE *gfi;
latsVar *var;
latsVar *vara[LATS_MAX_PARMS];
latsGribFile *gbfile;
char gmppath[LATS_MAX_PATH];
char gmpfile[LATS_MAX_PATH];
char grbfile[LATS_MAX_PATH];
char *mons[12] = {"jan","feb","mar","apr","may","jun","jul","aug",
"sep","oct","nov","dec"};
char grads_options [3][50];
int i,j,k,j1,j2,ik;
int ni,nj,nk,nt;
int nvara;
float dx,dy,dum1;
float lonb,latb,lone,late;
int tv;
float *glon,*glat;
int nlev,ntype,l1,l2;
float undef;
int xdir=0,ydir=0,zdir=0;
int options_set=0;
int notaucheck;
gbfile = (latsGribFile *)file->depend;
gfi=gbfile->gfi;
if(file->convention != LATS_GRIB_ONLY) {
/*-----------------!!!!!!!!!!!!!!!!
Force gribmap to match by the base DTG ALWAYS
appropriate for AMIP II, but will have to be changed for NWP
!!!!!!!!!!!!!!!!!!-----------*/
if(file->frequency == LATS_FORECAST_HOURLY) {
notaucheck=0;
} else {
notaucheck=1;
}
/*---
set the calendar to support 365 day calendars in LATS_GRADS_GRIB
---*/
mfcmn.cal365=1;
if( !(file->calendar & cdHasLeap) == 0) mfcmn.cal365=0;
cfi=stdout;
cfi=gbfile->cfi;
if(VERB) {
printf("cccccccc nvertdim %d\n",file->nvertdim);
printf("cccccccc nvar %d\n",file->nvar);
printf("cccccccc ngrid %d\n",file->ngrid);
printf("cccccccc ntimewriten %d\n",file->ntimewritten);
}
/*--- calc the grid from the first variable ----*/
var = file->varlist;
ni=var->grid->nlon;
nj=var->grid->nlat;
latb=var->grid->lats[0];
late=var->grid->lats[nj-1];
lonb=var->grid->lons[0];
lone=var->grid->lons[ni-1];
/*--- check if uniform --- */
/* --- single value check, set dx to 0 ---- */
if(ni == 1) {
dx=0.0;
} else {
/*
* 970517 - add fuzz to check of constant grid increment ; for cray
*/
dx=fabs(var->grid->lons[1]-var->grid->lons[0]);
for(i=2;i<ni;i++) {
if( fabs((fabs(var->grid->lons[i]-var->grid->lons[i-1]) - (double)dx )) > LATS_LINEAR_GRID_DELTA*dx) {
dx=-1.0;
break;
}
}
}
if(nj == 1) {
dy=0.0;
} else {
dy=fabs(var->grid->lats[1]-var->grid->lats[0]);
for(j=2;j<nj;j++) {
if( fabs((fabs(var->grid->lats[j]-var->grid->lats[j-1]) - (double)dy )) > LATS_LINEAR_GRID_DELTA*dy) {
dy=-1.0;
break;
}
}
}
/*----- undef is anything you like in GRIB ----*/
undef=1e20;
/*---
pull out the GRIB file name
---*/
j=strlen(file->path)-5;
while( (size_t)j > 0 ) {
if( ( *(file->path+j) == '/' ) || ( *(file->path+j) == '\\' ) ) {
break;
}
j--;
}
if(j==0) {
j1=0;
} else {
j1=j+1;
}
for(j=j1;j<strlen(file->path);j++) {
j2=j-j1;
*(grbfile+j2)=*(file->path+j);
}
*(grbfile+j2+1)='\0';
/*---
pull out the gribmap file name
---*/
strcpy(gmppath, file->path);
strcpy(gmppath+strlen(gmppath)-4, ".gmp");
j=strlen(file->path)-5;
while( (size_t)j > 0 ) {
if( ( *(gmppath+j) == '/' ) || ( *(gmppath+j) == '\\' ) ) {
break;
}
j--;
}
if(j==0) {
j1=0;
} else {
j1=j+1;
}
for(j=j1;j<strlen(gmppath);j++) {
j2=j-j1;
*(gmpfile+j2)=*(gmppath+j);
}
*(gmpfile+j2+1)='\0';
/*---
now set up the data stuff in the .ctl file
---*/
fprintf(cfi,"dset ^%s\n",grbfile);
fprintf(cfi,"title %s\n",file->comments);
fprintf(cfi,"undef %g\n",undef);
fprintf(cfi,"dtype grib\n");
fprintf(cfi,"index ^%s\n",gmpfile);
/*---
options
---*/
strcpy(grads_options[0]," ");
strcpy(grads_options[1]," ");
strcpy(grads_options[2]," ");
strcpy(grads_options[3]," ");
if(latb>late) {
ydir=1;
latb=late;
options_set=1;
strcpy(grads_options[0],"yrev");
}
if(mfcmn.cal365) {
options_set=1;
strcpy(grads_options[1],"365_day_calendar");
}
if(options_set) {
fprintf(cfi,"options %s %s %s %s\n",grads_options[0],
grads_options[1],grads_options[2],grads_options[3]);
}
/*---------- xdef ------------ */
if(dx > 0.0) {
fprintf(cfi,"xdef %d linear %f %f\n",ni,lonb,dx);
} else {
if(ni == 1) {
fprintf(cfi,"xdef %d levels % 7.3f\n",ni,var->grid->lons[0]);
} else {
fprintf(cfi,"xdef %d levels\n",ni);
if(xdir) {
for (i=ni-1;i>=0;i--) {
fprintf(cfi,"% 7.3f ",var->grid->lons[i]);
if((i-ni)%10 == 0) fprintf(cfi,"\n");
}
} else {
for (i=0;i<ni;i++) {
fprintf(cfi,"% 7.3f ",var->grid->lons[i]);
if((i-1)%10 == 0) fprintf(cfi,"\n");
}
}
if(ni%10 != 0) fprintf(cfi,"\n");
}
}
/*---------- ydef ------------ */
if(nj == 1) {
fprintf(cfi,"ydef %d levels % 7.3f\n",nj,var->grid->lats[0]);
} else {
if(dy > 0.0) {
fprintf(cfi,"ydef %d linear %f %f\n",nj,latb,dy);
} else {
fprintf(cfi,"ydef %d levels\n",nj);
if(ydir) {
for (j=nj-1;j>=0;j--) {
fprintf(cfi,"% 7.3f ",var->grid->lats[j]);
if((j-nj)%10 == 0) fprintf(cfi,"\n");
}
} else {
for (j=0;j<nj;j++) {
fprintf(cfi,"% 7.3f ",var->grid->lats[j]);
if((j-1)%10 == 0) fprintf(cfi,"\n");
}
}
if(nj%10 != 0) fprintf(cfi,"\n");
}
}
/*---------- zdef ---------- */
nk=0;
k=0;
ik=0;
for(i=0;i<file->nvertdim;i++) {
if(file->vertlist[i].nlev > k) {
k=file->vertlist[i].nlev;
ik=i;
}
}
if(k == 0) {
fprintf(cfi,"zdef 1 levels 1013\n");
} else {
nk=file->vertlist[ik].nlev;
/*
*
* round the level vealues to ints because GRIB 1 does not
* support storing of floats in the PDS for levels
*
*/
if(nk == 1) {
dum1=(float)file->vertlist[ik].levs[0]+0.5;
fprintf(cfi,"zdef %d levels %d\n",nk,(int)dum1);
} else {
fprintf(cfi,"zdef %d levels\n",nk);
if(file->vertlist[ik].levs[0] <= file->vertlist[ik].levs[nk-1]) zdir=1;
if(zdir) {
for (k=nk-1;k>=0;k--) {
dum1=(float)file->vertlist[ik].levs[k]+0.5;
fprintf(cfi,"%d ",(int)dum1);
if((k-nk)%10 == 0) fprintf(cfi,"\n");
}
} else {
for (k=0;k<nk;k++) {
dum1=(float)file->vertlist[ik].levs[k]+0.5;
fprintf(cfi,"%d ",(int)dum1);
if((k+1)%10 == 0) fprintf(cfi,"\n");
}
}
if(nk%10 != 0) fprintf(cfi,"\n");
}
}
/*---------- tdef ------------ */
if(file->frequency == LATS_FIXED) {
fprintf(cfi,"tdef 1 linear 00Z1jan1 1dy\n");
} else {
/*---
COARDS convention for climo -> my convention for climo year
---*/
if( !(file->calendar & cdStandardCal) ) {
gbfile->time.year = GRADS_CLIM_YEAR;
}
fprintf(cfi,"tdef %d linear %dZ%d%s%d ",file->ndelta,
(int)gbfile->time.hour,
gbfile->time.day,
mons[gbfile->time.month-1],
gbfile->time.year );
if(file->frequency == LATS_HOURLY) fprintf(cfi," %dhr\n",file->delta);
if(file->frequency == LATS_FORECAST_HOURLY) fprintf(cfi," %dhr\n",file->delta);
if(file->frequency == LATS_DAILY) fprintf(cfi," %ddy\n",file->delta);
if(file->frequency == LATS_WEEKLY) fprintf(cfi," %ddy\n",file->delta*7);
if(file->frequency == LATS_MONTHLY) fprintf(cfi," %dmo\n",file->delta);
if(file->frequency == LATS_YEARLY) fprintf(cfi," %dyr\n",file->delta);
}
/*----------- vars ------------ */
fprintf(cfi,"vars %d\n",file->nvar);
/*
*
* save var struct so we can reverse order (the way we defined the vars)
* to the ctl file
*
*/
nvara=file->nvar-1;
for(var = file->varlist; var; var = var->next){
vara[nvara]=var;
nvara--;
}
/*
* original version
*
* for(var = file->varlist; var; var = var->next){
*
* write out the vars the way we defined them
*/
for( nvara=0 ; nvara < file->nvar ; nvara++ ){
var=vara[nvara];
/* If the vertical dimension was declared explicitly
via lats_vert_dim ...*/
if(var->vertdim != NULL){
ntype=var->vertdim->type->gribid;
l1=var->vertdim->type->grib_p1;
l2=var->vertdim->type->grib_p2;
}
/* Else if the vertical dimension is implicit, via the
leveltype of the variable in the parameter table ...*/
else if(var->parm->levelset == 1){
ntype=var->parm->verttype->gribid;
if(var->parm->verttype->grib_p3 != 0) {
l1=var->parm->verttype->grib_p3;
l2=0;
} else {
l1=var->parm->verttype->grib_p1;
l2=var->parm->verttype->grib_p2;
}
}
/* Else no vertical dimension */
else{
ntype=LATS_VERT_UNDEF;
l1=0;
l2=0;
}
if(var->parm->levelset == 1 ) {
nlev=0;
if(var->parm->verttype->grib_p3 != 0) {
fprintf(cfi,"%-8s %2d %3d,%3d,%3d %s [%s]\n",
var->name,nlev,var->parm->id,
ntype,l1, /*----- set above -----*/
var->parm->title,var->parm->units);
} else {
fprintf(cfi,"%-8s %2d %3d,%3d,%3d,%3d %s [%s]\n",
var->name,nlev,var->parm->id,
ntype,l1,l2,
var->parm->title,var->parm->units);
}
} else {
fprintf(cfi,"%-8s %2d %3d,%3d %s [%s]\n",
var->name,var->nlev,var->parm->id,ntype,
var->parm->title,var->parm->units);
}
}
fprintf(cfi,"endvars\n");
/*------ close ------*/
fclose(gfi);
fclose(cfi);
/*----- run gribmap ----*/
latsgribmap(gbfile->ctlname,notaucheck);
return 1;
/*--- only create GRIB data ---- */
} else {
fclose(gfi);
return 1;
}
}
/*----------------------------------------
*
* Create a GRIB file. Returns the file ID, or 0 on error
*
---------------------------------------*/
int lats_create_grib(latsFile *file){
FILE *gfi;
FILE *cfi;
latsGribFile *gbfile;
char *gname;
int j;
gname = (char *)malloc(strlen(file->path)+5);
if(gname == NULL) {
latsError("GRIB (latsgrib.c) --> Allocating memory for GRIB file name %s", file->path);
return 0;
}
j=0;
while( (size_t)j < strlen(file->path) ) {
*(gname+j) = *(file->path+j);
j++;
}
*(gname+j) = '\0';
if((gbfile = (latsGribFile *)malloc(sizeof(latsGribFile)))==0){
latsError("GRIB (latsgrib.c) --> Allocating memory for GRIB file name %s", file->path);
return 0;
}
gfi=fopen(gname,"wb") ;
if(gfi == NULL) {
latsError("GRIB (latsgrib.c) --> lats_create create .grb failed for %s\n",gname);
return 0;
}
if(file->convention != LATS_GRIB_ONLY) {
/* Make sure that GrADS control file has a title */
if(strlen(file->comments)==0)
strcpy(file->comments, file->center);
j=0;
while( (size_t)j < strlen(file->path) ) {
*(gname+j) = *(file->path+j);
j++;
}
*(gname+j) = '\0';
strcpy(gname+strlen(gname)-4,".ctl");
cfi=fopen(gname,"w") ;
if(cfi == NULL) {
latsError("GRIB (latsgrib.c) --> lats_create create .ctl failed for %s\n",gname);
return 0;
}
} else {
cfi=NULL;
}
/*------ initialize the first time flag in gbfile struct */
gbfile->gbmode=1;
gbfile->gfi=gfi;
gbfile->cfi=cfi;
strcpy(gbfile->ctlname,gname);
free(gname);
file->depend = (void *)gbfile;
return file->id;
}
/*----------------------------------------
* Define a grid for a GRIB 'file'.
* Return 1 on success, 0 on failure;
*
* Note: this routine is called by lats_var for the
* first variable which is defined on this grid.
*
* Note: This routine is a no-op
-----------------------------------------*/
int lats_grid_grib(latsFile *file, latsGrid *grid){
if(file->ngrid > 1 && file->convention==LATS_GRADS_GRIB){
latsError("GRIB (latsgrib.c) --> Only one grid per file supported in the GrADS/GRIB convention; file: %s, grid: %s",
file->path, grid->name);
return 0;
}
return 1;
}
/* Define a variable to be written to a GRIB file.
* 'grid' is the grid structure.
* 'vertdim' is the vertical dimension structure, or 0 no level.
* Return the variable ID on success, 0 on failure.
*/
int lats_var_grib(latsFile *file, latsVar *var, latsGrid *grid, latsVertDim *vertdim){
return var->id;
}
/*----------------------------------------
*
* Write a vertical dimension 'vertdim' to GRIB 'file'.
* Return dimension ID on success, 0 on failure.
*
-----------------------------------------*/
int lats_vert_dim_grib(latsFile *file, latsVertDim *vertdim) {
if(file->latsmode != LATS_MODE_DEFINE){
latsError("GRIB (latsgrib.c) --> lats_vert_dim calls must precede any lats_write call");
return 0;
}
return file->nvertdim;
}
/*----------------------------------------
*
* Write a horizontal lon-lat section 'data' for variable 'var' to GRIB 'file'.
* 'levindex' is the 0-origin index, into var->levs, of the level value, or -1 if there are no levels.
* 'timeindex' is the 0-origin index of the time value, or -1 if there are no times.
* Return 1 on success, 0 on failure.
*
-----------------------------------------*/
int lats_write_grib(latsFile *file, latsVar *var,
int levindex, int timeindex, latsCompTime time, void *data) {
FILE *gfi;
int i,j,k;
float undef, undef_delta;
int rc,rcp,rcg,rcb;
int glen;
float *dataf;
latsGribFile *gbfile;
/*---
GRIB section pointers
---*/
grib_is *is;
grib_pds *pds;
grib_gds_ll *gds;
grib_bms *bms;
grib_bds *bds;
grib_es *es;
/*---
point to the default static GRIB section structs
---*/
is=&FGRIBAPI_is;
pds=&FGRIBAPI_pds;
gds=&FGRIBAPI_gds;
bms=&FGRIBAPI_bms;
bds=&FGRIBAPI_bds;
es=&FGRIBAPI_es;
pds->pds=(unsigned char *) malloc(pds->len);
/*---
initial checks
---*/
if(var->parm->datatype == LATS_INT) {
lats_flt2int(data,data,(var->grid->nlon*var->grid->nlat));
/* return 100; */
}
/*---
point to the GRIB file struct
---*/
gbfile = (latsGribFile *)file->depend;
gfi=gbfile->gfi;
if(VERB) {
printf("vvv %s\n",var->name);
if(var->parm->datatype == LATS_FLOAT) printf("vvv DATA TYPE is float\n");
if(var->parm->datatype == LATS_INT) printf("vvv DATA TYPE is int\n");
printf("vvv GRIB id = %d\n",var->parm->id);
printf("vvv GRIB dsf = %d\n",var->scalefac);
printf("vvv GRIB bits per grid point = %d\n",var->precision);
}
/*---
* undef values
---*/
if(var->hasmissing) {
if(var->parm->datatype == LATS_FLOAT) {
if(VERB) {
printf("vvv %g missing float\n",var->missing.f);
printf("vvv %g missing delta float\n",var->missingdelta);
}
undef=var->missing.f;
undef_delta = var->missingdelta;
}
if(var->parm->datatype == LATS_INT) {
if(VERB) {
printf("vvv %d missing integer\n",var->missing.i);
printf("vvv %g missing delta integer\n",var->missingdelta);
}
undef=(float)var->missing.i;
undef_delta = 0.0;
}
} else {
if(VERB) printf("vvv NO MISSING DATA\n");
}
/*---
* create the PDS, GDS and BDS (BMS)
---*/
rcp=lats_pds_set(file,var,levindex,timeindex,time,pds) ;
rcg=lats_gds_set(file,var,levindex,timeindex,time,gds) ;
rcb=bds_set(data,pds,bds,bms,undef,gds->ni*gds->nj,pds->nbits,var->hasmissing,undef_delta);
if(rcp) {
latsError("GRIB (latsgrib.c) --> bds_set failed rc = %d\n",rcp);
return 0;
} else if(rcg) {
latsError("GRIB (latsgrib.c) --> gds_set failed rc = %d\n",rcg);
return 0;
} else if(rcb) {
latsError("GRIB (latsgrib.c) --> bds_set failed rc = %d\n",rcb);
return 0;
}
glen=is->len + pds->len + gds->len + bms->len + bds->len + es->len ;
if(VERB) printf("vvv glen %d %d %d %d %d %d\n",
glen,is->len,pds->len,gds->len,bms->len,bds->len,es->len);
rc=is_set(is,glen) ;
if(rc) {
latsError("GRIB (latsgrib.c) --> is_set failed rc = %d\n",rc);
return 0;
}
/*---
* write out the sections
---*/
rc=fwrite(is->is,sizeof(char),is->len,gfi) ;
rc=fwrite(pds->pds,sizeof(char),pds->len,gfi) ;
rc=fwrite(gds->gds,sizeof(char),gds->len,gfi) ;
if(bms->len) rc=fwrite(bms->bms,sizeof(char),bms->len,gfi) ;
rc=fwrite(bds->bds,sizeof(char),bds->len,gfi) ;
rc=fwrite(es->es,sizeof(char),es->len,gfi) ;
if(gbfile->gbmode) {
gbfile->time=time;
gbfile->gbmode=0;
}
free(pds->pds);
free(gds->gds);
return 1;
}
int is_set(grib_is *is, unsigned int glen) {
set_int3(&is->is[4],glen);
is->is[7]=1;
return 0;
}
/*-----------------------------------
*
* LATS GRIB PDS routine
*
-------------------------------------*/
int lats_pds_set(latsFile *file, latsVar *var,
int levindex, int timeindex, latsCompTime time, grib_pds *pds) {
unsigned char cent,yr,mo,da,hr,mn;
float dum1;
/*---
time processing
---*/
if(file->frequency == LATS_FORECAST_HOURLY) {
cent=(unsigned char)(((int)((float)(file->btime.year*0.01)+0.001))+1);
yr=(unsigned char)(((float)file->btime.year-((float)cent-1.0)*100.0)+0.1);
mo=(unsigned char)(file->btime.month);
da=(unsigned char)(file->btime.day);
hr=(unsigned char)((int)file->btime.hour+0.1);
mn=0; /* set minute to 0 */
} else {
cent=(unsigned char)(((int)((float)(time.year*0.01)+0.001))+1);
yr=(unsigned char)(((float)time.year-((float)cent-1.0)*100.0)+0.1);
mo=(unsigned char)(time.month);