Worked Examples of connecting to, and receiving data back from the AHDB RB209 Web API Application

 

These examples all contain four sections:
• The first contains details on the scenario that the example is modeling.
• The second is how to structure the data input for collating data together in the required format to match the scenario. Note that all classes within the input structure need to be provided even if there is no data to provide within them.
• The third is example code in C# for calling the main endpoint of the API with the input data structure attached.
• While the last is the data output returned from the call to the API. Note that these have been condensed for brevity just to show the relevant sections for the example.

 

Example 1 - Arable

• The crop to be grown is winter barley for feed.
• Grown on a sandy loam soil over clay (medium).
• Average rainfall of 650 mm (Table 4.3, SNS = 1).
• Previous crop was winter wheat.
• Expected yield of 8 t/ha (+1.5 t/ha, +30 kg N/ha).

• Table 4.16: 170 kg N/ha.
• + 30 kg N/ha = 200 kg N/ha.

Data Input for Example 1

{
  "Field": {
    "FieldType": 1,
    "MultipleCrops": false,
    "Arable": [
      {
        "CropGroupID": 0,
        "CropTypeID": 1,
        "CropInfo1ID": 1,
        "CropInfo2ID": 1,
        "SowingDate": "2016-09-01T00:00:00",
        "ExpectedYield": 8.0
      }
    ],
    "Grassland": {}
    "Soil": {
      "SoilTypeID": 2,
      "NVZActionProgrammeID": 1,
      "SoilAnalyses": []
    },
    "HarvestYear": 2017,
    "Area": 1.0,
    "Postcode": "NP19 8HW",
    "Altitude": 0,
    "SiteClassID": 3,
    "RainfallAverage": 650.0,
    "ExcessWinterRainfall": 0.0,
    "OrganicMaterials": [],
    "PreviousCropping": {
      "PreviousGrassID": 1,
      "PreviousCropGroupID": 0,
      "PreviousCropTypeID": 0
    }
  },
  "Nutrients": {
    "Nitrogen": true,
    "Phosphate": true,
    "Potash": true,
    "Magnesium": true,
    "Sodium": true,
    "Sulphur": true,
    "Lime": true
  },
  "Totals": true
}

C# code to connect to API

//API Connection Details
var UserName = "...";        //Your User Name
var LicenceKey = "...";      //Your Licence Key
var ApiConnection = "https://rb209-api-v1.ahdb.org.uk";
 
//Serialize input details
var content = new StringContent(JsonConvert.SerializeObject(DataInput), Encoding.UTF8, "application/json");
//Add Authentication details
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", UserName, LicenceKey))));
//Connect to API & retrieve data
var url = string.Format("{0}/api/main/recommendations", ApiConnection);
var task = client.GetAsync(url);
var ReturnedData = task.Result.Content.ReadAsStringAsync().Result;
//Deserialize output details
var DataOutputDetails = JsonConvert.DeserializeObject<dataoutput>(ReturnedData);

Data Output for Example 1

{
  "Recommendations": [
    {
      "NutrientID": 0,
      "Type": "A",
      "SequenceID": 1,
      "DefoliationID": 1,
      "IndexpH": "1",
      "CropNeedValue": 170.00,
      "Breakdown": false,
      "Description": "Nitrogen Recommendation (kg/ha) based on crop group, crop type, crop info 1, soil type and soil nitrogen index."
    },
    {
      "NutrientID": 0,
      "Type": "A",
      "SequenceID": 1,
      "DefoliationID": 1,
      "IndexpH": "0",
      "CropNeedValue": 30.00,
      "Breakdown": false,
      "Description": "Nitrogen Yield Adjustment for Winter Barley Feed"
    },
    {
      "NutrientID": 0,
      "Type": "A",
      "SequenceID": 1,
      "DefoliationID": 1,
      "IndexpH": "0",
      "CropNeedValue": 200.00,
      "Breakdown": true,
      "Description": "Total"
    },
    /* Data for other nutrients is returned, but contents not shown for brevity */
  ],
  "NutrientsSupplied":[],
  "AdviceNotes": [
    {
      "NutrientID": 0,
      "Note": "Apply all the N as a single dressing by early stem extension but not before early April."
    },
    {
      "NutrientID": 0,
      "Note": "Apply up to 40 kg N/ha in mid Feb-early March and the balance by early stem extension but not before late March."
    },
    {
      "NutrientID": 0,
      "Note": "Reduce the N rate by 25 kg/ha if the lodging risk is high."
    },
    {
      "NutrientID": 0,
      "Note": "Yields above 6.5 t/ha, could increase rate by10 kg N/ha for each 0.5 t/ha additional yield, up to a maximum of 11 t/ha and reduce accordingly, if below average."
    },
    /* Data for other nutrients is returned, but contents not shown for brevity */
  ],
  "VersionNumber": "1.0.6396.25975"
}

