<jupyter_start><jupyter_text>O2 Budget for One Float, One Year<jupyter_code>np.set_printoptions(suppress=True)
def find_mld_avg(var):
    variable = np.flip(var, 0)
    variable[variable==0] = np.nan
    thickn[np.isnan(variable)] = np.nan
    avg_var = np.empty((1,np.size(variable, 1))).flatten()
    for i in range(len(imld)):
        avg_var[i] = np.nansum(variable[0:int(imld[i]),i]*thickn[0:int(imld[i]),i])/np.nansum(thickn[0:int(imld[i]),i])
    return avg_var
floats = [f_4180, f_4860, f_4767]
fl_names = ['5904180', '5904860', '5904767']
fig = plt.figure(figsize=(10,15))

for fl in range(len(floats)):
    
    time = floats[fl].JULD.values
    dates = pd.to_datetime(time).year    
    dayofyear = floats[fl].JULD.dt.dayofyear        

    mld, imld, thickn = get_mld_rho(floats[fl], False)

    #temporary (just for one yr)
    mld = mld[dates==2017]
    imld = imld[dates==2017]
    thickn = thickn[:,dates==2017]

    #set up o2, o2sat, no3
    o2 = set_oxygen_var(floats[fl])
    o2 = o2[:,dates==2017]
    oxygenSat = set_oxygen_sat_var(floats[fl])
    oxygenSat = oxygenSat[:,dates==2017]

    #calculate O2sol
    o2sol = (o2/oxygenSat*100)

    #calculate AOU 
    aou = o2sol - o2

    #calculate mld averages for O2, NO3, AOU
    no3 = set_nitrate_var(floats[fl])
    no3 = no3[:,dates==2017]
    variables = [o2, no3, aou]
    o2_mld = no3_mld = aou_mld = np.empty((1,np.size(o2, 1))).flatten()


    o2_mld = find_mld_avg(o2)
    no3_mld = find_mld_avg(no3)
    aou_mld = find_mld_avg(aou)


    #calculate ∆AOU
    aou_am = np.nanmean(aou_mld, axis=0)
    d_aou = aou_mld-aou_am

    #calculate ∆O2
    o2_am = np.nanmean(o2_mld, axis=0)
    d_o2 = o2_mld-o2_am

    #calculate ∆NO3
    no3_am = np.nanmean(no3_mld, axis=0)
    d_no3 = no3_mld-no3_am

    #calculate ∆O2sol
    d_o2sol = d_o2 + d_aou

    #calculate ∆O2mix + ∆O2bio
    r = -170/16
    d_o2mixbio = d_o2 + r*d_no3

    #calculate ∆O2gasex
    d_o2gasex = np.negative(d_aou) - r*d_no3

    #create plot
    o2_vars = [d_o2, d_o2sol, d_o2mixbio, d_o2gasex]
    varNames = ['∆O2', '∆O2sol', '∆O2mix + ∆O2bio', '∆O2gasex']

    ax = fig.add_subplot(3,1, fl +1)

    for var in range(len(o2_vars)): 
        ax.plot(dayofyear.values[dates==2017], o2_vars[var], linewidth=2, label=varNames[var])
    #ax.plot(dayofyear.values[dates==2017], mld, color = 'black')

    date_form = DateFormatter("%B") # %m for month number
    ax.xaxis.set_major_formatter(date_form)
    ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))
    leg = ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
    leg_lines = leg.get_lines()
    leg_texts = leg.get_texts()
    ax.set_xlabel('Month')
    plt.xticks(rotation = 45)
    plt.setp(leg_lines, linewidth=4)
    plt.title('Oxygen Budget - Float ' + fl_names[fl] + ' in 2017', fontweight='bold', fontsize='15')
    #plt.ylim([-90, 90])

    fig.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15495371957490572, 'fraction_numerical': 0.05862187178608159, 'mean_word_length': 3.524031007751938, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '47704', 'license': 'MIT', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1298, 'n_words': 212, 'path': 'notebooks/o2_budget.ipynb', 'repo_name': 'gmacgilchrist/hmei', 'size': '2917'}