Last active
December 9, 2021 13:56
-
-
Save leonardojimenez1990/c118ab525694843d582a13ab95946636 to your computer and use it in GitHub Desktop.
Descargar y plotear datos de temperatura de NCEP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
import requests | |
import xarray as xr | |
import netCDF4 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter | |
from matplotlib.cm import get_cmap | |
import cartopy.crs as ccrs | |
import cartopy.feature as cf | |
from cartopy.feature import NaturalEarthFeature | |
from tqdm import tqdm | |
def descarga(iniciald, finald): | |
for i in range(inicial, final, 1): | |
myurl = "https://downloads.psl.noaa.gov/Datasets/ncep.reanalysis/surface_gauss/air.2m.gauss." + str(i) + ".nc" | |
# Streaming, so we can iterate over the response. | |
response = requests.get(myurl, stream=True) | |
total_size_in_bytes = int(response.headers.get('content-length', 0)) | |
block_size = 1024 # 1 Kibibyte | |
print("air.2m.gauss." + str(i) + ".nc") | |
progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True) | |
with open("air.2m.gauss." + str(i) + ".nc", 'wb') as file: | |
for data in response.iter_content(block_size): | |
progress_bar.update(len(data)) | |
file.write(data) | |
progress_bar.close() | |
if total_size_in_bytes != 0 and progress_bar.n != total_size_in_bytes: | |
print("Error, no se pudo descargar") | |
# for i in tqdm(range(inicial, final, 1)): | |
# myurl = "https://downloads.psl.noaa.gov/Datasets/ncep.reanalysis/surface_gauss/air.2m.gauss." + str(i) + ".nc" | |
# filedowloand = "air.2m.gauss." + str(i) + ".nc" | |
# listyear.append(filedowloand) | |
# tqdm(urllib.request.urlretrieve(myurl, filedowloand)) | |
# print(listyear[i]) | |
def leerNetcdf(iniciall, finall): | |
for i in range(inicial, final + 1, 1): | |
datad = 0 | |
with xr.open_dataset("air.2m.gauss." + str(i) + ".nc") as ds: # air.2m.gauss. time=8760 | |
data = ds.air[:] | |
for j in range(diames*24): | |
datad = datad + data.isel(time=j) | |
datad = datad/(diames*24) | |
print(datad) | |
dataFram = datad.to_dataframe(name=None, dim_order=None) | |
dataFram.to_csv('salva.csv') | |
print(dataFram) | |
# Using geophysical units. `robust` disregards outliers for colour map creation. | |
fig = plt.figure(1, figsize=(15., 12.)) | |
ax = plt.axes(projection=ccrs.PlateCarree()) | |
ax.coastlines() | |
ax.add_feature(cf.LAND) # utilizar con internet | |
datad.plot() | |
plt.title("Mean air " + str(i)+'-'+str(mes)) | |
ax.gridlines(draw_labels=True) | |
#plt.scatter(312.5, 47.5) | |
plt.show() | |
while True: | |
print('Entre el año dentro del periodo 1948 - 2019') | |
inicial = int(input('Entre el año inicial del periodo que quiere descargar: ')) | |
if 1948 <= inicial <= 2019: | |
print() | |
break | |
else: | |
print('Escriba nuevamente el año inicial\n') | |
while True: | |
print('Entre el año dentro del periodo 1948 - 2019') | |
final = int(input('Entre el año final del periodo que quiere descargar: ')) | |
if 1948 <= final <= 2019: | |
print() | |
break | |
else: | |
print('Escriba nuevamente el año final\n') | |
mes = int(input("Escriba su mes de nacimiento: ")) | |
diames = 0 | |
if mes in [1, 3, 5, 7, 8, 10, 12]: | |
diames = 31 | |
elif mes in [4, 6, 9, 11]: | |
diames = 30 | |
elif mes == 2: | |
if inicial % 4 == 0: | |
diames = 29 | |
else: | |
diames = 28 | |
print('cantidad del dia del mes es igual: ', diames) | |
print('inicial: ', inicial, 'final: ', final) | |
descarga(inicial,final) | |
leerNetcdf(inicial, final) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment