{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# Pronóstico para una latitud, longitud y fecha determinada"],"metadata":{"id":"WvlRTI7dYXl7"}},{"cell_type":"markdown","source":["(Última actualización 1 sep 2023)\n","\n","| En este ejemplo se describe cómo obtener el pronóstico de una variable específica del dataset (temperatura a 2 m por ejemplo) en una latitud, longitud y fecha dadas.\n","| *In this example we show how to obtain the forecast of a specific variable (2-meter temperature for example) for a given latitude, longitude and date.*"],"metadata":{"id":"6GT8o95_bFYE"}},{"cell_type":"code","source":["# En caso de utilizar Google Colab, descomentar las siguientes líneas\n","# In case of using Google Colab, uncomment the following lines\n","\n","#!pip install cartopy"],"metadata":{"id":"dC5ymZue7WxK"},"execution_count":null,"outputs":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"9SwjG2XSYAjL"},"outputs":[],"source":["# Importamos las librerías necesarias\n","# We import the necessary libraries\n","import xarray as xr\n","import h5netcdf\n","import datetime\n","import cartopy.crs as ccrs"]},{"cell_type":"markdown","source":["| Definimos la fecha de inicialización del pronóstico, plazo de pronóstico y latitud y longitud a consultar.\n","| *We define the forecast initialization date, the desired lead time, latitude and longitude (March 21, 2022 00 UTC; March 22, 2022 17 UTC; -25; -70).*"],"metadata":{"id":"f7JhNwCSY1Zt"}},{"cell_type":"code","source":["init_year = 2022\n","init_month = 4\n","init_day = 1\n","init_hour = 0\n","INIT_DATE = datetime.datetime(init_year, init_month, init_day, init_hour)\n","\n","lead_time = 17\n","\n","latitude = -25\n","longitude = -70"],"metadata":{"id":"e9ZILfqzYPx4"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["| Definimos la variable a consultar:\n","| *We define the variable to consult:*"],"metadata":{"id":"gXLhVITAcIma"}},{"cell_type":"code","source":["var = 'T2'"],"metadata":{"id":"qlIf9IhrcKhQ"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["| Leemos el archivo:\n","| *We read the file containing the information we are looking for:*"],"metadata":{"id":"6nGRtnZ6amdZ"}},{"cell_type":"code","source":["# Descomentar la opción elegida:\n","\n","# --------\n","# Opción 1: Para acceder al archivo online\n","# Option 1: To access the file online\n","#!pip install s3fs\n","#import s3fs\n","#s3_file = f'smn-ar-wrf/DATA/WRF/DET/{INIT_DATE:%Y/%m/%d/%H}/WRFDETAR_01H_{INIT_DATE:%Y%m%d_%H}_{lead_time:03d}.nc'\n","#fs = s3fs.S3FileSystem(anon=True)\n","#if fs.exists(s3_file):\n","# f = fs.open(s3_file)\n","# ds = xr.open_dataset(f, decode_coords = 'all', engine = 'h5netcdf')\n","#else:\n","# print('El archivo buscado no existe')\n","# --------\n","\n","# --------\n","# Opción 2: Para abrir un archivo ya descargado\n","# Option 2: To open an already downloaded file\n","#filename = 'WRFDETAR_01H_{:%Y%m%d_%H}_{:03d}.nc'.format(INIT_DATE,lead_time)\n","#ds = xr.open_dataset(filename, decode_coords = 'all', engine = 'h5netcdf')\n","# --------"],"metadata":{"id":"qlKJwkxkcO4n"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["| Obtenemos el valor pronosticado:\n","| *We get the appropriate forecast value:*\n"],"metadata":{"id":"-DHk6lUE3kmY"}},{"cell_type":"code","source":["# Buscamos la ubicación del punto más cercano a la latitud y longitud solicitada\n","# We search the closest gridpoint to the selected lat-lon\n","data_crs = ccrs.LambertConformal(central_longitude = ds['Lambert_Conformal'].attrs['longitude_of_central_meridian'],\n"," central_latitude = ds['Lambert_Conformal'].attrs['latitude_of_projection_origin'],\n"," standard_parallels = ds['Lambert_Conformal'].attrs['standard_parallel'])\n","x, y = data_crs.transform_point(longitude, latitude, src_crs=ccrs.PlateCarree())\n","\n","# Seleccionamos el dato mas cercano a la latitud, longitud y fecha escogida\n","# We extract the value at the chosen gridpoint\n","\n","forecast = ds.sel(dict(x = x, y = y), method = 'nearest')[var]\n","\n","print(f'The forecast value for the variable {var} at latitude {latitude} and longitude {longitude} is: {forecast.values[0]:0.2f}°C')\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"CpAum8a1as6r","outputId":"cef45d33-0f9b-4303-f50b-04bc100359a4","executionInfo":{"status":"ok","timestamp":1690471626766,"user_tz":180,"elapsed":403,"user":{"displayName":"Federico Cutraro","userId":"17057351904426812022"}}},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["The forecast value for the variable T2 at latitude -25 and longitude -70 is: 26.33°C\n"]}]}]}