-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetNDICEA API.py
More file actions
485 lines (446 loc) · 15.1 KB
/
GetNDICEA API.py
File metadata and controls
485 lines (446 loc) · 15.1 KB
1
2
3
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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# import requests
#
# # API URL
# url = "https://ndiceaweb.eu/api/RequestToken"
#
# # 请求头
# headers = {
# "Content-Type": "application/json"
# }
#
# # 请求体
# data = {
# "UserName": "xinyu.yan@tus.ie", # 替换为您的用户名
# "Password": "kdjjK&4!O5oTuor6" # 替换为您的密码
# }
#
# try:
# # 发送 POST 请求
# response = requests.post(url, headers=headers, json=data)
#
# # 检查响应状态码
# if response.status_code == 200:
# # 打印返回的令牌
# token = response.json().get("token")
# print("Token:", token)
# else:
# print(f"Error: {response.status_code}")
# print("Response Content:", response.text)
#
# except Exception as e:
# print("An error occurred:", e)
import json
import requests
# ✅ Step 1: Prepare
url = "https://ndiceaweb.eu/api/RequestToken"
headers = {
"Content-Type": "application/json"
}
data = {
"UserName": "xinyu.yan@tus.ie", # replace with your actual username
"Password": "kdjjK&4!O5oTuor6" # replace with your actual password
}
# ✅ Step 2: Send POST to get token
response = requests.post(url, json=data, headers=headers)
# ✅ Step 3: Handle response
print("Status Code:", response.status_code)
print("Response Text:", response.text)
# Optional: try parsing JSON token
try:
token_data = response.json()
print("Parsed Token:", token_data)
# get the accessToken in token_data
access_token = token_data["AccessToken"]
print("Access Token:", access_token)
json.dump(token_data, open("token.json", "w"))
except:
print("Response is not valid JSON")
import requests
# Your token here
import requests
# 替换为你的真实 Token
# NDICEA API endpoint
# import requests
#
# # Replace with your actual Bearer token
#
# # 请求头
# # Headers
# headers = {
# "Authorization": f"Bearer {access_token}",
# "Accept": "application/json"
# }
#
# # ✅ 正确的 GET URL
# url = "https://ndiceaweb.eu/api/Scenario/GetAvailableRegions"
#
# # ✅ 发送 GET 请求(❗不要用 POST)
# response = requests.get(url, headers=headers)
#
# print("Status Code:", response.status_code)
# print("Response Text:", response.text)
#
# if response.status_code == 200:
# regions = response.json()
# for region in regions:
# print(f"ID: {region['Id']} | EN: {region['NameEn']}")
# else:
# print("❌ Failed with status:", response.status_code)
headers = {
"Authorization": f"Bearer {access_token.strip()}",
"Content-Type": "application/json",
"Accept": "application/json"
}
url = "https://ndiceaweb.eu/api/v6/RunModel"
# payload = {
# "Id": 1,
# "ScenName": "Valid NDICEA Scenario",
# "RegioId": 2,
# "startDate": "2024-01-01",
# "endDate": "2024-12-31",
# "Soil": {
# "EvaporationFactorBare": 0.25,
# "SomStart": 2.0,
# "Ph": 6.5,
# "MinWaterUptake": 0.75,
# "CNSoilLife": 6.5,
# "ADSoilLife": 0.4,
# "NFixThreshold": 15.0,
# "Tillage": 0,
# "NContentGroundwater": 15.0,
# "CapillaryRiseMax": 3.0,
# "Depth": 30.0,
# "SoilRootdepthMax": 70.0,
# "GwLevelDeepSummerDay": 196,
# "GwLevelHighWinterDay": 46,
# "GwLevelDeepSummer": 120.0,
# "GwLevelHighWinter": 80.0,
# "TopLayer": {
# "SoilSourceId": 6,
# "NLeachingFactor": 0.85,
# "DenitrificationFactor": 0.1,
# "DecayFactor": 0.86,
# "NmineralStart": 20.0,
# "SoilWaterSaturated": 0.5,
# "MoistureCurveFactor": 0.011,
# "CapillaryRiseDistance": 114.9,
# "CapillaryRiseReduction": 44.0,
# "InitialWaterPressure": 100.0,
# "WaterBypassFlowBench": 0.0,
# "WaterBypassFlowFactor": 0.0,
# "TextConventTillage": 0.0,
# "TextReducedTillage": 0.0,
# "TextNoTillage": 0.0,
# "CecScaler": 0.0,
# "BulkDensity": 1.35,
# "SOMOldQuant": 71600.0,
# "SOMYoungQuant": 6600.0,
# "SOMFreshQuant": 2800.0,
# "NContentOmYoung": 4.5,
# "NContentOmOld": 5.8,
# "NContentOmFresh": 3.0,
# "VirtualInitialAgeYoung": 4.0,
# "VirtualInitialAgeOld": 24.0,
# "VirtualInitialAgeFresh": 1.4
# },
# "SecondLayer": {
# "SoilSourceId": 6,
# "NLeachingFactor": 0.85,
# "DenitrificationFactor": 0.1,
# "DenitrificationThreshold": 5.0,
# "DecayFactor": 0.86,
# "NmineralStart": 20.0,
# "SoilWaterSaturated": 0.5,
# "MoistureCurveFactor": 0.011,
# "CapillaryRiseDistance": 114.9,
# "CapillaryRiseReduction": 44.0,
# "InitialWaterPressure": 100.0,
# "WaterBypassFlowBench": 0.0,
# "WaterBypassFlowFactor": 0.0,
# "TextConventTillage": 0.0,
# "TextReducedTillage": 0.0,
# "TextNoTillage": 0.0,
# "CecScaler": 0.0
# }
# },
# "Crops":
# {
# "CropId": 0,
# "ScenarioId": 0,
# "CultureName": "Potato organic",
# "DateSowing": "2022-04-19T00:00:00",
# "DateFullGrowth": "2022-06-18T00:00:00",
# "DateStartRipening": "2022-07-26T00:00:00",
# "DateHarvest": "2022-07-26T00:00:00",
# "Yield": 35000.0,
# "ProductIncorporated": 1.0,
# "ResidueIncorporated": 1.0,
# "RootIncorporated": 1.0,
# "DmContentProduct": 21.0,
# "NfixMax": 0.0,
# "TranspirationFactor": 1.1,
# "RootDepthGrowth": 0.07,
# "CropRootDepthMax": 80.0,
# "SeedQuant": 2000.0,
# "DmContentSeed": 23.0,
# "NContentDMSeed": 1.57,
# "P2O5ContentSeed": 0.59,
# "K2OContentSeed": 2.72,
# "TempMinimum": 0.0,
# "TempSumMinimum": 30.0,
# "GreenHarvest": False,
# "DmRatioProduct": 0.7,
# "DmRatioResidue": 0.25,
# "DmRatioRoot": 0.05,
# "NcontentDmProduct": 1.5,
# "NcontentDmResidue": 2.2,
# "NcontentDmRoot": 1.0,
# "P2O5ContentDmProduct": 0.59,
# "P2O5ContentDmResidue": 0.83,
# "P2O5ContentDmRoot": 0.0,
# "K2OContentProduct": 2.72,
# "K2OContentResidue": 3.3,
# "K2OContentRoot": 0.0,
# "InitialAgeProduct": 0.99,
# "InitialAgeResidue": 0.99,
# "InitialAgeRoot": 1.57,
# "RootStartDepth": 5.0,
# "GrassClover": 0,
# "GrasLandPeriodeType": 0,
# "RootDistribution": 0.03,
# "Tbase": 7.0,
# "DmPercResi": 10.0
# },
# "GreenManures": [],
# "Grasses": [],
# "Fertilizers": {
# "FertilizerId": 0,
# "ScenarioId": 0,
# "Name": "Rundvee drijfmest",
# "Date": "2022-05-01T00:00:00",
# "Quantity": 10000.0,
# "NContent": 4.1,
# "P2O5Content": 1.5,
# "K2OContent": 5.8,
# "DM": 90.0,
# "OM": 66.0,
# "NMineralContent": 2.6,
# "InitialAge": 1.95,
# "AmmoniumEmission": 3,
# "Soort": 0
# },
# "Irrigations": []
# }
payload = {
# "Id": 1,
"ScenName": "Demo with 1 crop and 1 fertilizer",
"RegioId": 2,
"Soil": {
"EvaporationFactorBare": 0.25,
"SomStart": 2.0,
"Ph": 7.0,
"MinWaterUptake": 0.75,
"CNSoilLife": 6.5,
"ADSoilLife": 0.4,
"NFixThreshold": 15.0,
"Tillage": 0,
"NContentGroundwater": 15.0,
"CapillaryRiseMax": 3.0,
"Depth": 30.0,
"SoilRootdepthMax": 70.0,
"GwLevelDeepSummerDay": 196,
"GwLevelHighWinterDay": 46,
"GwLevelDeepSummer": 120.0,
"GwLevelHighWinter": 80.0,
"TopLayer": {
"SoilSourceId": 6,
"NLeachingFactor": 0.85,
"DenitrificationFactor": 0.1,
"DecayFactor": 0.86,
"NmineralStart": 20.0,
"SoilWaterSaturated": 0.47,
"MoistureCurveFactor": 0.011,
"CapillaryRiseDistance": 114.9,
"CapillaryRiseReduction": 44.0,
"InitialWaterPressure": 100.0,
"WaterBypassFlowBench": 0.0,
"WaterBypassFlowFactor": 0.0,
"TextConventTillage": 0.0,
"TextReducedTillage": 0.0,
"TextNoTillage": 0.0,
"CecScaler": 0.0,
"BulkDensity": 1.35,
"SOMOldQuant": 71600.0,
"SOMYoungQuant": 6600.0,
"SOMFreshQuant": 2800.0,
"NContentOmYoung": 4.5,
"NContentOmOld": 5.8,
"NContentOmFresh": 3.0,
"VirtualInitialAgeYoung": 4.0,
"VirtualInitialAgeOld": 24.0,
"VirtualInitialAgeFresh": 1.4
},
"SecondLayer": {
"SoilSourceId": 6,
"NLeachingFactor": 0.85,
"DenitrificationFactor": 0.1,
"DenitrificationThreshold": 5.0,
"DecayFactor": 0.86,
"NmineralStart": 20.0,
"SoilWaterSaturated": 0.47,
"MoistureCurveFactor": 0.011,
"CapillaryRiseDistance": 114.9,
"CapillaryRiseReduction": 44.0,
"InitialWaterPressure": 100.0,
"WaterBypassFlowBench": 0.0,
"WaterBypassFlowFactor": 0.0,
"TextConventTillage": 0.0,
"TextReducedTillage": 0.0,
"TextNoTillage": 0.0,
"CecScaler": 0.0
}
},
"Crops": [
{
"CropId": 0,
"ScenarioId": 0,
"CultureName": "Potato organic",
"DateSowing": "2022-04-19T00:00:00",
"DateFullGrowth": "2022-06-18T00:00:00",
"DateStartRipening": "2022-07-26T00:00:00",
"DateHarvest": "2022-07-26T00:00:00",
"Yield": 35000.0,
"ProductIncorporated": 1.0,
"ResidueIncorporated": 1.0,
"RootIncorporated": 1.0,
"DmContentProduct": 21.0,
"NfixMax": 0.0,
"TranspirationFactor": 1.1,
"RootDepthGrowth": 0.07,
"CropRootDepthMax": 80.0,
"SeedQuant": 2000.0,
"DmContentSeed": 23.0,
"NContentDMSeed": 1.57,
"P2O5ContentSeed": 0.59,
"K2OContentSeed": 2.72,
"TempMinimum": 0.0,
"TempSumMinimum": 30.0,
"GreenHarvest": False,
"DmRatioProduct": 0.7,
"DmRatioResidue": 0.25,
"DmRatioRoot": 0.05,
"NcontentDmProduct": 1.5,
"NcontentDmResidue": 2.2,
"NcontentDmRoot": 1.0,
"P2O5ContentDmProduct": 0.59,
"P2O5ContentDmResidue": 0.83,
"P2O5ContentDmRoot": 0.0,
"K2OContentProduct": 2.72,
"K2OContentResidue": 3.3,
"K2OContentRoot": 0.0,
"InitialAgeProduct": 0.99,
"InitialAgeResidue": 0.99,
"InitialAgeRoot": 1.57,
"RootStartDepth": 5.0,
"GrassClover": 0,
"GrasLandPeriodeType": 0,
"RootDistribution": 0.03,
"Tbase": 7.0,
"DmPercResi": 10.0
}
],
"GreenManures": [],
"Grasses": [],
"Fertilizers": [
{
"FertilizerId": 0,
"ScenarioId": 0,
"Name": "Rundvee drijfmest",
"Date": "2022-05-01T00:00:00",
"Quantity": 10000.0,
"NContent": 4.1,
"P2O5Content": 1.5,
"K2OContent": 5.8,
"DM": 90.0,
"OM": 66.0,
"NMineralContent": 2.6,
"InitialAge": 1.95,
"AmmoniumEmission": 3,
"Soort": 0
}
],
"Irrigations": []
}
response = requests.post(url, headers=headers, json=payload)
print("Status Code:", response.status_code)
print("Response Text:", response.text)
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
import io
import json
# Assuming the user posted a raw JSON response, we parse it
# response_text = '''{"ScenId":1,"ScenName":"Valid NDICEA Scenario","Message":"Success","Results":[{"Date":"2025-01-01T01:00:00+01:00","Rain":14.8,"Irrigation":0.0,"NminTop":20.0,"NminSecond":20.0,"CumNleaching":0.0,"OrgMatterTop":2.0,"PfTop":2.0},{"Date":"2025-01-02T01:00:00+01:00","Rain":0.7,"Irrigation":0.0,"NminTop":18.7,"NminSecond":21.6,"CumNleaching":-0.1,"OrgMatterTop":2.0,"PfTop":1.87}]}''' # Shortened for demonstration
# Load the JSON
data = json.loads(response.text)
# Convert results to a DataFrame
df = pd.DataFrame(data["Results"])
# Convert the Date column to datetime
df["Date"] = pd.to_datetime(df["Date"])
# Plot example variables
plt.figure(figsize=(12, 6))
plt.plot(df["Date"], df["NminTop"], label="NminTop")
plt.plot(df["Date"], df["NminSecond"], label="NminSecond")
plt.plot(df["Date"], df["CumNleaching"], label="CumNleaching")
plt.xlabel("Date")
plt.ylabel("Values")
plt.title("NDICEA Scenario Data Over Time")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.xticks(rotation=45)
plt.show()
import pandas as pd
import json
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# Load the full JSON content (manually truncated here for Platform html demo purposes)
# The user already provided a very long JSON string; in practice, this would be parsed from response.
# For demonstration, here's how you would parse the JSON response text:
# json_data = json.loads(response_text)
# Since response_text is not directly passed, here's an example structure:
# example_data = {
# "Results": [
# {"Date": "2025-01-01T01:00:00+01:00", "Rain": 14.8, "NminTop": 20.0, "CumNleaching": 0.0},
# {"Date": "2025-01-02T01:00:00+01:00", "Rain": 0.7, "NminTop": 18.7, "CumNleaching": -0.1},
# {"Date": "2025-01-03T01:00:00+01:00", "Rain": 3.2, "NminTop": 18.8, "CumNleaching": 0.0},
# # Add more entries here...
# ]
# }
# Create DataFrame
df = pd.DataFrame(data["Results"])
# Convert 'Date' column to datetime
df["Date"] = pd.to_datetime(df["Date"])
# Plotting
fig, ax1 = plt.subplots(figsize=(12, 6))
ax1.plot(df["Date"], df["Rain"], label="Rain (mm)", marker='o')
ax1.set_xlabel("Date")
ax1.set_ylabel("Rain (mm)", color='blue')
ax1.tick_params(axis='y', labelcolor='blue')
# Add secondary axis for NminTop and CumNleaching
ax2 = ax1.twinx()
ax2.plot(df["Date"], df["NminTop"], label="NminTop (mg/kg)", color='green', marker='s')
ax2.plot(df["Date"], df["CumNleaching"], label="Cumulative N Leaching (kg/ha)", color='red', marker='^')
ax2.set_ylabel("NminTop / Cumulative N Leaching", color='black')
ax2.tick_params(axis='y', labelcolor='black')
# Date formatting
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
fig.autofmt_xdate()
# Legends
lines_1, labels_1 = ax1.get_legend_handles_labels()
lines_2, labels_2 = ax2.get_legend_handles_labels()
ax1.legend(lines_1 + lines_2, labels_1 + labels_2, loc='upper left')
plt.title("Rain, NminTop, and Cumulative N Leaching over Time")
plt.tight_layout()
plt.show()