Example 2 - Grassland

• The crop is first cut of silage plus grazing
• Moderate SNS
• Good GGC
• Target yield: 10t DM/ha

Nitrogen for first cut silage:
• Table 3.8: Nitrogen recommendations for grass silage
• Target annual DM yield: 9-12 t/ha
• N application rate = 100 kg N/ha

Nitrogen for grazed sward:
• Table 3.9: Nitrogen recommendations for grazed swards
• Indicative DM yield: 9-12 t/ha
• N application rate = 30 kg N/ha (June), 30 kg N/ha (July), 30 kg N/ha (August)

Total N recommendation = 190 kg N/ha

Data Input for Example 2

{
  "Field": {
    "FieldType": 2,
    "MultipleCrops": false,
    "Arable": [],
    "Grassland": {
      "SNSID": 2,
      "GrassGrowthClassID": 4,
      "YieldTypeID": 1,
      "SequenceID": 68,
      "GrasslandSequence" :[
        {
          "Position": 1,
          "CropMaterialID": 3,
          "Yield": 6.0
        },
        {
          "Position": 2,
          "CropMaterialID": 1,
          "Yield": 1.8
        },
        {
          "Position": 3,
          "CropMaterialID": 1,
          "Yield": 1.8
        },
        {
          "Position":4,
          "CropMaterialID": 1,
          "Yield": 1.8
        }],
      "EstablishedDate": "2016-01-01T00:00:00",
      "SeasonID": 0
    },
    "Soil": {
      "SoilTypeID": 2,
      "KReleasingClay": null,
      "NVZActionProgrammeID": 1,
      "SoilAnalyses": []
    },
    "HarvestYear": 2016,
    "Area": 150.00,
    "Postcode": "AB12 3CD",
    "Altitude": 75,
    "SiteClassID": 1,
    "RainfallAverage": 70.00,
    "ExcessWinterRainfall": 0.00,
    "OrganicMaterials": [],
    "PreviousCropping": {
      "PreviousGrassID": 1,
      "PreviousCropGroupID": 0,
      "PreviousCropTypeID": 1,
      "SNSID": null
    }
  },
  "Nutrients": {
    "Nitrogen": true,
    "Phosphate": true,
    "Potash": true,
    "Magnesium": true,
    "Sodium": true,
    "Sulphur": true,
    "Lime": true
  },
  "Totals": true
}

C# code to connect to API

//API Connection Details
var UserName = "...";        //Your User Name
var LicenceKey = "...";      //Your Licence Key
var ApiConnection = "https://rb209-api-v1.ahdb.org.uk";
 
//Serialize input details
var content = new StringContent(JsonConvert.SerializeObject(DataInput), Encoding.UTF8, "application/json");
//Add Authentication details
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", UserName, LicenceKey))));
//Connect to API & retrieve data
var url = string.Format("{0}/api/main/recommendations", ApiConnection);
var task = client.GetAsync(url);
var ReturnedData = task.Result.Content.ReadAsStringAsync().Result;
//Deserialize output details
var DataOutputDetails = JsonConvert.DeserializeObject<dataoutput>(ReturnedData);

Data Output for Example 2

