bes  Updated for version 3.20.6
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-2016 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 1800 South Oak Street,
20 // Suite 203, 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.
59 enum H5DataType
60 {H5FSTRING, H5FLOAT32,H5CHAR,H5UCHAR,H5INT16,H5UINT16,
61  H5INT32,H5UINT32,H5INT64,H5UINT64,H5FLOAT64,H5VSTRING,
62  H5REFERENCE,H5COMPOUND,H5ARRAY,H5UNSUPTYPE};
63 
64 enum 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.
69 const int HE5_HDFE_RAD_DEG=0;
70 const int HE5_HDFE_DEG_RAD=1;
71 const int HE5_HDFE_DMS_DEG=2;
72 const int HE5_HDFE_DEG_DMS=3;
73 const int HE5_HDFE_RAD_DMS=4;
74 const 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 
86 struct 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  static bool cf_strict_support_type(H5DataType dtype);
102  static bool cf_dap2_support_numeric_type(H5DataType dtype);
103 
104  // Obtain the unique name for the clashed names and save it to set namelist.
105  static void gen_unique_name(std::string &str, std::set<std::string>&namelist,int&clash_index);
106 
109  static void Split (const char *s, int len, char sep,
110  std::vector < std::string > &names);
111 
113  static void Split (const char *sz, char sep,
114  std::vector < std::string > &names);
115 
116  static void Split_helper(std::vector<std::string>&tokens, const std::string &text,const char sep);
117 
118  // Parse GPM Level 3 GridHeaders
119  static void parser_gpm_l3_gridheader(const std:: vector<char>&value, int& latsize, int&lonsize,
120  float& lat_start, float& lon_start, float& lat_res, float& lon_res, bool check_reg_orig);
121 
122  static void close_fileid(hid_t,bool);
123 
124  // wrap function of Unix read to a buffer. Memory for the buffer should be allocated.
125  static ssize_t read_buffer_from_file(int fd,void*buf,size_t);
126  static std::string obtain_cache_fname(const std::string & fprefix, const std::string & fname, const std::string &vname);
127 
128  //Use to generate cache file name.
129  // Reverse the char array order
130  static void rev_str(char *str, int len);
131 
132  // Return the index of the char array for the integer part
133  static int int_to_str(int,char str[],int);
134 
135  // Convert a double number to char array
136  static void dtoa(double,char *,int);
137 
138  // convert double to string
139  static std::string get_double_str(double x,int total_digit,int after_point);
140 
141  // convert int to string
142  static std::string get_int_str(int x);
143 
144 
145 #if 0
146  //static int GDij2ll(int projcode, int zonecode, double projparm[],
147  // int spherecode, int xdimsize, int ydimsize,
148  // double upleftpt[], double lowrightpt[],
149  // int npnts, int row[], int col[],
150  // double longitude[], double latitude[], int pixcen, int pixcnr);
151 
152  //static size_t INDEX_nD_TO_1D (const std::vector < size_t > &dims,
153  // const std::vector < size_t > &pos);
154 #endif
155 #if 0
156  template<typename T> int subset(
157  const T input[],
158  int rank,
159  vector<int> & dim,
160  int start[],
161  int stride[],
162  int edge[],
163  std::vector<T> *poutput,
164  vector<int>& pos,
165  int index);
166 #endif
167 } ;
168 
169 static inline struct flock *lock(int type) {
170  static struct flock lock;
171  lock.l_type = type;
172  lock.l_whence = SEEK_SET;
173  lock.l_start = 0;
174  lock.l_len = 0;
175  lock.l_pid = getpid();
176 
177  return &lock;
178 }
179 
180 static inline std::string get_errno() {
181  char *s_err = strerror(errno);
182  if (s_err)
183  return s_err;
184  else
185  return "Unknown error.";
186 }
187 
188 
189 
190 #if 0
191 {
192  //
193  // int a[10][20][30]; // & a[1][2][3] == a + (20*30+1 + 30*2 + 1 *3);
194  // int b[10][2]; // &b[1][2] == b + (20*1 + 2);
195  //
196  if(dims.size () != pos.size ())
197  throw InternalErr(__FILE__,__LINE__,"dimension error in INDEX_nD_TO_1D routine.");
198  size_t sum = 0;
199  size_t start = 1;
200 
201  for (size_t p = 0; p < pos.size (); p++) {
202  size_t m = 1;
203 
204  for (size_t j = start; j < dims.size (); j++)
205  m *= dims[j];
206  sum += m * pos[p];
207  start++;
208  }
209  return sum;
210 }
211 #endif
212  int GDij2ll(int projcode, int zonecode, double projparm[],
213  int spherecode, int xdimsize, int ydimsize,
214  double upleftpt[], double lowrightpt[],
215  int npnts, int row[], int col[],
216  double longitude[], double latitude[], EOS5GridPRType pixcen, EOS5GridOriginType pixcnr);
217 #if 0
218 //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*));
219 
220 //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 *));
221 #endif
222  double HE5_EHconvAng(double inAngle, int code);
223 #endif
HDF5CFUtil::H5type_to_H5DAPtype
static H5DataType H5type_to_H5DAPtype(hid_t h5_type_id)
Map HDF5 Datatype to the intermediate H5DAPtype for the future use.
Definition: HDF5CFUtil.cc:54
HDF5CFUtil
Definition: HDF5CFUtil.h:86
HDF5CFUtil::read_buffer_from_file
static ssize_t read_buffer_from_file(int fd, void *buf, size_t)
Getting a subset of a variable.
Definition: HDF5CFUtil.cc:1181
HDF5CFUtil::Split
static void Split(const char *s, int len, char sep, std::vector< std::string > &names)
Definition: HDF5CFUtil.cc:329
Name_Size_2Pairs
Definition: HDF5CFUtil.h:78
HDF5CFUtil::trim_string
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)
Definition: HDF5CFUtil.cc:223