Index: Source/Python/Classes/EcFlexpart.py
===================================================================
--- Source/Python/Classes/EcFlexpart.py	(revision 5bf163a83a60997cc70397ba4717876072d2a120)
+++ Source/Python/Classes/EcFlexpart.py	(revision 2c3c135f3c1956fcd3b017160897283b0358016d)
@@ -1416,5 +1416,5 @@
                                         'stepRange', 'values'],
                               keyvalues=[inumb, int(date.strftime('%Y%m%d')),
-                                         date.hour*100, 0, lsp_new_np[inumb, :, it]],
+                                         date.hour*100, 0, lsp_new_np[inumb, :, it]]
                              )
             fluxfile.set_keys(tmpfile, filemode='ab',
@@ -1479,10 +1479,7 @@
 
         gribfile = GribUtil(os.path.join(inputdir, 'rr_grib_dummy.grb'))
-
-        gribfile.copy_dummy_msg(ifile, keynames=['paramId'],
-                                keyvalues=[142], filemode='wb')
-
-        gribfile.copy_dummy_msg(ifile, keynames=['paramId'],
-                                keyvalues=[143], filemode='ab')
+        
+        gribfile.copy_dummy_msg(ifile, keynames=['paramId','paramId'],
+                                keyvalues=[142,143], filemode='wb')        
 
         return
@@ -1755,5 +1752,8 @@
                                                 c.ppid +
                                                 '*')[0])
-            fluxfile = 'flux' + cdate[0:2] + suffix
+            if c.marsclass == 'EP':
+                fluxfile = 'flux' + suffix
+            else:
+                fluxfile = 'flux' + cdate[0:2] + suffix
             if not c.cwc:
                 flist = ['fort.15', fluxfile, 'fort.16', orolsm]
Index: Source/Python/Classes/GribUtil.py
===================================================================
--- Source/Python/Classes/GribUtil.py	(revision 026b3592e0f0e4124d157e7b5b2d5dbea02dba75)
+++ Source/Python/Classes/GribUtil.py	(revision 2c3c135f3c1956fcd3b017160897283b0358016d)
@@ -122,6 +122,6 @@
 
 
-    def set_keys(self, fromfile, keynames, keyvalues, wherekeynames,
-                 wherekeyvalues, filemode='wb'):
+    def set_keys(self, fromfile, filemode='wb', keynames=[], keyvalues=[], 
+                 wherekeynames=[], wherekeyvalues=[]):
         '''Opens the file to read the grib messages and then write
         the selected messages (with wherekeys) to a new output file.
@@ -162,11 +162,16 @@
 
         fout = open(self.filenames, filemode)
-
-        with open(fromfile, 'rb') as fin:
+       # print(fout)
+        fin = open(fromfile, 'rb')
+       # print(fin)
+        while True:
             gid = codes_grib_new_from_file(fin)
-
+           # print('test')
+            if gid is None:
+                break            
+            
             select = True
-            i = 0
-            for wherekey in wherekeynames:
+            #print(str(codes_get(gid,'paramId')))
+            for i, wherekey in enumerate(wherekeynames):
                 if not codes_is_defined(gid, wherekey):
                     raise Exception("wherekey was not defined")
@@ -174,14 +179,11 @@
                 select = (select and (str(wherekeyvalues[i]) ==
                                       str(codes_get(gid, wherekey))))
-                i += 1
 
             if select:
-                i = 0
-                for key in keynames:
+                for i, key in enumerate(keynames):
                     if key == 'values':
                         codes_set_values(gid, keyvalues[i])
                     else:
                         codes_set(gid, key, keyvalues[i])
-                    i += 1
 
                 codes_write(gid, fout)
@@ -190,4 +192,5 @@
 
         fout.close()
+        fin.close()
 
         return
@@ -229,19 +232,22 @@
             raise Exception("Give a value for each keyname!")
 
-
         fout = open(self.filenames, filemode)
-
+        fin = open(filename_in, 'rb')
+        
         fields = 0
-
-        with open(filename_in, 'rb') as fin:
-            if fields >= 1:
+        while True:
+            if fields >= len(keyvalues):
                 fout.close()
+                fin.close()
                 return
-
+                
             gid = codes_grib_new_from_file(fin)
-
-            select = True
-            i = 0
-            for key in keynames:
+            if gid is None:
+                break            
+
+            for i, key in enumerate(keynames):
+                
+                select = True
+                
                 if not codes_is_defined(gid, key):
                     raise Exception("Key was not defined")
@@ -253,13 +259,13 @@
                     select = (select and (str(keyvalues[i]) !=
                                           str(codes_get(gid, key))))
-                i += 1
-
-            if select:
-                fields = fields + 1
-                codes_write(gid, fout)
-
+                
+                if select:
+                    fields = fields + 1
+                    codes_write(gid, fout)
+                    
             codes_release(gid)
 
         fout.close()
+        fin.close()
 
         return
