Navanihk commited on
Commit
4e5fef2
·
1 Parent(s): 84b596b
Files changed (1) hide show
  1. RoofTopCalculation.py +47 -55
RoofTopCalculation.py CHANGED
@@ -103,6 +103,25 @@ def rainFallPrediction(location,RainFallData):
103
  }
104
 
105
  return default_rainfall.get(location_lower, 750) # Default to 750mm if location not found
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  def get_groundwater_level(lat, lon, date):
107
  """
108
  Fetch groundwater level from India WRIS API for a given lat, lon, and date.
@@ -112,41 +131,20 @@ def get_groundwater_level(lat, lon, date):
112
  # Here, we use Erode, Tamil Nadu for demonstration
113
  import requests
114
  url = (
115
- "https://indiawris.gov.in/Dataset/Ground%20Water%20Level"
116
- "?stateName=Tamil%20Nadu&districtName=Erode&agencyName=CGWB"
117
- f"&startdate={date}&enddate={date}&download=false&page=0&size=1"
118
  )
119
- headers = {
120
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
121
- }
122
  try:
123
- resp = requests.get(url, timeout=5, headers=headers) # Reduced timeout to 5 seconds
124
- print(f"[DEBUG] Groundwater API status: {resp.status_code}")
125
- if resp.status_code == 200:
126
- try:
127
- data = resp.json()
128
- print(f"[DEBUG] Groundwater API response: {data}")
129
- # Parse groundwater level from response (update as per actual API response)
130
- if data and isinstance(data, list) and len(data) > 0:
131
- gw_level = data[0].get('groundWaterLevel', 15)
132
- return float(gw_level)
133
- else:
134
- print("[WARN] No groundwater data found in response.")
135
- except Exception as json_err:
136
- print(f"[ERROR] Failed to parse JSON: {json_err}\nResponse text: {resp.text}")
137
- else:
138
- print(f"[ERROR] Groundwater API returned status {resp.status_code}: {resp.text}")
139
- except requests.exceptions.ConnectTimeout:
140
- print(f"[ERROR] Connection timeout when fetching groundwater level")
141
- except requests.exceptions.ReadTimeout:
142
- print(f"[ERROR] Read timeout when fetching groundwater level")
143
- except requests.exceptions.ConnectionError:
144
- print(f"[ERROR] Connection error when fetching groundwater level")
145
  except Exception as e:
146
- print(f"[ERROR] Exception fetching groundwater level: {e}")
147
-
148
- # Return a reasonable default based on Indian groundwater levels
149
- return 15 # fallback default (meters below ground level)
150
 
151
  def get_soil_type(lat, lon):
152
  """
@@ -155,23 +153,16 @@ def get_soil_type(lat, lon):
155
  import requests
156
  url = f"https://api.openepi.io/soil/type?lat={lat}&lon={lon}&top_k=3"
157
  try:
158
- resp = requests.get(url, timeout=5) # Reduced timeout to 5 seconds
159
  if resp.status_code == 200:
160
  data = resp.json()
161
  # Parse soil type from response (update as per actual API response)
162
- if data and 'soil_types' in data and len(data['soil_types']) > 0:
163
- return data['soil_types'][0].get('name', 'Loamy')
164
- except requests.exceptions.ConnectTimeout:
165
- print(f"[ERROR] Connection timeout when fetching soil type")
166
- except requests.exceptions.ReadTimeout:
167
- print(f"[ERROR] Read timeout when fetching soil type")
168
- except requests.exceptions.ConnectionError:
169
- print(f"[ERROR] Connection error when fetching soil type")
170
  except Exception as e:
171
  print(f"Error fetching soil type: {e}")
172
  return "Loamy" # fallback default
173
 
174
-
175
  def RooftTopCalculation(input_data,RainFallData):
