def symmetrize_colormap(cm,vmin,vmax):
    #if min<0 and max>0, centers the colormap around zero
    # and if abs(min) != abs(max), truncates it to make values with the same absolute value
    # have colors with an equal distance from zero.
    if vmax*vmin<0:
        if vmax>abs(vmin):
            start=(vmax-abs(vmin))/(2.0*vmax) 
            stop=1.0
        else:
            start=0.0
            stop=(abs(vmin)+vmax)/(2.0*abs(vmin)) 
        midpoint=abs(vmin)/float((vmax+abs(vmin)))
        new_cm=shiftedColorMap(cm,midpoint=midpoint,start=start,stop=stop,name="new_map")
    else:
        # truncate lower half of colormap
        if vmax>0:
            start=0.5+0.5*abs(vmin/float(vmax))
            stop=1.0
        else:
            start=0.0
            stop=0.5-0.5*abs(vmax/float(vmin))
        new_cm=shiftedColorMap(cm,start=start,stop=stop,name="new_map",nomid=True)
    
    return new_cm
def invert_cm(cm,name="reversedCM"):
    #inverts a colormap
    new_cm=shiftedColorMap(cm,midpoint=0.5,start=1.0,stop=0.0)
    return new_cm
        for j in range(len(msk.T)):
            w2[i, j] = np.sum(ncdata2.variables[vv[number][1]][
                d1[0 + k] * 8:d1[1 + k] * 8, i, j]) / len(
                    range(d1[0 + k] * 8, d1[1 + k] * 8))  #Odyssea time range 1
            w1[i, j] = np.sum(
                nc1.variables[vv[number][0]][d1[0 + k]:d1[1 + k], i, j]) / len(
                    range(d1[0 + k], d1[1 + k]))
            #ROMS time range 1
    w = w1 - w2
    w = flipud(w)
    w = np.ma.masked_where(maska == 1, w)
    #mintemp=np.round(w.min(),-1); maxtemp=np.round(w.max(),-1);
    mintemp = temprange[number][0]
    maxtemp = temprange[number][1]
    midpt = 1 - maxtemp / (float(maxtemp) + abs(mintemp))
    v = range(mintemp, maxtemp + 10, 10)
    shifted_cmap = shiftedColorMap(cmap=mpl.cm.RdBu_r,
                                   midpoint=midpt,
                                   name='shifted')
    #norm = mpl.colors.Normalize(vmin=mintemp, vmax=maxtemp);
    nx2 = int((m1.xmax - m1.xmin) / 3000.) + 1
    ny2 = int((m1.ymax - m1.ymin) / 3000.) + 1
    tempo2 = m1.transform_scalar(w, lons, lats, nx2, ny2)
    CS3 = m1.imshow(tempo2, cmap=shifted_cmap, vmin=mintemp, vmax=maxtemp)
    plt.colorbar(CS3, cax, ticks=v, orientation='horizontal')
    m1.drawcountries()
    m1.fillcontinents(color='#ddaa66', lake_color='#9999FF')
    file_name = os.path.abspath("Time_period_%s_%s.png" %
                                (vv[number][0], str(k)))
    fig.savefig(file_name, dpi=200)