Exemplo n.º 1
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