176
  """
177
  Calculate rainwater harvesting parameters based on input data
@@ -280,18 +271,19 @@ def RooftTopCalculation(input_data,RainFallData):
280
  def get_coordinates(location):
281
  """Get coordinates for a location (simplified - would use geocoding service in production)"""
282
  location_coordinates = {
283
- "karur": [10.9601, 78.0766],
284
- "new delhi": [28.6139, 77.2090],
285
- "delhi": [28.6139, 77.2090],
286
- "mumbai": [19.0760, 72.8777],
287
- "bangalore": [12.9716, 77.5946],
288
- "chennai": [13.0827, 80.2707],
289
- "hyderabad": [17.3850, 78.4867],
290
- "pune": [18.5204, 73.8567],
291
- "kolkata": [22.5726, 88.3639],
292
- "ahmedabad": [23.0225, 72.5714],
293
- "jaipur": [26.9124, 75.7873]
294
- }
 
295
 
296
  location_lower = location.lower()
297
  return location_coordinates.get(location_lower, [28.6139, 77.2090]) # Default to Delhi
@@ -399,4 +391,4 @@ def get_regional_language(location):
399
  "jaipur": "Hindi"
400
  }
401
 
402
- return language_map.get(location.lower(), "Hindi")
 
103
  }
104
 
105
  return default_rainfall.get(location_lower, 750) # Default to 750mm if location not found
106
+
107
+ def daily_to_annual(daily_mm, method="simple", month=None, monthly_avg=None, mu=None, sigma=None, sims=10000):
108
+ if method == "simple":
109
+ return daily_mm * 365
110
+ if method == "monthly_factor":
111
+ if month is None or monthly_avg is None:
112
+ raise ValueError("month and monthly_avg (array of 12) required")
113
+ days = calendar.monthrange(2024, month)[1] # only for days count
114
+ # naive: scale predicted day to that month then sum using monthly averages
115
+ monthly_est = [monthly_avg[i] if i+1 != month else daily_mm * days for i in range(12)]
116
+ return sum(monthly_est)
117
+ if method == "monte_carlo":
118
+ if mu is None or sigma is None:
119
+ raise ValueError("mu and sigma required for monte_carlo")
120
+ draws = np.random.normal(mu, sigma, size=(sims, 365))
121
+ draws[draws < 0] = 0.0
122
+ totals = draws.sum(axis=1)
123
+ return {"mean": totals.mean(), "median": np.median(totals), "p10": np.percentile(totals,10), "p90": np.percentile(totals,90)}
124
+ raise ValueError("unknown method")
125
  def get_groundwater_level(lat, lon, date):
126
  """
127
  Fetch groundwater level from India WRIS API for a given lat, lon, and date.
 
131
  # Here, we use Erode, Tamil Nadu for demonstration
132
  import requests
133
  url = (
134
+ "https://indiawris.gov.in/Dataset/Ground Water Level?stateName=Tamil%20Nadu&districtName=Karur&agencyName=CGWB&startdate=2025-09-14&enddate=2025-09-14&download=false&page=0&size=1"
 
 
135
  )
 
 
 
136
  try:
137
+ resp = requests.post(url, timeout=10)
138
+ data = resp.json()
139
+ print(data)
140
+ if resp:
141
+ print(data)
142
+ if data:
143
+ gw_level = data['data'][0].get('dataValue', 15)
144
+ return float(gw_level)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  except Exception as e:
146
+ print(f"Error fetching groundwater level: {e}")
147
+ return 15 # fallback default
 
 
148
 
149
  def get_soil_type(lat, lon):
150
  """
 
153
  import requests
154
  url = f"https://api.openepi.io/soil/type?lat={lat}&lon={lon}&top_k=3"
155
  try:
156
+ resp = requests.get(url, timeout=10)
157
  if resp.status_code == 200:
158
  data = resp.json()
159
  # Parse soil type from response (update as per actual API response)
160
+ if data:
161
+ return data['properties'].get('most_probable_soil_type', 'Loamy')
 
 
 
 
 
 
162
  except Exception as e:
163
  print(f"Error fetching soil type: {e}")
164
  return "Loamy" # fallback default
165
 
 
166
  def RooftTopCalculation(input_data,RainFallData):
167
  """
168
  Calculate rainwater harvesting parameters based on input data
 
271
  def get_coordinates(location):
272
  """Get coordinates for a location (simplified - would use geocoding service in production)"""
273
  location_coordinates = {
274
+ "karur": [10.9601, 78.0766],
275
+ "new delhi": [28.6139, 77.2090],
276
+ "delhi": [28.6139, 77.2090],
277
+ "mumbai": [19.0760, 72.8777],
278
+ "bangalore": [12.9716, 77.5946],
279
+ "chennai": [13.0827, 80.2707],
280
+ "hyderabad": [17.3850, 78.4867],
281
+ "salem": [11.6643, 78.1460],
282
+ "kolkata": [22.5726, 88.3639],
283
+ "madurai": [9.9252, 78.1198],
284
+ "erode": [11.3410, 77.7172]
285
+ }
286
+
287
 
288
  location_lower = location.lower()
289
  return location_coordinates.get(location_lower, [28.6139, 77.2090]) # Default to Delhi
 
391
  "jaipur": "Hindi"
392
  }
393
 
394
+ return language_map.get(location.lower(), "Tamil")