Applied clang-format (Part 3)

This commit is contained in:
Shahram Najm 2020-01-22 14:57:43 +00:00
parent 3d188bebbf
commit 42453ffbd7
48 changed files with 1421 additions and 1378 deletions

View File

@ -13,53 +13,52 @@
int main(int argc, char* argv[])
{
FILE* f;
int err=0;
char infile[]="../../data/reduced_gaussian_model_level.grib1";
codes_handle *h=NULL;
int err = 0;
char infile[] = "../../data/reduced_gaussian_model_level.grib1";
codes_handle* h = NULL;
codes_box* box;
codes_points* points;
codes_context* c=codes_context_get_default();
double north,west,south,east;
codes_context* c = codes_context_get_default();
double north, west, south, east;
double* val;
int i;
north=60;
south=10;
west=-10;
east=30;
north = 60;
south = 10;
west = -10;
east = 30;
f=fopen(infile,"rb");
f = fopen(infile, "rb");
if (!f) {
perror(infile);
exit(1);
}
h=codes_handle_new_from_file(c, f, PRODUCT_GRIB, &err);
h = codes_handle_new_from_file(c, f, PRODUCT_GRIB, &err);
if (!h) {
printf("unable to create handle from file %s\n",infile);
printf("unable to create handle from file %s\n", infile);
exit(err);
}
box=codes_box_new(h,&err);
box = codes_box_new(h, &err);
if (!box) {
printf("unable to create box\n");
exit(err);
}
points=codes_box_get_points(box,north,west,south,east,&err);
points = codes_box_get_points(box, north, west, south, east, &err);
val=(double*)malloc(sizeof(double)*points->n);
codes_points_get_values(h,points,val);
val = (double*)malloc(sizeof(double) * points->n);
codes_points_get_values(h, points, val);
for (i=0;i<points->n;i++) {
printf("%d -- %.3f %.3f %ld %g\n",i,
points->latitudes[i], points->longitudes[i], (long)(points->indexes[i]), val[i]);
for (i = 0; i < points->n; i++) {
printf("%d -- %.3f %.3f %ld %g\n", i,
points->latitudes[i], points->longitudes[i], (long)(points->indexes[i]), val[i]);
}
free(val);
fclose(f);
return 0;
}

View File

@ -18,42 +18,41 @@
#include "eccodes.h"
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
FILE* in = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
codes_handle* h = NULL;
char* units= NULL;
char* unitsPercent= NULL;
char* units = NULL;
char* unitsPercent = NULL;
long longVal;
double doubleVal;
size_t len=0;
int err=0;
int cnt=0;
size_t len = 0;
int err = 0;
int cnt = 0;
const char* infile = "../../data/bufr/syno_multi.bufr";
in=fopen(infile,"rb");
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
/* we need to instruct ecCodes to expand the descriptors
i.e. unpack the data values */
CODES_CHECK(codes_set_long(h, "unpack", 1),0);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
/* ----------------------------------------------------------------
We will read the value and all the attributes available for
@ -61,34 +60,34 @@ int main(int argc,char* argv[])
-------------------------------------------------------------------*/
/* get the value as double */
CODES_CHECK(codes_get_double(h, "airTemperatureAt2M", &doubleVal),0);
printf(" airTemperatureAt2M: %.2f\n",doubleVal);
CODES_CHECK(codes_get_double(h, "airTemperatureAt2M", &doubleVal), 0);
printf(" airTemperatureAt2M: %.2f\n", doubleVal);
/* get the element's code (see BUFR code table B) */
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->code", &longVal),0);
printf(" airTemperatureAt2M->code: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->code", &longVal), 0);
printf(" airTemperatureAt2M->code: %ld\n", longVal);
/* get the element's units (see BUFR code table B) */
/* get the size and allocate memory*/
CODES_CHECK(codes_get_length(h, "airTemperatureAt2M->units", &len), 0);
units = (char*)malloc(len*sizeof(char));
units = (char*)malloc(len * sizeof(char));
/* get the values*/
codes_get_string(h, "airTemperatureAt2M->units", units, &len);
printf(" airTemperatureAt2M->units: %s\n", units);
/* get the element's scale (see BUFR code table B) */
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->scale", &longVal),0);
printf(" airTemperatureAt2M->scale: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->scale", &longVal), 0);
printf(" airTemperatureAt2M->scale: %ld\n", longVal);
/* get the element's reference (see BUFR code table B) */
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->reference", &longVal),0);
printf(" airTemperatureAt2M->reference: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->reference", &longVal), 0);
printf(" airTemperatureAt2M->reference: %ld\n", longVal);
/* get the element's width (see BUFR code table B) */
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->width", &longVal),0);
printf(" airTemperatureAt2M->width: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->width", &longVal), 0);
printf(" airTemperatureAt2M->width: %ld\n", longVal);
/* -----------------------------------------------------------------
The 2m temperature data element in this message has an associated
@ -97,34 +96,34 @@ int main(int argc,char* argv[])
----------------------------------------------------------------- */
/* get the value as long */
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence", &longVal),0);
printf(" airTemperatureAt2M->percentConfidence: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence", &longVal), 0);
printf(" airTemperatureAt2M->percentConfidence: %ld\n", longVal);
/* get the element's code (see BUFR code table B) */
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->code", &longVal),0);
printf(" airTemperatureAt2M->percentConfidence->code: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->code", &longVal), 0);
printf(" airTemperatureAt2M->percentConfidence->code: %ld\n", longVal);
/* get the element's units (see BUFR code table B) */
/* get the size and allocate memory*/
CODES_CHECK(codes_get_length(h, "airTemperatureAt2M->percentConfidence->units", &len), 0);
unitsPercent = (char*)malloc(len*sizeof(char));
unitsPercent = (char*)malloc(len * sizeof(char));
/* get the values*/
codes_get_string(h, "airTemperatureAt2M->percentConfidence->units", unitsPercent, &len);
printf(" airTemperatureAt2M->percentConfidence->units: %s\n", unitsPercent);
/* get the element's scale (see BUFR code table B) */
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->scale", &longVal),0);
printf(" airTemperatureAt2M->percentConfidence->scale: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->scale", &longVal), 0);
printf(" airTemperatureAt2M->percentConfidence->scale: %ld\n", longVal);
/* get the element's reference (see BUFR code table B) */
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->reference", &longVal),0);
printf(" airTemperatureAt2M->percentConfidence->reference: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->reference", &longVal), 0);
printf(" airTemperatureAt2M->percentConfidence->reference: %ld\n", longVal);
/* get the element's width (see BUFR code table B) */
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->width", &longVal),0);
printf(" airTemperatureAt2M->percentConfidence->width: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "airTemperatureAt2M->percentConfidence->width", &longVal), 0);
printf(" airTemperatureAt2M->percentConfidence->width: %ld\n", longVal);
/* free allocated arrays */
free(units);

View File

@ -18,21 +18,21 @@
#include <stdio.h>
#include "eccodes.h"
static void usage(const char *app)
static void usage(const char* app)
{
fprintf(stderr,"Usage is: %s input_file ouput_file\n", app);
fprintf(stderr, "Usage is: %s input_file ouput_file\n", app);
}
int main(int argc, char *argv[])
int main(int argc, char* argv[])
{
FILE *in = NULL;
FILE *out = NULL;
FILE* in = NULL;
FILE* out = NULL;
/* message handle. Required in all the ecCodes calls acting on a message.*/
codes_handle *source_handle = NULL;
codes_handle* source_handle = NULL;
const void *buffer = NULL;
size_t size = 0;
const void* buffer = NULL;
size_t size = 0;
int i, err = 0;
if (argc != 3) {
@ -40,8 +40,8 @@ int main(int argc, char *argv[])
return 1;
}
in = fopen(argv[1],"rb");
out = fopen(argv[2],"wb");
in = fopen(argv[1], "rb");
out = fopen(argv[2], "wb");
/* open input and output */
if (!in || !out) {
@ -64,10 +64,9 @@ int main(int argc, char *argv[])
/* create several clones of this message and alter them
in different ways */
for(i=0; i < 3; i++) {
for (i = 0; i < 3; i++) {
/* clone the current handle */
codes_handle *clone_handle = codes_handle_clone(source_handle);
codes_handle* clone_handle = codes_handle_clone(source_handle);
if (clone_handle == NULL) {
perror("ERROR: could not clone field");
@ -77,13 +76,13 @@ int main(int argc, char *argv[])
/* This is the place where you may wish to modify the clone
E.g. we change the typicalDay key */
CODES_CHECK(codes_set_long(clone_handle, "typicalDay", 20+i),0);
CODES_CHECK(codes_set_long(clone_handle, "typicalDay", 20 + i), 0);
/* get the coded message in a buffer */
CODES_CHECK(codes_get_message(clone_handle, &buffer, &size),0);
CODES_CHECK(codes_get_message(clone_handle, &buffer, &size), 0);
/* write the buffer to a file */
if(fwrite(buffer, 1, size, out) != size) {
if (fwrite(buffer, 1, size, out) != size) {
perror("ERROR: could not write message to file");
return 1;
}

View File

@ -19,35 +19,35 @@
static void usage(const char* progname);
int main(int argc, char *argv[])
int main(int argc, char* argv[])
{
FILE* f = NULL;
codes_handle* h = NULL;
FILE* f = NULL;
codes_handle* h = NULL;
codes_handle* ho = NULL;
long size;
char** keys = NULL;
size_t nkeys=0,i;
int err=0;
char* outfile = NULL;
char** keys = NULL;
size_t nkeys = 0, i;
int err = 0;
char* outfile = NULL;
const char* sampleName = "BUFR3";
const long ibitmap[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,0};
const long ibitmap[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 0 };
const long ud[]={307011,7006,10004,222000,101023,31031,1031,1032,101023,33007,
225000,236000,101023,31031,1031,1032,8024,101001,225255,225000,
236000,101023,31031,1031,1032,8024,101001,225255,
1063,2001,4001,4002,4003,4004,4005,5002,
6002,7001,7006,11001,11016,11017,11002};
const long ud[] = { 307011, 7006, 10004, 222000, 101023, 31031, 1031, 1032, 101023, 33007,
225000, 236000, 101023, 31031, 1031, 1032, 8024, 101001, 225255, 225000,
236000, 101023, 31031, 1031, 1032, 8024, 101001, 225255,
1063, 2001, 4001, 4002, 4003, 4004, 4005, 5002,
6002, 7001, 7006, 11001, 11016, 11017, 11002 };
if (argc != 3) usage(argv[0]);
outfile=argv[2];
outfile = argv[2];
f = fopen(argv[1],"rb");
if(!f) {
f = fopen(argv[1], "rb");
if (!f) {
perror(argv[1]);
exit(1);
}
@ -58,7 +58,7 @@ int main(int argc, char *argv[])
return 1;
}
size = sizeof(ibitmap)/sizeof(ibitmap[0]);
size = sizeof(ibitmap) / sizeof(ibitmap[0]);
CODES_CHECK(codes_set_long_array(ho, "inputDataPresentIndicator", ibitmap, size), 0);
CODES_CHECK(codes_set_long(ho, "bufrHeaderCentre", 98), 0);
CODES_CHECK(codes_set_long(ho, "updateSequenceNumber", 1), 0);
@ -75,17 +75,16 @@ int main(int argc, char *argv[])
CODES_CHECK(codes_set_long(ho, "observedData", 1), 0);
CODES_CHECK(codes_set_long(ho, "compressedData", 0), 0);
size = sizeof(ud)/sizeof(ud[0]);
codes_set_long_array(ho,"unexpandedDescriptors",ud,size);
size = sizeof(ud) / sizeof(ud[0]);
codes_set_long_array(ho, "unexpandedDescriptors", ud, size);
while((h = codes_handle_new_from_file(0,f,PRODUCT_BUFR,&err)) != NULL)
{
if(!h) {
while ((h = codes_handle_new_from_file(0, f, PRODUCT_BUFR, &err)) != NULL) {
if (!h) {
printf("ERROR: Unable to create BUFR handle\n");
return 1;
}
/* codes_copy_key(h,ho,"unexpandedDescriptors",0); */
err = codes_set_long(h,"unpack",1);
err = codes_set_long(h, "unpack", 1);
if (err) {
printf("ERROR: Unable to unpack BUFR message. Quitting\n");
printf(" %s\n", codes_get_error_message(err));
@ -93,9 +92,9 @@ int main(int argc, char *argv[])
}
/* err=codes_bufr_copy_data(h,ho); */
keys=codes_bufr_copy_data_return_copied_keys(h,ho,&nkeys,&err);
for (i=0;i<nkeys;i++) {
printf("Copied %s\n",keys[i]);
keys = codes_bufr_copy_data_return_copied_keys(h, ho, &nkeys, &err);
for (i = 0; i < nkeys; i++) {
printf("Copied %s\n", keys[i]);
free(keys[i]);
}
printf("Total number of copied keys = %lu\n", nkeys);
@ -104,7 +103,7 @@ int main(int argc, char *argv[])
codes_handle_delete(h);
}
fclose(f);
codes_write_message(ho,outfile,"w");
codes_write_message(ho, outfile, "w");
codes_handle_delete(ho);
return err;
@ -112,6 +111,6 @@ int main(int argc, char *argv[])
static void usage(const char* progname)
{
printf("\nUsage: %s bufr_in bufr_out\n",progname);
printf("\nUsage: %s bufr_in bufr_out\n", progname);
exit(1);
}

View File

@ -17,57 +17,56 @@
#include "eccodes.h"
static void usage(const char* prog) {
printf("usage: %s infile\n",prog);
static void usage(const char* prog)
{
printf("usage: %s infile\n", prog);
exit(1);
}
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
char* filename = NULL;
FILE* in = NULL;
FILE* in = NULL;
/* message handle. Required in all the ecCodes calls acting on a message.*/
codes_handle* h=NULL;
codes_handle* h = NULL;
double *values = NULL;
size_t values_len=0;
int i, err=0;
int cnt=0;
double* values = NULL;
size_t values_len = 0;
int i, err = 0;
int cnt = 0;
if (argc!=2) usage(argv[0]);
if (argc != 2) usage(argv[0]);
filename=argv[1];
filename = argv[1];
/* open bufr file */
in=fopen(filename,"rb");
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
/* get the size of the values array*/
CODES_CHECK(codes_get_size(h, "numericValues", &values_len),0);
CODES_CHECK(codes_get_size(h, "numericValues", &values_len), 0);
printf(" number of expanded values: %lu\n", values_len);
/* allocate array for data values */
values = (double*)malloc(values_len*sizeof(double));
values = (double*)malloc(values_len * sizeof(double));
/* get the expanded data values*/
CODES_CHECK(codes_get_double_array(h, "numericValues", values, &values_len),0);
CODES_CHECK(codes_get_double_array(h, "numericValues", values, &values_len), 0);
for(i = 0; i < values_len; i++)
{
printf(" %.10e\n",values[i]);
for (i = 0; i < values_len; i++) {
printf(" %.10e\n", values[i]);
}
free(values);

View File

@ -17,63 +17,62 @@
#include "eccodes.h"
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
FILE* in = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
codes_handle* h = NULL;
long *descriptors=NULL;
double *values = NULL;
char* typicalDate= NULL;
long* descriptors = NULL;
double* values = NULL;
char* typicalDate = NULL;
long longVal;
double doubleVal;
size_t values_len=0, desc_len=0, len=0;
int i, err=0;
int cnt=0;
size_t values_len = 0, desc_len = 0, len = 0;
int i, err = 0;
int cnt = 0;
const char* infile = "../../data/bufr/syno_multi.bufr";
in=fopen(infile,"rb");
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
/* we need to instruct ecCodes to expand the descriptors
i.e. unpack the data values */
CODES_CHECK(codes_set_long(h, "unpack", 1),0);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
/* read and print some data values */
/* long value */
CODES_CHECK(codes_get_long(h, "blockNumber", &longVal),0);
CODES_CHECK(codes_get_long(h, "blockNumber", &longVal), 0);
printf(" blockNumber: %ld\n", longVal);
/* long value */
CODES_CHECK(codes_get_long(h, "stationNumber", &longVal),0);
CODES_CHECK(codes_get_long(h, "stationNumber", &longVal), 0);
printf(" stationNumber: %ld\n", longVal);
/* double value */
CODES_CHECK(codes_get_double(h, "airTemperatureAt2M", &doubleVal),0);
CODES_CHECK(codes_get_double(h, "airTemperatureAt2M", &doubleVal), 0);
printf(" airTemperatureAt2M: %f\n", doubleVal);
/* ---- string value -----------------*/
/* get the size and allocate memory*/
CODES_CHECK(codes_get_length(h, "typicalDate", &len), 0);
typicalDate = (char*)malloc(len*sizeof(char));
typicalDate = (char*)malloc(len * sizeof(char));
/* get the values*/
codes_get_string(h, "typicalDate", typicalDate, &len);
@ -82,29 +81,27 @@ int main(int argc,char* argv[])
/* ---- array of long ----------------*/
/* get the size and allocate memory*/
CODES_CHECK(codes_get_size(h, "bufrdcExpandedDescriptors", &desc_len),0);
descriptors = (long*)malloc(desc_len*sizeof(long));
CODES_CHECK(codes_get_size(h, "bufrdcExpandedDescriptors", &desc_len), 0);
descriptors = (long*)malloc(desc_len * sizeof(long));
/* get the values */
CODES_CHECK(codes_get_long_array(h, "bufrdcExpandedDescriptors", descriptors, &desc_len),0);
CODES_CHECK(codes_get_long_array(h, "bufrdcExpandedDescriptors", descriptors, &desc_len), 0);
printf(" bufrdcExpandedDescriptors:\n");
for(i = 0; i < desc_len; i++)
{
printf(" %ld\n",descriptors[i]);
for (i = 0; i < desc_len; i++) {
printf(" %ld\n", descriptors[i]);
}
/* ---- array of double ---------------*/
/* get the size and allocate memory*/
CODES_CHECK(codes_get_size(h, "numericValues", &values_len),0);
values = (double*)malloc(values_len*sizeof(double));
CODES_CHECK(codes_get_size(h, "numericValues", &values_len), 0);
values = (double*)malloc(values_len * sizeof(double));
/* get the values*/
CODES_CHECK(codes_get_double_array(h, "numericValues", values, &values_len),0);
CODES_CHECK(codes_get_double_array(h, "numericValues", values, &values_len), 0);
printf(" numericValues:\n");
for(i = 0; i < values_len; i++)
{
printf(" %.10e\n",values[i]);
for (i = 0; i < values_len; i++) {
printf(" %.10e\n", values[i]);
}
/* free allocated arrays */

View File

@ -17,34 +17,33 @@
#include "eccodes.h"
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
codes_handle* h=NULL;
codes_handle* h = NULL;
size_t i=0, size = 0;
size_t len=0;
int err=0, cnt=0;
char** strArray = NULL; /* array of strings */
size_t i = 0, size = 0;
size_t len = 0;
int err = 0, cnt = 0;
char** strArray = NULL; /* array of strings */
const char* infile = "../../data/bufr/pgps_110.bufr";
FILE* in = fopen(infile,"rb");
FILE* in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
/* we need to instruct ecCodes to expand the descriptors
i.e. unpack the data values */
CODES_CHECK(codes_set_long(h, "unpack", 1),0);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
/* get the size and allocate memory */
CODES_CHECK(codes_get_size(h, "stationOrSiteName", &len), 0);
@ -54,13 +53,14 @@ int main(int argc,char* argv[])
return 1;
}
CODES_CHECK(codes_get_string_array(h, "stationOrSiteName", strArray, &size), 0);
for(i=0; i<size; ++i) {
for (i = 0; i < size; ++i) {
printf("%s\n", strArray[i]);
}
printf("stationOrSiteName string array size = %lu\n", (unsigned long)size);
/* free allocated arrays */
for(i=0; i<size; ++i) free( strArray[i] );
for (i = 0; i < size; ++i)
free(strArray[i]);
free(strArray);
/* delete handle */

View File

@ -19,82 +19,80 @@
#include "eccodes.h"
#define MAX_VAL_LEN 1024
#define MAX_VAL_LEN 1024
static void usage(const char* prog) {
printf("usage: %s infile\n",prog);
static void usage(const char* prog)
{
printf("usage: %s infile\n", prog);
exit(1);
}
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
char* filename = NULL;
FILE* in = NULL;
FILE* in = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
int err=0, cnt=0;
codes_handle* h = NULL;
int err = 0, cnt = 0;
int keyType = 0;
char value[MAX_VAL_LEN];
size_t vlen=MAX_VAL_LEN;
size_t klen=0;
size_t vlen = MAX_VAL_LEN;
size_t klen = 0;
if (argc!=2) usage(argv[0]);
if (argc != 2) usage(argv[0]);
filename=argv[1];
filename = argv[1];
in=fopen(filename,"rb");
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
/* loop over the messages in the BUFR file */
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
{
codes_bufr_keys_iterator* kiter=NULL;
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
codes_bufr_keys_iterator* kiter = NULL;
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
/* we need to instruct ecCodes to unpack the data values */
CODES_CHECK(codes_set_long(h,"unpack",1),0);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
/* get BUFR key iterator */
kiter=codes_bufr_keys_iterator_new(h,0);
kiter = codes_bufr_keys_iterator_new(h, 0);
if (!kiter) {
printf("ERROR: Unable to create BUFR keys iterator\n");
exit(1);
}
/* loop over the keys */
while(codes_bufr_keys_iterator_next(kiter))
{
while (codes_bufr_keys_iterator_next(kiter)) {
/* get key name */
char* name = codes_bufr_keys_iterator_get_name(kiter);
printf(" %s=",name);
printf(" %s=", name);
CODES_CHECK(codes_get_native_type(h,name,&keyType),0);
CODES_CHECK(codes_get_native_type(h, name, &keyType), 0);
/* get key size to see if it is an array */
CODES_CHECK(codes_get_size(h,name,&klen),0);
CODES_CHECK(codes_get_size(h, name, &klen), 0);
if (klen ==1)
{
if (klen == 1) {
/* not array */
vlen=MAX_VAL_LEN;
vlen = MAX_VAL_LEN;
memset(value, 0, vlen);
codes_get_string(h,name,value,&vlen);
printf("%s\n",value);
codes_get_string(h, name, value, &vlen);
printf("%s\n", value);
}
else {
/* for arrays */
printf("(array of %lu)\n",klen);
printf("(array of %lu)\n", klen);
}
}

View File

@ -17,51 +17,47 @@
#include "eccodes.h"
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
FILE* in = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
codes_handle* h = NULL;
double doubleVal;
int err=0;
int cnt=0;
int err = 0;
int cnt = 0;
const char* infile = "../../data/bufr/syno_1.bufr";
in=fopen(infile,"rb");
if (!in)
{
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
/* we need to instruct ecCodes to expand the descriptors
i.e. unpack the data values */
CODES_CHECK(codes_set_long(h,"unpack",1),0);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
/* the value of this key is missing in the message*/
CODES_CHECK(codes_get_double(h,"relativeHumidity",&doubleVal),0);
printf(" relativeHumidity: %.2f\n",doubleVal);
CODES_CHECK(codes_get_double(h, "relativeHumidity", &doubleVal), 0);
printf(" relativeHumidity: %.2f\n", doubleVal);
/* we will print "value missing" */
if(doubleVal == CODES_MISSING_DOUBLE)
{
if (doubleVal == CODES_MISSING_DOUBLE) {
printf(" --> value missing\n");
}
else
{
else {
printf(" --> value present\n");
}

View File

@ -22,36 +22,36 @@
static void* process_bufr(void* arg)
{
FILE* fin = (FILE*)arg;
int err = 0;
FILE* fin = (FILE*)arg;
int err = 0;
codes_handle* h = NULL;
long numSubsets = 0, lVal = 0;
size_t size = 0, i=0;
size_t size = 0, i = 0;
double* dValues = NULL;
/* Each thread gets a different message handle */
h = codes_handle_new_from_file(NULL, fin, PRODUCT_BUFR, &err);
assert(h);
/* Check expected values for this BUFR file */
CODES_CHECK(codes_get_long(h,"numberOfSubsets", &numSubsets),0);
assert( numSubsets == 1 );
CODES_CHECK(codes_get_long(h, "numberOfSubsets", &numSubsets), 0);
assert(numSubsets == 1);
CODES_CHECK(codes_get_long(h, "rectimeSecond", &lVal), 0);
assert( lVal == 27 );
assert(lVal == 27);
CODES_CHECK(codes_set_long(h, "unpack", 1),0);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
dValues = (double*)malloc(numSubsets*sizeof(double));
dValues = (double*)malloc(numSubsets * sizeof(double));
assert(dValues);
size = numSubsets;
CODES_CHECK(codes_get_double_array(h, "latitude", dValues, &size), 0);
for(i=0; i<size; ++i) {
for (i = 0; i < size; ++i) {
/* Specific test for latitudes in this BUFR file */
assert( dValues[0] < 79 && dValues[0] > 70 );
assert(dValues[0] < 79 && dValues[0] > 70);
}
free(dValues);
/* Some encoding too */
CODES_CHECK(codes_set_long(h, "bufrHeaderCentre", 88),0);
CODES_CHECK(codes_set_long(h, "bufrHeaderCentre", 88), 0);
CODES_CHECK(codes_set_long(h, "blockNumber", 2), 0);
CODES_CHECK(codes_set_long(h, "#3#verticalSignificanceSurfaceObservations", 8), 0);
CODES_CHECK(codes_set_long(h, "pack", 1), 0);
@ -62,20 +62,20 @@ static void* process_bufr(void* arg)
int main(int argc, char** argv)
{
pthread_t thread1, thread2, thread3;
int err = 0;
FILE* fin = 0;
int err = 0;
FILE* fin = 0;
codes_handle* h1 = 0;
codes_handle* h2 = 0;
fin = fopen("../../data/bufr/syno_multi.bufr", "rb");
fin = fopen("../../data/bufr/syno_multi.bufr", "rb");
assert(fin);
err = pthread_create(&thread1, NULL, process_bufr, (void *)fin);
err = pthread_create(&thread1, NULL, process_bufr, (void*)fin);
assert(!err);
err = pthread_create(&thread2, NULL, process_bufr, (void *)fin);
err = pthread_create(&thread2, NULL, process_bufr, (void*)fin);
assert(!err);
err = pthread_create(&thread3, NULL, process_bufr, (void *)fin);
err = pthread_create(&thread3, NULL, process_bufr, (void*)fin);
assert(!err);
pthread_join(thread1, NULL);

View File

@ -17,67 +17,67 @@
#include "eccodes.h"
static void usage(const char* prog) {
printf("usage: %s infile\n",prog);
static void usage(const char* prog)
{
printf("usage: %s infile\n", prog);
exit(1);
}
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
char* filename = NULL;
FILE* in = NULL;
FILE* in = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
codes_handle* h = NULL;
long longVal;
int err=0, cnt=0;
int err = 0, cnt = 0;
if (argc!=2) usage(argv[0]);
if (argc != 2) usage(argv[0]);
filename=argv[1];
filename = argv[1];
in=fopen(filename,"rb");
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
/* get and print some keys form the BUFR header */
CODES_CHECK(codes_get_long(h,"dataCategory",&longVal),0);
printf(" dataCategory: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "dataCategory", &longVal), 0);
printf(" dataCategory: %ld\n", longVal);
CODES_CHECK(codes_get_long(h,"dataSubCategory",&longVal),0);
printf(" dataSubCategory: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "dataSubCategory", &longVal), 0);
printf(" dataSubCategory: %ld\n", longVal);
CODES_CHECK(codes_get_long(h,"typicalDate",&longVal),0);
printf(" typicalDate: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "typicalDate", &longVal), 0);
printf(" typicalDate: %ld\n", longVal);
CODES_CHECK(codes_get_long(h,"bufrHeaderCentre",&longVal),0);
printf(" bufrHeaderCentre: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "bufrHeaderCentre", &longVal), 0);
printf(" bufrHeaderCentre: %ld\n", longVal);
CODES_CHECK(codes_get_long(h,"bufrHeaderSubCentre",&longVal),0);
printf(" bufrHeaderSubCentre: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "bufrHeaderSubCentre", &longVal), 0);
printf(" bufrHeaderSubCentre: %ld\n", longVal);
CODES_CHECK(codes_get_long(h,"masterTablesVersionNumber",&longVal),0);
printf(" masterTablesVersionNumber: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "masterTablesVersionNumber", &longVal), 0);
printf(" masterTablesVersionNumber: %ld\n", longVal);
CODES_CHECK(codes_get_long(h,"localTablesVersionNumber",&longVal),0);
printf(" localTablesVersionNumber: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "localTablesVersionNumber", &longVal), 0);
printf(" localTablesVersionNumber: %ld\n", longVal);
CODES_CHECK(codes_get_long(h,"numberOfSubsets",&longVal),0);
printf(" numberOfSubsets: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "numberOfSubsets", &longVal), 0);
printf(" numberOfSubsets: %ld\n", longVal);
/* delete handle */
codes_handle_delete(h);

View File

@ -23,41 +23,40 @@
#include "eccodes.h"
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
FILE* in = NULL;
/* Message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
codes_handle* h = NULL;
double *lat=NULL, *lon=NULL, *bscatter=NULL;
long numObs=0;
size_t len=0;
int i, err=0;
int cnt=0;
double *lat = NULL, *lon = NULL, *bscatter = NULL;
long numObs = 0;
size_t len = 0;
int i, err = 0;
int cnt = 0;
const char* infile = "../../data/bufr/asca_139.bufr";
char key_name[128];
in=fopen(infile,"rb");
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
/* Loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
/* We need to instruct ecCodes to expand the descriptors
* i.e. unpack the data values */
CODES_CHECK(codes_set_long(h,"unpack",1),0);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
/* The BUFR file contains a single message with 2016 subsets in a compressed form.
* It means each subset has exactly the same structure: they store one location with
@ -67,60 +66,60 @@ int main(int argc,char* argv[])
* we will simply access the key by condition (see below) */
/* Get the total number of subsets. */
CODES_CHECK(codes_get_long(h,"numberOfSubsets",&numObs),0);
CODES_CHECK(codes_get_long(h, "numberOfSubsets", &numObs), 0);
printf("Number of values: %ld\n",numObs);
printf("Number of values: %ld\n", numObs);
/* Get latitude */
sprintf(key_name,"latitude");
sprintf(key_name, "latitude");
/* Check the size (including all the subsets) */
CODES_CHECK(codes_get_size(h,key_name,&len),0);
if(len != numObs) {
printf("inconsistent number of %s values found!\n",key_name);
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
if (len != numObs) {
printf("inconsistent number of %s values found!\n", key_name);
return 1;
}
/* Allocate memory for the values to be read. Each
* parameter must have the same number of values. */
lat = (double*)malloc(numObs*sizeof(double));
lat = (double*)malloc(numObs * sizeof(double));
/* Get the values (from all the subsets) */
CODES_CHECK(codes_get_double_array(h,key_name,lat,&len),0);
CODES_CHECK(codes_get_double_array(h, key_name, lat, &len), 0);
/* Get longitude */
sprintf(key_name,"longitude");
sprintf(key_name, "longitude");
/* Check the size (including all the subsets) */
CODES_CHECK(codes_get_size(h,key_name,&len),0);
if(len != numObs) {
printf("inconsistent number of %s values found!\n",key_name);
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
if (len != numObs) {
printf("inconsistent number of %s values found!\n", key_name);
return 1;
}
/* Get the values (from all the subsets) */
lon = (double*)malloc(numObs*sizeof(double));
CODES_CHECK(codes_get_double_array(h,key_name,lon,&len),0);
lon = (double*)malloc(numObs * sizeof(double));
CODES_CHECK(codes_get_double_array(h, key_name, lon, &len), 0);
/* Get backScatter for beam two. We use an access by condition for this key. */
sprintf(key_name,"/beamIdentifier=2/backscatter");
sprintf(key_name, "/beamIdentifier=2/backscatter");
/* Check the size (including all the subsets) */
CODES_CHECK(codes_get_size(h,key_name,&len),0);
if(len != numObs) {
printf("inconsistent number of %s values found!\n",key_name);
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
if (len != numObs) {
printf("inconsistent number of %s values found!\n", key_name);
return 1;
}
/* Get the values (from all the subsets) */
bscatter = (double*)malloc(numObs*sizeof(double));
CODES_CHECK(codes_get_double_array(h,key_name,bscatter,&len),0);
bscatter = (double*)malloc(numObs * sizeof(double));
CODES_CHECK(codes_get_double_array(h, key_name, bscatter, &len), 0);
/* Print the values */
printf("pixel lat lon backscatter \n");
printf("-------------------------------\n");
for(i=0; i < numObs; i++) {
printf("%4d %.3f %.3f %.3f \n", i+1,lat[i],lon[i],bscatter[i]);
for (i = 0; i < numObs; i++) {
printf("%4d %.3f %.3f %.3f \n", i + 1, lat[i], lon[i], bscatter[i]);
}
/* Delete handle */

View File

@ -24,67 +24,66 @@
#include "eccodes.h"
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
FILE* in = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
codes_handle* h = NULL;
long longVal;
double doubleVal;
int err=0;
int cnt=0;
int err = 0;
int cnt = 0;
const char* infile = "../../data/bufr/syno_multi.bufr";
in=fopen(infile,"rb");
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
/* we need to instruct ecCodes to unpack the data values */
CODES_CHECK(codes_set_long(h,"unpack",1),0);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
/* station id*/
CODES_CHECK(codes_get_long(h,"blockNumber",&longVal),0);
printf(" blockNumber: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "blockNumber", &longVal), 0);
printf(" blockNumber: %ld\n", longVal);
CODES_CHECK(codes_get_long(h,"stationNumber",&longVal),0);
printf(" stationNumber: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "stationNumber", &longVal), 0);
printf(" stationNumber: %ld\n", longVal);
/* location*/
CODES_CHECK(codes_get_double(h,"latitude",&doubleVal),0);
printf(" latitude: %f\n",doubleVal);
CODES_CHECK(codes_get_double(h, "latitude", &doubleVal), 0);
printf(" latitude: %f\n", doubleVal);
CODES_CHECK(codes_get_double(h,"longitude",&doubleVal),0);
printf(" longitude: %f\n",doubleVal);
CODES_CHECK(codes_get_double(h, "longitude", &doubleVal), 0);
printf(" longitude: %f\n", doubleVal);
/* 2m temperature */
CODES_CHECK(codes_get_double(h,"airTemperatureAt2M",&doubleVal),0);
printf(" airTemperatureAt2M: %f\n",doubleVal);
CODES_CHECK(codes_get_double(h, "airTemperatureAt2M", &doubleVal), 0);
printf(" airTemperatureAt2M: %f\n", doubleVal);
/* 2m dewpoint temperature */
CODES_CHECK(codes_get_double(h,"dewpointTemperatureAt2M",&doubleVal),0);
printf(" dewpointTemperatureAt2M: %f\n",doubleVal);
CODES_CHECK(codes_get_double(h, "dewpointTemperatureAt2M", &doubleVal), 0);
printf(" dewpointTemperatureAt2M: %f\n", doubleVal);
/* 10 wind */
CODES_CHECK(codes_get_double(h,"windSpeedAt10M",&doubleVal),0);
printf(" windSpeedAt10M: %f\n",doubleVal);
CODES_CHECK(codes_get_double(h, "windSpeedAt10M", &doubleVal), 0);
printf(" windSpeedAt10M: %f\n", doubleVal);
CODES_CHECK(codes_get_double(h,"windDirectionAt10M",&doubleVal),0);
printf(" windDirectionAt10M: %f\n",doubleVal);
CODES_CHECK(codes_get_double(h, "windDirectionAt10M", &doubleVal), 0);
printf(" windDirectionAt10M: %f\n", doubleVal);
/* The cloud information is stored in several blocks in the
* SYNOP message and the same key means a different thing in different
@ -95,24 +94,24 @@ int main(int argc,char* argv[])
* read so we will use the # (occurrence) operator accordingly. */
/* Cloud amount (low and middleclouds) */
CODES_CHECK(codes_get_long(h,"#1#cloudAmount",&longVal),0);
printf(" cloudAmount (low and middle): %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "#1#cloudAmount", &longVal), 0);
printf(" cloudAmount (low and middle): %ld\n", longVal);
/* Height of cloud base */
CODES_CHECK(codes_get_long(h,"#1#heightOfBaseOfCloud",&longVal),0);
printf(" heightOfBaseOfCloud: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "#1#heightOfBaseOfCloud", &longVal), 0);
printf(" heightOfBaseOfCloud: %ld\n", longVal);
/* Cloud type (low clouds) */
CODES_CHECK(codes_get_long(h,"#1#cloudType",&longVal),0);
printf(" cloudType (low): %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "#1#cloudType", &longVal), 0);
printf(" cloudType (low): %ld\n", longVal);
/* Cloud type (middle clouds) */
CODES_CHECK(codes_get_long(h,"#2#cloudType",&longVal),0);
printf(" cloudType (middle): %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "#2#cloudType", &longVal), 0);
printf(" cloudType (middle): %ld\n", longVal);
/* Cloud type (high clouds) */
CODES_CHECK(codes_get_long(h,"#3#cloudType",&longVal),0);
printf(" cloudType (high): %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "#3#cloudType", &longVal), 0);
printf(" cloudType (high): %ld\n", longVal);
/* delete handle */
codes_handle_delete(h);

View File

@ -23,41 +23,40 @@
#include "eccodes.h"
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
FILE* in = NULL;
/* Message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
codes_handle* h = NULL;
double *sigt_pres=NULL, *sigt_geo=NULL, *sigt_t=NULL;
double *sigt_td=NULL;
size_t sigt_len=0, len=0, i=0;
int err=0;
int cnt=0;
double *sigt_pres = NULL, *sigt_geo = NULL, *sigt_t = NULL;
double* sigt_td = NULL;
size_t sigt_len = 0, len = 0, i = 0;
int err = 0;
int cnt = 0;
const char* infile = "../../data/bufr/temp_101.bufr";
char key_name[128];
in=fopen(infile,"rb");
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
/* Loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
/* We need to instruct ecCodes to expand the descriptors
i.e. unpack the data values */
CODES_CHECK(codes_set_long(h,"unpack",1),0);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
/* In what follows we rely on the fact that for
* temperature significant levels the value of key
@ -75,67 +74,65 @@ int main(int argc,char* argv[])
/* We find out the number of temperature significant levels by
* counting how many pressure values we have on these levels.*/
sprintf(key_name,"/verticalSoundingSignificance=4/pressure");
CODES_CHECK(codes_get_size(h,key_name,&sigt_len),0);
sprintf(key_name, "/verticalSoundingSignificance=4/pressure");
CODES_CHECK(codes_get_size(h, key_name, &sigt_len), 0);
printf("Number of T significant levels: %lu\n",sigt_len);
printf("Number of T significant levels: %lu\n", sigt_len);
/* Allocate memory for the values to be read. Each
* parameter must have the same number of values. */
sigt_pres = (double*)malloc(sigt_len*sizeof(double));
sigt_geo = (double*)malloc(sigt_len*sizeof(double));
sigt_t = (double*)malloc(sigt_len*sizeof(double));
sigt_td = (double*)malloc(sigt_len*sizeof(double));
sigt_pres = (double*)malloc(sigt_len * sizeof(double));
sigt_geo = (double*)malloc(sigt_len * sizeof(double));
sigt_t = (double*)malloc(sigt_len * sizeof(double));
sigt_td = (double*)malloc(sigt_len * sizeof(double));
/* Get pressure */
sprintf(key_name,"/verticalSoundingSignificance=4/pressure");
len=sigt_len;
CODES_CHECK(codes_get_double_array(h,key_name,sigt_pres,&len),0);
sprintf(key_name, "/verticalSoundingSignificance=4/pressure");
len = sigt_len;
CODES_CHECK(codes_get_double_array(h, key_name, sigt_pres, &len), 0);
/* Get geopotential */
sprintf(key_name,"/verticalSoundingSignificance=4/nonCoordinateGeopotential");
sprintf(key_name, "/verticalSoundingSignificance=4/nonCoordinateGeopotential");
/* Check the size */
CODES_CHECK(codes_get_size(h,key_name,&len),0);
if(len != sigt_len)
{
CODES_CHECK(codes_get_size(h, key_name, &len), 0);
if (len != sigt_len) {
printf("inconsistent number of geopotential values found!\n");
return 1;
}
/* Get the values */
CODES_CHECK(codes_get_double_array(h,key_name,sigt_geo,&len),0);
CODES_CHECK(codes_get_double_array(h, key_name, sigt_geo, &len), 0);
/* Get temperature */
if(len != sigt_len) /* Check the size */
if (len != sigt_len) /* Check the size */
{
printf("inconsistent number of temperature values found!\n");
return 1;
}
/* Get the values */
sprintf(key_name,"/verticalSoundingSignificance=4/airTemperature");
CODES_CHECK(codes_get_double_array(h,key_name,sigt_t,&len),0);
sprintf(key_name, "/verticalSoundingSignificance=4/airTemperature");
CODES_CHECK(codes_get_double_array(h, key_name, sigt_t, &len), 0);
/* Get dew point */
if(len != sigt_len) /* Check the size */
if (len != sigt_len) /* Check the size */
{
printf("inconsistent number of dewpoint temperature values found!\n");
return 1;
}
/* Get the values */
sprintf(key_name,"/verticalSoundingSignificance=4/dewpointTemperature");
CODES_CHECK(codes_get_double_array(h,key_name,sigt_td,&len),0);
sprintf(key_name, "/verticalSoundingSignificance=4/dewpointTemperature");
CODES_CHECK(codes_get_double_array(h, key_name, sigt_td, &len), 0);
/* Print the values */
printf("lev pres geo t td\n");
printf("-------------------------------\n");
for(i=0; i < sigt_len; i++)
{
for (i = 0; i < sigt_len; i++) {
printf("%3lu %6.0f %6.0f %.1f %.1f\n",
i+1,sigt_pres[i],sigt_geo[i],sigt_t[i],sigt_td[i]);
i + 1, sigt_pres[i], sigt_geo[i], sigt_t[i], sigt_td[i]);
}
/* Delete handle */

View File

@ -17,34 +17,35 @@
#include "eccodes.h"
static void usage(const char* prog) {
printf("usage: %s infile\n",prog);
static void usage(const char* prog)
{
printf("usage: %s infile\n", prog);
exit(1);
}
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
FILE* in = NULL;
FILE* in = NULL;
FILE* out = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
codes_handle* h = NULL;
long longVal;
/*double doubleVal;*/
int err=0;
int cnt=0;
size_t size = 0;
int err = 0;
int cnt = 0;
size_t size = 0;
const char* infile = "../../data/bufr/syno_multi.bufr";
const void *buffer = NULL;
const void* buffer = NULL;
if (argc != 2) {
usage(argv[0]);
return 1;
}
in = fopen(infile,"rb");
out = fopen(argv[1],"wb");
in = fopen(infile, "rb");
out = fopen(argv[1], "wb");
if (!in || !out) {
perror("ERROR: unable to open files");
@ -54,15 +55,14 @@ int main(int argc,char* argv[])
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
/* we need to instruct ecCodes to expand the descriptors
i.e. unpack the data values */
@ -72,20 +72,20 @@ int main(int argc,char* argv[])
/*E.g. we change the centre */
/* set bufrHeaderCentre */
longVal=222;
CODES_CHECK(codes_set_long(h, "bufrHeaderCentre", longVal),0);
printf(" set bufrHeaderCentre to: %ld\n",longVal);
longVal = 222;
CODES_CHECK(codes_set_long(h, "bufrHeaderCentre", longVal), 0);
printf(" set bufrHeaderCentre to: %ld\n", longVal);
/* check bufrHeaderCentre */
CODES_CHECK(codes_get_long(h,"bufrHeaderCentre",&longVal),0);
printf(" bufrHeaderCentre's new value is: %ld\n",longVal);
CODES_CHECK(codes_get_long(h, "bufrHeaderCentre", &longVal), 0);
printf(" bufrHeaderCentre's new value is: %ld\n", longVal);
/* get the modified message in a buffer */
CODES_CHECK(codes_get_message(h,&buffer,&size),0);
CODES_CHECK(codes_get_message(h, &buffer, &size), 0);
/* write the buffer to a file */
if(fwrite(buffer,1,size,out) != size) {
if (fwrite(buffer, 1, size, out) != size) {
perror(argv[0]);
return 1;
}

View File

@ -18,74 +18,72 @@
#include "eccodes.h"
#include <assert.h>
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
char key[200]={0,};
char key[200] = {0,};
FILE* in = NULL;
/* message handle. Required in all the eccodes calls acting on a message.*/
codes_handle* h=NULL;
codes_handle* h = NULL;
long numberOfSubsets=0;
long numberOfSubsets = 0;
long longVal;
double doubleVal;
size_t stringLen;
char stringVal[100]={0,};
int i,err=0;
int cnt=0;
char stringVal[100] = {0,};
int i, err = 0;
int cnt = 0;
const char* infile = "../../data/bufr/synop_multi_subset.bufr";
in=fopen(infile,"rb");
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
/* loop over the messages in the bufr file */
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_BUFR,&err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_BUFR, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
/* we need to instruct ecCodes to expand all the descriptors i.e. unpack the data values */
CODES_CHECK(codes_set_long(h,"unpack",1),0);
CODES_CHECK(codes_set_long(h, "unpack", 1), 0);
/* find out the number of subsets */
CODES_CHECK(codes_get_long(h,"numberOfSubsets",&numberOfSubsets),0);
printf(" numberOfSubsets: %ld\n",numberOfSubsets);
CODES_CHECK(codes_get_long(h, "numberOfSubsets", &numberOfSubsets), 0);
printf(" numberOfSubsets: %ld\n", numberOfSubsets);
/* loop over the subsets */
for(i=1; i <= numberOfSubsets; i++)
{
sprintf(key,"/subsetNumber=%d/blockNumber",i);
for (i = 1; i <= numberOfSubsets; i++) {
sprintf(key, "/subsetNumber=%d/blockNumber", i);
printf(" subsetNumber=%d",i);
printf(" subsetNumber=%d", i);
/* read and print some data values */
CODES_CHECK(codes_get_long(h,key,&longVal), 0);
printf(" blockNumber=%ld",longVal);
CODES_CHECK(codes_get_long(h, key, &longVal), 0);
printf(" blockNumber=%ld", longVal);
sprintf(key,"/subsetNumber=%d/stationNumber",i);
CODES_CHECK(codes_get_long(h,key,&longVal), 0);
printf(" stationNumber=%ld",longVal);
sprintf(key, "/subsetNumber=%d/stationNumber", i);
CODES_CHECK(codes_get_long(h, key, &longVal), 0);
printf(" stationNumber=%ld", longVal);
sprintf(key,"/subsetNumber=%d/stationOrSiteName->units",i);
CODES_CHECK(codes_get_length(h,key,&stringLen), 0);
sprintf(key, "/subsetNumber=%d/stationOrSiteName->units", i);
CODES_CHECK(codes_get_length(h, key, &stringLen), 0);
assert(stringLen == 10); /* should be "CCITT IA5" */
sprintf(key,"/subsetNumber=%d/stationOrSiteName",i);
CODES_CHECK(codes_get_length(h,key,&stringLen), 0);
CODES_CHECK(codes_get_string(h,key,stringVal,&stringLen), 0);
sprintf(key, "/subsetNumber=%d/stationOrSiteName", i);
CODES_CHECK(codes_get_length(h, key, &stringLen), 0);
CODES_CHECK(codes_get_string(h, key, stringVal, &stringLen), 0);
assert(stringLen > 0 && stringLen < 17);
printf(" stationOrSiteName=\"%s\"",stringVal);
printf(" stationOrSiteName=\"%s\"", stringVal);
sprintf(key,"/subsetNumber=%d/airTemperature",i);
CODES_CHECK(codes_get_double(h,key,&doubleVal),0);
printf(" airTemperature=%g\n",doubleVal);
sprintf(key, "/subsetNumber=%d/airTemperature", i);
CODES_CHECK(codes_get_double(h, key, &doubleVal), 0);
printf(" airTemperature=%g\n", doubleVal);
assert(doubleVal > 265 && doubleVal < 278);
}

View File

@ -21,57 +21,58 @@
#include "eccodes.h"
void usage(char* prog) {
printf("Usage: %s order_by grib_file grib_file ...\n",prog);
void usage(char* prog)
{
printf("Usage: %s order_by grib_file grib_file ...\n", prog);
exit(1);
}
int main(int argc, char** argv)
{
int err = 0;
long step,levelType;
long step, levelType;
char** filenames;
size_t nkeys,nfiles;
int i=0;
char* keys[]={"step","date","param","levelType"};
size_t nkeys, nfiles;
int i = 0;
char* keys[] = { "step", "date", "param", "levelType" };
codes_fieldset* set;
codes_handle* h;
char param[20]={0,};
char date[10]={0,};
size_t datelen=10;
size_t len=20;
char* order_by=0;
char param[20] = {0,};
char date[10] = {0,};
size_t datelen = 10;
size_t len = 20;
char* order_by = 0;
if (argc != 3) usage(argv[0]);
nkeys=sizeof(keys)/sizeof(*keys);
order_by=strdup(argv[1]);
nkeys = sizeof(keys) / sizeof(*keys);
order_by = strdup(argv[1]);
nfiles=argc-2;
filenames=(char**)malloc(sizeof(char*)*nfiles);
for (i=0;i<nfiles;i++)
filenames[i]=(char*)strdup(argv[i+2]);
nfiles = argc - 2;
filenames = (char**)malloc(sizeof(char*) * nfiles);
for (i = 0; i < nfiles; i++)
filenames[i] = (char*)strdup(argv[i + 2]);
set=codes_fieldset_new_from_files(0,filenames,nfiles,keys,nkeys,0,0,&err);
CODES_CHECK(err,0);
set = codes_fieldset_new_from_files(0, filenames, nfiles, keys, nkeys, 0, 0, &err);
CODES_CHECK(err, 0);
/* not jet implemented */
/* err=codes_fieldset_apply_where(set,"(centre=='ecmf') && number==1 || step==6 "); */
/* CODES_CHECK(err,0); */
codes_fieldset_apply_order_by(set,order_by);
CODES_CHECK(err,0);
codes_fieldset_apply_order_by(set, order_by);
CODES_CHECK(err, 0);
printf("\nordering by %s\n",order_by);
printf("\n%d fields in the fieldset\n",codes_fieldset_count(set));
printf("\nordering by %s\n", order_by);
printf("\n%d fields in the fieldset\n", codes_fieldset_count(set));
printf("step,date,levelType,levelType\n");
while ((h=codes_fieldset_next_handle(set,&err))!=NULL) {
CODES_CHECK(codes_get_long(h,"step",&step),0);
CODES_CHECK(codes_get_string(h,"date",date,&datelen),0);
CODES_CHECK(codes_get_string(h,"param",param,&len),0);
CODES_CHECK(codes_get_long(h,"levelType",&levelType),0);
while ((h = codes_fieldset_next_handle(set, &err)) != NULL) {
CODES_CHECK(codes_get_long(h, "step", &step), 0);
CODES_CHECK(codes_get_string(h, "date", date, &datelen), 0);
CODES_CHECK(codes_get_string(h, "param", param, &len), 0);
CODES_CHECK(codes_get_long(h, "levelType", &levelType), 0);
printf("%ld %s %ld %s\n",step,date,levelType,param);
printf("%ld %s %ld %s\n", step, date, levelType, param);
codes_handle_delete(h);
}

View File

@ -15,18 +15,18 @@
* and print the kind of product (e.g. GRIB, BUFR etc)
*/
#include "eccodes.h"
static void usage(const char *app)
static void usage(const char* app)
{
fprintf(stderr,"Usage: %s file\n", app);
fprintf(stderr, "Usage: %s file\n", app);
}
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
FILE* in = NULL;
codes_handle* h=NULL;
char* kind= NULL;
size_t len=0;
int err=0, cnt=0;
FILE* in = NULL;
codes_handle* h = NULL;
char* kind = NULL;
size_t len = 0;
int err = 0, cnt = 0;
char* infile = NULL;
if (argc != 2) {
@ -34,23 +34,22 @@ int main(int argc,char* argv[])
return 1;
}
infile = argv[1];
in=fopen(infile,"rb");
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
while ((h = codes_handle_new_from_file(NULL,in,PRODUCT_ANY,&err)) != NULL || err != CODES_SUCCESS)
{
while ((h = codes_handle_new_from_file(NULL, in, PRODUCT_ANY, &err)) != NULL || err != CODES_SUCCESS) {
if (h == NULL) {
printf("Error: unable to create handle for message %d\n",cnt);
printf("Error: unable to create handle for message %d\n", cnt);
cnt++;
continue;
}
printf("message: %d\n",cnt);
printf("message: %d\n", cnt);
CODES_CHECK(codes_get_length(h, "kindOfProduct", &len), 0);
kind = (char*)malloc(len*sizeof(char));
kind = (char*)malloc(len * sizeof(char));
codes_get_string(h, "kindOfProduct", kind, &len);
printf(" product: %s\n", kind);

View File

@ -18,27 +18,27 @@
#include <stdio.h>
#include "eccodes.h"
static void usage(const char *app)
static void usage(const char* app)
{
fprintf(stderr,"Usage is: %s input_file ouput_file\n", app);
fprintf(stderr, "Usage is: %s input_file ouput_file\n", app);
}
int main(int argc, char *argv[])
int main(int argc, char* argv[])
{
FILE *in = NULL;
FILE *out = NULL;
codes_handle *source_handle = NULL;
const void *buffer = NULL;
size_t size = 0;
int err = 0;
FILE* in = NULL;
FILE* out = NULL;
codes_handle* source_handle = NULL;
const void* buffer = NULL;
size_t size = 0;
int err = 0;
if (argc != 3) {
usage(argv[0]);
return 1;
}
in = fopen(argv[1],"rb");
out = fopen(argv[2],"wb");
in = fopen(argv[1], "rb");
out = fopen(argv[2], "wb");
if (!in || !out) {
perror("ERROR: unable to open files");
@ -48,9 +48,8 @@ int main(int argc, char *argv[])
}
/* loop over the messages in the source grib and clone them */
while ((source_handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err))!=NULL)
{
codes_handle *clone_handle = codes_handle_clone(source_handle);
while ((source_handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL) {
codes_handle* clone_handle = codes_handle_clone(source_handle);
if (clone_handle == NULL) {
perror("ERROR: could not clone field");
@ -64,9 +63,9 @@ int main(int argc, char *argv[])
*/
/* get the coded message in a buffer */
CODES_CHECK(codes_get_message(clone_handle,&buffer,&size),0);
CODES_CHECK(codes_get_message(clone_handle, &buffer, &size), 0);
/* write the buffer to a file */
if(fwrite(buffer,1,size,out) != size) {
if (fwrite(buffer, 1, size, out) != size) {
perror(argv[1]);
return 1;
}

View File

@ -18,23 +18,23 @@
#include "eccodes.h"
#include <assert.h>
static void usage(const char *prog)
static void usage(const char* prog)
{
fprintf(stderr,"Usage is: %s input_file ouput_file\n", prog);
fprintf(stderr, "Usage is: %s input_file ouput_file\n", prog);
}
int main(int argc, char *argv[])
int main(int argc, char* argv[])
{
FILE *in = NULL;
codes_handle *source_handle = NULL;
int err = 0;
FILE* in = NULL;
codes_handle* source_handle = NULL;
int err = 0;
if (argc != 3) {
usage(argv[0]);
return 1;
}
in = fopen(argv[1],"rb");
in = fopen(argv[1], "rb");
if (!in) {
perror("ERROR: unable to input file");
@ -42,16 +42,15 @@ int main(int argc, char *argv[])
}
/* loop over the GRIB messages in the source */
while ((source_handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err))!=NULL)
{
while ((source_handle = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL) {
size_t totalLength = 0, size = 0;
const void* buffer = NULL;
const void* buffer = NULL;
codes_handle* new_handle = NULL;
CODES_CHECK(codes_get_message_size(source_handle,&totalLength),0);
buffer=(unsigned char*)malloc(totalLength*sizeof(char));
CODES_CHECK(codes_get_message_size(source_handle, &totalLength), 0);
buffer = (unsigned char*)malloc(totalLength * sizeof(char));
CODES_CHECK(codes_get_message(source_handle, &buffer, &size),0);
CODES_CHECK(codes_get_message(source_handle, &buffer, &size), 0);
assert(size == totalLength);
new_handle = codes_handle_new_from_message(0, buffer, totalLength);
@ -60,8 +59,8 @@ int main(int argc, char *argv[])
perror("ERROR: could not create GRIB handle from message");
return 1;
}
CODES_CHECK(codes_set_long(new_handle, "hour", 18),0);
CODES_CHECK(codes_write_message(new_handle, argv[2], "w"),0);
CODES_CHECK(codes_set_long(new_handle, "hour", 18), 0);
CODES_CHECK(codes_write_message(new_handle, argv[2], "w"), 0);
codes_handle_delete(new_handle);
codes_handle_delete(source_handle);

View File

@ -17,41 +17,41 @@
#include "eccodes.h"
int main(int argc, char * argv[])
int main(int argc, char* argv[])
{
int ret;
int i, j;
int count=0;
size_t paramIdSize, numberSize, values_len=0;
int count = 0;
size_t paramIdSize, numberSize, values_len = 0;
char** paramId;
long* number;
double* values;
double* result=NULL;
double min=1e13,max=-1e13,avg=0;
double* result = NULL;
double min = 1e13, max = -1e13, avg = 0;
codes_index* index;
codes_handle* h=NULL;
codes_handle* h = NULL;
if (argc<2) return 1;
if (argc < 2) return 1;
/* create index of file contents for paramId and number */
index = codes_index_new_from_file(0, argv[1], "paramId,number",&ret);
CODES_CHECK(ret,0);
index = codes_index_new_from_file(0, argv[1], "paramId,number", &ret);
CODES_CHECK(ret, 0);
/* get size of "paramId" list */
CODES_CHECK(codes_index_get_size(index, "paramId", &paramIdSize),0);
printf("grib contains %lu different parameters\n",paramIdSize);
CODES_CHECK(codes_index_get_size(index, "paramId", &paramIdSize), 0);
printf("grib contains %lu different parameters\n", paramIdSize);
/* allocate memory for "paramId" list */
paramId = (char**) malloc(paramIdSize * sizeof(char*));
paramId = (char**)malloc(paramIdSize * sizeof(char*));
/* get list of "paramId" */
CODES_CHECK(codes_index_get_string(index, "paramId", paramId, &paramIdSize),0);
CODES_CHECK(codes_index_get_string(index, "paramId", paramId, &paramIdSize), 0);
/* get size of ensemble number list */
CODES_CHECK(codes_index_get_size(index, "number", &numberSize),0);
printf("GRIB contains %lu different ensemble members\n",numberSize);
CODES_CHECK(codes_index_get_size(index, "number", &numberSize), 0);
printf("GRIB contains %lu different ensemble members\n", numberSize);
/* allocate memory for ensemble number list */
number = (long*) malloc(numberSize * sizeof(long));
number = (long*)malloc(numberSize * sizeof(long));
/* get list of ensemble numbers */
CODES_CHECK(codes_index_get_long(index, "number", number, &numberSize),0);
CODES_CHECK(codes_index_get_long(index, "number", number, &numberSize), 0);
/* select T850 with paramId 130 */
CODES_CHECK(codes_index_select_string(index, "paramId", "130"), 0);
@ -63,7 +63,7 @@ int main(int argc, char * argv[])
CODES_CHECK(codes_index_select_long(index, "number", number[i]), 0);
/* create handle for next GRIB message */
h=codes_handle_new_from_index(index, &ret);
h = codes_handle_new_from_index(index, &ret);
if (ret) {
printf("Error: %s\n", codes_get_error_message(ret));
exit(ret);
@ -76,8 +76,8 @@ int main(int argc, char * argv[])
values = (double*)malloc(values_len * sizeof(double));
/* allocate memory for result */
if ( i == 0 ) {
result = (double *)calloc(values_len, sizeof(double));
if (i == 0) {
result = (double*)calloc(values_len, sizeof(double));
}
/* get data values */
@ -106,15 +106,15 @@ int main(int argc, char * argv[])
}
avg += result[j];
}
avg = avg/values_len;
avg = avg / values_len;
printf("==============================================================================\n");
printf("Stats for ensemble mean of T850\n");
printf("Min: %f Max: %f Avg: %f\n",min,max,avg);
printf("Min: %f Max: %f Avg: %f\n", min, max, avg);
printf("==============================================================================\n");
/* finally free all other memory */
for (i=0;i<paramIdSize;i++)
for (i = 0; i < paramIdSize; i++)
free(paramId[i]);
free(paramId);
free(number);

View File

@ -19,65 +19,65 @@
#include "eccodes.h"
int main (int argc, char **argv)
int main(int argc, char** argv)
{
int err = 0;
size_t i = 0;
FILE *in = NULL;
const char *filename = "../../data/reduced_latlon_surface.grib1";
codes_handle *h = NULL;
long numberOfPoints = 0;
int err = 0;
size_t i = 0;
FILE* in = NULL;
const char* filename = "../../data/reduced_latlon_surface.grib1";
codes_handle* h = NULL;
long numberOfPoints = 0;
const double missing = 9999.0;
double *lats, *lons, *values; /* arrays */
double *lats, *lons, *values; /* arrays */
in = fopen(filename, "rb");
if (!in) {
printf ("ERROR: unable to open input file %s\n", filename);
printf("ERROR: unable to open input file %s\n", filename);
return 1;
}
/* create new handle from a message in a file */
h = codes_handle_new_from_file (0, in, PRODUCT_GRIB, &err);
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf ("Error: unable to create handle from file %s\n", filename);
printf("Error: unable to create handle from file %s\n", filename);
return 1;
}
CODES_CHECK (codes_get_long (h, "numberOfPoints", &numberOfPoints), 0);
CODES_CHECK (codes_set_double (h, "missingValue", missing), 0);
CODES_CHECK(codes_get_long(h, "numberOfPoints", &numberOfPoints), 0);
CODES_CHECK(codes_set_double(h, "missingValue", missing), 0);
lats = (double *) malloc (numberOfPoints * sizeof (double));
lats = (double*)malloc(numberOfPoints * sizeof(double));
if (!lats) {
printf ("unable to allocate %ld bytes\n", (long) (numberOfPoints * sizeof (double)));
printf("unable to allocate %ld bytes\n", (long)(numberOfPoints * sizeof(double)));
return 1;
}
lons = (double *) malloc (numberOfPoints * sizeof (double));
lons = (double*)malloc(numberOfPoints * sizeof(double));
if (!lons) {
printf ("unable to allocate %ld bytes\n", (long) (numberOfPoints * sizeof (double)));
free (lats);
printf("unable to allocate %ld bytes\n", (long)(numberOfPoints * sizeof(double)));
free(lats);
return 1;
}
values = (double *) malloc (numberOfPoints * sizeof (double));
values = (double*)malloc(numberOfPoints * sizeof(double));
if (!values) {
printf ("unable to allocate %ld bytes\n", (long) (numberOfPoints * sizeof (double)));
free (lats);
free (lons);
printf("unable to allocate %ld bytes\n", (long)(numberOfPoints * sizeof(double)));
free(lats);
free(lons);
return 1;
}
CODES_CHECK (codes_grib_get_data (h, lats, lons, values), 0);
CODES_CHECK(codes_grib_get_data(h, lats, lons, values), 0);
for (i = 0; i < numberOfPoints; ++i) {
if (values[i] != missing) {
printf ("%f %f %f\n", lats[i], lons[i], values[i]);
printf("%f %f %f\n", lats[i], lons[i], values[i]);
}
}
free (lats);
free (lons);
free (values);
codes_handle_delete (h);
free(lats);
free(lons);
free(values);
codes_handle_delete(h);
fclose (in);
fclose(in);
return 0;
}

View File

@ -21,9 +21,9 @@
int main(int argc, char** argv)
{
int err = 0;
double *values = NULL;
size_t values_len= 0;
int err = 0;
double* values = NULL;
size_t values_len = 0;
size_t i = 0, len = 0;
double latitudeOfFirstGridPointInDegrees;
@ -37,23 +37,23 @@ int main(int argc, char** argv)
long numberOfPointsAlongAParallel;
long numberOfPointsAlongAMeridian;
double average = 0;
double average = 0;
char* packingType = NULL;
FILE* in = NULL;
FILE* in = NULL;
const char* filename = "../../data/regular_latlon_surface.grib1";
codes_handle *h = NULL;
codes_handle* h = NULL;
in = fopen(filename,"rb");
if(!in) {
printf("ERROR: unable to open file %s\n",filename);
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
/* create new handle from the first message in the file*/
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n",filename);
printf("Error: unable to create handle from file %s\n", filename);
return 1;
}
fclose(in);
@ -63,68 +63,68 @@ int main(int argc, char** argv)
CODES_CHECK(codes_set_string(h, "file", filename, &len), 0);
/* get as a long*/
CODES_CHECK(codes_get_long(h,"Ni",&numberOfPointsAlongAParallel),0);
printf("numberOfPointsAlongAParallel=%ld\n",numberOfPointsAlongAParallel);
CODES_CHECK(codes_get_long(h, "Ni", &numberOfPointsAlongAParallel), 0);
printf("numberOfPointsAlongAParallel=%ld\n", numberOfPointsAlongAParallel);
/* get as a long*/
CODES_CHECK(codes_get_long(h,"Nj",&numberOfPointsAlongAMeridian),0);
printf("numberOfPointsAlongAMeridian=%ld\n",numberOfPointsAlongAMeridian);
CODES_CHECK(codes_get_long(h, "Nj", &numberOfPointsAlongAMeridian), 0);
printf("numberOfPointsAlongAMeridian=%ld\n", numberOfPointsAlongAMeridian);
/* get as a double*/
CODES_CHECK(codes_get_double(h,"latitudeOfFirstGridPointInDegrees",&latitudeOfFirstGridPointInDegrees),0);
printf("latitudeOfFirstGridPointInDegrees=%g\n",latitudeOfFirstGridPointInDegrees);
CODES_CHECK(codes_get_double(h, "latitudeOfFirstGridPointInDegrees", &latitudeOfFirstGridPointInDegrees), 0);
printf("latitudeOfFirstGridPointInDegrees=%g\n", latitudeOfFirstGridPointInDegrees);
/* get as a double*/
CODES_CHECK(codes_get_double(h,"longitudeOfFirstGridPointInDegrees",&longitudeOfFirstGridPointInDegrees),0);
printf("longitudeOfFirstGridPointInDegrees=%g\n",longitudeOfFirstGridPointInDegrees);
CODES_CHECK(codes_get_double(h, "longitudeOfFirstGridPointInDegrees", &longitudeOfFirstGridPointInDegrees), 0);
printf("longitudeOfFirstGridPointInDegrees=%g\n", longitudeOfFirstGridPointInDegrees);
/* get as a double*/
CODES_CHECK(codes_get_double(h,"latitudeOfLastGridPointInDegrees",&latitudeOfLastGridPointInDegrees),0);
printf("latitudeOfLastGridPointInDegrees=%g\n",latitudeOfLastGridPointInDegrees);
CODES_CHECK(codes_get_double(h, "latitudeOfLastGridPointInDegrees", &latitudeOfLastGridPointInDegrees), 0);
printf("latitudeOfLastGridPointInDegrees=%g\n", latitudeOfLastGridPointInDegrees);
/* get as a double*/
CODES_CHECK(codes_get_double(h,"longitudeOfLastGridPointInDegrees",&longitudeOfLastGridPointInDegrees),0);
printf("longitudeOfLastGridPointInDegrees=%g\n",longitudeOfLastGridPointInDegrees);
CODES_CHECK(codes_get_double(h, "longitudeOfLastGridPointInDegrees", &longitudeOfLastGridPointInDegrees), 0);
printf("longitudeOfLastGridPointInDegrees=%g\n", longitudeOfLastGridPointInDegrees);
/* get as a double*/
CODES_CHECK(codes_get_double(h,"jDirectionIncrementInDegrees",&jDirectionIncrementInDegrees),0);
printf("jDirectionIncrementInDegrees=%g\n",jDirectionIncrementInDegrees);
CODES_CHECK(codes_get_double(h, "jDirectionIncrementInDegrees", &jDirectionIncrementInDegrees), 0);
printf("jDirectionIncrementInDegrees=%g\n", jDirectionIncrementInDegrees);
/* get as a double*/
CODES_CHECK(codes_get_double(h,"iDirectionIncrementInDegrees",&iDirectionIncrementInDegrees),0);
printf("iDirectionIncrementInDegrees=%g\n",iDirectionIncrementInDegrees);
CODES_CHECK(codes_get_double(h, "iDirectionIncrementInDegrees", &iDirectionIncrementInDegrees), 0);
printf("iDirectionIncrementInDegrees=%g\n", iDirectionIncrementInDegrees);
/* get as string */
CODES_CHECK(codes_get_length(h, "packingType", &len), 0);
packingType = (char*)malloc(len*sizeof(char));
packingType = (char*)malloc(len * sizeof(char));
codes_get_string(h, "packingType", packingType, &len);
printf("packingType=%s\n", packingType);
free(packingType);
/* get the size of the values array*/
CODES_CHECK(codes_get_size(h,"values",&values_len),0);
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
values = (double*)malloc(values_len*sizeof(double));
values = (double*)malloc(values_len * sizeof(double));
/* get data values*/
CODES_CHECK(codes_get_double_array(h,"values",values,&values_len),0);
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len), 0);
average = 0;
for(i = 0; i < values_len; i++)
for (i = 0; i < values_len; i++)
average += values[i];
average /=(double)values_len;
average /= (double)values_len;
free(values);
printf("There are %d values, average is %g\n",(int)values_len,average);
printf("There are %d values, average is %g\n", (int)values_len, average);
{
int eq = 0;
/* Now retrieve the value of the key "file" */
char file[256]={0,};
CODES_CHECK(codes_get_length(h, "file", &len),0);
assert(len == 1+strlen(filename));
char file[256] = {0,};
CODES_CHECK(codes_get_length(h, "file", &len), 0);
assert(len == 1 + strlen(filename));
codes_get_string(h, "file", file, &len);
eq = strcmp(file, filename);
if (eq != 0) assert(!"file and filename not equal");
@ -132,11 +132,11 @@ int main(int argc, char** argv)
{
/* Example of getting bytes */
const char* name = "reservedNeedNotBePresent";
unsigned char* byte_val = NULL ;
size_t keySize = 0;
const char* name = "reservedNeedNotBePresent";
unsigned char* byte_val = NULL;
size_t keySize = 0;
CODES_CHECK(codes_get_size(h, name, &keySize), 0);
byte_val = (unsigned char*)malloc(keySize*sizeof(char));
byte_val = (unsigned char*)malloc(keySize * sizeof(char));
GRIB_CHECK(codes_get_bytes(h, name, byte_val, &keySize), name);
}

View File

@ -17,125 +17,142 @@
#include "eccodes.h"
static void usage(const char* prog) {
printf("usage: %s infile\n",prog);
static void usage(const char* prog)
{
printf("usage: %s infile\n", prog);
exit(1);
}
int main(int argc,char* argv[])
int main(int argc, char* argv[])
{
codes_index* index=NULL;
codes_handle* h=NULL;
char* infile=NULL;
long *steps,*levels,*numbers; /* arrays */
char** shortName=NULL;
int i,j,k,l;
size_t stepSize,levelSize,shortNameSize,numberSize;
long ostep,olevel,onumber;
codes_index* index = NULL;
codes_handle* h = NULL;
char* infile = NULL;
long *steps, *levels, *numbers; /* arrays */
char** shortName = NULL;
int i, j, k, l;
size_t stepSize, levelSize, shortNameSize, numberSize;
long ostep, olevel, onumber;
char oshortName[200];
size_t lenshortName=200;
int ret=0,count=0;
size_t lenshortName = 200;
int ret = 0, count = 0;
if (argc != 2) usage(argv[0]);
infile=argv[1];
infile = argv[1];
printf("indexing...\n");
/* Create an index given set of keys*/
index=codes_index_new(0,"shortName,level,number,step",&ret);
if (ret) {printf("error: %s\n",codes_get_error_message(ret)); exit(ret);}
index = codes_index_new(0, "shortName,level,number,step", &ret);
if (ret) {
printf("error: %s\n", codes_get_error_message(ret));
exit(ret);
}
/* Indexes a file */
ret=codes_index_add_file(index,infile);
if (ret) {printf("error: %s\n",codes_get_error_message(ret)); exit(ret);}
ret = codes_index_add_file(index, infile);
if (ret) {
printf("error: %s\n", codes_get_error_message(ret));
exit(ret);
}
printf("end indexing...\n");
/* get the number of distinct values of "step" in the index */
CODES_CHECK(codes_index_get_size(index,"step",&stepSize),0);
steps=(long*)malloc(sizeof(long)*stepSize);
CODES_CHECK(codes_index_get_size(index, "step", &stepSize), 0);
steps = (long*)malloc(sizeof(long) * stepSize);
if (!steps) exit(1);
/* get the list of distinct steps from the index */
/* the list is in ascending order */
CODES_CHECK(codes_index_get_long(index,"step",steps,&stepSize),0);
printf("stepSize=%ld\n",(long)stepSize);
for (i=0;i<stepSize;i++) printf("%ld ",steps[i]);
CODES_CHECK(codes_index_get_long(index, "step", steps, &stepSize), 0);
printf("stepSize=%ld\n", (long)stepSize);
for (i = 0; i < stepSize; i++)
printf("%ld ", steps[i]);
printf("\n");
/*same as for "step"*/
CODES_CHECK(codes_index_get_size(index,"level",&levelSize),0);
levels=(long*)malloc(sizeof(long)*levelSize);
CODES_CHECK(codes_index_get_size(index, "level", &levelSize), 0);
levels = (long*)malloc(sizeof(long) * levelSize);
if (!levels) exit(1);
/*same as for "step"*/
CODES_CHECK(codes_index_get_long(index,"level",levels,&levelSize),0);
printf("levelSize=%ld\n",(long)levelSize);
for (i=0;i<levelSize;i++) printf("%ld ",levels[i]);
CODES_CHECK(codes_index_get_long(index, "level", levels, &levelSize), 0);
printf("levelSize=%ld\n", (long)levelSize);
for (i = 0; i < levelSize; i++)
printf("%ld ", levels[i]);
printf("\n");
/*same as for "step"*/
CODES_CHECK(codes_index_get_size(index,"number",&numberSize),0);
numbers=(long*)malloc(sizeof(long)*numberSize);
CODES_CHECK(codes_index_get_size(index, "number", &numberSize), 0);
numbers = (long*)malloc(sizeof(long) * numberSize);
if (!numbers) exit(1);
/*same as for "step"*/
CODES_CHECK(codes_index_get_long(index,"number",numbers,&numberSize),0);
printf("numberSize=%ld\n",(long)numberSize);
for (i=0;i<numberSize;i++) printf("%ld ",numbers[i]);
CODES_CHECK(codes_index_get_long(index, "number", numbers, &numberSize), 0);
printf("numberSize=%ld\n", (long)numberSize);
for (i = 0; i < numberSize; i++)
printf("%ld ", numbers[i]);
printf("\n");
/*same as for "step"*/
CODES_CHECK(codes_index_get_size(index,"shortName",&shortNameSize),0);
shortName=(char**)malloc(sizeof(char*)*shortNameSize);
CODES_CHECK(codes_index_get_size(index, "shortName", &shortNameSize), 0);
shortName = (char**)malloc(sizeof(char*) * shortNameSize);
if (!shortName) exit(1);
/*same as for "step"*/
CODES_CHECK(codes_index_get_string(index,"shortName",shortName,&shortNameSize),0);
printf("shortNameSize=%ld\n",(long)shortNameSize);
for (i=0;i<shortNameSize;i++) printf("%s ",shortName[i]);
CODES_CHECK(codes_index_get_string(index, "shortName", shortName, &shortNameSize), 0);
printf("shortNameSize=%ld\n", (long)shortNameSize);
for (i = 0; i < shortNameSize; i++)
printf("%s ", shortName[i]);
printf("\n");
count=0;
count = 0;
/* nested loops on the keys values of the index */
/* different order of the nested loops doesn't affect performance*/
for (i=0;i<shortNameSize;i++) {
for (i = 0; i < shortNameSize; i++) {
/* select the GRIB with shortName=shortName[i] */
codes_index_select_string(index,"shortName",shortName[i]);
codes_index_select_string(index, "shortName", shortName[i]);
for (l=0;l<levelSize;l++) {
for (l = 0; l < levelSize; l++) {
/* select the GRIB with level=levels[i] */
codes_index_select_long(index,"level",levels[l]);
codes_index_select_long(index, "level", levels[l]);
for (j=0;j<numberSize;j++) {
for (j = 0; j < numberSize; j++) {
/* select the GRIB with number=numbers[i] */
codes_index_select_long(index,"number",numbers[j]);
codes_index_select_long(index, "number", numbers[j]);
for (k=0;k<stepSize;k++) {
for (k = 0; k < stepSize; k++) {
/* select the GRIB with step=steps[i] */
codes_index_select_long(index,"step",steps[k]);
codes_index_select_long(index, "step", steps[k]);
/* create a new codes_handle from the index with the constraints
imposed by the select statements. It is a loop because
in the index there could be more than one GRIB with those
constraints */
while ((h=codes_handle_new_from_index(index,&ret))!=NULL){
while ((h = codes_handle_new_from_index(index, &ret)) != NULL) {
count++;
if (ret) {printf("error: %d\n",ret); exit(ret);}
lenshortName=200;
codes_get_string(h,"shortName",oshortName,&lenshortName);
codes_get_long(h,"level",&olevel);
codes_get_long(h,"number",&onumber);
codes_get_long(h,"step",&ostep);
printf("shortName=%s ",oshortName);
printf("level=%ld ",olevel);
printf("number=%ld ",onumber);
printf("step=%ld \n",ostep);
if (ret) {
printf("error: %d\n", ret);
exit(ret);
}
lenshortName = 200;
codes_get_string(h, "shortName", oshortName, &lenshortName);
codes_get_long(h, "level", &olevel);
codes_get_long(h, "number", &onumber);
codes_get_long(h, "step", &ostep);
printf("shortName=%s ", oshortName);
printf("level=%ld ", olevel);
printf("number=%ld ", onumber);
printf("step=%ld \n", ostep);
codes_handle_delete(h);
}
if (ret && ret!=GRIB_END_OF_INDEX ) {printf("error: %s\n",codes_get_error_message(ret)); exit(ret);}
if (ret && ret != GRIB_END_OF_INDEX) {
printf("error: %s\n", codes_get_error_message(ret));
exit(ret);
}
}
}
}
}
printf(" %d messages selected\n",count);
printf(" %d messages selected\n", count);
codes_index_write(index,"out.gribidx");
codes_index_write(index, "out.gribidx");
codes_index_delete(index);
return 0;

View File

@ -20,57 +20,59 @@
#include "eccodes.h"
static void usage(const char* prog) {
printf("Usage: %s grib_file\n",prog);
static void usage(const char* prog)
{
printf("Usage: %s grib_file\n", prog);
exit(1);
}
int main(int argc, char** argv)
{
FILE* in = NULL;
int err = 0;
double lat,lon,value;
int err = 0;
double lat, lon, value;
double missingValue = 1e+20; /* A value out of range */
int n=0;
char* filename = NULL;
int n = 0;
char* filename = NULL;
/* Message handle. Required in all the ecCodes calls acting on a message.*/
codes_handle *h = NULL;
codes_handle* h = NULL;
/* Iterator on lat/lon/values.*/
codes_iterator* iter=NULL;
codes_iterator* iter = NULL;
if (argc != 2) usage(argv[0]);
filename=argv[1];
filename = argv[1];
in = fopen(filename,"rb");
if(!in) {
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
/* Loop on all the messages in a file.*/
while ((h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL ) {
while ((h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL) {
/* Check of errors after reading a message. */
if (err != CODES_SUCCESS) CODES_CHECK(err,0);
if (err != CODES_SUCCESS) CODES_CHECK(err, 0);
/* Set the double representing the missing value in the field. */
/* Choose a missingValue that does not correspond to any real value in the data array */
CODES_CHECK(codes_set_double(h, "missingValue", missingValue),0);
CODES_CHECK(codes_set_double(h, "missingValue", missingValue), 0);
/* A new iterator on lat/lon/values is created from the message handle h. */
iter=codes_grib_iterator_new(h,0,&err);
if (err != CODES_SUCCESS) CODES_CHECK(err,0);
iter = codes_grib_iterator_new(h, 0, &err);
if (err != CODES_SUCCESS) CODES_CHECK(err, 0);
n = 0;
/* Loop on all the lat/lon/values. */
while(codes_grib_iterator_next(iter,&lat,&lon,&value)) {
while (codes_grib_iterator_next(iter, &lat, &lon, &value)) {
/* You can now print lat and lon, */
printf("- %d - lat=%f lon=%f value=",n,lat,lon);
printf("- %d - lat=%f lon=%f value=", n, lat, lon);
/* decide what to print if a missing value is found. */
if (value == missingValue ) printf("missing\n");
if (value == missingValue) printf("missing\n");
/* and print the value if is not missing. */
else printf("%f\n",value);
else
printf("%f\n", value);
n++;
}

View File

@ -21,64 +21,61 @@
static void usage(const char* prog)
{
printf("Usage: %s grib_file\n",prog);
printf("Usage: %s grib_file\n", prog);
exit(1);
}
int main(int argc, char** argv)
{
FILE* in = NULL;
int err = 0;
double lat,lon,value;
int n=0;
int err = 0;
double lat, lon, value;
int n = 0;
size_t bmp_len = 0, values_len = 0;
char* filename = NULL;
long bitmapPresent = 0;
long *bitmap = NULL;
double *values = NULL;
codes_handle *h = NULL;
codes_iterator* iter=NULL;
char* filename = NULL;
long bitmapPresent = 0;
long* bitmap = NULL;
double* values = NULL;
codes_handle* h = NULL;
codes_iterator* iter = NULL;
if (argc != 2) usage(argv[0]);
filename=argv[1];
in = fopen(filename,"rb");
if(!in) {
fprintf(stderr, "ERROR: unable to open file %s\n",filename);
filename = argv[1];
in = fopen(filename, "rb");
if (!in) {
fprintf(stderr, "ERROR: unable to open file %s\n", filename);
return 1;
}
while ((h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL )
{
if (err != CODES_SUCCESS) CODES_CHECK(err,0);
while ((h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL) {
if (err != CODES_SUCCESS) CODES_CHECK(err, 0);
CODES_CHECK(codes_get_long(h,"bitmapPresent",&bitmapPresent),0);
if (bitmapPresent)
{
CODES_CHECK(codes_get_size(h,"bitmap",&bmp_len),0);
bitmap = (long*)malloc(bmp_len*sizeof(long));
CODES_CHECK(codes_get_long_array(h,"bitmap",bitmap,&bmp_len),0);
CODES_CHECK(codes_get_long(h, "bitmapPresent", &bitmapPresent), 0);
if (bitmapPresent) {
CODES_CHECK(codes_get_size(h, "bitmap", &bmp_len), 0);
bitmap = (long*)malloc(bmp_len * sizeof(long));
CODES_CHECK(codes_get_long_array(h, "bitmap", bitmap, &bmp_len), 0);
printf("Bitmap is present. Num = %lu\n", bmp_len);
}
/* Sanity check. Number of values must match number in bitmap */
CODES_CHECK(codes_get_size(h,"values",&values_len),0);
values = (double*)malloc(values_len*sizeof(double));
CODES_CHECK(codes_get_double_array(h,"values",values,&values_len),0);
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
values = (double*)malloc(values_len * sizeof(double));
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len), 0);
if (bitmapPresent) {
assert(values_len==bmp_len);
assert(values_len == bmp_len);
}
/* A new iterator on lat/lon/values is created from the message handle h */
iter=codes_grib_iterator_new(h,0,&err);
if (err != CODES_SUCCESS) CODES_CHECK(err,0);
iter = codes_grib_iterator_new(h, 0, &err);
if (err != CODES_SUCCESS) CODES_CHECK(err, 0);
n = 0;
/* Loop on all the lat/lon/values. Only print non-missing values */
while(codes_grib_iterator_next(iter,&lat,&lon,&value))
{
while (codes_grib_iterator_next(iter, &lat, &lon, &value)) {
/* Consult bitmap to see if the n'th value is missing */
int is_missing_val = (bitmapPresent && bitmap[n] == 0);
if (!is_missing_val) {
printf("- %d - lat=%f lon=%f value=%f\n",n,lat,lon,value);
printf("- %d - lat=%f lon=%f value=%f\n", n, lat, lon, value);
}
n++;
}

View File

@ -28,11 +28,11 @@
#include "eccodes.h"
#define MAX_VAL_LEN 1024
#define MAX_VAL_LEN 1024
static void usage(char* progname);
int main(int argc, char *argv[])
int main(int argc, char* argv[])
{
/* To skip read only and computed keys
unsigned long key_iterator_filter_flags=CODES_KEYS_ITERATOR_SKIP_READ_ONLY |
@ -42,54 +42,52 @@ int main(int argc, char *argv[])
CODES_KEYS_ITERATOR_SKIP_DUPLICATES;
/* Choose a namespace. E.g. "ls", "time", "parameter", "geography", "statistics" */
const char* name_space="ls";
const char* name_space = "ls";
/* name_space=NULL to get all the keys */
/* char* name_space=0; */
FILE* f = NULL;
codes_handle* h=NULL;
FILE* f = NULL;
codes_handle* h = NULL;
int err=0;
int msg_count=0;
int err = 0;
int msg_count = 0;
char value[MAX_VAL_LEN];
size_t vlen=MAX_VAL_LEN;
size_t vlen = MAX_VAL_LEN;
if (argc != 2) usage(argv[0]);
f = fopen(argv[1],"rb");
if(!f) {
f = fopen(argv[1], "rb");
if (!f) {
perror(argv[1]);
exit(1);
}
while((h = codes_handle_new_from_file(0,f,PRODUCT_GRIB,&err)) != NULL)
{
codes_keys_iterator* kiter=NULL;
while ((h = codes_handle_new_from_file(0, f, PRODUCT_GRIB, &err)) != NULL) {
codes_keys_iterator* kiter = NULL;
msg_count++;
printf("-- GRIB N. %d --\n",msg_count);
if(!h) {
printf("-- GRIB N. %d --\n", msg_count);
if (!h) {
printf("ERROR: Unable to create grib handle\n");
exit(1);
}
kiter=codes_keys_iterator_new(h,key_iterator_filter_flags,name_space);
kiter = codes_keys_iterator_new(h, key_iterator_filter_flags, name_space);
if (!kiter) {
printf("ERROR: Unable to create keys iterator\n");
exit(1);
}
while(codes_keys_iterator_next(kiter))
{
while (codes_keys_iterator_next(kiter)) {
const char* name = codes_keys_iterator_get_name(kiter);
vlen=MAX_VAL_LEN;
vlen = MAX_VAL_LEN;
memset(value, 0, vlen);
CODES_CHECK(codes_get_string(h,name,value,&vlen),name);
printf("%s = %s\n",name,value);
CODES_CHECK(codes_get_string(h, name, value, &vlen), name);
printf("%s = %s\n", name, value);
/* Alternative way of getting the string value */
CODES_CHECK(codes_keys_iterator_get_string(kiter, value, &vlen),0);
CODES_CHECK(codes_keys_iterator_get_string(kiter, value, &vlen), 0);
}
codes_keys_iterator_delete(kiter);
@ -101,6 +99,6 @@ int main(int argc, char *argv[])
static void usage(char* progname)
{
printf("\nUsage: %s grib_file\n",progname);
printf("\nUsage: %s grib_file\n", progname);
exit(1);
}

View File

@ -31,67 +31,67 @@ int main(int argc, char** argv)
long values[1024];
long new_values[1024];
FILE* in = NULL;
FILE* in = NULL;
const char* filename = "../../data/satellite.grib";
codes_handle *h = NULL;
codes_handle* h = NULL;
in = fopen(filename,"rb");
if(!in) {
printf("ERROR: unable to open input file %s\n",filename);
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open input file %s\n", filename);
return 1;
}
/* create new handle from a message in a file*/
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n",filename);
printf("Error: unable to create handle from file %s\n", filename);
}
CODES_CHECK(codes_get_long(h,"numberOfContributingSpectralBands",&numberOfContributingSpectralBands),0);
CODES_CHECK(codes_get_long(h, "numberOfContributingSpectralBands", &numberOfContributingSpectralBands), 0);
assert(numberOfContributingSpectralBands == 3);
/* Shrink NB to 2 */
numberOfContributingSpectralBands = 2;
CODES_CHECK(codes_set_long(h,"numberOfContributingSpectralBands",numberOfContributingSpectralBands),0);
CODES_CHECK(codes_set_long(h, "numberOfContributingSpectralBands", numberOfContributingSpectralBands), 0);
/* Expand NB to 9 */
numberOfContributingSpectralBands = 9;
CODES_CHECK(codes_set_long(h,"numberOfContributingSpectralBands",numberOfContributingSpectralBands),0);
CODES_CHECK(codes_set_long(h, "numberOfContributingSpectralBands", numberOfContributingSpectralBands), 0);
/* get as a long*/
CODES_CHECK(codes_get_long(h,"numberOfContributingSpectralBands",&numberOfContributingSpectralBands),0);
printf("numberOfContributingSpectralBands=%ld\n",numberOfContributingSpectralBands);
CODES_CHECK(codes_get_long(h, "numberOfContributingSpectralBands", &numberOfContributingSpectralBands), 0);
printf("numberOfContributingSpectralBands=%ld\n", numberOfContributingSpectralBands);
/* get as a long*/
CODES_CHECK(codes_get_size(h,"scaledValueOfCentralWaveNumber",&count),0);
printf("count=%ld\n",(long)count);
CODES_CHECK(codes_get_size(h, "scaledValueOfCentralWaveNumber", &count), 0);
printf("count=%ld\n", (long)count);
assert(count < sizeof(values)/sizeof(values[0]));
assert(count < sizeof(values) / sizeof(values[0]));
size = count;
CODES_CHECK(codes_get_long_array(h,"scaledValueOfCentralWaveNumber",values,&size),0);
CODES_CHECK(codes_get_long_array(h, "scaledValueOfCentralWaveNumber", values, &size), 0);
assert(size == count);
for(i=0;i<count;i++) {
printf("scaledValueOfCentralWaveNumber %lu = %ld\n",(unsigned long)i,values[i]);
if (i == 0) assert( values[i] == 26870 );
if (i == 1) assert( values[i] == 9272 );
for (i = 0; i < count; i++) {
printf("scaledValueOfCentralWaveNumber %lu = %ld\n", (unsigned long)i, values[i]);
if (i == 0) assert(values[i] == 26870);
if (i == 1) assert(values[i] == 9272);
}
for(i=0;i<count;i++)
values[i] = i+1000;
for (i = 0; i < count; i++)
values[i] = i + 1000;
size = count;
/* size--; */
CODES_CHECK(codes_set_long_array(h,"scaledValueOfCentralWaveNumber",values,size),0);
CODES_CHECK(codes_set_long_array(h, "scaledValueOfCentralWaveNumber", values, size), 0);
assert(size == count);
/* check what we set */
CODES_CHECK(codes_get_long_array(h,"scaledValueOfCentralWaveNumber",new_values,&size),0);
CODES_CHECK(codes_get_long_array(h, "scaledValueOfCentralWaveNumber", new_values, &size), 0);
assert(size == count);
for(i=0;i<count;i++) {
printf("Now scaledValueOfCentralWaveNumber %lu = %ld\n",(unsigned long)i,new_values[i]);
assert( new_values[i] == (i+1000) );
for (i = 0; i < count; i++) {
printf("Now scaledValueOfCentralWaveNumber %lu = %ld\n", (unsigned long)i, new_values[i]);
assert(new_values[i] == (i + 1000));
}
codes_handle_delete(h);

View File

@ -23,11 +23,11 @@
int main(int argc, char** argv)
{
int err = 0;
long parameterCategory=0,parameterNumber=0,discipline=0;
FILE* in = NULL;
int err = 0;
long parameterCategory = 0, parameterNumber = 0, discipline = 0;
FILE* in = NULL;
const char* filename = "../../data/multi.grib2";
codes_handle *h = NULL;
codes_handle* h = NULL;
/* turn on support for multi fields messages */
codes_grib_multi_support_on(0);
@ -35,26 +35,25 @@ int main(int argc, char** argv)
/* turn off support for multi fields messages */
/* codes_multi_support_off(0); */
in = fopen(filename,"rb");
if(!in) {
printf("ERROR: unable to open file %s\n",filename);
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
while ((h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL ) {
while ((h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err)) != NULL) {
CODES_CHECK(err, 0);
CODES_CHECK(err,0);
CODES_CHECK(codes_get_long(h, "discipline", &discipline), 0);
printf("discipline=%ld\n", discipline);
CODES_CHECK(codes_get_long(h,"discipline",&discipline),0);
printf("discipline=%ld\n",discipline);
CODES_CHECK(codes_get_long(h, "parameterCategory", &parameterCategory), 0);
printf("parameterCategory=%ld\n", parameterCategory);
CODES_CHECK(codes_get_long(h,"parameterCategory",&parameterCategory),0);
printf("parameterCategory=%ld\n",parameterCategory);
CODES_CHECK(codes_get_long(h, "parameterNumber", &parameterNumber), 0);
printf("parameterNumber=%ld\n", parameterNumber);
CODES_CHECK(codes_get_long(h,"parameterNumber",&parameterNumber),0);
printf("parameterNumber=%ld\n",parameterNumber);
if ( discipline == 0 && parameterCategory==2) {
if (discipline == 0 && parameterCategory == 2) {
if (parameterNumber == 2) printf("-------- u -------\n");
if (parameterNumber == 3) printf("-------- v -------\n");
}

View File

@ -18,67 +18,68 @@
#include <stdlib.h>
#include "eccodes.h"
static void usage(const char* prog) {
printf("usage: %s in.grib out.grib\n",prog);
static void usage(const char* prog)
{
printf("usage: %s in.grib out.grib\n", prog);
exit(1);
}
int main(int argc, char** argv)
{
int err = 0;
int err = 0;
FILE* in = NULL;
FILE* of = NULL;
long edition, step;
char* filename=NULL;
char* ofilename=NULL;
codes_handle *h = NULL;
codes_multi_handle *mh=NULL;
char* filename = NULL;
char* ofilename = NULL;
codes_handle* h = NULL;
codes_multi_handle* mh = NULL;
const int start_section = 4; /* Grib2 Product Definition Section */
if (argc < 3) usage(argv[0]);
filename=argv[1];
ofilename=argv[2];
filename = argv[1];
ofilename = argv[2];
/* open input file */
in = fopen(filename,"rb");
if(!in) {
fprintf(stderr, "ERROR: unable to open input file %s\n",filename);
in = fopen(filename, "rb");
if (!in) {
fprintf(stderr, "ERROR: unable to open input file %s\n", filename);
return 1;
}
/* new grib handle from input file */
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
CODES_CHECK(err,0);
CODES_CHECK(codes_get_long(h,"edition",&edition),0);
CODES_CHECK(err, 0);
CODES_CHECK(codes_get_long(h, "edition", &edition), 0);
if (edition != 2) {
fprintf(stderr, "ERROR: Input grib must be edition 2 for multi fields\n");
exit(1);
}
/* create a new empty multi field handle */
mh=codes_grib_multi_handle_new(0);
mh = codes_grib_multi_handle_new(0);
if (!mh) {
fprintf(stderr,"ERROR: Unable to create multi field handle\n");
fprintf(stderr, "ERROR: Unable to create multi field handle\n");
exit(1);
}
for (step=12;step<=120;step+=12) {
for (step = 12; step <= 120; step += 12) {
/* set step */
codes_set_long(h,"step",step);
codes_set_long(h, "step", step);
/* append h to mh repeating from section 4 */
/* i.e. starting from section 4 all the sections to the end of the message will be copied */
codes_grib_multi_handle_append(h, start_section, mh);
}
/* open output file */
of=fopen(ofilename,"w");
if(!of) {
fprintf(stderr, "ERROR: unable to open output file %s\n",ofilename);
of = fopen(ofilename, "w");
if (!of) {
fprintf(stderr, "ERROR: unable to open output file %s\n", ofilename);
exit(1);
}
/* write multi fields handle to output file */
codes_grib_multi_handle_write(mh,of);
codes_grib_multi_handle_write(mh, of);
fclose(of);
/* release memory */

View File

@ -21,64 +21,66 @@
#include "eccodes.h"
static void usage(const char* prog) {
printf("Usage: %s grib_file grib_file ...\n",prog);
static void usage(const char* prog)
{
printf("Usage: %s grib_file grib_file ...\n", prog);
exit(1);
}
int main(int argc, char** argv)
{
int err = 0;
long step=0;
int err = 0;
long step = 0;
size_t nfiles;
int i=0;
codes_fieldset* set=NULL;
codes_handle* h=NULL;
char param[20]={0,};
size_t len=20;
double lats[4]={0,};
double lons[4]={0,};
double values[4]={0,};
double distances[4]={0,};
int indexes[4]={0,};
char* order_by="param,step";
int i = 0;
codes_fieldset* set = NULL;
codes_handle* h = NULL;
char param[20] = {0,};
size_t len = 20;
double lats[4] = {0,};
double lons[4] = {0,};
double values[4] = {0,};
double distances[4] = {0,};
int indexes[4] = {0,};
char* order_by = "param,step";
size_t size=4;
double lat=-40,lon=15;
int mode=0;
size_t size = 4;
double lat = -40, lon = 15;
int mode = 0;
int count;
char** filenames;
codes_nearest* nearest=NULL;
codes_nearest* nearest = NULL;
if (argc < 2) usage(argv[0]);
nfiles=argc-1;
filenames=(char**)malloc(sizeof(char*)*nfiles);
for (i=0;i<nfiles;i++)
filenames[i]=(char*)strdup(argv[i+1]);
nfiles = argc - 1;
filenames = (char**)malloc(sizeof(char*) * nfiles);
for (i = 0; i < nfiles; i++)
filenames[i] = (char*)strdup(argv[i + 1]);
set=codes_fieldset_new_from_files(0,filenames,nfiles,0,0,0,order_by,&err);
CODES_CHECK(err,0);
set = codes_fieldset_new_from_files(0, filenames, nfiles, 0, 0, 0, order_by, &err);
CODES_CHECK(err, 0);
printf("\nordering by %s\n",order_by);
printf("\n%d fields in the fieldset\n",codes_fieldset_count(set));
printf("\nordering by %s\n", order_by);
printf("\n%d fields in the fieldset\n", codes_fieldset_count(set));
printf("n,step,param\n");
mode=CODES_NEAREST_SAME_GRID | CODES_NEAREST_SAME_POINT;
count=1;
mode = CODES_NEAREST_SAME_GRID | CODES_NEAREST_SAME_POINT;
count = 1;
while ((h=codes_fieldset_next_handle(set,&err))!=NULL) {
CODES_CHECK(codes_get_long(h,"step",&step),0);
len=20;
CODES_CHECK(codes_get_string(h,"shortName",param,&len),0);
while ((h = codes_fieldset_next_handle(set, &err)) != NULL) {
CODES_CHECK(codes_get_long(h, "step", &step), 0);
len = 20;
CODES_CHECK(codes_get_string(h, "shortName", param, &len), 0);
printf("%d %ld %s ",count,step,param);
if (!nearest) nearest=codes_grib_nearest_new(h,&err);
CODES_CHECK(err,0);
CODES_CHECK(codes_grib_nearest_find(nearest,h,lat,lon,mode,lats,lons,values,distances,indexes,&size),0);
printf("%d %ld %s ", count, step, param);
if (!nearest) nearest = codes_grib_nearest_new(h, &err);
CODES_CHECK(err, 0);
CODES_CHECK(codes_grib_nearest_find(nearest, h, lat, lon, mode, lats, lons, values, distances, indexes, &size), 0);
printf("\nIdx\tlat\tlon\tdist\tval\n");
for (i=0;i<4;i++) printf("%d\t%.2f\t%.2f\t%g\t%g\n",
(int)indexes[i],lats[i],lons[i],distances[i],values[i]);
for (i = 0; i < 4; i++)
printf("%d\t%.2f\t%.2f\t%g\t%g\n",
(int)indexes[i], lats[i], lons[i], distances[i], values[i]);
printf("\n");
codes_handle_delete(h);

View File

@ -15,8 +15,9 @@
#include "eccodes.h"
static void usage(const char* prog) {
fprintf(stderr, "Usage: %s latlon_file grib_orography grib_file grib_file ...\n",prog);
static void usage(const char* prog)
{
fprintf(stderr, "Usage: %s latlon_file grib_orography grib_file grib_file ...\n", prog);
fprintf(stderr, " The latlon_file should have 3 columns: num lat lon\n");
fprintf(stderr, " The grib_orography file is treated as the land-sea mask\n");
fprintf(stderr, "\n");
@ -25,22 +26,22 @@ static void usage(const char* prog) {
int main(int argc, char** argv)
{
FILE* fin=0;
int ret=0;
char* fname=0;
float lat,lon;
double *vlat,*vlon;
int npoints=0,i=0,n=0;
FILE* fin = 0;
int ret = 0;
char* fname = 0;
float lat, lon;
double *vlat, *vlon;
int npoints = 0, i = 0, n = 0;
codes_handle* h;
double *outlats,*outlons,*values,*lsm_values,*distances;
double *outlats, *outlons, *values, *lsm_values, *distances;
int* indexes;
/*long step=0;
char time[10]={0,};
char date[10]={0,};
long parameter=0;
size_t len=0;*/
long iid=0;
long *id=NULL;
long iid = 0;
long* id = NULL;
const int is_lsm = 1;
if (argc < 2) usage(argv[0]);
@ -48,64 +49,109 @@ int main(int argc, char** argv)
/* Input lat/lon file should have 3 columns:
* number latitude longitude
*/
fname=argv[1];
fin=fopen(fname,"r"); /* Open in text mode */
if(!fin) { perror(fname); exit(1); }
npoints=0;
while (fscanf(fin,"%ld %g %g",&iid,&lat,&lon) != EOF) npoints++;
fname = argv[1];
fin = fopen(fname, "r"); /* Open in text mode */
if (!fin) {
perror(fname);
exit(1);
}
npoints = 0;
while (fscanf(fin, "%ld %g %g", &iid, &lat, &lon) != EOF)
npoints++;
fclose(fin);
id=(long*)malloc(npoints*sizeof(long));
if (!id) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(long)));exit(1);}
vlat=(double*)malloc(npoints*sizeof(double));
if (!vlat) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
vlon=(double*)malloc(npoints*sizeof(double));
if (!vlon) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
outlats=(double*)malloc(npoints*sizeof(double));
if (!outlats) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
outlons=(double*)malloc(npoints*sizeof(double));
if (!outlons) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
values=(double*)malloc(npoints*sizeof(double));
if (!values) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
lsm_values=(double*)malloc(npoints*sizeof(double));
if (!lsm_values) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
distances=(double*)malloc(npoints*sizeof(double));
if (!distances) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
indexes=(int*)malloc(npoints*sizeof(int));
if (!indexes) {printf("unable to allocate %ld bytes\n",(long)(npoints*sizeof(double)));exit(1);}
id = (long*)malloc(npoints * sizeof(long));
if (!id) {
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(long)));
exit(1);
}
vlat = (double*)malloc(npoints * sizeof(double));
if (!vlat) {
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
vlon = (double*)malloc(npoints * sizeof(double));
if (!vlon) {
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
outlats = (double*)malloc(npoints * sizeof(double));
if (!outlats) {
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
outlons = (double*)malloc(npoints * sizeof(double));
if (!outlons) {
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
values = (double*)malloc(npoints * sizeof(double));
if (!values) {
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
lsm_values = (double*)malloc(npoints * sizeof(double));
if (!lsm_values) {
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
distances = (double*)malloc(npoints * sizeof(double));
if (!distances) {
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
indexes = (int*)malloc(npoints * sizeof(int));
if (!indexes) {
printf("unable to allocate %ld bytes\n", (long)(npoints * sizeof(double)));
exit(1);
}
fname=argv[1];
fin=fopen(fname,"r"); /* Open in text mode */
if(!fin) { perror(fname); exit(1); }
i=0;
while (fscanf(fin,"%ld %g %g",&iid,&lat,&lon) != EOF) {
id[i]=iid;vlat[i]=lat;
while(lon < 0) lon+=360;
vlon[i]=lon;
fname = argv[1];
fin = fopen(fname, "r"); /* Open in text mode */
if (!fin) {
perror(fname);
exit(1);
}
i = 0;
while (fscanf(fin, "%ld %g %g", &iid, &lat, &lon) != EOF) {
id[i] = iid;
vlat[i] = lat;
while (lon < 0)
lon += 360;
vlon[i] = lon;
i++;
}
fclose(fin);
/* The first GRIB file on the arguments is treated as the land-sea mask file */
fname=argv[2];
fin=fopen(fname,"rb"); /* Open GRIB in binary mode */
if(!fin) { perror(fname); exit(1); }
h=codes_handle_new_from_file(0,fin,PRODUCT_GRIB, &ret);
if (!h || ret!=CODES_SUCCESS) {printf(" unable to create handle\n");exit(1);}
fname = argv[2];
fin = fopen(fname, "rb"); /* Open GRIB in binary mode */
if (!fin) {
perror(fname);
exit(1);
}
h = codes_handle_new_from_file(0, fin, PRODUCT_GRIB, &ret);
if (!h || ret != CODES_SUCCESS) {
printf(" unable to create handle\n");
exit(1);
}
codes_grib_nearest_find_multiple(h,is_lsm,vlat,vlon,npoints,
outlats,outlons,lsm_values,distances,indexes);
codes_grib_nearest_find_multiple(h, is_lsm, vlat, vlon, npoints,
outlats, outlons, lsm_values, distances, indexes);
codes_handle_delete(h);
fclose(fin);
for (n=3;n<=argc-1;n++) {
fname=argv[n];
fin=fopen(fname,"r");
if(!fin) { perror(fname); exit(1); }
while ((h=codes_handle_new_from_file(0,fin,PRODUCT_GRIB,&ret))!=NULL) {
codes_get_double_elements(h,"values",indexes,npoints,values);
for (n = 3; n <= argc - 1; n++) {
fname = argv[n];
fin = fopen(fname, "r");
if (!fin) {
perror(fname);
exit(1);
}
while ((h = codes_handle_new_from_file(0, fin, PRODUCT_GRIB, &ret)) != NULL) {
codes_get_double_elements(h, "values", indexes, npoints, values);
/*
CODES_CHECK(codes_get_length(h, "date", &len),0);
@ -117,10 +163,10 @@ int main(int argc, char** argv)
printf("# %s %s %ld %ld\n",date,time,step,parameter);
*/
codes_handle_delete(h);
for (i=0;i<npoints;i++)
for (i = 0; i < npoints; i++)
printf("%ld %.2f %.2f %.2f %.2f %d\n",
id[i],outlats[i],outlons[i],
lsm_values[i],values[i], indexes[i]);
id[i], outlats[i], outlons[i],
lsm_values[i], values[i], indexes[i]);
}
fclose(fin);

View File

@ -25,44 +25,44 @@
#include "eccodes.h"
void usage(const char* prog) {
printf("usage: %s filename\n",prog);
void usage(const char* prog)
{
printf("usage: %s filename\n", prog);
exit(1);
}
int main(int argc, char** argv)
{
int err = 0,i;
double *values = NULL;
double max,min,average;
size_t values_len= 0;
int err = 0, i;
double* values = NULL;
double max, min, average;
size_t values_len = 0;
FILE* in = NULL;
char* filename ;
codes_handle *h = NULL;
char* filename;
codes_handle* h = NULL;
if (argc<2) usage(argv[0]);
filename=argv[1];
if (argc < 2) usage(argv[0]);
filename = argv[1];
in = fopen(filename,"rb");
if(!in) {
printf("ERROR: unable to open file %s\n",filename);
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
/* create new handle from a message in a file*/
while((h = codes_handle_new_from_file(0,in,&err)) != NULL) {
while ((h = codes_handle_new_from_file(0, in, &err)) != NULL) {
/* get the size of the values array*/
CODES_CHECK(codes_get_size(h,"values",&values_len),0);
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
values = malloc(values_len*sizeof(double));
values = malloc(values_len * sizeof(double));
/* get data values*/
CODES_CHECK(codes_get_double_array(h,"values",values,&values_len),0);
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len), 0);
for(i = 0; i < values_len; i++)
printf("%d %.10e\n",i+1,values[i]);
for (i = 0; i < values_len; i++)
printf("%d %.10e\n", i + 1, values[i]);
free(values);

View File

@ -21,39 +21,39 @@
#include "eccodes.h"
#define EPSILON 1e-6
#define EPSILON 1e-6
#define EXPECTED_MIN 270.466796875
#define EXPECTED_MAX 311.096796875
int main(int argc, char** argv)
{
int err = 0;
size_t size=0;
int err = 0;
size_t size = 0;
FILE* in = NULL;
const char* infile = "../../data/regular_latlon_surface.grib1";
FILE* out = NULL;
FILE* in = NULL;
const char* infile = "../../data/regular_latlon_surface.grib1";
FILE* out = NULL;
const char* outfile = "out.precision.grib1";
codes_handle *h = NULL;
const void* buffer = NULL;
double* values1=NULL;
double* values2=NULL;
double maxa=0;
double maxv=0,minv=0;
double maxr=0,r=0;
codes_handle* h = NULL;
const void* buffer = NULL;
double* values1 = NULL;
double* values2 = NULL;
double maxa = 0;
double maxv = 0, minv = 0;
double maxr = 0, r = 0;
long decimalPrecision;
long bitsPerValue1=0, bitsPerValue2=0;
int i=0;
long bitsPerValue1 = 0, bitsPerValue2 = 0;
int i = 0;
in = fopen(infile,"rb");
if(!in) {
printf("ERROR: unable to open input file %s\n",infile);
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open input file %s\n", infile);
return 1;
}
out = fopen(outfile,"wb");
if(!out) {
printf("ERROR: unable to open output file %s\n",outfile);
out = fopen(outfile, "wb");
if (!out) {
printf("ERROR: unable to open output file %s\n", outfile);
fclose(in);
return 1;
}
@ -61,60 +61,59 @@ int main(int argc, char** argv)
/* create a new handle from a message in a file */
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n",infile);
printf("Error: unable to create handle from file %s\n", infile);
}
/* bitsPerValue before changing the packing parameters */
CODES_CHECK(codes_get_long(h,"bitsPerValue",&bitsPerValue1),0);
CODES_CHECK(codes_get_long(h, "bitsPerValue", &bitsPerValue1), 0);
assert(bitsPerValue1 == 16);
/* get the size of the values array*/
CODES_CHECK(codes_get_size(h,"values",&size),0);
CODES_CHECK(codes_get_size(h, "values", &size), 0);
assert(size == 496);
values1 = (double*)malloc(size*sizeof(double));
values1 = (double*)malloc(size * sizeof(double));
/* get data values before changing the packing parameters*/
CODES_CHECK(codes_get_double_array(h,"values",values1,&size),0);
CODES_CHECK(codes_get_double_array(h, "values", values1, &size), 0);
/* changing decimal precision to 2 means that 2 decimal digits
are preserved when packing. */
decimalPrecision=2;
CODES_CHECK(codes_set_long(h,"changeDecimalPrecision",decimalPrecision),0);
decimalPrecision = 2;
CODES_CHECK(codes_set_long(h, "changeDecimalPrecision", decimalPrecision), 0);
/* bitsPerValue after changing the packing parameters */
CODES_CHECK(codes_get_long(h,"bitsPerValue",&bitsPerValue2),0);
CODES_CHECK(codes_get_long(h, "bitsPerValue", &bitsPerValue2), 0);
assert(bitsPerValue2 == 12);
values2 = (double*)malloc(size*sizeof(double));
values2 = (double*)malloc(size * sizeof(double));
/* get data values after changing the packing parameters*/
CODES_CHECK(codes_get_double_array(h,"values",values2,&size),0);
CODES_CHECK(codes_get_double_array(h, "values", values2, &size), 0);
/* computing error */
maxa=0;
maxr=0;
maxv=values2[0];
minv=maxv;
for (i=0;i<size;i++) {
double a=fabs(values2[i]-values1[i]);
if ( values2[i] > maxv ) maxv=values2[i];
if ( values2[i] < minv ) minv=values2[i];
if ( values2[i] !=0 ) r=fabs((values2[i]-values1[i])/values2[i]);
if ( a > maxa ) maxa=a;
if ( r > maxr ) maxr=r;
maxa = 0;
maxr = 0;
maxv = values2[0];
minv = maxv;
for (i = 0; i < size; i++) {
double a = fabs(values2[i] - values1[i]);
if (values2[i] > maxv) maxv = values2[i];
if (values2[i] < minv) minv = values2[i];
if (values2[i] != 0) r = fabs((values2[i] - values1[i]) / values2[i]);
if (a > maxa) maxa = a;
if (r > maxr) maxr = r;
}
printf("max absolute error = %g\n",maxa);
printf("max relative error = %g\n",maxr);
printf("min value = %g\n",minv);
printf("max value = %g\n",maxv);
printf("max absolute error = %g\n", maxa);
printf("max relative error = %g\n", maxr);
printf("min value = %g\n", minv);
printf("max value = %g\n", maxv);
assert(fabs(minv - EXPECTED_MIN) < EPSILON);
assert(fabs(maxv - EXPECTED_MAX) < EPSILON);
/* get the coded message in a buffer */
CODES_CHECK(codes_get_message(h,&buffer,&size),0);
CODES_CHECK(codes_get_message(h, &buffer, &size), 0);
/* write the buffer in a file*/
if(fwrite(buffer,1,size,out) != size)
{
if (fwrite(buffer, 1, size, out) != size) {
perror(argv[1]);
exit(1);
}

View File

@ -19,68 +19,69 @@
#include "eccodes.h"
static void usage(const char* prog) {
printf("usage: %s filename\n",prog);
static void usage(const char* prog)
{
printf("usage: %s filename\n", prog);
exit(1);
}
int main(int argc, char** argv)
{
int err = 0,i;
double *values = NULL;
double max,min,average;
size_t values_len= 0;
int err = 0, i;
double* values = NULL;
double max, min, average;
size_t values_len = 0;
FILE* in = NULL;
char* filename ;
codes_handle *h = NULL;
char* filename;
codes_handle* h = NULL;
if (argc<2) usage(argv[0]);
filename=argv[1];
if (argc < 2) usage(argv[0]);
filename = argv[1];
in = fopen(filename,"rb");
if(!in) {
printf("ERROR: unable to open file %s\n",filename);
in = fopen(filename, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", filename);
return 1;
}
/* create new handle from a message in a file*/
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n",filename);
printf("Error: unable to create handle from file %s\n", filename);
}
/* get the size of the values array*/
CODES_CHECK(codes_get_size(h,"values",&values_len),0);
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
values = (double*)malloc(values_len*sizeof(double));
values = (double*)malloc(values_len * sizeof(double));
/* get data values*/
CODES_CHECK(codes_get_double_array(h,"values",values,&values_len),0);
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len), 0);
for(i = 0; i < values_len; i++)
printf("%d %.10e\n",i,values[i]);
for (i = 0; i < values_len; i++)
printf("%d %.10e\n", i, values[i]);
free(values);
CODES_CHECK(codes_get_double(h,"max",&max),0);
CODES_CHECK(codes_get_double(h,"min",&min),0);
CODES_CHECK(codes_get_double(h,"average",&average),0);
CODES_CHECK(codes_get_double(h, "max", &max), 0);
CODES_CHECK(codes_get_double(h, "min", &min), 0);
CODES_CHECK(codes_get_double(h, "average", &average), 0);
printf("%d values found in %s\n",(int)values_len,filename);
printf("max=%.10e min=%.10e average=%.10e\n",max,min,average);
printf("%d values found in %s\n", (int)values_len, filename);
printf("max=%.10e min=%.10e average=%.10e\n", max, min, average);
{
/* Example of accessing specific elements from data values */
double vals_arr[3] = {0, 0, 0};
const int NUM = 3;
double vals_arr[3] = { 0, 0, 0 };
const int NUM = 3;
int index_arr[3];
index_arr[0] = 0; /* first element */
index_arr[1] = values_len/2; /* middle element */
index_arr[2] = values_len-1; /* last element */
index_arr[0] = 0; /* first element */
index_arr[1] = values_len / 2; /* middle element */
index_arr[2] = values_len - 1; /* last element */
CODES_CHECK(codes_get_double_elements(h, "values", index_arr, NUM, vals_arr), 0);
for (i=0; i<NUM; ++i){
for (i = 0; i < NUM; ++i) {
printf("value at index %d = %.10e\n", index_arr[i], vals_arr[i]);
}
}

View File

@ -16,12 +16,12 @@
#define NUM_THREADS 4
/* Return 0 if numbers considered equal, otherwise 1 */
static int compare_doubles(double a,double b,double tolerance)
static int compare_doubles(double a, double b, double tolerance)
{
int ret=0;
double d=fabs(a-b);
int ret = 0;
double d = fabs(a - b);
if (d > tolerance) {
ret=1;
ret = 1;
}
return ret;
}
@ -32,12 +32,12 @@ static void* process_grib(void* threadID)
size_t str_len = 20, i = 0;
long indicatorOfUnitOfTimeRange = 1, step = 0;
char mystring[100];
double* values = NULL;
double* values = NULL;
size_t values_len = 0;
double min=0,max=0,avg=0;
const double tol = 1e-6;
double pv[4]={1,2,3,4};
const size_t pvsize=4;
double min = 0, max = 0, avg = 0;
const double tol = 1e-6;
double pv[4] = { 1, 2, 3, 4 };
const size_t pvsize = 4;
ProductKind prod_kind = 0;
codes_handle* h = codes_grib_handle_new_from_samples(0, "regular_ll_pl_grib2");
@ -46,35 +46,37 @@ static void* process_grib(void* threadID)
assert(prod_kind == PRODUCT_GRIB);
printf("Thread %ld running\n", tid);
CODES_CHECK(codes_set_long(h,"indicatorOfUnitOfTimeRange", indicatorOfUnitOfTimeRange),0);
CODES_CHECK(codes_set_string(h,"indicatorOfUnitOfTimeRange", "s", &str_len),0);
CODES_CHECK(codes_set_string(h,"stepUnits", "s", &str_len),0);
CODES_CHECK(codes_set_long(h, "indicatorOfUnitOfTimeRange", indicatorOfUnitOfTimeRange), 0);
CODES_CHECK(codes_set_string(h, "indicatorOfUnitOfTimeRange", "s", &str_len), 0);
CODES_CHECK(codes_set_string(h, "stepUnits", "s", &str_len), 0);
CODES_CHECK(codes_set_long(h, "endStep", 86400), 0);
CODES_CHECK(codes_set_long(h,"centre", 80),0);
CODES_CHECK(codes_set_long(h, "centre", 80), 0);
CODES_CHECK(codes_get_long(h,"endStep", &step),0);
CODES_CHECK(codes_get_string(h, "indicatorOfUnitOfTimeRange", mystring, &str_len),0);
CODES_CHECK(codes_get_long(h, "endStep", &step), 0);
CODES_CHECK(codes_get_string(h, "indicatorOfUnitOfTimeRange", mystring, &str_len), 0);
CODES_CHECK(codes_set_long(h,"PVPresent", 1),0);
CODES_CHECK(codes_set_double_array(h, "pv", pv, pvsize),0);
CODES_CHECK(codes_set_long(h, "PVPresent", 1), 0);
CODES_CHECK(codes_set_double_array(h, "pv", pv, pvsize), 0);
CODES_CHECK(codes_get_size(h,"values",&values_len),0);
values = (double*)malloc(values_len*sizeof(double));
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len),0);
for (i=0;i<values_len;i++) {
if (i%2) values[i] *= 0.94;
else if (i%3) values[i] *= 0.84;
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
values = (double*)malloc(values_len * sizeof(double));
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len), 0);
for (i = 0; i < values_len; i++) {
if (i % 2)
values[i] *= 0.94;
else if (i % 3)
values[i] *= 0.84;
}
GRIB_CHECK(grib_set_double_array(h,"values",values,values_len),0);
GRIB_CHECK(grib_set_double_array(h, "values", values, values_len), 0);
free(values);
CODES_CHECK(codes_get_double(h, "min", &min),0);
CODES_CHECK(codes_get_double(h, "max", &max),0);
CODES_CHECK(codes_get_double(h, "avg", &avg),0);
CODES_CHECK(codes_get_double(h, "min", &min), 0);
CODES_CHECK(codes_get_double(h, "max", &max), 0);
CODES_CHECK(codes_get_double(h, "avg", &avg), 0);
printf("Thread %ld: min=%g max=%g avg=%g\n", tid, min, max, avg);
assert( compare_doubles(min, 0.84, tol)==0 );
assert( compare_doubles(max, 1.00, tol)==0 );
assert( compare_doubles(avg, 0.916774, tol)==0 );
assert(compare_doubles(min, 0.84, tol) == 0);
assert(compare_doubles(max, 1.00, tol) == 0);
assert(compare_doubles(avg, 0.916774, tol) == 0);
codes_handle_delete(h);
pthread_exit(NULL);
@ -84,16 +86,16 @@ int main(int argc, char** argv)
{
pthread_t threads[NUM_THREADS];
int error = 0;
long i = 0;
for (i=0; i<NUM_THREADS; ++i) {
long i = 0;
for (i = 0; i < NUM_THREADS; ++i) {
printf("Creating thread %ld\n", i);
error = pthread_create(&threads[i], NULL, process_grib, (void *)i);
error = pthread_create(&threads[i], NULL, process_grib, (void*)i);
if (error) {
assert(0);
return 1;
}
}
for (i=0; i<NUM_THREADS; ++i) {
for (i = 0; i < NUM_THREADS; ++i) {
pthread_join(threads[i], NULL);
}

View File

@ -10,8 +10,9 @@
#include "eccodes.h"
#include <ctype.h>
static void usage(const char* prog) {
printf("usage: %s in1.grib in2.grib what out.grib\n",prog);
static void usage(const char* prog)
{
printf("usage: %s in1.grib in2.grib what out.grib\n", prog);
printf("in1.grib The grib in whose sections we are interested, i.e. the source of the sections (read-only)\n");
printf("in2.grib The input grib (read-only)\n");
printf("what The section(s) to copy: p(Product), g(Grid), l(Local), d(Data), b(Bitmap)\n");
@ -19,38 +20,38 @@ static void usage(const char* prog) {
exit(1);
}
int main ( int argc, char* argv[])
int main(int argc, char* argv[])
{
codes_handle *hfrom,*hto,*h;
FILE *in;
codes_handle *hfrom, *hto, *h;
FILE* in;
char *in_name1, *in_name2, *what_str, *out_name;
int i, err=0, what=0;
int i, err = 0, what = 0;
if (argc<5) usage(argv[0]);
if (argc < 5) usage(argv[0]);
in_name1=argv[1];
in_name2=argv[2];
what_str=argv[3];
out_name=argv[4];
in_name1 = argv[1];
in_name2 = argv[2];
what_str = argv[3];
out_name = argv[4];
in=fopen(in_name1,"rb");
in = fopen(in_name1, "rb");
if (!in) {
perror(in_name1);
exit(1);
}
hfrom=codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
CODES_CHECK(err,0);
hfrom = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
CODES_CHECK(err, 0);
fclose(in);
in=fopen(in_name2,"rb");
in = fopen(in_name2, "rb");
if (!in) {
perror(in_name2);
exit(1);
}
hto=codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
CODES_CHECK(err,0);
hto = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
CODES_CHECK(err, 0);
fclose(in);
/* The sections for the "what" argument are:
@ -62,7 +63,7 @@ int main ( int argc, char* argv[])
* One can bitwise-OR them to have more than one section copied
* E.g. what = CODES_SECTION_PRODUCT | CODES_SECTION_LOCAL;
*/
for(i=0; i<strlen(what_str); ++i) {
for (i = 0; i < strlen(what_str); ++i) {
if (what_str[i] == 'p') {
printf("Copying the PRODUCT section\n");
what |= CODES_SECTION_PRODUCT;
@ -87,15 +88,15 @@ int main ( int argc, char* argv[])
/* Ignore spaces and comma separator */
}
else {
fprintf(stderr,"Invalid option: '%c'. Ignoring.\n",
fprintf(stderr, "Invalid option: '%c'. Ignoring.\n",
what_str[i]);
}
}
h=codes_grib_util_sections_copy(hfrom,hto,what,&err);
CODES_CHECK(err,0);
h = codes_grib_util_sections_copy(hfrom, hto, what, &err);
CODES_CHECK(err, 0);
err=codes_write_message(h,out_name,"w");
err = codes_write_message(h, out_name, "w");
codes_handle_delete(hfrom);
codes_handle_delete(hto);

View File

@ -23,61 +23,61 @@
int main(int argc, char** argv)
{
int err = 0;
size_t size=0;
int err = 0;
size_t size = 0;
FILE* in = NULL;
const char* infile = "../../data/regular_latlon_surface.grib1";
FILE* out = NULL;
FILE* in = NULL;
const char* infile = "../../data/regular_latlon_surface.grib1";
FILE* out = NULL;
const char* outfile = "out.set_bitmap_c.grib";
codes_handle *h = NULL;
const void* buffer = NULL;
codes_handle* h = NULL;
const void* buffer = NULL;
size_t values_len;
double* values;
double missing=9999;
int i=0;
double missing = 9999;
int i = 0;
in = fopen(infile, "rb");
if(!in) {
printf("ERROR: unable to open input file %s\n",infile);
if (!in) {
printf("ERROR: unable to open input file %s\n", infile);
return 1;
}
out = fopen(outfile, "wb");
if(!out) {
printf("ERROR: unable to open output file %s\n",outfile);
if (!out) {
printf("ERROR: unable to open output file %s\n", outfile);
fclose(in);
return 1;
}
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n",infile);
printf("Error: unable to create handle from file %s\n", infile);
}
CODES_CHECK(codes_set_double(h, "missingValue", missing),0);
CODES_CHECK(codes_set_double(h, "missingValue", missing), 0);
/* get the size of the values array*/
CODES_CHECK(codes_get_size(h, "values", &values_len),0);
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
values = (double*)malloc(values_len*sizeof(double));
values = (double*)malloc(values_len * sizeof(double));
/* get data values*/
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len),0);
CODES_CHECK(codes_get_double_array(h, "values", values, &values_len), 0);
CODES_CHECK(codes_set_long(h, "bitmapPresent", 1),0);
CODES_CHECK(codes_set_long(h, "bitmapPresent", 1), 0);
for(i = 0; i < 10; i++) {
values[i]=missing;
for (i = 0; i < 10; i++) {
values[i] = missing;
}
CODES_CHECK(codes_set_double_array(h, "values", values, values_len),0);
CODES_CHECK(codes_set_double_array(h, "values", values, values_len), 0);
/* get the coded message in a buffer */
CODES_CHECK(codes_get_message(h, &buffer, &size),0);
CODES_CHECK(codes_get_message(h, &buffer, &size), 0);
/* write the buffer in a file*/
if(fwrite(buffer, 1, size, out) != size) {
if (fwrite(buffer, 1, size, out) != size) {
perror(outfile);
exit(1);
}

View File

@ -23,15 +23,15 @@
static void usage(const char* prog)
{
printf("usage: %s out.grib\n",prog);
printf("usage: %s out.grib\n", prog);
exit(1);
}
int main(int argc, char** argv)
{
int i;
double *values = NULL;
size_t values_len= 0;
double* values = NULL;
size_t values_len = 0;
/* Note: the full name of the sample file is "regular_ll_pl_grib1.tmpl" */
/* Sample files are stored in the samples directory (use codes_info to */
@ -40,11 +40,11 @@ int main(int argc, char** argv)
const char* sample_filename = "regular_ll_pl_grib1";
/* Here is how you can get the samples path */
const char* samples_path = codes_samples_path(NULL);
codes_handle *h = NULL;
double d,e;
codes_handle* h = NULL;
double d, e;
long count;
if (argc!=2) usage(argv[0]);
if (argc != 2) usage(argv[0]);
/* create new handle from message in sample file */
printf("Using samples path: %s\n", samples_path);
h = codes_grib_handle_new_from_samples(0, sample_filename);
@ -57,24 +57,27 @@ int main(int argc, char** argv)
/* will be the same as the sample GRIB. */
/* But if your data array has a different size, then specify the grid geometry */
/* (e.g. keys Ni, Nj etc) and set the correct number of data values */
CODES_CHECK(codes_get_size(h, "values", &values_len),0);
CODES_CHECK(codes_get_size(h, "values", &values_len), 0);
values = (double*)malloc(values_len*sizeof(double));
d=10e-8;
e=d;
count=1;
for (i=0;i<values_len;i++) {
if (count>100) {e*=10; count=1;}
values[i]=d;
values = (double*)malloc(values_len * sizeof(double));
d = 10e-8;
e = d;
count = 1;
for (i = 0; i < values_len; i++) {
if (count > 100) {
e *= 10;
count = 1;
}
values[i] = d;
/*printf("%g \n",values[i]);*/
d+=e;
d += e;
count++;
}
CODES_CHECK(codes_set_long(h,"bitsPerValue",16),0);
CODES_CHECK(codes_set_long(h, "bitsPerValue", 16), 0);
/* set data values */
CODES_CHECK(codes_set_double_array(h,"values",values,values_len),0);
CODES_CHECK(codes_set_double_array(h, "values", values, values_len), 0);
CODES_CHECK(codes_write_message(h, argv[1], "w"), 0);

View File

@ -21,30 +21,30 @@
int main(int argc, char** argv)
{
int err = 0;
long centre=80;
long long_value=0;
int err = 0;
long centre = 80;
long long_value = 0;
char string_value[100];
size_t len = sizeof(string_value)/sizeof(char);
size_t size=0;
size_t len = sizeof(string_value) / sizeof(char);
size_t size = 0;
FILE* in = NULL;
const char* infile = "../../data/regular_latlon_surface.grib1";
FILE* out = NULL;
FILE* in = NULL;
const char* infile = "../../data/regular_latlon_surface.grib1";
FILE* out = NULL;
const char* outfile = "out.set.grib1";
codes_handle *h = NULL;
const void* buffer = NULL;
size_t str_len = 0; /* See the call to codes_set_string later */
codes_handle* h = NULL;
const void* buffer = NULL;
size_t str_len = 0; /* See the call to codes_set_string later */
in = fopen(infile,"rb");
if(!in) {
printf("ERROR: unable to open file %s\n",infile);
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open file %s\n", infile);
return 1;
}
out = fopen(outfile,"wb");
if(!out) {
printf("ERROR: unable to open file %s\n",outfile);
out = fopen(outfile, "wb");
if (!out) {
printf("ERROR: unable to open file %s\n", outfile);
fclose(in);
return 1;
}
@ -52,31 +52,30 @@ int main(int argc, char** argv)
/* create a new handle from a message in a file */
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n",infile);
printf("Error: unable to create handle from file %s\n", infile);
}
/* set centre as a long */
CODES_CHECK(codes_set_long(h,"centre",centre),0);
CODES_CHECK(codes_set_long(h, "centre", centre), 0);
/* set paramId and shortName - normally you would do one or the other */
CODES_CHECK(codes_set_long(h,"paramId", 500004),0);
CODES_CHECK(codes_set_long(h, "paramId", 500004), 0);
/* the value of str_len is not used, it can be anything! */
CODES_CHECK(codes_set_string(h,"shortName", "fis", &str_len),0);
CODES_CHECK(codes_set_string(h, "shortName", "fis", &str_len), 0);
/* get centre as a long */
CODES_CHECK(codes_get_long(h,"centre",&long_value),0);
printf("centre long value=%ld\n",long_value);
CODES_CHECK(codes_get_long(h, "centre", &long_value), 0);
printf("centre long value=%ld\n", long_value);
/* get centre as a string */
CODES_CHECK(codes_get_string(h,"centre",string_value,&len),0);
printf("centre string value=%s\n",string_value);
CODES_CHECK(codes_get_string(h, "centre", string_value, &len), 0);
printf("centre string value=%s\n", string_value);
/* get the coded message in a buffer */
CODES_CHECK(codes_get_message(h,&buffer,&size),0);
CODES_CHECK(codes_get_message(h, &buffer, &size), 0);
/* write the buffer in a file*/
if(fwrite(buffer,1,size,out) != size)
{
if (fwrite(buffer, 1, size, out) != size) {
perror(argv[1]);
exit(1);
}

View File

@ -19,39 +19,40 @@
#include <assert.h>
#include "eccodes.h"
static void usage(const char* prog) {
fprintf(stderr, "usage: %s in out\n",prog);
static void usage(const char* prog)
{
fprintf(stderr, "usage: %s in out\n", prog);
exit(1);
}
int main(int argc, char** argv)
{
int err = 0;
long NV = 0;
size_t size=0;
double pv[4]={1,2,3,4};
size_t pvsize=4;
int err = 0;
long NV = 0;
size_t size = 0;
double pv[4] = { 1, 2, 3, 4 };
size_t pvsize = 4;
FILE* in = NULL;
char* infile = NULL;
FILE* out = NULL;
char* outfile = NULL;
codes_handle *h = NULL;
FILE* in = NULL;
char* infile = NULL;
FILE* out = NULL;
char* outfile = NULL;
codes_handle* h = NULL;
const void* buffer = NULL;
if (argc != 3) usage(argv[0]);
infile = argv[1];
outfile= argv[2];
infile = argv[1];
outfile = argv[2];
in = fopen(infile, "rb");
if(!in) {
fprintf(stderr, "ERROR: unable to open input file %s\n",infile);
if (!in) {
fprintf(stderr, "ERROR: unable to open input file %s\n", infile);
return 1;
}
out = fopen(outfile, "wb");
if(!out) {
fprintf(stderr, "ERROR: unable to open output file %s\n",outfile);
if (!out) {
fprintf(stderr, "ERROR: unable to open output file %s\n", outfile);
fclose(in);
return 1;
}
@ -59,23 +60,22 @@ int main(int argc, char** argv)
/* create a new handle from a message in a file */
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
fprintf(stderr, "Error: unable to create handle from file %s\n",infile);
fprintf(stderr, "Error: unable to create handle from file %s\n", infile);
}
CODES_CHECK(codes_set_long(h,"PVPresent", 1),0);
CODES_CHECK(codes_set_long(h, "PVPresent", 1), 0);
CODES_CHECK(codes_set_double_array(h, "pv", pv, pvsize),0);
CODES_CHECK(codes_set_double_array(h, "pv", pv, pvsize), 0);
/* Once we set the pv array, the NV key should be also set */
CODES_CHECK(codes_get_long(h, "NV", &NV),0);
assert( NV == pvsize );
CODES_CHECK(codes_get_long(h, "NV", &NV), 0);
assert(NV == pvsize);
/* get the coded message in a buffer */
CODES_CHECK(codes_get_message(h, &buffer, &size),0);
CODES_CHECK(codes_get_message(h, &buffer, &size), 0);
/* write the buffer in a file*/
if(fwrite(buffer, 1, size, out) != size)
{
if (fwrite(buffer, 1, size, out) != size) {
perror(argv[1]);
exit(1);
}

View File

@ -17,19 +17,19 @@ int main()
{
const int ni = 2880;
const int nj = 2880;
double *values;
int i = 0;
codes_handle *h = NULL;
double* values;
int i = 0;
codes_handle* h = NULL;
const char* filename = "bigfile.grib";
values = (double*) malloc(ni*nj*sizeof(double));
values = (double*)malloc(ni * nj * sizeof(double));
if (!values) {
printf ("Malloc failed\n");
printf("Malloc failed\n");
exit(1);
}
for (i=0; i<ni*nj; i++) {
double r = rand() * 1.0 / RAND_MAX;
for (i = 0; i < ni * nj; i++) {
double r = rand() * 1.0 / RAND_MAX;
values[i] = r;
}
@ -52,21 +52,21 @@ int main()
CODES_CHECK(codes_set_double(h, "iDirectionIncrementInDegrees", 0.125), 0);
CODES_CHECK(codes_set_double(h, "jDirectionIncrementInDegrees", 0.125), 0);
CODES_CHECK(codes_set_long(h, "iScansNegatively", 0), 0);
CODES_CHECK(codes_set_long(h, "jScansPositively", 1), 0);
CODES_CHECK(codes_set_long(h, "iScansNegatively", 0), 0);
CODES_CHECK(codes_set_long(h, "jScansPositively", 1), 0);
CODES_CHECK(codes_set_double(h, "latitudeOfFirstGridPointInDegrees", -90), 0);
CODES_CHECK(codes_set_double(h, "latitudeOfLastGridPointInDegrees", 90), 0);
CODES_CHECK(codes_set_double(h, "longitudeOfFirstGridPointInDegrees", -180), 0);
CODES_CHECK(codes_set_double(h, "longitudeOfLastGridPointInDegrees", 180), 0);
CODES_CHECK(codes_set_double_array(h, "values", values, ni*nj), 0);
CODES_CHECK(codes_set_double_array(h, "values", values, ni * nj), 0);
codes_write_message(h, filename, "w");
/*printf("Wrote file %s\n", filename);*/
codes_handle_delete(h);
free (values);
free(values);
return 0;
}

View File

@ -23,26 +23,26 @@ int main(int argc, char** argv)
codes_string_list* list;
codes_string_list* list2;
list=codes_grib_util_get_param_id("11.2");
list = codes_grib_util_get_param_id("11.2");
printf("mars.param=11.2 -> paramId= ");
while (list) {
printf("%s ",list->value);
list2=codes_grib_util_get_param_id("130.128");
printf("%s ", list->value);
list2 = codes_grib_util_get_param_id("130.128");
printf("mars.param=11.2 -> paramId= ");
while (list2) {
printf("%s ",list2->value);
list2=list2->next;
printf("%s ", list2->value);
list2 = list2->next;
}
list=list->next;
list = list->next;
}
printf("\n");
printf("\n");
printf("paramId=130 -> mars.param= ");
list=codes_grib_util_get_mars_param("130");
list = codes_grib_util_get_mars_param("130");
while (list) {
printf("%s ",list->value);
list=list->next;
printf("%s ", list->value);
list = list->next;
}
printf("\n");

View File

@ -21,36 +21,36 @@
#include <stdlib.h>
#include <assert.h>
const int NUM_FIELDS = 4;
const int COUNT = 20;
const int NUM_FIELDS = 4;
const int COUNT = 20;
const char* file_path = "../../data/multi_created.grib2";
static void read_data(int num_msgs)
{
int err = 0,i;
FILE* fp = NULL;
long stepRange = 0;
codes_handle *h = NULL;
int err = 0, i;
FILE* fp = NULL;
long stepRange = 0;
codes_handle* h = NULL;
fp = fopen(file_path, "rb");
if(!fp) {
if (!fp) {
fprintf(stderr, "ERROR: unable to open grib file %s\n", file_path);
exit(1);
}
printf("Opened GRIB file %s: \n", file_path);
for(i=0; i<num_msgs; ++i) {
for (i = 0; i < num_msgs; ++i) {
h = codes_handle_new_from_file(0, fp, PRODUCT_GRIB, &err);
CODES_CHECK(err, 0);
CODES_CHECK( codes_get_long(h, "stepRange", &stepRange), 0);
CODES_CHECK(codes_get_long(h, "stepRange", &stepRange), 0);
printf("%d : stepRange=%ld\n", i, stepRange);
codes_handle_delete(h);
/* These tests make sure we always start from 1st field of the grib msg */
/* and not where we left off last time */
if (i == 0) assert(stepRange == 0); /* 1st field */
if (i == 1) assert(stepRange == 12); /* 2nd field */
if (i == 2) assert(stepRange == 24); /* 3rd field */
if (i == 3) assert(stepRange == 36); /* 4th field */
if (i == 0) assert(stepRange == 0); /* 1st field */
if (i == 1) assert(stepRange == 12); /* 2nd field */
if (i == 2) assert(stepRange == 24); /* 3rd field */
if (i == 3) assert(stepRange == 36); /* 4th field */
}
/* Must reset this file pointer for the next round */
codes_grib_multi_support_reset_file(codes_context_get_default(), fp);
@ -64,8 +64,8 @@ int main(int argc, char** argv)
/* turn on support for multi fields messages */
codes_grib_multi_support_on(0);
for(i=1; i<COUNT; ++i) {
printf("Pass %d: \n",i);
for (i = 1; i < COUNT; ++i) {
printf("Pass %d: \n", i);
read_data(NUM_FIELDS);
}
return 0;

View File

@ -9,28 +9,29 @@
*/
#include "eccodes.h"
void usage(char* prog) {
printf("usage: %s in.nc\n",prog);
void usage(char* prog)
{
printf("usage: %s in.nc\n", prog);
exit(1);
}
int main(int argc,char* argv[]) {
int main(int argc, char* argv[])
{
char* file;
int err=0;
int err = 0;
codes_handle* h;
char identifier[7]={0,};
size_t len=7;
codes_context* c=codes_context_get_default();
char identifier[7] = {0,};
size_t len = 7;
codes_context* c = codes_context_get_default();
if (argc>2) usage(argv[0]);
if (argc > 2) usage(argv[0]);
file=argv[1];
file = argv[1];
h=codes_handle_new_from_nc_file(c,file,&err);
codes_get_string(h,"identifier",identifier,&len);
printf("%s\n",identifier);
CODES_CHECK(err,0);
h = codes_handle_new_from_nc_file(c, file, &err);
codes_get_string(h, "identifier", identifier, &len);
printf("%s\n", identifier);
CODES_CHECK(err, 0);
return err;
}

View File

@ -14,123 +14,123 @@
int main(int argc, char** argv)
{
codes_handle *h = NULL;
codes_handle* h = NULL;
size_t size = 0;
double* vdouble = NULL;
FILE* f = NULL;
const void* buffer = NULL;
if(argc != 2) {
fprintf(stderr,"usage: %s out\n",argv[0]);
if (argc != 2) {
fprintf(stderr, "usage: %s out\n", argv[0]);
exit(1);
}
h = codes_grib_handle_new_from_samples(NULL,"GRIB2");
if(!h) {
fprintf(stderr,"Cannot create grib handle\n");
h = codes_grib_handle_new_from_samples(NULL, "GRIB2");
if (!h) {
fprintf(stderr, "Cannot create grib handle\n");
exit(1);
}
CODES_CHECK(codes_set_long(h,"parametersVersion",1),0);
CODES_CHECK(codes_set_long(h,"truncateLaplacian",0),0);
CODES_CHECK(codes_set_long(h,"truncateDegrees",0),0);
CODES_CHECK(codes_set_long(h,"dummy",1),0);
CODES_CHECK(codes_set_long(h,"changingPrecision",0),0);
CODES_CHECK(codes_set_long(h,"unitsFactor",1),0);
CODES_CHECK(codes_set_long(h,"unitsBias",0),0);
CODES_CHECK(codes_set_long(h,"timeRangeIndicatorFromStepRange",-1),0);
CODES_CHECK(codes_set_long(h,"missingValue",9999),0);
CODES_CHECK(codes_set_long(h, "parametersVersion", 1), 0);
CODES_CHECK(codes_set_long(h, "truncateLaplacian", 0), 0);
CODES_CHECK(codes_set_long(h, "truncateDegrees", 0), 0);
CODES_CHECK(codes_set_long(h, "dummy", 1), 0);
CODES_CHECK(codes_set_long(h, "changingPrecision", 0), 0);
CODES_CHECK(codes_set_long(h, "unitsFactor", 1), 0);
CODES_CHECK(codes_set_long(h, "unitsBias", 0), 0);
CODES_CHECK(codes_set_long(h, "timeRangeIndicatorFromStepRange", -1), 0);
CODES_CHECK(codes_set_long(h, "missingValue", 9999), 0);
/* 0 = Meteorological products (grib2/tables/4/0.0.table) */
CODES_CHECK(codes_set_long(h,"discipline",0),0);
CODES_CHECK(codes_set_long(h, "discipline", 0), 0);
CODES_CHECK(codes_set_long(h,"editionNumber",2),0);
CODES_CHECK(codes_set_long(h, "editionNumber", 2), 0);
/* 98 = European Center for Medium-Range Weather Forecasts */
CODES_CHECK(codes_set_long(h,"centre",98),0);
CODES_CHECK(codes_set_long(h, "centre", 98), 0);
CODES_CHECK(codes_set_long(h,"subCentre",0),0);
CODES_CHECK(codes_set_long(h, "subCentre", 0), 0);
/* 4 = Version implemented on 7 November 2007 (grib2/tables/1.0.table) */
CODES_CHECK(codes_set_long(h,"tablesVersion",4),0);
CODES_CHECK(codes_set_long(h, "tablesVersion", 4), 0);
/* 0 = Local tables not used (grib2/tables/4/1.1.table) */
CODES_CHECK(codes_set_long(h,"localTablesVersion",0),0);
CODES_CHECK(codes_set_long(h, "localTablesVersion", 0), 0);
/* 1 = Start of forecast (grib2/tables/4/1.2.table) */
CODES_CHECK(codes_set_long(h,"significanceOfReferenceTime",1),0);
CODES_CHECK(codes_set_long(h, "significanceOfReferenceTime", 1), 0);
CODES_CHECK(codes_set_long(h,"year",2007),0);
CODES_CHECK(codes_set_long(h,"month",3),0);
CODES_CHECK(codes_set_long(h,"day",23),0);
CODES_CHECK(codes_set_long(h,"hour",12),0);
CODES_CHECK(codes_set_long(h,"minute",0),0);
CODES_CHECK(codes_set_long(h,"second",0),0);
CODES_CHECK(codes_set_long(h,"dataDate",20070323),0);
CODES_CHECK(codes_set_long(h,"dataTime",1200),0);
CODES_CHECK(codes_set_long(h, "year", 2007), 0);
CODES_CHECK(codes_set_long(h, "month", 3), 0);
CODES_CHECK(codes_set_long(h, "day", 23), 0);
CODES_CHECK(codes_set_long(h, "hour", 12), 0);
CODES_CHECK(codes_set_long(h, "minute", 0), 0);
CODES_CHECK(codes_set_long(h, "second", 0), 0);
CODES_CHECK(codes_set_long(h, "dataDate", 20070323), 0);
CODES_CHECK(codes_set_long(h, "dataTime", 1200), 0);
/* 0 = Operational products (grib2/tables/4/1.3.table) */
CODES_CHECK(codes_set_long(h,"productionStatusOfProcessedData",0),0);
CODES_CHECK(codes_set_long(h, "productionStatusOfProcessedData", 0), 0);
/* 2 = Analysis and forecast products (grib2/tables/4/1.4.table) */
CODES_CHECK(codes_set_long(h,"typeOfProcessedData",2),0);
CODES_CHECK(codes_set_long(h, "typeOfProcessedData", 2), 0);
CODES_CHECK(codes_set_long(h,"selectStepTemplateInterval",1),0);
CODES_CHECK(codes_set_long(h,"selectStepTemplateInstant",1),0);
CODES_CHECK(codes_set_long(h,"grib2LocalSectionPresent",0),0);
CODES_CHECK(codes_set_long(h, "selectStepTemplateInterval", 1), 0);
CODES_CHECK(codes_set_long(h, "selectStepTemplateInstant", 1), 0);
CODES_CHECK(codes_set_long(h, "grib2LocalSectionPresent", 0), 0);
/* 0 = Specified in Code table 3.1 (grib2/tables/4/3.0.table) */
CODES_CHECK(codes_set_long(h,"sourceOfGridDefinition",0),0);
CODES_CHECK(codes_set_long(h, "sourceOfGridDefinition", 0), 0);
CODES_CHECK(codes_set_long(h,"numberOfDataPoints",496),0);
CODES_CHECK(codes_set_long(h,"numberOfOctectsForNumberOfPoints",0),0);
CODES_CHECK(codes_set_long(h, "numberOfDataPoints", 496), 0);
CODES_CHECK(codes_set_long(h, "numberOfOctectsForNumberOfPoints", 0), 0);
/* 0 = There is no appended list (grib2/tables/4/3.11.table) */
CODES_CHECK(codes_set_long(h,"interpretationOfNumberOfPoints",0),0);
CODES_CHECK(codes_set_long(h, "interpretationOfNumberOfPoints", 0), 0);
CODES_CHECK(codes_set_long(h,"PLPresent",0),0);
CODES_CHECK(codes_set_long(h, "PLPresent", 0), 0);
/* 0 = Latitude/longitude. Also called equidistant cylindrical, or Plate Carree (grib2/tables/4/3.1.table) */
CODES_CHECK(codes_set_long(h,"gridDefinitionTemplateNumber",0),0);
CODES_CHECK(codes_set_long(h, "gridDefinitionTemplateNumber", 0), 0);
/* 0 = Earth assumed spherical with radius = 6,367,470.0 m (grib2/tables/4/3.2.table) */
CODES_CHECK(codes_set_long(h,"shapeOfTheEarth",0),0);
CODES_CHECK(codes_set_long(h, "shapeOfTheEarth", 0), 0);
CODES_CHECK(codes_set_missing(h,"scaleFactorOfRadiusOfSphericalEarth"),0);
CODES_CHECK(codes_set_missing(h,"scaledValueOfRadiusOfSphericalEarth"),0);
CODES_CHECK(codes_set_missing(h,"scaleFactorOfEarthMajorAxis"),0);
CODES_CHECK(codes_set_missing(h,"scaledValueOfEarthMajorAxis"),0);
CODES_CHECK(codes_set_missing(h,"scaleFactorOfEarthMinorAxis"),0);
CODES_CHECK(codes_set_missing(h,"scaledValueOfEarthMinorAxis"),0);
CODES_CHECK(codes_set_long(h,"radius",6367470),0);
CODES_CHECK(codes_set_long(h,"Ni",16),0);
CODES_CHECK(codes_set_long(h,"Nj",31),0);
CODES_CHECK(codes_set_long(h,"basicAngleOfTheInitialProductionDomain",0),0);
CODES_CHECK(codes_set_long(h,"mBasicAngle",0),0);
CODES_CHECK(codes_set_long(h,"angleMultiplier",1),0);
CODES_CHECK(codes_set_long(h,"mAngleMultiplier",1000000),0);
CODES_CHECK(codes_set_missing(h,"subdivisionsOfBasicAngle"),0);
CODES_CHECK(codes_set_long(h,"angleDivisor",1000000),0);
CODES_CHECK(codes_set_long(h,"latitudeOfFirstGridPoint",60000000),0);
CODES_CHECK(codes_set_long(h,"longitudeOfFirstGridPoint",0),0);
CODES_CHECK(codes_set_missing(h, "scaleFactorOfRadiusOfSphericalEarth"), 0);
CODES_CHECK(codes_set_missing(h, "scaledValueOfRadiusOfSphericalEarth"), 0);
CODES_CHECK(codes_set_missing(h, "scaleFactorOfEarthMajorAxis"), 0);
CODES_CHECK(codes_set_missing(h, "scaledValueOfEarthMajorAxis"), 0);
CODES_CHECK(codes_set_missing(h, "scaleFactorOfEarthMinorAxis"), 0);
CODES_CHECK(codes_set_missing(h, "scaledValueOfEarthMinorAxis"), 0);
CODES_CHECK(codes_set_long(h, "radius", 6367470), 0);
CODES_CHECK(codes_set_long(h, "Ni", 16), 0);
CODES_CHECK(codes_set_long(h, "Nj", 31), 0);
CODES_CHECK(codes_set_long(h, "basicAngleOfTheInitialProductionDomain", 0), 0);
CODES_CHECK(codes_set_long(h, "mBasicAngle", 0), 0);
CODES_CHECK(codes_set_long(h, "angleMultiplier", 1), 0);
CODES_CHECK(codes_set_long(h, "mAngleMultiplier", 1000000), 0);
CODES_CHECK(codes_set_missing(h, "subdivisionsOfBasicAngle"), 0);
CODES_CHECK(codes_set_long(h, "angleDivisor", 1000000), 0);
CODES_CHECK(codes_set_long(h, "latitudeOfFirstGridPoint", 60000000), 0);
CODES_CHECK(codes_set_long(h, "longitudeOfFirstGridPoint", 0), 0);
/* 48 = 00110000
(3=1) i direction increments given
(4=1) j direction increments given
(5=0) Resolved u- and v- components of vector quantities relative to easterly and northerly directions
See grib2/tables/[tablesVersion]/3.3.table */
CODES_CHECK(codes_set_long(h,"resolutionAndComponentFlags",48),0);
CODES_CHECK(codes_set_long(h, "resolutionAndComponentFlags", 48), 0);
CODES_CHECK(codes_set_long(h,"iDirectionIncrementGiven",1),0);
CODES_CHECK(codes_set_long(h,"jDirectionIncrementGiven",1),0);
CODES_CHECK(codes_set_long(h,"uvRelativeToGrid",0),0);
CODES_CHECK(codes_set_long(h,"latitudeOfLastGridPoint",0),0);
CODES_CHECK(codes_set_long(h,"longitudeOfLastGridPoint",30000000),0);
CODES_CHECK(codes_set_long(h,"iDirectionIncrement",2000000),0);
CODES_CHECK(codes_set_long(h,"jDirectionIncrement",2000000),0);
CODES_CHECK(codes_set_long(h, "iDirectionIncrementGiven", 1), 0);
CODES_CHECK(codes_set_long(h, "jDirectionIncrementGiven", 1), 0);
CODES_CHECK(codes_set_long(h, "uvRelativeToGrid", 0), 0);
CODES_CHECK(codes_set_long(h, "latitudeOfLastGridPoint", 0), 0);
CODES_CHECK(codes_set_long(h, "longitudeOfLastGridPoint", 30000000), 0);
CODES_CHECK(codes_set_long(h, "iDirectionIncrement", 2000000), 0);
CODES_CHECK(codes_set_long(h, "jDirectionIncrement", 2000000), 0);
/* 0 = 00000000
(1=0) Points of first row or column scan in the +i (+x) direction
@ -138,101 +138,101 @@ int main(int argc, char** argv)
(3=0) Adjacent points in i (x) direction are consecutive
(4=0) All rows scan in the same direction
See grib2/tables/[tablesVersion]/3.4.table */
CODES_CHECK(codes_set_long(h,"scanningMode",0),0);
CODES_CHECK(codes_set_long(h, "scanningMode", 0), 0);
CODES_CHECK(codes_set_long(h,"iScansNegatively",0),0);
CODES_CHECK(codes_set_long(h,"jScansPositively",0),0);
CODES_CHECK(codes_set_long(h,"jPointsAreConsecutive",0),0);
CODES_CHECK(codes_set_long(h,"alternativeRowScanning",0),0);
CODES_CHECK(codes_set_long(h,"iScansPositively",1),0);
CODES_CHECK(codes_set_long(h, "iScansNegatively", 0), 0);
CODES_CHECK(codes_set_long(h, "jScansPositively", 0), 0);
CODES_CHECK(codes_set_long(h, "jPointsAreConsecutive", 0), 0);
CODES_CHECK(codes_set_long(h, "alternativeRowScanning", 0), 0);
CODES_CHECK(codes_set_long(h, "iScansPositively", 1), 0);
/* ITERATOR */
/* NEAREST */
CODES_CHECK(codes_set_long(h,"timeRangeIndicator",0),0);
CODES_CHECK(codes_set_long(h,"NV",0),0);
CODES_CHECK(codes_set_long(h,"neitherPresent",0),0);
CODES_CHECK(codes_set_long(h, "timeRangeIndicator", 0), 0);
CODES_CHECK(codes_set_long(h, "NV", 0), 0);
CODES_CHECK(codes_set_long(h, "neitherPresent", 0), 0);
/* 0 = Analysis or forecast at a horizontal level or in a horizontal layer at a point in time (grib2/tables/4/4.0.table) */
CODES_CHECK(codes_set_long(h,"productDefinitionTemplateNumber",0),0);
CODES_CHECK(codes_set_long(h, "productDefinitionTemplateNumber", 0), 0);
/* Parameter information */
/* 0 = Temperature (grib2/tables/4/4.1.0.table) */
CODES_CHECK(codes_set_long(h,"parameterCategory",0),0);
CODES_CHECK(codes_set_long(h, "parameterCategory", 0), 0);
/* 0 = Temperature (K) (grib2/tables/4/4.2.0.0.table) */
CODES_CHECK(codes_set_long(h,"parameterNumber",0),0);
CODES_CHECK(codes_set_long(h, "parameterNumber", 0), 0);
/* 0 = Analysis (grib2/tables/4/4.3.table) */
CODES_CHECK(codes_set_long(h,"typeOfGeneratingProcess",0),0);
CODES_CHECK(codes_set_long(h, "typeOfGeneratingProcess", 0), 0);
CODES_CHECK(codes_set_long(h,"backgroundProcess",255),0);
CODES_CHECK(codes_set_long(h,"generatingProcessIdentifier",128),0);
CODES_CHECK(codes_set_long(h,"hoursAfterDataCutoff",0),0);
CODES_CHECK(codes_set_long(h,"minutesAfterDataCutoff",0),0);
CODES_CHECK(codes_set_long(h, "backgroundProcess", 255), 0);
CODES_CHECK(codes_set_long(h, "generatingProcessIdentifier", 128), 0);
CODES_CHECK(codes_set_long(h, "hoursAfterDataCutoff", 0), 0);
CODES_CHECK(codes_set_long(h, "minutesAfterDataCutoff", 0), 0);
/* 1 = Hour (grib2/tables/4/4.4.table) */
CODES_CHECK(codes_set_long(h,"indicatorOfUnitOfTimeRange",1),0);
CODES_CHECK(codes_set_long(h, "indicatorOfUnitOfTimeRange", 1), 0);
/* 1 = Hour (stepUnits.table) */
CODES_CHECK(codes_set_long(h,"stepUnits",1),0);
CODES_CHECK(codes_set_long(h, "stepUnits", 1), 0);
CODES_CHECK(codes_set_long(h,"forecastTime",0),0);
CODES_CHECK(codes_set_long(h, "forecastTime", 0), 0);
/* 1 = Ground or water surface (grib2/tables/4/4.5.table) */
CODES_CHECK(codes_set_long(h,"typeOfFirstFixedSurface",1),0);
CODES_CHECK(codes_set_long(h, "typeOfFirstFixedSurface", 1), 0);
CODES_CHECK(codes_set_missing(h,"scaleFactorOfFirstFixedSurface"),0);
CODES_CHECK(codes_set_missing(h,"scaledValueOfFirstFixedSurface"),0);
CODES_CHECK(codes_set_missing(h, "scaleFactorOfFirstFixedSurface"), 0);
CODES_CHECK(codes_set_missing(h, "scaledValueOfFirstFixedSurface"), 0);
/* 255 = Missing (grib2/tables/4/4.5.table) */
CODES_CHECK(codes_set_long(h,"typeOfSecondFixedSurface",255),0);
CODES_CHECK(codes_set_long(h, "typeOfSecondFixedSurface", 255), 0);
CODES_CHECK(codes_set_missing(h,"scaleFactorOfSecondFixedSurface"),0);
CODES_CHECK(codes_set_missing(h,"scaledValueOfSecondFixedSurface"),0);
CODES_CHECK(codes_set_long(h,"level",0),0);
CODES_CHECK(codes_set_long(h,"bottomLevel",0),0);
CODES_CHECK(codes_set_long(h,"topLevel",0),0);
CODES_CHECK(codes_set_long(h,"dummyc",0),0);
CODES_CHECK(codes_set_long(h,"PVPresent",0),0);
CODES_CHECK(codes_set_missing(h, "scaleFactorOfSecondFixedSurface"), 0);
CODES_CHECK(codes_set_missing(h, "scaledValueOfSecondFixedSurface"), 0);
CODES_CHECK(codes_set_long(h, "level", 0), 0);
CODES_CHECK(codes_set_long(h, "bottomLevel", 0), 0);
CODES_CHECK(codes_set_long(h, "topLevel", 0), 0);
CODES_CHECK(codes_set_long(h, "dummyc", 0), 0);
CODES_CHECK(codes_set_long(h, "PVPresent", 0), 0);
/* grib 2 Section 5 DATA REPRESENTATION SECTION */
CODES_CHECK(codes_set_long(h,"numberOfValues",496),0);
CODES_CHECK(codes_set_long(h, "numberOfValues", 496), 0);
/* 0 = Grid point data - simple packing (grib2/tables/4/5.0.table) */
CODES_CHECK(codes_set_long(h,"dataRepresentationTemplateNumber",0),0);
CODES_CHECK(codes_set_long(h, "dataRepresentationTemplateNumber", 0), 0);
CODES_CHECK(codes_set_long(h,"decimalScaleFactor",0),0);
CODES_CHECK(codes_set_long(h,"bitsPerValue",0),0);
CODES_CHECK(codes_set_long(h, "decimalScaleFactor", 0), 0);
CODES_CHECK(codes_set_long(h, "bitsPerValue", 0), 0);
/* 0 = Floating point (grib2/tables/4/5.1.table) */
CODES_CHECK(codes_set_long(h,"typeOfOriginalFieldValues",0),0);
CODES_CHECK(codes_set_long(h, "typeOfOriginalFieldValues", 0), 0);
CODES_CHECK(codes_set_long(h,"representationMode",0),0);
CODES_CHECK(codes_set_long(h, "representationMode", 0), 0);
/* grib 2 Section 6 BIT-MAP SECTION */
/* 255 = A bit map does not apply to this product (grib2/tables/4/6.0.table) */
CODES_CHECK(codes_set_long(h,"bitMapIndicator",255),0);
CODES_CHECK(codes_set_long(h, "bitMapIndicator", 255), 0);
CODES_CHECK(codes_set_long(h,"bitmapPresent",0),0);
CODES_CHECK(codes_set_long(h, "bitmapPresent", 0), 0);
/* grib 2 Section 7 data */
size = 496;
vdouble = (double*)calloc(size,sizeof(double));
if(!vdouble) {
fprintf(stderr,"failed to allocate %lu bytes\n",size*sizeof(double));
size = 496;
vdouble = (double*)calloc(size, sizeof(double));
if (!vdouble) {
fprintf(stderr, "failed to allocate %lu bytes\n", size * sizeof(double));
exit(1);
}
@ -361,28 +361,28 @@ int main(int argc, char** argv)
vdouble[ 488] = 1; vdouble[ 489] = 1; vdouble[ 490] = 1; vdouble[ 491] = 1;
vdouble[ 492] = 1; vdouble[ 493] = 1; vdouble[ 494] = 1; vdouble[ 495] = 1;
CODES_CHECK(codes_set_double_array(h,"values",vdouble,size),0);
CODES_CHECK(codes_set_double_array(h, "values", vdouble, size), 0);
free(vdouble);
CODES_CHECK(codes_set_long(h,"dirty_statistics",1),0);
CODES_CHECK(codes_set_long(h,"changeDecimalPrecision",0),0);
CODES_CHECK(codes_set_long(h,"decimalPrecision",0),0);
CODES_CHECK(codes_set_long(h,"setBitsPerValue",0),0);
/* Save the message */
CODES_CHECK(codes_set_long(h, "dirty_statistics", 1), 0);
CODES_CHECK(codes_set_long(h, "changeDecimalPrecision", 0), 0);
CODES_CHECK(codes_set_long(h, "decimalPrecision", 0), 0);
CODES_CHECK(codes_set_long(h, "setBitsPerValue", 0), 0);
/* Save the message */
f = fopen(argv[1],"wb");
if(!f) {
f = fopen(argv[1], "wb");
if (!f) {
perror(argv[1]);
exit(1);
}
CODES_CHECK(codes_get_message(h,&buffer,&size),0);
CODES_CHECK(codes_get_message(h, &buffer, &size), 0);
if(fwrite(buffer,1,size,f) != size) {
if (fwrite(buffer, 1, size, f) != size) {
perror(argv[1]);
exit(1);
}
if(fclose(f)) {
if (fclose(f)) {
perror(argv[1]);
exit(1);
}

View File

@ -23,26 +23,26 @@ int main(int argc, char** argv)
{
int err = 0, is_missing = 0;
FILE* in = NULL;
const char* infile = "../../data/reduced_gaussian_pressure_level.grib2";
FILE* out = NULL;
FILE* in = NULL;
const char* infile = "../../data/reduced_gaussian_pressure_level.grib2";
FILE* out = NULL;
const char* outfile = "out_surface_level.grib2";
codes_handle *h = NULL;
const void* buffer = NULL;
size_t size=0;
char str[]="sfc";
size_t str_len=3;
long Ni = 0;
codes_handle* h = NULL;
const void* buffer = NULL;
size_t size = 0;
char str[] = "sfc";
size_t str_len = 3;
long Ni = 0;
in = fopen(infile,"rb");
if(!in) {
printf("ERROR: unable to open input file %s\n",infile);
in = fopen(infile, "rb");
if (!in) {
printf("ERROR: unable to open input file %s\n", infile);
return 1;
}
out = fopen(outfile,"wb");
if(!out) {
printf("ERROR: unable to open output file %s\n",outfile);
out = fopen(outfile, "wb");
if (!out) {
printf("ERROR: unable to open output file %s\n", outfile);
fclose(in);
return 1;
}
@ -50,26 +50,25 @@ int main(int argc, char** argv)
/* create a new handle from a message in a file */
h = codes_handle_new_from_file(0, in, PRODUCT_GRIB, &err);
if (h == NULL) {
printf("Error: unable to create handle from file %s\n",infile);
printf("Error: unable to create handle from file %s\n", infile);
}
CODES_CHECK(codes_set_string(h,"typeOfFirstFixedSurface",str,&str_len),0);
CODES_CHECK(codes_set_missing(h,"scaleFactorOfFirstFixedSurface"),0);
CODES_CHECK(codes_set_missing(h,"scaledValueOfFirstFixedSurface"),0);
CODES_CHECK(codes_set_string(h, "typeOfFirstFixedSurface", str, &str_len), 0);
CODES_CHECK(codes_set_missing(h, "scaleFactorOfFirstFixedSurface"), 0);
CODES_CHECK(codes_set_missing(h, "scaledValueOfFirstFixedSurface"), 0);
/* See GRIB-490 */
CODES_CHECK(codes_get_long(h,"Ni",&Ni),0);
CODES_CHECK(codes_get_long(h, "Ni", &Ni), 0);
is_missing = codes_is_missing(h, "Ni", &err);
CODES_CHECK(err,0);
CODES_CHECK(err, 0);
if (is_missing != 1) assert(!"Ni should be missing");
CODES_CHECK(codes_set_long(h,"Ni", Ni),0);
CODES_CHECK(codes_set_long(h, "Ni", Ni), 0);
/* get the coded message in a buffer */
CODES_CHECK(codes_get_message(h,&buffer,&size),0);
CODES_CHECK(codes_get_message(h, &buffer, &size), 0);
/* write the buffer in a file*/
if(fwrite(buffer,1,size,out) != size)
{
if (fwrite(buffer, 1, size, out) != size) {
perror(argv[1]);
exit(1);
}

View File

@ -11,55 +11,54 @@
int main(int argc, char* argv[])
{
FILE* f;
int err=0;
char infile[]="../../data/reduced_gaussian_model_level.grib1";
codes_handle *h=NULL;
codes_context* c=codes_context_get_default();
codes_values values[2];
int nvalues=2;
int i;
char* name = NULL;
FILE* f;
int err = 0;
char infile[] = "../../data/reduced_gaussian_model_level.grib1";
codes_handle* h = NULL;
codes_context* c = codes_context_get_default();
codes_values values[2];
int nvalues = 2;
int i;
char* name = NULL;
f=fopen(infile,"rb");
if (!f) {
perror(infile);
exit(1);
}
f = fopen(infile, "rb");
if (!f) {
perror(infile);
exit(1);
}
h=codes_handle_new_from_file(c,f,PRODUCT_GRIB,&err);
if (!h) {
printf("unable to create handle from file %s\n",infile);
exit(err);
}
fclose(f);
h = codes_handle_new_from_file(c, f, PRODUCT_GRIB, &err);
if (!h) {
printf("unable to create handle from file %s\n", infile);
exit(err);
}
fclose(f);
values[0].type=CODES_TYPE_LONG;
values[0].name="centre";
values[0].long_value=98;
values[0].type = CODES_TYPE_LONG;
values[0].name = "centre";
values[0].long_value = 98;
values[1].type=CODES_TYPE_LONG;
values[1].name="level";
values[1].long_value=2;
values[1].type = CODES_TYPE_LONG;
values[1].name = "level";
values[1].long_value = 2;
/*CODES_VALUE_DIFFERENT -> value is different*/
err=codes_values_check(h,values,nvalues);
if (err) {
for (i=0;i<nvalues;i++) {
if (values[i].error==err) name=(char*)values[i].name;
}
printf("ERROR: \"%s\" %s\n",name,codes_get_error_message(err));
}
/*CODES_VALUE_DIFFERENT -> value is different*/
err = codes_values_check(h, values, nvalues);
if (err) {
for (i = 0; i < nvalues; i++) {
if (values[i].error == err) name = (char*)values[i].name;
}
printf("ERROR: \"%s\" %s\n", name, codes_get_error_message(err));
}
values[1].name="levelll";
err=codes_values_check(h,values,nvalues);
if (err) {
for (i=0;i<nvalues;i++) {
if (values[i].error==err) name=(char*)values[i].name;
}
printf("ERROR: \"%s\" %s\n",name,codes_get_error_message(err));
}
return 0;
values[1].name = "levelll";
err = codes_values_check(h, values, nvalues);
if (err) {
for (i = 0; i < nvalues; i++) {
if (values[i].error == err) name = (char*)values[i].name;
}
printf("ERROR: \"%s\" %s\n", name, codes_get_error_message(err));
}
return 0;
}