Changeset 1437 in lmdz_wrf for trunk/tools/generic_tools.py


Ignore:
Timestamp:
Feb 8, 2017, 7:53:47 PM (8 years ago)
Author:
lfita
Message:

adding `auto_val': Function to provide a value following an 'auto' configuration
adding type('S') to `retype'
adding `rectangular_spiral': Function to provide a rectangular spiral (along x,y-axis values) of values

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1414 r1437  
    4545# ASCII_LaTeX: Function to transform from an ASCII character to LaTeX codification
    4646# ASCII_to: Function to provide the equivalence text to a given lenguage omitting certain characters
     47# auto_val: Function to provide a value following an 'auto' configuration
    4748# Capturing: Class to capture the standard output from a function
    4849# CFmonthU_daysU: Function to transform from a CF date series with units as 'months since [DATE]' to 'days since [DATE]'
     
    105106# radius_angle: Function to generate a matrix with the angle at a given point
    106107# radius_dist: Function to generate a matrix with the distance at a given point
     108# rectangular_spiral: Function to provide a rectangular spiral (along x,y-axis values) of values
    107109# replace_list: Function to replace a value in a given list
    108110# roman_to_int: Convert a roman numeral to an integer
     
    50875089        else:
    50885090            newval = True
    5089     elif vtype == '|S1':
     5091    elif vtype == '|S1' or vtype == type('s'):
    50905092        newval = str(val)
    50915093    else:
     
    1125811260    return stringv
    1125911261
     11262def auto_val(valchk, aval):
     11263    """ Function to provide a value following an 'auto' configuration
     11264      valchk= value to take (if != 'auto'), value is transformed to the type(aval)
     11265      aval= value to take when it is configured as 'auto'
     11266      >>> auto_val('auto', 3.)
     11267      3.
     11268      >>> auto_val('2.3456', 3.)
     11269      2.3456
     11270    """
     11271    fname = 'auto_val'
     11272
     11273    if valchk == 'auto':
     11274        autoval = aval
     11275    else:
     11276        autoval = retype(valchk, type(aval))
     11277
     11278    return autoval
     11279
     11280def rectangular_spiral(Ntimes, dunits):
     11281    """ Function to provide a rectangular spiral (along x,y-axis values) of values
     11282      Ntimes: number of loops of the spiral (increasing by 1 along each axis
     11283      dunits: units interval from which assign a value for each spiral value
     11284    >>> rectangular_spiral(3, 3600.)
     11285    array([ 0.,  1.,  1.,  0., -1., -1., -1.,  0.,  1.,  2.,  2.,  0., -2., -2., -2., 
     11286        0 array([ 0.,  0.,  1.,  1.,  1.,  0., -1., -1., -1.,  0.,  2.,  2.,  2.,
     11287        0., -2., -2., -2.,  0.,  3.,  3.,  3.,  0., -3., -3., -3.]), [0.0, 3600.0,
     11288        7200.0, 10800.0, 14400.0, 18000.0, 21600.0, 25200.0, 28800.0, 32400.0, 36000.0,
     11289        39600.0, 43200.0, 46800.0, 50400.0, 54000.0, 57600.0, 61200.0, 64800.0, 68400.0,
     11290        72000.0, 75600.0, 79200.0, 82800.0, 86400.0])
     11291    """
     11292    fname = 'rectangular_spiral'
     11293
     11294    # Wind test values
     11295    uwindtestv0 = [1., 1., 0., -1., -1., -1., 0., 1. ]
     11296    vwindtestv0 = [0., 1., 1., 1., 0., -1., -1., -1. ]
     11297
     11298    Nvals = len(uwindtestv0)
     11299
     11300    uwindtestv = np.zeros((Nvals*Ntimes+1), dtype=np.float)
     11301    vwindtestv = np.zeros((Nvals*Ntimes+1), dtype=np.float)
     11302
     11303    for i in range(Ntimes):
     11304        uwindtestv[i*Nvals+1:(i+1)*Nvals+1] = np.array(uwindtestv0)*(i+1)
     11305        vwindtestv[i*Nvals+1:(i+1)*Nvals+1] = np.array(vwindtestv0)*(i+1)
     11306
     11307    dimt = len(uwindtestv)
     11308    twindtestv = []
     11309    for it in range(dimt):
     11310        twindtestv.append(dtsecs*it)
     11311
     11312    return uwindtestv, vwindtestv, twindtestv
     11313
    1126011314#quit()
Note: See TracChangeset for help on using the changeset viewer.