1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-22 20:35:44 +01:00

modify front end for more general numerical attributes

This commit is contained in:
q-posev 2021-09-20 13:16:44 +02:00
parent 418aa304cd
commit 261e7c8b84
2 changed files with 104 additions and 74 deletions

View File

@ -1135,70 +1135,39 @@ def close(trexio_file):
(non-suffixed) API call on dimensioning variables deals with single (non-suffixed) API call on dimensioning variables deals with single
precision (see Table above). precision (see Table above).
**** Function declarations
#+begin_src c :tangle hrw_num_front.h :exports none #+begin_src c :tangle hrw_num_front.h :exports none
trexio_exit_code trexio_has_$group_num$(trexio_t* const file); trexio_exit_code trexio_has_$group_num$(trexio_t* const file);
trexio_exit_code trexio_read_$group_num$(trexio_t* const file, int32_t* const num); trexio_exit_code trexio_read_$group_num$(trexio_t* const file, $group_num_dtype_default$* const num);
trexio_exit_code trexio_write_$group_num$(trexio_t* const file, const int32_t num); trexio_exit_code trexio_write_$group_num$(trexio_t* const file, const $group_num_dtype_default$ num);
trexio_exit_code trexio_read_$group_num$_32(trexio_t* const file, int32_t* const num); trexio_exit_code trexio_read_$group_num$_32(trexio_t* const file, $group_num_dtype_single$* const num);
trexio_exit_code trexio_write_$group_num$_32(trexio_t* const file, const int32_t num); trexio_exit_code trexio_write_$group_num$_32(trexio_t* const file, const $group_num_dtype_single$ num);
trexio_exit_code trexio_read_$group_num$_64(trexio_t* const file, int64_t* const num); trexio_exit_code trexio_read_$group_num$_64(trexio_t* const file, $group_num_dtype_double$* const num);
trexio_exit_code trexio_write_$group_num$_64(trexio_t* const file, const int64_t num); trexio_exit_code trexio_write_$group_num$_64(trexio_t* const file, const $group_num_dtype_double$ num);
#+end_src #+end_src
**** Source code for double precision functions
#+begin_src c :tangle read_num_64_front.c #+begin_src c :tangle read_num_64_front.c
trexio_exit_code trexio_exit_code
trexio_read_$group_num$_64 (trexio_t* const file, int64_t* const num) trexio_read_$group_num$_64 (trexio_t* const file, $group_num_dtype_double$* const num)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
if (trexio_has_$group_num$(file) != TREXIO_SUCCESS) return TREXIO_ATTR_MISSING; if (trexio_has_$group_num$(file) != TREXIO_SUCCESS) return TREXIO_ATTR_MISSING;
uint64_t u_num = 0;
trexio_exit_code rc = TREXIO_GROUP_READ_ERROR;
switch (file->back_end) { switch (file->back_end) {
case TREXIO_TEXT: case TREXIO_TEXT:
rc = trexio_text_read_$group_num$(file, &u_num); return trexio_text_read_$group_num$(file, num);
break; break;
case TREXIO_HDF5: case TREXIO_HDF5:
rc = trexio_hdf5_read_$group_num$(file, &u_num); return trexio_hdf5_read_$group_num$(file, num);
break; break;
/* /*
case TREXIO_JSON: case TREXIO_JSON:
rc =trexio_json_read_$group_num$(file, &u_num); return trexio_json_read_$group_num$(file, num);
break;
,*/
}
if (rc != TREXIO_SUCCESS) return rc;
*num = (int64_t) u_num;
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle write_num_64_front.c
trexio_exit_code
trexio_write_$group_num$_64 (trexio_t* const file, const int64_t num)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num < 0 ) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS;
switch (file->back_end) {
case TREXIO_TEXT:
return trexio_text_write_$group_num$(file, (int64_t) num);
break;
case TREXIO_HDF5:
return trexio_hdf5_write_$group_num$(file, (int64_t) num);
break;
/*
case TREXIO_JSON:
return trexio_json_write_$group_num$(file, (int64_t) num);
break; break;
,*/ ,*/
} }
@ -1207,60 +1176,89 @@ trexio_write_$group_num$_64 (trexio_t* const file, const int64_t num)
} }
#+end_src #+end_src
#+begin_src c :tangle write_num_64_front.c
trexio_exit_code
trexio_write_$group_num$_64 (trexio_t* const file, const $group_num_dtype_double$ num)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS;
switch (file->back_end) {
case TREXIO_TEXT:
return trexio_text_write_$group_num$(file, num);
break;
case TREXIO_HDF5:
return trexio_hdf5_write_$group_num$(file, num);
break;
/*
case TREXIO_JSON:
return trexio_json_write_$group_num$(file, num);
break;
,*/
}
return TREXIO_FAILURE;
}
#+end_src
**** Source code for single precision functions
#+begin_src c :tangle read_num_32_front.c #+begin_src c :tangle read_num_32_front.c
trexio_exit_code trexio_exit_code
trexio_read_$group_num$_32 (trexio_t* const file, int32_t* const num) trexio_read_$group_num$_32 (trexio_t* const file, $group_num_dtype_single$* const num)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
if (trexio_has_$group_num$(file) != TREXIO_SUCCESS) return TREXIO_ATTR_MISSING; if (trexio_has_$group_num$(file) != TREXIO_SUCCESS) return TREXIO_ATTR_MISSING;
uint64_t u_num = 0; $group_num_dtype_double$ num_64 = 0;
trexio_exit_code rc = TREXIO_GROUP_READ_ERROR; trexio_exit_code rc = TREXIO_GROUP_READ_ERROR;
switch (file->back_end) { switch (file->back_end) {
case TREXIO_TEXT: case TREXIO_TEXT:
rc = trexio_text_read_$group_num$(file, &u_num); rc = trexio_text_read_$group_num$(file, &num_64);
break; break;
case TREXIO_HDF5: case TREXIO_HDF5:
rc = trexio_hdf5_read_$group_num$(file, &u_num); rc = trexio_hdf5_read_$group_num$(file, &num_64);
break; break;
/* /*
case TREXIO_JSON: case TREXIO_JSON:
rc =trexio_json_read_$group_num$(file, &u_num); rc =trexio_json_read_$group_num$(file, &num_64);
break; break;
,*/ ,*/
} }
if (rc != TREXIO_SUCCESS) return rc; if (rc != TREXIO_SUCCESS) return rc;
*num = (int32_t) u_num; *num = ($group_num_dtype_single$) num_64;
return TREXIO_SUCCESS; return TREXIO_SUCCESS;
} }
#+end_src #+end_src
#+begin_src c :tangle write_num_32_front.c #+begin_src c :tangle write_num_32_front.c
trexio_exit_code trexio_exit_code
trexio_write_$group_num$_32 (trexio_t* const file, const int32_t num) trexio_write_$group_num$_32 (trexio_t* const file, const $group_num_dtype_single$ num)
{ {
if (file == NULL) return TREXIO_INVALID_ARG_1; if (file == NULL) return TREXIO_INVALID_ARG_1;
if (num < 0 ) return TREXIO_INVALID_ARG_2; //if (num < 0 ) return TREXIO_INVALID_ARG_2;
if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS; if (trexio_has_$group_num$(file) == TREXIO_SUCCESS) return TREXIO_ATTR_ALREADY_EXISTS;
switch (file->back_end) { switch (file->back_end) {
case TREXIO_TEXT: case TREXIO_TEXT:
return trexio_text_write_$group_num$(file, (int64_t) num); return trexio_text_write_$group_num$(file, ($group_num_dtype_double$) num);
break; break;
case TREXIO_HDF5: case TREXIO_HDF5:
return trexio_hdf5_write_$group_num$(file, (int64_t) num); return trexio_hdf5_write_$group_num$(file, ($group_num_dtype_double$) num);
break; break;
/* /*
case TREXIO_JSON: case TREXIO_JSON:
return trexio_json_write_$group_num$(file, (int64_t) num); return trexio_json_write_$group_num$(file, ($group_num_dtype_double$) num);
break; break;
,*/ ,*/
} }
@ -1269,19 +1267,21 @@ trexio_write_$group_num$_32 (trexio_t* const file, const int32_t num)
} }
#+end_src #+end_src
**** Source code for default functions
#+begin_src c :tangle read_num_def_front.c #+begin_src c :tangle read_num_def_front.c
trexio_exit_code trexio_exit_code
trexio_read_$group_num$ (trexio_t* const file, int32_t* const num) trexio_read_$group_num$ (trexio_t* const file, $group_num_dtype_default$ * const num)
{ {
return trexio_read_$group_num$_32(file, num); return trexio_read_$group_num$_$default_prec$(file, num);
} }
#+end_src #+end_src
#+begin_src c :tangle write_num_def_front.c #+begin_src c :tangle write_num_def_front.c
trexio_exit_code trexio_exit_code
trexio_write_$group_num$ (trexio_t* const file, const int32_t num) trexio_write_$group_num$ (trexio_t* const file, const $group_num_dtype_default$ num)
{ {
return trexio_write_$group_num$_32(file, num); return trexio_write_$group_num$_$default_prec$(file, num);
} }
#+end_src #+end_src
@ -1324,7 +1324,7 @@ interface
integer function trexio_write_$group_num$_64 (trex_file, num) bind(C) integer function trexio_write_$group_num$_64 (trex_file, num) bind(C)
use, intrinsic :: iso_c_binding use, intrinsic :: iso_c_binding
integer(8), intent(in), value :: trex_file integer(8), intent(in), value :: trex_file
integer(8), intent(in), value :: num $group_num_f_dtype_double$, intent(in), value :: num
end function trexio_write_$group_num$_64 end function trexio_write_$group_num$_64
end interface end interface
#+end_src #+end_src
@ -1334,7 +1334,7 @@ interface
integer function trexio_read_$group_num$_64 (trex_file, num) bind(C) integer function trexio_read_$group_num$_64 (trex_file, num) bind(C)
use, intrinsic :: iso_c_binding use, intrinsic :: iso_c_binding
integer(8), intent(in), value :: trex_file integer(8), intent(in), value :: trex_file
integer(8), intent(out) :: num $group_num_f_dtype_double$, intent(out) :: num
end function trexio_read_$group_num$_64 end function trexio_read_$group_num$_64
end interface end interface
#+end_src #+end_src
@ -1344,7 +1344,7 @@ interface
integer function trexio_write_$group_num$_32 (trex_file, num) bind(C) integer function trexio_write_$group_num$_32 (trex_file, num) bind(C)
use, intrinsic :: iso_c_binding use, intrinsic :: iso_c_binding
integer(8), intent(in), value :: trex_file integer(8), intent(in), value :: trex_file
integer(4), intent(in), value :: num $group_num_f_dtype_single$, intent(in), value :: num
end function trexio_write_$group_num$_32 end function trexio_write_$group_num$_32
end interface end interface
#+end_src #+end_src
@ -1354,7 +1354,7 @@ interface
integer function trexio_read_$group_num$_32 (trex_file, num) bind(C) integer function trexio_read_$group_num$_32 (trex_file, num) bind(C)
use, intrinsic :: iso_c_binding use, intrinsic :: iso_c_binding
integer(8), intent(in), value :: trex_file integer(8), intent(in), value :: trex_file
integer(4), intent(out) :: num $group_num_f_dtype_single$, intent(out) :: num
end function trexio_read_$group_num$_32 end function trexio_read_$group_num$_32
end interface end interface
#+end_src #+end_src
@ -1364,7 +1364,7 @@ interface
integer function trexio_write_$group_num$ (trex_file, num) bind(C) integer function trexio_write_$group_num$ (trex_file, num) bind(C)
use, intrinsic :: iso_c_binding use, intrinsic :: iso_c_binding
integer(8), intent(in), value :: trex_file integer(8), intent(in), value :: trex_file
integer(4), intent(in), value :: num $group_num_f_dtype_default$, intent(in), value :: num
end function trexio_write_$group_num$ end function trexio_write_$group_num$
end interface end interface
#+end_src #+end_src
@ -1374,7 +1374,7 @@ interface
integer function trexio_read_$group_num$ (trex_file, num) bind(C) integer function trexio_read_$group_num$ (trex_file, num) bind(C)
use, intrinsic :: iso_c_binding use, intrinsic :: iso_c_binding
integer(8), intent(in), value :: trex_file integer(8), intent(in), value :: trex_file
integer(4), intent(out) :: num $group_num_f_dtype_default$, intent(out) :: num
end function trexio_read_$group_num$ end function trexio_read_$group_num$
end interface end interface
#+end_src #+end_src
@ -1391,7 +1391,7 @@ end interface
*** Python templates for front end *** Python templates for front end
#+begin_src python :tangle write_num_front.py #+begin_src python :tangle write_num_front.py
def write_$group_num$(trexio_file, num_w: int) -> None: def write_$group_num$(trexio_file, num_w: $group_num_py_dtype$) -> None:
"""Write the $group_num$ variable in the TREXIO file. """Write the $group_num$ variable in the TREXIO file.
Parameters: Parameters:
@ -1416,7 +1416,7 @@ def write_$group_num$(trexio_file, num_w: int) -> None:
#+end_src #+end_src
#+begin_src python :tangle read_num_front.py #+begin_src python :tangle read_num_front.py
def read_$group_num$(trexio_file) -> int: def read_$group_num$(trexio_file) -> $group_num_py_dtype$:
"""Read the $group_num$ variable from the TREXIO file. """Read the $group_num$ variable from the TREXIO file.
Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function. Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.

