eccodes/html/precision_8c-example.html

139 lines
11 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>grib_api: precision.c</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.5.3 -->
<div class="tabs">
<ul>
<li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="pages.html"><span>Related&nbsp;Pages</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
</ul>
</div>
<h1>precision.c</h1>precision.c How to control precision when coding a grib field.<p>
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001
<a name="l00010"></a>00010 <span class="comment">/*</span>
<a name="l00011"></a>00011 <span class="comment"> * C Implementation: precision</span>
<a name="l00012"></a>00012 <span class="comment"> *</span>
<a name="l00013"></a>00013 <span class="comment"> * Description: how to control decimal precision when packing fields.</span>
<a name="l00014"></a>00014 <span class="comment"> *</span>
<a name="l00015"></a>00015 <span class="comment"> *</span>
<a name="l00016"></a>00016 <span class="comment"> * Author: Enrico Fucile</span>
<a name="l00017"></a>00017 <span class="comment"> *</span>
<a name="l00018"></a>00018 <span class="comment"> *</span>
<a name="l00019"></a>00019 <span class="comment"> */</span>
<a name="l00020"></a>00020 <span class="preprocessor">#include &lt;stdio.h&gt;</span>
<a name="l00021"></a>00021 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00022"></a>00022 <span class="preprocessor">#include &lt;math.h&gt;</span>
<a name="l00023"></a>00023
<a name="l00024"></a>00024 <span class="preprocessor">#include "<a class="code" href="grib__api_8h.html" title="Copyright 2005-2016 ECMWF.">grib_api.h</a>"</span>
<a name="l00025"></a>00025
<a name="l00026"></a>00026 <span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv) {
<a name="l00027"></a>00027 <span class="keywordtype">int</span> err = 0;
<a name="l00028"></a>00028 <span class="keywordtype">size_t</span> size=0;
<a name="l00029"></a>00029
<a name="l00030"></a>00030 FILE* in = NULL;
<a name="l00031"></a>00031 <span class="keywordtype">char</span>* infile = <span class="stringliteral">"../../data/regular_latlon_surface.grib1"</span>;
<a name="l00032"></a>00032 FILE* out = NULL;
<a name="l00033"></a>00033 <span class="keywordtype">char</span>* outfile = <span class="stringliteral">"out.grib1"</span>;
<a name="l00034"></a>00034 <a name="a0"></a><a class="code" href="group__grib__handle.html#g309a5ee24f4c730646d3f80ad0ef5f1b">grib_handle</a> *h = NULL;
<a name="l00035"></a>00035 <span class="keyword">const</span> <span class="keywordtype">void</span>* buffer = NULL;
<a name="l00036"></a>00036 <span class="keywordtype">double</span>* values1=NULL;
<a name="l00037"></a>00037 <span class="keywordtype">double</span>* values2=NULL;
<a name="l00038"></a>00038 <span class="keywordtype">double</span> maxa=0,a=0;
<a name="l00039"></a>00039 <span class="keywordtype">double</span> maxv=0,minv=0;
<a name="l00040"></a>00040 <span class="keywordtype">double</span> maxr=0,r=0;
<a name="l00041"></a>00041 <span class="keywordtype">long</span> decimalPrecision;
<a name="l00042"></a>00042 <span class="keywordtype">long</span> bitsPerValue1=0, bitsPerValue2=0;
<a name="l00043"></a>00043 <span class="keywordtype">int</span> i=0;
<a name="l00044"></a>00044
<a name="l00045"></a>00045 in = fopen(infile,<span class="stringliteral">"r"</span>);
<a name="l00046"></a>00046 <span class="keywordflow">if</span>(!in) {
<a name="l00047"></a>00047 printf(<span class="stringliteral">"ERROR: unable to open file %s\n"</span>,infile);
<a name="l00048"></a>00048 <span class="keywordflow">return</span> 1;
<a name="l00049"></a>00049 }
<a name="l00050"></a>00050
<a name="l00051"></a>00051 out = fopen(outfile,<span class="stringliteral">"w"</span>);
<a name="l00052"></a>00052 <span class="keywordflow">if</span>(!in) {
<a name="l00053"></a>00053 printf(<span class="stringliteral">"ERROR: unable to open file %s\n"</span>,outfile);
<a name="l00054"></a>00054 <span class="keywordflow">return</span> 1;
<a name="l00055"></a>00055 }
<a name="l00056"></a>00056
<a name="l00057"></a>00057 <span class="comment">/* create a new handle from a message in a file */</span>
<a name="l00058"></a>00058 h = <a name="a1"></a><a class="code" href="group__grib__handle.html#g5e24f8499aa7e4178ccc25a5de3145c5" title="Create a handle from a file resource.">grib_handle_new_from_file</a>(0,in,&amp;err);
<a name="l00059"></a>00059 <span class="keywordflow">if</span> (h == NULL) {
<a name="l00060"></a>00060 printf(<span class="stringliteral">"Error: unable to create handle from file %s\n"</span>,infile);
<a name="l00061"></a>00061 }
<a name="l00062"></a>00062
<a name="l00063"></a>00063 <span class="comment">/* bitsPerValue before changing the packing parameters */</span>
<a name="l00064"></a>00064 GRIB_CHECK(<a name="a2"></a><a class="code" href="group__get__set.html#g03cfa6762312face1a3cc3ef23e16526" title="Get a long value from a key, if several keys of the same name are present, the last...">grib_get_long</a>(h,<span class="stringliteral">"bitsPerValue"</span>,&amp;bitsPerValue1),0);
<a name="l00065"></a>00065
<a name="l00066"></a>00066 <span class="comment">/* get the size of the values array*/</span>
<a name="l00067"></a>00067 GRIB_CHECK(<a name="a3"></a><a class="code" href="group__get__set.html#g18b622ed86b24d5e5fcab70c309fc245" title="Get the number of coded value from a key, if several keys of the same name are present...">grib_get_size</a>(h,<span class="stringliteral">"values"</span>,&amp;size),0);
<a name="l00068"></a>00068
<a name="l00069"></a>00069 values1 = malloc(size*<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));
<a name="l00070"></a>00070 <span class="comment">/* get data values before changing the packing parameters*/</span>
<a name="l00071"></a>00071 GRIB_CHECK(<a name="a4"></a><a class="code" href="group__get__set.html#g61f1029d7f37d51f33835e218d58378a" title="Get double array values from a key.">grib_get_double_array</a>(h,<span class="stringliteral">"values"</span>,values1,&amp;size),0);
<a name="l00072"></a>00072
<a name="l00073"></a>00073 <span class="comment">/* changing decimal precition to 2 means that 2 decimal digits</span>
<a name="l00074"></a>00074 <span class="comment"> are preserved when packing. */</span>
<a name="l00075"></a>00075 decimalPrecision=2;
<a name="l00076"></a>00076 GRIB_CHECK(<a name="a5"></a><a class="code" href="group__get__set.html#g94c33cfe90c3aa887fb8e14f0bd87fe2" title="Set a long value from a key.">grib_set_long</a>(h,<span class="stringliteral">"changeDecimalPrecision"</span>,decimalPrecision),0);
<a name="l00077"></a>00077
<a name="l00078"></a>00078 <span class="comment">/* bitsPerValue after changing the packing parameters */</span>
<a name="l00079"></a>00079 GRIB_CHECK(<a class="code" href="group__get__set.html#g03cfa6762312face1a3cc3ef23e16526" title="Get a long value from a key, if several keys of the same name are present, the last...">grib_get_long</a>(h,<span class="stringliteral">"bitsPerValue"</span>,&amp;bitsPerValue2),0);
<a name="l00080"></a>00080
<a name="l00081"></a>00081 values2 = malloc(size*<span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));
<a name="l00082"></a>00082 <span class="comment">/* get data values after changing the packing parameters*/</span>
<a name="l00083"></a>00083 GRIB_CHECK(<a class="code" href="group__get__set.html#g61f1029d7f37d51f33835e218d58378a" title="Get double array values from a key.">grib_get_double_array</a>(h,<span class="stringliteral">"values"</span>,values2,&amp;size),0);
<a name="l00084"></a>00084
<a name="l00085"></a>00085 <span class="comment">/* computing error */</span>
<a name="l00086"></a>00086 maxa=0;
<a name="l00087"></a>00087 maxr=0;
<a name="l00088"></a>00088 maxv=values2[0];
<a name="l00089"></a>00089 minv=maxv;
<a name="l00090"></a>00090 <span class="keywordflow">for</span> (i=0;i&lt;size;i++) {
<a name="l00091"></a>00091 a=fabs(values2[i]-values1[i]);
<a name="l00092"></a>00092 <span class="keywordflow">if</span> ( values2[i] &gt; maxv ) maxv=values2[i];
<a name="l00093"></a>00093 <span class="keywordflow">if</span> ( values2[i] &lt; maxv ) minv=values2[i];
<a name="l00094"></a>00094 <span class="keywordflow">if</span> ( values2[i] !=0 ) r=fabs((values2[i]-values1[i])/values2[i]);
<a name="l00095"></a>00095 <span class="keywordflow">if</span> ( a &gt; maxa ) maxa=a;
<a name="l00096"></a>00096 <span class="keywordflow">if</span> ( r &gt; maxr ) maxr=r;
<a name="l00097"></a>00097 }
<a name="l00098"></a>00098 printf(<span class="stringliteral">"max absolute error = %g\n"</span>,maxa);
<a name="l00099"></a>00099 printf(<span class="stringliteral">"max relative error = %g\n"</span>,maxr);
<a name="l00100"></a>00100 printf(<span class="stringliteral">"min value = %g\n"</span>,minv);
<a name="l00101"></a>00101 printf(<span class="stringliteral">"max value = %g\n"</span>,maxv);
<a name="l00102"></a>00102
<a name="l00103"></a>00103 printf(<span class="stringliteral">"old number of bits per value=%ld\n"</span>,(<span class="keywordtype">long</span>)bitsPerValue1);
<a name="l00104"></a>00104 printf(<span class="stringliteral">"new number of bits per value=%ld\n"</span>,(<span class="keywordtype">long</span>)bitsPerValue2);
<a name="l00105"></a>00105
<a name="l00106"></a>00106 <span class="comment">/* get the coded message in a buffer */</span>
<a name="l00107"></a>00107 GRIB_CHECK(<a name="a6"></a><a class="code" href="group__handling__coded__messages.html#g9d654bd4fc5f422c161edd0a140ea185" title="getting the message attached to a handle">grib_get_message</a>(h,&amp;buffer,&amp;size),0);
<a name="l00108"></a>00108
<a name="l00109"></a>00109 <span class="comment">/* write the buffer in a file*/</span>
<a name="l00110"></a>00110 <span class="keywordflow">if</span>(fwrite(buffer,1,size,out) != size)
<a name="l00111"></a>00111 {
<a name="l00112"></a>00112 perror(argv[1]);
<a name="l00113"></a>00113 exit(1);
<a name="l00114"></a>00114 }
<a name="l00115"></a>00115
<a name="l00116"></a>00116 <span class="comment">/* delete handle */</span>
<a name="l00117"></a>00117 <a name="a7"></a><a class="code" href="group__grib__handle.html#g0e4b2585f22247c49b930c1579257677" title="Frees a handle, also frees the message if it is not a user message.">grib_handle_delete</a>(h);
<a name="l00118"></a>00118
<a name="l00119"></a>00119 fclose(in);
<a name="l00120"></a>00120 fclose(out);
<a name="l00121"></a>00121
<a name="l00122"></a>00122 <span class="keywordflow">return</span> 0;
<a name="l00123"></a>00123 }
<a name="l00124"></a>00124
</pre></div> <hr size="1"><address style="text-align: right;"><small>Generated on Tue Sep 22 15:18:21 2009 for grib_api by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.3 </small></address>
</body>
</html>