Index: Documentation/html/_modules/GribUtil.html
===================================================================
--- Documentation/html/_modules/GribUtil.html	(revision 6931f619b7c5d1d97ad8d890d856f96211da5866)
+++ Documentation/html/_modules/GribUtil.html	(revision 08c90911f8a1c5051fd7421321897fe5517f12d9)
@@ -175,5 +175,5 @@
 <span class="c1">#</span>
 <span class="c1"># @License:</span>
-<span class="c1">#    (C) Copyright 2014-2019.</span>
+<span class="c1">#    (C) Copyright 2014-2020.</span>
 <span class="c1">#    Anne Philipp, Leopold Haimberger</span>
 <span class="c1">#</span>
@@ -284,6 +284,6 @@
 
 
-<div class="viewcode-block" id="GribUtil.set_keys"><a class="viewcode-back" href="../Documentation/Api/api_python.html#GribUtil.GribUtil.set_keys">[docs]</a>    <span class="k">def</span> <span class="nf">set_keys</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fromfile</span><span class="p">,</span> <span class="n">keynames</span><span class="p">,</span> <span class="n">keyvalues</span><span class="p">,</span> <span class="n">wherekeynames</span><span class="p">,</span>
-                 <span class="n">wherekeyvalues</span><span class="p">,</span> <span class="n">filemode</span><span class="o">=</span><span class="s1">&#39;wb&#39;</span><span class="p">):</span>
+<div class="viewcode-block" id="GribUtil.set_keys"><a class="viewcode-back" href="../Documentation/Api/api_python.html#GribUtil.GribUtil.set_keys">[docs]</a>    <span class="k">def</span> <span class="nf">set_keys</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fromfile</span><span class="p">,</span> <span class="n">filemode</span><span class="o">=</span><span class="s1">&#39;wb&#39;</span><span class="p">,</span> <span class="n">keynames</span><span class="o">=</span><span class="p">[],</span> <span class="n">keyvalues</span><span class="o">=</span><span class="p">[],</span> 
+                 <span class="n">wherekeynames</span><span class="o">=</span><span class="p">[],</span> <span class="n">wherekeyvalues</span><span class="o">=</span><span class="p">[]):</span>
         <span class="sd">&#39;&#39;&#39;Opens the file to read the grib messages and then write</span>
 <span class="sd">        the selected messages (with wherekeys) to a new output file.</span>
@@ -324,11 +324,16 @@
 
         <span class="n">fout</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filenames</span><span class="p">,</span> <span class="n">filemode</span><span class="p">)</span>
-
-        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">fromfile</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fin</span><span class="p">:</span>
+       <span class="c1"># print(fout)</span>
+        <span class="n">fin</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">fromfile</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span>
+       <span class="c1"># print(fin)</span>
+        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
             <span class="n">gid</span> <span class="o">=</span> <span class="n">codes_grib_new_from_file</span><span class="p">(</span><span class="n">fin</span><span class="p">)</span>
-
+           <span class="c1"># print(&#39;test&#39;)</span>
+            <span class="k">if</span> <span class="n">gid</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
+                <span class="k">break</span>            
+            
             <span class="n">select</span> <span class="o">=</span> <span class="kc">True</span>
-            <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
-            <span class="k">for</span> <span class="n">wherekey</span> <span class="ow">in</span> <span class="n">wherekeynames</span><span class="p">:</span>
+            <span class="c1">#print(str(codes_get(gid,&#39;paramId&#39;)))</span>
+            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">wherekey</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">wherekeynames</span><span class="p">):</span>
                 <span class="k">if</span> <span class="ow">not</span> <span class="n">codes_is_defined</span><span class="p">(</span><span class="n">gid</span><span class="p">,</span> <span class="n">wherekey</span><span class="p">):</span>
                     <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;wherekey was not defined&quot;</span><span class="p">)</span>
@@ -336,14 +341,11 @@
                 <span class="n">select</span> <span class="o">=</span> <span class="p">(</span><span class="n">select</span> <span class="ow">and</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">wherekeyvalues</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">==</span>
                                       <span class="nb">str</span><span class="p">(</span><span class="n">codes_get</span><span class="p">(</span><span class="n">gid</span><span class="p">,</span> <span class="n">wherekey</span><span class="p">))))</span>
