def _get_comparison_data_swe(    start_date = datetime(1980,1,1),
                                 end_date = datetime(1997, 12, 31),
    crcm4_data_folder = "/home/huziy/skynet1_rech3/crcm4_data/aex_p1sno",
    swe_obs_data_path = "data/swe_ross_brown/swe.nc",
    swe_var_name = "swe", season_to_months = None, crcm4_id = "aex"):

    cache_file = "seasonal_bias_{0}.bin".format(swe_var_name)

    if not os.path.isfile(cache_file):
        #get crcm4 data
        crcm4_data_store = {}
        for season, month_list in season_to_months.iteritems():
            data = ccc_analysis.get_seasonal_mean(crcm4_data_folder, start_date=start_date,
                end_date=end_date, months=month_list
            )

            crcm4_data_store[season] = data

        #get cru data
        cru_data_store = {}
        cru = CruReader(path=swe_obs_data_path, var_name=swe_var_name, transpose_xy_dimensions=False)
        lons2d_amno, lats2d_amno = polar_stereographic.lons, polar_stereographic.lats
        for season, month_list in season_to_months.iteritems():
            data = cru.get_seasonal_mean_field(months=month_list, start_date=start_date, end_date=end_date)

            nneighbors = 1 if swe_var_name in ["pre", "swe"] else 4
            data = cru.interpolate_data_to(data, lons2d_amno, lats2d_amno, nneighbors=nneighbors)
            cru_data_store[season] = data
        data_list = [cru_data_store, crcm4_data_store]
        pickle.dump(data_list, open(cache_file, "w"))
    else:
        data_list = pickle.load(open(cache_file))

    cru_data_store, crcm4_data_store = data_list
    return cru_data_store, crcm4_data_store
def _get_comparison_data(    start_date = datetime(1970,1,1),
    end_date = datetime(1999, 12, 31),
    crcm4_data_folder = "data/ccc_data/aex/aex_p1st",
    cru_data_path = "data/cru_data/CRUTS3.1/cru_ts_3_10.1901.2009.tmp.dat.nc",
    cru_var_name = "tmp", season_to_months = None, crcm4_id = "aex"):



    cache_file = "seasonal_bias_{0}.bin".format(cru_var_name)

    if not os.path.isfile(cache_file):
        #get crcm4 data
        crcm4_data_store = {}
        day_seconds = 24.0 * 60.0 * 60.0
        dt = members.id_to_step[crcm4_id]
        crcm4_coef = day_seconds
        for season, month_list in season_to_months.iteritems():
            data = ccc_analysis.get_seasonal_mean(crcm4_data_folder, start_date=start_date,
                end_date=end_date, months=month_list
            )

            if cru_var_name == "pre":
                data *= crcm4_coef

            crcm4_data_store[season] = data

        #get cru data
        cru_data_store = {}
        cru = CruReader(path=cru_data_path, var_name=cru_var_name)
        lons2d_amno, lats2d_amno = polar_stereographic.lons, polar_stereographic.lats
        dt = cru.get_time_step()
        cru_coef = day_seconds / (dt.seconds + dt.days * day_seconds)
        for season, month_list in season_to_months.iteritems():
            data = cru.get_seasonal_mean_field(months=month_list, start_date=start_date, end_date=end_date)

            nneighbors = 1 if cru_var_name == "pre" else 4

            data = cru.interpolate_data_to(data, lons2d_amno, lats2d_amno, nneighbors=nneighbors)

            if cru_var_name == "pre":
                data *= cru_coef

            cru_data_store[season] = data

        data_list = [cru_data_store, crcm4_data_store]
        pickle.dump(data_list, open(cache_file, "w"))
    else:
        data_list = pickle.load(open(cache_file))

    cru_data_store, crcm4_data_store = data_list
    return cru_data_store, crcm4_data_store
