import requests import datetime import pandas as pd from tempfile import tempdir import firebase_admin from firebase_admin import credentials from firebase_admin import db import traceback import json import numpy as np from sklearn.cluster import KMeans import traceback import numpy as np from google.cloud import storage import requests cred = credentials.Certificate('servicekey.json') import json import os # Function to check if file exists def check_file_exists(file_path): return os.path.isfile(file_path) # s1_images = ["B02", "B03", "B04", "B05"] #s2_images = ["RVI-NEW"] try: firebase_admin.initialize_app(cred, {'databaseURL': 'https://farmbase-b2f7e-31c0c.firebaseio.com/'}) except: print('fire running') def get_historical_weather(uuid, field_id, api_key, lat, lon, start_date, end_date, output_file): url = "http://api.weatherapi.com/v1/history.json" current_date = datetime.datetime.strptime(start_date, "%Y-%m-%d") end_date = datetime.datetime.strptime(end_date, "%Y-%m-%d") weather_data = [] while current_date <= end_date: params = { "key": api_key, "q": f"{lat},{lon}", "dt": current_date.strftime("%Y-%m-%d") } response = requests.get(url, params=params) if response.status_code == 200: data = response.json() day_data = data['forecast']['forecastday'][0]['day'] weather_data.append({ "date": current_date.strftime("%Y-%m-%d"), "uuid": uuid, "field_id":field_id, "lat":lat, "long":lon, "rainfall_mm": day_data['totalprecip_mm'], "avg_temp_c": day_data['avgtemp_c'], "max_temp_c": day_data['maxtemp_c'], "min_temp_c": day_data['mintemp_c'] }) else: print(f"Failed to retrieve data for {current_date.strftime('%Y-%m-%d')}: {response.status_code}, {response.text}") current_date += datetime.timedelta(days=1) # Create a DataFrame and save it as an Excel file df = pd.DataFrame(weather_data) df.to_excel(output_file, index=False) print(f"Weather data saved to {output_file}") with open('Downloads/ifpri_filtered_sheet.json', 'r') as file: ifpri_json = json.load(file) uid = 'ipRHhCOFIDV2pxgg7Nfz1ufZBmV2' ifpri_num = 0 w_start, w_end = 0, 2700 for single_field_obj in ifpri_json: ifpri_num = ifpri_num + 1 try: print(single_field_obj) field_id = str(single_field_obj["FieldID"]) uuid = single_field_obj["UUID"] output_file = f"ifpri_weather_sep/{uuid}_{field_id}_historical_weather.xlsx" print(field_id) print(uuid) print(ifpri_num) if check_file_exists(output_file): print('weather_processed_already') continue if ifpri_num < w_start or ifpri_num > w_end: continue latitude = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(field_id).child('Coordinates').child('a').child('Latitude').get() longitude = db.reference('PaidMonitoredFields').child('PMF').child(uid).child(field_id).child('Coordinates').child('a').child('Longitude').get() # Example usage: api_key = "dd6e11f5d20e4694bed72932241007" start_date = "2022-07-01" end_date = "2024-01-31" output_file = f"ifpri_weather_sep/{uuid}_{field_id}_historical_weather.xlsx" get_historical_weather(uuid, field_id, api_key, latitude, longitude, start_date, end_date, output_file) except: print(traceback.format_exc())