# Different version of CoTeDe might give slightly different outputs.
# Please let me know if you see something that I should update.

import cotede
print("CoTeDe version: {}".format(cotede.__version__))

# Importing some requirements

from datetime import datetime

import numpy as np
from numpy import ma
from cotede.qc import ProfileQC

"""
Explanation: Quality Controlling a generic data object
Objective:
This notebook shows how to use CoTeDe with a generic dataset. If you want to use CoTeDe in your own dataset, or want to plug CoTeDe in your application, this notebook is for you.
How to use CoTeDe to quality control any type of measurement?
CoTeDe operates with a minimalist common data model to connect with other applications. To use it from another application all you need to do is to provide your data in that standard. For this example, let's call this dataset object as 'ds'.
CoTeDe expects common information for the dataset, like latitude of the profile, to be accessed as:
ds.attrs['latitude']
or
ds.attrs['datetime']
While the measurements and auxiliary variables accessed directly like::
ds['temperature']
or
ds['depth']
With that structure, each test implemented in CoTeDe knows where to search the relevant information. For instance, the test 'at sea' for a profile only needs to know latitude and longitude, which as described above is available at ds.attrs
Let's see a real example.
End of explanation
"""


class DummyDataset(object):
    """Minimalist data object that contains data and attributes
    """
    def __init__(self):
        """Two dictionaries to store the data and attributes
        """
        self.attrs = {}
        self.data = {}
            
    def __getitem__(self, key):
        """Return the requested item from the data
        """
        return self.data[key]
    
    def keys(self):
        """Show the available variables in data
        """
        return self.data.keys()
        
    @property
    def attributes(self):
        """Temporary requirement while Gui is refactoring CoTeDe. This will be soon unecessary
        """
        return self.attrs

"""
Explanation: Let's create a minimalist class that behaves as CoTeDe would like to. It is like a dictionary of relevant variables with a propriety attrs with some metatada.
End of explanation
"""


mydata = DummyDataset()

"""
Explanation: Let's create an empty data object.
End of explanation
"""


mydata.attrs['datetime'] = datetime(2016,6,4)
mydata.attrs['latitude'] = 15
mydata.attrs['longitude'] = -38

print(mydata.attrs)

"""
Explanation: Let's define some metadata as position and time that the profile was measured.
End of explanation
"""


mydata.data['PRES'] = ma.fix_invalid([2, 6, 10, 21, 44, 79, 100, 150, 200, 400, 410, 650, 1000, 2000, 5000])
mydata.data['TEMP'] = ma.fix_invalid([25.32, 25.34, 25.34, 25.31, 24.99, 23.46, 21.85, 17.95, 15.39, 11.08, 6.93, 7.93, 5.71, 3.58, np.nan])
mydata.data['PSAL'] = ma.fix_invalid([36.49, 36.51, 36.52, 36.53, 36.59, 36.76, 36.81, 36.39, 35.98, 35.30, 35.28, 34.93, 34.86, np.nan, np.nan])

"""
Explanation: Now let's create some data. Here I'll use create pressure, temperature, and salinity. I'm using masked array, but it could be a simple array.
Here I'm creating these values, but in a real world case we would be reading from a netCDF, an ASCII file, an SQL query, or whatever is your data source.
End of explanation
"""


mydata.keys()

"""
Explanation: Let's check the available variables
End of explanation
"""


mydata['TEMP']

"""
Explanation: Let's check one of the variables, temperature:
End of explanation
"""


pqced = ProfileQC(mydata, cfg='eurogoos')

"""
Explanation: Now that we have our data and metadata as this object, CoTeDe can do its job. On this example let's evaluate this fictious profile using the EuroGOOS recommended QC test. For that we can use ProfileQC() like:
End of explanation
"""


pqced.keys()

"""
Explanation: The returned object (pqced) has the same content of the original mydata. Let's check the variables again,
End of explanation
"""


pqced.flags.keys()

"""
Explanation: But now there is a new propriety named 'flags' which is a dictionary with all tests applied and the flag resulted. Those flags are groupped by variables.
End of explanation
"""


pqced.flags['TEMP'].keys()

"""
Explanation: Let's see which flags are available for temperature,
End of explanation
"""


pqced.flags['TEMP']['gradient_depthconditional']

"""
Explanation: Let's check the flags for the test gradient conditional to the depth, as defined by EuroGOOS
End of explanation
"""


pqced.flags['PSAL']['overall']

"""
Explanation: One means that that measurement was approved by this test. Nine means that the data was not available or not valid at that level. And zero means no QC. For the gradient test it is not possible to evaluate the first or the last values (check the manual), so those measurements exist but the flag was zero.
The overall flag is a special one that combines all other flags by taking the most critical assessment. If a single test identify a problem and flag as 4 (bad data), the overall flag for that measurement will be 4 even if that measurement passed in all other tests. Therefore, a measurement with flag 1 (good value) means that it was approved in all other tests.
End of explanation
"""


