eccodes/html/precision__fortran_8_f-exam...

119 lines
6.9 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_fortran.F</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_fortran.F</h1>precision_fortran.F How to control precision when coding a grib field.<p>
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 C Copyright 2005-2016 ECMWF
<a name="l00002"></a>00002 C This software is licensed under the terms of the Apache Licence Version 2.0
<a name="l00003"></a>00003 C which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
<a name="l00004"></a>00004 C
<a name="l00005"></a>00005 C In applying this licence, ECMWF does not waive the privileges and immunities granted to it by
<a name="l00006"></a>00006 C virtue of its status as an intergovernmental organisation nor does it submit to any jurisdiction.
<a name="l00007"></a>00007 C
<a name="l00008"></a>00008 C
<a name="l00009"></a>00009 C Fortran 77 Implementation: precision
<a name="l00010"></a>00010 C
<a name="l00011"></a>00011 C Description: how to control <span class="keyword">decimal precision</span> when packing fields.
<a name="l00012"></a>00012 C
<a name="l00013"></a>00013 C
<a name="l00014"></a>00014 C Author: Enrico Fucile
<a name="l00015"></a>00015 C
<a name="l00016"></a>00016 C
<a name="l00017"></a>00017 C
<a name="l00018"></a>00018 program precision
<a name="l00019"></a>00019 implicit none
<a name="l00020"></a>00020 integer maxNumberOfValues
<a name="l00021"></a>00021 parameter (maxNumberOfValues=10000)
<a name="l00022"></a>00022 include 'grib_api_f77.h'
<a name="l00023"></a>00023 integer*4 size
<a name="l00024"></a>00024 integer infile,outfile
<a name="l00025"></a>00025 integer igrib
<a name="l00026"></a>00026 real*8 values1(maxNumberOfValues)
<a name="l00027"></a>00027 real*8 values2(maxNumberOfValues)
<a name="l00028"></a>00028 real*8 maxa,a,maxv,minv,maxr,r
<a name="l00029"></a>00029 integer*4 decimalPrecision,bitsPerValue1,bitsPerValue2
<a name="l00030"></a>00030 integer i
<a name="l00031"></a>00031
<a name="l00032"></a>00032 call grib_check(grib_open_file(infile
<a name="l00033"></a>00033 X,'../../data/regular_latlon_surface.grib1','r'))
<a name="l00034"></a>00034
<a name="l00035"></a>00035 call grib_check(grib_open_file(outfile
<a name="l00036"></a>00036 X,'../../data/regular_latlon_surface_prec.grib1','w'))
<a name="l00037"></a>00037
<a name="l00038"></a>00038 C a new grib message is loaded from file
<a name="l00039"></a>00039 C igrib is the grib <span class="keywordtype">id</span> to be used in subsequent calls
<a name="l00040"></a>00040 call grib_check(grib_new_from_file(infile,igrib))
<a name="l00041"></a>00041
<a name="l00042"></a>00042 C bitsPerValue before changing the packing parameters
<a name="l00043"></a>00043 call grib_check(grib_get_int(igrib,'bitsPerValue',bitsPerValue1))
<a name="l00044"></a>00044
<a name="l00045"></a>00045 C get the size of the values array
<a name="l00046"></a>00046 call grib_check(<a name="a0"></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>(igrib,"values",size))
<a name="l00047"></a>00047
<a name="l00048"></a>00048 C get data values before changing the packing parameters*/
<a name="l00049"></a>00049 call grib_check(grib_get_real8_array(igrib,"values",values1,size))
<a name="l00050"></a>00050
<a name="l00051"></a>00051 C setting decimal precision=2 means that 2 decimal digits
<a name="l00052"></a>00052 C are preserved when packing.
<a name="l00053"></a>00053 decimalPrecision=2
<a name="l00054"></a>00054 call grib_check(grib_set_int(igrib,"changeDecimalPrecision"
<a name="l00055"></a>00055 X,decimalPrecision))
<a name="l00056"></a>00056
<a name="l00057"></a>00057 C bitsPerValue after changing the packing parameters
<a name="l00058"></a>00058 call grib_check(grib_get_int(igrib,"bitsPerValue",bitsPerValue2))
<a name="l00059"></a>00059
<a name="l00060"></a>00060 C get data values after changing the packing parameters
<a name="l00061"></a>00061 call grib_check(grib_get_real8_array(igrib,"values",values2,size))
<a name="l00062"></a>00062
<a name="l00063"></a>00063 C computing error
<a name="l00064"></a>00064 maxa=0
<a name="l00065"></a>00065 maxr=0
<a name="l00066"></a>00066 maxv=values2(1)
<a name="l00067"></a>00067 minv=maxv
<a name="l00068"></a>00068 do i=1,size
<a name="l00069"></a>00069 a=abs(values2(i)-values1(i))
<a name="l00070"></a>00070 if ( values2(i) .gt. maxv ) maxv=values2(i)
<a name="l00071"></a>00071 if ( values2(i) .lt. maxv ) minv=values2(i)
<a name="l00072"></a>00072 if ( values2(i) .ne. 0 ) then
<a name="l00073"></a>00073 r=abs((values2(i)-values1(i))/values2(i))
<a name="l00074"></a>00074 endif
<a name="l00075"></a>00075 if ( a .gt. maxa ) maxa=a
<a name="l00076"></a>00076 if ( r .gt. maxr ) maxr=r
<a name="l00077"></a>00077 enddo
<a name="l00078"></a>00078 write(*,*) "max absolute error = ",maxa
<a name="l00079"></a>00079 write(*,*) "max relative error = ",maxr
<a name="l00080"></a>00080 write(*,*) "min value = ",minv
<a name="l00081"></a>00081 write(*,*) "max value = ",maxv
<a name="l00082"></a>00082
<a name="l00083"></a>00083 write(*,*) "old number of bits per value=",bitsPerValue1
<a name="l00084"></a>00084 write(*,*) "new number of bits per value=",bitsPerValue2
<a name="l00085"></a>00085
<a name="l00086"></a>00086 C write modified message to a file
<a name="l00087"></a>00087 call grib_check(grib_write(igrib,outfile))
<a name="l00088"></a>00088
<a name="l00089"></a>00089 call grib_check(grib_release(igrib))
<a name="l00090"></a>00090
<a name="l00091"></a>00091 call grib_check(grib_close_file(infile))
<a name="l00092"></a>00092
<a name="l00093"></a>00093 call grib_check(grib_close_file(outfile))
<a name="l00094"></a>00094
<a name="l00095"></a>00095 end
<a name="l00096"></a>00096
</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>