Beispiel #1
0
def get_monthly_normals(data_folder_path, start_date = None, end_date = None):

    """
    returns numpy array of the monthly normals of the shape (12, nx, ny)
    """
    result = [0.0 for i in xrange(12)]
    record_counts = [0.0 for i in xrange(12)]
    for file_name in os.listdir(data_folder_path):
        date_month = ccc_util.get_month_date_from_name(file_name)

        #focus only on the interval of interest
        if start_date is not None:
            if date_month < start_date:
                continue

        if end_date is not None:
            if date_month > end_date:
                continue


        data = champ_ccc(os.path.join(data_folder_path, file_name)).charge_champs()
        m = date_month.month - 1
        for record in data:
            field = record["field"]
            result[m] += field
            record_counts[m] += 1.0

    result = np.array(result)
    record_counts = [ count * np.ones(result.shape[1:]) for count in record_counts ]
    record_counts = np.array(record_counts)
    result /= np.array(record_counts)
    return result


    pass
Beispiel #2
0
def get_seasonal_mean(data_folder_path, start_date = None, end_date = None, months = None):
    """
    returns seasonal mean
    """
    result = 0.0
    record_count = 0.0
    for file_name in os.listdir(data_folder_path):
        date_month = ccc_util.get_month_date_from_name(file_name)


        #take only months of interest
        if date_month.month not in months:
            continue

        #focus only on the interval of interest
        if start_date is not None:
            if date_month < start_date:
                continue

        if end_date is not None:
            if date_month > end_date:
                continue


        data = champ_ccc(os.path.join(data_folder_path, file_name)).charge_champs()
        for record in data:
            field = record["field"]
            result += field
            record_count += 1.0

    return result / record_count
Beispiel #3
0
def get_seasonal_means_for_year_range(data_folder, year_range, months = None):
    """
    Using the fact that the data for each month are in a separate file

    return data (n_years, nx, ny) - numpy array
    """
    path_map = ccc_util.get_yearmonth_to_path_map(data_folder)
    result = []
    for the_year in year_range:
        print("year = {0}".format(the_year))
        data = []
        for the_month in months:
            key = (the_year, the_month)
            ccc_obj = champ_ccc(path_map[key])
            records = ccc_obj.charge_champs()
            data.extend(map(lambda x: x["field"],records))
        result.append(np.mean(data, axis=0))
    return np.array(result)
Beispiel #4
0
def main():
    data_path = "data/permafrost/p1perma"
    cccObj = champ_ccc(data_path)
    pData = cccObj.charge_champs()[0]["field"]


    plot_utils.apply_plot_params()
    basemap = polar_stereographic.basemap


    x, y = basemap(polar_stereographic.lons, polar_stereographic.lats)
    pData =   pData[10:190,10:182]

    pData = np.ma.masked_where(pData < 0, pData)


    x1d = x[~pData.mask]
    y1d = y[~pData.mask]
    xmin = np.min(x1d)
    xmax = np.max(x1d)
    ymin, ymax = np.min(y1d), np.max(y1d)


    #basemap.contourf(x,y, pData)

#    basemap.drawcoastlines()
#    basemap.drawcountries()
#    basemap.drawstates()
#    plt.colorbar()


    path_to_shapefile = "data/shape/canada_provinces_shape/PROVINCE"
    basemap.readshapefile(path_to_shapefile, name = "provinces")
    gdal.UseExceptions()
    driver = ogr.GetDriverByName("ESRI Shapefile")
    dataStore = driver.Open(path_to_shapefile + ".SHP", 0)

    layer = dataStore.GetLayer(0)

    print dataStore.GetLayerCount()



    geometries = []
    for feature in layer:
        name = feature.GetFieldAsString('NAME').lower()

        geom = feature.GetGeometryRef()
        geometries.append(geom.ExportToWkt())
        centroid = geom.Centroid()

        lon, lat = centroid.GetX(), centroid.GetY()

        abbr = name_to_abbr[name]

        if abbr == "YT":
            lon -= 1
        if abbr == "PE":
            lon -= 1.5

        if abbr == "NB":
            lat -= 0.5
            lon -= 1.5

        if abbr == "NS":
            lat -= 0.5
            lon += 1



        if abbr == "NU":
            lat -= 8
            lon -= 10

        if abbr == "NT":
            lat -= 3

        if abbr == "NL":
            lon, lat = basemap(9.18639e6, 3.36468e6, True)

        x1, y1 = basemap(lon, lat)
        plt.annotate(name_to_abbr[name], xy = (x1,y1))
        print name


    geometries = map(ogr.CreateGeometryFromWkt, geometries)
    lons = polar_stereographic.lons
    lats = polar_stereographic.lats
    nx, ny = lons.shape
    for i in xrange(nx):
        for j in xrange(ny):
            if pData.mask[i, j]:
                continue
            else:
                pData.mask[i, j] = not inside_region(lons[i,j], lats[i,j], geometries)


    basemap.pcolormesh(x,y, pData, cmap = mpl.cm.get_cmap(name = "gist_yarg",  lut = 5),
                       alpha = 0.8, linewidth = 0, shading='flat', vmin = 0, vmax = 5)

#    basemap.contourf(x, y, pData, levels = [0.0, 1.0,2.0,3.0,4.0, 5.0],
#                     cmap = mpl.cm.get_cmap(name = "gist_yarg",  lut = 5), alpha = 0.6)
    plt.colorbar(ticks = LinearLocator(numticks = 6), format = "%.1f")
    plot_stations(basemap)
    plt.xlim(xmin, xmax)
    plt.ylim(ymin, ymax)
    plt.show()

    pass