Reading MATLAB .mat files#

In one of the radiation lecture, you are asked to read files in the MATLAB .mat files (one file per day with vectors of I measured at the surface at 450 nm, along with the sun elevation angle for each measurement SNH). There seem to be no time attached to these measurements. Here is an example of how to read one of the file into a pandas dataframe with help of the scipy library:

Reading mat files#

from scipy.io import loadmat  # this is the SciPy module that loads mat-files
import pandas as pd
import matplotlib.pyplot as plt

mat = loadmat('I_and_SNH_20111003.mat')

# Put all columns which are not weird MATLAB ones into a dataframe
df = pd.DataFrame()
for k in mat:
    if '__' in k:
        continue
    df[k] = mat[k].flatten()

# Name it "day1" for clarity
df_day1 = df
df_day1
SNH I_450
0 10.028338 0.238662
1 10.838524 0.282054
2 11.646388 0.321627
3 12.446173 0.361128
4 13.246367 0.398293
... ... ...
109 13.503050 0.485676
110 12.684507 0.442991
111 11.890372 0.395319
112 11.071531 0.343366
113 10.293703 0.302224

114 rows × 2 columns

Plots#

df_day1.plot();
../_images/eeed69a5b5ce0f0f12f065da7d6d87ada30ca3cb2946f6e6c020586d25991cf7.png
df_day1.plot(x='SNH', y='I_450');
../_images/47d9462b87c4eefc4586952140599ff4234755b08408b93d6a704dbbbe614ec2.png
df_day1_morning = df_day1.loc[:df_day1['SNH'].argmax()]
df_day1_afternoon = df_day1.loc[df_day1['SNH'].argmax()+1:]

f, ax = plt.subplots()
df_day1_morning.plot(x='SNH', y='I_450', ax=ax, label='Morning');
df_day1_afternoon.plot(x='SNH', y='I_450', ax=ax, label='Afternoon');
../_images/f1b0026d09439a02bf5db7f0177f02b34719d3b1d7b44bffd4a5b309bff8d8b1.png

Merge several days into one dataframe#

import glob

all_df = []
for f in sorted(glob.glob('*.mat')):

    mat = loadmat(f)

    # Put all columns which are not weird MATLAB ones into a dataframe
    df = pd.DataFrame()
    for k in mat:
        if '__' in k:
            continue
        df[k] = mat[k].flatten()
    df['date'] = f.split('_')[-1].split('.')[0]

    all_df.append(df)

all_df = pd.concat(all_df, ignore_index=True)
all_df
SNH I_450 date
0 10.028338 0.238662 20111003
1 10.838524 0.282054 20111003
2 11.646388 0.321627 20111003
3 12.446173 0.361128 20111003
4 13.246367 0.398293 20111003
... ... ... ...
450 16.725457 0.630984 20120514
451 15.535872 0.577296 20120514
452 14.617152 0.532635 20120514
453 13.705881 0.487820 20120514
454 12.807493 0.449735 20120514

455 rows × 3 columns

all_df.plot(x='SNH', y='I_450', style='.');
../_images/78b162d7fcf0397b93ddbc13f9465284d39d758442898f62772660fcc5bcba52.png