source: TOOLS/WATER_BUDGET/libIGCM_sys.py

Last change on this file was 6764, checked in by omamce, 2 months ago

O.M. : water budget - version mars 2024

  • Property svn:keywords set to Date Revision HeadURL Author Id
File size: 17.6 KB
Line 
1# -*- coding: utf-8 -*-
2'''
3Defines libIGCM directories, depending of the computer
4
5olivier.marti@lsce.ipsl.fr
6
7  This library if a layer under some usefull
8  environment variables and commands.
9  All those definitions depend on host particularities.
10
11  This software is governed by the CeCILL  license under French law and
12  abiding by the rules of distribution of free software.  You can  use,
13  modify and/ or redistribute the software under the terms of the CeCILL
14  license as circulated by CEA, CNRS and INRIA at the following URL
15  "http://www.cecill.info".
16
17  Warning, to install, configure, run, use any of Olivier Marti's
18  software or to read the associated documentation you'll need at least
19  one (1) brain in a reasonably working order. Lack of this implement
20  will void any warranties (either express or implied).
21  O. Marti assumes no responsability for errors, omissions,
22  data loss, or any other consequences caused directly or indirectly by
23  the usage of his software by incorrectly or partially configured
24  personal.
25
26SVN information
27 $Author$
28 $Date$
29 $Revision$
30 $Id$
31 $HeadURL$
32'''
33
34import os, subprocess, configparser, types
35from pathlib import Path
36
37# Where do we run ?
38SysName, NodeName, Release, Version, Machine = os.uname ()
39
40# def unDefined (char) :
41#     '''Returns True if a variable is not defined, ot if it's set to None'''
42#     if char in globals () :
43#         if globals ()[char] == None or  globals ()[char] == 'None': unDefined = True
44#         else                                                      : unDefined = False
45#     else : unDefined = True
46#     return unDefined
47
48def Mach (long=False) :
49    '''
50    Find the computer we are on
51
52    On Irene, Mach returns Irene, Irene-Next, Rome or Rome-Prev if long==True
53    '''
54
55    zmach = None
56
57    if SysName == 'Darwin' and 'lsce5138' in NodeName : zmach = 'Spip'
58    if 'obelix'    in NodeName : zmach = 'Obelix'
59    if 'forge'     in NodeName : zmach = 'Forge'
60    if 'ciclad'    in NodeName : zmach = 'Ciclad'
61    if 'climserv'  in NodeName : zmach = 'SpiritX'
62    if 'spirit'    in NodeName : zmach = 'SpiritJ'
63    if 'spiritj'   in NodeName : zmach = 'SpiritJ'
64    if 'spiritx'   in NodeName : zmach = 'SpiritX'
65    if 'irene'     in NodeName : zmach = 'Irene'
66    if 'jean-zay'  in NodeName : zmach = 'Jean-Zay'
67
68    if long :
69        zmachfull = zmach
70
71        if zmach == 'Irene' :
72            CPU    = subprocess.getoutput ('lscpu')
73            ccc_os = subprocess.getoutput ('ccc_os')
74
75            if "Intel(R) Xeon(R) Platinum" in CPU :
76                zmachfull = 'Irene'
77
78            if "AMD" in CPU :
79                zmachfull = 'Rome'
80
81        zmach = zmachfull
82
83    return zmach
84
85class config :
86    def __str__ (self) :
87        return str (self.__dict__)
88
89    def __repr__ (self) :
90        return str (self.__dict__)
91
92    def __name__ (self) :
93        return self.__class__.__name__
94
95    def __getitem__ (self, key):
96        return getattr (self, key)
97
98    def __iter__ (self) :
99        return self
100
101    def __next__ (self) :
102        if self.index == 0:
103            raise StopIteration
104        self.index = self.index - 1
105        return self.data[self.index]
106
107    def __init__ (self, JobName=None, TagName=None, SpaceName=None, ExperimentName=None,
108                  LongName=None, ModelName=None, ShortName=None,
109                  Source=None, Host=None, ConfigCard=None, User=None, Group=None,
110                  TGCC_User='p86mart', TGCC_Group='gen12006', IDRIS_User='rces009', IDRIS_Group='ces',
111                  ARCHIVE=None, SCRATCHDIR=None, STORAGE=None, R_IN=None, R_OUT=None,
112                  R_FIG=None, L_EXP=None,
113                  R_SAVE=None, R_FIGR=None, R_BUF=None, R_BUFR=None, R_BUF_KSH=None,
114                  REBUILD_DIR=None, POST_DIR=None,
115                  ThreddsPrefix=None, R_GRAF=None, DB=None,
116                  IGCM_OUT=None, IGCM_OUT_name=None, rebuild=None, TmpDir=None) :
117        '''
118        Defines the libIGCM directories - Returns a dictionary or a namespace
119       
120        Source : for Spip
121        Possibilities :
122        Local        : local (~/Data/IGCMG/...)
123        TGCC_sshfs   : TGCC disks mounted via sshfs
124        TGCC_thredds : thredds TGCC via IPSL
125        ('https://thredds-su.ipsl.fr/thredds/dodsC/tgcc_thredds/store/...)
126       
127        Exemple of use :
128        libIGCM = libIGCM_sys.config ( .... )
129        globals().update ( libIGCM )
130        '''
131       
132        if not Host : Host = Mach (long=False)
133           
134        LocalUser = os.environ ['USER']
135           
136        # ===========================================================================================
137        # Reads config.card if available
138        if ConfigCard :
139            # Text existence of ConfigCard
140            if not os.path.exists (ConfigCard ) :
141                raise FileExistsError ( f"File not found : {ConfigCard = }" )
142               
143            ## Creates parser for reading .ini input file
144            MyReader = configparser.ConfigParser (interpolation=configparser.ExtendedInterpolation() )
145            MyReader.optionxform = str # To keep capitals
146           
147            MyReader.read (ConfigCard)
148           
149            JobName        = MyReader['UserChoices']['JobName']
150            #----- Short Name of Experiment
151            ExperimentName = MyReader['UserChoices']['ExperimentName']
152            #----- DEVT TEST PROD
153            SpaceName      = MyReader['UserChoices']['SpaceName']
154            LongName       = MyReader['UserChoices']['LongName']
155            ModelName      = MyReader['UserChoices']['ModelName']
156            TagName        = MyReader['UserChoices']['TagName']
157           
158        ### ===========================================================================================
159        ## Part specific to access by OpenDAP/Thredds server
160       
161        # ===========================================================================================
162        if Source == 'TGCC_thredds' :
163            if not User  and TGCC_User  : User  = TGCC_User
164            if not Group and TGCC_Group : Group = TGCC_Group
165               
166            if not ThreddsPrefix :
167                ThreddsPrefix = 'https://thredds-su.ipsl.fr/thredds/dodsC/tgcc_thredds'
168               
169            if not ARCHIVE : ARCHIVE = f'{ThreddsPrefix}/store/{TGCC_User}'
170            if not R_FIG   : R_FIG   = f'{ThreddsPrefix}/work/{TGCC_User}'
171            if not R_IN    : R_IN    = f'{ThreddsPrefix}/work/igcmg/IGCM'
172            if not R_GRAF  : R_GRAF  = f'{ThreddsPrefix}/work/p86mart/GRAF/DATA'
173               
174        # ===========================================================================================
175        if Source == 'IDRIS_thredds' :
176            if not User  and IDRIS_User  : User  = IDRIS_User
177            if not Group and IDRIS_Group : Group = IDRIS_Group
178               
179            if not ThreddsPrefix :
180                ThreddsPrefix = 'https://thredds-su.ipsl.fr/thredds/catalog/idris_thredds'
181               
182            if not ARCHIVE : ARCHIVE = f'{ThreddsPrefix}/store/{IDRIS_User}'
183            if not R_FIG   : R_FIG   = f'{ThreddsPrefix}/work/{IDRIS_User}'
184            if not R_IN    : R_IN    = f'{ThreddsPrefix}/work/igcmg/IGCM'
185            if not R_GRAF  : R_GRAF  = 'https://thredds-su.ipsl.fr/thredds/dodsC/tgcc_thredds/work/p86mart/GRAF/DATA'
186             
187        ### ===========================================================================================
188        ## Machine dependant part
189       
190        # ===========================================================================================
191        if Host == 'Spip' :
192            if not User : User = LocalUser
193            IGCM_OUT_name = 'IGCM_OUT'
194            if not ARCHIVE    : ARCHIVE     = os.path.join ( Path.home (), 'Data' )
195            if not SCRATCHDIR : SCRATCHDIR  = os.path.join ( Path.home (), 'Scratch' )
196            if not R_BUF      : R_BUF       = os.path.join ( Path.home (), 'Scratch' )
197            if not R_FIG      : R_FIG       = os.path.join ( Path.home (), 'Data'    )
198               
199            if not STORAGE    : STORAGE     = ARCHIVE
200            if not R_IN       : R_IN        = os.path.join ( '/', 'Users', 'marti', 'Data', 'IGCM' )
201            if not R_GRAF     : R_GRAF      = os.path.join ( '/', 'Users', 'marti', 'GRAF', 'DATA' )
202            if not DB         : DB          = os.path.join ( '/', 'Users', 'marti', 'GRAF', 'DB'   )
203            if not TmpDir     : TmpDir      = os.path.join ( Path.home (), 'Scratch' )
204               
205        # ===========================================================================================
206        if ( 'Irene' in Host ) or ( 'Rome' in Host ) :
207       
208            LocalHome  = subprocess.getoutput ( 'ccc_home --ccchome' )
209            LocalGroup = os.path.basename ( os.path.dirname (LocalHome))
210            if not User or User == 'marti' :
211                if not TGCC_User : User = LocalUser
212                else             : User = TGCC_User
213                   
214            if not Group :
215                if TGCC_Group : Group = TGCC_Group
216                else          : Group = LocalGroup
217                   
218            IGCM_OUT_name = 'IGCM_OUT'
219           
220            if not R_IN        :
221                R_IN       = os.path.join ( subprocess.getoutput (
222                    'ccc_home --cccwork -d igcmg -u igcmg' ), 'IGCM')
223            if not ARCHIVE     :
224                ARCHIVE    = subprocess.getoutput (
225                    f'ccc_home --cccstore   -u {User} -d {Group}' )
226            if not STORAGE     :
227                STORAGE    = subprocess.getoutput (
228                    f'ccc_home --cccwork    -u {User} -d {Group}' )
229            if not SCRATCHDIR  :
230                SCRATCHDIR = subprocess.getoutput (
231                    f'ccc_home --cccscratch -u {User} -d {Group}' )
232            if not R_BUF       :
233                R_BUF      = subprocess.getoutput (
234                    f'ccc_home --cccscratch -u {User} -d {Group}' )
235            if not R_FIG       :
236                R_FIG      = subprocess.getoutput (
237                    f'ccc_home --cccwork    -u {User} -d {Group}' )
238            if not R_GRAF      :
239                R_GRAF     = os.path.join ( subprocess.getoutput (
240                    'ccc_home --cccwork -d drf -u p86mart'), 'GRAF', 'DATA' )
241            if not DB          :
242                DB         = os.path.join ( subprocess.getoutput (
243                    'ccc_home --cccwork -d igcmg -u igcmg'), 'database' )
244            if not rebuild :
245                rebuild = os.path.join (
246                    subprocess.getoutput ( 'ccc_home --ccchome -d igcmg -u igcmg' ),
247                    'Tools', 'irene', 'rebuild_nemo', 'bin', 'rebuild_nemo' )
248               
249            if not TmpDir : TmpDir = subprocess.getoutput ( f'ccc_home --cccscratch' )
250               
251        # ===========================================================================================
252        if Host == 'SpiritJ' :
253            if not User  :
254                if TGCC_User  : User = TGCC_User
255                else          : User = LocalUser
256            if not ARCHIVE    :
257                ARCHIVE    = os.path.join ( '/', 'thredds', 'tgcc', 'store', User )
258            if not  STORAGE   :
259                STORAGE    = os.path.join ( '/', 'thredds', 'tgcc', 'work' , User )
260            #if not SCRATCHDIR : SCRATCHDIR = os.path.join ( '/', 'thredds'  , 'tgcc', 'store', User )
261            if not R_IN       :
262                R_IN       = os.path.join ( '/', 'projsu', 'igcmg', 'IGCM' )
263            #if not R_GRAF     : R_GRAF     = os.path.join ('/', 'data', 'omamce', 'GRAF', 'DATA' )
264            if not R_GRAF     :
265                R_GRAF     = os.path.join  ( '/', 'thredds', 'tgcc', 'work', 'p86mart', 'GRAF', 'DATA' )
266            if not DB         :
267                DB         = os.path.join  ( '/', 'data', 'igcmg', 'database' )
268            if not TmpDir     :
269                TmpDir = os.path.join ( '/', 'data', LocalUser )
270               
271        # ===========================================================================================
272        if Host == 'SpiritX' :
273            if not User  :
274                if TGCC_User  : User  = TGCC_User
275                else          : User = os.environ ['USER']
276            if not ARCHIVE    :
277                ARCHIVE    = os.path.join ( '/', 'thredds', 'tgcc', 'store', User )
278            if not  STORAGE   :
279                STORAGE    = os.path.join ( '/', 'thredds', 'tgcc', 'work' , User )
280            #if not SCRATCHDIR :
281            #  SCRATCHDIR = os.path.join ( '/', 'thredds', 'tgcc', 'store', User )
282            if not R_IN       :
283                R_IN       = os.path.join ( '/', 'projsu', 'igcmg', 'IGCM' )
284            if not R_GRAF     :
285                R_GRAF     = os.path.join  ( '/', 'thredds', 'tgcc', 'work' , 'p86mart', 'GRAF', 'DATA' )
286            if not DB         :
287                DB         = os.path.join  ( '/', 'data', 'igcmg', 'database' )
288               
289        # ===========================================================================================
290        if Host == 'Jean-Zay' :
291            if not User  : User  = os.environ ['USER']
292            LocalGroup = os.path.basename ( os.path.dirname ( Path.home () ))
293            if not Group : Group = LocalGroup
294               
295            if not ARCHIVE    :
296                ARCHIVE    = os.path.join ( '/', 'gpfsstore'  , 'rech', Group, User )
297            if not STORAGE    :
298                STORAGE    = os.path.join ( '/', 'gpfswork'   , 'rech', Group, User )
299            if not SCRATCHDIR :
300                SCRATCHDIR = os.path.join ( '/', 'gpfsscratch', 'rech', Group, User )
301            if not R_FIG      :
302                R_FIG      = os.path.join ( '/', 'cccwork'    , 'rech', Group, User )
303            if not R_BUF      :
304                R_BUF      = os.path.join ( '/', 'gpfsscratch', 'rech', Group, User )
305            if not R_IN       :
306                R_IN       = os.path.join ( '/', 'gpfswork'   , 'rech', 'psl', 'commun', 'IGCM' )
307            if not R_GRAF     :
308                R_GRAF     = os.path.join ( '/', 'gpfswork'   , 'rech', Group, User, 'GRAF', 'DATA' )
309            if not DB         :
310                DB         = os.path.join ( '/', 'gpfswork'   , 'rech', 'psl', 'commun', 'database' )
311            if not rebuild :
312                rebuild = os.path.join ( '/', 'gpfswork', 'rech', 'psl', 'commun', 'Tools',
313                                            'rebuild', 'modipsl_IOIPSL_PLUS_v2_2_4', 'bin', 'rebuild' )
314            if not TmpDir : TmpDir = os.path.join ( '/', 'gpfsscratch', 'rech',
315                                os.path.basename ( os.path.dirname ( Path.home () )), LocalUser )
316               
317        ### ===========================================================================================
318        ### The construction of the following variables is not machine dependant
319        ### ===========================================================================================
320        if SpaceName == 'TEST' :
321            if SCRATCHDIR and not R_OUT : R_OUT = SCRATCHDIR
322            if SCRATCHDIR and not R_FIG : R_FIG = SCRATCHDIR
323        else  :
324            if ARCHIVE    and not R_OUT : R_OUT = ARCHIVE
325            if STORAGE    and not R_FIG : R_FIG = STORAGE
326        if IGCM_OUT_name :
327            R_OUT = os.path.join ( R_OUT, IGCM_OUT_name )
328            R_FIG = os.path.join ( R_FIG, IGCM_OUT_name )
329           
330        if SCRATCHDIR and not R_BUF : R_BUF  = os.path.join ( SCRATCHDIR, IGCM_OUT_name )
331        if not IGCM_OUT : IGCM_OUT = R_OUT
332           
333        if TagName and SpaceName and ExperimentName and JobName :
334            if not L_EXP : L_EXP = os.path.join ( TagName, SpaceName, ExperimentName, JobName )
335               
336            if R_OUT and not R_SAVE : R_SAVE      = os.path.join ( R_OUT  , L_EXP )
337            if IGCM_OUT_name :
338                if STORAGE and not R_FIGR : R_FIGR      = os.path.join ( STORAGE, IGCM_OUT_name, L_EXP )
339            else :
340                if STORAGE and not R_FIGR  : R_FIGR      = os.path.join ( STORAGE, L_EXP )
341            if R_BUF   and not R_BUFR      : R_BUFR      = os.path.join ( R_BUF  , L_EXP )
342            if R_BUFR  and not R_BUF_KSH   : R_BUF_KSH   = os.path.join ( R_BUFR , 'Out' )
343            if R_BUF   and not REBUILD_DIR : REBUILD_DIR = os.path.join ( R_BUF  , L_EXP, 'REBUILD' )
344            if R_BUF   and not POST_DIR    : POST_DIR    = os.path.join ( R_BUF  , L_EXP, 'Out' )
345               
346        ### ===========================================================================================
347        ## Builds the class components
348       
349        self.TagName        = TagName
350        self.SpaceName      = SpaceName
351        self.ExperimentName = ExperimentName
352        self.JobName        = JobName
353        self.LongName       = LongName
354        self.ModelName      = ModelName
355        self.ShortName      = ShortName
356        self.ConfigCard     = ConfigCard
357        self.ARCHIVE        = ARCHIVE
358        self.STORAGE        = STORAGE
359        self.SCRATCHDIR     = SCRATCHDIR
360        self.R_OUT          = R_OUT
361        self.R_BUF          = R_BUFR
362        self.R_GRAF         = R_GRAF
363        self.DB             = DB
364        self.IGCM_OUT       = IGCM_OUT
365        self.R_SAVE         = R_SAVE
366        self.R_FIGR         = R_FIGR
367        self.R_BUFR         = R_BUFR
368        self.REBUILD_DIR    = REBUILD_DIR
369        self.POST_DIR       = POST_DIR
370        self.R_IN           = R_IN
371        self.Mach           = Host
372        self.Source         = Source
373        self.User           = User
374        self.Group          = Group
375        self.IGCM_OUT_name  = IGCM_OUT_name
376        self.rebuild        = rebuild
377        self.TmpDir         = TmpDir
378        self.TGCC_User      = TGCC_User
379        self.TGCC_Group     = TGCC_Group
380        self.Thredds        = ThreddsPrefix
381        self.IDRIS_User     = IDRIS_User
382        self.IDRIS_Group    = IDRIS_Group
383
384 
Note: See TracBrowser for help on using the repository browser.