#%matplotlib inline

from seabird.cnv import fCNV
from seabird.qc import fProfileQC

"""
Explanation: Quality Control CTD data with PySeabird
Author: Guilherme Castelão
This is a minimalist example on how to use the Python Seabird package to read and apply a quality control in a CTD output file. For more details, please check the documentation.
Requirements
This notebook requires the packages seabird, supportdata, and cotede. You can install those using pip as following:
shell
pip install seabird[QC]
End of explanation
"""


!wget https://raw.githubusercontent.com/castelao/seabird/master/sampledata/CTD/dPIRX003.cnv

profile = fCNV('dPIRX003.cnv')

print("Header: %s" % profile.attributes.keys())

print("Data: %s" % profile.keys())

"""
Explanation: Let's first download an example file with some CTD data
End of explanation
"""


profile = fProfileQC('dPIRX003.cnv', cfg='gtspp')

"""
Explanation: Let's apply the quality control procedure recommended by GTSPP
End of explanation
"""


profile.flags.keys()

"""
Explanation: The QC flags are groupped for each variable. On this example there are temperature, salinity and the respective secondary sensors.
End of explanation
"""


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

"""
Explanation: Let's check which tests were performed, hence which flags are available, on the the primary temperature sensor
End of explanation
"""


profile.flags['TEMP']['spike']

idx = profile.flags['TEMP']['overall'] <= 2

from matplotlib import pyplot as plt

plt.figure(figsize=(12,8))
plt.plot(profile['TEMP'][idx], profile['PRES'][idx],'b')
plt.plot(profile['TEMP'][~idx], profile['PRES'][~idx],'ro')
plt.gca().invert_yaxis()
# plt.plot(profile['TEMP2'], profile['PRES'],'g')
plt.xlabel('Temperature')
plt.ylabel('Pressure')
plt.title(profile.attributes['filename'])

"""
Explanation: The flagging standard is described in CoTeDe's manual . The one used here is 0 for no QC performed, 1 for approved data, and 9 for missing data.
Note that the overall flag is the combined result from all tested flags. In the example above it considers the other 7 flags and takes the highest value, therefore, if the overall is equal to 1 means that all possible tests approved that measurement, while a value of 4 means that at least one tests suggests its a bad measurement.
End of explanation
"""


profile = fProfileQC('dPIRX003.cnv', cfg='eurogoos')
profile.flags['TEMP'].keys()

"""
Explanation: Other pre defined quality control procedures are available, please check CoTeDe's manual to learn the details of the tests and what is available. For instance, to apply the EuroGOOS recommendations change the cfg argument
End of explanation
"""


profile = fProfileQC('dPIRX003.cnv')
profile.flags['TEMP'].keys()

"""
Explanation: If not defined, the default configuration is a collection of tests resulted for our work on IQuOD, and is equivalent to define cfg='cotede'.
End of explanation
"""


