<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Precipitation in the Meteorology component
Goal
Step1:  Programmatically create a file holding the precipitation rate time series. This will mimic what I'll need to do in WMT, where I'll have access to the model time step and run duration. Start by defining the precipitation rate values
Step2:  Next, write the values to a file to the input directory, where it's expected by the cfg file
Step3:  Check the file
Step4:  BMI component
Import the BMI Meteorology component and create an instance
Step5:  Initialize the model. A value of snow depth h_snow is needed for the model to update.
Step6:  Unlike when P is a scalar, the initial model precipitation volume flux is the first value from precip_rates.txt
Step7:  Advance the model by one time step
Step8:  Unlike the scalar case, there's an output volume flux of precipitation
Step9:  Advance the model to the end, saving the model time and output P values (converted back to mm/hr for convenience) at each step
Step10:  Check the time and flux values
Step11:  Result
Step12:  Initialize the model.
Step13:  The initial model precipitation volume flux is the first value from precip_rates.txt
Step14:  Advance the model to the end, saving the model time and output P values (converted back to mm/hr for convenience) at each step
Step15:  Check the time and flux values (noting that I've included the time = 0.0 value here)
<ASSISTANT_TASK:>
Python Code:
mps_to_mmph = 1000 * 3600
Explanation: Precipitation in the Meteorology component
Goal: In this example, I give the Meteorology component a time series of precipitation values and check whether it produces output when the model state is updated.
Define a helpful constant:
End of explanation
import numpy as np
n_steps = 10  # can get from cfg file
precip_rates = np.linspace(5, 20, num=n_steps, endpoint=False)
precip_rates
Explanation: Programmatically create a file holding the precipitation rate time series. This will mimic what I'll need to do in WMT, where I'll have access to the model time step and run duration. Start by defining the precipitation rate values:
End of explanation
np.savetxt('./input/precip_rates.txt', precip_rates, fmt='%6.2f')
Explanation: Next, write the values to a file to the input directory, where it's expected by the cfg file:
End of explanation
cat input/precip_rates.txt
Explanation: Check the file:
End of explanation
from topoflow.components.met_base import met_component
m = met_component()
Explanation: BMI component
Import the BMI Meteorology component and create an instance:
End of explanation
m.initialize('./input/meteorology-1.cfg')
m.h_snow = 0.0  # Needed for update
Explanation: Initialize the model. A value of snow depth h_snow is needed for the model to update.
End of explanation
precip = m.get_value('atmosphere_water__precipitation_leq-volume_flux')  # `P` internally
print type(precip)
print precip.size
precip * mps_to_mmph
Explanation: Unlike when P is a scalar, the initial model precipitation volume flux is the first value from precip_rates.txt:
End of explanation
m.update()
print '\nCurrent time: {} s'.format(m.get_current_time())
Explanation: Advance the model by one time step:
End of explanation
print precip * mps_to_mmph  # note that this is a reference, so it'll take the current value of `P`
Explanation: Unlike the scalar case, there's an output volume flux of precipitation:
End of explanation
time = [m.get_current_time().copy()]
flux = [precip.copy() * mps_to_mmph]
while m.get_current_time() < m.get_end_time():
    m.update()
    time.append(m.get_current_time().copy())
    flux.append(m.get_value('atmosphere_water__precipitation_leq-volume_flux').copy() * mps_to_mmph)
Explanation: Advance the model to the end, saving the model time and output P values (converted back to mm/hr for convenience) at each step:
End of explanation
time
flux
Explanation: Check the time and flux values:
End of explanation
from cmt.components import Meteorology
met = Meteorology()
Explanation: Result: Fails. Input precipipation rates do not match output precipitation volume flux because of changes we made to TopoFlow source.
Babel-wrapped component
Import the Babel-wrapped Meteorology component and create an instance:
End of explanation
%cd input
met.initialize('meteorology-1.cfg')
Explanation: Initialize the model.
End of explanation
bprecip = met.get_value('atmosphere_water__precipitation_leq-volume_flux')
print type(bprecip)
print bprecip.size
print bprecip.shape
bprecip * mps_to_mmph
Explanation: The initial model precipitation volume flux is the first value from precip_rates.txt:
End of explanation
time = [met.get_current_time()]
flux = [bprecip.max() * mps_to_mmph]
count = 1
while met.get_current_time() < met.get_end_time():
    met.update(met.get_time_step()*count)
    time.append(met.get_current_time())
    flux.append(met.get_value('atmosphere_water__precipitation_leq-volume_flux').max() * mps_to_mmph)
    count += 1
Explanation: Advance the model to the end, saving the model time and output P values (converted back to mm/hr for convenience) at each step:
End of explanation
time
flux
Explanation: Check the time and flux values (noting that I've included the time = 0.0 value here):
End of explanation
<END_TASK>