Calculate speed and write to NetCDF using Xray and Iris


In [1]:
url = 'http://geoport.whoi.edu/thredds/dodsC/usgs/data2/emontgomery/stellwagen/CF-1.6/RCNWR/9541aqd-cal.nc'

In [2]:
def calc_speed(u, v):
    """Calculate the speed"""
    speed = (u**2 + v**2)**0.5
    return speed

1. Xray


In [3]:
import xray

In [4]:
ds = xray.open_dataset(url)

In [5]:
# return varaibles matching standard name as a dict
def get_std_name_vars(ds,sn):
    return {k: v for k, v in ds.data_vars.iteritems() if 'standard_name' in v.attrs.keys() and sn in v.standard_name}

In [6]:
sn = 'eastward_sea_water_velocity'
u = get_std_name_vars(ds,sn=sn)
sn = 'northward_sea_water_velocity'
v = get_std_name_vars(ds,sn=sn)

In [7]:
# convert to list and extract value from first key,value pair
u = list(u.values())[0]
v = list(v.values())[0]

In [8]:
spData = calc_speed(u,v)

# copy variable attribute data from 'u' variable
spData.attrs = u.attrs

# modify specific variable attribute data
spData.attrs['standard_name']='sea_water_speed'

In [9]:
ds_out = xray.Dataset({'speed': spData})

In [10]:
ds_out.attrs = ds.attrs

In [11]:
ds_out.to_netcdf('speed_xray.nc')

In [12]:
!ncdump -h speed_xray.nc


