omuse-geoscience / omuse

The Oceanographic Multi-purpose Software Environment: a package for multi-physics and multi-scale earth science simulations.
Apache License 2.0
18 stars 12 forks source link

DALES interface workdir and output redirection #36

Open fjansson opened 3 years ago

fjansson commented 3 years ago

It is currently not possible to redirect stdout, stderr to files in the model working directory. If workdir doesn't exist when creating the Dales object and a file in workdir is used for redirection the code fails, because the directory doesn't exist when the output files are created.

The DALES interface behaves differently depending on whether workdir exists. If not, it assumes a fresh start and copies files from inputdir. If it exists, it assumes a restart and doesn't copy files. This means the user code cannot create the working directory in advance.

Proposed fix: in the DALES interface.py, move the directory logic before call to model creation, add mkdirs in between:

class Dales(CommonCode, CodeWithNamelistParameters):
...
def __init__(self, **options):                                                                                                                                                            
        inputdir = None                                                                                                                                                                       

        # Set working directory                                                                                                                                                               
        self.workdir = "."                                                                                                                                                                    
        if "workdir" in options:                                                                                                                                                              
            self.workdir = options["workdir"]                                                                                                                                                 
            if os.path.exists(self.workdir):  # This is a restart                                                                                                                             
                inputdir = self.workdir                                                                                                                                                       
            else:                                                                                                                                                                             
                os.makedirs(self.workdir)                                                                                                                                                     
                # create work directory so that output files can be placed there                                                                                                              

        CodeWithNamelistParameters.__init__(self, namelist_parameters)                                                                                                                        
        CommonCode.__init__(self, DalesInterface(**options), **options)                                                                                                                       
        self.stopping_conditions = StoppingConditions(self)                                     
ipelupessy commented 3 years ago

sorry for getting to this late - I think this solution is ok (is there anything against it?)