Repository: yt-project/yt

Issue:
FLASH unit systems (Only old FLASH files)
<!--To help us understand and resolve your issue, please fill out the form to
the best of your ability.-->
<!--You can feel free to delete the sections that do not apply.-->

### Bug report

**Bug summary**

In old FLASH data files, the unit system "cgs" is not recognized.

<!--A short 1-2 sentences that succinctly describes the bug-->

**Code for reproduction**

<!--A minimum code snippet required to reproduce the bug, also minimizing the
number of dependencies required.-->

<!-- If you need to use a data file to trigger the issue you're having, consider
using one of the datasets from the yt data hub (http://yt-project.org/data). If
your issue cannot be triggered using a public dataset, you can use the yt
curldrop (https://docs.hub.yt/services.html#curldrop) to share data
files. Please include a link to the dataset in the issue if you use the
curldrop.-->

```python
import yt
yt.load("old_flash_file_hdf5_plt_cnt_0566")
```

**Actual outcome**

<!--The output produced by the above code, which may be a screenshot, console
output, etc.-->

```
RuntimeError                              Traceback (most recent call last)
<ipython-input-7-659f0932c34d> in <module>()
----> 1 ds = yt.load(fn)

/opt/anaconda3/lib/python3.5/site-packages/yt/convenience.py in load(*args, **kwargs)
     84     candidates = find_lowest_subclasses(candidates)
     85     if len(candidates) == 1:
---> 86         return candidates[0](*args, **kwargs)
     87     if len(candidates) == 0:
     88         if ytcfg.get("yt", "enzo_db") != '' \

/opt/anaconda3/lib/python3.5/site-packages/yt/frontends/flash/data_structures.py in __init__(self, filename, dataset_type, storage_filename, particle_filename, units_override, unit_system)
    229 
    230         Dataset.__init__(self, filename, dataset_type, units_override=units_override,
--> 231                          unit_system=unit_system)
    232         self.storage_filename = storage_filename
    233 

/opt/anaconda3/lib/python3.5/site-packages/yt/data_objects/static_output.py in __init__(self, filename, dataset_type, file_style, units_override, unit_system)
    265 
    266         self._parse_parameter_file()
--> 267         self.set_units()
    268         self._setup_coordinate_handler()
    269 

/opt/anaconda3/lib/python3.5/site-packages/yt/data_objects/static_output.py in set_units(self)
    868             self.unit_registry.modify('a', 1/(1+self.current_redshift))
    869 
--> 870         self.set_code_units()
    871 
    872         if getattr(self, "cosmological_simulation", False):

/opt/anaconda3/lib/python3.5/site-packages/yt/frontends/flash/data_structures.py in set_code_units(self)
    265 
    266     def set_code_units(self):
--> 267         super(FLASHDataset, self).set_code_units()
    268 
    269     def _find_parameter(self, ptype, pname, scalar = False):

/opt/anaconda3/lib/python3.5/site-packages/yt/data_objects/static_output.py in set_code_units(self)
    899 
    900         # set attributes like ds.length_unit
--> 901         self._set_code_unit_attributes()
    902 
    903         self.unit_registry.modify("code_length", self.length_unit)

/opt/anaconda3/lib/python3.5/site-packages/yt/frontends/flash/data_structures.py in _set_code_unit_attributes(self)
    246             else:
    247                 raise RuntimeError("Runtime parameter unitsystem with "
--> 248                                    "value %s is unrecognized" % self['unitsystem'])
    249         else:
    250             b_factor = 1.

RuntimeError: Runtime parameter unitsystem with value 'cgs' is unrecognized
```

**Expected outcome**

<!--A description of the expected outcome from the code snippet-->
<!--If this used to work in an earlier version of yt, please note the
version it used to work on-->

```
# If applicable, paste the console output here
#
#
```


**Version Information**
<!--Please specify your platform and versions of the relevant libraries you are
using:-->
  * Operating System: Mac OS 10.13.3 or Linux
  * Python Version: 3
  * yt version: 3.5.dev0
  * Other Libraries (if applicable): 

<!--Please tell us how you installed yt and python e.g., from source,
pip, conda. If you installed from conda, please specify which channel you used
if not the default-->

From the error message, it is saying that I am using 'cgs' units, but the comparison with 'cgs' failed

```
if self['unitsystem'].lower() == "cgs":
    b_factor = 1.0
elif ....

else:
    raise RuntimeError("Runtime parameter unitsystem with "
            "value %s is unrecognized" % self['unitsystem'])

```

I have no problem to open recent FLASH data but having problem with this old file.
I am sorry that I don't know how to share this old data file. This file is 13.29 GB.
My work around solution is just comment out the line of `raise RuntimeError` and set `b_factor` to 1 if failed. However, I don't understand why `self['unitsystem'].lower() == "cgs"` is `False` and I don't know if this affect other analysis.







Discussion:
Could you define "old"? FLASH2? 
What is the value of “unitsystem”? You should be able to print that out using a debugger or a print statement.
If I print `self['unitsystem'].lower()`, it is `cgs`. It is 4 years old from a custom version of FLASH (close to FLASH 4)
Are there trailing spaces or something? Maybe we just need a `.strip()` after `.lower()`.
no trailing spaces.  `print(self['unitsystem'].lower())` just give me `"cgs"`
Then I don’t understand how that if statement is evaluating as you’re seeing.

Any chance you can make the dataset available for download somewhere? Or have a smaller dataset with the same issue? Google drive and Dropbox are useful for this sort of thing.
Thanks. I will upload it to my dropbox disk (~13 GB)
https://www.dropbox.com/s/wpg8ioaadyyp6sx/m25_mri_o110b9_hdf5_plt_cnt_0566?dl=0  You can download the file here
Here's the issue:

```
(Pdb) self['unitsystem'].lower()
"'cgs'"
```

So for some reason FLASH has injected quotes into the value of the 'unitsystem' parameter. I don't think other versions of FLASH do that so it sounds like a bug in your fork.

Regardless, this shouldn't be enough to crash when loading data that has this issue. Thankfully the fix is straightforward, PR incoming.

Resolving patch:
diff --git a/yt/frontends/flash/data_structures.py b/yt/frontends/flash/data_structures.py
--- a/yt/frontends/flash/data_structures.py
+++ b/yt/frontends/flash/data_structures.py
@@ -232,11 +232,14 @@ def __init__(self, filename, dataset_type='flash_hdf5',
     def _set_code_unit_attributes(self):
 
         if 'unitsystem' in self.parameters:
-            if self['unitsystem'].lower() == "cgs":
+            # Some versions of FLASH inject quotes in the runtime parameters
+            # See issue #1721
+            us = self['unitsystem'].replace('\'', '').replace('"', '').lower()
+            if us == "cgs":
                 b_factor = 1.0
-            elif self['unitsystem'].lower() == "si":
+            elif us == "si":
                 b_factor = np.sqrt(4*np.pi/1e7)
-            elif self['unitsystem'].lower() == "none":
+            elif us == "none":
                 b_factor = np.sqrt(4*np.pi)
             else:
                 raise RuntimeError("Runtime parameter unitsystem with "


Test patch: