API reference

This page provides an auto-generated summary of metsim’s API. For more details and examples, refer to the relevant chapters in the main part of the documentation.

MetSim

The main object of the MetSim package. The MetSim object is used to set up and launch forcing generation and/or disaggregation routines.

The MetSim object uses a class dictionary to refer to the model setup, which can be modified after instantiation if necessary. Before calling run or launch on the instance it is required to call the load function to ensure that all of the required parameters have been set and that the input data is sufficient to provide the output specified.

class metsim.metsim.MetSim(params: dict)[source]

MetSim handles the distribution of jobs that write to a common file by launching muliple processes and queueing up their writeback so that work can be done while IO is happening.

launch()[source]

Farm out the jobs to separate processes

run()[source]

Kicks off the disaggregation and queues up data for IO

write(suffix='')[source]

Dispatch to the right function based on the configuration given

write_ascii(suffix)[source]

Write out as ASCII to the output file

write_netcdf(suffix: str)[source]

Write out as NetCDF to the output file

metsim.metsim.wrap_run(func: <built-in function callable>, loc: dict, params: dict, ds: xarray.core.dataset.Dataset, state: xarray.core.dataset.Dataset, disagg: bool, out_times: pandas.tseries.index.DatetimeIndex, year: str)[source]

Iterate over a chunk of the domain. This is wrapped so we can return a tuple of locs and df.

Parameters:
  • func (callable) – The function to call to do the work
  • loc (dict) – Some subset of the domain to do work on
  • params (dict) – Parameters from a MetSim object
  • ds (xr.Dataset) – Input forcings and domain
  • state (xr.Dataset) – State variables at the point of interest
  • disagg (bool) – Whether or not we should run a disagg routine
  • out_times (pd.DatetimeIndex) – Times to return (should be trimmed 1 day at each end from the given index)
  • year (str) – The year being run. This is used to add on extra times to make output smooth at endpoints if the run is chunked in time.
Returns:

A list of tuples arranged as (location, hourly_output, daily_output)

Return type:

results

Physics

physics

metsim.physics.atm_pres(elev: float, lr: float) → float[source]

Atmospheric pressure (Pa) as a function of elevation (m)

[1]Iribane, J.V., and W.L. Godson, 1981. Atmospheric Thermodynamics, 2nd Edition. D. Reidel Publishing Company, Dordrecht, The Netherlands (p. 168)
Parameters:
  • elev – Elevation in meters
  • lr – Lapse rate (K/m)
Returns:

Atmospheric pressure (Pa)

Return type:

pressure

metsim.physics.calc_pet(rad: <built-in function array>, ta: <built-in function array>, dayl: <built-in function array>, pa: float, dt: float = 0.2) → <built-in function array>[source]

Calculates the potential evapotranspiration for aridity corrections in calc_vpd(), according to Kimball et al., 1997

Parameters:
  • rad – daylight average incident shortwave radiation (W/m2)
  • ta – daylight average air temperature (deg C)
  • dayl – daylength (s)
  • pa – air pressure (Pa)
  • dt – offset for saturation vapor pressure calculation
Returns:

Potential evapotranspiration (cm/day)

Return type:

pet

metsim.physics.solar_geom[source]

Flat earth assumption

Parameters:
  • elev – Elevation in meters
  • lat – Latitude in decimal format
  • lr – Lapse rate in K/m
Returns:

(tiny_rad_fract, daylength, flat_potrad, tt_max0)

Return type:

sg

metsim.physics.svp[source]

Compute the saturated vapor pressure.

[2]Maidment, David R. Handbook of hydrology. McGraw-Hill Inc., 1992 Equation 4.2.2.
Parameters:
  • temp – Temperature (degrees Celsius)
  • a – (optional) parameter
  • b – (optional) parameter
  • c – (optional) parameter
Returns:

Saturated vapor pressure (Pa)

Return type:

svp

metsim.physics.svp_slope(temp: pandas.core.series.Series, a: float = 0.61078, b: float = 17.269, c: float = 237.3)[source]

Compute the gradient of the saturated vapor pressure as a function of temperature.

[3]Maidment, David R. Handbook of hydrology. McGraw-Hill Inc., 1992. Equation 4.2.3.
Parameters:temp – Temperature (degrees Celsius)
Returns:Gradient of d(svp)/dT.
Return type:dsvp_dT

MtClim

MTCLIM

metsim.methods.mtclim.calc_snowpack(df: pandas.core.frame.DataFrame, params: dict, snowpack: float = 0.0)[source]

Estimate snowpack as swe.

