htcp.h (5491B)
1 /* Copyright (C) 2018, 2020-2023, 2025, 2025 |Méso|Star> (contact@meso-star.com) 2 * Copyright (C) 2018 Centre National de la Recherche Scientifique 3 * Copyright (C) 2018 Université Paul Sabatier 4 * 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18 #ifndef HTCP_H 19 #define HTCP_H 20 21 #include <rsys/rsys.h> 22 23 /* Library symbol management */ 24 #if defined(HTCP_SHARED_BUILD) /* Build shared library */ 25 #define HTCP_API extern EXPORT_SYM 26 #elif defined(HTCP_STATIC) /* Use/build static library */ 27 #define HTCP_API extern LOCAL_SYM 28 #else /* Use shared library */ 29 #define HTCP_API extern IMPORT_SYM 30 #endif 31 32 /* Helper macro that asserts if the invocation of the htcp function `Func' 33 * returns an error. One should use this macro on htcp function calls for 34 * which no explicit error checking is performed */ 35 #ifndef NDEBUG 36 #define HTCP(Func) ASSERT(htcp_ ## Func == RES_OK) 37 #else 38 #define HTCP(Func) htcp_ ## Func 39 #endif 40 41 /* Forward declaration of external data types */ 42 struct logger; 43 struct mem_allocator; 44 45 /* Forward declaration of opaque data types */ 46 struct htcp; 47 48 struct htcp_desc { 49 size_t pagesize; /* In bytes */ 50 int irregular_z; 51 52 /* #coordinates along the X, Y and Z axis. X means for the West-Est axis, Y 53 * is the South-North axis and finally Z is the altitude */ 54 size_t spatial_definition[3]; 55 size_t time_definition; /* Definition of the time */ 56 57 double lower[3]; /* Lower position of the grid in meters */ 58 double upper[3]; /* Upper position of the grid in meters */ 59 double vxsz_x; /* Voxel size in X in meters */ 60 double vxsz_y; /* Voxel size in Y in meters */ 61 const double* vxsz_z; /* Voxel size along Z in meters */ 62 const double* coord_z; /* Voxel lower bound along Z. NULL if !irregular_z */ 63 64 const double* RCT; 65 const double* RVT; 66 const double* PABST; /* Pressure */ 67 const double* T; /* Temperature */ 68 }; 69 #define HTCP_DESC_NULL__ \ 70 {0,-1,{0,0,0},0,{-1,-1,-1},{0,0,0},-1,-1,NULL,NULL,NULL,NULL,NULL,NULL} 71 static const struct htcp_desc HTCP_DESC_NULL = HTCP_DESC_NULL__; 72 73 BEGIN_DECLS 74 75 /******************************************************************************* 76 * HTCP API 77 ******************************************************************************/ 78 HTCP_API res_T 79 htcp_create 80 (struct logger* logger, /* NULL <=> use default logger */ 81 struct mem_allocator* allocator, /* NULL <=> use default allocator */ 82 const int verbose, /* Verbosity level */ 83 struct htcp** htcp); 84 85 HTCP_API res_T 86 htcp_load 87 (struct htcp* htcp, 88 const char* path); 89 90 HTCP_API res_T 91 htcp_load_stream 92 (struct htcp* htcp, 93 FILE* stream); 94 95 HTCP_API res_T 96 htcp_ref_get 97 (struct htcp* htcp); 98 99 HTCP_API res_T 100 htcp_ref_put 101 (struct htcp* htcp); 102 103 HTCP_API res_T 104 htcp_get_desc 105 (const struct htcp* htcp, 106 struct htcp_desc* desc); 107 108 /* Internal function */ 109 static FINLINE double 110 htcp_dblgrid4D_at__ 111 (const double* grid, 112 const struct htcp_desc* desc, 113 const size_t x, const size_t y, const size_t z, const size_t t) 114 { 115 size_t row, slice, array; 116 ASSERT(desc && grid); 117 ASSERT(x < desc->spatial_definition[0]); 118 ASSERT(y < desc->spatial_definition[1]); 119 ASSERT(z < desc->spatial_definition[2]); 120 ASSERT(t < desc->time_definition); 121 row = desc->spatial_definition[0]; 122 slice = desc->spatial_definition[1] * row; 123 array = desc->spatial_definition[2] * slice; 124 return grid[t*array + z*slice + y*row + x]; 125 } 126 127 static FINLINE double 128 htcp_desc_RCT_at 129 (const struct htcp_desc* desc, 130 const size_t x, const size_t y, const size_t z, const size_t t) 131 { 132 return htcp_dblgrid4D_at__(desc->RCT, desc, x, y, z, t); 133 } 134 135 136 static FINLINE double 137 htcp_desc_RVT_at 138 (const struct htcp_desc* desc, 139 const size_t x, const size_t y, const size_t z, const size_t t) 140 { 141 return htcp_dblgrid4D_at__(desc->RVT, desc, x, y, z, t); 142 } 143 144 static FINLINE double 145 htcp_desc_PABST_at 146 (const struct htcp_desc* desc, 147 const size_t x, const size_t y, const size_t z, const size_t t) 148 { 149 return htcp_dblgrid4D_at__(desc->PABST, desc, x, y, z, t); 150 } 151 152 static FINLINE double 153 htcp_desc_T_at 154 (const struct htcp_desc* desc, 155 const size_t x, const size_t y, const size_t z, const size_t t) 156 { 157 return htcp_dblgrid4D_at__(desc->T, desc, x, y, z, t); 158 } 159 160 static FINLINE void 161 htcp_desc_get_voxel_aabb 162 (const struct htcp_desc* desc, 163 const size_t x, const size_t y, const size_t z, 164 double lower[3], 165 double upper[3]) 166 { 167 ASSERT(desc && lower && upper); 168 ASSERT(x < desc->spatial_definition[0]); 169 ASSERT(y < desc->spatial_definition[1]); 170 ASSERT(z < desc->spatial_definition[2]); 171 lower[0] = (double)x * desc->vxsz_x; 172 lower[1] = (double)y * desc->vxsz_y; 173 upper[0] = lower[0] + desc->vxsz_x; 174 upper[1] = lower[1] + desc->vxsz_y; 175 if(!desc->irregular_z) { 176 lower[2] = (double)z * desc->vxsz_z[0]; 177 upper[2] = lower[2] + desc->vxsz_z[0]; 178 } else { 179 lower[2] = desc->coord_z[z]; 180 upper[2] = lower[2] + desc->vxsz_z[z]; 181 } 182 } 183 184 END_DECLS 185 186 #endif /* HTCP_H */