netcdf speed_xray {
dimensions:
	z = 8 ;
	time = 17425 ;
variables:
	double latitude ;
		string latitude:units = "degrees_north" ;
		string latitude:standard_name = "latitude" ;
		string latitude:long_name = "sensor latitude" ;
	double z(z) ;
		z:_FillValue = -9999.9 ;
		string z:grid_mapping = "crs" ;
		string z:long_name = "z of the sensor relative to the water surface" ;
		string z:standard_name = "height" ;
		string z:positive = "up" ;
		string z:units = "m" ;
		string z:axis = "Z" ;
	double speed(time, z) ;
		speed:epic_code = 1205 ;
		string speed:feneric_name = "u" ;
		string speed:name = "u_1205" ;
		speed:standard_name = "sea_water_speed" ;
		speed:sensor_depth = -2.5796268716204 ;
		speed:actual_max = 168.2529f ;
		speed:initial_sensor_height = 0.25 ;
		string speed:FORTRAN_format = " " ;
		string speed:long_name = "Eastward (u) velocity" ;
		string speed:sensor_type = "Nortek Aquadopp Profiler" ;
		speed:actual_min = -63.2215f ;
		string speed:units = "m/s" ;
		string speed:serial_number = "AQD 5379" ;
		string speed:height_depth_units = "m" ;
		string speed:grid_mapping = "crs" ;
		speed:_ChunkSize = 1000, 8 ;
	double longitude ;
		string longitude:units = "degrees_east" ;
		string longitude:standard_name = "longitude" ;
		string longitude:long_name = "sensor longitude" ;
	double time(time) ;
		string time:standard_name = "time" ;
		string time:long_name = "time of measurement" ;
		time:_ChunkSize = 1000 ;
		string time:units = "seconds since 1970-01-01T00:00:00+00:00" ;
		string time:calendar = "gregorian" ;

// global attributes:
		string :project_summary = "Oceanographic and water-quality observations made at 3 sites in the wetlands of the Rachel Carson National Wildlife Refuge." ;
		:AQDNumberOfPingsPerBurst = 6. ;
		:AQDCompassUpdateRate = 600. ;
		string :DELTA_T = "600" ;
		:DEPTH_CONST = 0. ;
		:AQDSoftwareVersion = 1.35 ;
		string :contributor_role = "principalInvestigator" ;
		string :keywords = "Oceans > Ocean Pressure > Water Pressure, Oceans > Ocean Temperature > Water Temperature, Oceans > Salinity/Density > Conductivity, Oceans > Salinity/Density > Salinity" ;
		string :Recovery_date = "9-Dec-2013" ;
		string :title = "RCNWR - 9541aqd-cal.nc" ;
		:blanking_distance = 0.4 ;
		:bin_count = 8. ;
		string :platform = "I-mount " ;
		string :AQDSyncOutDelay = "0 sec" ;
		string :AQDAnalogInput1 = "NONE" ;
		string :AQDAnalogInput2 = "NONE" ;
		string :stop_time = "26-July-2013 01:30:59" ;
		string :zeroed_pressure = "Yes" ;
		string :AQDRecorderSize = "361 MByte" ;
		string :start_time = "03-Mar-2013 13:31:00" ;
		string :fileRoot = "9541aqdoldnc" ;
		:bin_size = 0.5 ;
		string :nominal_sensor_depth_note = "WATER_DEPTH-initial_instrument_height" ;
		string :DATA_CMNT = "early termination on 7/26 due to instrument failure" ;
		string :AQDSalinity = "30.0 ppt" ;
		string :EXPERIMENT = "Rachel Carson National Wildlife Refuge 2013" ;
		:AQDFrequency = 1000. ;
		:fixgaps = 1. ;
		string :WATER_MASS = "?" ;
		string :WATER_DEPTH_NOTE = "(meters), nominal depth" ;
		string :publisher_email = "emontgomery@usgs.gov" ;
		:AQDTempRange = -4., 40. ;
		:frequency = 1000. ;
		:longitude = -70.58675 ;
		:nominal_sensor_depth = 2.5796268716204 ;
		string :metadatafile_version = "0.0" ;
		string :AQDSoundSpeed = "MEASURED" ;
		string :DATA_ORIGIN = "USGS WHSC Sed Trans Group" ;
		string :project = "Coastal and Marine Geology Program" ;
		string :Deployment_date = "27-Mar-2013" ;
		string :original_folder = "RCNWR" ;
		:AQDBlankingDistance = 0.4 ;
		:beam_width = 3.4 ;
		:magnetic_variation = 99. ;
		string :initial_instrument_height_note = "from mooring log" ;
		string :keywords_vocabulary = "GCMD Science Keywords" ;
		string :publisher_phone = "+1 (508) 548-8700" ;
		string :AQDSyncPowerDelay = "0 sec" ;
		string :WaveMeasurements = "DISABLED" ;
		:POS_CONST = 0. ;
		:AQDNumberOfCells = 8. ;
		string :AQDAnalogPowerOutput = "BATTERY" ;
		string :contributor_name = "N. Ganju" ;
		:COMPOSITE = 0. ;
		:AQDBeamAngle = 25. ;
		string :WATER_DEPTH_source = "Water depth determined by mean pressure value" ;
		string :original_filename = "9541aqd-cal.nc" ;
		:transducer_offset_from_bottom = 0.25 ;
		:AQDNumberOfBeams = 3. ;
		:AQDPressRange = 0., 100. ;
		string :standard_name_vocabulary = "CF-1.6" ;
		string :INST_TYPE = "Nortek Aquadopp Profiler" ;
		:beam_angle = 25. ;
		string :DATA_SUBTYPE = "MOORED" ;
		string :source = "USGS" ;
		:AQDBeamWidth = 3.4 ;
		:AQDMeasurementLoad = 75. ;
		string :AQDDeploymentTime = "3/27/2013" ;
		string :serial_number = "ASP 3795" ;
		string :AQDCoordinateSystem = "BEAM" ;
		:AQDTransMatrix = 1.5774, 0., 0.3677, -0.7891, -1.3662, 0.3677, -0.7891, 1.3662, 0.3677 ;
		string :metadatafile_author = "Ellyn Montgomery, USGS CMGP" ;
		string :creator_email = "rsignell@usgs.gov" ;
		string :creator_url = "http://www.usgs.gov" ;
		string :AQDHeadRotation = "horizontal" ;
		:latitude = 43.27374 ;
		:AQDAnalogInputCal2 = -1471., 8826. ;
		string :WATER_DEPTH_datum = "MSL" ;
		:WATER_DEPTH = 2.8296268716204 ;
		string :institution = "USGS Woods Hole Coastal and Marine Science Center" ;
		:MOORING = 954 ;
		string :institution_url = "http://woodshole.er.usgs.gov" ;
		string :AQDHeadSerialNumber = "ASP 3795" ;
		string :project_title = "Rachel Carson NWR 2013" ;
		string :salinity_set_by_user = "30.0 ppt" ;
		string :salinity_set_by_user_units = "ppt" ;
		string :creator_phone = "+1 (508) 548-8700" ;
		string :platform_type = "I frame bottom mount" ;
		string :metadatafile_name = "emrun954AQDprfoldnc" ;
		:AQDVelRange = 1000. ;
		string :history = "Data cropped by user defined ensemble numbers;Data rotated into Earth Coordinates by aqdprf2nc SVN $Revision: 3846 $ using a magnetic variation of 99.00; Amplitude of 30.000 counts used for data screening; Data trimmed at the surface using pressure data;: " ;
		string :instrument_type = "Nortek Aquadopp Profiler" ;
		string :beam_pattern = "convex" ;
		string :orientation = "UP" ;
		string :AQDAnalogPowerLevel = "LOW" ;
		string :AQDSerial_Number = "AQD 5379" ;
		string :publisher_name = "Ellyn Montgomery" ;
		string :id = "9541aqd-cal" ;
		:AQDCellSize = 50. ;
		string :LatLonDatum = "NAD83" ;
		:center_first_bin = 0.9 ;
		:DRIFTER = 0. ;
		:AQDProfileInterval = 600. ;
		:inst_height = 0.25 ;
		string :publisher_url = "http://www.usgs.gov" ;
		:initial_instrument_height = 0.25 ;
		:AQDAverageInterval = 120. ;
		string :SciPi = "N. Ganju" ;
		string :naming_authority = "gov.usgs.cmgp" ;
		:ClockError = 0. ;
		:AQDPressureCal = 0., 0., 3753., 12332. ;
		string :PROJECT = "USGS Coastal Marine Geology Program" ;
		:pred_accuracy = 4.8 ;
		string :AQDBeamPattern = "convex" ;
		string :DESCRIPTION = "Under Bourne Ave. Bridge" ;
		string :creator_name = "Rich Signell" ;
		:AQDAnalogInputCal1 = -1471., 8826. ;
		string :AQDDeploymentName = "954aqd" ;
		string :COORD_SYSTEM = "GEOGRAPHIC" ;
		string :AQDTransmitPulseLength = "0.50 m" ;
		string :AQDFirmwareVersion = "3.38" ;
		:magnetic_variation_applied = 99. ;
		string :Conventions = "CF-1.6" ;
		string :date_created = "2015-08-12T20:39:00Z" ;
		:geospatial_lat_min = 43.27374f ;
		:geospatial_lat_max = 43.27374f ;
		string :time_coverage_start = "2013-03-27T01:31:00" ;
		string :time_coverage_end = "2013-07-26T01:30:59" ;
		string :time_coverage_duration = "P10454399S" ;
		string :time_coverage_resolution = "P600S" ;
		string :featureType = "timeSeriesProfile" ;
		string :geospatial_vertical_positive = "up" ;
		:geospatial_vertical_min = -2.1596268716204 ;
		:geospatial_vertical_max = 1.3403731283796 ;
		string :geospatial_vertical_resolution = "0.5 0.5 0.5 0.5 0.5 0.5 0.5" ;
		:geospatial_lon_min = -70.58675f ;
		:geospatial_lon_max = -70.58675f ;
		:DODS.strlen = 11 ;
		string :DODS.dimName = "feature_type_instance" ;
		string :DODS_EXTRA.Unlimited_Dimension = "time" ;
		:coordinates = "latitude longitude" ;
}