{
  "Recommendations": [
    {
      "NutrientID": 0,
      "Type": "G",
      "SequenceID": 1,
      "DefoliationID": 1,
      "IndexpH": "Moderate",
      "CropNeedValue": 100.0,
      "Breakdown": true,
      "Description": "Total"
    },
    {
      "NutrientID": 0,
      "Type": "G",
      "SequenceID": 2,
      "DefoliationID": 2,
      "IndexpH": "Moderate",
      "CropNeedValue": 30.0,
      "Breakdown": true,
      "Description": "Total"
    },
    {
      "NutrientID": 0,
      "Type": "G",
      "SequenceID": 3,
      "DefoliationID": 3,
      "IndexpH": "Moderate",
      "CropNeedValue": 30.0,
      "Breakdown": true,
      "Description": "Total"
    },
    {
      "NutrientID": 0,
      "Type": "G",
      "SequenceID": 4,
      "DefoliationID": 4,
      "IndexpH": "Moderate",
      "CropNeedValue": 30.0,
      "Breakdown": true,
      "Description": "Total"
    },
    /* Data for other nutrients is returned, but contents not shown for brevity */
  ],
  "NutrientsSupplied":[],
  "AdviceNotes": [
    {
      "NutrientID": 0,
      "Note": "Grazing: If previous growth has been severely restricted by drought, reduce or omit this application."
    },
    /* Data for other nutrients is returned, but contents not shown for brevity */
  ],
  "VersionNumber":"1.0.6459.26132"
}

Example 3 - Organic Materials

• 6% DM Cattle Slurry applied at 30 m3/ha.

Values returned are (numbers in brackets are RB209 values unrounded):
• 80 (78) kg N/ha - Total
• 35 (36) kg P2O5/ha - Total
• 20 (18) kg P2O5/ha - Available
• 75 (75) kg K2O/ha - Total
• 70 (69) kg K2O/ha - Available
• 20 (18) kg MgO/ha - Total
• 20 (21) kg SO3/ha - Total

If optional inputs are provided, then the availability of N and SO3 will also be returned. In this example, the application was made during the autumn, and on a medium soil:
• 20 (19.5) kg N/ha - Available
• 0 (1.6) kg SO3/ha - Available

Data Input for Example 3

{
  "Field": {
    "FieldType": 1,
    "MultipleCrops": false,
    "Arable": [
      {
        "CropGroupID": 0,
        "CropTypeID": 0,
        "CropInfo1ID": 1,
        "CropInfo2ID": 1,
        "SowingDate": "2016-09-01T00:00:00",
        "ExpectedYield": 8.0
      }
    ],
    "Grassland": {},
    "Soil": {
      "SoilTypeID": 2,
      "NVZActionProgrammeID": 1,
      "SoilAnalyses": []
    },
    "HarvestYear": 2017,
    "Area": 1.0,
    "Postcode": "AB12 3CD",
    "Altitude": 0,
    "SiteClassID": 3,
    "RainfallAverage": 0.0,
    "ExcessWinterRainfall": 0.0,
    "OrganicMaterials": [
      {
        "ApplicationDate": "2016-10-18T00:00:00",
        "ApplicationRate": 30.0,
        "IncorporationMethodID": 5,
        "MaterialID": 18
      }
    ],
    "PreviousCropping": {
      "PreviousGrassID": 1,
      "PreviousCropGroupID": 0,
      "PreviousCropTypeID": 0
    }
  },
  "Nutrients": {
    "Nitrogen": true,
    "Phosphate": true,
    "Potash": true,
    "Magnesium": true,
    "Sodium": true,
    "Sulphur": true,
    "Lime": true
  },
  "Totals": true
}

C# code to connect to API

//API Connection Details
var UserName = "...";        //Your User Name
var LicenceKey = "...";      //Your Licence Key
var ApiConnection = "https://rb209-api-v1.ahdb.org.uk";
 
//Serialize input details
var content = new StringContent(JsonConvert.SerializeObject(DataInput), Encoding.UTF8, "application/json");
//Add Authentication details
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", UserName, LicenceKey))));
//Connect to API & retrieve data
var url = string.Format("{0}/api/main/recommendations", ApiConnection);
var task = client.GetAsync(url);
var ReturnedData = task.Result.Content.ReadAsStringAsync().Result;
//Deserialize output details
var DataOutputDetails = JsonConvert.DeserializeObject<dataoutput>(ReturnedData);

