Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract numbers from within an array?

Using Python to try and pull forecast dew point data for a single location, with the goal being to come up with code that can pull the forecasted dew points and write it into a csv file. My question is to see if there's a way to print only the data within the array without showing the coordinates and other items.

Computer is a Windows running Python 3.6 via Anaconda. I've been able to focus in on just a few forecast data points I'm looking for (shown as [12:15] in the code) but no matter what it produces the coordinates and other data that I don't exactly need. I tried messing around with xarray.DataArray.drop a bit but I couldn't figure out exactly what I would need to drop.

import csv
import matplotlib.pyplot as plt
import pandas as pd
import xarray as xr
import datetime as dt
from datetime import datetime, timedelta

dayFile = datetime.now() - timedelta(days=1)
dayFile  = dayFile.strftime("%Y%m%d")

url='https://nomads.ncep.noaa.gov:9090/dods/gfs_0p50/gfs%s/gfs_0p50_00z' %(dayFile)
ds = xr.open_dataset(url)

lati = 41.4; loni = 100.8
dsloc = ds.sel(lon=loni, lat=lati, method='nearest')

data_to_write_day_0 = dsloc['dpt2m'][12:15]

print(data_to_write_day_0)

Edit: Expected output would be producing the forecast numbers only (I.e. 283.729, 284.827, 283.282). I guess in other words the desire is to break up the array so that only the forecasted values are printed out.

like image 332
alberta_cross_1090 Avatar asked Oct 14 '25 18:10

alberta_cross_1090


1 Answers

Use the values attribute to access the data. It returns the data as a numpy.ndarray:

for x in data_to_write_day_0.values:
    print(x)

Output:

283.1779
282.57788
282.7585
like image 194
Stef Avatar answered Oct 17 '25 07:10

Stef