Skip to content

Instantly share code, notes, and snippets.

@schwehr
Created March 15, 2022 16:03
Show Gist options
  • Select an option

  • Save schwehr/70911066d82f47c4e7d0d0056dc3b97a to your computer and use it in GitHub Desktop.

Select an option

Save schwehr/70911066d82f47c4e7d0d0056dc3b97a to your computer and use it in GitHub Desktop.
HYCOM Velocity in Google Earth Engine
// Copyright 2019 Google LLC.
// SPDX-License-Identifier: Apache-2.0
//
// Hycom in Earth Engine demo by Kurt Schwehr
//
// https://code.earthengine.google.com/b475a38642624aca72fecafaa3f311ab
var extent = ee.Geometry.Polygon([-180, 80.48, -180, -80.48, 180, -80.48, 180, 80.48], null, false);
// var date_range = ee.Filter.date('2018-08-01', '2018-08-10');
//////////////////////////////////////////////////////////////
var velocity_collection = ee.ImageCollection('HYCOM/sea_water_velocity')
print('velocity_collection.size', velocity_collection.size())
var asset = '2017053000'
var velocity_uv = ee.Image('HYCOM/sea_water_velocity/' + asset)
var velocity_uv = velocity_uv.divide(1000) // scale to m/s
var u = velocity_uv.select(0)
var v = velocity_uv.select(1)
var depths_m = [
0, 2, 4, 6, 8, 10, 12, 15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100,
125, 150, 200, 250, 300, 350, 400, 500, 600, 700, 800, 900, 1000, 1250, 1500,
2000, 2500, 3000, 4000, 5000];
var before = [];
var after_v = []
var after_dir = []
for(var i = 0; i < depths_m.length; i++) {
before.push('velocity_u_' + depths_m[i]);
after_v.push('v_' + depths_m[i]);
after_dir.push('dir_' + depths_m[i]);
}
var u = velocity_uv.select(ee.List.sequence(0, 78, 2))
var v = velocity_uv.select(ee.List.sequence(1, 79, 2))
var speed = u.hypot(v).select(before, after_v)
var dir = u.atan2(v).multiply(180).divide(Math.PI).select(before, after_dir);
Map.addLayer(speed, {}, 'speed m/s', true)
Map.addLayer(dir, {}, 'dir deg', false)
var speed_minmax = speed.select(['v_0'], ['speed']).reduceRegion(
{reducer: ee.Reducer.minMax(), geometry: extent})
// print(speed_minmax)
print('speed minMax:', speed_minmax.get('speed_min'), speed_minmax.get('speed_max'));
print(ui.Chart.image.histogram(speed.select(['v_0'], ['speed_0']), extent, 10000, 40));
//////////////////////////////////////////////////////////////
var dataset = ee.ImageCollection('HYCOM/sea_temp_salinity');
// dataset = dataset.filter(date_range);
print(dataset.size())
var temp_salinity = ee.Image('HYCOM/sea_temp_salinity/' + asset)
// Regular expressions in select
var salinity = temp_salinity.select('salinity_.*').divide(1000).add(20)
var water_temp = temp_salinity.select('water_temp_.*').divide(1000).add(20)
// All depths
print('salinity stddev:', salinity.reduceRegion(
{reducer: ee.Reducer.stdDev(), geometry: extent, maxPixels: 1e9}));
print('temp stddev:', water_temp.reduceRegion(
{reducer: ee.Reducer.stdDev(), geometry: extent, maxPixels: 1e9}));
// One depth
print('salinity mean:', salinity.select(0).reduceRegion(
{reducer: ee.Reducer.mean(), geometry: extent}));
print('temp mean:', water_temp.select(0).reduceRegion(
{reducer: ee.Reducer.mean(), geometry: extent}));
var salinity_minmax = salinity.select(0).reduceRegion(
{reducer: ee.Reducer.minMax(), geometry: extent});
var water_temp_minmax = water_temp.select(0).reduceRegion(
{reducer: ee.Reducer.minMax(), geometry: extent})
print('salinity minMax:', salinity_minmax.get('salinity_0_min'), salinity_minmax.get('salinity_0_max'));
print('temp minMax:', water_temp_minmax.get('water_temp_0_min'), water_temp_minmax.get('water_temp_0_max'));
var visParamsSalinity = {
min: 31.0, // Could be as low as 0.5
max: 37.5, // Could be as high as 40.4
palette: ['000000', '005aff', '43c8c8', 'fff700', 'ff0000'],
};
var visParamsTemp = {
min: -2.2,
max: 33.9,
palette: ['000000', '005aff', '43c8c8', 'fff700', 'ff0000'],
};
Map.addLayer(salinity, {}, 'salinity psu', false);
Map.addLayer(salinity.select(0), visParamsSalinity, 'salinity_0 psu', true);
Map.addLayer(water_temp, {}, 'water_temp deg C', false);
Map.addLayer(water_temp.select(0), visParamsTemp, 'water_temp_0 deg C', true);
print(ui.Chart.image.histogram(salinity.select(0), extent, 10000, 40));
print(ui.Chart.image.histogram(water_temp.select(0), extent, 10000, 40));
//////////////////////////////////////////////////////////////
var sse = ee.Image('HYCOM/sea_surface_elevation/' + asset);
// Scale the data otherwise we would see nothing.
var terrain = ee.Terrain.products(sse.multiply(100));
// Print to discover what bands are in there.
// print('terrain', terrain);
Map.addLayer(terrain, {bands: ['hillshade']}, 'bands hillshade', false);
Map.addLayer(terrain.select('slope'), {}, 'slope', false);
Map.addLayer(terrain.select('aspect'), {}, 'aspect', false);
Map.addLayer(terrain.select('hillshade'), {}, 'hillshade');
sse = sse.divide(1000); // Scale to meters
print(ui.Chart.image.histogram(sse, extent, 10000, 40));
var sse_minmax = sse.reduceRegion(
{reducer: ee.Reducer.minMax(), geometry: extent, tileScale: 4})
print (sse_minmax)
print('sse minMax:', sse_minmax.get('surface_elevation_min'), sse_minmax.get('surface_elevation_max'));
var surfaceElevationVis = {
min: -1.8, // -2.1,
max: 1.3, // 2.5,
palette: ['blue', 'cyan', 'yellow', 'red'],
};
Map.addLayer(sse, surfaceElevationVis, 'Sea Surface Elevation');
// gdalinfo hycom_glby_930_2019052712_t018_ts3z.nc
// Driver: netCDF/Network Common Data Format
// Files: hycom_glby_930_2019052712_t018_ts3z.nc
// Size is 512, 512
// Coordinate System is `'
// Metadata:
// NC_GLOBAL#classification_authority=not applicable
// NC_GLOBAL#classification_level=UNCLASSIFIED
// NC_GLOBAL#Conventions=CF-1.6 NAVO_netcdf_v1.1
// NC_GLOBAL#distribution_statement=Approved for public release. Distribution unlimited.
// NC_GLOBAL#downgrade_date=not applicable
// NC_GLOBAL#field_type=instantaneous
// NC_GLOBAL#history=archv2ncdf3z
// NC_GLOBAL#institution=Fleet Numerical Meteorology and Oceanography Center
// NC_GLOBAL#source=HYCOM archive file
// Subdatasets:
// SUBDATASET_1_NAME=NETCDF:"hycom_glby_930_2019052712_t018_ts3z.nc":water_temp
// SUBDATASET_1_DESC=[1x40x4251x4500] sea_water_temperature (16-bit integer)
// SUBDATASET_2_NAME=NETCDF:"hycom_glby_930_2019052712_t018_ts3z.nc":water_temp_bottom
// SUBDATASET_2_DESC=[1x4251x4500] sea_water_temperature_at_bottom (16-bit integer)
// SUBDATASET_3_NAME=NETCDF:"hycom_glby_930_2019052712_t018_ts3z.nc":salinity
// SUBDATASET_3_DESC=[1x40x4251x4500] sea_water_salinity (16-bit integer)
// SUBDATASET_4_NAME=NETCDF:"hycom_glby_930_2019052712_t018_ts3z.nc":salinity_bottom
// SUBDATASET_4_DESC=[1x4251x4500] sea_water_salinity_at_bottom (16-bit integer)
// Corner Coordinates:
// Upper Left ( 0.0, 0.0)
// Lower Left ( 0.0, 512.0)
// Upper Right ( 512.0, 0.0)
// Lower Right ( 512.0, 512.0)
// Center ( 256.0, 256.0)
// gdalinfo NETCDF:hycom_glby_930_2019052712_t018_ts3z.nc:water_temp | head -75
// Driver: netCDF/Network Common Data Format
// Files: hycom_glby_930_2019052712_t018_ts3z.nc
// Size is 4500, 4251
// Coordinate System is `'
// Origin = (-0.040000004883898,90.019999999999996)
// Pixel Size = (0.080000009767796,-0.040000000000000)
// Metadata:
// depth#axis=Z
// depth#long_name=Depth
// depth#NAVO_code=5
// depth#positive=down
// depth#standard_name=depth
// depth#units=m
// lat#axis=Y
// lat#long_name=Latitude
// lat#NAVO_code=1
// lat#point_spacing=even
// lat#standard_name=latitude
// lat#units=degrees_north
// lon#axis=X
// lon#long_name=Longitude
// lon#modulo=360 degrees
// lon#NAVO_code=2
// lon#standard_name=longitude
// lon#units=degrees_east
// NC_GLOBAL#classification_authority=not applicable
// NC_GLOBAL#classification_level=UNCLASSIFIED
// NC_GLOBAL#Conventions=CF-1.6 NAVO_netcdf_v1.1
// NC_GLOBAL#distribution_statement=Approved for public release. Distribution unlimited.
// NC_GLOBAL#downgrade_date=not applicable
// NC_GLOBAL#field_type=instantaneous
// NC_GLOBAL#history=archv2ncdf3z
// NC_GLOBAL#institution=Fleet Numerical Meteorology and Oceanography Center
// NC_GLOBAL#source=HYCOM archive file
// NETCDF_DIM_depth_DEF={40,6}
// NETCDF_DIM_depth_VALUES={0,2,4,6,8,10,12,15,20,25,30,35,40,45,50,60,70,80,90,100,125,150,200,250,300,350,400,500,600,700,800,900,1000,1250,1500,2000,2500,3000,4000,5000}
// NETCDF_DIM_EXTRA={time,depth}
// NETCDF_DIM_time_DEF={1,6}
// NETCDF_DIM_time_VALUES=170094
// time#axis=T
// time#calendar=gregorian
// time#long_name=Valid Time
// time#NAVO_code=13
// time#time_origin=2000-01-01 00:00:00
// time#units=hours since 2000-01-01 00:00:00
// water_temp#add_offset=20
// water_temp#comment=in-situ temperature
// water_temp#long_name=Water Temperature
// water_temp#missing_value=-30000
// water_temp#NAVO_code=15
// water_temp#scale_factor=0.001
// water_temp#standard_name=sea_water_temperature
// water_temp#units=degC
// water_temp#_FillValue=-30000
// Corner Coordinates:
// Upper Left ( -0.0400000, 90.0200000)
// Lower Left ( -0.0400000, -80.0200000)
// Upper Right ( 359.960, 90.020)
// Lower Right ( 359.960, -80.020)
// Center ( 179.9600220, 5.0000000)
// Band 1 Block=4500x1 Type=Int16, ColorInterp=Undefined
// NoData Value=-30000
// Unit Type: degC
// Offset: 20, Scale:0.00100000004749745
// Metadata:
// add_offset=20
// comment=in-situ temperature
// long_name=Water Temperature
// missing_value=-30000
// NAVO_code=15
// NETCDF_DIM_depth=0
// NETCDF_DIM_time=170094
// NETCDF_VARNAME=water_temp
// scale_factor=0.001
// standard_name=sea_water_temperature
// (/usr/local/google/home/schwehr/hycom) Fri 09:49:22 (schwehr@schwehr2)
// Desktop $ gdalinfo NETCDF:hycom_glby_930_2019052712_t018_ts3z.nc:water_temp | head -90
// Driver: netCDF/Network Common Data Format
// Files: hycom_glby_930_2019052712_t018_ts3z.nc
// Size is 4500, 4251
// Coordinate System is `'
// Origin = (-0.040000004883898,90.019999999999996)
// Pixel Size = (0.080000009767796,-0.040000000000000)
// Metadata:
// depth#axis=Z
// depth#long_name=Depth
// depth#NAVO_code=5
// depth#positive=down
// depth#standard_name=depth
// depth#units=m
// lat#axis=Y
// lat#long_name=Latitude
// lat#NAVO_code=1
// lat#point_spacing=even
// lat#standard_name=latitude
// lat#units=degrees_north
// lon#axis=X
// lon#long_name=Longitude
// lon#modulo=360 degrees
// lon#NAVO_code=2
// lon#standard_name=longitude
// lon#units=degrees_east
// NC_GLOBAL#classification_authority=not applicable
// NC_GLOBAL#classification_level=UNCLASSIFIED
// NC_GLOBAL#Conventions=CF-1.6 NAVO_netcdf_v1.1
// NC_GLOBAL#distribution_statement=Approved for public release. Distribution unlimited.
// NC_GLOBAL#downgrade_date=not applicable
// NC_GLOBAL#field_type=instantaneous
// NC_GLOBAL#history=archv2ncdf3z
// NC_GLOBAL#institution=Fleet Numerical Meteorology and Oceanography Center
// NC_GLOBAL#source=HYCOM archive file
// NETCDF_DIM_depth_DEF={40,6}
// NETCDF_DIM_depth_VALUES={0,2,4,6,8,10,12,15,20,25,30,35,40,45,50,60,70,80,90,100,125,150,200,250,300,350,400,500,600,700,800,900,1000,1250,1500,2000,2500,3000,4000,5000}
// NETCDF_DIM_EXTRA={time,depth}
// NETCDF_DIM_time_DEF={1,6}
// NETCDF_DIM_time_VALUES=170094
// time#axis=T
// time#calendar=gregorian
// time#long_name=Valid Time
// time#NAVO_code=13
// time#time_origin=2000-01-01 00:00:00
// time#units=hours since 2000-01-01 00:00:00
// water_temp#add_offset=20
// water_temp#comment=in-situ temperature
// water_temp#long_name=Water Temperature
// water_temp#missing_value=-30000
// water_temp#NAVO_code=15
// water_temp#scale_factor=0.001
// water_temp#standard_name=sea_water_temperature
// water_temp#units=degC
// water_temp#_FillValue=-30000
// Corner Coordinates:
// Upper Left ( -0.0400000, 90.0200000)
// Lower Left ( -0.0400000, -80.0200000)
// Upper Right ( 359.960, 90.020)
// Lower Right ( 359.960, -80.020)
// Center ( 179.9600220, 5.0000000)
// Band 1 Block=4500x1 Type=Int16, ColorInterp=Undefined
// NoData Value=-30000
// Unit Type: degC
// Offset: 20, Scale:0.00100000004749745
// Metadata:
// add_offset=20
// comment=in-situ temperature
// long_name=Water Temperature
// missing_value=-30000
// NAVO_code=15
// NETCDF_DIM_depth=0
// NETCDF_DIM_time=170094
// NETCDF_VARNAME=water_temp
// scale_factor=0.001
// standard_name=sea_water_temperature
// units=degC
// _FillValue=-30000
// Band 2 Block=4500x1 Type=Int16, ColorInterp=Undefined
// gdalinfo NETCDF:hycom_glby_930_2019052712_t018_ts3z.nc:salinity | head -100
// Driver: netCDF/Network Common Data Format
// Files: hycom_glby_930_2019052712_t018_ts3z.nc
// Size is 4500, 4251
// Coordinate System is `'
// Origin = (-0.040000004883898,90.019999999999996)
// Pixel Size = (0.080000009767796,-0.040000000000000)
// Metadata:
// depth#axis=Z
// depth#long_name=Depth
// depth#NAVO_code=5
// depth#positive=down
// depth#standard_name=depth
// depth#units=m
// lat#axis=Y
// lat#long_name=Latitude
// lat#NAVO_code=1
// lat#point_spacing=even
// lat#standard_name=latitude
// lat#units=degrees_north
// lon#axis=X
// lon#long_name=Longitude
// lon#modulo=360 degrees
// lon#NAVO_code=2
// lon#standard_name=longitude
// lon#units=degrees_east
// NC_GLOBAL#classification_authority=not applicable
// NC_GLOBAL#classification_level=UNCLASSIFIED
// NC_GLOBAL#Conventions=CF-1.6 NAVO_netcdf_v1.1
// NC_GLOBAL#distribution_statement=Approved for public release. Distribution unlimited.
// NC_GLOBAL#downgrade_date=not applicable
// NC_GLOBAL#field_type=instantaneous
// NC_GLOBAL#history=archv2ncdf3z
// NC_GLOBAL#institution=Fleet Numerical Meteorology and Oceanography Center
// NC_GLOBAL#source=HYCOM archive file
// NETCDF_DIM_depth_DEF={40,6}
// NETCDF_DIM_depth_VALUES={0,2,4,6,8,10,12,15,20,25,30,35,40,45,50,60,70,80,90,100,125,150,200,250,300,350,400,500,600,700,800,900,1000,1250,1500,2000,2500,3000,4000,5000}
// NETCDF_DIM_EXTRA={time,depth}
// NETCDF_DIM_time_DEF={1,6}
// NETCDF_DIM_time_VALUES=170094
// salinity#add_offset=20
// salinity#long_name=Salinity
// salinity#missing_value=-30000
// salinity#NAVO_code=16
// salinity#scale_factor=0.001
// salinity#standard_name=sea_water_salinity
// salinity#units=psu
// salinity#_FillValue=-30000
// time#axis=T
// time#calendar=gregorian
// time#long_name=Valid Time
// time#NAVO_code=13
// time#time_origin=2000-01-01 00:00:00
// time#units=hours since 2000-01-01 00:00:00
// Corner Coordinates:
// Upper Left ( -0.0400000, 90.0200000)
// Lower Left ( -0.0400000, -80.0200000)
// Upper Right ( 359.960, 90.020)
// Lower Right ( 359.960, -80.020)
// Center ( 179.9600220, 5.0000000)
// Band 1 Block=4500x1 Type=Int16, ColorInterp=Undefined
// NoData Value=-30000
// Unit Type: psu
// Offset: 20, Scale:0.00100000004749745
// Metadata:
// add_offset=20
// long_name=Salinity
// missing_value=-30000
// NAVO_code=16
// NETCDF_DIM_depth=0
// NETCDF_DIM_time=170094
// NETCDF_VARNAME=salinity
// scale_factor=0.001
// standard_name=sea_water_salinity
// units=psu
// _FillValue=-30000
// Band 2 Block=4500x1 Type=Int16, ColorInterp=Undefined
// gdalinfo hycom_glby_930_2019052712_t030_ssh.nc
// Driver: netCDF/Network Common Data Format
// Files: hycom_glby_930_2019052712_t030_ssh.nc
// Size is 4500, 4251
// Coordinate System is `'
// Origin = (-0.040000004883898,90.019999999999996)
// Pixel Size = (0.080000009767796,-0.040000000000000)
// Metadata:
// lat#axis=Y
// lat#long_name=Latitude
// lat#NAVO_code=1
// lat#point_spacing=even
// lat#standard_name=latitude
// lat#units=degrees_north
// lon#axis=X
// lon#long_name=Longitude
// lon#modulo=360 degrees
// lon#NAVO_code=2
// lon#standard_name=longitude
// lon#units=degrees_east
// NC_GLOBAL#classification_authority=not applicable
// NC_GLOBAL#classification_level=UNCLASSIFIED
// NC_GLOBAL#comment=p-grid
// NC_GLOBAL#Conventions=CF-1.6 NAVO_netcdf_v1.1
// NC_GLOBAL#distribution_statement=Approved for public release. Distribution unlimited.
// NC_GLOBAL#downgrade_date=not applicable
// NC_GLOBAL#field_type=instantaneous
// NC_GLOBAL#history=archv2ncdf2d
// NC_GLOBAL#institution=Fleet Numerical Meteorology and Oceanography Center
// NC_GLOBAL#source=HYCOM archive file
// NETCDF_DIM_EXTRA={time}
// NETCDF_DIM_time_DEF={1,6}
// NETCDF_DIM_time_VALUES=170106
// surf_el#add_offset=0
// surf_el#long_name=Water Surface Elevation
// surf_el#missing_value=-30000
// surf_el#NAVO_code=32
// surf_el#scale_factor=0.001
// surf_el#standard_name=sea_surface_elevation
// surf_el#units=m
// surf_el#_FillValue=-30000
// time#axis=T
// time#calendar=gregorian
// time#long_name=Valid Time
// time#NAVO_code=13
// time#time_origin=2000-01-01 00:00:00
// time#units=hours since 2000-01-01 00:00:00
// Corner Coordinates:
// Upper Left ( -0.0400000, 90.0200000)
// Lower Left ( -0.0400000, -80.0200000)
// Upper Right ( 359.960, 90.020)
// Lower Right ( 359.960, -80.020)
// Center ( 179.9600220, 5.0000000)
// Band 1 Block=4500x1 Type=Int16, ColorInterp=Undefined
// NoData Value=-30000
// Unit Type: m
// Offset: 0, Scale:0.00100000004749745
// Metadata:
// add_offset=0
// long_name=Water Surface Elevation
// missing_value=-30000
// NAVO_code=32
// NETCDF_DIM_time=170106
// NETCDF_VARNAME=surf_el
// scale_factor=0.001
// standard_name=sea_surface_elevation
// units=m
// _FillValue=-30000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment