Ignore:
Timestamp:
Nov 21, 2011, 6:27:42 PM (13 years ago)
Author:
aslmd
Message:

GRAPHICS: 1. handling of anomaly in profiles as well. 2. fixed xmin ymin etc... that were not working with 1D plot. 3. fixed localtime for mesoscale files, still a bit of work though.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UTIL/PYTHON/myplot.py

    r400 r405  
    6767
    6868## Author: AS + TN + AC
    69 def reducefield (input,d4=None,d3=None,d2=None,d1=None,yint=False,alt=None):
     69def reducefield (input,d4=None,d3=None,d2=None,d1=None,yint=False,alt=None,anomaly=False):
    7070    ### we do it the reverse way to be compliant with netcdf "t z y x" or "t y x" or "y x"
    7171    ### it would be actually better to name d4 d3 d2 d1 as t z y x
     72    ### ... note, anomaly is only computed over d1 and d2 for the moment
    7273    import numpy as np
    7374    from mymath import max,mean
     75    csmooth = 12 ## a fair amount of grid points
    7476    dimension = np.array(input).ndim
    7577    shape = np.array(input).shape
    7678    #print 'd1,d2,d3,d4: ',d1,d2,d3,d4
    7779    print 'IN  REDUCEFIELD dim,shape: ',dimension,shape
     80    if anomaly: print 'ANOMALY ANOMALY'
    7881    output = input
    7982    error = False
     
    110113             output = mean(input[d4,:,:,:],axis=0)
    111114             output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3)
     115             if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.)
    112116             output = mean(output[d2,:],axis=0)
    113117             output = mean(output[d1],axis=0)
     
    115119             output = mean(input[d4,:,:,:],axis=0)
    116120             output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3)
     121             if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.)
    117122             output = mean(output[d2,:],axis=0)
    118123        elif d4 is not None and d3 is not None and d1 is not None:
    119124             output = mean(input[d4,:,:,:],axis=0)
    120125             output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3)
     126             if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.)
    121127             output = mean(output[:,d1],axis=1)
    122128        elif d4 is not None and d2 is not None and d1 is not None:
    123129             output = mean(input[d4,:,:,:],axis=0)
     130             if anomaly:
     131                 for k in range(output.shape[0]):  output[k,:,:] = 100. * ((output[k,:,:] / smooth(output[k,:,:],csmooth)) - 1.)
    124132             output = mean(output[:,d2,:],axis=1)
    125133             output = mean(output[:,d1],axis=1)
     134             #noperturb = smooth1d(output,window_len=7)
     135             #lenlen = len(output) ; output = output[1:lenlen-7] ; yeye = noperturb[4:lenlen-4]
     136             #plot(output) ; plot(yeye) ; show() ; plot(output-yeye) ; show()
    126137        elif d3 is not None and d2 is not None and d1 is not None:
    127138             output = reduce_zaxis(input[:,d3,:,:],ax=1,yint=yint,vert=alt,indice=d3)
     139             if anomaly:
     140                 for k in range(output.shape[0]):  output[k,:,:] = 100. * ((output[k,:,:] / smooth(output[k,:,:],csmooth)) - 1.)
    128141             output = mean(output[:,d2,:],axis=1)
    129142             output = mean(output[:,d1],axis=1)
     
    131144             output = mean(input[d4,:,:,:],axis=0)
    132145             output = reduce_zaxis(output[d3,:,:],ax=0,yint=yint,vert=alt,indice=d3)
     146             if anomaly: output = 100. * ((output / smooth(output,csmooth)) - 1.)
    133147        elif d4 is not None and d2 is not None: 
    134148             output = mean(input[d4,:,:,:],axis=0)
     
    248262    from numpy import array
    249263    from string import rstrip
    250     namefile = rstrip( rstrip( namefile, chars="_z"), chars="_zabg")
     264    namefile = rstrip( rstrip( rstrip( namefile, chars="_z"), chars="_zabg"), chars="_p")
    251265    #### we assume that wrfout is next to wrfout_z and wrfout_zabg
    252266    nc  = Dataset(namefile)
     
    394408        [lon2d,lat2d] = [lon,lat]
    395409    return lon2d,lat2d
     410
     411## FROM COOKBOOK http://www.scipy.org/Cookbook/SignalSmooth
     412def smooth1d(x,window_len=11,window='hanning'):
     413    import numpy
     414    """smooth the data using a window with requested size.
     415    This method is based on the convolution of a scaled window with the signal.
     416    The signal is prepared by introducing reflected copies of the signal
     417    (with the window size) in both ends so that transient parts are minimized
     418    in the begining and end part of the output signal.
     419    input:
     420        x: the input signal
     421        window_len: the dimension of the smoothing window; should be an odd integer
     422        window: the type of window from 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'
     423            flat window will produce a moving average smoothing.
     424    output:
     425        the smoothed signal
     426    example:
     427    t=linspace(-2,2,0.1)
     428    x=sin(t)+randn(len(t))*0.1
     429    y=smooth(x)
     430    see also:
     431    numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve
     432    scipy.signal.lfilter
     433    TODO: the window parameter could be the window itself if an array instead of a string   
     434    """
     435    x = numpy.array(x)
     436    if x.ndim != 1:
     437        raise ValueError, "smooth only accepts 1 dimension arrays."
     438    if x.size < window_len:
     439        raise ValueError, "Input vector needs to be bigger than window size."
     440    if window_len<3:
     441        return x
     442    if not window in ['flat', 'hanning', 'hamming', 'bartlett', 'blackman']:
     443        raise ValueError, "Window is on of 'flat', 'hanning', 'hamming', 'bartlett', 'blackman'"
     444    s=numpy.r_[x[window_len-1:0:-1],x,x[-1:-window_len:-1]]
     445    #print(len(s))
     446    if window == 'flat': #moving average
     447        w=numpy.ones(window_len,'d')
     448    else:
     449        w=eval('numpy.'+window+'(window_len)')
     450    y=numpy.convolve(w/w.sum(),s,mode='valid')
     451    return y
    396452
    397453## Author: AS
     
    607663def fmtvar (whichvar="def"):
    608664    fmtvar    =     { \
    609              "tk":           "%.0f",\
     665             "TK":           "%.0f",\
    610666             "T_NADIR_DAY":  "%.0f",\
    611667             "T_NADIR_NIT":  "%.0f",\
    612668             "TEMP_DAY":     "%.0f",\
    613669             "TEMP_NIGHT":   "%.0f",\
    614              "tpot":         "%.0f",\
     670             "TPOT":         "%.0f",\
    615671             "TSURF":        "%.0f",\
    616672             "def":          "%.1e",\
     
    623679             "VMR_ICE":      "%.1e",\
    624680             "MTOT":         "%.1f",\
    625              "anomaly":      "%.1f",\
     681             "ANOMALY":      "%.1f",\
    626682             "W":            "%.1f",\
    627683             "WMAX_TH":      "%.1f",\
    628684             "QSURFICE":     "%.0f",\
    629              "Um":           "%.0f",\
     685             "UM":           "%.0f",\
    630686             "ALBBARE":      "%.2f",\
    631687             "TAU":          "%.1f",\
     
    650706             "def":          "spectral",\
    651707             "HGT":          "spectral",\
    652              "tk":           "gist_heat",\
     708             "TK":           "gist_heat",\
    653709             "TSURF":        "RdBu_r",\
    654710             "QH2O":         "PuBu",\
     
    666722             "W":            "jet",\
    667723             "WMAX_TH":      "spectral",\
    668              "anomaly":      "RdBu_r",\
     724             "ANOMALY":      "RdBu_r",\
    669725             "QSURFICE":     "hot_r",\
    670726             "ALBBARE":      "spectral",\
Note: See TracChangeset for help on using the changeset viewer.