Data Output for Example 3

    {
  "Recommendations": [
    /* Data is returned, but contents not shown for brevity */
  ],
  "NutrientsSupplied":[
    {
      "MaterialID": 18,
      "ApplicationDate": "2016-10-18T00:00:00",
      "NutrientID": 0,
      "Total": 80.0,
      "Available": 20.0
    },
    {
      "MaterialID": 18,
      "ApplicationDate": "2016-10-18T00:00:00",
      "NutrientID": 1,
      "Total": 35.0,
      "Available": 20.0
    },
    {
      "MaterialID": 18,
      "ApplicationDate": "2016-10-18T00:00:00",
      "NutrientID": 2,
      "Total": 75.0,
      "Available": 70.0
    },
    {
      "MaterialID": 18,
      "ApplicationDate": "2016-10-18T00:00:00",
      "NutrientID": 3,
      "Total": 20.0,
      "Available": null
    },
    {
      "MaterialID": 18,
      "ApplicationDate": "2016-10-18T00:00:00",
      "NutrientID": 5,
      "Total": 20.0,
      "Available": 0.0
    }
  ],
  "AdviceNotes": [
    /* Data is returned, but contents not shown for brevity */
  ],
  "VersionNumber": "1.0.6396.25975"
}

Example 4 - Lab Analysis

• 30 m3/ha DM Cattle Slurry applied in the spring before the first silage cut.

A lab analysis is carried out on a sample of slurry, the returned values are:
• 2.0 kg N/ha - Total
• 1.1 kg P2O5/ha - Total
• 3.4 kg K2O/ha - Total
• 0.7 kg SO3/ha - Total
• 0.6 kg MgO/ha - Total

The API will use these input values instead of the default values associated to the organic material.
Values returned using the lab analysis inputs are (numbers in brackets are RB209 values unrounded):
• 25 (24.0) kg N/ha - Available
• 15 (16.5) kg P2O5/ha - Available
• 90 (91.8) kg K2O/ha - Available
• 5 (7.35) kg SO3/ha - Available

Data Input for Example 4

{
  "Field": {
    "FieldType": 1,
    "MultipleCrops": false,
    "Arable": [
      {
        "CropGroupID": 0,
        "CropTypeID": 0,
        "CropInfo1ID": 1,
        "CropInfo2ID": 1,
        "SowingDate": "2016-09-01T00:00:00",
        "ExpectedYield": 8.0
      }
    ],
    "Grassland": {},
    "Soil": {
      "SoilTypeID": 2,
      "NVZActionProgrammeID": 1,
      "SoilAnalyses": []
    },
    "HarvestYear": 2017,
    "Area": 1.0,
    "Postcode": "AB12 3CD",
    "Altitude": 0,
    "SiteClassID": 3,
    "RainfallAverage": 0.0,
    "ExcessWinterRainfall": 0.0,
    "OrganicMaterials": [
      {
        "ApplicationDate": "2017-03-09T00:00:00",
        "ApplicationRate": 30.0,
        "IncorporationMethodID": 1,
        "MaterialID": 18,
        "Nitrogen" : 2.0,
        "Phosphate" : 1.1,
        "Potash" : 3.4,
        "Sulphur" : 0.7,
        "Magnesium" : 0.6
      }
    ],
    "PreviousCropping": {
      "PreviousGrassID": 1,
      "PreviousCropGroupID": 0,
      "PreviousCropTypeID": 0
    }
  },
  "Nutrients": {
    "Nitrogen": true,
    "Phosphate": true,
    "Potash": true,
    "Magnesium": true,
    "Sodium": true,
    "Sulphur": true,
    "Lime": true
  },
  "Totals": true
}

C# code to connect to API

//API Connection Details
var UserName = "...";        //Your User Name
var LicenceKey = "...";      //Your Licence Key
var ApiConnection = "https://rb209-api-v1.ahdb.org.uk";
 
