from __future__ import print_function # make sure print behaves the same in 2.7 and 3.x import netCDF4 import numpy as np f = netCDF4.Dataset('data/rtofs_glo_3dz_f006_6hrly_reg3.nc') print(f) print(f.variables.keys()) # get all variable names temp = f.variables['temperature'] # temperature variable print(temp) for d in f.dimensions.items(): print(d) temp.dimensions temp.shape mt = f.variables['MT'] depth = f.variables['Depth'] x,y = f.variables['X'], f.variables['Y'] print(mt) print(x) time = mt[:] # Reads the netCDF variable MT, array of one element print(time) dpth = depth[:] # examine depth array print(dpth) xx,yy = x[:],y[:] print('shape of temp variable: %s' % repr(temp.shape)) tempslice = temp[0, dpth > 400, yy > yy.max()/2, xx > xx.max()/2] print('shape of temp slice: %s' % repr(tempslice.shape)) lat, lon = f.variables['Latitude'], f.variables['Longitude'] print(lat) # extract lat/lon values (in degrees) to numpy arrays latvals = lat[:]; lonvals = lon[:] # a function to find the index of the point closest pt # (in squared distance) to give lat/lon value. def getclosest_ij(lats,lons,latpt,lonpt): # find squared distance of every point on grid dist_sq = (lats-latpt)**2 + (lons-lonpt)**2 # 1D index of minimum dist_sq element minindex_flattened = dist_sq.argmin() # Get 2D index for latvals and lonvals arrays from 1D index return np.unravel_index(minindex_flattened, lats.shape) iy_min, ix_min = getclosest_ij(latvals, lonvals, 50., -140) sal = f.variables['salinity'] # Read values out of the netCDF file for temperature and salinity print('%7.4f %s' % (temp[0,0,iy_min,ix_min], temp.units)) print('%7.4f %s' % (sal[0,0,iy_min,ix_min], sal.units)) import datetime date = datetime.datetime.now() # build URL for latest synoptic analysis time URL = 'http://thredds.ucar.edu/thredds/dodsC/grib/NCEP/GFS/Global_0p5deg/GFS_Global_0p5deg_%04i%02i%02i_%02i%02i.grib2/GC' %\ (date.year,date.month,date.day,6*(date.hour//6),0) # keep moving back 6 hours until a valid URL found validURL = False; ncount = 0 while (not validURL and ncount < 10): print(URL) try: gfs = netCDF4.Dataset(URL) validURL = True except RuntimeError: date -= datetime.timedelta(hours=6) ncount += 1 # Look at metadata for a specific variable # gfs.variables.keys() will show all available variables. sfctmp = gfs.variables['Temperature_surface'] # get info about sfctmp print(sfctmp) # print coord vars associated with this variable for dname in sfctmp.dimensions: print(gfs.variables[dname]) soilmvar = gfs.variables['Volumetric_Soil_Moisture_Content_depth_below_surface_layer'] # flip the data in latitude so North Hemisphere is up on the plot soilm = soilmvar[0,0,::-1,:] print('shape=%s, type=%s, missing_value=%s' % \ (soilm.shape, type(soilm), soilmvar.missing_value)) import matplotlib.pyplot as plt %matplotlib inline cs = plt.contourf(soilm) from netCDF4 import num2date, date2num, date2index timedim = sfctmp.dimensions[0] # time dim name print('name of time dimension = %s' % timedim) times = gfs.variables[timedim] # time coord var print('units = %s, values = %s' % (times.units, times[:])) dates = num2date(times[:], times.units) print([date.strftime('%Y-%m-%d %H:%M:%S') for date in dates[:10]]) # print only first ten... from datetime import datetime, timedelta date = datetime.now() + timedelta(days=3) print(date) ntime = date2index(date,times,select='nearest') print('index = %s, date = %s' % (ntime, dates[ntime])) lats, lons = gfs.variables['lat'][:], gfs.variables['lon'][:] # lats, lons are 1-d. Make them 2-d using numpy.meshgrid. lons, lats = np.meshgrid(lons,lats) j, i = getclosest_ij(lats,lons,40,-105) fcst_temp = sfctmp[ntime,j,i] print('Boulder forecast valid at %s UTC = %5.1f %s' % \ (dates[ntime],fcst_temp,sfctmp.units)) !ls -l data/prmsl*nc mf = netCDF4.MFDataset('data/prmsl*nc') times = mf.variables['time'] dates = num2date(times[:],times.units) print('starting date = %s' % dates[0]) print('ending date = %s'% dates[-1]) prmsl = mf.variables['prmsl'] print('times shape = %s' % times.shape) print('prmsl dimensions = %s, prmsl shape = %s' %\ (prmsl.dimensions, prmsl.shape)) f.close() gfs.close()