OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
HDFCFUtil.h
Go to the documentation of this file.
1 // Helper functions for handling dimension maps,clear memories and separating namelist
3 //
4 // Authors: MuQun Yang <myang6@hdfgroup.org> Eunsoo Seo
5 // Copyright (c) 2010-2012 The HDF Group
7 #ifndef HDFCFUTIL_H
8 #define HDFCFUTIL_H
9 
10 #include <stdlib.h>
11 #include <string>
12 #include <iostream>
13 #include <sstream>
14 #include <vector>
15 #include <map>
16 #include <set>
17 #include <assert.h>
18 #include <iomanip>
19 #include <dirent.h>
20 #include <libgen.h>
21 #include <unistd.h>
22 
23 #include "mfhdf.h"
24 #include "hdf.h"
25 
26 #ifdef USE_HDFEOS2_LIB
27 #include "HdfEosDef.h"
28 #include "HDFEOS2.h"
29 #endif
30 #include "HDFSP.h"
31 
32 #include <TheBESKeys.h>
33 #include <BESUtil.h>
34 #include <DDS.h>
35 #include <DAS.h>
36 #include <InternalErr.h>
37 
38 #include "escaping.h" // for escattr
39 
40 // This is the maximum number of MODIS special values.
41 #define MAX_NON_SCALE_SPECIAL_VALUE 65535
42 
43 // This is the minimum number of MODIS special values.
44 #define MIN_NON_SCALE_SPECIAL_VALUE 65500
45 
46 
47 // This is used to retrieve dimension map info. when retrieving the data values of HDF-EOS swath that
48 // has swath dimension maps.
50 {
51  // geo dimension name
52  std::string geodim;
53 
54  // data dimension name
55  std::string datadim;
56 
57  // offset and increment of a dimension map
58  int32 offset, inc;
59 };
60 
61 
62 struct HDFCFUtil
63 {
64 
67  static void close_fileid(int32 sdfd,int32 file_id,int32 gridfd,int32 swathfd,bool pass_fileid_key);
68 
69  //static void reset_fileid(int& sdfd, int&file_id,int&gridfd,int&swathfd);
70 
77  static std::string escattr(std::string s);
78 
85 
86  static bool check_beskeys(const std::string& key);
87 
90  static void Split (const char *s, int len, char sep,
91  std::vector < std::string > &names);
92 
94  static void Split (const char *sz, char sep,
95  std::vector < std::string > &names);
96 
100  static bool insert_map(std::map<std::string,std::string>& m, std::string key, std::string val);
101 
103  static std::string get_CF_string(std::string s);
104 
106  static void gen_unique_name(std::string &str,std::set<std::string>& namelist, int&clash_index);
107 
109  static void Handle_NameClashing(std::vector<std::string>&newobjnamelist);
110  static void Handle_NameClashing(std::vector<std::string>&newobjnamelist,std::set<std::string>&objnameset);
111 
113  static std::string print_attr(int32, int, void*);
114 
116  static std::string print_type(int32);
117 
118  // Subsetting the 2-D fields
119  template <typename T> static void LatLon2DSubset (T* outlatlon, int ydim, int xdim, T* latlon, int32 * offset, int32 * count, int32 * step);
120 
123  static void correct_fvalue_type(libdap::AttrTable *at,int32 dtype);
124 
125 
129  static void correct_scale_offset_type(libdap::AttrTable *at);
130 
131 
132 #ifdef USE_HDFEOS2_LIB
133 
136 
145  static bool change_data_type(libdap::DAS & das, SOType scaletype, const std::string & new_field_name);
146 
151  static bool is_special_value(int32 dtype,float fillvalue, float realvalue);
152 
154  static int check_geofile_dimmap(const std::string & geofilename);
155 
159  static bool is_modis_dimmap_nonll_field(std::string & fieldname);
160 
162  static void obtain_dimmap_info(const std::string& filename, HDFEOS2::Dataset*dataset,std::vector<struct dimmap_entry>& dimmaps, std::string & modis_geofilename,bool &geofile_nas_dimmap);
163 
165  static void handle_modis_special_attrs_disable_scale_comp(libdap::AttrTable *at,const string &filename, bool is_grid, const std::string &newfname, SOType scaletype);
166 
167 
171  static void handle_modis_special_attrs(libdap::AttrTable *at,const std::string &filename, bool is_grid, const std::string & newfname, SOType scaletype, bool gridname_change_valid_range, bool changedtype, bool &change_fvtype);
172 
174  static void handle_modis_vip_special_attrs(const std::string& valid_range_value,const std::string& scale_factor_value, float& valid_min, float & valid_max);
175 
177  static void handle_amsr_attrs(libdap::AttrTable *at);
178 #endif
179 
180  // Check OBPG attributes. Specifically, check if slope and intercept can be obtained from the file level.
181  // If having global slope and intercept, obtain OBPG scaling, slope and intercept values.
182  static void check_obpg_global_attrs(HDFSP::File*f,std::string & scaling, float & slope,bool &global_slope_flag,float & intercept, bool & global_intercept_flag);
183 
184  // For some OBPG files that only provide slope and intercept at the file level,
185  // global slope and intercept are needed to add to all fields and their names are needed to be changed to scale_factor and add_offset.
186  // For OBPG files that provide slope and intercept at the field level, slope and intercept are needed to rename to scale_factor and add_offset.
187  static void add_obpg_special_attrs(HDFSP::File*f,libdap::DAS &das, HDFSP::SDField* spsds,std::string & scaling, float&slope,bool &global_slope_flag,float& intercept,bool &global_intercept_flag);
188 
189  // Handle HDF4 OTHERHDF products that follow SDS dimension scale model.
190  // The special handling of AVHRR data is also included.
191  static void handle_otherhdf_special_attrs(HDFSP::File *f, libdap::DAS &das);
192 
193  // Add missing CF attributes for non-CV varibles
194  static void add_missing_cf_attrs(HDFSP::File*f,libdap::DAS &das);
195 
196  // Handle Merra and CERES attributes with the BES key EnableCERESMERRAShortName.
197  static void handle_merra_ceres_attrs_with_bes_keys(HDFSP::File*f, libdap::DAS &das,const std::string& filename);
198 
199  // Handle the attributes with the BES key EnableVdataDescAttr.
200  static void handle_vdata_attrs_with_desc_key(HDFSP::File*f,libdap::DAS &das);
201 
202  // Parse TRMM V7 GridHeaders
203  //static void parser_trmm_v7_gridheader(int& latsize, int&lonsize, float& lat_start, float& lon_start, bool &sw_origin, bool & cr_reg);
204  static void parser_trmm_v7_gridheader(const std:: vector<char>&value, int& latsize, int&lonsize, float& lat_start, float& lon_start, float& lat_res, float& lon_res, bool check_reg_orig);
205 
206  // Use to generate cache file name.
207  // Reverse the char array order
208  static void rev_str(char *str, int len);
209 
210  // Return the index of the char array for the integer part
211  static int int_to_str(int,char str[],int);
212 
213  // Convert a double number to char array
214  static void dtoa(double,char *,int);
215 
216  // Obtain the double number in the string format
217  static std::string get_double_str(double,int,int);
218 
219  // Obtain the integer in the string format
220  static std::string get_int_str(int);
221 
222  //template<typename T> static size_t write_vector_to_file(const std::string &,const vector<T> &,size_t);
223 
224 #if 0
225  static size_t write_vector_to_file(const std::string &,const vector<double> &,size_t);
226  static ssize_t write_vector_to_file2(const std::string &,const vector<double> &,size_t);
227 #endif
228  // Read double-type data from a file to a vector of double type
229  static ssize_t read_vector_from_file(int fd,vector<double> &,size_t);
230 
231 };
232 
234 inline int32
235 INDEX_nD_TO_1D (const std::vector < int32 > &dims,
236  const std::vector < int32 > &pos)
237 {
238  /*
239  int a[10][20][30]; // & a[1][2][3] == a + (20*30+1 + 30*2 + 1 *3);
240  int b[10][2]; // &b[1][2] == b + (20*1 + 2);
241  */
242  assert (dims.size () == pos.size ());
243  int32 sum = 0;
244  int32 start = 1;
245 
246  for (unsigned int p = 0; p < pos.size (); p++) {
247  int32 m = 1;
248 
249  for (unsigned int j = start; j < dims.size (); j++)
250  m *= dims[j];
251  sum += m * pos[p];
252  start++;
253  }
254  return sum;
255 }
256 
257 
258 #endif
SOType
static void add_missing_cf_attrs(HDFSP::File *f, libdap::DAS &das)
Definition: HDFCFUtil.cc:1671
int32 inc
Definition: HDFCFUtil.h:58
static void parser_trmm_v7_gridheader(const std::vector< char > &value, int &latsize, int &lonsize, float &lat_start, float &lon_start, float &lat_res, float &lon_res, bool check_reg_orig)
Definition: HDFCFUtil.cc:2534
static bool insert_map(std::map< std::string, std::string > &m, std::string key, std::string val)
This is a safer way to insert and update a c++ map value. Otherwise, the local testsuite at The HDF G...
Definition: HDFCFUtil.cc:64
Definition: HDFCFUtil.h:49
static void check_obpg_global_attrs(HDFSP::File *f, std::string &scaling, float &slope, bool &global_slope_flag, float &intercept, bool &global_intercept_flag)
Definition: HDFCFUtil.cc:1384
This class retrieves all information from an HDF4 file. It is a container for SDS and Vdata...
Definition: HDFSP.h:727
static void close_fileid(int32 sdfd, int32 file_id, int32 gridfd, int32 swathfd, bool pass_fileid_key)
Close HDF4 and HDF-EOS2 file IDs. For performance reasons, we want to keep HDF-EOS2/HDF4 IDs open for...
Definition: HDFCFUtil.cc:2881
static void handle_otherhdf_special_attrs(HDFSP::File *f, libdap::DAS &das)
Definition: HDFCFUtil.cc:1577
#define assert(exp)
Definition: freeform.h:735
static void handle_merra_ceres_attrs_with_bes_keys(HDFSP::File *f, libdap::DAS &das, const std::string &filename)
Definition: HDFCFUtil.cc:2246
static void handle_vdata_attrs_with_desc_key(HDFSP::File *f, libdap::DAS &das)
Definition: HDFCFUtil.cc:2295
static void Handle_NameClashing(std::vector< std::string > &newobjnamelist)
General routines to handle name clashings.
Definition: HDFCFUtil.cc:176
static void correct_fvalue_type(libdap::AttrTable *at, int32 dtype)
CF requires the _FillValue attribute datatype is the same as the corresponding field datatype...
Definition: HDFCFUtil.cc:395
static std::string get_int_str(int)
Definition: HDFCFUtil.cc:2803
static std::string get_double_str(double, int, int)
Definition: HDFCFUtil.cc:2770
One instance of this class represents one SDS object.
Definition: HDFSP.h:344
static void gen_unique_name(std::string &str, std::set< std::string > &namelist, int &clash_index)
Obtain the unique name for the clashed names and save it to set namelist.
Definition: HDFCFUtil.cc:110
static void dtoa(double, char *, int)
Definition: HDFCFUtil.cc:2739
static std::string print_type(int32)
Print datatype in string.
Definition: HDFCFUtil.cc:290
int32 INDEX_nD_TO_1D(const std::vector< int32 > &dims, const std::vector< int32 > &pos)
This inline routine will translate N dimensions into 1 dimension.
Definition: HDFCFUtil.h:235
static int int_to_str(int, char str[], int)
Definition: HDFCFUtil.cc:2719
static void add_obpg_special_attrs(HDFSP::File *f, libdap::DAS &das, HDFSP::SDField *spsds, std::string &scaling, float &slope, bool &global_slope_flag, float &intercept, bool &global_intercept_flag)
Definition: HDFCFUtil.cc:1452
static void Split(const char *s, int len, char sep, std::vector< std::string > &names)
From a string separated by a separator to a list of string, for example, split "ab,c" to {"ab","c"}.
Definition: HDFCFUtil.cc:38
std::string geodim
Definition: HDFCFUtil.h:52
static std::string escattr(std::string s)
A customized escaping function to escape special characters following OPeNDAP's escattr function that...
Definition: HDFCFUtil.cc:2502
static std::string print_attr(int32, int, void *)
Print attribute values in string.
Definition: HDFCFUtil.cc:184
static void rev_str(char *str, int len)
Definition: HDFCFUtil.cc:2701
static ssize_t read_vector_from_file(int fd, vector< double > &, size_t)
Definition: HDFCFUtil.cc:2863
int32 offset
Definition: HDFCFUtil.h:58
static std::string get_CF_string(std::string s)
Change special characters to "_".
Definition: HDFCFUtil.cc:80
static bool check_beskeys(const std::string &key)
Check the BES key. This function will check a BES key specified at the file h4.conf.in. If the key's value is either true or yes. The handler claims to find a key and will do some operations. Otherwise, will do different operations. For example, One may find a line H4.EnableCF=true at h4.conf.in. That means, the HDF4 handler will handle the HDF4 files by following CF conventions.
Definition: HDFCFUtil.cc:17
static void LatLon2DSubset(T *outlatlon, int ydim, int xdim, T *latlon, int32 *offset, int32 *count, int32 *step)
Definition: HDFCFUtil.cc:353
static void correct_scale_offset_type(libdap::AttrTable *at)
CF requires the scale_factor and add_offset attribute datatypes hold the same datatype. So far we haven't found that scale_factor and add_offset attributes hold different datatypes in NASA files. But just in case, we implement a BES key to give users a chance to check this. By default, the key is always off.
Definition: HDFCFUtil.cc:462
std::string datadim
Definition: HDFCFUtil.h:55