TNO Intern

Commit f7c048ed authored by Arjo Segers's avatar Arjo Segers
Browse files

Retry reading of S5p data, sometimes fails for unknown reason.

parent ef3bfc89
Loading
Loading
Loading
Loading
+82 −23
Original line number Diff line number Diff line
@@ -189,6 +189,7 @@ class S5p_File( object ) :
        # modules:
        import os
        import xarray
        import time
    
        # split:
        if '/' in path :
@@ -199,6 +200,14 @@ class S5p_File( object ) :
            group,vname = None,path
        #endif

        # somtimes fails ..
        maxtry = 5
        ntry = 0
        while True :
            # increase counter:
            ntry += 1
            # try to read:
            try :
                # access group:
                with xarray.open_dataset( self.filename, group=group ) as ds :
                    # check ..
@@ -210,7 +219,24 @@ class S5p_File( object ) :
                    da = ds[vname]
                    # not lazy ...
                    da.load()
        #endwith # xarray
                #endwith # ds
                # nothing wrong, leave try loop
                break
            except :
                # again?
                if ntry <= maxtry :
                    # info ...
                    logging.warning( 'attempt %i of %i to read group "%s" from "%s", try again ...' % (ntry,maxtry,group,self.filename) )
                    # wait ..
                    time.sleep(5)
                    # try again:
                    continue
                else :
                    logging.error( 'tried to read %i times, break ..' % maxtry )
                    raise Exception
                #endif
            #endtry # read group
        #endwhile # try to read
        
        # unit stuff?
        if units is not None :
@@ -1574,12 +1600,6 @@ class CSO_S5p_File( cso_file.CSO_File ) :
                        avk[:,k,iretr] = pdat['avk'][:,k] * pdat['amf'] / pdat['amft']
                    #endfor

                    # check ...
                    if sfile.nlayer is None :
                        logging.error( 'no layer dimension defined yet' )
                        raise Exception
                    #endif

                    # is troposphere layer defined ?
                    key = 'troplayer'
                    opath = rcf.get( vkey+'.'+key )
@@ -2030,8 +2050,46 @@ class CSO_S5p_Convert( utopya.UtopyaRc ) :
            # combine:
            output_filename = os.path.join( outdir, outfile )
            
            # create file?
            if not os.path.isfile(output_filename) :
                # info ...
                logging.info( '    create new file ...' )
                # always create:
                create = True
            #~ renew?
            elif renew :
                # info ...
                logging.info( '    renew file ...' )
                # always create:
                create = True
            #~ already a version present:
            elif os.path.isfile(output_filename) :
                # there might be duplicated processings, with different processing times;
                # filenames are sorted, so current is probably newer
                # open existing file:
                csf = CSO_S5p_File( output_filename )
                # original orbit as attribute?
                orbit_file = csf.GetAttr( 'orbit_file', quiet=True )
                if orbit_file is None :
                    # info ...
                    logging.info( '    replace existing file, no "orbit_file" attribute yet ...' )
                    # attribute not found, re-create to have this attribute:
                    create = True
                elif fname <= orbit_file :
                    # info ...
                    logging.info( '    keep existing file, based on same or newer source file ...' )
                    # same file, no need to re-create:
                    create = False
                else :
                    # info ...
                    logging.info( '    replace existing file, based on older processing: %s' % orbit_file )
                    # replace:
                    create = True
                #endif
            #endif
            
            # create?
            if (not os.path.isfile(output_filename)) or renew :
            if create :
            
                # initialize list of temporary files, removed at end:
                tmpfiles = []
@@ -2083,6 +2141,7 @@ class CSO_S5p_Convert( utopya.UtopyaRc ) :
                    history.append( 'added %i pixels from %s' % (nselected,os.path.basename(fname)) )
                    # update attributes:
                    attrs['orbit'     ] = orbit
                    attrs['orbit_file'] = fname
                    # write:
                    csf.Write( filename=output_filename, attrs=attrs, history=history )
                    
@@ -2143,7 +2202,7 @@ class CSO_S5p_Listing( utopya.UtopyaRc ) :
        ! - orbit number
        <rcbase>.file        :  /Scratch/CSO/S5p/RPRO/NO2/Europe/listing.csv
        
    An existing listing files is not replaced,
    An existing listing file is not replaced,
    unless the following flag is set::

        ! renew table?
@@ -2239,7 +2298,7 @@ class CSO_S5p_Listing( utopya.UtopyaRc ) :
            # remove entries that do not exist anymore:
            listing.Cleanup( indent=indent+'    ' )

            # patternectory templates:
            # filename pattern templates:
            pattern_templates = self.GetSetting( 'patterns' ).split()
            # collection of scanned patterns:
            patterns = []
@@ -2279,7 +2338,7 @@ class CSO_S5p_Listing( utopya.UtopyaRc ) :
                    fnames.sort()
                    # loop over files:
                    for fname in fnames :
                        # relative path to basedir:
                        # absolute path:
                        filename = os.path.join(bdir,fname)
                        # already in table?
                        if fname in listing :