1 #include <cdi.h>
2 #include <cstring>
3 #include <cstdlib>
4 
5 #include "cdi_int.h"
6 #include "cdo_default_values.h"
7 #include "cdo_cdi_wrapper.h"
8 
9 const char *
cdi_filetype_to_str(int filetype)10 cdi_filetype_to_str(int filetype)
11 {
12   switch (filetype)
13     {
14     // clang-format off
15     case CDI_FILETYPE_GRB:  return "GRIB";
16     case CDI_FILETYPE_GRB2: return "GRIB2";
17     case CDI_FILETYPE_NC:   return "NetCDF";
18     case CDI_FILETYPE_NC2:  return "NetCDF2";
19     case CDI_FILETYPE_NC4:  return "NetCDF4";
20     case CDI_FILETYPE_NC4C: return "NetCDF4 classic";
21     case CDI_FILETYPE_NC5:  return "NetCDF5";
22     case CDI_FILETYPE_SRV:  return "SERVICE";
23     case CDI_FILETYPE_EXT:  return "EXTRA";
24     case CDI_FILETYPE_IEG:  return "IEG";
25     default:                return "";
26       // clang-format on
27     }
28 }
29 
30 const char *
cdi_datatype_to_str(int datatype)31 cdi_datatype_to_str(int datatype)
32 {
33   static char str[20];
34   str[0] = 0;
35   if (datatype > 0 && datatype <= 32) snprintf(str, sizeof(str), "P%d", datatype);
36 
37   // clang-format off
38   if      (datatype == CDI_DATATYPE_PACK  ) return "P0";
39   else if (datatype > 0 && datatype <= 32 ) return str;
40   else if (datatype == CDI_DATATYPE_CPX32 ) return "C32";
41   else if (datatype == CDI_DATATYPE_CPX64 ) return "C64";
42   else if (datatype == CDI_DATATYPE_FLT32 ) return "F32";
43   else if (datatype == CDI_DATATYPE_FLT64 ) return "F64";
44   else if (datatype == CDI_DATATYPE_INT8  ) return "I8";
45   else if (datatype == CDI_DATATYPE_INT16 ) return "I16";
46   else if (datatype == CDI_DATATYPE_INT32 ) return "I32";
47   else if (datatype == CDI_DATATYPE_UINT8 ) return "U8";
48   else if (datatype == CDI_DATATYPE_UINT16) return "U16";
49   else if (datatype == CDI_DATATYPE_UINT32) return "U32";
50   else                                      return "";
51   // clang-format on
52 }
53 
54 int
cdo_str_to_datatype(const char * datatypestr)55 cdo_str_to_datatype(const char *datatypestr)
56 {
57   int datatype = -1;
58   size_t len = strlen(datatypestr);
59 
60   if (len > 1)
61     {
62       int ilen = atoi(datatypestr + 1);
63       // clang-format off
64       if (strncmp(datatypestr, "P0", len) == 0)          datatype = CDI_DATATYPE_PACK;
65       else if (strncmp(datatypestr, "P", 1) == 0 && ilen > 0 && ilen <= 32) datatype = atoi(datatypestr + 1);
66       else if (strncmp(datatypestr, "C32", len) == 0)    datatype = CDI_DATATYPE_CPX32;
67       else if (strncmp(datatypestr, "C64", len) == 0)    datatype = CDI_DATATYPE_CPX64;
68       else if (strncmp(datatypestr, "F32", len) == 0)    datatype = CDI_DATATYPE_FLT32;
69       else if (strncmp(datatypestr, "F64", len) == 0)    datatype = CDI_DATATYPE_FLT64;
70       else if (strncmp(datatypestr, "I8", len) == 0)     datatype = CDI_DATATYPE_INT8;
71       else if (strncmp(datatypestr, "I16", len) == 0)    datatype = CDI_DATATYPE_INT16;
72       else if (strncmp(datatypestr, "I32", len) == 0)    datatype = CDI_DATATYPE_INT32;
73       else if (strncmp(datatypestr, "U8", len) == 0)     datatype = CDI_DATATYPE_UINT8;
74       else if (strncmp(datatypestr, "U16", len) == 0)    datatype = CDI_DATATYPE_UINT16;
75       else if (strncmp(datatypestr, "U32", len) == 0)    datatype = CDI_DATATYPE_UINT32;
76       else if (strncmp(datatypestr, "real", len) == 0)   datatype = CDI_DATATYPE_FLT32;
77       else if (strncmp(datatypestr, "double", len) == 0) datatype = CDI_DATATYPE_FLT64;
78       // clang-format on
79     }
80 
81   return datatype;
82 }
83 
84 int
cdo_taxis_create(int taxisType)85 cdo_taxis_create(int taxisType)
86 {
87   if (CdoDefault::TaxisType != CDI_UNDEFID) taxisType = CdoDefault::TaxisType;
88   return taxisCreate(taxisType);
89 }
90 
cdo_taxis_copy_timestep(int taxisIDdes,int taxisIDsrc)91 void cdo_taxis_copy_timestep(int taxisIDdes, int taxisIDsrc)
92 {
93      taxisCopyTimestep(taxisIDdes, taxisIDsrc);
94 }
95 
96 void
cdo_def_table_id(int tableID)97 cdo_def_table_id(int tableID)
98 {
99   cdiDefTableID(tableID);
100 }
101 
102 void
grid_gen_xvals(int xsize,double xfirst,double xlast,double xinc,double * xvals)103 grid_gen_xvals(int xsize, double xfirst, double xlast, double xinc, double *xvals)
104 {
105   gridGenXvals(xsize, xfirst, xlast, xinc, xvals);
106 }
107 void
grid_gen_yvals(int gridtype,int ysize,double yfirst,double ylast,double yinc,double * yvals)108 grid_gen_yvals(int gridtype, int ysize, double yfirst, double ylast, double yinc, double *yvals)
109 {
110   gridGenYvals(gridtype, ysize, yfirst, ylast, yinc, yvals);
111 }
112