Update version info for release v4.6.1 (#2122)
[WRF.git] / external / io_grib1 / MEL_grib1 / map_lvl.c
blob1458d033b90173f1ddfef33ceb38649449cf6d14
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include "dprints.h" /* for debug printing */
6 #include "grib_lookup.h" /* LVL_DEFN */
7 #include "gribfuncs.h" /* prototypes */
9 extern LVL_DEFN db_lvl_tbl[NLEV]; /* defined in ld_dec_lookup.c */
12 ************************************************************************
13 * A. FUNCTION: map_lvl
14 * Map the given Level_type to its appropriate usLevelid, scale up the
15 * Level_1 and Level_2 to GRIB unit and also return the Scale Factor,
16 * Reference.
18 * INTERFACE:
19 * int map_lvl (lvl_type, data_input, lvl_scl_fctr, lvl_reference, errmsg)
21 * ARGUMENTS (I=input, O=output, I&O=input and output):
22 * (I) char *lvl_type;
23 * name of Level to look for in the array of Level structures;
24 * (I&O) DATA_INPUT *data_input;
25 * structure holding data pertaining to current message required by
26 * the encoder; Three of its attributes get filled (usLevel_id,
27 * nLvl_1, nLvl_2);
28 * (O) float *lvl_scl_fctr, float *lvl_reference;
29 * numbers needed to scale the Level up to GRIB unit.
30 * multiply the level value by the Scale Factor, then add to the
31 * Reference to convert to GRIB unit;
32 * (O) char *errmsg;
33 * empty array, returned filled if error occurred;
35 * RETURN CODE:
36 * 0: success, DATA_INPUT filled, fbuff may have changed;
37 * 1: parameter not found, errmsg filled;
38 ************************************************************************
40 #if PROTOTYPE_NEEDED
41 int map_lvl ( char *lvl_type,
42 DATA_INPUT *data_input,
43 float *lvl_scl_fctr,
44 float *lvl_reference,
45 char *errmsg)
46 #else
47 int map_lvl ( lvl_type, data_input, lvl_scl_fctr, lvl_reference,errmsg)
48 char *lvl_type;
49 DATA_INPUT *data_input;
50 float *lvl_scl_fctr;
51 float *lvl_reference;
52 char *errmsg;
53 #endif
55 char *func= "map_lvl";
56 int indx= 0; /* index for array */
57 int found = 0; /* set if located level */
58 LVL_DEFN *PL; /* working var */
60 DPRINT1 ("Entering %s\n", func);
62 * A.1 SEARCH the Level info table for the given Level Type
64 for (PL=db_lvl_tbl; indx < NLEV ; PL=(++indx +db_lvl_tbl))
65 if (PL->db_name[0] && !strcmp (PL->db_name, lvl_type)) {
66 found=1; break;
70 * A.2 IF (cannot find it) THEN
71 * FILL errmsg with message
72 * RETURN 1 ! bad status
73 * ENDIF
75 if (!found) {
76 DPRINT1 ("No '%s' in db_lvl_tbl;\n", lvl_type);
77 sprintf (errmsg, "%s: no '%s' in db_lvl_tbl;", func, lvl_type);
78 return (1);
82 * A.3 SCALE up nLvl_1 and nLvl_2 to GRIB's unit
84 data_input->nLvl_1 = (int)(data_input->nLvl_1 * PL->fScale + PL->fOffset);
85 data_input->nLvl_2 = (int)(data_input->nLvl_2 * PL->fScale + PL->fOffset);
89 * A.4 FILL in Level_id DATA_INPUT struct
90 * FILL in caller's Scale factor & Reference
92 data_input->usLevel_id = PL->usLevel_id;
93 *lvl_scl_fctr = PL->fScale;
94 *lvl_reference = PL->fOffset;
98 * A.5 RETURN with no errors
100 DPRINT6 (
101 "Found '%s'\nfill Data_Input->usLevel_id=%d; *lvl_scl=%lf, *lvl_ref=%lf\n"\
102 "Scaled up Data_Input->nLvl_1= %d\nScaled up Data_Input->Lvl_2= %d\n",
103 lvl_type,
104 data_input->usLevel_id , *lvl_scl_fctr ,*lvl_reference,
105 data_input->nLvl_1, data_input->nLvl_2);
107 DPRINT1 ("Exiting %s with no errors\n", func);
108 return (0);