import numpy as np
import csv
import matplotlib.pyplot as plt
#from mpl_toolkits.basemap import Basemap
from matplotlib.colors import ListedColormap, LinearSegmentedColormap
import os
import glob
#import wradlib as wrl
import cartopy
import cartopy.crs as ccrs
import sys
import pandas
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import matplotlib.pyplot as plt
import xarray as xr
import numpy as np
import metpy
import metpy.calc as mpcalc
from metpy.plots import ctables
from metpy.cbook import get_test_data
from metpy.units import units
import os
import scipy.integrate as integrate
import datetime as dt
import glob
import json
from datetime import datetime
from datetime import timedelta
from metpy.plots import ctables
from matplotlib.colors import Normalize
from matplotlib.colors import ListedColormap, LinearSegmentedColormap, BoundaryNorm
#import wrf
import scipy
#import xcape
import xarray
file_dir = '/home/lmathias/Desktop/UVINDEX/'
os.chdir(file_dir)
os.remove('uvindex.nc')
data = xarray.open_dataset('uvindex.grib', engine='cfgrib')
data.to_netcdf('uvindex.nc')
data_uv = xr.open_dataset('uvindex.nc')
print(data_uv)
Ignoring index file 'uvindex.grib.923a8.idx' older than GRIB file
<xarray.Dataset> Dimensions: (step: 121, latitude: 1, longitude: 1) Coordinates: number int64 ... time datetime64[ns] ... * step (step) timedelta64[ns] 00:00:00 01:00:00 ... 5 days 00:00:00 surface float64 ... * latitude (latitude) float64 49.6 * longitude (longitude) float64 6.2 valid_time (step) datetime64[ns] ... Data variables: uvbedcs (step, latitude, longitude) float32 ... uvbed (step, latitude, longitude) float32 ... Attributes: GRIB_edition: 1 GRIB_centre: ecmf GRIB_centreDescription: European Centre for Medium-Range Weather Forecasts GRIB_subCentre: 0 Conventions: CF-1.7 institution: European Centre for Medium-Range Weather Forecasts history: 2023-07-12T21:30 GRIB to CDM+CF via cfgrib-0.9.9...
data_uv = data.metpy.parse_cf()
x, y = data_uv['uvbed'].metpy.coordinates('x', 'y')
time = data_uv['uvbed'].step
timeinit = data_uv.time
timeinit = datetime.utcfromtimestamp(timeinit.item()/1e9)
print(timeinit)
lon = data_uv['uvbed'].metpy.longitude
lat = data_uv['uvbed'].metpy.latitude
dx, dy = mpcalc.lat_lon_grid_deltas(lon, lat)
dx = dx[None, :]
dy = dy[None, :]
uvi = data_uv['uvbed']
uvics = data_uv['uvbedcs']
lat = 49.6
lon = 6.2
uvi = uvi.metpy.loc[{'latitude': lat, 'longitude': lon}]*40
uvics = uvics.metpy.loc[{'latitude': lat, 'longitude': lon}]*40
print(uvi.values)
/home/lmathias/anaconda3/envs/metpy/lib/python3.9/site-packages/metpy/xarray.py:349: UserWarning: More than one time coordinate present for variable "uvbedcs". warnings.warn('More than one ' + axis + ' coordinate present for variable' Found valid latitude/longitude coordinates, assuming latitude_longitude for projection grid_mapping variable Found valid latitude/longitude coordinates, assuming latitude_longitude for projection grid_mapping variable
2023-07-12 12:00:00 [5.9708977 5.4041505 4.2237735 2.8815293 1.590885 0.70822984 0.2582489 0.04583398 0. 0. 0. 0. 0. 0. 0. 0. 0.03491498 0.23330301 0.6912817 1.4212732 2.4563227 3.145702 4.4612765 5.417676 5.138383 4.834099 4.111538 2.5997376 1.4231155 0.6774448 0.24668843 0.04378217 0. 0. 0. 0. 0. 0. 0. 0. 0.03334106 0.25260508 0.7866167 1.7260991 3.144164 4.743843 5.846801 6.341107 6.775415 6.3316298 5.0252676 3.4752107 2.012678 0.9235685 0.3058809 0.04914793 0. 0. 0. 0. 0. 0. 0. 0. 0.02906333 0.20064339 0.52568746 0.91662943 1.2822582 2.92835 2.9995012 4.053235 5.1845837 3.3995652 2.2459304 2.1795917 1.4772279 0.6047341 0.29652968 0.04704583 0. 0. 0. 0. 0. 0. 0. 0. 0.02888026 0.23566708 0.77248096 1.820787 3.366487 5.1893353 6.755016 7.5136375 6.7165327 6.1296654 3.8357759 2.7403498 1.6019697 0.8268079 0.28513148 0.04088613 0. 0. 0. 0. 0. 0. 0. 0. 0.01971994 0.17707959 0.71418047 1.7141459 3.212607 4.8566866 6.397114 7.3559713 7.4043345 ]
today = datetime.today()
#date2=datetime.strptime(timeinit, '%d-%m-%Y %H:%M:%S').strftime('%Y%m%d000000')
date2=timeinit.strftime('%Y%m%d%H0000')
date3=today.strftime('%Y%m%d000000')
print(date2)
with open(date2+'_UVINDEX.TXT', 'w', newline='') as csvfile:
obswriter = csv.writer(csvfile, delimiter='|')
obswriter.writerow(['971 Luxembourg-South'])
for i in range(121):
today=timeinit+timedelta(hours=i)
date2=today.strftime('%Y%m%d%H0000')
obswriter.writerow([date2,uvi.values[i],uvics.values[i],0])
20230712120000