bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
HDF5CFUtil.h
Go to the documentation of this file.
1// This file is part of the hdf5_handler implementing for the CF-compliant
2// Copyright (c) 2011-2023 The HDF Group, Inc. and OPeNDAP, Inc.
3//
4// This is free software; you can redistribute it and/or modify it under the
5// terms of the GNU Lesser General Public License as published by the Free
6// Software Foundation; either version 2.1 of the License, or (at your
7// option) any later version.
8//
9// This software is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
12// License for more details.
13//
14// You should have received a copy of the GNU Lesser General Public
15// License along with this library; if not, write to the Free Software
16// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17//
18// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
19// You can contact The HDF Group, Inc. at 410 E University Ave,
20// Suite 200, Champaign, IL 61820
21
31
32#ifndef _HDF5CFUtil_H
33#define _HDF5CFUtil_H
34#include <stdlib.h>
35#include <fcntl.h>
36#include <string.h>
37#include <set>
38#include <vector>
39#include <string>
40#include <iostream>
41#include <unistd.h>
42#include <cerrno>
43#include "hdf5.h"
44#include "HE5Grid.h"
45
46// We create this intermediate enum H5DataType in order to totally separate the
47// creating of DAS and DDS from any HDF5 API calls. When mapping to DAP, only this
48// intermediate H5DataType will be used to map to the corresponding DAP datatype.
49// Here H5UNSUPTYPE includes H5T_TIME, H5T_BITFIELD, H5T_OPAQUE,H5T_ENUM, H5T_VLEN.
50// For CF option, H5REFERENCE, H5COMPOUND, H5ARRAY will not be supported. We leave them
51// here for future merging of default option and CF option. Currently DAP2 doesn't
52// support 64-bit integer. We still list int64 bit types since we find ACOSL2S or OCO2L1B have this
53// datatype and we need to provide a special mapping for this datatype.
54// H5UCHAR also needs a special mapping. Similiarly other unsupported types may need to
55// have special mappings in the future. So the following enum type may be extended
56// according to the future need. The idea is that all the necessary special mappings should
57// be handled in the HDF5CF name space.
58// The DDS and DAS generation modules should not use any HDF5 APIs.
59enum H5DataType
60{H5FSTRING, H5FLOAT32,H5CHAR,H5UCHAR,H5INT16,H5UINT16,
61 H5INT32,H5UINT32,H5INT64,H5UINT64,H5FLOAT64,H5VSTRING,
62 H5REFERENCE,H5COMPOUND,H5ARRAY,H5UNSUPTYPE};
63
64enum CVType { CV_EXIST,CV_LAT_MISS,CV_LON_MISS,CV_NONLATLON_MISS,CV_FILLINDEX,CV_MODIFY,CV_SPECIAL,CV_UNSUPPORTED};
65
66// Angle Conversion Codes, this is for using GCTP to calculate lat/lon. Since currently we only have HDF-EOS5 grid products
67// to test and we adopt the way how HDF-EOS uses GCTP, we still keep HDF-EOS5 mark(HE5) although our functions can be
68// used to handle non-HDF-EOS5 products.
69const int HE5_HDFE_RAD_DEG=0;
70const int HE5_HDFE_DEG_RAD=1;
71const int HE5_HDFE_DMS_DEG=2;
72const int HE5_HDFE_DEG_DMS=3;
73const int HE5_HDFE_RAD_DMS=4;
74const int HE5_HDFE_DMS_RAD=5;
75
76
77
79 std::string name1;
80 std::string name2;
81 hsize_t size1;
82 hsize_t size2;
83 int rank;
84};
85
86struct HDF5CFUtil {
87
88 static bool use_data_mem_cache(H5DataType h5type,CVType cvtype,const std::string & varpath);
89
90 static size_t H5_numeric_atomic_type_size(H5DataType h5type);
92 static H5DataType H5type_to_H5DAPtype(hid_t h5_type_id);
93
96 static std::string trim_string(hid_t dtypeid,const std::string & s, int num_sect, size_t section_size, std::vector<size_t>& sect_newsize);
97
98 static std::string obtain_string_after_lastslash(const std::string & s);
99 static std::string obtain_string_before_lastslash(const std::string & s);
100 static std::string remove_substrings(std::string str, const std::string &s);
101
102
103 // Make the coordinate follow the CF conventions when the group hierarchy is flattened.
104 static void cha_co(std::string &co,const std::string & vpath);
105
106 // Supposed string temp_str contains several relpath, Obtain all the positions of relpath in temp_str.
107 static void get_relpath_pos(const std::string& temp_str,const std::string& relpath,std::vector<size_t>&var_pos);
108
109 static bool cf_strict_support_type(H5DataType dtype,bool is_dap4);
110 static bool cf_dap2_support_numeric_type(H5DataType dtype,bool is_dap4);
111
112 // Obtain the unique name for the clashed names and save it to set namelist.
113 static void gen_unique_name(std::string &str, std::set<std::string>&namelist,int&clash_index);
114
117 static void Split (const char *s, int len, char sep,
118 std::vector < std::string > &names);
119
121 static void Split (const char *sz, char sep,
122 std::vector < std::string > &names);
123
124 static void Split_helper(std::vector<std::string>&tokens, const std::string &text,const char sep);
125
126 // Parse GPM Level 3 GridHeaders
127 static void parser_gpm_l3_gridheader(const std:: vector<char>&value, int& latsize, int&lonsize,
128 float& lat_start, float& lon_start, float& lat_res, float& lon_res, bool check_reg_orig);
129
130 static void close_fileid(hid_t,bool);
131
132 // wrap function of Unix read to a buffer. Memory for the buffer should be allocated.
133 static ssize_t read_buffer_from_file(int fd,void*buf,size_t);
134 static std::string obtain_cache_fname(const std::string & fprefix, const std::string & fname, const std::string &vname);
135
136 //Use to generate cache file name.
137 // Reverse the char array order
138 static void rev_str(char *str, int len);
139
140 // Return the index of the char array for the integer part
141 static int int_to_str(int,char str[],int);
142
143 // Convert a double number to char array
144 static void dtoa(double,char *,int);
145
146 // convert double to string
147 static std::string get_double_str(double x,int total_digit,int after_point);
148
149 // convert int to string
150 static std::string get_int_str(int x);
151
152
153#if 0
154 //static int GDij2ll(int projcode, int zonecode, double projparm[],
155 // int spherecode, int xdimsize, int ydimsize,
156 // double upleftpt[], double lowrightpt[],
157 // int npnts, int row[], int col[],
158 // double longitude[], double latitude[], int pixcen, int pixcnr);
159
160 //static size_t INDEX_nD_TO_1D (const std::vector < size_t > &dims,
161 // const std::vector < size_t > &pos);
162#endif
163#if 0
164 template<typename T> int subset(
165 const T input[],
166 int rank,
167 vector<int> & dim,
168 int start[],
169 int stride[],
170 int edge[],
171 std::vector<T> *poutput,
172 vector<int>& pos,
173 int index);
174#endif
175} ;
176
177static inline struct flock *lock(int type) {
178 static struct flock lock;
179 lock.l_type = (short)type;
180 lock.l_whence = SEEK_SET;
181 lock.l_start = 0;
182 lock.l_len = 0;
183 lock.l_pid = getpid();
184
185 return &lock;
186}
187
188static inline std::string get_errno() {
189 const char *s_err = strerror(errno);
190 if (s_err)
191 return s_err;
192 else
193 return "Unknown error.";
194}
195
196
197
198#if 0
199{
200 //
201 // int a[10][20][30]; // & a[1][2][3] == a + (20*30+1 + 30*2 + 1 *3);
202 // int b[10][2]; // &b[1][2] == b + (20*1 + 2);
203 //
204 if(dims.size () != pos.size ())
205 throw InternalErr(__FILE__,__LINE__,"dimension error in INDEX_nD_TO_1D routine.");
206 size_t sum = 0;
207 size_t start = 1;
208
209 for (size_t p = 0; p < pos.size (); p++) {
210 size_t m = 1;
211
212 for (size_t j = start; j < dims.size (); j++)
213 m *= dims[j];
214 sum += m * pos[p];
215 start++;
216 }
217 return sum;
218}
219#endif
220 int GDij2ll(int projcode, int zonecode, double projparm[],
221 int spherecode, int xdimsize, int ydimsize,
222 double upleftpt[], double lowrightpt[],
223 int npnts, int row[], int col[],
224 double longitude[], double latitude[], EOS5GridPRType pixcen, EOS5GridOriginType pixcnr);
225#if 0
226//extern int inv_init(int insys, int inzone, double *inparm, int indatum, char *fn27, char *fn83, int *iflg, int (*inv_trans[])(double, double, double*, double*));
227
228//extern int for_init(int outsys, int outzone, double *outparm, int outdatum, char *fn27, char *fn83, int *iflg, int (*for_trans[])(double, double, double *, double *));
229#endif
230 double HE5_EHconvAng(double inAngle, int code);
231#endif
STL class.
static void Split(const char *s, int len, char sep, std::vector< std::string > &names)
static H5DataType H5type_to_H5DAPtype(hid_t h5_type_id)
Map HDF5 Datatype to the intermediate H5DAPtype for the future use.
Definition HDF5CFUtil.cc:53
static std::string trim_string(hid_t dtypeid, const std::string &s, int num_sect, size_t section_size, std::vector< size_t > &sect_newsize)
static ssize_t read_buffer_from_file(int fd, void *buf, size_t)
Getting a subset of a variable.