Parameters:
  • df – Dataframe with daily timeseries of precipitation and minimum temperature.
  • snowpack – (Optional - defaults to 0) Initial snowpack
metsim.methods.mtclim.calc_srad_hum(df: pandas.core.frame.DataFrame, sg: dict, elev: float, params: dict)[source]

Calculate shortwave, humidity

Parameters:
  • df – Dataframe containing daily timeseries
  • elev – Elevation in meters
  • params – A dictionary of parameters from the MetSim object
metsim.methods.mtclim.calc_t_air(df: pandas.core.frame.DataFrame, elev: float, params: dict)[source]

Calculate mean daily temperature

Parameters:
  • df – Dataframe with daily max and min temperatures
  • elev – Elevation in meters
  • params – Dictionary containing parameters from a MetSim object.
metsim.methods.mtclim.run(forcing: pandas.core.frame.DataFrame, params: dict, sg: dict, elev: float, swe: float)[source]

Run all of the mtclim forcing generation

Parameters:
  • forcing (pd.DataFrame) – The daily forcings given from input
  • params (dict) – Dictionary of parameters from a MetSim object
  • solar_geom (dict) – Solar geometry of the site
  • elev (float) – Elevation of the site being simulated
  • swe (float) – Initial snow water equivalent (SWE) for the site being simulated
Returns:

Dataframe of daily or subdaily forcings

Return type:

forcing

metsim.methods.mtclim.sw_hum_iter(df: pandas.core.frame.DataFrame, sg: dict, pa: float, pva: pandas.core.series.Series, dtr: pandas.core.series.Series, params: dict)[source]

Calculated updated values for dewpoint temperature and saturation vapor pressure.

Parameters:
  • df – Dataframe containing daily timeseries of cloud cover fraction, tfmax, swe, and shortwave radiation
  • sg – Solar geometry dictionary, calculated with metsim.physics.solar_geom.
  • pa – Air pressure in Pascals
  • pva – Vapor presure in Pascals
  • dtr – Daily temperature range
  • params – A dictionary of parameters from a MetSim object
Returns:

A tuple of dewpoint temperature and saturation vapor pressure

Return type:

(tdew, svp)

Disagg

Disaggregates daily data down to finer grained data using some heuristics

metsim.disaggregate.disaggregate(df_daily: pandas.core.frame.DataFrame, params: dict, solar_geom: dict, t_begin: list = None, t_end: list = None)[source]

Take a daily timeseries and scale it down to a finer time scale.

Parameters:
  • df_daily (pd.DataFrame) – Dataframe containing daily timeseries. Should be the result of one of the methods provided in the methods directory.
  • params (dict) – A dictionary containing the class parameters of the MetSim object.
  • solar_geom (dict) – A dictionary of solar geometry variables
  • t_begin (list) – List of t_min and t_max for day previous to the start of df_daily. None indicates no extension of the record.
  • t_end (list) – List of t_min and t_max for day after the end of df_daily. None indicates no extension of the record.
Returns:

A dataframe with sub-daily timeseries.

Return type:

df_disagg

metsim.disaggregate.longwave(air_temp: pandas.core.series.Series, vapor_pressure: pandas.core.series.Series, tskc: pandas.core.series.Series, params: dict)[source]

Calculate longwave. This calculation can be performed using a variety of parameterizations for both the clear sky and cloud covered emissivity. Options for choosing these parameterizations should be passed in via the params argument.

For more information about the options provided in this function see:

[1]Bohn, T.J., Livneh, B., Oyler, J.W., Running, S.W., Nijssen, B. and Lettenmaier, D.P., 2013. Global evaluation of MTCLIM and related algorithms for forcing of ecological and hydrological models. Agricultural and forest meteorology, 176, pp.38-49, doi:10.1016/j.agrformet.2013.03.003.
Parameters:
  • air_temp – Sub-daily temperature
  • vapor_pressure – Sub-daily vapor pressure
  • tskc – Daily cloud fraction
  • params – A dictionary of parameters, which contains information about which emissivity and cloud fraction methods to use.
Returns:

A sub-daily timeseries of the longwave radiation as well as a sub-daily timeseries of the cloud cover fraction.

Return type:

(lwrad, tskc)

metsim.disaggregate.prec(prec: pandas.core.series.Series, ts: float)[source]

Splits the daily precipitation evenly throughout the day Note: this returns only through to the beginning of the

last day. Final values are filled in using a forward fill in the top level disaggregate function
Parameters:
  • prec – Daily timeseries of precipitation
  • ts – Timestep to disaggregate down to
Returns:

A sub-daily timeseries of precipitation