2. Iris


In [13]:
import iris

In [14]:
u = iris.load_cube(url,'eastward_sea_water_velocity')
v = iris.load_cube(url,'northward_sea_water_velocity')

In [25]:
spData = calc_speed(u[:],v[:])

In [26]:
spData.attributes = u.attributes

In [28]:
spData.var_name = 'speed'

In [30]:
spData.standard_name = 'sea_water_speed'

In [31]:
iris.FUTURE.netcdf_no_unlimited=True
iris.save(spData,'speed_iris.nc')

In [32]:
!ncdump -h speed_iris.nc


netcdf speed_iris {
dimensions:
	time = 17425 ;
	z = 8 ;
variables:
	double speed(time, z) ;
		speed:standard_name = "sea_water_speed" ;
		speed:units = "meter-second^-1" ;
		speed:grid_mapping = "latitude_longitude" ;
		speed:coordinates = "latitude longitude" ;
	int latitude_longitude ;
		latitude_longitude:grid_mapping_name = "latitude_longitude" ;
		latitude_longitude:longitude_of_prime_meridian = 0. ;
		latitude_longitude:semi_major_axis = 6378137. ;
		latitude_longitude:semi_minor_axis = 6356752.31424518 ;
	double time(time) ;
		time:axis = "T" ;
		time:units = "seconds since 1970-01-01T00:00:00Z" ;
		string time:standard_name = "time" ;
		string time:long_name = "time of measurement" ;
		string time:calendar = "gregorian" ;
		time:_ChunkSize = 1000 ;
	double z(z) ;
		z:axis = "Z" ;
		z:units = "m" ;
		string z:standard_name = "height" ;
		string z:long_name = "z of the sensor relative to the water surface" ;
		z:positive = "up" ;
	double latitude ;
		latitude:units = "degrees_north" ;
		string latitude:standard_name = "latitude" ;
		string latitude:long_name = "sensor latitude" ;
	double longitude ;
		longitude:units = "degrees_east" ;
		string longitude:standard_name = "longitude" ;
		string longitude:long_name = "sensor longitude" ;

// global attributes:
		:AQDAnalogInput1 = "NONE" ;
		:AQDAnalogInput2 = "NONE" ;
		:AQDAnalogInputCal1 = -1471., 8826. ;
		:AQDAnalogInputCal2 = -1471., 8826. ;
		:AQDAnalogPowerLevel = "LOW" ;
		:AQDAnalogPowerOutput = "BATTERY" ;
		:AQDAverageInterval = 120. ;
		:AQDBeamAngle = 25. ;
		:AQDBeamPattern = "convex" ;
		:AQDBeamWidth = 3.4 ;
		:AQDBlankingDistance = 0.4 ;
		:AQDCellSize = 50. ;
		:AQDCompassUpdateRate = 600. ;
		:AQDCoordinateSystem = "BEAM" ;
		:AQDDeploymentName = "954aqd" ;
		:AQDDeploymentTime = "3/27/2013" ;
		:AQDFirmwareVersion = "3.38" ;
		:AQDFrequency = 1000. ;
		:AQDHeadRotation = "horizontal" ;
		:AQDHeadSerialNumber = "ASP 3795" ;
		:AQDMeasurementLoad = 75. ;
		:AQDNumberOfBeams = 3. ;
		:AQDNumberOfCells = 8. ;
		:AQDNumberOfPingsPerBurst = 6. ;
		:AQDPressRange = 0., 100. ;
		:AQDPressureCal = 0., 0., 3753., 12332. ;
		:AQDProfileInterval = 600. ;
		:AQDRecorderSize = "361 MByte" ;
		:AQDSalinity = "30.0 ppt" ;
		:AQDSerial_Number = "AQD 5379" ;
		:AQDSoftwareVersion = 1.35 ;
		:AQDSoundSpeed = "MEASURED" ;
		:AQDSyncOutDelay = "0 sec" ;
		:AQDSyncPowerDelay = "0 sec" ;
		:AQDTempRange = -4., 40. ;
		:AQDTransMatrix = 1.5774, 0., 0.3677, -0.7891, -1.3662, 0.3677, -0.7891, 1.3662, 0.3677 ;
		:AQDTransmitPulseLength = "0.50 m" ;
		:AQDVelRange = 1000. ;
		:COMPOSITE = 0. ;
		:COORD_SYSTEM = "GEOGRAPHIC" ;
		:ClockError = 0. ;
		:DATA_CMNT = "early termination on 7/26 due to instrument failure" ;
		:DATA_ORIGIN = "USGS WHSC Sed Trans Group" ;
		:DATA_SUBTYPE = "MOORED" ;
		:DELTA_T = "600" ;
		:DEPTH_CONST = 0. ;
		:DESCRIPTION = "Under Bourne Ave. Bridge" ;
		:DODS.dimName = "feature_type_instance" ;
		:DODS.strlen = 11 ;
		:DODS_EXTRA.Unlimited_Dimension = "time" ;
		:DRIFTER = 0. ;
		:Deployment_date = "27-Mar-2013" ;
		:EXPERIMENT = "Rachel Carson National Wildlife Refuge 2013" ;
		:FORTRAN_format = " " ;
		:INST_TYPE = "Nortek Aquadopp Profiler" ;
		:LatLonDatum = "NAD83" ;
		:MOORING = 954 ;
		:POS_CONST = 0. ;
		:PROJECT = "USGS Coastal Marine Geology Program" ;
		:Recovery_date = "9-Dec-2013" ;
		:SciPi = "N. Ganju" ;
		:WATER_DEPTH = 2.8296268716204 ;
		:WATER_DEPTH_NOTE = "(meters), nominal depth" ;
		:WATER_DEPTH_datum = "MSL" ;
		:WATER_DEPTH_source = "Water depth determined by mean pressure value" ;
		:WATER_MASS = "?" ;
		:WaveMeasurements = "DISABLED" ;
		:_ChunkSize = 1000, 8 ;
		:actual_max = 168.2529f ;
		:actual_min = -63.2215f ;
		:beam_angle = 25. ;
		:beam_pattern = "convex" ;
		:beam_width = 3.4 ;
		:bin_count = 8. ;
		:bin_size = 0.5 ;
		:blanking_distance = 0.4 ;
		:center_first_bin = 0.9 ;
		:contributor_name = "N. Ganju" ;
		:contributor_role = "principalInvestigator" ;
		:creator_email = "rsignell@usgs.gov" ;
		:creator_name = "Rich Signell" ;
		:creator_phone = "+1 (508) 548-8700" ;
		:creator_url = "http://www.usgs.gov" ;
		:date_created = "2015-08-12T20:39:00Z" ;
		:epic_code = 1205 ;
		:featureType = "timeSeriesProfile" ;
		:feneric_name = "u" ;
		:fileRoot = "9541aqdoldnc" ;
		:fixgaps = 1. ;
		:frequency = 1000. ;
		:geospatial_lat_max = 43.27374f ;
		:geospatial_lat_min = 43.27374f ;
		:geospatial_lon_max = -70.58675f ;
		:geospatial_lon_min = -70.58675f ;
		:geospatial_vertical_max = 1.3403731283796 ;
		:geospatial_vertical_min = -2.1596268716204 ;
		:geospatial_vertical_positive = "up" ;
		:geospatial_vertical_resolution = "0.5 0.5 0.5 0.5 0.5 0.5 0.5" ;
		:height_depth_units = "m" ;
		:history = "Data cropped by user defined ensemble numbers;Data rotated into Earth Coordinates by aqdprf2nc SVN $Revision: 3846 $ using a magnetic variation of 99.00; Amplitude of 30.000 counts used for data screening; Data trimmed at the surface using pressure data;: " ;
		:id = "9541aqd-cal" ;
		:initial_instrument_height = 0.25 ;
		:initial_instrument_height_note = "from mooring log" ;
		:initial_sensor_height = 0.25 ;
		:inst_height = 0.25 ;
		:institution = "USGS Woods Hole Coastal and Marine Science Center" ;
		:institution_url = "http://woodshole.er.usgs.gov" ;
		:instrument_type = "Nortek Aquadopp Profiler" ;
		:keywords = "Oceans > Ocean Pressure > Water Pressure, Oceans > Ocean Temperature > Water Temperature, Oceans > Salinity/Density > Conductivity, Oceans > Salinity/Density > Salinity" ;
		:keywords_vocabulary = "GCMD Science Keywords" ;
		:latitude = 43.27374 ;
		:longitude = -70.58675 ;
		:magnetic_variation = 99. ;
		:magnetic_variation_applied = 99. ;
		:metadatafile_author = "Ellyn Montgomery, USGS CMGP" ;
		:metadatafile_name = "emrun954AQDprfoldnc" ;
		:metadatafile_version = "0.0" ;
		:name = "u_1205" ;
		:naming_authority = "gov.usgs.cmgp" ;
		:nominal_sensor_depth = 2.5796268716204 ;
		:nominal_sensor_depth_note = "WATER_DEPTH-initial_instrument_height" ;
		:orientation = "UP" ;
		:original_filename = "9541aqd-cal.nc" ;
		:original_folder = "RCNWR" ;
		:platform = "I-mount " ;
		:platform_type = "I frame bottom mount" ;
		:pred_accuracy = 4.8 ;
		:project = "Coastal and Marine Geology Program" ;
		:project_summary = "Oceanographic and water-quality observations made at 3 sites in the wetlands of the Rachel Carson National Wildlife Refuge." ;
		:project_title = "Rachel Carson NWR 2013" ;
		:publisher_email = "emontgomery@usgs.gov" ;
		:publisher_name = "Ellyn Montgomery" ;
		:publisher_phone = "+1 (508) 548-8700" ;
		:publisher_url = "http://www.usgs.gov" ;
		:salinity_set_by_user = "30.0 ppt" ;
		:salinity_set_by_user_units = "ppt" ;
		:sensor_depth = -2.5796268716204 ;
		:sensor_type = "Nortek Aquadopp Profiler" ;
		:serial_number = "AQD 5379" ;
		:source = "USGS" ;
		:standard_name_vocabulary = "CF-1.6" ;
		:start_time = "03-Mar-2013 13:31:00" ;
		:stop_time = "26-July-2013 01:30:59" ;
		:time_coverage_duration = "P10454399S" ;
		:time_coverage_end = "2013-07-26T01:30:59" ;
		:time_coverage_resolution = "P600S" ;
		:time_coverage_start = "2013-03-27T01:31:00" ;
		:title = "RCNWR - 9541aqd-cal.nc" ;
		:transducer_offset_from_bottom = 0.25 ;
		:zeroed_pressure = "Yes" ;
		:Conventions = "CF-1.5" ;
}

In [17]: