pyemu.utils.smp_utils

PEST-style site sample (smp) file support utilities

Module Contents

Functions

smp_to_ins(smp_filename[, ins_filename, ...])

create an instruction file for an smp file

dataframe_to_smp(dataframe, smp_filename[, name_col, ...])

write a dataframe as an smp file

_date_parser(items)

datetime parser to help load smp files

smp_to_dataframe(smp_filename[, datetime_format])

load an smp file into a pandas dataframe

pyemu.utils.smp_utils.smp_to_ins(smp_filename, ins_filename=None, use_generic_names=False, gwutils_compliant=False, datetime_format=None, prefix='')

create an instruction file for an smp file

Parameters:
  • smp_filename (str) – path and name of an existing smp file

  • ins_filename (str, optional) – the name of the instruction file to create. If None, smp_filename +”.ins” is used. Default is None.

  • use_generic_names (bool) – flag to force observations names to use a generic int counter instead of trying to use a datetime string. Default is False

  • gwutils_compliant (bool) – flag to use instruction set that is compliant with the PEST gw utils (fixed format instructions). If false, use free format (with whitespace) instruction set. Default is False

  • datetime_format (str) – string to pass to datetime.strptime in the smp_utils.smp_to_dataframe() function. If None, not used. Default is None.

  • prefix (str) – a prefix to add to the front of the derived observation names. Default is ‘’

Returns:

a dataframe of the smp file information with the observation names and instruction lines as additional columns.

Return type:

pandas.DataFrame

Example:

df = pyemu.smp_utils.smp_to_ins("my.smp")
pyemu.utils.smp_utils.dataframe_to_smp(dataframe, smp_filename, name_col='name', datetime_col='datetime', value_col='value', datetime_format='dd/mm/yyyy', value_format='{0:15.6E}', max_name_len=12)

write a dataframe as an smp file

Parameters:
  • dataframe (pandas.DataFrame) – the dataframe to write to an SMP file. This dataframe should be in “long” form - columns for site name, datetime, and value.

  • smp_filename (str) – smp file to write

  • name_col (str,optional) – the name of the dataframe column that contains the site name. Default is “name”

  • datetime_col (str) – the column in the dataframe that the datetime values. Default is “datetime”.

  • value_col (str) – the column in the dataframe that is the values

  • datetime_format (str, optional) – The format to write the datetimes in the smp file. Can be either ‘dd/mm/yyyy’ or ‘mm/dd/yyy’. Default is ‘dd/mm/yyyy’.

  • value_format (str, optional) – a python float-compatible format. Default is “{0:15.6E}”.

Example:

pyemu.smp_utils.dataframe_to_smp(df,"my.smp")
pyemu.utils.smp_utils._date_parser(items)

datetime parser to help load smp files

pyemu.utils.smp_utils.smp_to_dataframe(smp_filename, datetime_format=None)

load an smp file into a pandas dataframe

Parameters:
  • smp_filename (str) – path and nane of existing smp filename to load

  • datetime_format (str, optional) – The format of the datetime strings in the smp file. Can be either “%m/%d/%Y %H:%M:%S” or “%d/%m/%Y %H:%M:%S” If None, then we will try to deduce the format for you, which always dangerous.

Returns:

a dataframe with index of datetime and columns of site names. Missing values are set to NaN.

Return type:

pandas.DataFrame

Example:

df = smp_to_dataframe("my.smp")