1 /*
2   This file is part of CDO. CDO is a collection of Operators to manipulate and analyse Climate model Data.
3 
4   Author: Uwe Schulzweida
5 
6 */
7 
8 #include <string.h>
9 #include <assert.h>
10 #include "compare.h"
11 #include "stdnametable.h"
12 
13 struct stdnametable_t
14 {
15   int varid;
16   int echamcode;
17   const char *name;
18   const char *stdname;  // Standard name
19   const char *units;    // Units
20 };
21 
22 static const stdnametable_t stdnametable[] = {
23   // clang-format off
24   // varid                                  code    name           standard name                             units
25   { air_pressure,                              1,  "apres",       "air_pressure",                           "Pa" },
26   { pressure_thickness,                        2,  "dpress",      "pressure_thickness",                     "Pa" },
27   { surface_geopotential,                    129,  "geosp",       "surface_geopotential",                   "m2 s-2" },
28   { geopotential,                            129,  "z",           "geopotential",                           "m2 s-2" },
29   { air_temperature,                         130,  "ta",          "air_temperature",                        "K" },
30   { specific_humidity,                       133,  "hus",         "specific_humidity",                      "1" },
31   { surface_air_pressure,                    134,  "aps",         "surface_air_pressure",                   "Pa" },
32   { air_pressure_at_sea_level,               151,  "psl",         "air_pressure_at_sea_level",              "Pa" },
33   { geopotential_height,                     156,  "zh",          "geopotential_height",                    "m" },
34   { geometric_height_at_full_level_center,    21,  "zg",          "geometric_height_at_full_level_center",  "m" },
35   { geometric_height_at_half_level_center,    22,  "zghalf",      "geometric_height_at_half_level_center",  "m" },
36   // clang-format on
37 };
38 
39 static int
stdnametable_idx(int varid)40 stdnametable_idx(int varid)
41 {
42   int num_entries = (int) (sizeof(stdnametable) / sizeof(stdnametable_t));
43 
44   int idx;
45   for (idx = 0; idx < num_entries; ++idx)
46     if (stdnametable[idx].varid == varid) break;
47 
48   assert(idx < num_entries);
49 
50   return idx;
51 }
52 
53 int
var_echamcode(int varid)54 var_echamcode(int varid)
55 {
56   return stdnametable[stdnametable_idx(varid)].echamcode;
57 }
58 
59 const char *
var_name(int varid)60 var_name(int varid)
61 {
62   return stdnametable[stdnametable_idx(varid)].name;
63 }
64 
65 const char *
var_stdname(int varid)66 var_stdname(int varid)
67 {
68   return stdnametable[stdnametable_idx(varid)].stdname;
69 }
70 
71 const char *
var_units(int varid)72 var_units(int varid)
73 {
74   return stdnametable[stdnametable_idx(varid)].units;
75 }
76 
77 int
echamcode_from_stdname(const char * stdname)78 echamcode_from_stdname(const char *stdname)
79 {
80   int code = -1;
81 
82   // clang-format off
83   if      (cdo_cmpstr(stdname, var_stdname(surface_geopotential))     ) code = 129;
84   else if (cdo_cmpstr(stdname, var_stdname(geopotential))             ) code = 129;
85   else if (cdo_cmpstr(stdname, var_stdname(air_temperature))          ) code = 130;
86   else if (cdo_cmpstr(stdname, var_stdname(specific_humidity))        ) code = 133;
87   else if (cdo_cmpstr(stdname, var_stdname(surface_air_pressure))     ) code = 134;
88   else if (cdo_cmpstr(stdname, var_stdname(air_pressure_at_sea_level))) code = 151;
89   else if (cdo_cmpstr(stdname, var_stdname(geopotential_height))      ) code = 156;
90   // clang-format on
91 
92   return code;
93 }
94 
95 void
echam_gribcodes(gribcode_t * gribcodes)96 echam_gribcodes(gribcode_t *gribcodes)
97 {
98   // clang-format off
99   gribcodes->geopot  =  129;
100   gribcodes->temp    =  130;
101   gribcodes->hum     =  133;
102   gribcodes->ps      =  134;
103   gribcodes->lsp     =  152;
104   gribcodes->gheight =  156;
105   gribcodes->wind    =    0;
106   gribcodes->uwind   =  131;
107   gribcodes->vwind   =  132;
108   // clang-format on
109 }
110 
111 void
wmo_gribcodes(gribcode_t * gribcodes)112 wmo_gribcodes(gribcode_t *gribcodes)
113 {
114   // clang-format off
115   gribcodes->geopot  =   6;
116   gribcodes->temp    =  11;
117   gribcodes->hum     =   0;
118   gribcodes->ps      =   1;
119   gribcodes->lsp     =   0;
120   gribcodes->gheight =   7;
121   gribcodes->wind    =  10;
122   gribcodes->uwind   = 131;
123   gribcodes->vwind   = 132;
124   // clang-format on
125   /*  ECMWF (IFS) GLOBAL Model
126    *
127    *
128    http://old.ecmwf.int/publications/manuals/d/gribapi/param/filter=grib1/order=paramId/order_type=asc/p=1/search=wind/table=128/
129    Wind speed	ws	m s**-1	10	grib1	grib2
130    U component of wind	u	m s**-1	131	grib1	grib2	netcdf
131    V component of wind	v	m s**-1	132	grib1	grib2	netcdf
132    10 metre U wind component	10u	m s**-1	165	grib1	grib2
133    10 metre V wind component	10v	m s**-1	166	grib1	grib2
134    10 metre wind speed	10si	m s**-1	207	grib1	grib2
135   */
136 }
137 
138 void
hirlam_harmonie_gribcodes(gribcode_t * gribcodes)139 hirlam_harmonie_gribcodes(gribcode_t *gribcodes)
140 {
141   // clang-format off
142   gribcodes->geopot  =   6; // Geopotential [m2/s2]
143   gribcodes->temp    =  11; // Temperature [K]
144   gribcodes->hum     =  51; // Specific humidity [kg/kg]
145   gribcodes->ps      =   1; // Pressure [Pa]
146   gribcodes->lsp     =   0; // - not available -
147   gribcodes->gheight =   7; // 	007 Geopotential height [Gpm]; 008 Geometric height [m]
148   gribcodes->wind    =  32;
149   gribcodes->uwind   =  33;
150   gribcodes->vwind   =  34;
151   // clang-format on
152   /*
153    1/103/0 	"mean sea level pressure" pressure_msl   [Pa]
154    1/105/0 	"surface pressure"  pressure_surf  [Pa]
155    032	Wind speed          	m/s 	WIND
156    033	u-component of wind 	m/s 	UGRD
157    034	v-component of wind 	m/s 	VGRD
158    051	Specific humidity
159    052	Relative humidity
160    NOT AVAILABLE:
161    - Natural log of surface pressure 	ln(kPa) 	NLGSP
162   */
163 }
164