View File

@ -104,6 +104,8 @@ def recursive_populate_file(fname: str, paths: dict, detailed_source: dict) -> N
'group_dset_f_dtype_default', 'group_dset_f_dtype_double', 'group_dset_f_dtype_single', 'group_dset_f_dtype_default', 'group_dset_f_dtype_double', 'group_dset_f_dtype_single',
'group_dset_dtype_default', 'group_dset_dtype_double', 'group_dset_dtype_single', 'group_dset_dtype_default', 'group_dset_dtype_double', 'group_dset_dtype_single',
'group_dset_rank', 'group_dset_dim_list', 'group_dset_f_dims', 'group_dset_rank', 'group_dset_dim_list', 'group_dset_f_dims',
'group_num_f_dtype_default', 'group_num_f_dtype_double', 'group_num_f_dtype_single',
'group_num_dtype_default', 'group_num_dtype_double', 'group_num_dtype_single', 'group_num_py_dtype',
'group_dset', 'group_num', 'group_str', 'group'] 'group_dset', 'group_num', 'group_str', 'group']
for item in detailed_source.keys(): for item in detailed_source.keys():
@ -468,6 +470,34 @@ def get_detailed_num_dict (configuration: dict) -> dict:
tmp_dict['group_num'] = tmp_num tmp_dict['group_num'] = tmp_num
num_dict[tmp_num] = tmp_dict num_dict[tmp_num] = tmp_dict
# TODO the line below is the same as for group_dset and can be exported from somewhere
if v2[0] == 'float':
tmp_dict['datatype'] = 'double'
tmp_dict['group_num_h5_dtype'] = 'native_double'
tmp_dict['group_num_f_dtype_default']= 'real(8)'
tmp_dict['group_num_f_dtype_double'] = 'real(8)'
tmp_dict['group_num_f_dtype_single'] = 'real(4)'
tmp_dict['group_num_dtype_default']= 'double'
tmp_dict['group_num_dtype_double'] = 'double'
tmp_dict['group_num_dtype_single'] = 'float'
tmp_dict['default_prec'] = '64'
tmp_dict['group_num_std_dtype_out'] = '24.16e'
tmp_dict['group_num_std_dtype_in'] = 'lf'
tmp_dict['group_num_py_dtype'] = 'float'
elif v2[0] in ['int']:
tmp_dict['datatype'] = 'int64_t'
tmp_dict['group_num_h5_dtype'] = 'native_int64'
tmp_dict['group_num_f_dtype_default']= 'integer(4)'
tmp_dict['group_num_f_dtype_double'] = 'integer(8)'
tmp_dict['group_num_f_dtype_single'] = 'integer(4)'
tmp_dict['group_num_dtype_default']= 'int32_t'
tmp_dict['group_num_dtype_double'] = 'int64_t'
tmp_dict['group_num_dtype_single'] = 'int32_t'
tmp_dict['default_prec'] = '32'
tmp_dict['group_num_std_dtype_out'] = '" PRId64 "'
tmp_dict['group_num_std_dtype_in'] = '" SCNd64 "'
tmp_dict['group_num_py_dtype'] = 'int'
return num_dict return num_dict