1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-10-02 22:41:07 +02:00
trexio/templator_front.html

4425 lines
234 KiB
HTML
Raw Normal View History

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2021-11-03 Wed 09:29 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Front end API</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="TREX-CoE" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<link rel="stylesheet" title="Standard" href="trexio.css" type="text/css" />
<script type="text/javascript" src="org-info.js">
/**
*
* @source: org-info.js
*
* @licstart The following is the entire license notice for the
* JavaScript code in org-info.js.
*
* Copyright (C) 2012-2019 Free Software Foundation, Inc.
*
*
* The JavaScript code in this tag is free software: you can
* redistribute it and/or modify it under the terms of the GNU
* General Public License (GNU GPL) as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version. The code is distributed WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
*
* As additional permission under GNU GPL version 3 section 7, you
* may distribute non-source (e.g., minimized or compacted) forms of
* that code without the copy of the GNU GPL normally required by
* section 4, provided you include this license notice and a URL
* through which recipients can access the Corresponding Source.
*
* @licend The above is the entire license notice
* for the JavaScript code in org-info.js.
*
*/
</script>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2019 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
org_html_manager.set("TOC_DEPTH", "4");
org_html_manager.set("LINK_HOME", "index.html");
org_html_manager.set("LINK_UP", "");
org_html_manager.set("LOCAL_TOC", "1");
org_html_manager.set("VIEW_BUTTONS", "0");
org_html_manager.set("MOUSE_HINT", "underline");
org_html_manager.set("FIXED_TOC", "0");
org_html_manager.set("TOC", "1");
org_html_manager.set("VIEW", "info");
org_html_manager.setup(); // activate after the parameters are set
/*]]>*///-->
</script>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2019 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
displayAlign: "center",
displayIndent: "0em",
"HTML-CSS": { scale: 100,
linebreaks: { automatic: "false" },
webFont: "TeX"
},
SVG: {scale: 100,
linebreaks: { automatic: "false" },
font: "TeX"},
NativeMML: {scale: 100},
TeX: { equationNumbers: {autoNumber: "AMS"},
MultLineWidth: "85%",
TagSide: "right",
TagIndent: ".8em"
}
});
</script>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
<div id="org-div-home-and-up">
<a accesskey="h" href=""> UP </a>
|
<a accesskey="H" href="index.html"> HOME </a>
</div><div id="content">
<h1 class="title">Front end API</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org95e23c7">1. Coding conventions</a>
<ul>
<li><a href="#orgc47e7fd">1.1. Memory allocation</a></li>
</ul>
</li>
<li><a href="#orgfa74347">2. Front end</a>
<ul>
<li><a href="#orga54aac4">2.1. Error handling</a>
<ul>
<li><a href="#org9aa71e2">2.1.1. Decoding errors</a>
<ul>
<li><a href="#org8358574">2.1.1.1. C source code</a></li>
<li><a href="#orga917412">2.1.1.2. Fortran interface</a></li>
<li><a href="#org27115ea">2.1.1.3. Python interface</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#org50aa890">2.2. Back ends</a>
<ul>
<li><a href="#orge0c1eb9">2.2.1. C</a></li>
<li><a href="#org260abdb">2.2.2. Fortran</a></li>
<li><a href="#org4499e3e">2.2.3. Python</a></li>
</ul>
</li>
<li><a href="#org473f060">2.3. Read/write behavior</a></li>
<li><a href="#org3973cc1">2.4. TREXIO file type</a>
<ul>
<li><a href="#org14418b8">2.4.1. TREXIO<sub>File</sub> Python class</a></li>
</ul>
</li>
<li><a href="#orge237bb5">2.5. Polymorphism of the file handle</a></li>
<li><a href="#org2ff65b4">2.6. File opening</a>
<ul>
<li><a href="#orgeb04f92">2.6.1. C</a></li>
<li><a href="#org0360a10">2.6.2. Fortran</a></li>
<li><a href="#orgb67ea9f">2.6.3. Python</a></li>
<li><a href="#orgc51fe1e">2.6.4. Zero-based versus one-based arrays of indices</a></li>
</ul>
</li>
<li><a href="#org6d82da2">2.7. File closing</a>
<ul>
<li><a href="#org380ec63">2.7.1. C</a></li>
<li><a href="#org819adcf">2.7.2. Fortran</a></li>
<li><a href="#orga807798">2.7.3. Python</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orgbf2c7a0">3. Templates for front end</a>
<ul>
<li><a href="#org0ab196d">3.1. Description</a></li>
<li><a href="#org9c9274d">3.2. Templates for front end has/read/write a single numerical attribute</a>
<ul>
<li><a href="#org2ddbed8">3.2.1. Introduction</a></li>
<li><a href="#org5855868">3.2.2. C templates for front end</a>
<ul>
<li><a href="#org9b094fb">3.2.2.1. Function declarations</a></li>
<li><a href="#orga838cc6">3.2.2.2. Source code for double precision functions</a></li>
<li><a href="#orgfdfade3">3.2.2.3. Source code for single precision functions</a></li>
<li><a href="#orge895d2f">3.2.2.4. Source code for default functions</a></li>
</ul>
</li>
<li><a href="#org2be5a57">3.2.3. Fortran templates for front end</a></li>
<li><a href="#orgb5aee71">3.2.4. Python templates for front end</a></li>
</ul>
</li>
<li><a href="#orgb466703">3.3. Templates for front end has/read/write a dataset of numerical data</a>
<ul>
<li><a href="#org4d8ad8c">3.3.1. Introduction</a></li>
<li><a href="#org4bc1723">3.3.2. C templates for front end</a>
<ul>
<li><a href="#org0f77224">3.3.2.1. Function declarations</a></li>
<li><a href="#org187f1c3">3.3.2.2. Source code for double precision functions</a></li>
<li><a href="#org6196890">3.3.2.3. Source code for single precision functions</a></li>
<li><a href="#org50534bf">3.3.2.4. Source code for memory-safe functions</a></li>
<li><a href="#orgbdf2c84">3.3.2.5. Source code for default functions</a></li>
</ul>
</li>
<li><a href="#org67dd1ad">3.3.3. Fortran templates for front end</a></li>
<li><a href="#orgb4539f0">3.3.4. Python templates for front end</a></li>
</ul>
</li>
<li><a href="#org077d1ad">3.4. Sparse data structures</a></li>
<li><a href="#org64f4ae0">3.5. Templates for front end has/read/write a dataset of strings</a>
<ul>
<li><a href="#org0c9bf8a">3.5.1. Introduction</a></li>
<li><a href="#org4bcdc90">3.5.2. C templates for front end</a>
<ul>
<li><a href="#orgbba3863">3.5.2.1. Function declarations</a></li>
<li><a href="#orgb995b10">3.5.2.2. Source code for default functions</a></li>
</ul>
</li>
<li><a href="#orgba8c1fd">3.5.3. Fortran templates for front end</a></li>
<li><a href="#org93768c6">3.5.4. Python templates for front end</a></li>
</ul>
</li>
<li><a href="#org286ce2e">3.6. Templates for front end has/read/write a single string attribute</a>
<ul>
<li><a href="#org71ec410">3.6.1. Introduction</a></li>
<li><a href="#orgfcacb76">3.6.2. C templates for front end</a>
<ul>
<li><a href="#org46bfa2b">3.6.2.1. Function declarations</a></li>
<li><a href="#org0c5c1d4">3.6.2.2. Source code for default functions</a></li>
</ul>
</li>
<li><a href="#org3f0096e">3.6.3. Fortran templates for front end</a></li>
<li><a href="#org30cd357">3.6.4. Python templates for front end</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#orga1e5cb3">4. Fortran helper/wrapper functions</a></li>
</ul>
</div>
</div>
<div id="outline-container-org95e23c7" class="outline-2">
<h2 id="org95e23c7"><span class="section-number-2">1</span> Coding conventions</h2>
<div class="outline-text-2" id="text-1">
<ul class="org-ul">
<li>integer types will be defined using types given in <code>stdint.h</code></li>
<li>pointers are always initialized to <code>NULL</code></li>
<li>when memory is freed, the pointer is set to <code>NULL</code></li>
<li><code>assert.h</code> should be used extensively</li>
<li>variable names are in lower case</li>
<li><code>#define</code> constants are in upper case</li>
<li>structs are suffixed by <code>_s</code></li>
<li>types are suffixed by <code>_t</code></li>
<li>API calls return <code>trexio_exit_code</code> (except for <code>trexio_open</code> function)</li>
</ul>
</div>
<div id="outline-container-orgc47e7fd" class="outline-3">
<h3 id="orgc47e7fd"><span class="section-number-3">1.1</span> Memory allocation</h3>
<div class="outline-text-3" id="text-1-1">
<p>
Memory allocation of structures can be facilitated by using the
following macro, which ensures that the size of the allocated
object is the same as the size of the data type pointed by the pointer.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #483d8b;">#define</span> <span style="color: #0000ff;">MALLOC</span>(<span style="color: #a0522d;">T</span>) (<span style="color: #228b22;">T</span>*) malloc (<span style="color: #a020f0;">sizeof</span>(T))
<span style="color: #483d8b;">#define</span> <span style="color: #0000ff;">CALLOC</span>(<span style="color: #a0522d;">N</span>,<span style="color: #a0522d;">T</span>) (<span style="color: #228b22;">T</span>*) calloc ( (N) , <span style="color: #a020f0;">sizeof</span>(T) )
</pre>
</div>
<p>
When a pointer is freed, it should be set to <code>NULL</code>.
This can be facilitated by the use of the following macro:
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #483d8b;">#define</span> <span style="color: #0000ff;">FREE</span>(<span style="color: #a0522d;">X</span>) { free(X) ; (X)=<span style="color: #008b8b;">NULL</span>; }
</pre>
</div>
<p>
The maximum string size for the filenames is 4096 characters.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #483d8b;">#define</span> <span style="color: #a0522d;">TREXIO_MAX_FILENAME_LENGTH</span> 4096
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgfa74347" class="outline-2">
<h2 id="orgfa74347"><span class="section-number-2">2</span> Front end</h2>
<div class="outline-text-2" id="text-2">
<p>
All calls to TREXIO are thread-safe.
TREXIO front end is modular, which simplifies implementation of new back ends.
</p>
</div>
<div id="outline-container-orga54aac4" class="outline-3">
<h3 id="orga54aac4"><span class="section-number-3">2.1</span> Error handling</h3>
<div class="outline-text-3" id="text-2-1">
<table id="org053e971" border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-right" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Macro</th>
<th scope="col" class="org-right">Code</th>
<th scope="col" class="org-left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>TREXIO_FAILURE</code></td>
<td class="org-right">-1</td>
<td class="org-left">'Unknown failure'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_SUCCESS</code></td>
<td class="org-right">0</td>
<td class="org-left">'Success'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_INVALID_ARG_1</code></td>
<td class="org-right">1</td>
<td class="org-left">'Invalid argument 1'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_INVALID_ARG_2</code></td>
<td class="org-right">2</td>
<td class="org-left">'Invalid argument 2'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_INVALID_ARG_3</code></td>
<td class="org-right">3</td>
<td class="org-left">'Invalid argument 3'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_INVALID_ARG_4</code></td>
<td class="org-right">4</td>
<td class="org-left">'Invalid argument 4'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_INVALID_ARG_5</code></td>
<td class="org-right">5</td>
<td class="org-left">'Invalid argument 5'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_END</code></td>
<td class="org-right">6</td>
<td class="org-left">'End of file'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_READONLY</code></td>
<td class="org-right">7</td>
<td class="org-left">'Read-only file'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_ERRNO</code></td>
<td class="org-right">8</td>
<td class="org-left">strerror(errno)</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_INVALID_ID</code></td>
<td class="org-right">9</td>
<td class="org-left">'Invalid ID'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_ALLOCATION_FAILED</code></td>
<td class="org-right">10</td>
<td class="org-left">'Allocation failed'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_HAS_NOT</code></td>
<td class="org-right">11</td>
<td class="org-left">'Element absent'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_INVALID_NUM</code></td>
<td class="org-right">12</td>
<td class="org-left">'Invalid (negative or 0) dimension'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_ATTR_ALREADY_EXISTS</code></td>
<td class="org-right">13</td>
<td class="org-left">'Attribute already exists'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_DSET_ALREADY_EXISTS</code></td>
<td class="org-right">14</td>
<td class="org-left">'Dataset already exists'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_OPEN_ERROR</code></td>
<td class="org-right">15</td>
<td class="org-left">'Error opening file'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_LOCK_ERROR</code></td>
<td class="org-right">16</td>
<td class="org-left">'Error locking file'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_UNLOCK_ERROR</code></td>
<td class="org-right">17</td>
<td class="org-left">'Error unlocking file'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_FILE_ERROR</code></td>
<td class="org-right">18</td>
<td class="org-left">'Invalid file handle'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_GROUP_READ_ERROR</code></td>
<td class="org-right">19</td>
<td class="org-left">'Error reading group'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_GROUP_WRITE_ERROR</code></td>
<td class="org-right">20</td>
<td class="org-left">'Error writing group'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_ELEM_READ_ERROR</code></td>
<td class="org-right">21</td>
<td class="org-left">'Error reading element'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_ELEM_WRITE_ERROR</code></td>
<td class="org-right">22</td>
<td class="org-left">'Error writing element'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_UNSAFE_ARRAY_DIM</code></td>
<td class="org-right">23</td>
<td class="org-left">'Access to memory beyond allocated'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_ATTR_MISSING</code></td>
<td class="org-right">24</td>
<td class="org-left">'Attribute does not exist in the file'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_DSET_MISSING</code></td>
<td class="org-right">25</td>
<td class="org-left">'Dataset does not exist in the file'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_BACK_END_MISSING</code></td>
<td class="org-right">26</td>
<td class="org-left">'Requested back end is disabled'</td>
</tr>
<tr>
<td class="org-left"><code>TREXIO_INVALID_STR_LEN</code></td>
<td class="org-right">30</td>
<td class="org-left">'Invalid max<sub>str</sub><sub>len</sub>'</td>
</tr>
</tbody>
</table>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #8b2252;">""" This script generates the C and Fortran constants for the error</span>
<span style="color: #8b2252;"> codes from the org-mode table.</span>
<span style="color: #8b2252;">"""</span>
<span style="color: #a0522d;">result</span> = [ <span style="color: #8b2252;">"#+begin_src c :tangle prefix_front.h :exports none"</span> ]
<span style="color: #a020f0;">for</span> (text, code,_) <span style="color: #a020f0;">in</span> table:
<span style="color: #a0522d;">text</span>=text.replace(<span style="color: #8b2252;">"~"</span>,<span style="color: #8b2252;">""</span>)
<span style="color: #a0522d;">result</span> += [ f<span style="color: #8b2252;">"#define {text:30s} ((trexio_exit_code) {code:d})"</span> ]
<span style="color: #a0522d;">result</span> += [ <span style="color: #8b2252;">"#+end_src"</span> ]
<span style="color: #a0522d;">result</span> += [ <span style="color: #8b2252;">""</span> ]
<span style="color: #a0522d;">result</span> += [ <span style="color: #8b2252;">"#+begin_src f90 :tangle prefix_fortran.f90 :exports none"</span> ]
<span style="color: #a020f0;">for</span> (text, code,_) <span style="color: #a020f0;">in</span> table:
<span style="color: #a0522d;">text</span>=text.replace(<span style="color: #8b2252;">"~"</span>,<span style="color: #8b2252;">""</span>)
<span style="color: #a0522d;">result</span> += [ f<span style="color: #8b2252;">" integer(trexio_exit_code), parameter :: {text:30s} = {code:d}"</span> ]
<span style="color: #a0522d;">result</span> += [ <span style="color: #8b2252;">"#+end_src"</span> ]
<span style="color: #a0522d;">result</span> += [ <span style="color: #8b2252;">""</span> ]
<span style="color: #a0522d;">result</span> += [ <span style="color: #8b2252;">"#+begin_src python :tangle prefix_python.py :exports none"</span> ]
<span style="color: #a0522d;">result</span> += [ <span style="color: #8b2252;">"# define TREXIO exit codes"</span> ]
<span style="color: #a020f0;">for</span> (text, code,_) <span style="color: #a020f0;">in</span> table:
<span style="color: #a0522d;">text</span>=text.replace(<span style="color: #8b2252;">"~"</span>,<span style="color: #8b2252;">""</span>)
<span style="color: #a0522d;">result</span> += [ f<span style="color: #8b2252;">"{text:30s} = {code:d}"</span> ]
<span style="color: #a0522d;">result</span> += [ <span style="color: #8b2252;">"#+end_src"</span> ]
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">'\n'</span>.join(result)
</pre>
</div>
</div>
<div id="outline-container-org9aa71e2" class="outline-4">
<h4 id="org9aa71e2"><span class="section-number-4">2.1.1</span> Decoding errors</h4>
<div class="outline-text-4" id="text-2-1-1">
<p>
The <code>trexio_string_of_error</code> converts an exit code into a string. The
string is assumed to be large enough to contain the error message
(typically 128 characters).
</p>
<p>
To decode the error messages, <code>trexio_string_of_error</code> converts an
error code into a string.
</p>
<pre class="example">
128
</pre>
<p>
The text strings are extracted from the previous table.
</p>
</div>
<div id="outline-container-org8358574" class="outline-5">
<h5 id="org8358574"><span class="section-number-5">2.1.1.1</span> C source code</h5>
<div class="outline-text-5" id="text-2-1-1-1">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>*
<span style="color: #0000ff;">trexio_string_of_error</span> (<span style="color: #a020f0;">const</span> <span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">error</span>)
{
<span style="color: #a020f0;">switch</span> (error) {
<span style="color: #a020f0;">case</span> TREXIO_FAILURE:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Unknown failure"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_SUCCESS:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Success"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_INVALID_ARG_1:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Invalid argument 1"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_INVALID_ARG_2:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Invalid argument 2"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_INVALID_ARG_3:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Invalid argument 3"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_INVALID_ARG_4:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Invalid argument 4"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_INVALID_ARG_5:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Invalid argument 5"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_END:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"End of file"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_READONLY:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Read-only file"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_ERRNO:
<span style="color: #a020f0;">return</span> strerror(errno);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_INVALID_ID:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Invalid ID"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_ALLOCATION_FAILED:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Allocation failed"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HAS_NOT:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Element absent"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_INVALID_NUM:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Invalid (negative or 0) dimension"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_ATTR_ALREADY_EXISTS:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Attribute already exists"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_DSET_ALREADY_EXISTS:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Dataset already exists"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_OPEN_ERROR:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Error opening file"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_LOCK_ERROR:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Error locking file"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_UNLOCK_ERROR:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Error unlocking file"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_FILE_ERROR:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Invalid file handle"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_GROUP_READ_ERROR:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Error reading group"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_GROUP_WRITE_ERROR:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Error writing group"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_ELEM_READ_ERROR:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Error reading element"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_ELEM_WRITE_ERROR:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Error writing element"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_UNSAFE_ARRAY_DIM:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Access to memory beyond allocated"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_ATTR_MISSING:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Attribute does not exist in the file"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_DSET_MISSING:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Dataset does not exist in the file"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_BACK_END_MISSING:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Requested back end is disabled"</span>;
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_INVALID_STR_LEN:
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Invalid max_str_len"</span>;
<span style="color: #a020f0;">break</span>;
}
<span style="color: #a020f0;">return</span> <span style="color: #8b2252;">"Unknown error"</span>;
}
<span style="color: #228b22;">void</span>
<span style="color: #0000ff;">trexio_string_of_error_f</span> (<span style="color: #a020f0;">const</span> <span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">error</span>, <span style="color: #228b22;">char</span> <span style="color: #a0522d;">result</span>[128])
{
strncpy(result, trexio_string_of_error(error), 128);
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orga917412" class="outline-5">
<h5 id="orga917412"><span class="section-number-5">2.1.1.2</span> Fortran interface</h5>
<div class="outline-text-5" id="text-2-1-1-2">
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #a020f0;">subroutine</span> <span style="color: #0000ff;">trexio_string_of_error</span> (error, string) <span style="color: #a020f0;">bind</span>(C, name=<span style="color: #8b2252;">'trexio_string_of_error_f'</span>)
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #a020f0;">import</span>
<span style="color: #228b22;">integer</span> (trexio_exit_code), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> error</span>
<span style="color: #228b22;">character</span>, <span style="color: #a020f0;">intent</span>(out) ::<span style="color: #a0522d;"> string(128)</span>
<span style="color: #a020f0;">end subroutine</span> <span style="color: #0000ff;">trexio_string_of_error</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org27115ea" class="outline-5">
<h5 id="org27115ea"><span class="section-number-5">2.1.1.3</span> Python interface</h5>
<div class="outline-text-5" id="text-2-1-1-3">
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">class</span> <span style="color: #228b22;">Error</span>(<span style="color: #228b22;">Exception</span>):
<span style="color: #8b2252;">"""Base class for TREXIO errors.</span>
<span style="color: #8b2252;"> Attributes:</span>
<span style="color: #8b2252;"> error: int -- exit code returned by the call to TREXIO library;</span>
<span style="color: #8b2252;"> message: str -- decoded string corresponding to trexio_exit_code.</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">def</span> <span style="color: #0000ff;">__init__</span>(<span style="color: #a020f0;">self</span>, trexio_return_code):
<span style="color: #a020f0;">self</span>.error = trexio_return_code
<span style="color: #a020f0;">self</span>.message = string_of_error(trexio_return_code)
<span style="color: #483d8b;">super</span>().__init__(<span style="color: #a020f0;">self</span>.message)
<span style="color: #a020f0;">def</span> <span style="color: #0000ff;">string_of_error</span>(return_code: <span style="color: #483d8b;">int</span>) -&gt; <span style="color: #483d8b;">str</span>:
<span style="color: #8b2252;">"""Decode the TREXIO exit code.</span>
<span style="color: #8b2252;"> Argument is an integer return code that correspond to one of the TREXIO errors.</span>
<span style="color: #8b2252;"> Returns string that contains description of TREXIO ~return_code~.</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">error_str</span> = pytr.trexio_string_of_error(return_code)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a020f0;">return</span> error_str
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="outline-container-org50aa890" class="outline-3">
<h3 id="org50aa890"><span class="section-number-3">2.2</span> Back ends</h3>
<div class="outline-text-3" id="text-2-2">
<p>
TREXIO has several back ends:
</p>
<ol class="org-ol">
<li><code>TREXIO_HDF5</code> relies on extensive use of the HDF5 library and the associated file format. The HDF5 file is binary and tailored to high-performance I/O. This back end is the default one. HDF5 can be compiled with MPI for parallel I/O. Note, that HDF5 has to be downloaded and installed independently of TREXIO, which may cause some obstacles, especially when the user is not allowed to install external software. The produced files usually have <code>.h5</code> extension.</li>
<li><code>TREXIO_TEXT</code> relies on basic file I/O in C, namely <code>fopen, fclose, fprintf, fscanf</code> etc. from <code>stdio.h</code> library. This back end is not optimized for performance. It is supposed to be used for debug purposes or, for example, when the user wants to modify some data manually within the file. This back end is supposed to work "out-of-the-box" since there are no external dependencies, which might be useful for users that do not have access to HDF5 library. The produced files usually have <code>.txt</code> extension.</li>
</ol>
<p>
Additional back ends can be implemented thanks to the modular nature of the front end.
This can be achieved by adding a new <code>case</code> (corresponding to the desired back end) in the front-end <code>switch</code>.
Then the corresponding back-end <code>has/read/write</code> functions has to be implemented. For example, see the commented
lines that correspond to the <code>TREXIO_JSON</code> back end (not implemented yet).
</p>
<p>
<span class="underline">Note</span>: It is important to increment the value of TREXIO<sub>INVALID</sub><sub>BACK</sub><sub>END</sub> when a new back end is added. Otherwise, it will not be available.
</p>
</div>
<div id="outline-container-orge0c1eb9" class="outline-4">
<h4 id="orge0c1eb9"><span class="section-number-4">2.2.1</span> C</h4>
<div class="outline-text-4" id="text-2-2-1">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #a020f0;">typedef</span> <span style="color: #228b22;">int32_t</span> <span style="color: #228b22;">back_end_t</span>;
<span style="color: #483d8b;">#define</span> <span style="color: #a0522d;">TREXIO_HDF5</span> ( (<span style="color: #228b22;">back_end_t</span>) 0 )
<span style="color: #483d8b;">#define</span> <span style="color: #a0522d;">TREXIO_TEXT</span> ( (<span style="color: #228b22;">back_end_t</span>) 1 )
<span style="color: #483d8b;">#define</span> <span style="color: #a0522d;">TREXIO_INVALID_BACK_END</span> ( (<span style="color: #228b22;">back_end_t</span>) 2 )
/*<span style="color: #b22222;">#define TREXIO_JSON ( (back_end_t) 2 )</span>*/
<span style="color: #483d8b;">#define</span> <span style="color: #a0522d;">TREXIO_DELIM</span> <span style="color: #8b2252;">"\n"</span>
</pre>
</div>
<p>
The helper function <code>trexio_has_backend</code> returns <code>true</code> if TREXIO compilation includes a back end provided as an argument; <code>false</code> otherwise.
This is useful due to the fact that HDF5 back end can be disabled at configure step.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">bool</span> <span style="color: #0000ff;">trexio_has_backend</span>(<span style="color: #228b22;">back_end_t</span> <span style="color: #a0522d;">back_end</span>);
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">bool</span> <span style="color: #0000ff;">trexio_has_backend</span>(<span style="color: #228b22;">back_end_t</span> <span style="color: #a0522d;">back_end</span>) {
<span style="color: #a020f0;">switch</span> (back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">true</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">true</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">false</span>;
<span style="color: #483d8b;">#endif</span>
}
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">false</span>;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org260abdb" class="outline-4">
<h4 id="org260abdb"><span class="section-number-4">2.2.2</span> Fortran</h4>
<div class="outline-text-4" id="text-2-2-2">
<div class="org-src-container">
<pre class="src src-f90"> <span style="color: #228b22;">integer</span>(trexio_backend), <span style="color: #a020f0;">parameter</span> ::<span style="color: #a0522d;"> TREXIO_HDF5 = 0 </span>
<span style="color: #228b22;">integer</span>(trexio_backend), <span style="color: #a020f0;">parameter</span> ::<span style="color: #a0522d;"> TREXIO_TEXT = 1</span>
! <span style="color: #b22222;">integer(trexio_backend), parameter :: TREXIO_JSON = 2</span>
<span style="color: #228b22;">integer</span>(trexio_backend), <span style="color: #a020f0;">parameter</span> ::<span style="color: #a0522d;"> TREXIO_INVALID_BACK_END = 2</span>
</pre>
</div>
<p>
The function below is a Fortran interface for the aforementioned C-compatible <code>trexio_has_backend</code> function.
</p>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">logical </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_has_backend</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(back_end) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(4), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> back_end</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_has_backend</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org4499e3e" class="outline-4">
<h4 id="org4499e3e"><span class="section-number-4">2.2.3</span> Python</h4>
<div class="outline-text-4" id="text-2-2-3">
<div class="org-src-container">
<pre class="src src-python"># <span style="color: #b22222;">define TREXIO back ends</span>
<span style="color: #a0522d;">TREXIO_HDF5</span> = 0
<span style="color: #a0522d;">TREXIO_TEXT</span> = 1
#<span style="color: #b22222;">TREXIO_JSON = 2</span>
<span style="color: #a0522d;">TREXIO_INVALID_BACK_END</span> = 2
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org473f060" class="outline-3">
<h3 id="org473f060"><span class="section-number-3">2.3</span> Read/write behavior</h3>
<div class="outline-text-3" id="text-2-3">
<p>
Every time a reading function is called, the data is read from the
disk. If data needs to be cached, this is left to the user of the
library.
</p>
<p>
Writing to TREXIO files is done with transactions (all-or-nothing
effect) in a per-group fashion. File writes are attempted by
calling explicitly the write (<code>TREXIO_HDF5</code>) or flush (<code>TREXIO_TEXT</code>)
function, or when the TREXIO file is closed.
If writing is impossible because the data is not valid, no data is written.
</p>
<p>
The order in which the data is written is not necessarily consistent
with the order in which the function calls were made.
</p>
<p>
The TREXIO files are supposed to be opened by only one program at a
time: if the same TREXIO file is modified simultaneously by multiple
concurrent programs, the behavior is not specified.
</p>
</div>
</div>
<div id="outline-container-org3973cc1" class="outline-3">
<h3 id="org3973cc1"><span class="section-number-3">2.4</span> TREXIO file type</h3>
<div class="outline-text-3" id="text-2-4">
<p>
<code>trexio_s</code> is the the main type for TREXIO files, visible to the users
of the library. This type is kept opaque, and all modifications to
the files will be necessarily done through the use of functions,
taking such a type as argument.
</p>
<p>
File creation and opening functions will return <i>TREXIO file handles</i>,
namely pointers to <code>trexio_s</code> types. All functions accessing to the
TREXIO files will have as a first argument the TREXIO file handle.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #a020f0;">typedef</span> <span style="color: #a020f0;">struct</span> <span style="color: #228b22;">trexio_s</span> <span style="color: #228b22;">trexio_t</span>;
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #a020f0;">struct</span> <span style="color: #228b22;">trexio_s</span> {
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">file_name</span>[TREXIO_MAX_FILENAME_LENGTH];
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">version</span>[16];
<span style="color: #228b22;">pthread_mutex_t</span> <span style="color: #a0522d;">thread_lock</span>;
<span style="color: #228b22;">back_end_t</span> <span style="color: #a0522d;">back_end</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">mode</span>;
<span style="color: #228b22;">bool</span> <span style="color: #a0522d;">one_based</span>;
<span style="color: #228b22;">char</span> <span style="color: #a0522d;">padding</span>[6]; /* <span style="color: #b22222;">Ensures the proper alignment of back ends </span>*/
};
</pre>
</div>
</div>
<div id="outline-container-org14418b8" class="outline-4">
<h4 id="org14418b8"><span class="section-number-4">2.4.1</span> TREXIO<sub>File</sub> Python class</h4>
<div class="outline-text-4" id="text-2-4-1">
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">class</span> <span style="color: #228b22;">File</span>:
<span style="color: #8b2252;">"""TREXIO File object.</span>
<span style="color: #8b2252;"> General information about the TREXIO file.</span>
<span style="color: #8b2252;"> Parameters:</span>
<span style="color: #8b2252;"> filename: str</span>
<span style="color: #8b2252;"> Is a name of the full path to the TREXIO file.</span>
<span style="color: #8b2252;"> back_end: int</span>
<span style="color: #8b2252;"> One of the currently supported TREXIO back ends.</span>
<span style="color: #8b2252;"> For example, TREXIO_HDF5 (0) or TREXIO_TEXT (1).</span>
<span style="color: #8b2252;"> mode: str</span>
<span style="color: #8b2252;"> One of the currently supported TREXIO open modes.</span>
<span style="color: #8b2252;"> For example, 'r' or 'w'.</span>
<span style="color: #8b2252;"> isOpen: bool</span>
<span style="color: #8b2252;"> Flag indicating whether the current object is still open for I/O</span>
<span style="color: #8b2252;"> pytrexio_s:</span>
<span style="color: #8b2252;"> A PyObject corresponding to SWIG proxy of the trexio_s struct in C.</span>
<span style="color: #8b2252;"> This argument is in fact a TREXIO file handle, which is required for</span>
<span style="color: #8b2252;"> communicating with the C back end. </span>
<span style="color: #8b2252;"> info: dict</span>
<span style="color: #8b2252;"> Dictionary of key-value pairs with additional information about the file.</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">def</span> <span style="color: #0000ff;">__init__</span>(<span style="color: #a020f0;">self</span>, filename, mode=<span style="color: #8b2252;">'r'</span>, back_end=TREXIO_HDF5,
pytrexio_s=<span style="color: #008b8b;">None</span>, info=<span style="color: #008b8b;">None</span>):
<span style="color: #8b2252;">"""This is a TREXIO File constructor."""</span>
<span style="color: #a020f0;">self</span>.filename = filename
<span style="color: #a020f0;">self</span>.mode = mode
<span style="color: #a020f0;">self</span>.back_end = back_end
<span style="color: #a020f0;">self</span>.isOpen = <span style="color: #008b8b;">False</span>
<span style="color: #a020f0;">if</span> pytrexio_s <span style="color: #a020f0;">is</span> <span style="color: #008b8b;">None</span>:
<span style="color: #a020f0;">self</span>.pytrexio_s = <span style="color: #483d8b;">open</span>(filename, mode, back_end)
<span style="color: #a020f0;">self</span>.isOpen = <span style="color: #008b8b;">True</span>
<span style="color: #a020f0;">else</span>:
<span style="color: #a020f0;">self</span>.pytrexio_s = pytrexio_s
<span style="color: #a020f0;">self</span>.isOpen = <span style="color: #008b8b;">None</span>
<span style="color: #a020f0;">self</span>.info = info
<span style="color: #a020f0;">def</span> <span style="color: #0000ff;">close</span>(<span style="color: #a020f0;">self</span>):
<span style="color: #8b2252;">"""Close a TREXIO File."""</span>
<span style="color: #a020f0;">if</span> <span style="color: #a020f0;">self</span>.isOpen:
close(<span style="color: #a020f0;">self</span>.pytrexio_s)
<span style="color: #a020f0;">self</span>.isOpen = <span style="color: #008b8b;">False</span>
<span style="color: #a020f0;">else</span>:
<span style="color: #a020f0;">raise</span> <span style="color: #228b22;">Exception</span>(<span style="color: #8b2252;">"TREXIO File object has not been opened."</span>)
<span style="color: #a020f0;">def</span> <span style="color: #0000ff;">__del__</span>(<span style="color: #a020f0;">self</span>):
<span style="color: #8b2252;">"""This is a TREXIO File destructor."""</span>
<span style="color: #a020f0;">if</span> <span style="color: #a020f0;">self</span>.isOpen:
close(<span style="color: #a020f0;">self</span>.pytrexio_s)
<span style="color: #a020f0;">elif</span> <span style="color: #a020f0;">self</span>.isOpen <span style="color: #a020f0;">is</span> <span style="color: #008b8b;">None</span>:
<span style="color: #a020f0;">raise</span> <span style="color: #228b22;">Exception</span>(<span style="color: #8b2252;">"[WIP]: TREXIO file handle provided but what if the file is already closed?"</span>)
<span style="color: #a020f0;">else</span>:
<span style="color: #a020f0;">pass</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orge237bb5" class="outline-3">
<h3 id="orge237bb5"><span class="section-number-3">2.5</span> Polymorphism of the file handle</h3>
<div class="outline-text-3" id="text-2-5">
<p>
Polymorphism of the <code>trexio_t</code> type is handled by ensuring that the
corresponding types for all back ends can be safely casted to
<code>trexio_t</code>. This is done by making the back-end structs start with
<code>struct trexio_s</code>:
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #a020f0;">struct</span> <span style="color: #228b22;">trexio_back_end_s</span> {
<span style="color: #228b22;">trexio_t</span> <span style="color: #a0522d;">parent</span> ;
/* <span style="color: #b22222;">add below specific back-end data </span>*/
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org2ff65b4" class="outline-3">
<h3 id="org2ff65b4"><span class="section-number-3">2.6</span> File opening</h3>
<div class="outline-text-3" id="text-2-6">
<p>
<code>trexio_open</code> creates a new TREXIO file or opens existing one.
</p>
<p>
input parameters:
</p>
<ol class="org-ol">
<li><code>file_name</code> - string containing file name</li>
<li><code>mode</code> - character containing open mode (see below)
<ul class="org-ul">
<li><code>'w'</code> - (write) creates a new file as READWRITE (overwrite existing file)</li>
<li><code>'r'</code> - (read) opens existing file as READONLY</li>
</ul></li>
<li><code>back_end</code> - integer number (or the corresponding global parameter) specifying the back end
<ul class="org-ul">
<li><code>TREXIO_HDF5</code> - for HDF5 back end (integer alternative: 0)</li>
<li><code>TREXIO_TEXT</code> - for TEXT back end (integer alternative: 1)</li>
</ul></li>
</ol>
<p>
output:
<code>trexio_t</code> file handle
</p>
<p>
Note: the <code>file_name</code> in TEXT back end actually corresponds to the
name of the directory where <code>.txt</code> data files are stored. The
actual name of each <code>.txt</code> file corresponds to the group name
provided in <code>trex.config</code> (e.g. <code>nucleus.txt</code> for nuclei-related
data). These names are populated by the generator.py (i.e. they
are hard-coded), which is why the user should tend to avoid
renaming the <code>.txt</code> data files.
</p>
</div>
<div id="outline-container-orgeb04f92" class="outline-4">
<h4 id="orgeb04f92"><span class="section-number-4">2.6.1</span> C</h4>
<div class="outline-text-4" id="text-2-6-1">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_t</span>*
<span style="color: #0000ff;">trexio_open</span>(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>* <span style="color: #a0522d;">file_name</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span> <span style="color: #a0522d;">mode</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">back_end_t</span> <span style="color: #a0522d;">back_end</span>, <span style="color: #228b22;">trexio_exit_code</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">rc_open</span>)
{
<span style="color: #a020f0;">if</span> (file_name == <span style="color: #008b8b;">NULL</span> || file_name[0] == <span style="color: #8b2252;">'\0'</span>) {
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
/* <span style="color: #b22222;">Check overflow in file_name </span>*/
<span style="color: #a020f0;">if</span> (back_end &lt; 0 || back_end &gt;= TREXIO_INVALID_BACK_END) {
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_INVALID_ARG_3;
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
<span style="color: #a020f0;">if</span> (mode != <span style="color: #8b2252;">'r'</span> &amp;&amp; mode != <span style="color: #8b2252;">'w'</span>) {
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
<span style="color: #228b22;">trexio_t</span>* <span style="color: #a0522d;">result</span> = <span style="color: #008b8b;">NULL</span>;
<span style="color: #228b22;">void</span>* <span style="color: #a0522d;">result_tmp</span> = <span style="color: #008b8b;">NULL</span>;
/* <span style="color: #b22222;">Allocate data structures </span>*/
<span style="color: #a020f0;">switch</span> (back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
result_tmp = malloc(<span style="color: #a020f0;">sizeof</span>(trexio_text_t));
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
result_tmp = malloc(<span style="color: #a020f0;">sizeof</span>(trexio_hdf5_t));
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_BACK_END_MISSING;
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> result = (trexio_t*) malloc (sizeof(trexio_json_t));</span>
<span style="color: #b22222;"> break;</span>
*/
}
result = (<span style="color: #228b22;">trexio_t</span>*) result_tmp;
assert (result != <span style="color: #008b8b;">NULL</span>); /* <span style="color: #b22222;">TODO: Error handling </span>*/
/* <span style="color: #b22222;">Data for the parent type </span>*/
strncpy(result-&gt;file_name, file_name, TREXIO_MAX_FILENAME_LENGTH);
<span style="color: #a020f0;">if</span> (result-&gt;file_name[TREXIO_MAX_FILENAME_LENGTH-1] != <span style="color: #8b2252;">'\0'</span>) {
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_INVALID_ARG_1;
free(result);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
strncpy(result-&gt;version, TREXIO_PACKAGE_VERSION, 16);
<span style="color: #a020f0;">if</span> (result-&gt;version[15] != <span style="color: #8b2252;">'\0'</span>) {
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_FAILURE;
free(result);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
result-&gt;back_end = back_end;
result-&gt;mode = mode;
result-&gt;one_based = <span style="color: #008b8b;">false</span>; // <span style="color: #b22222;">Need to be flipped in Fortran interface</span>
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">irc</span> = pthread_mutex_init ( &amp;(result-&gt;thread_lock), <span style="color: #008b8b;">NULL</span>);
<span style="color: #a020f0;">if</span> (irc != 0) {
free(result);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
assert (irc == 0);
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
/* <span style="color: #b22222;">Back end initialization </span>*/
rc = TREXIO_OPEN_ERROR;
<span style="color: #a020f0;">switch</span> (back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_init(result);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = trexio_hdf5_init(result);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_BACK_END_MISSING;
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_init(result);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) {
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_OPEN_ERROR;
free(result);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
rc = trexio_has_metadata_package_version(result);
<span style="color: #a020f0;">if</span> (rc == TREXIO_FAILURE) {
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_OPEN_ERROR;
free(result);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
<span style="color: #a020f0;">if</span> (rc == TREXIO_HAS_NOT) {
<span style="color: #a020f0;">switch</span> (back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_write_metadata_package_version(result, TREXIO_PACKAGE_VERSION);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = trexio_hdf5_write_metadata_package_version(result, TREXIO_PACKAGE_VERSION);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_BACK_END_MISSING;
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_write_metadata_package_version(result, TREXIO_PACKAGE_VERSION);</span>
<span style="color: #b22222;"> break;</span>
*/
}
}
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) {
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_OPEN_ERROR;
free(result);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
/* <span style="color: #b22222;">File locking </span>*/
rc = TREXIO_LOCK_ERROR;
<span style="color: #a020f0;">switch</span> (back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_lock(result);
<span style="color: #a020f0;">break</span>;
/* <span style="color: #b22222;">HDF5 v.&gt;=1.10 has file locking activated by default </span>*/
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = TREXIO_SUCCESS;
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_BACK_END_MISSING;
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_lock(result);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) {
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_LOCK_ERROR;
free(result);
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">NULL</span>;
}
<span style="color: #a020f0;">if</span> (rc_open != <span style="color: #008b8b;">NULL</span>) *rc_open = TREXIO_SUCCESS;
<span style="color: #a020f0;">return</span> result;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org0360a10" class="outline-4">
<h4 id="org0360a10"><span class="section-number-4">2.6.2</span> Fortran</h4>
<div class="outline-text-4" id="text-2-6-2">
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer</span>(8) <span style="color: #a020f0;">function</span> <span style="color: #0000ff;">trexio_open_c</span> (filename, mode, backend, rc_open) <span style="color: #a020f0;">bind</span>(C, name=<span style="color: #8b2252;">"trexio_open"</span>)
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #a020f0;">import</span>
<span style="color: #228b22;">character</span>(kind=<span style="color: #008b8b;">c_char</span>), <span style="color: #a020f0;">dimension</span>(*) ::<span style="color: #a0522d;"> filename</span>
<span style="color: #228b22;">character</span>, <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> mode</span>
<span style="color: #228b22;">integer</span>(trexio_backend), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> backend</span>
<span style="color: #228b22;">integer</span>(trexio_exit_code), <span style="color: #a020f0;">intent</span>(out) ::<span style="color: #a0522d;"> rc_open</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_open_c</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgb67ea9f" class="outline-4">
<h4 id="orgb67ea9f"><span class="section-number-4">2.6.3</span> Python</h4>
<div class="outline-text-4" id="text-2-6-3">
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">open</span>(file_name: <span style="color: #483d8b;">str</span>, mode: <span style="color: #483d8b;">str</span>, back_end: <span style="color: #483d8b;">int</span>):
<span style="color: #8b2252;">"""Create TREXIO file or open existing one.</span>
<span style="color: #8b2252;"> Parameters:</span>
<span style="color: #8b2252;"> file_name: str</span>
<span style="color: #8b2252;"> Name of the TREXIO file</span>
<span style="color: #8b2252;"> mode: str</span>
<span style="color: #8b2252;"> One of the currently supported ~open~ modes (e.g. 'w', 'r')</span>
<span style="color: #8b2252;"> back_end: int</span>
<span style="color: #8b2252;"> One of the currently supported TREXIO back ends (e.g. TREXIO_HDF5, TREXIO_TEXT) </span>
<span style="color: #8b2252;"> Return: </span>
<span style="color: #8b2252;"> SWIG object of type trexio_s.</span>
<span style="color: #8b2252;"> Examples:</span>
<span style="color: #8b2252;"> &gt;&gt;&gt; from trexio import open as tr_open</span>
<span style="color: #8b2252;"> &gt;&gt;&gt; trex_file = tr_open("example.h5", "w", TREXIO_HDF5)</span>
<span style="color: #8b2252;"> """</span>
# <span style="color: #b22222;">The new trexio_open function is capable of returning error code which SWIG can append to the output trexio_s file struct</span>
# <span style="color: #b22222;">However, if trexio_s* == NULL, then SWIG returns only an error code rc_open instead of a list [trexio_s, rc_open]</span>
# <span style="color: #b22222;">Thus, the following try/except sequence is needed</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">return_obj</span> = pytr.trexio_open(file_name, mode, back_end)
<span style="color: #a020f0;">assert</span> return_obj <span style="color: #a020f0;">is</span> <span style="color: #a020f0;">not</span> <span style="color: #008b8b;">None</span>
<span style="color: #a020f0;">if</span> <span style="color: #483d8b;">isinstance</span>(return_obj, <span style="color: #483d8b;">int</span>):
<span style="color: #a020f0;">raise</span> Error(return_obj)
<span style="color: #a020f0;">else</span>:
<span style="color: #a0522d;">rc_open</span> = return_obj[1]
# <span style="color: #b22222;">this is a sanity check in case the code evolves and SWIG issue is patched</span>
<span style="color: #a020f0;">if</span> rc_open == TREXIO_SUCCESS:
<span style="color: #a0522d;">trexio_file</span> = return_obj[0]
<span style="color: #a020f0;">assert</span> trexio_file <span style="color: #a020f0;">is</span> <span style="color: #a020f0;">not</span> <span style="color: #008b8b;">None</span>
<span style="color: #a020f0;">except</span> <span style="color: #228b22;">AssertionError</span>:
<span style="color: #a020f0;">raise</span> <span style="color: #228b22;">Exception</span>(f<span style="color: #8b2252;">"Could not open TREXIO file {file_name} using trexio_open function. The return value is None (NULL pointer)."</span>)
<span style="color: #a020f0;">return</span> trexio_file
</pre>
</div>
</div>
</div>
<div id="outline-container-orgc51fe1e" class="outline-4">
<h4 id="orgc51fe1e"><span class="section-number-4">2.6.4</span> Zero-based versus one-based arrays of indices</h4>
<div class="outline-text-4" id="text-2-6-4">
<p>
Because arrays are zero-based in Fortran, we need to set a flag to
know if we need to shift by 1 arrays of indices.
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span> <span style="color: #0000ff;">trexio_set_one_based</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>)
<span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
file-&gt;one_based = <span style="color: #008b8b;">true</span>;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_set_one_based</span><span style="color: #000000; background-color: #ffffff;">(trex_file) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_set_one_based</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org6d82da2" class="outline-3">
<h3 id="org6d82da2"><span class="section-number-3">2.7</span> File closing</h3>
<div class="outline-text-3" id="text-2-7">
<p>
<code>trexio_close</code> closes an existing <code>trexio_t</code> file.
</p>
<p>
input parameters:
<code>file</code> &#x2013; TREXIO file handle.
</p>
<p>
output:
<code>trexio_exit_code</code> exit code.
</p>
</div>
<div id="outline-container-org380ec63" class="outline-4">
<h4 id="org380ec63"><span class="section-number-4">2.7.1</span> C</h4>
<div class="outline-text-4" id="text-2-7-1">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_close</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_FILE_ERROR;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span> = TREXIO_FAILURE;
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
/* <span style="color: #b22222;">Terminate the back end </span>*/
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_deinit(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = trexio_hdf5_deinit(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_deinit(file);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) {
FREE(file);
<span style="color: #a020f0;">return</span> rc;
}
/* <span style="color: #b22222;">File unlocking </span>*/
rc = TREXIO_UNLOCK_ERROR;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_unlock(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = TREXIO_SUCCESS;
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_unlock(file);</span>
<span style="color: #b22222;"> break;</span>
*/
}
/* <span style="color: #b22222;">Terminate front end </span>*/
<span style="color: #228b22;">int</span> <span style="color: #a0522d;">irc</span> = pthread_mutex_destroy( &amp;(file-&gt;thread_lock) );
free(file);
<span style="color: #a020f0;">if</span> (irc != 0) <span style="color: #a020f0;">return</span> TREXIO_ERRNO;
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> rc;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org819adcf" class="outline-4">
<h4 id="org819adcf"><span class="section-number-4">2.7.2</span> Fortran</h4>
<div class="outline-text-4" id="text-2-7-2">
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_close</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_close</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orga807798" class="outline-4">
<h4 id="orga807798"><span class="section-number-4">2.7.3</span> Python</h4>
<div class="outline-text-4" id="text-2-7-3">
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">close</span>(trexio_file):
<span style="color: #8b2252;">"""Close TREXIO file.</span>
<span style="color: #8b2252;"> Parameter is a ~trexio_file~ object that has been created by a call to ~open~ function.</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span> = pytr.trexio_close(trexio_file)
<span style="color: #a020f0;">if</span> rc != TREXIO_SUCCESS:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="outline-container-orgbf2c7a0" class="outline-2">
<h2 id="orgbf2c7a0"><span class="section-number-2">3</span> Templates for front end</h2>
<div class="outline-text-2" id="text-3">
</div>
<div id="outline-container-org0ab196d" class="outline-3">
<h3 id="org0ab196d"><span class="section-number-3">3.1</span> Description</h3>
<div class="outline-text-3" id="text-3-1">
<p>
Consider the following block of <code>trex.json</code>:
</p>
<div class="org-src-container">
<pre class="src src-python">{
<span style="color: #8b2252;">"nucleus"</span>: {
<span style="color: #8b2252;">"num"</span> : [ <span style="color: #8b2252;">"int"</span> , [ ] ]
, <span style="color: #8b2252;">"charge"</span> : [ <span style="color: #8b2252;">"float"</span>, [ <span style="color: #8b2252;">"nucleus.num"</span> ] ]
, <span style="color: #8b2252;">"coord"</span> : [ <span style="color: #8b2252;">"float"</span>, [ <span style="color: #8b2252;">"nucleus.num"</span>, <span style="color: #8b2252;">"3"</span> ] ]
, <span style="color: #8b2252;">"label"</span> : [ <span style="color: #8b2252;">"str"</span> , [ <span style="color: #8b2252;">"nucleus.num"</span> ] ]
, <span style="color: #8b2252;">"point_group"</span> : [ <span style="color: #8b2252;">"str"</span> , [ ] ]
}
}
</pre>
</div>
<p>
<code>TREXIO</code> is generated automatically by the <code>generator.py</code> Python
script based on the tree-like configuration provided in the
<code>trex.json</code> file. Because of that, generalized templates can be
implemented and re-used. This approach minimizes the number of bugs
as compared with manual copy-paste-modify scheme.
</p>
<p>
All templates presented below use the <code>$var$</code> notation to indicate
the variable, which will be replaced by the
<code>generator.py</code>. Sometimes the upper case is used, i.e. <code>$VAR$</code> (for
example, in <code>#define</code> statements). More detailed description of
each variable can be found below:
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Template variable</th>
<th scope="col" class="org-left">Description</th>
<th scope="col" class="org-left">Example</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>$group$</code></td>
<td class="org-left">Name of the group</td>
<td class="org-left"><code>nucleus</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_num$</code></td>
<td class="org-left">Name of the numerical attribute (scalar)</td>
<td class="org-left"><code>nucleus_num</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_str$</code></td>
<td class="org-left">Name of the string attribute (scalar)</td>
<td class="org-left"><code>nucleus_point_group</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset$</code></td>
<td class="org-left">Name of the dataset (vector/matrix/tensor)</td>
<td class="org-left"><code>nucleus_coord</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_rank$</code></td>
<td class="org-left">Rank of the dataset</td>
<td class="org-left"><code>2</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_dim$</code></td>
<td class="org-left">Selected dimension of the dataset</td>
<td class="org-left"><code>nucleus_num</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_dim_list$</code></td>
<td class="org-left">All dimensions of the dataset</td>
<td class="org-left"><code>{nucleus_num, 3}</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_dtype$</code></td>
<td class="org-left">Basic type of the dataset (int/float/char)</td>
<td class="org-left"><code>float</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_h5_dtype$</code></td>
<td class="org-left">Type of the dataset in HDF5</td>
<td class="org-left"><code>double</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_std_dtype_in$</code></td>
<td class="org-left">Input type of the dataset in TEXT [fscanf]</td>
<td class="org-left"><code>%lf</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_std_dtype_out$</code></td>
<td class="org-left">Output type of the dataset in TEXT [fprintf]</td>
<td class="org-left"><code>%24.16e</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_dtype_default$</code></td>
<td class="org-left">Default datatype of the dataset [C]</td>
<td class="org-left"><code>double/int32_t</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_dtype_single$</code></td>
<td class="org-left">Single precision datatype of the dataset [C]</td>
<td class="org-left"><code>float/int32_t</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_dtype_double$</code></td>
<td class="org-left">Double precision datatype of the dataset [C]</td>
<td class="org-left"><code>double/int64_t</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_f_dtype_default$</code></td>
<td class="org-left">Default datatype of the dataset [Fortran]</td>
<td class="org-left"><code>real(8)/integer(4)</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_f_dtype_single$</code></td>
<td class="org-left">Single precision datatype of the dataset [Fortran]</td>
<td class="org-left"><code>real(4)/integer(4)</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_f_dtype_double$</code></td>
<td class="org-left">Double precision datatype of the dataset [Fortran]</td>
<td class="org-left"><code>real(8)/integer(8)</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_f_dims$</code></td>
<td class="org-left">Dimensions in Fortran</td>
<td class="org-left"><code>(:,:)</code></td>
</tr>
<tr>
<td class="org-left"><code>$group_dset_py_dtype$</code></td>
<td class="org-left">Standard datatype of the dataset [Python]</td>
<td class="org-left"><code>float/int</code></td>
</tr>
<tr>
<td class="org-left"><code>$default_prec$</code></td>
<td class="org-left">Default precision for read/write without suffix [C]</td>
<td class="org-left"><code>64/32</code></td>
</tr>
<tr>
<td class="org-left"><code>$is_index$</code></td>
<td class="org-left">Expands to <code>true</code> if dataset has a type <code>index</code> [C]</td>
<td class="org-left"><code>true/false</code></td>
</tr>
</tbody>
</table>
<p>
Some of the aforementioned template variables with <code>group_dset</code> prefix are duplicated with <code>group_num</code> prefix,
e.g. you might find \(group_num_dtype_double\) in the templates corresponding to numerical attributes.
The expanding values are the same as for <code>group_dset</code> and thus are not listed in the table above.
</p>
<p>
Note: parent group name is always added to the child objects upon
construction of TREXIO (e.g. <code>num</code> of <code>nucleus</code> group becomes
<code>nucleus_num</code> and should be accessed accordingly within TREXIO).
</p>
<p>
TREXIO generator parses the <code>trex.json</code> file. TREXIO operates with
names of variables based on the 1-st (parent group) and 2-nd (child
object) levels of <code>trex.json</code> . The parsed data is divided in 2
parts:
</p>
<ol class="org-ol">
<li>Single attributes. These can be numerical values or strings.</li>
<li>Datasets. These can be vectors, matrices or tensors. The types are indicated in <code>trex.json</code>.
Currently supported data types: int, float and strings.</li>
</ol>
<p>
For each of the aforementioned objects, TREXIO provides <b>has</b>,
<b>read</b> and <b>write</b> functionality. TREXIO supports I/O with single
or double precision for integer and floating point numbers.
</p>
<p>
<b>Note:</b> single integer attributes that contain <code>num</code> in their name (e.g. <code>nucleus_num</code>) are
considered dimensioning variables and cannot be negative or 0. An attempt to write negative or 0
value will result in <code>TREXIO_INVALID_ARG_2</code> exit code.
</p>
</div>
</div>
<div id="outline-container-org9c9274d" class="outline-3">
<h3 id="org9c9274d"><span class="section-number-3">3.2</span> Templates for front end has/read/write a single numerical attribute</h3>
<div class="outline-text-3" id="text-3-2">
</div>
<div id="outline-container-org2ddbed8" class="outline-4">
<h4 id="org2ddbed8"><span class="section-number-4">3.2.1</span> Introduction</h4>
<div class="outline-text-4" id="text-3-2-1">
<p>
This section concerns API calls related to numerical attributes,
namely single value of int/float types.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Function name</th>
<th scope="col" class="org-left">Description</th>
<th scope="col" class="org-left">Precision</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>trexio_has_$group_num$</code></td>
<td class="org-left">Check if an attribute exists in a file</td>
<td class="org-left">---</td>
</tr>
<tr>
<td class="org-left"><code>trexio_read_$group_num$</code></td>
<td class="org-left">Read a attribute</td>
<td class="org-left">Single</td>
</tr>
<tr>
<td class="org-left"><code>trexio_write_$group_num$</code></td>
<td class="org-left">Write a attribute</td>
<td class="org-left">Single</td>
</tr>
<tr>
<td class="org-left"><code>trexio_read_$group_num$_32</code></td>
<td class="org-left">Read a attribute</td>
<td class="org-left">Single</td>
</tr>
<tr>
<td class="org-left"><code>trexio_write_$group_num$_32</code></td>
<td class="org-left">Write a attribute</td>
<td class="org-left">Single</td>
</tr>
<tr>
<td class="org-left"><code>trexio_read_$group_num$_64</code></td>
<td class="org-left">Read a attribute</td>
<td class="org-left">Double</td>
</tr>
<tr>
<td class="org-left"><code>trexio_write_$group_num$_64</code></td>
<td class="org-left">Write a attribute</td>
<td class="org-left">Double</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-org5855868" class="outline-4">
<h4 id="org5855868"><span class="section-number-4">3.2.2</span> C templates for front end</h4>
<div class="outline-text-4" id="text-3-2-2">
<p>
The <code>C</code> templates that correspond to each of the abovementioned
functions can be found below. First parameter is the <code>TREXIO</code> file
handle. Second parameter is the variable to be written/read
to/from the <code>TREXIO</code> file (except for <code>trexio_has_</code> functions).
Suffixes <code>_32</code> and <code>_64</code> correspond to API calls dealing with
single and double precision, respectively. The basic
(non-suffixed) API call on dimensioning variables deals with single
precision (see Table above).
</p>
</div>
<div id="outline-container-org9b094fb" class="outline-5">
<h5 id="org9b094fb"><span class="section-number-5">3.2.2.1</span> Function declarations</h5>
</div>
<div id="outline-container-orga838cc6" class="outline-5">
<h5 id="orga838cc6"><span class="section-number-5">3.2.2.2</span> Source code for double precision functions</h5>
<div class="outline-text-5" id="text-3-2-2-2">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_$group_num$_64</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_num_dtype_double$* <span style="color: #a020f0;">const</span> num)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (trexio_has_$group_num$(file) != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_ATTR_MISSING;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_read_$group_num$(file, num);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> trexio_hdf5_read_$group_num$(file, num);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> return trexio_json_read_$group_num$(file, num);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_$group_num$_64</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_num_dtype_double$ num)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
//<span style="color: #b22222;">if (num &lt;= 0L) return TREXIO_INVALID_NUM; /* this line is uncommented by the generator for dimensioning variables; do NOT remove! */</span>
<span style="color: #a020f0;">if</span> (trexio_has_$group_num$(file) == TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_ATTR_ALREADY_EXISTS;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_write_$group_num$(file, num);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> trexio_hdf5_write_$group_num$(file, num);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON: </span>
<span style="color: #b22222;"> return trexio_json_write_$group_num$(file, num);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgfdfade3" class="outline-5">
<h5 id="orgfdfade3"><span class="section-number-5">3.2.2.3</span> Source code for single precision functions</h5>
<div class="outline-text-5" id="text-3-2-2-3">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_$group_num$_32</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_num_dtype_single$* <span style="color: #a020f0;">const</span> num)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (trexio_has_$group_num$(file) != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_ATTR_MISSING;
$group_num_dtype_double$ num_64 = 0;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span> = TREXIO_GROUP_READ_ERROR;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_read_$group_num$(file, &amp;num_64);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = trexio_hdf5_read_$group_num$(file, &amp;num_64);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc =trexio_json_read_$group_num$(file, &amp;num_64);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> rc;
*num = ($group_num_dtype_single$) num_64;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_$group_num$_32</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_num_dtype_single$ num)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
//<span style="color: #b22222;">if (num &lt;= 0) return TREXIO_INVALID_NUM; /* this line is uncommented by the generator for dimensioning variables; do NOT remove! */</span>
<span style="color: #a020f0;">if</span> (trexio_has_$group_num$(file) == TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_ATTR_ALREADY_EXISTS;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_write_$group_num$(file, ($group_num_dtype_double$) num);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> trexio_hdf5_write_$group_num$(file, ($group_num_dtype_double$) num);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON: </span>
<span style="color: #b22222;"> return trexio_json_write_$group_num$(file, ($group_num_dtype_double$) num);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orge895d2f" class="outline-5">
<h5 id="orge895d2f"><span class="section-number-5">3.2.2.4</span> Source code for default functions</h5>
<div class="outline-text-5" id="text-3-2-2-4">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_$group_num$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_num_dtype_default$* <span style="color: #a020f0;">const</span> num)
{
<span style="color: #a020f0;">return</span> trexio_read_$group_num$_$default_prec$(file, num);
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_$group_num$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_num_dtype_default$ num)
{
<span style="color: #a020f0;">return</span> trexio_write_$group_num$_$default_prec$(file, num);
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_has_$group_num$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_has_$group_num$(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> trexio_hdf5_has_$group_num$(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> return trexio_json_has_$group_num$(file);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org2be5a57" class="outline-4">
<h4 id="org2be5a57"><span class="section-number-4">3.2.3</span> Fortran templates for front end</h4>
<div class="outline-text-4" id="text-3-2-3">
<p>
The <code>Fortran</code> templates that provide an access to the <code>C</code> API calls from Fortran.
These templates are based on the use of <code>iso_c_binding</code>. Pointers have to be passed by value.
</p>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_write_$group_num$_64</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, num) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_num_f_dtype_double$, <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> :: num
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_write_$group_num$_64</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_read_$group_num$_64</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, num) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_num_f_dtype_double$, <span style="color: #a020f0;">intent</span>(out) :: num
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_read_$group_num$_64</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_write_$group_num$_32</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, num) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_num_f_dtype_single$, <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> :: num
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_write_$group_num$_32</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_read_$group_num$_32</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, num) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_num_f_dtype_single$, <span style="color: #a020f0;">intent</span>(out) :: num
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_read_$group_num$_32</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_write_$group_num$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, num) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_num_f_dtype_default$, <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> :: num
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_write_$group_num$</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_read_$group_num$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, num) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_num_f_dtype_default$, <span style="color: #a020f0;">intent</span>(out) :: num
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_read_$group_num$</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_has_$group_num$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_has_$group_num$</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgb5aee71" class="outline-4">
<h4 id="orgb5aee71"><span class="section-number-4">3.2.4</span> Python templates for front end</h4>
<div class="outline-text-4" id="text-3-2-4">
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">write_</span>$group_num$(trexio_file, num_w: $group_num_py_dtype$) -&gt; <span style="color: #008b8b;">None</span>:
<span style="color: #8b2252;">"""Write the $group_num$ variable in the TREXIO file.</span>
<span style="color: #8b2252;"> Parameters:</span>
<span style="color: #8b2252;"> trexio_file:</span>
<span style="color: #8b2252;"> TREXIO File object.</span>
<span style="color: #8b2252;"> num_w: int</span>
<span style="color: #8b2252;"> Value of the $group_num$ variable to be written.</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from AssertionError if TREXIO return code ~rc~ is different from TREXIO_SUCCESS and prints the error message using trexio_string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span> = pytr.trexio_write_$group_num$(trexio_file.pytrexio_s, num_w)
<span style="color: #a020f0;">if</span> rc != TREXIO_SUCCESS:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">read_</span>$group_num$(trexio_file) -&gt; $group_num_py_dtype$:
<span style="color: #8b2252;">"""Read the $group_num$ variable from the TREXIO file.</span>
<span style="color: #8b2252;"> Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.</span>
<span style="color: #8b2252;"> Returns:</span>
<span style="color: #8b2252;"> ~num_r~: int</span>
<span style="color: #8b2252;"> Integer value of $group_num$ variable read from ~trexio_file~.</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from AssertionError if TREXIO return code ~rc~ is different from TREXIO_SUCCESS and prints the error message using trexio_string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span>, <span style="color: #a0522d;">num_r</span> = pytr.trexio_read_$group_num$(trexio_file.pytrexio_s)
<span style="color: #a020f0;">if</span> rc != TREXIO_SUCCESS:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a020f0;">return</span> num_r
</pre>
</div>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">has_</span>$group_num$(trexio_file) -&gt; <span style="color: #483d8b;">bool</span>:
<span style="color: #8b2252;">"""Check that $group_num$ variable exists in the TREXIO file.</span>
<span style="color: #8b2252;"> Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.</span>
<span style="color: #8b2252;"> Returns:</span>
<span style="color: #8b2252;"> True if the variable exists, False otherwise</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from trexio.Error class if TREXIO return code ~rc~ is TREXIO_FAILURE and prints the error message using string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span> = pytr.trexio_has_$group_num$(trexio_file.pytrexio_s)
<span style="color: #a020f0;">if</span> rc == TREXIO_FAILURE:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a020f0;">if</span> rc == TREXIO_SUCCESS:
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">True</span>
<span style="color: #a020f0;">else</span>:
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">False</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgb466703" class="outline-3">
<h3 id="orgb466703"><span class="section-number-3">3.3</span> Templates for front end has/read/write a dataset of numerical data</h3>
<div class="outline-text-3" id="text-3-3">
</div>
<div id="outline-container-org4d8ad8c" class="outline-4">
<h4 id="org4d8ad8c"><span class="section-number-4">3.3.1</span> Introduction</h4>
<div class="outline-text-4" id="text-3-3-1">
<p>
This section concerns API calls related to datasets.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Function name</th>
<th scope="col" class="org-left">Description</th>
<th scope="col" class="org-left">Precision</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>trexio_has_$group_dset$</code></td>
<td class="org-left">Check if a dataset exists in a file</td>
<td class="org-left">---</td>
</tr>
<tr>
<td class="org-left"><code>trexio_read_$group_dset$</code></td>
<td class="org-left">Read a dataset in default precision</td>
<td class="org-left">Double/Single for float/int</td>
</tr>
<tr>
<td class="org-left"><code>trexio_write_$group_dset$</code></td>
<td class="org-left">Write a dataset in default precision</td>
<td class="org-left">Double/Single for float/int</td>
</tr>
<tr>
<td class="org-left"><code>trexio_read_safe_$group_dset$</code></td>
<td class="org-left">Read a bounded dataset</td>
<td class="org-left">Double</td>
</tr>
<tr>
<td class="org-left"><code>trexio_write_safe_$group_dset$</code></td>
<td class="org-left">Write a bounded dataset</td>
<td class="org-left">Double</td>
</tr>
<tr>
<td class="org-left"><code>trexio_read_$group_dset$_32</code></td>
<td class="org-left">Read a dataset in single precision</td>
<td class="org-left">Single</td>
</tr>
<tr>
<td class="org-left"><code>trexio_write_$group_dset$_32</code></td>
<td class="org-left">Write a dataset in single precision</td>
<td class="org-left">Single</td>
</tr>
<tr>
<td class="org-left"><code>trexio_read_$group_dset$_64</code></td>
<td class="org-left">Read a dataset in double precision</td>
<td class="org-left">Double</td>
</tr>
<tr>
<td class="org-left"><code>trexio_write_$group_dset$_64</code></td>
<td class="org-left">Write a dataset in double precision</td>
<td class="org-left">Double</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-org4bc1723" class="outline-4">
<h4 id="org4bc1723"><span class="section-number-4">3.3.2</span> C templates for front end</h4>
<div class="outline-text-4" id="text-3-3-2">
<p>
The C templates that correspond to each of the abovementioned functions can be found below.
First parameter is the <code>TREXIO</code> file handle. Second parameter is the variable to be written/read
to/from the <code>TREXIO</code> file (except for <code>trexio_has_</code> functions).
Suffixes <code>_32</code> and <code>_64</code> correspond to API calls dealing with single and double precision, respectively.
The basic (non-suffixed) API call on datasets deals with double precision (see Table above).
</p>
</div>
<div id="outline-container-org0f77224" class="outline-5">
<h5 id="org0f77224"><span class="section-number-5">3.3.2.1</span> Function declarations</h5>
</div>
<div id="outline-container-org187f1c3" class="outline-5">
<h5 id="org187f1c3"><span class="section-number-5">3.3.2.2</span> Source code for double precision functions</h5>
<div class="outline-text-5" id="text-3-3-2-2">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_$group_dset$_64</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_dset_dtype_double$* <span style="color: #a020f0;">const</span> $group_dset$)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> ($group_dset$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_MISSING;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
rc = TREXIO_FAILURE;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_read_$group_dset$(file, $group_dset$, rank, dims);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = trexio_hdf5_read_$group_dset$(file, $group_dset$, rank, dims);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_read_$group_dset$(file, $group_dset$, rank, dims);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> rc;
/* <span style="color: #b22222;">Handle index type </span>*/
<span style="color: #a020f0;">if</span> ($is_index$) {
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;rank; ++i){
dim_size *= dims[i];
}
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;dim_size; ++i){
$group_dset$[i] += ($group_dset_dtype_single$) 1;
}
}
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_$group_dset$_64</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_dset_dtype_double$* $group_dset$)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> ($group_dset$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_ALREADY_EXISTS;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
$group_dset_dtype_double$* $group_dset$_p = ($group_dset_dtype_double$*) $group_dset$;
/* <span style="color: #b22222;">Handle index type </span>*/
<span style="color: #a020f0;">if</span> ($is_index$) {
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;rank; ++i){
dim_size *= dims[i];
}
$group_dset$_p = CALLOC(dim_size, $group_dset_dtype_double$);
<span style="color: #a020f0;">if</span> ($group_dset$_p == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_ALLOCATION_FAILED;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;dim_size; ++i){
$group_dset$_p[i] = $group_dset$_p[i] - ($group_dset_dtype_single$) 1;
}
}
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
rc = TREXIO_FAILURE;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_write_$group_dset$(file, $group_dset$_p, rank, dims);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = trexio_hdf5_write_$group_dset$(file, $group_dset$_p, rank, dims);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_write_$group_dset$(file, $group_dset$_p, rank, dims);</span>
<span style="color: #b22222;"> break;</span>
*/
}
/* <span style="color: #b22222;">Handle index type </span>*/
<span style="color: #a020f0;">if</span> ($is_index$) {
FREE($group_dset$_p);
}
<span style="color: #a020f0;">return</span> rc;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org6196890" class="outline-5">
<h5 id="org6196890"><span class="section-number-5">3.3.2.3</span> Source code for single precision functions</h5>
<div class="outline-text-5" id="text-3-3-2-3">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_$group_dset$_32</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_dset_dtype_single$* <span style="color: #a020f0;">const</span> $group_dset$)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> ($group_dset$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_MISSING;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;rank; ++i){
dim_size *= dims[i];
}
$group_dset_dtype_double$* $group_dset$_64 = CALLOC(dim_size, $group_dset_dtype_double$);
<span style="color: #a020f0;">if</span> ($group_dset$_64 == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_ALLOCATION_FAILED;
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
rc = TREXIO_FAILURE;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_read_$group_dset$(file, $group_dset$_64, rank, dims);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = trexio_hdf5_read_$group_dset$(file, $group_dset$_64, rank, dims);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_read_$group_dset$(file, $group_dset$_64, rank, dims);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS){
FREE($group_dset$_64);
<span style="color: #a020f0;">return</span> rc;
}
<span style="color: #a020f0;">if</span> ($is_index$) {
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;dim_size; ++i){
$group_dset$[i] = ($group_dset_dtype_single$) $group_dset$_64[i] + ($group_dset_dtype_single$) 1;
}
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;dim_size; ++i){
$group_dset$[i] = ($group_dset_dtype_single$) $group_dset$_64[i];
}
}
FREE($group_dset$_64);
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_$group_dset$_32</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_dset_dtype_single$* $group_dset$)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> ($group_dset$ == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_ALREADY_EXISTS;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;rank; ++i){
dim_size *= dims[i];
}
$group_dset_dtype_double$* $group_dset$_64 = CALLOC(dim_size, $group_dset_dtype_double$);
<span style="color: #a020f0;">if</span> ($group_dset$_64 == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_ALLOCATION_FAILED;
/* <span style="color: #b22222;">A type conversion from single precision to double reqired since back end only accepts 64-bit data </span>*/
<span style="color: #a020f0;">if</span> ($is_index$) {
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;dim_size; ++i){
$group_dset$_64[i] = ($group_dset_dtype_double$) $group_dset$[i] - ($group_dset_dtype_double$) 1;
}
} <span style="color: #a020f0;">else</span> {
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;dim_size; ++i){
$group_dset$_64[i] = ($group_dset_dtype_double$) $group_dset$[i];
}
}
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
rc = TREXIO_FAILURE;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_write_$group_dset$(file, $group_dset$_64, rank, dims);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = trexio_hdf5_write_$group_dset$(file, $group_dset$_64, rank, dims);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_write_$group_dset$(file, $group_dset$_64, rank, dims);</span>
<span style="color: #b22222;"> break;</span>
*/
}
FREE($group_dset$_64);
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> rc;
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org50534bf" class="outline-5">
<h5 id="org50534bf"><span class="section-number-5">3.3.2.4</span> Source code for memory-safe functions</h5>
<div class="outline-text-5" id="text-3-3-2-4">
<div class="org-src-container">
<pre class="src src-c" id="org18dd0c9"><span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
/* <span style="color: #b22222;">The block below is specific to safe API as it checks the boundaries </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;rank; ++i){
dim_size *= dims[i];
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_safe_$group_dset$_32</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_dset_dtype_single$* <span style="color: #a020f0;">const</span> dset_out, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">dim_out</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (dset_out == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_MISSING;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
/* <span style="color: #b22222;">The block below is specific to safe API as it checks the boundaries </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;rank; ++i){
dim_size *= dims[i];
}
<span style="color: #a020f0;">if</span> (dim_out &gt; (<span style="color: #228b22;">int64_t</span>) dim_size) <span style="color: #a020f0;">return</span> TREXIO_UNSAFE_ARRAY_DIM;
<span style="color: #a020f0;">return</span> trexio_read_$group_dset$_32(file, dset_out);
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_safe_$group_dset$_32</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_dset_dtype_single$* dset_in, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">dim_in</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (dset_in == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_ALREADY_EXISTS;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
/* <span style="color: #b22222;">The block below is specific to safe API as it checks the boundaries </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;rank; ++i){
dim_size *= dims[i];
}
<span style="color: #a020f0;">if</span> (dim_in &gt; (<span style="color: #228b22;">int64_t</span>) dim_size) <span style="color: #a020f0;">return</span> TREXIO_UNSAFE_ARRAY_DIM;
<span style="color: #a020f0;">return</span> trexio_write_$group_dset$_32(file, dset_in);
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_safe_$group_dset$_64</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_dset_dtype_double$* <span style="color: #a020f0;">const</span> dset_out, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">dim_out</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (dset_out == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_MISSING;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
/* <span style="color: #b22222;">The block below is specific to safe API as it checks the boundaries </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;rank; ++i){
dim_size *= dims[i];
}
<span style="color: #a020f0;">if</span> (dim_out &gt; (<span style="color: #228b22;">int64_t</span>) dim_size) <span style="color: #a020f0;">return</span> TREXIO_UNSAFE_ARRAY_DIM;
<span style="color: #a020f0;">return</span> trexio_read_$group_dset$_64(file, dset_out);
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_safe_$group_dset$_64</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_dset_dtype_double$* dset_in, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">dim_in</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (dset_in == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_ALREADY_EXISTS;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
/* <span style="color: #b22222;">The block below is specific to safe API as it checks the boundaries </span>*/
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dim_size</span> = 1;
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;rank; ++i){
dim_size *= dims[i];
}
<span style="color: #a020f0;">if</span> (dim_in &gt; (<span style="color: #228b22;">int64_t</span>) dim_size) <span style="color: #a020f0;">return</span> TREXIO_UNSAFE_ARRAY_DIM;
<span style="color: #a020f0;">return</span> trexio_write_$group_dset$_64(file, dset_in);
}
</pre>
</div>
</div>
</div>
<div id="outline-container-orgbdf2c84" class="outline-5">
<h5 id="orgbdf2c84"><span class="section-number-5">3.3.2.5</span> Source code for default functions</h5>
<div class="outline-text-5" id="text-3-3-2-5">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_safe_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_dset_dtype_default$* <span style="color: #a020f0;">const</span> $group_dset$, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">dim_out</span>)
{
<span style="color: #a020f0;">return</span> trexio_read_safe_$group_dset$_$default_prec$(file, $group_dset$, dim_out);
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_safe_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_dset_dtype_default$* $group_dset$, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">dim_in</span>)
{
<span style="color: #a020f0;">return</span> trexio_write_safe_$group_dset$_$default_prec$(file, $group_dset$, dim_in);
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, $group_dset_dtype_default$* <span style="color: #a020f0;">const</span> $group_dset$)
{
<span style="color: #a020f0;">return</span> trexio_read_$group_dset$_$default_prec$(file, $group_dset$);
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> $group_dset_dtype_default$* $group_dset$)
{
<span style="color: #a020f0;">return</span> trexio_write_$group_dset$_$default_prec$(file, $group_dset$);
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_has_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_has_$group_dset$(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> trexio_hdf5_has_$group_dset$(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> return trexio_json_has_$group_dset$(file);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org67dd1ad" class="outline-4">
<h4 id="org67dd1ad"><span class="section-number-4">3.3.3</span> Fortran templates for front end</h4>
<div class="outline-text-4" id="text-3-3-3">
<p>
The <code>Fortran</code> templates that provide an access to the <code>C</code> API calls from <code>Fortran</code>.
These templates are based on the use of <code>iso_c_binding</code>. Pointers have to be passed by value.
</p>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_write_$group_dset$_64</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, dset) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_dset_f_dtype_double$, <span style="color: #a020f0;">intent</span>(in) :: dset$group_dset_f_dims$
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_write_$group_dset$_64</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_read_$group_dset$_64</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, dset) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_dset_f_dtype_double$, <span style="color: #a020f0;">intent</span>(out) :: dset$group_dset_f_dims$
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_read_$group_dset$_64</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_write_$group_dset$_32</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, dset) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_dset_f_dtype_single$, <span style="color: #a020f0;">intent</span>(in) :: dset$group_dset_f_dims$
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_write_$group_dset$_32</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_read_$group_dset$_32</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, dset) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_dset_f_dtype_single$, <span style="color: #a020f0;">intent</span>(out) :: dset$group_dset_f_dims$
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_read_$group_dset$_32</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_write_$group_dset$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, dset) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_dset_f_dtype_default$, <span style="color: #a020f0;">intent</span>(in) :: dset$group_dset_f_dims$
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_write_$group_dset$</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_read_$group_dset$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, dset) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
$group_dset_f_dtype_default$, <span style="color: #a020f0;">intent</span>(out) :: dset$group_dset_f_dims$
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_read_$group_dset$</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_has_$group_dset$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_has_$group_dset$</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgb4539f0" class="outline-4">
<h4 id="orgb4539f0"><span class="section-number-4">3.3.4</span> Python templates for front end</h4>
<div class="outline-text-4" id="text-3-3-4">
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">write_</span>$group_dset$(trexio_file, dset_w) -&gt; <span style="color: #008b8b;">None</span>:
<span style="color: #8b2252;">"""Write the $group_dset$ array of numbers in the TREXIO file.</span>
<span style="color: #8b2252;"> Parameters:</span>
<span style="color: #8b2252;"> trexio_file:</span>
<span style="color: #8b2252;"> TREXIO File object.</span>
<span style="color: #8b2252;"> dset_w: list OR numpy.ndarray</span>
<span style="color: #8b2252;"> Array of $group_dset$ values to be written. If array data type does not correspond to int64 or float64, the conversion is performed.</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from AssertionError if TREXIO return code ~rc~ is different from TREXIO_SUCCESS and prints the error message using trexio_string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a020f0;">import</span> numpy <span style="color: #a020f0;">as</span> np
<span style="color: #a020f0;">except</span> <span style="color: #228b22;">ImportError</span>:
<span style="color: #a020f0;">raise</span> <span style="color: #228b22;">Exception</span>(<span style="color: #8b2252;">"NumPy cannot be imported."</span>)
<span style="color: #a0522d;">doConversion</span> = <span style="color: #008b8b;">False</span>
<span style="color: #a0522d;">doFlatten</span> = <span style="color: #008b8b;">False</span>
<span style="color: #a020f0;">if</span> <span style="color: #a020f0;">not</span> <span style="color: #483d8b;">isinstance</span>(dset_w, (<span style="color: #483d8b;">list</span>, <span style="color: #483d8b;">tuple</span>)):
# <span style="color: #b22222;">if input array is not a list or tuple then it is probably a numpy array</span>
<span style="color: #a020f0;">if</span> <span style="color: #483d8b;">isinstance</span>(dset_w, np.ndarray) <span style="color: #a020f0;">and</span> (<span style="color: #a020f0;">not</span> dset_w.dtype==np.int64 <span style="color: #a020f0;">or</span> <span style="color: #a020f0;">not</span> dset_w.dtype==np.float64):
<span style="color: #a0522d;">doConversion</span> = <span style="color: #008b8b;">True</span>
<span style="color: #a020f0;">if</span> <span style="color: #483d8b;">len</span>(dset_w.shape) &gt; 1:
<span style="color: #a0522d;">doFlatten</span> = <span style="color: #008b8b;">True</span>
<span style="color: #a020f0;">if</span> doConversion:
<span style="color: #a0522d;">dset_64</span> = np.$group_dset_py_dtype$64(dset_w).flatten()
<span style="color: #a020f0;">else</span>:
<span style="color: #a0522d;">dset_flat</span> = np.array(dset_w, dtype=np.$group_dset_py_dtype$64).flatten()
<span style="color: #a020f0;">else</span>:
<span style="color: #a020f0;">if</span> doConversion:
<span style="color: #a0522d;">dset_64</span> = np.$group_dset_py_dtype$64(dset_w)
<span style="color: #a020f0;">else</span>:
# <span style="color: #b22222;">if input array is a multidimensional list or tuple, we have to convert it</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">doFlatten</span> = <span style="color: #008b8b;">True</span>
<span style="color: #a0522d;">ncol</span> = <span style="color: #483d8b;">len</span>(dset_w[0])
<span style="color: #a0522d;">dset_flat</span> = np.array(dset_w, dtype=np.$group_dset_py_dtype$64).flatten()
<span style="color: #a020f0;">except</span> <span style="color: #228b22;">TypeError</span>:
<span style="color: #a0522d;">doFlatten</span> = <span style="color: #008b8b;">False</span>
<span style="color: #a020f0;">pass</span>
<span style="color: #a020f0;">if</span> doConversion:
<span style="color: #a0522d;">rc</span> = pytr.trexio_write_safe_$group_dset$_64(trexio_file.pytrexio_s, dset_64)
<span style="color: #a020f0;">elif</span> doFlatten:
<span style="color: #a0522d;">rc</span> = pytr.trexio_write_safe_$group_dset$_64(trexio_file.pytrexio_s, dset_flat)
<span style="color: #a020f0;">else</span>:
<span style="color: #a0522d;">rc</span> = pytr.trexio_write_safe_$group_dset$_64(trexio_file.pytrexio_s, dset_w)
<span style="color: #a020f0;">if</span> rc != TREXIO_SUCCESS:
<span style="color: #a020f0;">raise</span> Error(rc)
</pre>
</div>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">read_</span>$group_dset$(trexio_file, dim = <span style="color: #008b8b;">None</span>, doReshape = <span style="color: #008b8b;">None</span>, dtype = <span style="color: #008b8b;">None</span>):
<span style="color: #8b2252;">"""Read the $group_dset$ array of numbers from the TREXIO file.</span>
<span style="color: #8b2252;"> Parameters:</span>
<span style="color: #8b2252;"> trexio_file:</span>
<span style="color: #8b2252;"> TREXIO File object.</span>
<span style="color: #8b2252;"> dim (Optional): int</span>
<span style="color: #8b2252;"> Size of the block to be read from the file (i.e. how many items of $group_dset$ will be returned)</span>
<span style="color: #8b2252;"> If None, the function will read all necessary array dimensions from the file.</span>
<span style="color: #8b2252;"> dtype (Optional): type</span>
<span style="color: #8b2252;"> NumPy data type of the output (e.g. np.int32|int16 or np.float32|float16). If specified, the output array will be converted from the default double precision.</span>
<span style="color: #8b2252;"> doReshape (Optional): bool</span>
<span style="color: #8b2252;"> Flag to determine whether the output NumPy array has be reshaped or not. Be default, reshaping is performed</span>
<span style="color: #8b2252;"> based on the dimensions from the ~trex.json~ file. Otherwise, ~shape~ array (list or tuple) is used if provided by the user.</span>
<span style="color: #8b2252;"> Returns:</span>
<span style="color: #8b2252;"> ~dset_64~ if dtype is None or ~dset_converted~ otherwise: numpy.ndarray </span>
<span style="color: #8b2252;"> 1D NumPy array with ~dim~ elements corresponding to $group_dset$ values read from the TREXIO file.</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from AssertionError if TREXIO return code ~rc~ is different from TREXIO_SUCCESS and prints the error message using trexio_string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;">"""</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a020f0;">import</span> numpy <span style="color: #a020f0;">as</span> np
<span style="color: #a020f0;">except</span> <span style="color: #228b22;">ImportError</span>:
<span style="color: #a020f0;">raise</span> <span style="color: #228b22;">Exception</span>(<span style="color: #8b2252;">"NumPy cannot be imported."</span>)
<span style="color: #a020f0;">if</span> doReshape <span style="color: #a020f0;">is</span> <span style="color: #008b8b;">None</span>:
<span style="color: #a0522d;">doReshape</span> = <span style="color: #008b8b;">True</span>
# <span style="color: #b22222;">if dim is not specified, read dimensions from the TREXIO file</span>
<span style="color: #a0522d;">dims_list</span> = <span style="color: #008b8b;">None</span>
<span style="color: #a020f0;">if</span> dim <span style="color: #a020f0;">is</span> <span style="color: #008b8b;">None</span> <span style="color: #a020f0;">or</span> doReshape:
$group_dset_dim$ = read_$group_dset_dim$(trexio_file)
<span style="color: #a0522d;">dims_list</span> = [$group_dset_dim_list$]
<span style="color: #a0522d;">dim</span> = 1
<span style="color: #a020f0;">for</span> i <span style="color: #a020f0;">in</span> <span style="color: #483d8b;">range</span>($group_dset_rank$):
<span style="color: #a0522d;">dim</span> *= dims_list[i]
<span style="color: #a0522d;">shape</span> = <span style="color: #483d8b;">tuple</span>(dims_list)
<span style="color: #a020f0;">if</span> shape <span style="color: #a020f0;">is</span> <span style="color: #008b8b;">None</span> <span style="color: #a020f0;">and</span> doReshape:
<span style="color: #a020f0;">raise</span> <span style="color: #228b22;">ValueError</span>(<span style="color: #8b2252;">"Reshaping failure: shape is None."</span>)
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span>, <span style="color: #a0522d;">dset_64</span> = pytr.trexio_read_safe_$group_dset$_64(trexio_file.pytrexio_s, dim)
<span style="color: #a020f0;">if</span> rc != TREXIO_SUCCESS:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a0522d;">isConverted</span> = <span style="color: #008b8b;">False</span>
<span style="color: #a0522d;">dset_converted</span> = <span style="color: #008b8b;">None</span>
<span style="color: #a020f0;">if</span> dtype <span style="color: #a020f0;">is</span> <span style="color: #a020f0;">not</span> <span style="color: #008b8b;">None</span>:
<span style="color: #a020f0;">try</span>:
<span style="color: #a020f0;">assert</span> <span style="color: #483d8b;">isinstance</span>(dtype, <span style="color: #483d8b;">type</span>)
<span style="color: #a020f0;">except</span> <span style="color: #228b22;">AssertionError</span>:
<span style="color: #a020f0;">raise</span> <span style="color: #228b22;">TypeError</span>(<span style="color: #8b2252;">"dtype argument has to be an instance of the type class (e.g. np.float32)."</span>)
<span style="color: #a020f0;">if</span> <span style="color: #a020f0;">not</span> dtype==np.int64 <span style="color: #a020f0;">or</span> <span style="color: #a020f0;">not</span> dtype==np.float64:
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">dset_converted</span> = np.array(dset_64, dtype=dtype)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a0522d;">isConverted</span> = <span style="color: #008b8b;">True</span>
# <span style="color: #b22222;">additional assert can be added here to check that read_safe functions returns numpy array of proper dimension</span>
<span style="color: #a020f0;">if</span> doReshape:
<span style="color: #a020f0;">try</span>:
# <span style="color: #b22222;">in-place reshaping did not work so I have to make a copy</span>
<span style="color: #a020f0;">if</span> isConverted:
<span style="color: #a0522d;">dset_reshaped</span> = np.reshape(dset_converted, shape, order=<span style="color: #8b2252;">'C'</span>)
<span style="color: #a020f0;">else</span>:
<span style="color: #a0522d;">dset_reshaped</span> = np.reshape(dset_64, shape, order=<span style="color: #8b2252;">'C'</span>)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a020f0;">if</span> isConverted:
<span style="color: #a020f0;">return</span> dset_converted
<span style="color: #a020f0;">elif</span> doReshape:
<span style="color: #a020f0;">return</span> dset_reshaped
<span style="color: #a020f0;">else</span>:
<span style="color: #a020f0;">return</span> dset_64
</pre>
</div>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">has_</span>$group_dset$(trexio_file) -&gt; <span style="color: #483d8b;">bool</span>:
<span style="color: #8b2252;">"""Check that $group_dset$ variable exists in the TREXIO file.</span>
<span style="color: #8b2252;"> Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.</span>
<span style="color: #8b2252;"> Returns:</span>
<span style="color: #8b2252;"> True if the variable exists, False otherwise</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from trexio.Error class if TREXIO return code ~rc~ is TREXIO_FAILURE and prints the error message using string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span> = pytr.trexio_has_$group_dset$(trexio_file.pytrexio_s)
<span style="color: #a020f0;">if</span> rc == TREXIO_FAILURE:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a020f0;">if</span> rc == TREXIO_SUCCESS:
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">True</span>
<span style="color: #a020f0;">else</span>:
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">False</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org077d1ad" class="outline-3">
<h3 id="org077d1ad"><span class="section-number-3">3.4</span> Sparse data structures</h3>
<div class="outline-text-3" id="text-3-4">
<p>
Sparse data structures are used typically for large tensors such as
two-electron integrals. For example, in the <code>trex.json</code> file sparse
arrays appear as for the <code>eri</code> :
</p>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #8b2252;">"ao_2e_int"</span> : {
<span style="color: #8b2252;">"eri_num"</span> : [ <span style="color: #8b2252;">"int"</span>, [ ] ]
<span style="color: #8b2252;">"eri"</span> : [ <span style="color: #8b2252;">"float sparse"</span>, [ <span style="color: #8b2252;">"ao.num"</span>, <span style="color: #8b2252;">"ao.num"</span>, <span style="color: #8b2252;">"ao.num"</span>, <span style="color: #8b2252;">"ao.num"</span> ] ]
}
</pre>
</div>
<p>
The electron repulsion integral \(\langle ij | kl \rangle\) is
represented as a quartet of integers \((i,j,k,l)\) and a floating
point value.
</p>
<p>
To store \(N\) integrals in the file, we store
</p>
<ul class="org-ul">
<li>An array of quartets of integers</li>
<li>An array of values (floats)</li>
</ul>
<p>
Both arrays have the same size, \(N\), the number of non-zero integrals.
Knowing the maximum dimensions allows to check that the integers are
in a valid range, and also lets the library choose the smallest
integer representation to compress the storage.
</p>
<p>
Fortran uses 1-based array indexing, while C uses 0-based indexing.
Internally, we use a 0-based representation but the Fortran binding
does the appropriate conversion when reading or writing.
</p>
<p>
As the number of integrals to store can be prohibitively large, we
provide the possibility to read/write the integrals in chunks. So the
functions take two extra parameters:
</p>
<ul class="org-ul">
<li><code>offset</code> : the index of the 1st integral we want to read. An
offset of zero implies to read the first integral.</li>
<li><code>num</code> : the number of integrals to read.</li>
</ul>
<p>
We provide a function to read a chunk of indices, and a function to
read a chunk of values, because some users might want to read only
the values of the integrals, or only the indices.
</p>
<p>
Here is an example for the indices:
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_chunk_ao_2e_int_eri_index_32</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">offset</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">num</span>,
<span style="color: #228b22;">int32_t</span>* <span style="color: #a0522d;">buffer</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (offset &lt; 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (num &lt; 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_3;
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = 4; // <span style="color: #b22222;">To be set by generator : number of indices</span>
<span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">nmax</span>; // <span style="color: #b22222;">Max number of integrals</span>
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
rc = trexio_read_ao_2e_int_eri_num(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">file</span>, &amp;nmax);
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> rc;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_read_chunk_ao_2e_int_eri_index(file, buffer, offset, num, rank, nmax);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #a020f0;">return</span> trexio_hdf5_read_chunk_ao_2e_int_eri_index(file, buffer, offset, num, rank, nmax);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">default</span>:
<span style="color: #a020f0;">return</span> TREXIO_FAILURE; /* <span style="color: #b22222;">Impossible case </span>*/
}
}
</pre>
</div>
<p>
For the values,
</p>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_chunk_ao_2e_int_eri_value_64</span>(<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">offset</span>,
<span style="color: #a020f0;">const</span> <span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">num</span>,
<span style="color: #228b22;">double</span>* <span style="color: #a0522d;">buffer</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (offset &lt; 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (num &lt; 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_3;
<span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">nmax</span>; // <span style="color: #b22222;">Max number of integrals</span>
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
rc = trexio_read_ao_2e_int_eri_num(<span style="color: #a020f0;">const</span> <span style="color: #228b22;">file</span>, &amp;nmax);
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> rc;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_read_chunk_ao_2e_int_eri_value(file, buffer, offset, num, nmax);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #a020f0;">return</span> trexio_hdf5_read_chunk_ao_2e_int_eri_index(file, buffer, offset, num, nmax);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">default</span>:
<span style="color: #a020f0;">return</span> TREXIO_FAILURE; /* <span style="color: #b22222;">Impossible case </span>*/
}
}
</pre>
</div>
</div>
</div>
<div id="outline-container-org64f4ae0" class="outline-3">
<h3 id="org64f4ae0"><span class="section-number-3">3.5</span> Templates for front end has/read/write a dataset of strings</h3>
<div class="outline-text-3" id="text-3-5">
</div>
<div id="outline-container-org0c9bf8a" class="outline-4">
<h4 id="org0c9bf8a"><span class="section-number-4">3.5.1</span> Introduction</h4>
<div class="outline-text-4" id="text-3-5-1">
<p>
This section concerns API calls related to datasets of strings.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Function name</th>
<th scope="col" class="org-left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>trexio_has_$group_dset$</code></td>
<td class="org-left">Check if a dataset exists in a file</td>
</tr>
<tr>
<td class="org-left"><code>trexio_read_$group_dset$</code></td>
<td class="org-left">Read a dataset</td>
</tr>
<tr>
<td class="org-left"><code>trexio_write_$group_dset$</code></td>
<td class="org-left">Write a dataset</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-org4bcdc90" class="outline-4">
<h4 id="org4bcdc90"><span class="section-number-4">3.5.2</span> C templates for front end</h4>
<div class="outline-text-4" id="text-3-5-2">
<p>
First parameter is the <code>TREXIO</code> file handle. Second parameter is the variable to be written/read
to/from the <code>TREXIO</code> file (except for <code>trexio_has_</code> functions).
</p>
</div>
<div id="outline-container-orgbba3863" class="outline-5">
<h5 id="orgbba3863"><span class="section-number-5">3.5.2.1</span> Function declarations</h5>
</div>
<div id="outline-container-orgb995b10" class="outline-5">
<h5 id="orgb995b10"><span class="section-number-5">3.5.2.2</span> Source code for default functions</h5>
<div class="outline-text-5" id="text-3-5-2-2">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_$group_dset$_low</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #228b22;">char</span>* <span style="color: #a0522d;">dset_out</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span> <span style="color: #a0522d;">max_str_len</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (dset_out == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (max_str_len &lt;= 0) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_3;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_MISSING;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_read_$group_dset$(file, dset_out, rank, dims, (<span style="color: #228b22;">uint32_t</span>) max_str_len);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> trexio_hdf5_read_$group_dset$(file, dset_out, rank, dims, (<span style="color: #228b22;">uint32_t</span>) max_str_len);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_read_$group_dset$(file, dset_out, rank, dims);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
<span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #228b22;">char</span>** <span style="color: #a0522d;">dset_out</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span> <span style="color: #a0522d;">max_str_len</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (dset_out == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (max_str_len &lt;= 0) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_3;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_MISSING;
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
<span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">dset_dim</span> = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;(dset_dim));
<span style="color: #a020f0;">if</span> (dset_dim == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">char</span>* <span style="color: #a0522d;">str_compiled</span> = CALLOC(dset_dim*(max_str_len+1) + 1, <span style="color: #228b22;">char</span>);
<span style="color: #a020f0;">if</span> (str_compiled == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_ALLOCATION_FAILED;
rc = trexio_read_$group_dset$_low(file, str_compiled, max_str_len);
<span style="color: #a020f0;">if</span> (rc != TREXIO_SUCCESS) {
FREE(str_compiled);
<span style="color: #a020f0;">return</span> rc;
}
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0; i &lt; (<span style="color: #228b22;">uint64_t</span>) dset_dim; i++) {
<span style="color: #228b22;">char</span> * <span style="color: #a0522d;">pch</span>;
pch = i == 0 ? strtok(str_compiled, TREXIO_DELIM) : strtok(<span style="color: #008b8b;">NULL</span>, TREXIO_DELIM) ;
<span style="color: #a020f0;">if</span> (pch == <span style="color: #008b8b;">NULL</span>) {
FREE(str_compiled);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
strcpy(dset_out[i], <span style="color: #8b2252;">""</span>);
strcat(dset_out[i], pch);
}
FREE(str_compiled);
<span style="color: #a020f0;">return</span> TREXIO_SUCCESS;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_$group_dset$_low</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>* <span style="color: #a0522d;">dset_in</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span> <span style="color: #a0522d;">max_str_len</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (dset_in == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (max_str_len &lt;= 0) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_3;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_ALREADY_EXISTS;
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
int64_t $group_dset_dim$ = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;($group_dset_dim$));
<span style="color: #a020f0;">if</span> ($group_dset_dim$ == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">uint32_t</span> <span style="color: #a0522d;">rank</span> = $group_dset_rank$;
<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">dims</span>[$group_dset_rank$] = {$group_dset_dim_list$};
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
<span style="color: #228b22;">char</span>* <span style="color: #a0522d;">tmp_str</span> = CALLOC(dims[0]*(max_str_len+1), <span style="color: #228b22;">char</span>);
<span style="color: #a020f0;">if</span> (tmp_str == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_ALLOCATION_FAILED;
<span style="color: #228b22;">char</span>** <span style="color: #a0522d;">dset_str</span> = CALLOC(dims[0], <span style="color: #228b22;">char</span>*);
<span style="color: #a020f0;">if</span> (dset_str == <span style="color: #008b8b;">NULL</span>) {
FREE(tmp_str);
<span style="color: #a020f0;">return</span> TREXIO_ALLOCATION_FAILED;
}
/* <span style="color: #b22222;">parse the string using strtok </span>*/
<span style="color: #a020f0;">for</span>(<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0; i&lt;dims[0]; i++) {
<span style="color: #228b22;">char</span>* <span style="color: #a0522d;">pch</span>;
pch = i == 0 ? strtok( (<span style="color: #228b22;">char</span>*) dset_in, TREXIO_DELIM) : strtok(<span style="color: #008b8b;">NULL</span>, TREXIO_DELIM) ;
<span style="color: #a020f0;">if</span> (pch == <span style="color: #008b8b;">NULL</span>) {
FREE(dset_str[0]);
FREE(dset_str);
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
<span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">pch_len</span> = strlen(pch) + 1;
<span style="color: #a020f0;">if</span> (pch_len &gt; (<span style="color: #228b22;">size_t</span>) max_str_len) {
FREE(dset_str[0]);
FREE(dset_str);
<span style="color: #a020f0;">return</span> TREXIO_INVALID_STR_LEN;
}
dset_str[i] = tmp_str;
strncpy(tmp_str, pch, pch_len);
tmp_str += pch_len + 1;
}
rc = TREXIO_FAILURE;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
rc = trexio_text_write_$group_dset$(file, (<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>**) dset_str, rank, dims);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
rc = trexio_hdf5_write_$group_dset$(file, (<span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>**) dset_str, rank, dims);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> rc = trexio_json_write_$group_dset$(file, dset, rank, dims);</span>
<span style="color: #b22222;"> break;</span>
*/
}
FREE(dset_str[0]);
FREE(dset_str);
<span style="color: #a020f0;">return</span> rc;
}
<span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>** <span style="color: #a0522d;">dset_in</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span> <span style="color: #a0522d;">max_str_len</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (dset_in == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (max_str_len &lt;= 0) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_3;
<span style="color: #a020f0;">if</span> (trexio_has_$group_dset$(file) == TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_DSET_ALREADY_EXISTS;
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
<span style="color: #228b22;">trexio_exit_code</span> <span style="color: #a0522d;">rc</span>;
<span style="color: #228b22;">int64_t</span> <span style="color: #a0522d;">dset_dim</span> = 0;
/* <span style="color: #b22222;">Error handling for this call is added by the generator </span>*/
rc = trexio_read_$group_dset_dim$_64(file, &amp;(dset_dim));
<span style="color: #a020f0;">if</span> (dset_dim == 0L) <span style="color: #a020f0;">return</span> TREXIO_INVALID_NUM;
<span style="color: #228b22;">char</span>* <span style="color: #a0522d;">str_compiled</span> = CALLOC(dset_dim*max_str_len + 1, <span style="color: #228b22;">char</span>);
<span style="color: #a020f0;">if</span> (str_compiled == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_ALLOCATION_FAILED;
strcpy(str_compiled, <span style="color: #8b2252;">""</span>);
<span style="color: #a020f0;">for</span> (<span style="color: #228b22;">uint64_t</span> <span style="color: #a0522d;">i</span>=0; i &lt; (<span style="color: #228b22;">uint64_t</span>) dset_dim; i++) {
strcat(str_compiled, dset_in[i]);
strcat(str_compiled, TREXIO_DELIM);
}
rc = trexio_write_$group_dset$_low(file, str_compiled, max_str_len);
FREE(str_compiled);
<span style="color: #a020f0;">return</span> rc;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_has_$group_dset$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_has_$group_dset$(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> trexio_hdf5_has_$group_dset$(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> return trexio_json_has_$group_dset$(file);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgba8c1fd" class="outline-4">
<h4 id="orgba8c1fd"><span class="section-number-4">3.5.3</span> Fortran templates for front end</h4>
<div class="outline-text-4" id="text-3-5-3">
<p>
The <code>Fortran</code> templates that provide an access to the <code>C</code> API calls from <code>Fortran</code>.
These templates are based on the use of <code>iso_c_binding</code>. Pointers have to be passed by value.
</p>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_write_$group_dset$_low</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, dset, max_str_len) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #228b22;">character</span>, <span style="color: #a020f0;">intent</span>(in) ::<span style="color: #a0522d;"> dset(*)</span>
<span style="color: #228b22;">integer</span>(4), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_str_len</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_write_$group_dset$_low</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_read_$group_dset$_low</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, dset, max_str_len) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #228b22;">character</span>, <span style="color: #a020f0;">intent</span>(out) ::<span style="color: #a0522d;"> dset(*)</span>
<span style="color: #228b22;">integer</span>(4), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_str_len</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_read_$group_dset$_low</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_has_$group_dset$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_has_$group_dset$</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_read_$group_dset$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, dset, max_str_len)</span>
<span style="color: #a020f0;">implicit</span> <span style="color: #228b22;">none</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #228b22;">integer</span>(4), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_str_len</span>
<span style="color: #228b22;">character</span>(len=*), <span style="color: #a020f0;">intent</span>(inout) ::<span style="color: #a0522d;"> dset(*)</span>
<span style="color: #228b22;">character</span>, <span style="color: #a020f0;">allocatable</span> ::<span style="color: #a0522d;"> str_compiled(:)</span>
<span style="color: #228b22;">integer</span>(8) ::<span style="color: #a0522d;"> $group_dset_dim$</span>
<span style="color: #228b22;">integer</span> ::<span style="color: #a0522d;"> rc</span>
rc = trexio_read_$group_dset_dim$_64(trex_file, $group_dset_dim$)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) trexio_read_$group_dset$ = rc
<span style="color: #a020f0;">allocate</span>(str_compiled($group_dset_dim$*(max_str_len+1)+1))
rc = trexio_read_$group_dset$_low(trex_file, str_compiled, max_str_len)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
<span style="color: #a020f0;">deallocate</span>(str_compiled)
trexio_read_$group_dset$ = rc
<span style="color: #a020f0;">else</span>
<span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_str2strarray</span>(str_compiled, $group_dset_dim$, max_str_len, dset)
<span style="color: #a020f0;">deallocate</span>(str_compiled)
trexio_read_$group_dset$ = TREXIO_SUCCESS
<span style="color: #a020f0;">endif</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_read_$group_dset$</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_write_$group_dset$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, dset, max_str_len)</span>
<span style="color: #a020f0;">implicit</span> <span style="color: #228b22;">none</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #228b22;">integer</span>(4), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_str_len</span>
<span style="color: #228b22;">character</span>(len=*), <span style="color: #a020f0;">intent</span>(in) ::<span style="color: #a0522d;"> dset(*)</span>
<span style="color: #228b22;">character</span>(len=:), <span style="color: #a020f0;">allocatable</span> ::<span style="color: #a0522d;"> str_compiled</span>
<span style="color: #228b22;">integer</span>(8) ::<span style="color: #a0522d;"> $group_dset_dim$</span>
<span style="color: #228b22;">integer</span> ::<span style="color: #a0522d;"> rc</span>
rc = trexio_read_$group_dset_dim$_64(trex_file, $group_dset_dim$)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
trexio_write_$group_dset$ = rc
<span style="color: #a020f0;">else</span>
<span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_strarray2str</span>(dset, $group_dset_dim$, max_str_len, str_compiled)
trexio_write_$group_dset$ = trexio_write_$group_dset$_low(trex_file, str_compiled, max_str_len)
<span style="color: #a020f0;">endif</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_write_$group_dset$</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org93768c6" class="outline-4">
<h4 id="org93768c6"><span class="section-number-4">3.5.4</span> Python templates for front end</h4>
<div class="outline-text-4" id="text-3-5-4">
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">write_</span>$group_dset$(trexio_file, dset_w: <span style="color: #483d8b;">list</span>) -&gt; <span style="color: #008b8b;">None</span>:
<span style="color: #8b2252;">"""Write the $group_dset$ array of strings in the TREXIO file.</span>
<span style="color: #8b2252;"> Parameters:</span>
<span style="color: #8b2252;"> trexio_file:</span>
<span style="color: #8b2252;"> TREXIO File object.</span>
<span style="color: #8b2252;"> dset_w: list</span>
<span style="color: #8b2252;"> Array of $group_dset$ strings to be written.</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from AssertionError if TREXIO return code ~rc~ is different from TREXIO_SUCCESS and prints the error message using trexio_string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a0522d;">max_str_length</span> = <span style="color: #483d8b;">len</span>(<span style="color: #483d8b;">max</span>(dset_w, key=<span style="color: #483d8b;">len</span>)) + 1
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span> = pytr.trexio_write_$group_dset$(trexio_file.pytrexio_s, dset_w, max_str_length)
<span style="color: #a020f0;">if</span> rc != TREXIO_SUCCESS:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">read_</span>$group_dset$(trexio_file, dim = <span style="color: #008b8b;">None</span>) -&gt; <span style="color: #483d8b;">list</span>:
<span style="color: #8b2252;">"""Read the $group_dset$ array of strings from the TREXIO file.</span>
<span style="color: #8b2252;"> Parameters:</span>
<span style="color: #8b2252;"> trexio_file:</span>
<span style="color: #8b2252;"> TREXIO File object.</span>
<span style="color: #8b2252;"> dim (Optional): int</span>
<span style="color: #8b2252;"> Size of the block to be read from the file (i.e. how many items of $group_dset$ will be returned)</span>
<span style="color: #8b2252;"> If None, the function will read all necessary array dimensions from the file.</span>
<span style="color: #8b2252;"> Returns:</span>
<span style="color: #8b2252;"> ~dset_r~: list</span>
<span style="color: #8b2252;"> 1D list with ~dim~ elements corresponding to $group_dset$ strings read from the TREXIO file.</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from AssertionError if TREXIO return code ~rc~ is different from TREXIO_SUCCESS and prints the error message using trexio_string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
# <span style="color: #b22222;">if dim is not specified, read dimensions from the TREXIO file</span>
<span style="color: #a020f0;">if</span> dim <span style="color: #a020f0;">is</span> <span style="color: #008b8b;">None</span>:
$group_dset_dim$ = read_$group_dset_dim$(trexio_file)
<span style="color: #a0522d;">dims_list</span> = [$group_dset_dim_list$]
<span style="color: #a0522d;">dim</span> = 1
<span style="color: #a020f0;">for</span> i <span style="color: #a020f0;">in</span> <span style="color: #483d8b;">range</span>($group_dset_rank$):
<span style="color: #a0522d;">dim</span> *= dims_list[i]
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span>, <span style="color: #a0522d;">dset_1d_r</span> = pytr.trexio_read_$group_dset$_low(trexio_file.pytrexio_s, PYTREXIO_MAX_STR_LENGTH)
<span style="color: #a020f0;">if</span> rc != TREXIO_SUCCESS:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">dset_full</span> = dset_1d_r.split(pytr.TREXIO_DELIM)
<span style="color: #a0522d;">dset_2d_r</span> = [dset_full[i] <span style="color: #a020f0;">for</span> i <span style="color: #a020f0;">in</span> <span style="color: #483d8b;">range</span>(dim) <span style="color: #a020f0;">if</span> dset_full[i]]
<span style="color: #a020f0;">if</span> <span style="color: #a020f0;">not</span> dset_2d_r:
<span style="color: #a020f0;">raise</span> <span style="color: #228b22;">ValueError</span>(f<span style="color: #8b2252;">"Output of {read_$group_dset$.__name__} function cannot be an empty list."</span>)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a020f0;">return</span> dset_2d_r
</pre>
</div>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">has_</span>$group_dset$(trexio_file) -&gt; <span style="color: #483d8b;">bool</span>:
<span style="color: #8b2252;">"""Check that $group_dset$ variable exists in the TREXIO file.</span>
<span style="color: #8b2252;"> Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.</span>
<span style="color: #8b2252;"> Returns:</span>
<span style="color: #8b2252;"> True if the variable exists, False otherwise</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from trexio.Error class if TREXIO return code ~rc~ is TREXIO_FAILURE and prints the error message using string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span> = pytr.trexio_has_$group_dset$(trexio_file.pytrexio_s)
<span style="color: #a020f0;">if</span> rc == TREXIO_FAILURE:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a020f0;">if</span> rc == TREXIO_SUCCESS:
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">True</span>
<span style="color: #a020f0;">else</span>:
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">False</span>
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org286ce2e" class="outline-3">
<h3 id="org286ce2e"><span class="section-number-3">3.6</span> Templates for front end has/read/write a single string attribute</h3>
<div class="outline-text-3" id="text-3-6">
</div>
<div id="outline-container-org71ec410" class="outline-4">
<h4 id="org71ec410"><span class="section-number-4">3.6.1</span> Introduction</h4>
<div class="outline-text-4" id="text-3-6-1">
<p>
This section concerns API calls related to string attributes.
</p>
<table border="2" cellspacing="0" cellpadding="6" rules="groups" frame="hsides">
<colgroup>
<col class="org-left" />
<col class="org-left" />
</colgroup>
<thead>
<tr>
<th scope="col" class="org-left">Function name</th>
<th scope="col" class="org-left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td class="org-left"><code>trexio_has_$group_str$</code></td>
<td class="org-left">Check if a string attribute exists in a file</td>
</tr>
<tr>
<td class="org-left"><code>trexio_read_$group_str$</code></td>
<td class="org-left">Read a string attribute</td>
</tr>
<tr>
<td class="org-left"><code>trexio_write_$group_str$</code></td>
<td class="org-left">Write a string attribute</td>
</tr>
</tbody>
</table>
</div>
</div>
<div id="outline-container-orgfcacb76" class="outline-4">
<h4 id="orgfcacb76"><span class="section-number-4">3.6.2</span> C templates for front end</h4>
<div class="outline-text-4" id="text-3-6-2">
</div>
<div id="outline-container-org46bfa2b" class="outline-5">
<h5 id="org46bfa2b"><span class="section-number-5">3.6.2.1</span> Function declarations</h5>
</div>
<div id="outline-container-org0c5c1d4" class="outline-5">
<h5 id="org0c5c1d4"><span class="section-number-5">3.6.2.2</span> Source code for default functions</h5>
<div class="outline-text-5" id="text-3-6-2-2">
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_read_$group_str$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #228b22;">char</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">str_out</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span> <span style="color: #a0522d;">max_str_len</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (str_out == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (max_str_len &lt;= 0) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_3;
<span style="color: #a020f0;">if</span> (trexio_has_$group_str$(file) != TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_ATTR_MISSING;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_read_$group_str$(file, str_out, (<span style="color: #228b22;">uint32_t</span>) max_str_len);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> trexio_hdf5_read_$group_str$(file, str_out, (<span style="color: #228b22;">uint32_t</span>) max_str_len);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> return trexio_json_read_$group_str$(file, str);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_write_$group_str$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">char</span>* <span style="color: #a0522d;">str</span>, <span style="color: #a020f0;">const</span> <span style="color: #228b22;">int32_t</span> <span style="color: #a0522d;">max_str_len</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
<span style="color: #a020f0;">if</span> (str == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_2;
<span style="color: #a020f0;">if</span> (max_str_len &lt;= 0) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_3;
<span style="color: #a020f0;">if</span> (trexio_has_$group_str$(file) == TREXIO_SUCCESS) <span style="color: #a020f0;">return</span> TREXIO_ATTR_ALREADY_EXISTS;
<span style="color: #228b22;">size_t</span> <span style="color: #a0522d;">len_write</span> = strlen(str);
<span style="color: #a020f0;">if</span> ((<span style="color: #228b22;">size_t</span>) max_str_len &lt; len_write) <span style="color: #a020f0;">return</span> TREXIO_INVALID_STR_LEN;
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_write_$group_str$(file, str);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> trexio_hdf5_write_$group_str$(file, str);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> return trexio_json_write_$group_str$(file, str);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
</pre>
</div>
<div class="org-src-container">
<pre class="src src-c"><span style="color: #228b22;">trexio_exit_code</span>
<span style="color: #0000ff;">trexio_has_$group_str$</span> (<span style="color: #228b22;">trexio_t</span>* <span style="color: #a020f0;">const</span> <span style="color: #a0522d;">file</span>)
{
<span style="color: #a020f0;">if</span> (file == <span style="color: #008b8b;">NULL</span>) <span style="color: #a020f0;">return</span> TREXIO_INVALID_ARG_1;
assert(file-&gt;back_end &lt; TREXIO_INVALID_BACK_END);
<span style="color: #a020f0;">switch</span> (file-&gt;back_end) {
<span style="color: #a020f0;">case</span> TREXIO_TEXT:
<span style="color: #a020f0;">return</span> trexio_text_has_$group_str$(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #a020f0;">case</span> TREXIO_HDF5:
<span style="color: #483d8b;">#ifdef</span> HAVE_HDF5
<span style="color: #a020f0;">return</span> trexio_hdf5_has_$group_str$(file);
<span style="color: #a020f0;">break</span>;
<span style="color: #483d8b;">#else</span>
<span style="color: #a020f0;">return</span> TREXIO_BACK_END_MISSING;
<span style="color: #483d8b;">#endif</span>
/*
<span style="color: #b22222;"> case TREXIO_JSON:</span>
<span style="color: #b22222;"> return trexio_json_has_$group_str$(file);</span>
<span style="color: #b22222;"> break;</span>
*/
}
<span style="color: #a020f0;">return</span> TREXIO_FAILURE;
}
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-org3f0096e" class="outline-4">
<h4 id="org3f0096e"><span class="section-number-4">3.6.3</span> Fortran templates for front end</h4>
<div class="outline-text-4" id="text-3-6-3">
<p>
The <code>Fortran</code> templates that provide an access to the <code>C</code> API calls from Fortran.
These templates are based on the use of <code>iso_c_binding</code>. Pointers have to be passed by value.
</p>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_write_$group_str$_c</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, str, max_str_len)</span><span style="color: #a0522d;"> </span><span style="color: #a020f0;">&amp;</span>
<span style="color: #a020f0;">bind</span>(C, name=<span style="color: #8b2252;">"trexio_write_$group_str$"</span>)
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #228b22;">character</span>, <span style="color: #a020f0;">intent</span>(in) ::<span style="color: #a0522d;"> str(*)</span>
<span style="color: #228b22;">integer</span>(4), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_str_len</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_write_$group_str$_c</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_read_$group_str$_c</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, str, max_str_len)</span><span style="color: #a0522d;"> </span><span style="color: #a020f0;">&amp;</span>
<span style="color: #a020f0;">bind</span>(C, name=<span style="color: #8b2252;">"trexio_read_$group_str$"</span>)
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #228b22;">character</span>, <span style="color: #a020f0;">intent</span>(out) ::<span style="color: #a0522d;"> str(*)</span>
<span style="color: #228b22;">integer</span>(4), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_str_len</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_read_$group_str$_c</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">interface</span>
<span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_has_$group_str$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file) bind(C)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_has_$group_str$</span>
<span style="color: #a020f0;">end interface</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_read_$group_str$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, str, max_str_len)</span>
<span style="color: #a020f0;">implicit</span> <span style="color: #228b22;">none</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #228b22;">integer</span>(4), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_str_len</span>
<span style="color: #228b22;">character</span>, <span style="color: #a020f0;">intent</span>(out) ::<span style="color: #a0522d;"> str(*)</span>
trexio_read_$group_str$ = trexio_read_$group_str$_c(trex_file, str, max_str_len)
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_read_$group_str$</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #228b22;">integer </span><span style="color: #a020f0;">function</span><span style="color: #a0522d;"> </span><span style="color: #0000ff;">trexio_write_$group_str$</span><span style="color: #a0522d;"> </span><span style="color: #000000; background-color: #ffffff;">(trex_file, str, max_str_len)</span>
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>, <span style="color: #a020f0;">only</span> : <span style="color: #008b8b;">c_null_char</span>
<span style="color: #a020f0;">implicit</span> <span style="color: #228b22;">none</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trex_file</span>
<span style="color: #228b22;">integer</span>(4), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_str_len</span>
<span style="color: #228b22;">character</span>(len=*), <span style="color: #a020f0;">intent</span>(in) ::<span style="color: #a0522d;"> str</span>
<span style="color: #228b22;">character</span>(len=<span style="color: #a020f0;">len_trim</span>(str)+1) ::<span style="color: #a0522d;"> str_c</span>
str_c = <span style="color: #a020f0;">trim</span>(str) // <span style="color: #008b8b;">c_null_char</span>
trexio_write_$group_str$ = trexio_write_$group_str$_c(trex_file, str_c, max_str_len)
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_write_$group_str$</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org30cd357" class="outline-4">
<h4 id="org30cd357"><span class="section-number-4">3.6.4</span> Python templates for front end</h4>
<div class="outline-text-4" id="text-3-6-4">
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">write_</span>$group_str$(trexio_file, str_w: <span style="color: #483d8b;">str</span>) -&gt; <span style="color: #008b8b;">None</span>:
<span style="color: #8b2252;">"""Write the $group_str$ variable in the TREXIO file.</span>
<span style="color: #8b2252;"> Parameters:</span>
<span style="color: #8b2252;"> trexio_file:</span>
<span style="color: #8b2252;"> TREXIO File object.</span>
<span style="color: #8b2252;"> str_w: str</span>
<span style="color: #8b2252;"> String corresponding to the $group_str$ variable to be written.</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from AssertionError if TREXIO return code ~rc~ is different from TREXIO_SUCCESS and prints the error message using trexio_string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a0522d;">max_str_length</span> = <span style="color: #483d8b;">len</span>(str_w) + 1
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span> = pytr.trexio_write_$group_str$(trexio_file.pytrexio_s, str_w, max_str_length)
<span style="color: #a020f0;">if</span> rc != TREXIO_SUCCESS:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">read_</span>$group_str$(trexio_file) -&gt; <span style="color: #483d8b;">str</span>:
<span style="color: #8b2252;">"""Read the $group_str$ variable from the TREXIO file.</span>
<span style="color: #8b2252;"> Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.</span>
<span style="color: #8b2252;"> Returns:</span>
<span style="color: #8b2252;"> ~str_r~: str</span>
<span style="color: #8b2252;"> String corresponding to the $group_str$ variable read from ~trexio_file~.</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from AssertionError if TREXIO return code ~rc~ is different from TREXIO_SUCCESS and prints the error message using trexio_string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span>, <span style="color: #a0522d;">str_r</span> = pytr.trexio_read_$group_str$(trexio_file.pytrexio_s, PYTREXIO_MAX_STR_LENGTH)
<span style="color: #a020f0;">if</span> rc != TREXIO_SUCCESS:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a020f0;">return</span> str_r
</pre>
</div>
<div class="org-src-container">
<pre class="src src-python"><span style="color: #a020f0;">def</span> <span style="color: #0000ff;">has_</span>$group_str$(trexio_file) -&gt; <span style="color: #483d8b;">bool</span>:
<span style="color: #8b2252;">"""Check that $group_str$ variable exists in the TREXIO file.</span>
<span style="color: #8b2252;"> Parameter is a ~TREXIO File~ object that has been created by a call to ~open~ function.</span>
<span style="color: #8b2252;"> Returns:</span>
<span style="color: #8b2252;"> True if the variable exists, False otherwise</span>
<span style="color: #8b2252;"> Raises:</span>
<span style="color: #8b2252;"> - Exception from trexio.Error class if TREXIO return code ~rc~ is TREXIO_FAILURE and prints the error message using string_of_error.</span>
<span style="color: #8b2252;"> - Exception from some other error (e.g. RuntimeError).</span>
<span style="color: #8b2252;"> """</span>
<span style="color: #a020f0;">try</span>:
<span style="color: #a0522d;">rc</span> = pytr.trexio_has_$group_str$(trexio_file.pytrexio_s)
<span style="color: #a020f0;">if</span> rc == TREXIO_FAILURE:
<span style="color: #a020f0;">raise</span> Error(rc)
<span style="color: #a020f0;">except</span>:
<span style="color: #a020f0;">raise</span>
<span style="color: #a020f0;">if</span> rc == TREXIO_SUCCESS:
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">True</span>
<span style="color: #a020f0;">else</span>:
<span style="color: #a020f0;">return</span> <span style="color: #008b8b;">False</span>
</pre>
</div>
</div>
</div>
</div>
</div>
<div id="outline-container-orga1e5cb3" class="outline-2">
<h2 id="orga1e5cb3"><span class="section-number-2">4</span> Fortran helper/wrapper functions</h2>
<div class="outline-text-2" id="text-4">
<p>
The function below adapts the original C-based <code>trexio_open</code> for Fortran.
This is needed due to the fact that strings in C are terminated by <code>NULL</code> character <code>\0</code>
unlike strings in Fortran.
Note, that Fortran interface calls the main <code>TREXIO</code> API, which is written in C.
</p>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">contains</span>
<span style="color: #228b22;">integer</span>(8) <span style="color: #a020f0;">function</span> <span style="color: #0000ff;">trexio_open</span> (filename, mode, backend, rc_open)
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>, <span style="color: #a020f0;">only</span> : <span style="color: #008b8b;">c_null_char</span>
<span style="color: #a020f0;">implicit</span> <span style="color: #228b22;">none</span>
<span style="color: #228b22;">character</span>(len=*), <span style="color: #a020f0;">intent</span>(in) ::<span style="color: #a0522d;"> filename</span>
<span style="color: #228b22;">character</span>, <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> mode</span>
<span style="color: #228b22;">integer</span>(trexio_backend), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> backend</span>
<span style="color: #228b22;">integer</span>(trexio_exit_code), <span style="color: #a020f0;">intent</span>(out) ::<span style="color: #a0522d;"> rc_open</span>
<span style="color: #228b22;">character</span>(len=<span style="color: #a020f0;">len_trim</span>(filename)+1) ::<span style="color: #a0522d;"> filename_c</span>
<span style="color: #228b22;">integer</span>(trexio_exit_code) ::<span style="color: #a0522d;"> rc</span>
filename_c = <span style="color: #a020f0;">trim</span>(filename) // <span style="color: #008b8b;">c_null_char</span>
trexio_open = trexio_open_c(filename_c, mode, backend, rc_open)
<span style="color: #a020f0;">if</span> (trexio_open == 0_8 <span style="color: #a020f0;">.or.</span> rc_open /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
<span style="color: #a020f0;">return</span>
<span style="color: #a020f0;">endif</span>
rc = trexio_set_one_based(trexio_open)
<span style="color: #a020f0;">if</span> (rc /= TREXIO_SUCCESS) <span style="color: #a020f0;">then</span>
rc = trexio_close(trexio_open)
trexio_open = 0_8
<span style="color: #a020f0;">endif</span>
<span style="color: #a020f0;">end function</span> <span style="color: #0000ff;">trexio_open</span>
</pre>
</div>
<p>
The subroutine below transforms an array of Fortran strings into one big string using <code>TREXIO_DELIM</code> symbol
as a delimeter and adds <code>NULL</code> character in the end in order to properly pass the desired string to
C API. This is needed due to the fact that strings in C are terminated by <code>NULL</code> character <code>\0</code>.
</p>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">subroutine</span> <span style="color: #0000ff;">trexio_strarray2str</span>(str_array, max_num_str, max_len_str, str_res)
<span style="color: #a020f0;">use</span>, <span style="color: #a020f0;">intrinsic</span> :: <span style="color: #0000ff;">iso_c_binding</span>, <span style="color: #a020f0;">only</span> : <span style="color: #008b8b;">c_null_char</span>
<span style="color: #a020f0;">implicit</span> <span style="color: #228b22;">none</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_num_str </span>! <span style="color: #b22222;">number of elements in strign array</span>
<span style="color: #228b22;">integer</span>, <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_len_str </span>! <span style="color: #b22222;">maximum length of a string in an array</span>
<span style="color: #228b22;">character</span>(len=*), <span style="color: #a020f0;">intent</span>(in) ::<span style="color: #a0522d;"> str_array(*)</span>
<span style="color: #228b22;">character</span>(len=:), <span style="color: #a020f0;">allocatable</span>, <span style="color: #a020f0;">intent</span>(out) ::<span style="color: #a0522d;"> str_res</span>
<span style="color: #228b22;">integer</span> ::<span style="color: #a0522d;"> i</span>
str_res = <span style="color: #8b2252;">''</span>
<span style="color: #a020f0;">do</span> i = 1, max_num_str
str_res = str_res // <span style="color: #a020f0;">trim</span>(str_array(i)) // TREXIO_DELIM
<span style="color: #a020f0;">enddo</span>
str_res = str_res // <span style="color: #008b8b;">c_null_char</span>
<span style="color: #a020f0;">end subroutine</span> <span style="color: #0000ff;">trexio_strarray2str</span>
</pre>
</div>
<p>
The subroutine below does the reverse tranformation from one big string with delimeters into an array of Fortran strings.
</p>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">subroutine</span> <span style="color: #0000ff;">trexio_str2strarray</span>(str_flat, max_num_str, max_len_str, str_array)
<span style="color: #a020f0;">implicit</span> <span style="color: #228b22;">none</span>
<span style="color: #228b22;">integer</span>(8), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_num_str </span>! <span style="color: #b22222;">number of elements in strign array</span>
<span style="color: #228b22;">integer</span>, <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> max_len_str </span>! <span style="color: #b22222;">maximum length of a string in an array</span>
<span style="color: #228b22;">character</span>, <span style="color: #a020f0;">intent</span>(in) ::<span style="color: #a0522d;"> str_flat(*)</span>
<span style="color: #228b22;">character</span>(len=*), <span style="color: #a020f0;">intent</span>(inout) ::<span style="color: #a0522d;"> str_array(*)</span>
<span style="color: #228b22;">character</span>(len=max_len_str) ::<span style="color: #a0522d;"> tmp_str</span>
<span style="color: #228b22;">integer</span> ::<span style="color: #a0522d;"> i, j, k, ind, offset</span>
<span style="color: #228b22;">integer</span>(8) ::<span style="color: #a0522d;"> len_flat</span>
len_flat = (max_len_str+1)*max_num_str + 1
ind=1
offset=1
<span style="color: #a020f0;">do</span> i=1,max_num_str
k = 1
tmp_str=<span style="color: #8b2252;">''</span>
<span style="color: #a020f0;">do</span> j=ind,len_flat
<span style="color: #a020f0;">if</span> (str_flat(j) == TREXIO_DELIM) <span style="color: #a020f0;">then</span>
ind=j+1
<span style="color: #a020f0;">exit</span>
<span style="color: #a020f0;">endif</span>
tmp_str(k:k) = str_flat(j)
k = k + 1
<span style="color: #a020f0;">enddo</span>
str_array(i)=tmp_str
offset=ind
<span style="color: #a020f0;">enddo</span>
<span style="color: #a020f0;">end subroutine</span> <span style="color: #0000ff;">trexio_str2strarray</span>
</pre>
</div>
<p>
The subroutine is a Fortran analogue of <code>assert</code> in C. It check that the the return code of the
TREXIO API call is equal to a given return code. It can optionally print a success message if the
two code are identical, i.e. if the <code>assert</code> statement pass.
</p>
<div class="org-src-container">
<pre class="src src-f90"><span style="color: #a020f0;">subroutine</span> <span style="color: #0000ff;">trexio_assert</span>(trexio_rc, check_rc, success_message)
<span style="color: #a020f0;">implicit</span> <span style="color: #228b22;">none</span>
<span style="color: #228b22;">integer</span>, <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> trexio_rc</span>
<span style="color: #228b22;">integer</span>, <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">value</span> ::<span style="color: #a0522d;"> check_rc</span>
<span style="color: #228b22;">character</span>(len=*), <span style="color: #a020f0;">intent</span>(in), <span style="color: #a020f0;">optional</span> ::<span style="color: #a0522d;"> success_message</span>
<span style="color: #228b22;">character</span>*(128) ::<span style="color: #a0522d;"> str</span>
<span style="color: #a020f0;">if</span> (trexio_rc == check_rc) <span style="color: #a020f0;">then</span>
<span style="color: #a020f0;">if</span> (<span style="color: #a020f0;">present</span>(success_message)) <span style="color: #a020f0;">write</span>(*,*) success_message
<span style="color: #a020f0;">else</span>
<span style="color: #a020f0;">call</span> <span style="color: #0000ff;">trexio_string_of_error</span>(trexio_rc, str)
<span style="color: #a020f0;">print</span> *, <span style="color: #a020f0;">trim</span>(str)
<span style="color: #a020f0;">call</span> <span style="color: #0000ff;">exit</span>(1)
<span style="color: #a020f0;">endif</span>
<span style="color: #a020f0;">end subroutine</span> <span style="color: #0000ff;">trexio_assert</span>
</pre>
</div>
</div>
</div>
</div>
<div id="postamble" class="status">
<p class="author">Author: TREX-CoE</p>
<p class="date">Created: 2021-11-03 Wed 09:29</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>