Changeset 2810 in lmdz_wrf


Ignore:
Timestamp:
Apr 8, 2020, 9:45:23 PM (5 years ago)
Author:
lfita
Message:

Adding:

  • `truncate': Function to truncate a Float value to a certain decimal
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r2807 r2810  
    256256#   of time-steps for a whole period is multiple of a given number
    257257# timefmt_timelab: Function to transform from C-like time format to general one
     258# truncate: Function to truncate a Float value to a certain decimal
    258259# unitsdsDate: Function to know how many units of time are from a given pair of dates
    259260# units_lunits: Function to provide LaTeX equivalences from a given units
     
    1409514096    return values
    1409614097
     14098def truncate(fv, decimals=7):
     14099    """ Function to truncate a Float value to a certain decimal
     14100      FROM: https://realpython.com/python-rounding/#truncation
     14101      fv: float value
     14102      decimals: decimals to which truncate (7, default)
     14103    >>> truncate(np.pi)
     14104    3.1415926
     14105    >>> truncate(np.pi, decimals=3)
     14106    3.141
     14107    >>> truncate(np.pi, decimals=0)
     14108    3.0
     14109    >>> truncate(1./3., decimals=7)
     14110    0.3333333
     14111    """
     14112    fname = 'truncate'
     14113
     14114    multiplier = 10. ** decimals
     14115    tfv = int(fv * multiplier) / multiplier
     14116
     14117    return int(fv * multiplier) / multiplier
     14118
    1409714119def angle_DegMinSec(angle):
    1409814120   """ Function to transform an angle to Degrees Minutes Seconds
    1409914121     angle= decimal angle to transform
    1410014122     >>> angle_DegMinSec(3.98765)
    14101      3 59 15.54
     14123     3, 59, 15.54
    1410214124     >>> angle_DegMinSec(-23.0025)
    14103      -23 0 9.0                         
     14125     -23, 0, 9.0                         
    1410414126   """
    1410514127   fname = 'angle_DegMinSec'
     
    1410814130   minv = int((abs(angle) - degv*1.)*60.)
    1410914131   secv = (abs(angle) - degv*1. - minv/60.)*3600.
     14132
     14133   if secv < 1.e-8: secv = 0.
    1411014134                                                 
    1411114135   if angle < 0.:
     
    1411314137   else:
    1411414138       return degv, minv, secv
     14139
     14140print angle_DegMinSec(49.1)
     14141print angle_DegMinSec(3.98765)
     14142quit()
    1411514143
    1411614144def DegMinSec_angle(degv, minv, secv):
     
    1756817596    return newlist
    1756917597
    17570 #quit()
    17571 
    17572 
Note: See TracChangeset for help on using the changeset viewer.