Return type:

prec

metsim.disaggregate.pressure(temp: pandas.core.series.Series, elev: float, lr: float)[source]

Calculates air pressure.

Parameters:
  • temp – A sub-daily timeseries of temperature
  • elev – Elevation
  • lr – Lapse rate
Returns:

A sub-daily timeseries of air pressure (kPa)

Return type:

pressure

metsim.disaggregate.relative_humidity(vapor_pressure: pandas.core.series.Series, temp: pandas.core.series.Series)[source]

Calculate relative humidity from vapor pressure and temperature.

Parameters:
  • vapor_pressure – A sub-daily timeseries of vapor pressure
  • temp – A sub-daily timeseries of temperature
Returns:

A sub-daily timeseries of relative humidity

Return type:

rh

metsim.disaggregate.set_min_max_hour(disagg_rad: pandas.core.series.Series, n_days: int, ts: float, params: dict)[source]

Determine the time at which min and max temp is reached for each day.

Parameters:
  • disagg_rad – Shortwave radiation disaggregated to sub-daily timesteps.
  • n_days – The number of days being disaggregated
  • ts – Timestep used for disaggregation
  • params – A dictionary of class parameters of the MetSim object.
Returns:

A tuple containing 2 timeseries, corresponding to time of min and max temp, respectively

Return type:

(t_t_min, t_t_max)

metsim.disaggregate.shortwave(sw_rad: pandas.core.series.Series, daylength: pandas.core.series.Series, day_of_year: pandas.core.series.Series, tiny_rad_fract: <built-in function array>, params: dict)[source]

Disaggregate shortwave radiation down to a subdaily timeseries.

Parameters:
  • sw_rad – Daily incoming shortwave radiation
  • daylength – List of daylength time for each day of year
  • day_of_year – Timeseries of index of days since Jan-1
  • tiny_rad_fract – Fraction of the daily potential radiation during a radiation time step defined by SW_RAD_DT
  • params – Dictionary of parameters from the MetSim object
Returns:

A sub-daily timeseries of shortwave radiation.

Return type:

disaggrad

metsim.disaggregate.specific_humidity(vapor_pressure: pandas.core.series.Series, air_pressure: pandas.core.series.Series)[source]

Calculates specific humidity

Parameters:
  • vapor_pressure – A sub-daily timeseries of vapor pressure (kPa)
  • air_pressure – A sub-daily timeseries of air pressure (kPa)
Returns:

A sub-daily timeseries of specific humidity

Return type:

spec_humid

metsim.disaggregate.temp(df_daily: pandas.core.frame.DataFrame, df_disagg: pandas.core.frame.DataFrame, t_t_min: <built-in function array>, t_t_max: <built-in function array>, ts: float, t_begin: list = None, t_end: list = None)[source]

Disaggregate temperature using a Hermite polynomial interpolation scheme.

Parameters:
  • df_daily – A dataframe of daily values.
  • df_disagg – A dataframe of sub-daily values.
  • t_t_min – Times at which minimum daily temperatures are reached.
  • t_t_max – Times at which maximum daily temperatures are reached.
  • ts – Timestep for disaggregation
  • t_begin (list) – List of t_min and t_max for day previous to the start of df_daily. None indicates no extension of the record.
  • t_end (list) – List of t_min and t_max for day after the end of df_daily. None indicates no extension of the record.
Returns:

A sub-daily timeseries of temperature.

Return type:

temps

metsim.disaggregate.vapor_pressure(vp_daily: pandas.core.series.Series, temp: pandas.core.series.Series, t_t_min: <built-in function array>, n_out: int, ts: float)[source]

Calculate vapor pressure. First a linear interpolation of the daily values is calculated. Then this is compared to the saturated vapor pressure calculated using the disaggregated temperature. When the interpolated vapor pressure is greater than the calculated saturated vapor pressure, the interpolation is replaced with the saturation value.

Parameters:
  • vp_daily – Daily vapor pressure
  • temp – Sub-daily temperature
  • t_t_min – Timeseries of minimum daily temperature
  • n_out – Number of output observations
  • ts – Timestep to disaggregate down to
Returns:

A sub-daily timeseries of the vapor pressure

Return type:

vp

metsim.disaggregate.wind(wind: pandas.core.series.Series, ts: float)[source]

Wind is assumed constant throughout the day Note: this returns only through to the beginning of the

last day. Final values are filled in using a forward fill in the top level disaggregate function
Parameters:
  • wind – Daily timeseries of wind
  • ts – Timestep to disaggregate down to
Returns:

A sub-daily timeseries of wind

Return type:

wind