We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Misleading error message in case_setup.py 
I suggest changing "%s NINST value %d greater than %s NTASKS %d" 
(e.g ERROR: ATM NINST value 1 greater than ATM NTASKS 0)

to 

"NINST_%s value %d greater than NTASKS_%s %d
(e.g. ERROR: NINST_ATM value 1 greater than NTASKS_ATM 0)

to reflect the real variable name which can be queried or changed with xmlquery/xmlchange


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `utils/python/CIME/case_setup.py`
Content:
```
1 """
2 Library for case.setup.
3 """
4 
5 from CIME.XML.standard_module_setup import *
6 
7 from CIME.check_lockedfiles import check_lockedfiles
8 from CIME.preview_namelists import preview_namelists
9 from CIME.XML.env_mach_pes  import EnvMachPes
10 from CIME.XML.component     import Component
11 from CIME.XML.compilers     import Compilers
12 from CIME.utils             import expect, run_cmd, append_status
13 
14 import shutil, time, glob
15 
16 logger = logging.getLogger(__name__)
17 
18 ###############################################################################
19 def _check_pelayouts_require_rebuild(case, models):
20 ###############################################################################
21     """
22     Create if we require a rebuild, expects cwd is caseroot
23     """
24     locked_pes = "LockedFiles/env_mach_pes.xml"
25     if os.path.exists(locked_pes):
26         # Look to see if $comp_PE_CHANGE_REQUIRES_REBUILD is defined
27         # for any component
28         env_mach_pes_locked = EnvMachPes(infile=locked_pes)
29         for comp in models:
30             if case.get_value("%s_PE_CHANGE_REQUIRES_REBUILD" % comp):
31                 # Changing these values in env_mach_pes.xml will force
32                 # you to clean the corresponding component
33                 old_tasks   = env_mach_pes_locked.get_value("NTASKS_%s" % comp)
34                 old_threads = env_mach_pes_locked.get_value("NTHRDS_%s" % comp)
35                 old_inst    = env_mach_pes_locked.get_value("NINST_%s" % comp)
36 
37                 new_tasks   = case.get_value("NTASKS_%s" % comp)
38                 new_threads = case.get_value("NTHRDS_%s" % comp)
39                 new_inst    = case.get_value("NINST_%s" % comp)
40 
41                 if old_tasks != new_tasks or old_threads != new_threads or old_inst != new_inst:
42                     logger.warn("%s pe change requires clean build" % comp)
43                     cleanflag = comp.lower()
44                     run_cmd("./case.build --clean %s" % cleanflag)
45 
46         os.remove(locked_pes)
47 
48 ###############################################################################
49 def _build_usernl_files(case, model, comp):
50 ###############################################################################
51     """
52     Create user_nl_xxx files, expects cwd is caseroot
53     """
54     model = model.upper()
55     model_file = case.get_value("CONFIG_%s_FILE" % model)
56     model_dir = os.path.dirname(model_file)
57 
58     expect(os.path.isdir(model_dir),
59            "cannot find cime_config directory %s for component %s" % (model_dir, comp))
60 
61     if comp == "cpl":
62         if not os.path.exists("user_nl_cpl"):
63             shutil.copy(os.path.join(model_dir, "user_nl_cpl"), ".")
64     else:
65         ninst = case.get_value("NINST_%s" % model)
66         nlfile = "user_nl_%s" % comp
67         model_nl = os.path.join(model_dir, nlfile)
68         if os.path.exists(model_nl):
69             if ninst > 1:
70                 for inst_counter in xrange(1, ninst+1):
71                     case_nlfile = "%s_%04d" % (nlfile, inst_counter)
72                     if not os.path.exists(case_nlfile):
73                         shutil.copy(model_nl, case_nlfile)
74             else:
75                 if not os.path.exists(nlfile):
76                     shutil.copy(model_nl, nlfile)
77 
78 ###############################################################################
79 def case_setup(case, clean=False, test_mode=False, reset=False):
80 ###############################################################################
81     caseroot = case.get_value("CASEROOT")
82     os.chdir(caseroot)
83     msg = "case.setup starting"
84     append_status(msg, caseroot=caseroot, sfile="CaseStatus")
85 
86     cimeroot = os.environ["CIMEROOT"]
87 
88     # Check that $DIN_LOC_ROOT exists - and abort if not a namelist compare tests
89     din_loc_root = case.get_value("DIN_LOC_ROOT")
90     testcase     = case.get_value("TESTCASE")
91     expect(not (not os.path.isdir(din_loc_root) and testcase != "SBN"),
92            "inputdata root is not a directory: \"$din_loc_root\" ")
93 
94     # Check that userdefine settings are specified before expanding variable
95     for vid, value in case:
96         expect(not (type(value) is str and "USERDEFINED_required_build" in value),
97                "Parameter '%s' must be defined" % vid)
98 
99     # Create batch script
100     if reset or clean:
101         # Clean batch script
102 
103         backup_dir = "PESetupHist/b.%s" % time.strftime("%y%m%d-%H%M%S")
104         if not os.path.isdir(backup_dir):
105             os.makedirs(backup_dir)
106 
107         # back up relevant files
108         for fileglob in ["case.run", "env_build.xml", "env_mach_pes.xml", "Macros*"]:
109             for filename in glob.glob(fileglob):
110                 shutil.copy(filename, backup_dir)
111         if os.path.exists("case.run"):
112             os.remove("case.run")
113 
114         # only do the following if are NOT in testmode
115         if not test_mode:
116             # rebuild the models (even on restart)
117             case.set_value("BUILD_COMPLETE", False)
118 
119             # backup and then clean test script
120             if os.path.exists("case.test"):
121                 shutil.copy("case.test", backup_dir)
122                 os.remove("case.test")
123                 logger.info("Successfully cleaned test script case.test")
124 
125             if os.path.exists("case.testdriver"):
126                 shutil.copy("case.testdriver", backup_dir)
127                 os.remove("case.testdriver")
128                 logger.info("Successfully cleaned test script case.testdriver")
129 
130         logger.info("Successfully cleaned batch script case.run")
131 
132         logger.info("Successfully cleaned batch script case.run")
133         logger.info("Some files have been saved to %s" % backup_dir)
134 
135         msg = "case.setup clean complete"
136         append_status(msg, caseroot=caseroot, sfile="CaseStatus")
137 
138     if not clean:
139         drv_comp = Component()
140         models = drv_comp.get_valid_model_components()
141         models.remove("DRV")
142 
143         mach = case.get_value("MACH")
144         expect(mach is not None, "xml variable MACH is not set")
145 
146         # Create Macros file only if it does not exist
147         if not os.path.exists("Macros"):
148             logger.debug("Creating Macros file for %s" % mach)
149             compilers = Compilers(compiler=case.get_value("COMPILER"), machine=mach, os_=case.get_value("OS"), mpilib=case.get_value("MPILIB"))
150             compilers.write_macros_file()
151         else:
152             logger.debug("Macros script already created ...skipping")
153 
154         # Set tasks to 1 if mpi-serial library
155         if case.get_value("MPILIB") == "mpi-serial":
156             for vid, value in case:
157                 if vid.startswith("NTASKS_") and value != 1:
158                     case.set_value(vid, 1)
159 
160         # Check ninst.
161         # In CIME there can be multiple instances of each component model (an ensemble) NINST is the instance of that component.
162         for comp in models:
163             ninst  = case.get_value("NINST_%s" % comp)
164             ntasks = case.get_value("NTASKS_%s" % comp)
165             if ninst > ntasks:
166                 if ntasks == 1:
167                     case.set_value("NTASKS_%s" % comp, ninst)
168                 else:
169                     expect(False, "%s NINST value %d greater than %s NTASKS %d" % (comp, ninst, comp, ntasks))
170 
171         expect(not (case.get_value("BUILD_THREADED") and case.get_value("COMPILER") == "nag"),
172                "it is not possible to run with OpenMP if using the NAG Fortran compiler")
173 
174         if os.path.exists("case.run"):
175             logger.info("Machine/Decomp/Pes configuration has already been done ...skipping")
176         else:
177             _check_pelayouts_require_rebuild(case, models)
178 
179             if os.path.exists("LockedFiles/env_build.xml"):
180                 os.remove("LockedFiles/env_build.xml")
181 
182             case.flush()
183             check_lockedfiles()
184 
185             pestot = int(run_cmd("Tools/taskmaker.pl -sumonly"))
186             case.set_value("TOTALPES", pestot)
187 
188             # Compute cost based on PE count
189             pval = 1
190             pcnt = 0
191             while pval < pestot:
192                 pval *= 2
193                 pcnt += 6 # (scaling like sqrt(6/10))
194             pcost = 3 - pcnt / 10 # (3 is 64 with 6)
195 
196             # Compute cost based on DEBUG
197             dcost = 3 if case.get_value("DEBUG") else 0
198 
199             # Compute cost based on run length
200             # For simplicity, we use a heuristic just based on STOP_OPTION (not considering
201             # STOP_N), and only deal with options longer than ndays
202             lcost = 0
203             if "nmonth" in case.get_value("STOP_OPTION"):
204                 # N months costs 30x as much as N days; since cost is based on log-base-2, add 5
205                 lcost = 5
206             elif "nyear" in case.get_value("STOP_OPTION"):
207                 # N years costs 365x as much as N days; since cost is based on log-base-2, add 9
208                 lcost = 9
209 
210             estcost = pcost + dcost + lcost
211             for cost in ["CCSM_CCOST", "CCSM_GCOST", "CCSM_TCOST", "CCSM_CCOST"]:
212                 estcost += case.get_value(cost)
213 
214             case.set_value("CCSM_PCOST", pcost)
215             case.set_value("CCSM_ESTCOST", estcost)
216 
217             # create batch file
218             logger.info("Creating batch script case.run")
219 
220             # Use BatchFactory to get the appropriate instance of a BatchMaker,
221             # use it to create our batch scripts
222             env_batch = case._get_env("batch")
223             for job in env_batch.get_jobs():
224                 input_batch_script  = os.path.join(case.get_value("MACHDIR"), env_batch.get_value('template', subgroup=job))
225                 if job == "case.test" and testcase is not None and not test_mode:
226                     logger.info("Writing %s script" % job)
227                     testscript = os.path.join(cimeroot, "scripts", "Testing", "Testcases", "%s_script" % testcase)
228                     # Short term fix to be removed when csh tests are removed
229                     if not os.path.exists(testscript):
230                         env_batch.make_batch_script(input_batch_script, job, case)
231                 elif job != "case.test":
232                     logger.info("Writing %s script" % job)
233                     env_batch.make_batch_script(input_batch_script, job, case)
234 
235             # Make a copy of env_mach_pes.xml in order to be able
236             # to check that it does not change once case.setup is invoked
237             logger.info("Locking file env_mach_pes.xml")
238             case.flush()
239             shutil.copy("env_mach_pes.xml", "LockedFiles")
240 
241         # Create user_nl files for the required number of instances
242         if not os.path.exists("user_nl_cpl"):
243             logger.info("Creating user_nl_xxx files for components and cpl")
244         # loop over models
245         for model in models:
246             comp = case.get_value("COMP_%s" % model)
247             logger.info("Building %s usernl files"%model)
248             _build_usernl_files(case, model, comp)
249             if comp == "cism":
250                 run_cmd("%s/../components/cism/cime_config/cism.template %s" % (cimeroot, caseroot))
251 
252         _build_usernl_files(case, "drv", "cpl")
253 
254         # Run preview namelists for scripts
255         logger.info("preview_namelists")
256         preview_namelists(case)
257 
258         logger.info("See ./CaseDoc for component namelists")
259         logger.info("If an old case build already exists, might want to run \'case.build --clean-all\' before building")
260 
261         # Create test script if appropriate
262         # Short term fix to be removed when csh tests are removed
263         if os.path.exists("env_test.xml"):
264             if not os.path.exists("case.test"):
265                 logger.info("Starting testcase.setup")
266                 run_cmd("./testcase.setup -caseroot %s" % caseroot)
267                 logger.info("Finished testcase.setup")
268 
269         msg = "case.setup complete"
270         append_status(msg, caseroot=caseroot, sfile="CaseStatus")
271 
272
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/utils/python/CIME/case_setup.py b/utils/python/CIME/case_setup.py
--- a/utils/python/CIME/case_setup.py
+++ b/utils/python/CIME/case_setup.py
@@ -166,7 +166,7 @@
                 if ntasks == 1:
                     case.set_value("NTASKS_%s" % comp, ninst)
                 else:
-                    expect(False, "%s NINST value %d greater than %s NTASKS %d" % (comp, ninst, comp, ntasks))
+                    expect(False, "NINST_%s value %d greater than NTASKS_%s %d" % (comp, ninst, comp, ntasks))
 
         expect(not (case.get_value("BUILD_THREADED") and case.get_value("COMPILER") == "nag"),
                "it is not possible to run with OpenMP if using the NAG Fortran compiler")

{"golden_diff": "diff --git a/utils/python/CIME/case_setup.py b/utils/python/CIME/case_setup.py\n--- a/utils/python/CIME/case_setup.py\n+++ b/utils/python/CIME/case_setup.py\n@@ -166,7 +166,7 @@\n                 if ntasks == 1:\n                     case.set_value(\"NTASKS_%s\" % comp, ninst)\n                 else:\n-                    expect(False, \"%s NINST value %d greater than %s NTASKS %d\" % (comp, ninst, comp, ntasks))\n+                    expect(False, \"NINST_%s value %d greater than NTASKS_%s %d\" % (comp, ninst, comp, ntasks))\n \n         expect(not (case.get_value(\"BUILD_THREADED\") and case.get_value(\"COMPILER\") == \"nag\"),\n                \"it is not possible to run with OpenMP if using the NAG Fortran compiler\")\n", "issue": "Misleading error message in case_setup.py \nI suggest changing \"%s NINST value %d greater than %s NTASKS %d\" \n(e.g ERROR: ATM NINST value 1 greater than ATM NTASKS 0)\n\nto \n\n\"NINST_%s value %d greater than NTASKS_%s %d\n(e.g. ERROR: NINST_ATM value 1 greater than NTASKS_ATM 0)\n\nto reflect the real variable name which can be queried or changed with xmlquery/xmlchange\n\n", "before_files": [{"content": "\"\"\"\nLibrary for case.setup.\n\"\"\"\n\nfrom CIME.XML.standard_module_setup import *\n\nfrom CIME.check_lockedfiles import check_lockedfiles\nfrom CIME.preview_namelists import preview_namelists\nfrom CIME.XML.env_mach_pes  import EnvMachPes\nfrom CIME.XML.component     import Component\nfrom CIME.XML.compilers     import Compilers\nfrom CIME.utils             import expect, run_cmd, append_status\n\nimport shutil, time, glob\n\nlogger = logging.getLogger(__name__)\n\n###############################################################################\ndef _check_pelayouts_require_rebuild(case, models):\n###############################################################################\n    \"\"\"\n    Create if we require a rebuild, expects cwd is caseroot\n    \"\"\"\n    locked_pes = \"LockedFiles/env_mach_pes.xml\"\n    if os.path.exists(locked_pes):\n        # Look to see if $comp_PE_CHANGE_REQUIRES_REBUILD is defined\n        # for any component\n        env_mach_pes_locked = EnvMachPes(infile=locked_pes)\n        for comp in models:\n            if case.get_value(\"%s_PE_CHANGE_REQUIRES_REBUILD\" % comp):\n                # Changing these values in env_mach_pes.xml will force\n                # you to clean the corresponding component\n                old_tasks   = env_mach_pes_locked.get_value(\"NTASKS_%s\" % comp)\n                old_threads = env_mach_pes_locked.get_value(\"NTHRDS_%s\" % comp)\n                old_inst    = env_mach_pes_locked.get_value(\"NINST_%s\" % comp)\n\n                new_tasks   = case.get_value(\"NTASKS_%s\" % comp)\n                new_threads = case.get_value(\"NTHRDS_%s\" % comp)\n                new_inst    = case.get_value(\"NINST_%s\" % comp)\n\n                if old_tasks != new_tasks or old_threads != new_threads or old_inst != new_inst:\n                    logger.warn(\"%s pe change requires clean build\" % comp)\n                    cleanflag = comp.lower()\n                    run_cmd(\"./case.build --clean %s\" % cleanflag)\n\n        os.remove(locked_pes)\n\n###############################################################################\ndef _build_usernl_files(case, model, comp):\n###############################################################################\n    \"\"\"\n    Create user_nl_xxx files, expects cwd is caseroot\n    \"\"\"\n    model = model.upper()\n    model_file = case.get_value(\"CONFIG_%s_FILE\" % model)\n    model_dir = os.path.dirname(model_file)\n\n    expect(os.path.isdir(model_dir),\n           \"cannot find cime_config directory %s for component %s\" % (model_dir, comp))\n\n    if comp == \"cpl\":\n        if not os.path.exists(\"user_nl_cpl\"):\n            shutil.copy(os.path.join(model_dir, \"user_nl_cpl\"), \".\")\n    else:\n        ninst = case.get_value(\"NINST_%s\" % model)\n        nlfile = \"user_nl_%s\" % comp\n        model_nl = os.path.join(model_dir, nlfile)\n        if os.path.exists(model_nl):\n            if ninst > 1:\n                for inst_counter in xrange(1, ninst+1):\n                    case_nlfile = \"%s_%04d\" % (nlfile, inst_counter)\n                    if not os.path.exists(case_nlfile):\n                        shutil.copy(model_nl, case_nlfile)\n            else:\n                if not os.path.exists(nlfile):\n                    shutil.copy(model_nl, nlfile)\n\n###############################################################################\ndef case_setup(case, clean=False, test_mode=False, reset=False):\n###############################################################################\n    caseroot = case.get_value(\"CASEROOT\")\n    os.chdir(caseroot)\n    msg = \"case.setup starting\"\n    append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n    cimeroot = os.environ[\"CIMEROOT\"]\n\n    # Check that $DIN_LOC_ROOT exists - and abort if not a namelist compare tests\n    din_loc_root = case.get_value(\"DIN_LOC_ROOT\")\n    testcase     = case.get_value(\"TESTCASE\")\n    expect(not (not os.path.isdir(din_loc_root) and testcase != \"SBN\"),\n           \"inputdata root is not a directory: \\\"$din_loc_root\\\" \")\n\n    # Check that userdefine settings are specified before expanding variable\n    for vid, value in case:\n        expect(not (type(value) is str and \"USERDEFINED_required_build\" in value),\n               \"Parameter '%s' must be defined\" % vid)\n\n    # Create batch script\n    if reset or clean:\n        # Clean batch script\n\n        backup_dir = \"PESetupHist/b.%s\" % time.strftime(\"%y%m%d-%H%M%S\")\n        if not os.path.isdir(backup_dir):\n            os.makedirs(backup_dir)\n\n        # back up relevant files\n        for fileglob in [\"case.run\", \"env_build.xml\", \"env_mach_pes.xml\", \"Macros*\"]:\n            for filename in glob.glob(fileglob):\n                shutil.copy(filename, backup_dir)\n        if os.path.exists(\"case.run\"):\n            os.remove(\"case.run\")\n\n        # only do the following if are NOT in testmode\n        if not test_mode:\n            # rebuild the models (even on restart)\n            case.set_value(\"BUILD_COMPLETE\", False)\n\n            # backup and then clean test script\n            if os.path.exists(\"case.test\"):\n                shutil.copy(\"case.test\", backup_dir)\n                os.remove(\"case.test\")\n                logger.info(\"Successfully cleaned test script case.test\")\n\n            if os.path.exists(\"case.testdriver\"):\n                shutil.copy(\"case.testdriver\", backup_dir)\n                os.remove(\"case.testdriver\")\n                logger.info(\"Successfully cleaned test script case.testdriver\")\n\n        logger.info(\"Successfully cleaned batch script case.run\")\n\n        logger.info(\"Successfully cleaned batch script case.run\")\n        logger.info(\"Some files have been saved to %s\" % backup_dir)\n\n        msg = \"case.setup clean complete\"\n        append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n    if not clean:\n        drv_comp = Component()\n        models = drv_comp.get_valid_model_components()\n        models.remove(\"DRV\")\n\n        mach = case.get_value(\"MACH\")\n        expect(mach is not None, \"xml variable MACH is not set\")\n\n        # Create Macros file only if it does not exist\n        if not os.path.exists(\"Macros\"):\n            logger.debug(\"Creating Macros file for %s\" % mach)\n            compilers = Compilers(compiler=case.get_value(\"COMPILER\"), machine=mach, os_=case.get_value(\"OS\"), mpilib=case.get_value(\"MPILIB\"))\n            compilers.write_macros_file()\n        else:\n            logger.debug(\"Macros script already created ...skipping\")\n\n        # Set tasks to 1 if mpi-serial library\n        if case.get_value(\"MPILIB\") == \"mpi-serial\":\n            for vid, value in case:\n                if vid.startswith(\"NTASKS_\") and value != 1:\n                    case.set_value(vid, 1)\n\n        # Check ninst.\n        # In CIME there can be multiple instances of each component model (an ensemble) NINST is the instance of that component.\n        for comp in models:\n            ninst  = case.get_value(\"NINST_%s\" % comp)\n            ntasks = case.get_value(\"NTASKS_%s\" % comp)\n            if ninst > ntasks:\n                if ntasks == 1:\n                    case.set_value(\"NTASKS_%s\" % comp, ninst)\n                else:\n                    expect(False, \"%s NINST value %d greater than %s NTASKS %d\" % (comp, ninst, comp, ntasks))\n\n        expect(not (case.get_value(\"BUILD_THREADED\") and case.get_value(\"COMPILER\") == \"nag\"),\n               \"it is not possible to run with OpenMP if using the NAG Fortran compiler\")\n\n        if os.path.exists(\"case.run\"):\n            logger.info(\"Machine/Decomp/Pes configuration has already been done ...skipping\")\n        else:\n            _check_pelayouts_require_rebuild(case, models)\n\n            if os.path.exists(\"LockedFiles/env_build.xml\"):\n                os.remove(\"LockedFiles/env_build.xml\")\n\n            case.flush()\n            check_lockedfiles()\n\n            pestot = int(run_cmd(\"Tools/taskmaker.pl -sumonly\"))\n            case.set_value(\"TOTALPES\", pestot)\n\n            # Compute cost based on PE count\n            pval = 1\n            pcnt = 0\n            while pval < pestot:\n                pval *= 2\n                pcnt += 6 # (scaling like sqrt(6/10))\n            pcost = 3 - pcnt / 10 # (3 is 64 with 6)\n\n            # Compute cost based on DEBUG\n            dcost = 3 if case.get_value(\"DEBUG\") else 0\n\n            # Compute cost based on run length\n            # For simplicity, we use a heuristic just based on STOP_OPTION (not considering\n            # STOP_N), and only deal with options longer than ndays\n            lcost = 0\n            if \"nmonth\" in case.get_value(\"STOP_OPTION\"):\n                # N months costs 30x as much as N days; since cost is based on log-base-2, add 5\n                lcost = 5\n            elif \"nyear\" in case.get_value(\"STOP_OPTION\"):\n                # N years costs 365x as much as N days; since cost is based on log-base-2, add 9\n                lcost = 9\n\n            estcost = pcost + dcost + lcost\n            for cost in [\"CCSM_CCOST\", \"CCSM_GCOST\", \"CCSM_TCOST\", \"CCSM_CCOST\"]:\n                estcost += case.get_value(cost)\n\n            case.set_value(\"CCSM_PCOST\", pcost)\n            case.set_value(\"CCSM_ESTCOST\", estcost)\n\n            # create batch file\n            logger.info(\"Creating batch script case.run\")\n\n            # Use BatchFactory to get the appropriate instance of a BatchMaker,\n            # use it to create our batch scripts\n            env_batch = case._get_env(\"batch\")\n            for job in env_batch.get_jobs():\n                input_batch_script  = os.path.join(case.get_value(\"MACHDIR\"), env_batch.get_value('template', subgroup=job))\n                if job == \"case.test\" and testcase is not None and not test_mode:\n                    logger.info(\"Writing %s script\" % job)\n                    testscript = os.path.join(cimeroot, \"scripts\", \"Testing\", \"Testcases\", \"%s_script\" % testcase)\n                    # Short term fix to be removed when csh tests are removed\n                    if not os.path.exists(testscript):\n                        env_batch.make_batch_script(input_batch_script, job, case)\n                elif job != \"case.test\":\n                    logger.info(\"Writing %s script\" % job)\n                    env_batch.make_batch_script(input_batch_script, job, case)\n\n            # Make a copy of env_mach_pes.xml in order to be able\n            # to check that it does not change once case.setup is invoked\n            logger.info(\"Locking file env_mach_pes.xml\")\n            case.flush()\n            shutil.copy(\"env_mach_pes.xml\", \"LockedFiles\")\n\n        # Create user_nl files for the required number of instances\n        if not os.path.exists(\"user_nl_cpl\"):\n            logger.info(\"Creating user_nl_xxx files for components and cpl\")\n        # loop over models\n        for model in models:\n            comp = case.get_value(\"COMP_%s\" % model)\n            logger.info(\"Building %s usernl files\"%model)\n            _build_usernl_files(case, model, comp)\n            if comp == \"cism\":\n                run_cmd(\"%s/../components/cism/cime_config/cism.template %s\" % (cimeroot, caseroot))\n\n        _build_usernl_files(case, \"drv\", \"cpl\")\n\n        # Run preview namelists for scripts\n        logger.info(\"preview_namelists\")\n        preview_namelists(case)\n\n        logger.info(\"See ./CaseDoc for component namelists\")\n        logger.info(\"If an old case build already exists, might want to run \\'case.build --clean-all\\' before building\")\n\n        # Create test script if appropriate\n        # Short term fix to be removed when csh tests are removed\n        if os.path.exists(\"env_test.xml\"):\n            if not os.path.exists(\"case.test\"):\n                logger.info(\"Starting testcase.setup\")\n                run_cmd(\"./testcase.setup -caseroot %s\" % caseroot)\n                logger.info(\"Finished testcase.setup\")\n\n        msg = \"case.setup complete\"\n        append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n", "path": "utils/python/CIME/case_setup.py"}], "after_files": [{"content": "\"\"\"\nLibrary for case.setup.\n\"\"\"\n\nfrom CIME.XML.standard_module_setup import *\n\nfrom CIME.check_lockedfiles import check_lockedfiles\nfrom CIME.preview_namelists import preview_namelists\nfrom CIME.XML.env_mach_pes  import EnvMachPes\nfrom CIME.XML.component     import Component\nfrom CIME.XML.compilers     import Compilers\nfrom CIME.utils             import expect, run_cmd, append_status\n\nimport shutil, time, glob\n\nlogger = logging.getLogger(__name__)\n\n###############################################################################\ndef _check_pelayouts_require_rebuild(case, models):\n###############################################################################\n    \"\"\"\n    Create if we require a rebuild, expects cwd is caseroot\n    \"\"\"\n    locked_pes = \"LockedFiles/env_mach_pes.xml\"\n    if os.path.exists(locked_pes):\n        # Look to see if $comp_PE_CHANGE_REQUIRES_REBUILD is defined\n        # for any component\n        env_mach_pes_locked = EnvMachPes(infile=locked_pes)\n        for comp in models:\n            if case.get_value(\"%s_PE_CHANGE_REQUIRES_REBUILD\" % comp):\n                # Changing these values in env_mach_pes.xml will force\n                # you to clean the corresponding component\n                old_tasks   = env_mach_pes_locked.get_value(\"NTASKS_%s\" % comp)\n                old_threads = env_mach_pes_locked.get_value(\"NTHRDS_%s\" % comp)\n                old_inst    = env_mach_pes_locked.get_value(\"NINST_%s\" % comp)\n\n                new_tasks   = case.get_value(\"NTASKS_%s\" % comp)\n                new_threads = case.get_value(\"NTHRDS_%s\" % comp)\n                new_inst    = case.get_value(\"NINST_%s\" % comp)\n\n                if old_tasks != new_tasks or old_threads != new_threads or old_inst != new_inst:\n                    logger.warn(\"%s pe change requires clean build\" % comp)\n                    cleanflag = comp.lower()\n                    run_cmd(\"./case.build --clean %s\" % cleanflag)\n\n        os.remove(locked_pes)\n\n###############################################################################\ndef _build_usernl_files(case, model, comp):\n###############################################################################\n    \"\"\"\n    Create user_nl_xxx files, expects cwd is caseroot\n    \"\"\"\n    model = model.upper()\n    model_file = case.get_value(\"CONFIG_%s_FILE\" % model)\n    model_dir = os.path.dirname(model_file)\n\n    expect(os.path.isdir(model_dir),\n           \"cannot find cime_config directory %s for component %s\" % (model_dir, comp))\n\n    if comp == \"cpl\":\n        if not os.path.exists(\"user_nl_cpl\"):\n            shutil.copy(os.path.join(model_dir, \"user_nl_cpl\"), \".\")\n    else:\n        ninst = case.get_value(\"NINST_%s\" % model)\n        nlfile = \"user_nl_%s\" % comp\n        model_nl = os.path.join(model_dir, nlfile)\n        if os.path.exists(model_nl):\n            if ninst > 1:\n                for inst_counter in xrange(1, ninst+1):\n                    case_nlfile = \"%s_%04d\" % (nlfile, inst_counter)\n                    if not os.path.exists(case_nlfile):\n                        shutil.copy(model_nl, case_nlfile)\n            else:\n                if not os.path.exists(nlfile):\n                    shutil.copy(model_nl, nlfile)\n\n###############################################################################\ndef case_setup(case, clean=False, test_mode=False, reset=False):\n###############################################################################\n    caseroot = case.get_value(\"CASEROOT\")\n    os.chdir(caseroot)\n    msg = \"case.setup starting\"\n    append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n    cimeroot = os.environ[\"CIMEROOT\"]\n\n    # Check that $DIN_LOC_ROOT exists - and abort if not a namelist compare tests\n    din_loc_root = case.get_value(\"DIN_LOC_ROOT\")\n    testcase     = case.get_value(\"TESTCASE\")\n    expect(not (not os.path.isdir(din_loc_root) and testcase != \"SBN\"),\n           \"inputdata root is not a directory: \\\"$din_loc_root\\\" \")\n\n    # Check that userdefine settings are specified before expanding variable\n    for vid, value in case:\n        expect(not (type(value) is str and \"USERDEFINED_required_build\" in value),\n               \"Parameter '%s' must be defined\" % vid)\n\n    # Create batch script\n    if reset or clean:\n        # Clean batch script\n\n        backup_dir = \"PESetupHist/b.%s\" % time.strftime(\"%y%m%d-%H%M%S\")\n        if not os.path.isdir(backup_dir):\n            os.makedirs(backup_dir)\n\n        # back up relevant files\n        for fileglob in [\"case.run\", \"env_build.xml\", \"env_mach_pes.xml\", \"Macros*\"]:\n            for filename in glob.glob(fileglob):\n                shutil.copy(filename, backup_dir)\n        if os.path.exists(\"case.run\"):\n            os.remove(\"case.run\")\n\n        # only do the following if are NOT in testmode\n        if not test_mode:\n            # rebuild the models (even on restart)\n            case.set_value(\"BUILD_COMPLETE\", False)\n\n            # backup and then clean test script\n            if os.path.exists(\"case.test\"):\n                shutil.copy(\"case.test\", backup_dir)\n                os.remove(\"case.test\")\n                logger.info(\"Successfully cleaned test script case.test\")\n\n            if os.path.exists(\"case.testdriver\"):\n                shutil.copy(\"case.testdriver\", backup_dir)\n                os.remove(\"case.testdriver\")\n                logger.info(\"Successfully cleaned test script case.testdriver\")\n\n        logger.info(\"Successfully cleaned batch script case.run\")\n\n        logger.info(\"Successfully cleaned batch script case.run\")\n        logger.info(\"Some files have been saved to %s\" % backup_dir)\n\n        msg = \"case.setup clean complete\"\n        append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n    if not clean:\n        drv_comp = Component()\n        models = drv_comp.get_valid_model_components()\n        models.remove(\"DRV\")\n\n        mach = case.get_value(\"MACH\")\n        expect(mach is not None, \"xml variable MACH is not set\")\n\n        # Create Macros file only if it does not exist\n        if not os.path.exists(\"Macros\"):\n            logger.debug(\"Creating Macros file for %s\" % mach)\n            compilers = Compilers(compiler=case.get_value(\"COMPILER\"), machine=mach, os_=case.get_value(\"OS\"), mpilib=case.get_value(\"MPILIB\"))\n            compilers.write_macros_file()\n        else:\n            logger.debug(\"Macros script already created ...skipping\")\n\n        # Set tasks to 1 if mpi-serial library\n        if case.get_value(\"MPILIB\") == \"mpi-serial\":\n            for vid, value in case:\n                if vid.startswith(\"NTASKS_\") and value != 1:\n                    case.set_value(vid, 1)\n\n        # Check ninst.\n        # In CIME there can be multiple instances of each component model (an ensemble) NINST is the instance of that component.\n        for comp in models:\n            ninst  = case.get_value(\"NINST_%s\" % comp)\n            ntasks = case.get_value(\"NTASKS_%s\" % comp)\n            if ninst > ntasks:\n                if ntasks == 1:\n                    case.set_value(\"NTASKS_%s\" % comp, ninst)\n                else:\n                    expect(False, \"NINST_%s value %d greater than NTASKS_%s %d\" % (comp, ninst, comp, ntasks))\n\n        expect(not (case.get_value(\"BUILD_THREADED\") and case.get_value(\"COMPILER\") == \"nag\"),\n               \"it is not possible to run with OpenMP if using the NAG Fortran compiler\")\n\n        if os.path.exists(\"case.run\"):\n            logger.info(\"Machine/Decomp/Pes configuration has already been done ...skipping\")\n        else:\n            _check_pelayouts_require_rebuild(case, models)\n\n            if os.path.exists(\"LockedFiles/env_build.xml\"):\n                os.remove(\"LockedFiles/env_build.xml\")\n\n            case.flush()\n            check_lockedfiles()\n\n            pestot = int(run_cmd(\"Tools/taskmaker.pl -sumonly\"))\n            case.set_value(\"TOTALPES\", pestot)\n\n            # Compute cost based on PE count\n            pval = 1\n            pcnt = 0\n            while pval < pestot:\n                pval *= 2\n                pcnt += 6 # (scaling like sqrt(6/10))\n            pcost = 3 - pcnt / 10 # (3 is 64 with 6)\n\n            # Compute cost based on DEBUG\n            dcost = 3 if case.get_value(\"DEBUG\") else 0\n\n            # Compute cost based on run length\n            # For simplicity, we use a heuristic just based on STOP_OPTION (not considering\n            # STOP_N), and only deal with options longer than ndays\n            lcost = 0\n            if \"nmonth\" in case.get_value(\"STOP_OPTION\"):\n                # N months costs 30x as much as N days; since cost is based on log-base-2, add 5\n                lcost = 5\n            elif \"nyear\" in case.get_value(\"STOP_OPTION\"):\n                # N years costs 365x as much as N days; since cost is based on log-base-2, add 9\n                lcost = 9\n\n            estcost = pcost + dcost + lcost\n            for cost in [\"CCSM_CCOST\", \"CCSM_GCOST\", \"CCSM_TCOST\", \"CCSM_CCOST\"]:\n                estcost += case.get_value(cost)\n\n            case.set_value(\"CCSM_PCOST\", pcost)\n            case.set_value(\"CCSM_ESTCOST\", estcost)\n\n            # create batch file\n            logger.info(\"Creating batch script case.run\")\n\n            # Use BatchFactory to get the appropriate instance of a BatchMaker,\n            # use it to create our batch scripts\n            env_batch = case._get_env(\"batch\")\n            for job in env_batch.get_jobs():\n                input_batch_script  = os.path.join(case.get_value(\"MACHDIR\"), env_batch.get_value('template', subgroup=job))\n                if job == \"case.test\" and testcase is not None and not test_mode:\n                    logger.info(\"Writing %s script\" % job)\n                    testscript = os.path.join(cimeroot, \"scripts\", \"Testing\", \"Testcases\", \"%s_script\" % testcase)\n                    # Short term fix to be removed when csh tests are removed\n                    if not os.path.exists(testscript):\n                        env_batch.make_batch_script(input_batch_script, job, case)\n                elif job != \"case.test\":\n                    logger.info(\"Writing %s script\" % job)\n                    env_batch.make_batch_script(input_batch_script, job, case)\n\n            # Make a copy of env_mach_pes.xml in order to be able\n            # to check that it does not change once case.setup is invoked\n            logger.info(\"Locking file env_mach_pes.xml\")\n            case.flush()\n            shutil.copy(\"env_mach_pes.xml\", \"LockedFiles\")\n\n        # Create user_nl files for the required number of instances\n        if not os.path.exists(\"user_nl_cpl\"):\n            logger.info(\"Creating user_nl_xxx files for components and cpl\")\n        # loop over models\n        for model in models:\n            comp = case.get_value(\"COMP_%s\" % model)\n            logger.info(\"Building %s usernl files\"%model)\n            _build_usernl_files(case, model, comp)\n            if comp == \"cism\":\n                run_cmd(\"%s/../components/cism/cime_config/cism.template %s\" % (cimeroot, caseroot))\n\n        _build_usernl_files(case, \"drv\", \"cpl\")\n\n        # Run preview namelists for scripts\n        logger.info(\"preview_namelists\")\n        preview_namelists(case)\n\n        logger.info(\"See ./CaseDoc for component namelists\")\n        logger.info(\"If an old case build already exists, might want to run \\'case.build --clean-all\\' before building\")\n\n        # Create test script if appropriate\n        # Short term fix to be removed when csh tests are removed\n        if os.path.exists(\"env_test.xml\"):\n            if not os.path.exists(\"case.test\"):\n                logger.info(\"Starting testcase.setup\")\n                run_cmd(\"./testcase.setup -caseroot %s\" % caseroot)\n                logger.info(\"Finished testcase.setup\")\n\n        msg = \"case.setup complete\"\n        append_status(msg, caseroot=caseroot, sfile=\"CaseStatus\")\n\n", "path": "utils/python/CIME/case_setup.py"}]}