//Serialize input details
var content = new StringContent(JsonConvert.SerializeObject(DataInput), Encoding.UTF8, "application/json");
//Add Authentication details
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", UserName, LicenceKey))));
//Connect to API & retrieve data
var url = string.Format("{0}/api/main/recommendations", ApiConnection);
var task = client.GetAsync(url);
var ReturnedData = task.Result.Content.ReadAsStringAsync().Result;
//Deserialize output details
var DataOutputDetails = JsonConvert.DeserializeObject<dataoutput>(ReturnedData);

Data Output for Example 4

    {
  "Recommendations": [
    /* Data is returned, but contents not shown for brevity */
  ],
  "NutrientsSupplied":[
    {
      "MaterialID": 18,
      "ApplicationDate": "2017-03-09T00:00:00",
      "NutrientID": 0,
      "Total": 60.0,
      "Available": 25.0
    },
    {
      "MaterialID": 18,    
      "ApplicationDate": "2017-03-09T00:00:00",
      "NutrientID": 1,
      "Total": 35.0,
      "Available": 15.0
    },
    {
      "MaterialID": 18,    
      "ApplicationDate": "2017-03-09T00:00:00",
      "NutrientID": 2,
      "Total": 100.0,
      "Available": 90.0
    },
    {
      "MaterialID": 18,    
      "ApplicationDate": "2017-03-09T00:00:00",
      "NutrientID": 3,
      "Total": 20.0,
      "Available": null
    },
    {
      "MaterialID": 18,    
      "ApplicationDate": "2017-03-09T00:00:00",
      "NutrientID": 5,
      "Total": 20.0,
      "Available": 5.0
    }
  ],
  "AdviceNotes": [
    /* Data is returned, but contents not shown for brevity */
  ],
  "VersionNumber": "1.0.6396.25975"
}

Example 5 - Measurement Method

The measurement method is used to calculate the Soil Nitrogen Supply value for a field based on measurements from the field. This can be used in place of the default field assessment method for calculating the Soil Nitrogen Supply value.

• An arable crop is being grown, and measurements are taken in the spring.
• The Soil Mineral Nitrogen is kg N/ha at depth 0cm to 30cm.
• The Soil Mineral Nitrogen is kg N/ha at depth 30cm to 60cm.
• The Soil Mineral Nitrogen is kg N/ha at depth 60cm to 90cm. Note that a measurement at this depth is only required in the spring.
• The crop height is measured at cm.
• With an organic matter percentage value of %. Note that as a guide, where measurement is not done, for every 1% organic matter above 4%, a topsoil may release an additional 10 kg N/ha.

• This returns an SNS value of kg N/ha.
• With an SNS Index of .

To use the returned value in place of the default field assessment method, the output SnsIndex value from this calculation should be provided to the main endpoint (recommendations). This should be as the SNSIndexID parameter within the soilanalysis section.

Please note, that in this interactive example, for Step2, only crop height is available to change the provided input value. When connecting to the endpoint through code, all inputs are available to provide values for.

Data Input for Example 5

{
  "CropGroupId": 1,
  "SeasonId": 2,
  "Step1ArablePotato": {
    "Depth0To30Cm": ,
    "Depth30To60Cm": ,
    "Depth60To90Cm": 
  },
  "Step1Veg": null,
  "Step2": {    
    "CropHeight": ,
    "GreenAreaIndex": null,
    "ShootNumber": null
  },
  "Step3": {
    "Adjustment": null,
    "OrganicMatterPercentage": 
  }
}

C# code to connect to API

//API Connection Details
var UserName = "...";        //Your User Name
var LicenceKey = "...";      //Your Licence Key
var ApiConnection = "https://rb209-api-v1.ahdb.org.uk";
 
//Serialize input details
var content = new StringContent(JsonConvert.SerializeObject(DataInput), Encoding.UTF8, "application/json");
//Add Authentication details
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", UserName, LicenceKey))));
//Connect to API & retrieve data
var url = string.Format("{0}/api/measurement/measurementmethod", ApiConnection);
var task = client.GetAsync(url);
var ReturnedData = task.Result.Content.ReadAsStringAsync().Result;
//Deserialize output details
var DataOutputDetails = JsonConvert.DeserializeObject<measurementmethodoutput>(ReturnedData);

Data Output for Example 5

{
  "SnsValue": ,
  "SnsIndex": 
}