-                <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
 
             <span class="k">if</span> <span class="n">select</span><span class="p">:</span>
-                <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
-                <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">keynames</span><span class="p">:</span>
+                <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">keynames</span><span class="p">):</span>
                     <span class="k">if</span> <span class="n">key</span> <span class="o">==</span> <span class="s1">&#39;values&#39;</span><span class="p">:</span>
                         <span class="n">codes_set_values</span><span class="p">(</span><span class="n">gid</span><span class="p">,</span> <span class="n">keyvalues</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
                     <span class="k">else</span><span class="p">:</span>
                         <span class="n">codes_set</span><span class="p">(</span><span class="n">gid</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">keyvalues</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
-                    <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
 
                 <span class="n">codes_write</span><span class="p">(</span><span class="n">gid</span><span class="p">,</span> <span class="n">fout</span><span class="p">)</span>
@@ -352,4 +354,5 @@
 
         <span class="n">fout</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
+        <span class="n">fin</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
 
         <span class="k">return</span></div>
@@ -391,19 +394,22 @@
             <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;Give a value for each keyname!&quot;</span><span class="p">)</span>
 
-
         <span class="n">fout</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">filenames</span><span class="p">,</span> <span class="n">filemode</span><span class="p">)</span>
-
+        <span class="n">fin</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename_in</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span>
+        
         <span class="n">fields</span> <span class="o">=</span> <span class="mi">0</span>
-
-        <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename_in</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fin</span><span class="p">:</span>
-            <span class="k">if</span> <span class="n">fields</span> <span class="o">&gt;=</span> <span class="mi">1</span><span class="p">:</span>
+        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
+            <span class="k">if</span> <span class="n">fields</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="n">keyvalues</span><span class="p">):</span>
                 <span class="n">fout</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
+                <span class="n">fin</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
                 <span class="k">return</span>
-
+                
             <span class="n">gid</span> <span class="o">=</span> <span class="n">codes_grib_new_from_file</span><span class="p">(</span><span class="n">fin</span><span class="p">)</span>
-
-            <span class="n">select</span> <span class="o">=</span> <span class="kc">True</span>
-            <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span>
-            <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">keynames</span><span class="p">:</span>
+            <span class="k">if</span> <span class="n">gid</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
+                <span class="k">break</span>            
+
+            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">key</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">keynames</span><span class="p">):</span>
+                
+                <span class="n">select</span> <span class="o">=</span> <span class="kc">True</span>
+                
                 <span class="k">if</span> <span class="ow">not</span> <span class="n">codes_is_defined</span><span class="p">(</span><span class="n">gid</span><span class="p">,</span> <span class="n">key</span><span class="p">):</span>
                     <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s2">&quot;Key was not defined&quot;</span><span class="p">)</span>
@@ -415,13 +421,13 @@
                     <span class="n">select</span> <span class="o">=</span> <span class="p">(</span><span class="n">select</span> <span class="ow">and</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">keyvalues</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">!=</span>
                                           <span class="nb">str</span><span class="p">(</span><span class="n">codes_get</span><span class="p">(</span><span class="n">gid</span><span class="p">,</span> <span class="n">key</span><span class="p">))))</span>
-                <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
-
-            <span class="k">if</span> <span class="n">select</span><span class="p">:</span>
-                <span class="n">fields</span> <span class="o">=</span> <span class="n">fields</span> <span class="o">+</span> <span class="mi">1</span>
-                <span class="n">codes_write</span><span class="p">(</span><span class="n">gid</span><span class="p">,</span> <span class="n">fout</span><span class="p">)</span>
-
+                
+                <span class="k">if</span> <span class="n">select</span><span class="p">:</span>
+                    <span class="n">fields</span> <span class="o">=</span> <span class="n">fields</span> <span class="o">+</span> <span class="mi">1</span>
+                    <span class="n">codes_write</span><span class="p">(</span><span class="n">gid</span><span class="p">,</span> <span class="n">fout</span><span class="p">)</span>
+                    
             <span class="n">codes_release</span><span class="p">(</span><span class="n">gid</span><span class="p">)</span>
 
         <span class="n">fout</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
+        <span class="n">fin</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
 
         <span class="k">return</span></div>
