Skip to content

utils

aquacrop.utils.data

get_data(filename, **kwargs)

get selected data file

Source code in aquacrop/utils/data.py
25
26
27
28
29
30
31
def get_data(filename, **kwargs):
    """
    get selected data file
    """
    filepath = os.path.join(data.__path__[0], filename)

    return np.genfromtxt(filepath, **kwargs)

get_filepath(filename)

get selected data file

Source code in aquacrop/utils/data.py
16
17
18
19
20
21
22
def get_filepath(filename):
    """
    get selected data file
    """
    filepath = os.path.join(data.__path__[0], filename)

    return filepath

list_data()

lists all built-in data files

Source code in aquacrop/utils/data.py
 7
 8
 9
10
11
12
13
def list_data():
    """
    lists all built-in data files
    """
    path = data.__path__[0]

    return os.listdir(path)

aquacrop.utils.lars

prepare_lars_weather(file, year, generated=True, order=['year', 'jday', 'minTemp', 'maxTemp', 'precip', 'rad'], wind_speed=3.4)

Uses FAO-PM to calculate reference evapotranspiration for LARS generated and baseline input data.

Source code in aquacrop/utils/lars.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def prepare_lars_weather(
    file,
    year,
    generated=True,
    order=["year", "jday", "minTemp", "maxTemp", "precip", "rad"],
    wind_speed=3.4,
):
    """
    Uses FAO-PM to calculate reference evapotranspiration for LARS generated and baseline input data.

    """

    def vap_pres(t):
        return 0.6108 * np.exp((17.27 * t) / (t + 237.3))

    df = pd.read_csv(file, delim_whitespace=True, header=None)

    if generated:
        df.columns = order
        df["tdelta"] = pd.to_timedelta(df.jday, unit="D")
        df["date"] = pd.to_datetime(f"{year-1}/12/31") + df["tdelta"]

        psyc = 0.054  # sychometric constant
        tmean = (df.maxTemp + df.minTemp) / 2
        e_s = (vap_pres(df.maxTemp) + vap_pres(df.minTemp)) / 2
        e_a = vap_pres(df.minTemp)
        slope = 4098 * vap_pres(tmean) / (tmean + 237.3) ** 2
        R_ns = (1 - 0.23) * df.rad
        sb_const = 4.903e-9
        R_nl = (
            sb_const
            * 0.5
            * ((df.maxTemp + 273.15) ** 4 + (df.minTemp + 273.15) ** 4)
            * (0.34 - 0.14 * (e_a) ** 0.5)
            * (1.35 * 0.77 - 0.35)
        )
        Rn = R_ns - R_nl
        u2 = wind_speed

        eto = 0.408 * slope * Rn + (psyc * 900 * u2 * (e_s - e_a) / (tmean + 273)) / (
            slope + psyc * (1 + 0.34 * u2)
        )
        df["eto"] = eto

        # df["eto"] = df.rad*(0.0023)*(((df.maxTemp+df.minTemp)/2)+17.8)*(df.maxTemp-df.minTemp)**0.5
        df.eto = df.eto.clip(0.1)
        df = df[["simyear", "minTemp", "maxTemp", "precip", "eto", "date"]]
        df.columns = ["simyear", "MinTemp", "MaxTemp", "Precipitation", "ReferenceET", "Date"]

    else:
        df.columns = order
        df["date"] = pd.to_datetime(df.year, format="%Y") + pd.to_timedelta(df.jday - 1, unit="d")

        psyc = 0.054  # sychometric constant
        tmean = (df.maxTemp + df.minTemp) / 2
        e_s = (vap_pres(df.maxTemp) + vap_pres(df.minTemp)) / 2
        e_a = vap_pres(df.minTemp)
        slope = 4098 * vap_pres(tmean) / (tmean + 237.3) ** 2
        R_ns = (1 - 0.23) * df.rad
        sb_const = 4.903e-9
        R_nl = (
            sb_const
            * 0.5
            * ((df.maxTemp + 273.15) ** 4 + (df.minTemp + 273.15) ** 4)
            * (0.34 - 0.14 * (e_a) ** 0.5)
            * (1.35 * 0.77 - 0.35)
        )
        Rn = R_ns - R_nl
        u2 = wind_speed

        eto = 0.408 * slope * Rn + (psyc * 900 * u2 * (e_s - e_a) / (tmean + 273)) / (
            slope + psyc * (1 + 0.34 * u2)
        )
        df["eto"] = eto

        # df["eto"] = df.rad*(0.0023)*(((df.maxTemp+df.minTemp)/2)+17.8)*(df.maxTemp-df.minTemp)**0.5
        df.eto = df.eto.clip(0.1)
        df = df[["minTemp", "maxTemp", "precip", "eto", "date"]]
        df.columns = ["MinTemp", "MaxTemp", "Precipitation", "ReferenceET", "Date"]

    return df

aquacrop.utils.prepare_weather

prepare_weather(weather_file_path)

function to read in weather data and return a dataframe containing
the weather data

Arguments:

FileLocations): FileLocationsClass: input File Locations

weather_file_path): `str): file location of weather data

Returns:

weather_df (pandas.DataFrame): weather data for simulation period

Source code in aquacrop/utils/prepare_weather.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def prepare_weather(weather_file_path):
    """
    function to read in weather data and return a dataframe containing
    the weather data

    Arguments:\n

FileLocations): `FileLocationsClass`:  input File Locations

weather_file_path): `str):  file location of weather data



    Returns:

weather_df (pandas.DataFrame):  weather data for simulation period

    """

    weather_df = pd.read_csv(weather_file_path, header=0, delim_whitespace=True)

    assert len(weather_df.columns) == 7

    # rename the columns
    weather_df.columns = str(
        "Day Month Year MinTemp MaxTemp Precipitation ReferenceET").split()

    # put the weather dates into datetime format
    weather_df["Date"] = pd.to_datetime(weather_df[["Year", "Month", "Day"]])

    # drop the day month year columns
    weather_df = weather_df.drop(["Day", "Month", "Year"], axis=1)

    # set limit on ET0 to avoid divide by zero errors
    weather_df.ReferenceET.clip(lower=0.1, inplace=True)

    return weather_df