bes Updated for version 3.20.13
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.
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 static void cha_co(std::string &co,const std::string & vpath);
102 static void get_relpath_pos(const std::string& temp_str,const std::string& relpath,std::vector<size_t>&var_pos);
103
104 static bool cf_strict_support_type(H5DataType dtype,bool is_dap4);
105 static bool cf_dap2_support_numeric_type(H5DataType dtype,bool is_dap4);
106
107 // Obtain the unique name for the clashed names and save it to set namelist.
108 static void gen_unique_name(std::string &str, std::set<std::string>&namelist,int&clash_index);
109
112 static void Split (const char *s, int len, char sep,
113 std::vector < std::string > &names);
114
116 static void Split (const char *sz, char sep,
117 std::vector < std::string > &names);
118
119 static void Split_helper(std::vector<std::string>&tokens, const std::string &text,const char sep);
120
121 // Parse GPM Level 3 GridHeaders
122 static void parser_gpm_l3_gridheader(const std:: vector<char>&value, int& latsize, int&lonsize,
123 float& lat_start, float& lon_start, float& lat_res, float& lon_res, bool check_reg_orig);
124
125 static void close_fileid(hid_t,bool);
126
127 // wrap function of Unix read to a buffer. Memory for the buffer should be allocated.
128 static ssize_t read_buffer_from_file(int fd,void*buf,size_t);
129 static std::string obtain_cache_fname(const std::string & fprefix, const std::string & fname, const std::string &vname);
130
131 //Use to generate cache file name.
132 // Reverse the char array order
133 static void rev_str(char *str, int len);
134
135 // Return the index of the char array for the integer part
136 static int int_to_str(int,char str[],int);
137
138 // Convert a double number to char array
139 static void dtoa(double,char *,int);
140
141 // convert double to string
142 static std::string get_double_str(double x,int total_digit,int after_point);
143
144 // convert int to string
145 static std::string get_int_str(int x);
146
147
148#if 0
149 //static int GDij2ll(int projcode, int zonecode, double projparm[],
150 // int spherecode, int xdimsize, int ydimsize,
151 // double upleftpt[], double lowrightpt[],
152 // int npnts, int row[], int col[],
153 // double longitude[], double latitude[], int pixcen, int pixcnr);
154
155 //static size_t INDEX_nD_TO_1D (const std::vector < size_t > &dims,
156 // const std::vector < size_t > &pos);
157#endif
158#if 0
159 template<typename T> int subset(
160 const T input[],
161 int rank,
162 vector<int> & dim,
163 int start[],
164 int stride[],
165 int edge[],
166 std::vector<T> *poutput,
167 vector<int>& pos,
168 int index);
169#endif
170} ;
171
172static inline struct flock *lock(int type) {
173 static struct flock lock;
174 lock.l_type = (short)type;
175 lock.l_whence = SEEK_SET;
176 lock.l_start = 0;
177 lock.l_len = 0;
178 lock.l_pid = getpid();
179
180 return &lock;
181}
182
183static inline std::string get_errno() {
184 const char *s_err = strerror(errno);
185 if (s_err)
186 return s_err;
187 else
188 return "Unknown error.";
189}
190
191
192
193#if 0
194{
195 //
196 // int a[10][20][30]; // & a[1][2][3] == a + (20*30+1 + 30*2 + 1 *3);
197 // int b[10][2]; // &b[1][2] == b + (20*1 + 2);
198 //
199 if(dims.size () != pos.size ())
200 throw InternalErr(__FILE__,__LINE__,"dimension error in INDEX_nD_TO_1D routine.");
201 size_t sum = 0;
202 size_t start = 1;
203
204 for (size_t p = 0; p < pos.size (); p++) {
205 size_t m = 1;
206
207 for (size_t j = start; j < dims.size (); j++)
208 m *= dims[j];
209 sum += m * pos[p];
210 start++;
211 }
212 return sum;
213}
214#endif
215 int GDij2ll(int projcode, int zonecode, double projparm[],
216 int spherecode, int xdimsize, int ydimsize,
217 double upleftpt[], double lowrightpt[],
218 int npnts, int row[], int col[],
219 double longitude[], double latitude[], EOS5GridPRType pixcen, EOS5GridOriginType pixcnr);
220#if 0
221//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*));
222
223//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 *));
224#endif
225 double HE5_EHconvAng(double inAngle, int code);
226#endif
static void Split(const char *s, int len, char sep, std::vector< std::string > &names)
Definition: HDF5CFUtil.cc:343
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
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:235
static ssize_t read_buffer_from_file(int fd, void *buf, size_t)
Getting a subset of a variable.
Definition: HDF5CFUtil.cc:1194