Ejemplo n.º 3
0
def main():

    start_date = datetime(1970, 1, 1)
    end_date = datetime(1999, 12, 31)

    #compare 2m temperatures
    cache_file = "compare_basinwise.bin"
    if not os.path.isfile(cache_file):
        cru = CruReader(path="data/cru_data/CRUTS3.1/cru_ts_3_10.1901.2009.tmp.dat.nc",
            var_name="tmp", create_tree_for_interpolation=False)
        cru_t2m = cru.get_monthly_normals(start_date = start_date, end_date = end_date)
        data_store = {"cru_t2m": cru_t2m, "cru_lons": cru.lons_2d, "cru_lats": cru.lats_2d}

        crcm4_t2m = ccc_analysis.get_monthly_normals("data/ccc_data/aex/aex_p1st",
            start_date = start_date, end_date = end_date
        )
        data_store["crcm4_t2m"] = crcm4_t2m


        #create basin masks
        #read basins polygons
        basins_map = read_shape_file.get_basins_as_shapely_polygons()
        data_store["basin_polygons_map"] = basins_map

        print( "created basin polygons from the shape file." )
        #obs mask
        obs_mask = {}
        cru_lons1d = cru.lons_2d.flatten()
        cru_lats1d = cru.lats_2d.flatten()
        p = Point()
        for basin_name, basin_poly in basins_map.iteritems():
            assert isinstance(basin_poly, Polygon)
            the_mask = map( lambda x: int( basin_poly.intersects(get_point(x[0], x[1], p))), zip(cru_lons1d, cru_lats1d))
            the_mask = np.reshape( np.array(the_mask), cru.lons_2d.shape )
            obs_mask[basin_name] = the_mask
            print( "created mask for {0}".format( basin_name ))
        data_store["obs_mask"] = obs_mask

        #model mask
        data_store["crcm4_mask"] = get_basin_masks_from_file()

        pickle.dump(data_store, open(cache_file, "wb"))
    else:
        data_store = pickle.load(open(cache_file))

    #plot data
    compare_basin_wise(data_store["cru_t2m"], data_store["obs_mask"],
        data_store["crcm4_t2m"], data_store["crcm4_mask"], data_store["basin_polygons_map"]
    )

    pass
Ejemplo n.º 4
0
def compare_precip(
    path_to_ccc="data/ccc_data/aex/aex_p1pcp",
    mask=None,
    label="",
    subplot_count=-1,
    start_date=None,
    end_date=None,
    subplot_total=10,
):
    """
    compare total precip from gpcc and from CRCM4
    """

    global figure, cru_reader

    import compare_swe

    ccc_data = compare_swe.getMonthlyNormalsAveragedOverMask(
        path_to_ccc=path_to_ccc, mask=mask, startDate=start_date, endDate=end_date
    )

    lons = polar_stereographic.lons[mask == 1]
    lats = polar_stereographic.lats[mask == 1]

    if cru_reader is None:
        cru_reader = CruReader()

    cru_data = cru_reader.get_monthly_normals_integrated_over(lons, lats, start_date=start_date, end_date=end_date)
    #

    if subplot_count == 1:
        plot_utils.apply_plot_params(font_size=25, width_pt=1200, aspect_ratio=2)
        figure = plt.figure()
        figure.subplots_adjust(hspace=0.9, wspace=0.4, top=0.9)

    if figure is None:
        print "figure object was not created"
        return

    ax = figure.add_subplot(5, 2, subplot_count)
    ax.set_title("Upstream of {0}".format(label))

    # tmp: data stub
    #    ccc_data = np.ones((12,)) * 5000
    #    cru_data = np.ones((12,)) * 7000

    line1 = ax.plot(ccc_data, label="CRCM4", lw=3, color="b")
    line2 = ax.plot(np.array(cru_data), label="CRU", lw=3, color="r")
    months = ("", "Feb", "", "Apr", "", "Jun", "", "Aug", "", "Oct", "", "Dec")
    ax.set_xticks(xrange(12))
    ax.set_xticklabels(months)

    ax.yaxis.set_major_formatter(ticker.FuncFormatter(format_yaxis))

    if subplot_count == subplot_total:
        lines = (line1, line2)
        labels = ("CRCM4", "CRU")
        figure.legend(lines, labels, "upper center")
        #        figure.text(0.05, 0.5, 'TOTAL PRECIPITATION (${\\rm mm / month}$)',
        #                      rotation=90,
        #                      ha = 'center', va = 'center'
        #                      )
        figure.savefig("tp.pdf")
        cru_reader.close_data_connection()  # close the file