Repository: ESMCI/cime

Issue:
xmlchange should detect if it is in a case directory or not
I was helping a user update the account key charged by a case, and instructed her to run

```
$ ./xmlchange PROJECT=#####
```

I wasn't clear about where to run this, though, and she ran the command from `cime/scripts/Tools` rather than her case root. The result of such a mistake is

```
$ git status
HEAD detached at cime5.8.42
Untracked files:
  (use "git add <file>..." to include in what will be committed)

	scripts/Tools/env_archive.xml
	scripts/Tools/env_batch.xml
	scripts/Tools/env_build.xml
	scripts/Tools/env_case.xml
	scripts/Tools/env_mach_pes.xml
	scripts/Tools/env_mach_specific.xml
	scripts/Tools/env_run.xml
	scripts/Tools/env_workflow.xml
```

It would be great if, rather than blindly creating a set of xml files, `xmlchange` reported an error along the lines of "This command must be run from a case directory." I talked with @billsacks about this, and we came up with two possibilities:

1. add a check in the [`xmlchange()`](https://github.com/ESMCI/cime/blob/1e2bbff/scripts/Tools/xmlchange#L183) function before creating the `Case` object. This would be easy to implement but would not catch issues with running other scripts in the wrong location
2. add a check in the `Case` class itself. This would be a little bit more involved, and obviously there are some instances (such as `create_newcase`) where we want to allow a new `Case` object to generate files if `caseroot` is not currently a directory, so this would require a flag of some sort in the constructor... but it would probably be worth the extra effort to follow this path. I would recommend have the default for this flag be to report an error if `caseroot` is not already a case dir, and then explicitly allow scripts like `create_newcase` to proceed anyway.


Discussion:
I tried to reproduce this problem in both maint-5.6 and the master and do not get the git status output you posted here - is that what you meant to post?  However I see that these files are added to the directory where you ran xmlchange.  If you run a second time you get an xmllint error. 
How about (2) where the caseroot is either expected to not exist or to already have an env_case.xml file present within?
> I tried to reproduce this problem in both maint-5.6 and the master and do not get the git status output you posted here - is that what you meant to post? However I see that these files are added to the directory where you ran xmlchange. If you run a second time you get an xmllint error.

I wasn't clear in the first post, but that is the output of running `git status` from the root cime directory... so I think we are seeing the same behavior (running `./xmlchange` directly from `scripts/Tools` created files in that directory)
```
 ./xmlchange PROJECT=P93300606
ERROR: Directory /glade/work/jedwards/sandboxes/cesm2_1/cime/scripts/Tools does not appear to be a valid case directory
```
> ```
>  ./xmlchange PROJECT=P93300606
> ERROR: Directory /glade/work/jedwards/sandboxes/cesm2_1/cime/scripts/Tools does not appear to be a valid case directory
> ```

that's perfect! Would've saved me two or three emails in trying to figure out what was happening with her case :)

Resolving patch:
diff --git a/scripts/lib/CIME/case/case.py b/scripts/lib/CIME/case/case.py
--- a/scripts/lib/CIME/case/case.py
+++ b/scripts/lib/CIME/case/case.py
@@ -84,6 +84,9 @@ def __init__(self, case_root=None, read_only=True):
 
         if case_root is None:
             case_root = os.getcwd()
+
+        expect(not os.path.isdir(case_root) or os.path.isfile(os.path.join(case_root,"env_case.xml")), "Directory {} does not appear to be a valid case directory".format(case_root))
+
         self._caseroot = case_root
         logger.debug("Initializing Case.")
         self._read_only_mode = True


Test patch: