def run_clark2009(catchment, output_dem, hydro_year_to_take, met_inp_folder,
                  catchment_shp_folder):
    """
    wrapper to call the clark 2009 snow model for given area
    :param catchment: string giving catchment area to run model on
    :param output_dem: string identifying the grid to run model on
    :param hydro_year_to_take: integer specifying the hydrological year to run model over. 2001 = 1/4/2000 to 31/3/2001
    :return: st_swe, st_melt, st_acc, out_dt, mask. daily grids of SWE at day's end, total melt and accumulation over the previous day, and datetimes of ouput
    """
    print('loading met data')
    data_id = '{}_{}'.format(catchment, output_dem)
    inp_met = nc.Dataset(
        met_inp_folder +
        '/met_inp_{}_hy{}.nc'.format(data_id, hydro_year_to_take), 'r')
    inp_dt = nc.num2date(inp_met.variables['time'][:],
                         inp_met.variables['time'].units)
    inp_doy = convert_date_hydro_DOY(inp_dt)
    inp_hourdec = [datt.hour for datt in inp_dt]
    inp_ta = inp_met.variables['temperature'][:]
    inp_precip = inp_met.variables['precipitation'][:]
    print('met data loaded')

    mask = create_mask_from_shpfile(
        inp_met.variables['lat'][:], inp_met.variables['lon'][:],
        catchment_shp_folder + '/{}.shp'.format(catchment))
    # TODO: think about masking input data to speed up

    print('starting snow model')
    # start with no snow.
    # call main function once hourly/sub-hourly temp and precip data available.
    st_swe, st_melt, st_acc = snow_main_simple(inp_ta,
                                               inp_precip,
                                               inp_doy,
                                               inp_hourdec,
                                               dtstep=3600)
    out_dt = np.asarray(
        make_regular_timeseries(inp_dt[0], inp_dt[-1] + dt.timedelta(days=1),
                                86400))

    print('snow model finished')

    # mask out values outside of catchment
    st_swe[:, mask == False] = np.nan
    st_melt[:, mask == False] = np.nan
    st_acc[:, mask == False] = np.nan

    return st_swe, st_melt, st_acc, out_dt, mask
def create_average(Stname, var='SWE'):
    data = dict_sin_snow[Stname][var][:]

    # remove nans
    inp_datobs = data[~np.isnan(data)]
    inp_dtobs = dict_sin_snow['dt_UTC+12'][~np.isnan(data)]

    if var == 'SWE':
        inp_datobs = inp_datobs * 1000
    # remove periods of poor data

    if Stname == 'Castle Mount' and var == 'Snow Depth':  # noisy snow depth data
        ind = inp_dtobs < dt.datetime(2018, 12, 1)

    elif Stname == 'Mueller' and var == 'SWE':  # clearly wrong as snow depth >> 1 m over this period
        ind = ~np.logical_and(inp_dtobs > dt.datetime(2012, 10, 20),
                              inp_dtobs < dt.datetime(2012, 12, 4))

    elif Stname == 'Philistine' and var == 'SWE':  # clearly wrong as snow depth < 1 m during this period
        ind = ~np.logical_and(inp_dtobs > dt.datetime(2014, 6, 1),
                              inp_dtobs < dt.datetime(2014, 10, 1))

    else:
        ind = np.ones(inp_datobs.shape, dtype=np.bool)
    dohy = convert_date_hydro_DOY(inp_dtobs)
    # jd = np.asarray(convert_datetime_julian_day(inp_dtobs))
    dat_jd = np.full([365], np.nan)
    for i in range(365):
        if i < 5:
            dat_jd[i] = np.median(inp_datobs[np.logical_or(
                dohy > i - 5 + 365, dohy <= i + 5)])
        elif i > 360:
            dat_jd[i] = np.median(inp_datobs[np.logical_or(
                dohy > i - 5, dohy <= i + 5 - 365)])
        else:
            dat_jd[i] = np.median(inp_datobs[np.logical_and(
                dohy > i - 5, dohy <= i + 5)])
    dt_to_plot = [
        dt.datetime(2000, 4, 1, 0, 0) + dt.timedelta(days=j)
        for j in range(365)
    ]

    return dt_to_plot, dat_jd
    # ann_ts_av_melt2 = []
    # ann_ts_av_acc2 = []
    ann_hydro_days2 = []
    ann_dt2 = []
    ann_scd2 = []
    configs = []

    for hydro_year_to_take in hydro_years_to_take:

        print('loading modis data HY {}'.format(hydro_year_to_take))

        # load modis data for evaluation
        modis_fsca, modis_dt, modis_mask = load_subset_modis(
            catchment, output_dem, hydro_year_to_take, modis_folder,
            dem_folder, modis_dem, mask_folder, catchment_shp_folder)
        modis_hydro_days = convert_date_hydro_DOY(modis_dt)
        modis_sc = modis_fsca >= modis_sc_threshold

        # print('calculating basin average sca')
        # modis
        num_modis_gridpoints = np.sum(modis_mask)
        ba_modis_sca = []
        ba_modis_sca_thres = []
        for i in range(modis_fsca.shape[0]):
            ba_modis_sca.append(np.nanmean(modis_fsca[i, modis_mask]) / 100.0)
            ba_modis_sca_thres.append(
                np.nansum(modis_sc[i, modis_mask]).astype('d') /
                num_modis_gridpoints)

        # print('adding to annual series')
        ann_ts_av_sca_m.append(np.asarray(ba_modis_sca))