Changeset 628 in lmdz_wrf for trunk


Ignore:
Timestamp:
Sep 14, 2015, 6:56:14 PM (9 years ago)
Author:
lfita
Message:

Working on the radi_traj

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r627 r628  
    1313fillValueC = '-'
    1414fillValueI = -99999
     15
     16def values_line(line, splitv, chars):
     17    """ Function to retrieve values from a line of ASCII text removing a series of characters
     18      line= line of ASCII text to work with
     19      splitv= character to use to split with the line
     20      chars= list of characters/strings to remove
     21    >>> values_line('0.0 0.0 1 [ 0 0 ]', ' ', ['[', ']'])
     22    ['0.0', '0.0', '1', '0', '0']
     23    """
     24    fname = 'values_line'
     25
     26    vals = line.replace('\n', '')
     27    for ic in chars:
     28        vals = vals.replace(ic, '')
     29
     30    vals0 = vals.split(splitv)
     31    vals = []
     32    for val in vals0:
     33        if not len(val) == 0: vals.append(val)
     34
     35    return vals
    1536
    1637def filexist(filen, emsg, fmsg):
     
    1515515176    return radpos
    1515615177
     15178def read_SQradii(filename='SQradii.dat', Npt=-1, maxradii=None):
     15179    """ Function to read the outcome of the function `squared_radial'
     15180      filename= Name of the outcome file of the function
     15181      Npt= Number of points on each grid
     15182      maxradii= allowed maximum radii
     15183      'SQradii.dat': file with the radii and paris of integers after ran squared_radial(1000)
     15184    """
     15185    fname = 'read_SQradii'
     15186
     15187    if not os.path.isfile(filename):
     15188        print errormsg
     15189        print '  ' + fname + ": file '" + filename + "' not found!!"
     15190        quit(-1)
     15191
     15192    infile = open(filename, 'r')
     15193
     15194    gridradii = {}
     15195    Nradii = {}
     15196    radii = []
     15197    for line in infile:
     15198        values = values_line(line, ' ', ['[', ']'])
     15199        rad = np.float(values[0])
     15200# Limited by a given radii
     15201        if maxradii is not None and rad > maxradii: break
     15202        Nrad = int(values[2])
     15203        couples = None
     15204        Nradgood = 0
     15205        if Nrad > 1:
     15206           Ncouples = 0
     15207           couples = []
     15208           for ic in range(Nrad):
     15209               ix = int(values[3 + ic*2])
     15210               iy = int(values[3 + ic*2 + 1])
     15211               if ix <= Npt and iy <= Npt:
     15212                   couples.append(np.array([ix,iy], dtype=int))
     15213                   Nradgood = Nradgood + 1
     15214               elif ix > Npt and iy > Npt: break
     15215        else:
     15216             ix = int(values[3])
     15217             iy = int(values[4])
     15218             if ix <= Npt and iy <= Npt:
     15219                 couples = np.array([ix,iy], dtype=int)
     15220                 Nradgood = Nradgood + 1
     15221             elif ix > Npt and iy > Npt: break
     15222
     15223        if Nradgood > 0:
     15224            Nradii[rad] = Nradgood
     15225            gridradii[rad] = np.array(couples)
     15226
     15227#    for rad in sorted(gridradii.keys()):
     15228#        print rad, gridradii[rad]
     15229
     15230    return gridradii
     15231
    1515715232def grid_combinations(x,y):
    1515815233    """ Function to provide all the possible grid points combination for a given pair of values
     
    1518815263    fname = 'radii_points'
    1518915264
    15190     radiipos = np.zeros((Nangle, radii, 2), dtype = int)
     15265    radiipos = np.zeros((Nang, Lrad, 2), dtype = int)
    1519115266    SQradiipos = {}
    1519215267
     
    1519915274    for ia in range(Nang):
    1520015275        angle = 2.*np.pi*ia/Nang
     15276        posangle = np.zeros((Lrad,2), dtype=np.float)
    1520115277
    1520215278# Points at that given angle
    15203         pangle = radial_points(angle,radii)
     15279        pangle = radial_points(angle,Lrad)
    1520415280        posangle[:,0] = pangle[:,0] + xpos
    1520515281        posangle[:,1] = pangle[:,1] + ypos
    1520615282
    15207         for ip in range(radii):
     15283        for ip in range(Lrad):
    1520815284            iipos = int(posangle[ip,0])
    1520915285            jjpos = int(posangle[ip,1])
    1521015286
    1521115287# Creation of a matrix with all the grid points at each time-step
    15212             if iipos >= 0 and iipos < dx and jjpos >= 0 and jjpos < dy:
     15288            if iipos >= 0 and iipos < dx-1 and jjpos >= 0 and jjpos < dy-1:
    1521315289                radiipos[ia,ip,0] = iipos
    1521415290                radiipos[ia,ip,1] = jjpos
     
    1522215298
    1522315299# pairs loop
     15300        Npairs = len(pairs.flatten())/2
    1522415301        pairsw = []
    15225         for ip in range(len(pairs)):
    15226             allpairs = grid_combinations(pairs[ip*2],pairs[ip*2+1])
    15227 
     15302        print 'Lluis ',SQradii[ir],'pairs:',pairs, type([pairs])
     15303        for ip in range(Npairs):
     15304            if Npairs == 0:
     15305                print '  Lluis NO pair:', SQradii[ir],':',pairs
     15306                break
     15307            elif Npairs == 1:
     15308                print '  Lluis single pair:',pairs[ip*2],pairs[ip*2+1]
     15309                allpairs = grid_combinations(pairs[ip*2],pairs[ip*2+1])
     15310            else:
     15311                print ip,'  Lluis 1 pair:',pairs[ip,0],pairs[ip,1]
     15312                allpairs = grid_combinations(pairs[ip,0],pairs[ip,1])
     15313               
    1522815314            for iap in range(len(allpairs)):
    1522915315                ppos = allpairs[iap]
    15230                 iipos = trajvals[it,1] + ppos[0]
    15231                 jjpos = trajvals[it,1] + ppos[0]
     15316                iipos = xpos + ppos[0]
     15317                jjpos = ypos + ppos[1]
    1523215318
    1523315319# Creation of a matrix with all the grid points at each time-step
     
    1531715403        dimt = objfile.variables[timn].shape
    1531815404
    15319     if Tbeg + Ttraj > dimt:
    15320         print errormsg
    15321         print '  ' + fname + ': trajectory has ', Ttraj, ' time steps and starts ' + \
    15322         ' at',Tbeg,' and data',varobj.shape[0], '" !!!!!'
    15323         quit(-1)
     15405# Removing this checking
     15406#    if Tbeg + Ttraj > dimt:
     15407#        print errormsg
     15408#        print '  ' + fname + ': trajectory has ', Ttraj, ' time steps and starts ' + \
     15409#        ' at',Tbeg,' and data',varobj.shape[0], '" !!!!!'
     15410#        quit(-1)
    1532415411
    1532515412    print '    ' + fname + ': Number of time-steps in trajectory file: ',Ttraj
     
    1534215429    it = 0
    1534315430
    15344     trajpos = np.zeros(Ttraj,Nangle,raddi)
     15431    trajpos = np.zeros((Ttraj,Nangle,radii), dtype=np.float)
     15432    gtrajvals = np.zeros((Ttraj,3), dtype=int)
     15433    trajvals = np.zeros((Ttraj,3), dtype=np.float)
    1534515434
    1534615435# Grid position at each time-step and angle
    15347     varvalst = np.ones((Traj, Nangle, radii, 2), dtype=np.float)*fillValue
    15348     varvalstSQ = np.zeros((Traj, radii, radii), dtype=bool)
     15436#    varvalst = np.ones((Ttraj, Nangle, radii, 2), dtype=np.float)*fillValue
     15437#    varvalstSQ = np.zeros((Ttraj, radii, radii), dtype=bool)
    1534915438
    1535015439    it = 0
     
    1603616125
    1603716126compute_tevolboxtraj_radialsec('/bdd/PCER/workspace/lfita/etudes/FF/medic051215_2km/run/trajectory.dat@6,XLONG,XLAT,ZNU,Times,wrf,4,10',       \
    16038   '001/full_concatenated.nc', 'U10')
     16127  '/bdd/PCER/workspace/lfita/etudes/FF/medic051215_2km/run/wrfout/wrfout_d02_2005-12-12_00:00:00', 'U10')
    1603916128quit()
    1604016129
     
    1644316532    return fvalues
    1644416533
    16445 def values_line(line, splitv, chars):
    16446     """ Function to retrieve values from a line of ASCII text removing a series of characters
    16447       line= line of ASCII text to work with
    16448       splitv= character to use to split with the line
    16449       chars= list of characters/strings to remove
    16450     >>> values_line('0.0 0.0 1 [ 0 0 ]', ' ', ['[', ']'])
    16451     ['0.0', '0.0', '1', '0', '0']
    16452     """
    16453     fname = 'values_line'
    16454 
    16455     vals = line.replace('\n', '')
    16456     for ic in chars:
    16457         vals = vals.replace(ic, '')
    16458 
    16459     vals0 = vals.split(splitv)
    16460     vals = []
    16461     for val in vals0:
    16462         if not len(val) == 0: vals.append(val)
    16463 
    16464     return vals
    16465 
    1646616534def reduce_last_spaces(string):
    1646716535    """ Function to reduce the last right spaces from a string
     
    1649016558
    1649116559    return newstr
    16492 
    16493 def read_SQradii(filename='SQradii.dat', Npt=1000, maxradii=None):
    16494     """ Function to read the outcome of the function `squared_radial'
    16495       filename= Name of the outcome file of the function
    16496       Npt= Number of points on each grid
    16497       maxradii= allowed maximum radii
    16498       'SQradii.dat': file with the radii and paris of integers after ran squared_radial(1000)
    16499     """
    16500     fname = 'read_SQradii'
    16501 
    16502     if not os.path.isfile(filename):
    16503         print errormsg
    16504         print '  ' + fname + ": file '" + filename + "' not found!!"
    16505         quit(-1)
    16506 
    16507     infile = open(filename, 'r')
    16508 
    16509     gridradii = {}
    16510     Nradii = {}
    16511     radii = []
    16512     for line in infile:
    16513         print 'line:',line
    16514         values = values_line(line, ' ', ['[', ']'])
    16515         rad = np.float(values[0])
    16516 # Limited by a given radii
    16517         if maxradii is not None and rad > maxradii: break
    16518         Nrad = int(values[2])
    16519         couples = None
    16520         Nradgood = 0
    16521         if Nrad > 1:
    16522            Ncouples = 0
    16523            couples = []
    16524            for ic in range(Nrad):
    16525                ix = int(values[3 + ic*2])
    16526                iy = int(values[3 + ic*2 + 1])
    16527                if ix <= Npt and iy <= Npt:
    16528                    couples.append(np.array([ix,iy], dtype=int))
    16529                    Nradgood = Nradgood + 1
    16530                elif ix > Npt and iy > Npt: break
    16531         else:
    16532              ix = int(values[3])
    16533              iy = int(values[4])
    16534              if ix <= Npt and iy <= Npt:
    16535                  couples = np.array([ix,iy], dtype=int)
    16536                  Nradgood = Nradgood + 1
    16537              elif ix > Npt and iy > Npt: break
    16538 
    16539         if Nradgood > 0:
    16540             Nradii[rad] = Nradgood
    16541             gridradii[rad] = np.array(couples)
    16542 
    16543     for rad in sorted(gridradii.keys()):
    16544         print rad, gridradii[rad]
    16545 
    16546     return gridradii
    1654716560
    1654816561def squared_radial(Npt):
Note: See TracChangeset for help on using the changeset viewer.