Beispiel #1
0
def compare_spatial_means_over_mask(mask = None, label = ''):
    """
    timeseries not changed, averaging over the points where mask == 1
    """
    start = datetime(1980,01,01,00)
    end = datetime(1996, 12, 31,00)

    lons = polar_stereographic.lons
    lats = polar_stereographic.lats

    lons_selected = lons[ mask == 1 ]
    lats_selected = lats[ mask == 1 ]
    points = [GeoPoint(longitude = lon, latitude = lat) for lon, lat in zip(lons_selected, lats_selected)]

    sweObs = SweHolder()
    obsData = sweObs.getSpatialMeanDataFromNetCDFforPoints(points, startDate = start, endDate = end)
    modelData = getSpatialMeanCCCDataForMask(mask, startDate = start, endDate = end)

    plt.figure()
    plt.title('SWE' + ' ' + label)
    plt.plot(obsData[0], obsData[1], label = 'Obs.', color = 'red', lw = 3)
    plt.plot(modelData[0], modelData[1], label = 'Model', color = 'blue', lw = 3)
    plt.ylabel('mm')
    plt.legend()
    plt.savefig('swe_compare_{0}.pdf'.format(label), bbox_inches = 'tight')
Beispiel #2
0
def compare_swe_2d():
    """
    Compare seasonal mean
    """
    start = datetime(1980,01,01,00)
    end = datetime(1996, 12, 31,00)
    months = [12,1,2]
    #calculate mean for ccc data accounting for the mask
    domain_mask = get_domain_mask()
    cccData = getTemporalMeanCCCDataForMask(domain_mask,
                                            startDate = start,
                                            endDate = end, months = months)
    lon_selected = polar_stereographic.lons[domain_mask == 1]
    lat_selected = polar_stereographic.lats[domain_mask == 1]

    geopointList = []
    for lon, lat in zip(lon_selected, lat_selected):
        geopointList.append(GeoPoint(longitude = lon, latitude = lat))

    print 'Selecting obs data'
    sweObs = SweHolder()
    obsData = sweObs.getTemporalMeanDataFromNetCDFforPoints(geopointList, startDate = start,
                                                            endDate = end, months = months)

    

    to_plot = np.ma.masked_all(polar_stereographic.lons.shape)
    condition = domain_mask == 1
    to_plot[condition] = (cccData - obsData) / obsData * 100
    xs = polar_stereographic.xs
    ys = polar_stereographic.ys
    basemap = polar_stereographic.basemap

    plot_utils.apply_plot_params()
    basemap.pcolormesh(xs, ys, to_plot, cmap = mpl.cm.get_cmap('jet', 9), vmin = -60, vmax = 120)
    basemap.drawcoastlines()
    plt.colorbar(ticks = LinearLocator(numticks = 10), format = '%.1f')
    plt.title('Snow Water Equivalent (%) \n $(S_{\\rm CRCM4} - S_{\\rm obs.})/S_{\\rm obs.}\\cdot100\%$\n')

    #zoom to domain
    selected_x = xs[~to_plot.mask]
    selected_y = ys[~to_plot.mask]
    marginx = abs(np.min(selected_x) * 5.0e-2)
    marginy = abs(np.min(selected_y) * 5.0e-2)

    plt.xlim(np.min(selected_x) - marginx, np.max(selected_x) + marginx)
    plt.ylim(np.min(selected_y) - marginy, np.max(selected_y) + marginy)

    bb.plot_basin_boundaries_from_shape(basemap, plotter = plt, linewidth = 2, edge_color = 'k')



    ##overlay flow directions
    cells  = cpe.get_connected_cells('data/hydrosheds/directions_for_streamflow9.nc')

    read_infocell.plot_directions(cells, basemap = basemap, domain_mask = get_domain_mask())


    selected_stations = [
            104001, 103715, 93806, 93801,
            92715, 81006, 61502, 80718,
            40830, 42607
    ]
    selected_ids = map(lambda x: "%06d" % x, selected_stations)
    print selected_ids

    cpe.plot_station_positions(id_list=selected_ids, use_warpimage=False,
                               save_to_file=False, the_basemap=basemap)

    plt.savefig('swe_djf_validation.pdf', bbox_inches = 'tight')


    pass
