1 /*
2  * Copyright 2005-2018 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  *
7  * In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
8  * virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
9  */
10 
11 #include "grib_api_internal.h"
12 
13 #if GRIB_TIMER
14 
usage(char * prog)15 void usage(char* prog) {
16   printf("usage: %s [-a outfile | -w outfile ] grib_file repetitions bitsPerValue\n",prog);
17   exit(1);
18 }
19 
grib_handle_write(grib_handle * h,char * filename)20 size_t grib_handle_write(grib_handle* h,char* filename) {
21   FILE* of=NULL;
22   const void *buffer; size_t size;
23 
24   of = fopen(filename,"w");
25   if(!of) {
26       perror(filename);
27       exit(1);
28   }
29   GRIB_CHECK(grib_get_message(h,&buffer,&size),0);
30   if(fwrite(buffer,1,size,of) != size) {
31     perror(filename);
32     exit(1);
33   }
34   fclose(of);
35 
36   return size;
37 
38 }
39 
print_timer(grib_timer * t,int repeat)40 static void print_timer(grib_timer* t,int repeat) {
41    printf("%s : %g cpu\n",t->name_,t->timer_/repeat);
42 }
43 
main(int argc,char * argv[])44 int main(int argc, char* argv[]) {
45   grib_handle *h=NULL;
46   grib_context* c=NULL;
47   FILE* fin=NULL;
48   FILE* fout=NULL;
49   char* finname;
50   char* ofilename;
51   char defofilename[]="ccsds_perf.out";
52   double *values=NULL;
53   int append=0;
54   size_t nvalues=0;
55   int count,e=0;
56   int repeatccsds=1;
57   int repeatsimple=1;
58   grib_timer *tes,*tds,*tej,*tdj;
59   char grid_ccsds[]="grid_ccsds";
60   size_t grid_ccsds_l=strlen(grid_ccsds);
61   char grid_simple[]="grid_simple";
62   size_t grid_simple_l=strlen(grid_simple);
63   char packingType[50]={0,};
64   size_t len=50;
65   char param[50]={0,};
66   char gridType[50]={0,};
67   char outfilename[255]={0,};
68   size_t filesize_ccsds=0;
69   size_t filesize_simple=0;
70   double perc=0;
71   long bitsPerValue=0;
72   int iarg=1;
73   char grid[20]={0,};
74   char shortName[20]={0,};
75   long level;
76   char levelType[20]={0,};
77 
78   tes=grib_get_timer(0,"encoding simple", 0, 0);
79   tds=grib_get_timer(0,"decoding simple", 0, 0);
80   tej=grib_get_timer(0,"encoding ccsds", 0, 0);
81   tdj=grib_get_timer(0,"decoding ccsds", 0, 0);
82 
83   if (argc != 4 && argc != 6 ) usage(argv[0]);
84   if (!strcmp(argv[iarg],"-w")) {
85     append=0;
86     iarg++;
87     ofilename=argv[iarg];
88     iarg++;
89   } else if (!strcmp(argv[iarg],"-a")) {
90     append=1;
91     iarg++;
92     ofilename=argv[iarg];
93     iarg++;
94   } else {
95     append=0;
96     ofilename=defofilename;
97   }
98   finname=argv[iarg++];
99   repeatsimple=atoi(argv[iarg++]);
100   bitsPerValue=atoi(argv[iarg++]);
101 
102   fin = fopen(finname,"r");
103   if(!fin) {perror(finname);exit(1);}
104 
105   if (append)
106       fout = fopen(ofilename,"a");
107   else
108       fout = fopen(ofilename,"w");
109 
110   if(!fout) {perror(ofilename);exit(1);}
111 
112   c=grib_context_get_default();
113   e=0;
114   h=grib_handle_new_from_file(c,fin,&e);
115   fclose(fin);
116 
117   GRIB_CHECK(e,0);
118 
119   len=50;
120   grib_get_string(h,"shortName",param,&len);
121 
122   len=20;
123   grib_get_string(h,"levelType",levelType,&len);
124 
125   if (!strcmp(levelType,"pl")) {
126     GRIB_CHECK(grib_get_long(h,"level",&level),0);
127     sprintf(shortName,"%s%ld",param,level);
128   } else {
129     sprintf(shortName,"%s",param);
130   }
131 
132 
133   grib_set_long(h,"editionNumber",2);
134   GRIB_CHECK(grib_get_size(h,"values",&nvalues),0);
135   values=(double*)grib_context_malloc(c,sizeof(double)*nvalues);
136   if (!values) { printf("%s: memory allocation error\n",argv[0]); exit(1); }
137 
138   len=50;
139   grib_get_string(h,"gridType",gridType,&len);
140 
141   len=50;
142   grib_get_string(h,"packingType",packingType,&len);
143 
144   GRIB_CHECK(grib_get_double_array(h,"values",values,&nvalues),0);
145   grib_set_long(h,"bitsPerValue",bitsPerValue);
146   GRIB_CHECK(grib_set_double_array(h,"values",values,nvalues),0);
147 
148   printf("--------------------------------\n");
149   printf("- %s - gridType=%s packingType=%s numberOfValues=%ld bitsPerValue=%ld\n",
150     param,gridType,packingType,(long)nvalues,bitsPerValue);
151 
152   if (!strcmp(packingType,"spectral_complex") || !strcmp(packingType,"spectral_simple")) {
153      printf("unable to process spectral data\n");
154      exit(1);
155   }
156 
157   if (!strcmp(gridType,"reduced_gg") || !strcmp(gridType,"regular_gg")) {
158      long N;
159      grib_get_long(h,"N",&N);
160      printf("    N=%ld\n",N);
161      sprintf(grid,"%ld",N);
162   }
163   if (!strcmp(gridType,"regular_ll")) {
164      double Di,Dj;
165      grib_get_double(h,"DiInDegrees",&Di);
166      grib_get_double(h,"DjInDegrees",&Dj);
167      printf("    Di=%g Dj=%g\n",Di,Dj);
168      sprintf(grid,"%g/%g",Di,Dj);
169   }
170 
171 
172   if (!append)
173     fprintf(fout,
174     "shortName gridType numberOfValues bitsPerValue grid sizeSimple sizeccsds encodeccsds encodeSimple decodeccsds decodeSimple\n");
175 
176   /* decode values grid_simple */
177   if (strcmp(packingType,grid_simple))
178     grib_set_string(h,"packingType",grid_simple,&grid_simple_l);
179   /* printf("decoding simple\n"); */
180   grib_timer_start(tds);
181   for (count=0;count<repeatsimple;count++)
182     GRIB_CHECK(grib_get_double_array(h,"values",values,&nvalues),0);
183   grib_timer_stop(tds,0);
184   /* printf("%d messages decoded\n\n",count); */
185 
186   *outfilename='\0';
187   sprintf(outfilename,"%s_%s_%ld_simple.grib2",param,gridType,bitsPerValue);
188   filesize_simple=grib_handle_write(h,outfilename);
189   printf("file size simple = %ld\n",(long)filesize_simple);
190 
191   /* encode values grid_simple*/
192   /* printf("encoding simple\n"); */
193   grib_timer_start(tes);
194   for (count=0;count<repeatsimple;count++)
195     GRIB_CHECK(grib_set_double_array(h,"values",values,nvalues),0);
196   grib_timer_stop(tes,0);
197   /* printf("%d messages encoded \n\n",count); */
198 
199   /* decode values grid_ccsds */
200   grib_set_string(h,"packingType",grid_ccsds,&grid_ccsds_l);
201   /* printf("decoding ccsds\n"); */
202   grib_timer_start(tdj);
203   for (count=0;count<repeatccsds;count++)
204     GRIB_CHECK(grib_get_double_array(h,"values",values,&nvalues),0);
205   grib_timer_stop(tdj,0);
206   /* printf("%d messages decoded\n\n",count); */
207 
208   *outfilename='\0';
209   sprintf(outfilename,"%s_%s_%ld_ccsds.grib2",param,gridType,bitsPerValue);
210   filesize_ccsds=grib_handle_write(h,outfilename);
211   printf("file size ccsds   = %ld\n",(long)filesize_ccsds);
212 
213   perc=(double)filesize_simple/(double)filesize_ccsds;
214 
215   printf("compression ratio = %g \n",perc);
216   printf("space savings = %g \n",(1.0-1.0/perc)*100);
217 
218   /* encode values grid_ccsds*/
219   /* printf("encoding ccsds\n"); */
220   grib_timer_start(tej);
221   for (count=0;count<repeatccsds;count++)
222     GRIB_CHECK(grib_set_double_array(h,"values",values,nvalues),0);
223   grib_timer_stop(tej,0);
224   /* printf("%d messages encoded \n\n",count); */
225 
226   grib_handle_delete(h);
227   grib_context_free(c,values);
228 
229   print_timer(tej,repeatccsds);
230   print_timer(tdj,repeatccsds);
231   print_timer(tes,repeatsimple);
232   print_timer(tds,repeatsimple);
233   printf("--------------------------------\n\n");
234   fprintf(fout,"%s %s %ld %ld %s %ld %ld %g %g %g %g\n",
235     shortName,gridType,(long)nvalues,bitsPerValue,
236     grid,(long)filesize_simple,(long)filesize_ccsds,tej->timer_/repeatccsds,tes->timer_/repeatsimple,tdj->timer_/repeatccsds,tds->timer_/repeatsimple);
237 
238   return 0;
239 }
240 #else
241 
main(int argc,char * argv[])242 int main(int argc,char* argv[]) {
243 return 0;
244 }
245 
246 #endif
247