"""Calculations involved in mass and energy budgets."""
import numpy as np
from aospy.utils import coord_to_new_dataarray

from .. import TIME_STR


def first_to_last_vals_dur(arr, freq='1M'):
    """Time elapsed between 1st and last values in each given time period."""
    time = coord_to_new_dataarray(arr, TIME_STR)
    first = time.resample(freq, TIME_STR, how='first')
    last = time.resample(freq, TIME_STR, how='last')
    delta_time = last - first
    # Divide by a 1 sec timedelta to convert to seconds.
    delta_time.values = delta_time.values / np.timedelta64(1, 's')
    return delta_time


def time_tendency(arr, freq='1M'):
    """Monthly time tendency of the given field."""
    first = arr.resample(freq, TIME_STR, how='first').dropna(TIME_STR)
    last = arr.resample(freq, TIME_STR, how='last').dropna(TIME_STR)
    return (last - first) / first_to_last_vals_dur(arr, freq)

"""Calculations involved in mass and energy budgets."""
import numpy as np
from aospy.utils import coord_to_new_dataarray

from .. import TIME_STR


def first_to_last_vals_dur(arr, freq='1M'):
    """Time elapsed between 1st and last values in each given time period."""
    time = coord_to_new_dataarray(arr, TIME_STR)
    first = time.resample(freq, TIME_STR, how='first')
    last = time.resample(freq, TIME_STR, how='last')
    delta_time = last - first
    # Divide by a 1 sec timedelta to convert to seconds.
    delta_time.values = delta_time.values / np.timedelta64(1, 's')
    return delta_time


def time_tendency(arr, freq='1M'):
    """Monthly time tendency of the given field."""
    first = arr.resample(freq, TIME_STR, how='first').dropna(TIME_STR)
    last = arr.resample(freq, TIME_STR, how='last').dropna(TIME_STR)
    return (last - first) / first_to_last_vals_dur(arr, freq)


def time_tendency_each_timestep(arr):
    """Time tendency of the given field between each timestep.

    The last or first timestep will not have a tendency, due to the finite
    differencing.  So the output array will have length one less.
    """
    return (arr.diff(dim=TIME_STR, label='upper') /
            arr[TIME_STR].diff(dim=TIME_STR, label='upper'))

Add experimental every-timestep tendency function
Add experimental every-timestep tendency function

  """Calculations involved in mass and energy budgets."""
  import numpy as np
  from aospy.utils import coord_to_new_dataarray
  
  from .. import TIME_STR
  
  
  def first_to_last_vals_dur(arr, freq='1M'):
      """Time elapsed between 1st and last values in each given time period."""
      time = coord_to_new_dataarray(arr, TIME_STR)
      first = time.resample(freq, TIME_STR, how='first')
      last = time.resample(freq, TIME_STR, how='last')
      delta_time = last - first
      # Divide by a 1 sec timedelta to convert to seconds.
      delta_time.values = delta_time.values / np.timedelta64(1, 's')
      return delta_time
  
  
  def time_tendency(arr, freq='1M'):
      """Monthly time tendency of the given field."""
      first = arr.resample(freq, TIME_STR, how='first').dropna(TIME_STR)
      last = arr.resample(freq, TIME_STR, how='last').dropna(TIME_STR)
      return (last - first) / first_to_last_vals_dur(arr, freq)
  
+ 
+ def time_tendency_each_timestep(arr):
+     """Time tendency of the given field between each timestep.
+ 
+     The last or first timestep will not have a tendency, due to the finite
+     differencing.  So the output array will have length one less.
+     """
+     return (arr.diff(dim=TIME_STR, label='upper') /
+             arr[TIME_STR].diff(dim=TIME_STR, label='upper'))
+ 
Add experimental every-timestep tendency function
## Code Before:
"""Calculations involved in mass and energy budgets."""
import numpy as np
from aospy.utils import coord_to_new_dataarray

from .. import TIME_STR


def first_to_last_vals_dur(arr, freq='1M'):
    """Time elapsed between 1st and last values in each given time period."""
    time = coord_to_new_dataarray(arr, TIME_STR)
    first = time.resample(freq, TIME_STR, how='first')
    last = time.resample(freq, TIME_STR, how='last')
    delta_time = last - first
    # Divide by a 1 sec timedelta to convert to seconds.
    delta_time.values = delta_time.values / np.timedelta64(1, 's')
    return delta_time


def time_tendency(arr, freq='1M'):
    """Monthly time tendency of the given field."""
    first = arr.resample(freq, TIME_STR, how='first').dropna(TIME_STR)
    last = arr.resample(freq, TIME_STR, how='last').dropna(TIME_STR)
    return (last - first) / first_to_last_vals_dur(arr, freq)

## Instruction:
Add experimental every-timestep tendency function
## Code After:
"""Calculations involved in mass and energy budgets."""
import numpy as np
from aospy.utils import coord_to_new_dataarray

from .. import TIME_STR


def first_to_last_vals_dur(arr, freq='1M'):
    """Time elapsed between 1st and last values in each given time period."""
    time = coord_to_new_dataarray(arr, TIME_STR)
    first = time.resample(freq, TIME_STR, how='first')
    last = time.resample(freq, TIME_STR, how='last')
    delta_time = last - first
    # Divide by a 1 sec timedelta to convert to seconds.
    delta_time.values = delta_time.values / np.timedelta64(1, 's')
    return delta_time


def time_tendency(arr, freq='1M'):
    """Monthly time tendency of the given field."""
    first = arr.resample(freq, TIME_STR, how='first').dropna(TIME_STR)
    last = arr.resample(freq, TIME_STR, how='last').dropna(TIME_STR)
    return (last - first) / first_to_last_vals_dur(arr, freq)


def time_tendency_each_timestep(arr):
    """Time tendency of the given field between each timestep.

    The last or first timestep will not have a tendency, due to the finite
    differencing.  So the output array will have length one less.
    """
    return (arr.diff(dim=TIME_STR, label='upper') /
            arr[TIME_STR].diff(dim=TIME_STR, label='upper'))