Beispiel #3
0
def compare_daily_normals_integral_over_mask(mask = None, start = None, end = None, label = '',
                                             subplot_count = None, subplot_total = 10
                                             ):
    """

    """
    lons = polar_stereographic.lons
    lats = polar_stereographic.lats

    lons_selected = lons[ mask == 1 ]
    lats_selected = lats[ mask == 1 ]

    global swe_fig

    if subplot_count == 1:
        swe_fig = plt.figure()
        plot_utils.apply_plot_params(font_size=25, width_pt=900, aspect_ratio=2.5)
        swe_fig.subplots_adjust(hspace = 0.6, wspace = 0.2, top = 0.9)



    points = [GeoPoint(longitude = lon, latitude = lat) for lon, lat in zip(lons_selected, lats_selected)]

    sweObs = SweHolder()
    obsData = sweObs.getSpatialIntegralFromNetcdfForPoints(points, startDate = start, endDate = end)
    print 'finished reading observations'
    modelData = getSpatialIntegralCCCDataForMask(mask = mask, path_to_ccc = 'data/ccc_data/aex/aex_p1sno',
                                                 startDate = start, endDate = end)
    print 'finished reading model data'
    print 'finished reading input mean timeseries'

    stamp_year = 2000
    obsStamp = map(lambda x: toStampYear(x, stamp_year = stamp_year), obsData[0])
    modelStamp = map(lambda x: toStampYear(x, stamp_year = stamp_year), modelData[0])
    print 'calculated stamp dates'

    ##calculate mean for a day of year
    obsDict = {}
    for stampDate, value in zip(obsStamp, obsData[1]):
        if not obsDict.has_key(stampDate):
            obsDict[stampDate] = []
        obsDict[stampDate].append(value)

    for key, theList in obsDict.iteritems():
        obsDict[key] = np.mean(theList)

    obsDates = sorted(obsDict)
    obsMeanValues = [obsDict[d] for d in obsDates]



    #do the same thing as for obs for the model data
    modelDict = {}
    for stampDate, value in zip(modelStamp, modelData[1]):
        if not modelDict.has_key(stampDate):
            modelDict[stampDate] = []
        modelDict[stampDate].append(value)


    for key, theList in modelDict.iteritems():
        modelDict[key] = np.mean(theList)

    modelDates = sorted(modelDict)
    modelMeanValues = [modelDict[d] for d in modelDates]

    print 'Calculated mean for day of year and over selected points'


    plt.title('Upstream of {0}'.format(label))
    line1 = plt.plot(modelDates, modelMeanValues, color = 'blue', lw = 3)
    line2 = plt.plot(obsDates, obsMeanValues, color = 'red', lw = 3)

    #plt.ylabel('mm')

    ax = plt.gca()
    ax.xaxis.set_major_formatter(mpl.dates.DateFormatter('%b'))
    ax.xaxis.set_major_locator(
        mpl.dates.MonthLocator(bymonth = range(2,13,2))
    )


    if subplot_count == subplot_total:
         lines = (line1, line2)
         labels = ("CRCM4", "CRU")
         swe_fig.legend(lines, labels, 'upper center')
         swe_fig.text(0.05, 0.5, 'SWE (mm)',
                       rotation=90,
                       ha = 'center', va = 'center'
                        )
         swe_fig.savefig("swe.pdf")
Beispiel #4
0
def compare_daily_normals_mean_over_mask(mask = None, start = None, end = None, label = ''):
    lons = polar_stereographic.lons
    lats = polar_stereographic.lats

    lons_selected = lons[ mask == 1 ]
    lats_selected = lats[ mask == 1 ]

    points = [GeoPoint(longitude = lon, latitude = lat) for lon, lat in zip(lons_selected, lats_selected)]

    sweObs = SweHolder()
    obsData = sweObs.getSpatialMeanDataFromNetCDFforPoints(points, startDate = start, endDate = end)
    print 'finished reading observations'
    modelData = getSpatialMeanCCCDataForMask(mask, startDate = start, endDate = end)
    print 'finished reading model data'
    print 'finished reading input mean timeseries'

    stamp_year = 2000
    obsStamp = map(lambda x: toStampYear(x, stamp_year = stamp_year), obsData[0])
    modelStamp = map(lambda x: toStampYear(x, stamp_year = stamp_year), modelData[0])
    print 'calculated stamp dates'

    ##calculate mean for a day of year
    obsDict = {}
    for stampDate, value in zip(obsStamp, obsData[1]):
        if not obsDict.has_key(stampDate):
            obsDict[stampDate] = []
        obsDict[stampDate].append(value)

    for key, theList in obsDict.iteritems():
        obsDict[key] = np.mean(theList)

    obsDates = sorted(obsDict)
    obsMeanValues = [obsDict[d] for d in obsDates]



    #do the same thing as for obs for the model data
    modelDict = {}
    for stampDate, value in zip(modelStamp, modelData[1]):
        if not modelDict.has_key(stampDate):
            modelDict[stampDate] = []
        modelDict[stampDate].append(value)


    for key, theList in modelDict.iteritems():
        modelDict[key] = np.mean(theList)

    modelDates = sorted(modelDict)
    modelMeanValues = [modelDict[d] for d in modelDates]

    print 'Calculated mean for day of year and over a basin points'


    plt.figure(figsize = (8, 6), dpi = 80)
    plt.title('SWE {0}'.format(label))
    plt.plot(obsDates, obsMeanValues, label = 'Obs.', color = 'red', lw = 3)
    plt.plot(modelDates, modelMeanValues, label = 'Model', color = 'blue', lw = 3)
    plt.ylabel('mm')

    ax = plt.gca()
    ax.xaxis.set_major_formatter(mpl.dates.DateFormatter('%b'))
    ax.xaxis.set_major_locator(
        mpl.dates.MonthLocator(bymonth = range(2,13,2))
    )
    plt.legend()
    plt.savefig(label + '_swe.pdf', bbox_inches = 'tight')