More on units can be found at the MetPy help page at: http://metpy.readthedocs.io/en/latest/ under 'Unit Support'.
# Import essential packages and setup inline plotting.
import matplotlib.pyplot as plt
import numpy as np
from metpy.units import units
%matplotlib inline
# Start setting up thredds (Thematic Real-time Environmental Distributed Data Services) access
# using siphon ncss (netCDF subset service) method.
from siphon.catalog import get_latest_access_url
gfs_catalog = "http://thredds.ucar.edu/thredds/catalog/grib/NCEP/GFS/Global_0p25deg/catalog.html"
latest_gfs_ncss = get_latest_access_url(gfs_catalog, "NetcdfSubset")
# Set up access via NCSS
from siphon.ncss import NCSS
ncss = NCSS(latest_gfs_ncss)
# Create a query to ask for all times in netcdf4 format for
# the Temperature_surface variable, with a bounding box centered
# on lat,lon with a height and width speficied in degrees.
query = ncss.query()
lat = 42
lon = -92.5
width = 25
height = 20
/usr/local/miniconda3/lib/python3.5/site-packages/siphon/catalog.py:72: UserWarning: URL http://thredds.ucar.edu/thredds/catalog/grib/NCEP/GFS/Global_0p25deg/catalog.html returned HTML. Changing to: http://thredds.ucar.edu/thredds/catalog/grib/NCEP/GFS/Global_0p25deg/catalog.xml new_url))
# Let's take a look at the available variables.
ncss.variables
{'5-Wave_Geopotential_Height_isobaric', 'Absolute_vorticity_isobaric', 'Albedo_surface_Mixed_intervals_Average', 'Apparent_temperature_height_above_ground', 'Best_4_layer_Lifted_Index_surface', 'Categorical_Freezing_Rain_surface_Mixed_intervals_Average', 'Categorical_Ice_Pellets_surface_Mixed_intervals_Average', 'Categorical_Rain_surface_Mixed_intervals_Average', 'Categorical_Snow_surface_Mixed_intervals_Average', 'Cloud_Work_Function_entire_atmosphere_single_layer_Mixed_intervals_Average', 'Cloud_mixing_ratio_isobaric', 'Cloud_water_entire_atmosphere_single_layer', 'Convective_Precipitation_Rate_surface_Mixed_intervals_Average', 'Convective_available_potential_energy_pressure_difference_layer', 'Convective_available_potential_energy_surface', 'Convective_inhibition_pressure_difference_layer', 'Convective_inhibition_surface', 'Convective_precipitation_surface_Mixed_intervals_Accumulation', 'Dewpoint_temperature_height_above_ground', 'Downward_Long-Wave_Radp_Flux_surface_Mixed_intervals_Average', 'Downward_Short-Wave_Radiation_Flux_surface_Mixed_intervals_Average', 'Field_Capacity_surface', 'Geopotential_height_highest_tropospheric_freezing', 'Geopotential_height_isobaric', 'Geopotential_height_maximum_wind', 'Geopotential_height_potential_vorticity_surface', 'Geopotential_height_surface', 'Geopotential_height_tropopause', 'Geopotential_height_zeroDegC_isotherm', 'Ground_Heat_Flux_surface_Mixed_intervals_Average', 'Haines_Index_surface', 'ICAO_Standard_Atmosphere_Reference_Height_maximum_wind', 'ICAO_Standard_Atmosphere_Reference_Height_tropopause', 'Ice_cover_surface', 'Icing_severity_isobaric', 'Land_cover_0__sea_1__land_surface', 'Latent_heat_net_flux_surface_Mixed_intervals_Average', 'MSLP_Eta_model_reduction_msl', 'Maximum_temperature_height_above_ground_Mixed_intervals_Maximum', 'Meridional_Flux_of_Gravity_Wave_Stress_surface_Mixed_intervals_Average', 'Minimum_temperature_height_above_ground_Mixed_intervals_Minimum', 'Momentum_flux_u-component_surface_Mixed_intervals_Average', 'Momentum_flux_v-component_surface_Mixed_intervals_Average', 'Ozone_Mixing_Ratio_isobaric', 'Per_cent_frozen_precipitation_surface', 'Planetary_Boundary_Layer_Height_surface', 'Potential_Evaporation_Rate_surface', 'Potential_temperature_sigma', 'Precipitable_water_entire_atmosphere_single_layer', 'Precipitation_rate_surface_Mixed_intervals_Average', 'Pressure_convective_cloud_bottom', 'Pressure_convective_cloud_top', 'Pressure_height_above_ground', 'Pressure_high_cloud_bottom_Mixed_intervals_Average', 'Pressure_high_cloud_top_Mixed_intervals_Average', 'Pressure_low_cloud_bottom_Mixed_intervals_Average', 'Pressure_low_cloud_top_Mixed_intervals_Average', 'Pressure_maximum_wind', 'Pressure_middle_cloud_bottom_Mixed_intervals_Average', 'Pressure_middle_cloud_top_Mixed_intervals_Average', 'Pressure_of_level_from_which_parcel_was_lifted_pressure_difference_layer', 'Pressure_potential_vorticity_surface', 'Pressure_reduced_to_MSL_msl', 'Pressure_surface', 'Pressure_tropopause', 'Relative_humidity_entire_atmosphere_single_layer', 'Relative_humidity_height_above_ground', 'Relative_humidity_highest_tropospheric_freezing', 'Relative_humidity_isobaric', 'Relative_humidity_pressure_difference_layer', 'Relative_humidity_sigma', 'Relative_humidity_sigma_layer', 'Relative_humidity_zeroDegC_isotherm', 'Sensible_heat_net_flux_surface_Mixed_intervals_Average', 'Snow_depth_surface', 'Soil_temperature_depth_below_surface_layer', 'Specific_humidity_height_above_ground', 'Specific_humidity_pressure_difference_layer', 'Storm_relative_helicity_height_above_ground_layer', 'Sunshine_Duration_surface', 'Surface_Lifted_Index_surface', 'Temperature_altitude_above_msl', 'Temperature_height_above_ground', 'Temperature_high_cloud_top_Mixed_intervals_Average', 'Temperature_isobaric', 'Temperature_low_cloud_top_Mixed_intervals_Average', 'Temperature_maximum_wind', 'Temperature_middle_cloud_top_Mixed_intervals_Average', 'Temperature_potential_vorticity_surface', 'Temperature_pressure_difference_layer', 'Temperature_sigma', 'Temperature_surface', 'Temperature_tropopause', 'Total_cloud_cover_boundary_layer_cloud_Mixed_intervals_Average', 'Total_cloud_cover_convective_cloud', 'Total_cloud_cover_entire_atmosphere_Mixed_intervals_Average', 'Total_cloud_cover_high_cloud_Mixed_intervals_Average', 'Total_cloud_cover_low_cloud_Mixed_intervals_Average', 'Total_cloud_cover_middle_cloud_Mixed_intervals_Average', 'Total_ozone_entire_atmosphere_single_layer', 'Total_precipitation_surface_Mixed_intervals_Accumulation', 'U-Component_Storm_Motion_height_above_ground_layer', 'Upward_Long-Wave_Radp_Flux_atmosphere_top_Mixed_intervals_Average', 'Upward_Long-Wave_Radp_Flux_surface_Mixed_intervals_Average', 'Upward_Short-Wave_Radiation_Flux_atmosphere_top_Mixed_intervals_Average', 'Upward_Short-Wave_Radiation_Flux_surface_Mixed_intervals_Average', 'V-Component_Storm_Motion_height_above_ground_layer', 'Ventilation_Rate_planetary_boundary', 'Vertical_Speed_Shear_potential_vorticity_surface', 'Vertical_Speed_Shear_tropopause', 'Vertical_velocity_pressure_isobaric', 'Vertical_velocity_pressure_sigma', 'Volumetric_Soil_Moisture_Content_depth_below_surface_layer', 'Water_equivalent_of_accumulated_snow_depth_surface', 'Water_runoff_surface_Mixed_intervals_Accumulation', 'Wilting_Point_surface', 'Wind_speed_gust_surface', 'Zonal_Flux_of_Gravity_Wave_Stress_surface_Mixed_intervals_Average', 'u-component_of_wind_altitude_above_msl', 'u-component_of_wind_height_above_ground', 'u-component_of_wind_isobaric', 'u-component_of_wind_maximum_wind', 'u-component_of_wind_planetary_boundary', 'u-component_of_wind_potential_vorticity_surface', 'u-component_of_wind_pressure_difference_layer', 'u-component_of_wind_sigma', 'u-component_of_wind_tropopause', 'v-component_of_wind_altitude_above_msl', 'v-component_of_wind_height_above_ground', 'v-component_of_wind_isobaric', 'v-component_of_wind_maximum_wind', 'v-component_of_wind_planetary_boundary', 'v-component_of_wind_potential_vorticity_surface', 'v-component_of_wind_pressure_difference_layer', 'v-component_of_wind_sigma', 'v-component_of_wind_tropopause'}
# Build our query time and domain.
query.all_times().accept('netcdf4').variables('Pressure_reduced_to_MSL_msl')
query.lonlat_box(north=lat+height/2., south=lat - height/2., east=lon + width/2., west=lon - width/2.)
# Get the data!
nc = ncss.get_data(query)
# Let's see what this netCDF variable looks like.
nc
<class 'netCDF4._netCDF4.Dataset'> root group (NETCDF4 data model, file format HDF5): Originating_or_generating_Center: US National Weather Service, National Centres for Environmental Prediction (NCEP) Originating_or_generating_Subcenter: 0 GRIB_table_version: 2,1 Type_of_generating_process: Forecast Analysis_or_forecast_generating_process_identifier_defined_by_originating_centre: Analysis from GFS (Global Forecast System) Conventions: CF-1.6 history: Read using CDM IOSP GribCollection v3 featureType: GRID History: Translated to CF-1.0 Conventions by Netcdf-Java CDM (CFGridWriter2) Original Dataset = /data/ldm/pub/native/grid/NCEP/GFS/Global_0p25deg/GFS_Global_0p25deg_20161209_1200.grib2.ncx3#LatLon_721X1440-p125S-180p0E; Translation Date = 2016-12-09T19:33:13.494Z geospatial_lat_min: 32.0 geospatial_lat_max: 52.0 geospatial_lon_min: -105.0 geospatial_lon_max: -80.0 dimensions(sizes): time(93), lat(81), lon(101) variables(dimensions): float32 Pressure_reduced_to_MSL_msl(time,lat,lon), float64 time(time), float32 lat(lat), float32 lon(lon) groups:
# Grab a variable. I am going to grad mean sea-level pressure and mess around with the units.
var='Pressure_reduced_to_MSL_msl'
ncvar = nc[var]
ncvar
<class 'netCDF4._netCDF4.Variable'> float32 Pressure_reduced_to_MSL_msl(time, lat, lon) long_name: Pressure reduced to MSL @ Mean sea level units: Pa abbreviation: PRMSL missing_value: nan grid_mapping: LatLon_Projection coordinates: time lat lon Grib_Variable_Id: VAR_0-3-1_L101 Grib2_Parameter: [0 3 1] Grib2_Parameter_Discipline: Meteorological products Grib2_Parameter_Category: Mass Grib2_Parameter_Name: Pressure reduced to MSL Grib2_Level_Type: Mean sea level Grib2_Generating_Process_Type: Forecast unlimited dimensions: current shape = (93, 81, 101) filling on, default _FillValue of 9.969209968386869e+36 used
# What I didn't come up with in class (embarrassing!!!!) is that all these varialbes already have units. Of course they do,
# they are netCDF objects. Let's see what they are.
print(ncvar.units)
Pa
# What do they look like?
ncvar[:]
masked_array(data = [[[104483.0390625 104467.359375 104448.9609375 ..., 101988.640625 101950.8828125 101915.0390625] [104495.6796875 104481.921875 104465.28125 ..., 102008.4765625 101970.2421875 101933.921875] [104506.3984375 104494.71875 104480.15625 ..., 102028.9609375 101990.2421875 101953.6015625] ..., [103504.796875 103539.359375 103573.921875 ..., 102823.0390625 102798.2421875 102772.15625] [103510.8828125 103543.84375 103576.9609375 ..., 102791.84375 102764.0 102735.0390625] [103513.4375 103544.9609375 103576.9609375 ..., 102757.6015625 102726.8828125 102695.203125]] [[104447.1171875 104437.203125 104424.5625 ..., 102156.5625 102122.0 102089.0390625] [104461.359375 104453.359375 104442.3203125 ..., 102179.7578125 102144.2421875 102110.640625] [104474.15625 104468.2421875 104458.9609375 ..., 102203.28125 102167.28125 102132.8828125] ..., [103336.078125 103367.921875 103401.203125 ..., 102999.6015625 102981.203125 102961.0390625] [103346.4765625 103377.0390625 103409.203125 ..., 102972.8828125 102951.6015625 102928.8828125] [103351.6015625 103380.8828125 103411.7578125 ..., 102943.4375 102919.1171875 102893.84375]] [[104397.5234375 104390.640625 104380.5625 ..., 102229.84375 102199.28125 102170.3203125] [104411.4375 104406.4765625 104398.3203125 ..., 102253.359375 102221.84375 102191.921875] [104422.15625 104419.6015625 104413.5234375 ..., 102276.71875 102244.3984375 102213.6796875] ..., [102914.796875 102942.4765625 102973.203125 ..., 102926.4765625 102918.15625 102907.7578125] [102941.359375 102969.0390625 102999.921875 ..., 102904.2421875 102893.5234375 102881.203125] [102959.28125 102987.1171875 103017.6796875 ..., 102878.9609375 102866.0 102851.6015625]] ..., [[102186.0078125 102190.8125 102198.171875 ..., 101637.6875 101616.25 101597.8515625] [102243.609375 102247.453125 102253.6875 ..., 101705.2109375 101684.40625 101666.6484375] [102302.4921875 102305.6875 102311.2890625 ..., 101769.046875 101748.890625 101731.453125] ..., [103589.3671875 103638.171875 103681.8515625 ..., 102808.7265625 102793.53125 102774.8125] [103561.8515625 103610.328125 103653.8515625 ..., 102768.890625 102751.1328125 102730.171875] [103530.8125 103578.8125 103621.8515625 ..., 102725.046875 102704.890625 102681.53125]] [[101232.8359375 101247.234375 101262.4375 ..., 102533.4375 102501.0390625 102468.234375] [101267.8359375 101283.0390625 101299.234375 ..., 102612.640625 102583.8359375 102554.8359375] [101301.234375 101317.4375 101334.640625 ..., 102690.8359375 102665.640625 102640.4375] ..., [102911.640625 102944.0390625 102974.640625 ..., 102972.8359375 102963.640625 102951.4375] [102917.640625 102949.8359375 102980.0390625 ..., 102931.4375 102919.4375 102904.4375] [102914.234375 102947.0390625 102977.4375 ..., 102886.8359375 102871.8359375 102854.0390625]] [[100820.2265625 100803.421875 100787.421875 ..., 102708.625 102732.625 102757.421875] [100839.828125 100822.421875 100805.828125 ..., 102782.0234375 102806.421875 102831.421875] [100855.828125 100837.625 100820.421875 ..., 102854.0234375 102878.828125 102904.421875] ..., [102419.2265625 102427.421875 102438.625 ..., 103059.0234375 103045.625 103031.625] [102433.0234375 102443.421875 102456.421875 ..., 102992.2265625 102977.625 102962.625] [102440.828125 102453.2265625 102468.0234375 ..., 102926.0234375 102910.421875 102894.828125]]], mask = [[[False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False] ..., [False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False]] [[False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False] ..., [False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False]] [[False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False] ..., [False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False]] ..., [[False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False] ..., [False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False]] [[False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False] ..., [False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False]] [[False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False] ..., [False False False ..., False False False] [False False False ..., False False False] [False False False ..., False False False]]], fill_value = nan)
# Pascals are great, but let's change them to something more useful, like millibars.
ncvar = (ncvar[:] * units('Pa')).to('mbar')
print(ncvar.units)
millibar
# Let's take a look at the units again. They should be in millibars.
ncvar[:]