__init__.py 4.17 KB
Newer Older
1
import os
2
import shutil
3

4
from params import make_rundef, make_totinput
5
6
from pycif.utils import path
from pycif.utils.check import verbose
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22


def make_input(self, mod_input, di, df, mode, runsubdir, workdir,
               lmdz_yearref=1979, **kwargs):
    """Prepares inputs for the current LMDZ simulation. It includes:
        - meteo files (defstoke.nc, fluxstoke.nc fluxstokev.nc and phystoke.nc)
        - def files (run.def, totinput, ACTIVE_SPECIES)
        - initial conditions (start.nc)
        - fields for SACS (inca.nc, incaMCF.nc)

    Args:
        - self (Plugin): LMDZ plugin
        - mod_input (str): one of: 'meteo', 'def', 'inicond', 'chem_fields'
        - di (datetime): beginning of the simulation window (along the fwd
        or bckwd axis)
        - df (datetime): end of simulation
Antoine Berchet's avatar
Antoine Berchet committed
23
        - workdir (str): path to the pycif simulation
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
        - runsubdir (str): path to the LMDZ sub-simulation
        - mode (str): running mode; fwd or adj
        - lmdz_yearref (int): reference year for LMDZ dates. Default is 1979

    """

    datei = min(di, df)
    
    if mod_input == 'meteo':
        # Links meteorological mass fluxes
        for ftype in ['defstoke', 'fluxstoke', 'fluxstokev', 'phystoke']:
            fic = "{}.an{}.m{:02d}.nc".format(ftype,
                                              datei.year, datei.month)
            
            source = '{}/meteo/{}'.format(workdir, fic)
            target = '{}/{}.nc'.format(runsubdir, ftype)
            
            if ftype == 'defstoke' and not os.path.isfile(source):
                fic = "{}.nc".format(ftype)
                source = '{}/meteo/{}'.format(workdir, fic)
            
            path.link(source, target)
        
        # Links vertical coordinates from previous simulations
        source = self.fic_vcoord
        target = '{}/vcoord.nc'.format(runsubdir)
        path.link(source, target)
        
    elif mod_input == 'def':
        # run.def
        make_rundef(self.ficdef, datei, self.physic, runsubdir, lmdz_yearref)
        
        # totinput
        make_totinput(self, runsubdir, datei, mode)
        
        # Remove restart TL
        if mode != 'tl':
Espen Sollum's avatar
Espen Sollum committed
61
            shutil.rmtree('{}/restart_tl.bin'.format(runsubdir),
62
                          ignore_errors=True)
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
        
        # TODO: at the moment, no satellite observation
        os.system('echo 0 > {}/infousedsat.txt'.format(runsubdir))
        
    elif mod_input == 'chem_fields':
        if hasattr(self, 'chemistry'):
            # Linking to pre-computed INCA fields for kinetics
            source = \
                datei.strftime("{}/{}".format(
                    self.chemistry.kinetic.dir,
                    self.chemistry.kinetic.fic))
            target = "{}/kinetic.nc".format(runsubdir)
            path.link(source, target)
            
            # Linking to pre-computed INCA fields for deposition
            if hasattr(self.chemistry, 'deposition'):
                for spec in self.chemistry.deposition.attributes:
                    tracer = getattr(self.chemistry.deposition, spec)
                    source = \
                        datei.strftime("{}/{}".format(
                            tracer.dir, tracer.fic))
                    target = "{}/dep_{}.nc".format(runsubdir, spec)
                    path.link(source, target)
86
            
87
88
89
            finf = '{}/chemical_scheme.nml'.format(self.chemistry.dirchem_ref)
            target = '{}/chemical_scheme.nml'.format(runsubdir)
            path.link(finf, target)
90
91
92
93
94
95
96
97
98
            
    elif mod_input == 'traj':
        if mode == 'adj':
            if not hasattr(self, 'adj_refdir'):
                verbose("Adjoint LMDZ couldn't be initialized "
                        "with forward trajq.bin files")
                raise Exception
            
            else:
Antoine Berchet's avatar
Antoine Berchet committed
99
                for spec in self.chemistry.acspecies.attributes:
100
101
                    fic = "trajq_{}_%Y%m%d%H%M.bin".format(spec)
                    source = datei.strftime(
102
                        "{}/chain/{}".format(
103
                            self.adj_refdir, fic))
104
                    target = "{}/trajq_{}.bin".format(runsubdir, spec)
105
                    
106
107
108
                    path.link(source, target)