Repository: pydicom/pydicom

Issue:
Error decoding dataset with ambiguous VR element when the value is None
Hi all,
    I used the storescu in pynetdicom 1.5.3 to send the dicom ct files(both on mac and ubuntu): 
**python storescu.py 192.168.1.120 9002 ~/Downloads/test/**
(I also tried https://pydicom.github.io/pynetdicom/stable/examples/storage.html#storage-scu)
but it throwed errors: 

_E: Failed to encode the supplied Dataset
E: Store failed: /Users/me/Downloads/test/CT_S1_118.dcm
E: Failed to encode the supplied Dataset
Traceback (most recent call last):
  File "storescu.py", line 283, in main
    status = assoc.send_c_store(ds, ii)
  File "/Users/me/.pyenv/versions/3.8.2/lib/python3.8/site-packages/pynetdicom/association.py", line 1736, in send_c_store
    raise ValueError('Failed to encode the supplied Dataset')
ValueError: Failed to encode the supplied Dataset_

But I used to send same files with storescu in dcm4che successfully.
File attached.

[test.zip](https://github.com/pydicom/pynetdicom/files/5258867/test.zip)



Discussion:
```python
>>> from pydicom import dcmread
>>> dcmread("CT_S1_001.dcm")
Traceback (most recent call last):
  File ".../pydicom/tag.py", line 30, in tag_in_exception
    yield
  File ".../pydicom/filewriter.py", line 555, in write_dataset
    write_data_element(fp, dataset.get_item(tag), dataset_encoding)
  File ".../pydicom/dataset.py", line 1060, in get_item
    return self[key]
  File ".../pydicom/dataset.py", line 878, in __getitem__
    self[tag] = correct_ambiguous_vr_element(
  File ".../pydicom/filewriter.py", line 160, in correct_ambiguous_vr_element
    _correct_ambiguous_vr_element(elem, ds, is_little_endian)
  File ".../pydicom/filewriter.py", line 86, in _correct_ambiguous_vr_element
    elem_value = elem.value if elem.VM == 1 else elem.value[0]
TypeError: 'NoneType' object is not subscriptable
```
Issue occurs because the dataset is Implicit VR and the *Smallest Image Pixel Value* is ambiguous but empty,

Patch:
diff --git a/pydicom/filewriter.py b/pydicom/filewriter.py
--- a/pydicom/filewriter.py
+++ b/pydicom/filewriter.py
@@ -87,6 +87,9 @@ def _correct_ambiguous_vr_element(elem, ds, is_little_endian):
             elem.VR = 'SS'
             byte_type = 'h'
 
+        if elem.VM == 0:
+            return elem
+
         # Need to handle type check for elements with VM > 1
         elem_value = elem.value if elem.VM == 1 else elem.value[0]
         if not isinstance(elem_value, int):
@@ -115,6 +118,9 @@ def _correct_ambiguous_vr_element(elem, ds, is_little_endian):
         # As per PS3.3 C.11.1.1.1
         if ds.LUTDescriptor[0] == 1:
             elem.VR = 'US'
+            if elem.VM == 0:
+                return elem
+
             elem_value = elem.value if elem.VM == 1 else elem.value[0]
             if not isinstance(elem_value, int):
                 elem.value = convert_numbers(elem.value, is_little_endian, 'H')


Test patch:
diff --git a/pydicom/tests/test_filewriter.py b/pydicom/tests/test_filewriter.py
--- a/pydicom/tests/test_filewriter.py
+++ b/pydicom/tests/test_filewriter.py
@@ -1054,7 +1054,7 @@ def test_not_ambiguous_raw_data_element(self):
         elem = RawDataElement(0x60003000, 'OB', 1, b'\x00', 0, True, True)
         out = correct_ambiguous_vr_element(elem, Dataset(), True)
         assert out == elem
-        assert type(out) == RawDataElement
+        assert isinstance(out, RawDataElement)
 
     def test_correct_ambiguous_data_element(self):
         """Test correct ambiguous US/SS element"""
@@ -1086,10 +1086,28 @@ def test_correct_ambiguous_raw_data_element(self):
         ds[0x00280120] = elem
         ds.PixelRepresentation = 0
         out = correct_ambiguous_vr_element(elem, ds, True)
-        assert type(out) == DataElement
+        assert isinstance(out, DataElement)
         assert out.VR == 'US'
         assert out.value == 0xfffe
 
+    def test_empty_value(self):
+        """Regression test for #1193: empty value raises exception."""
+        ds = Dataset()
+        elem = RawDataElement(0x00280106, 'US or SS', 0, None, 0, True, True)
+        ds[0x00280106] = elem
+        out = correct_ambiguous_vr_element(elem, ds, True)
+        assert isinstance(out, DataElement)
+        assert out.VR == 'US'
+
+        ds.LUTDescriptor = [1, 1, 1]
+        elem = RawDataElement(0x00283006, 'US or SS', 0, None, 0, True, True)
+        assert out.value is None
+        ds[0x00283006] = elem
+        out = correct_ambiguous_vr_element(elem, ds, True)
+        assert isinstance(out, DataElement)
+        assert out.VR == 'US'
+        assert out.value is None
+
 
 class TestWriteAmbiguousVR:
     """Attempt to write data elements with ambiguous VR."""