Skip to content

Instantly share code, notes, and snippets.

@dblodgett-usgs
Created August 22, 2018 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dblodgett-usgs/7732ed8cb54f6b389bc88db6e7c7b998 to your computer and use it in GitHub Desktop.
Save dblodgett-usgs/7732ed8cb54f6b389bc88db6e7c7b998 to your computer and use it in GitHub Desktop.
Create ncdfgeom sample file
devtools::install_github("USGS-R/ncdfgeom")
# Also need jsonlite and sf but devtools should install with ncdfgeom
fixtureData <- jsonlite::fromJSON(system.file("extdata/fixture_wkt.json",
package = "ncdfgeom"))
polygon <- sf::st_sf(geom = sf::st_as_sfc(fixtureData$`2d`$polygon),
crs = "+init=epsg:4326")
lon_lat <- sf::st_coordinates(sf::st_centroid(polygon))
data_length <- 9
seed <- seq(0, 2*pi, length.out = data_length)
times <- seq(as.POSIXct("1970-01-01 00:00", tz = "gmt"), by = "day", length.out = data_length)
ts <- data.frame(i = sin(seed))
dm <- list(name="sin",
long_name="the sin function")
ts2 <- data.frame(i = cos(seed))
dm2 <- list(name = "cos",
long_name = "the cos function")
ncdfgeom::write_timeseries_dsg("polygon.nc",
instance_names = "i",
lats = lon_lat[,1],
lons = lon_lat[,2],
times = times,
data = ts,
data_unit = "unitless",
data_metadata = dm,
attributes = c(title = "title",
abstract = "very abstract",
provider_site = "CIDA",
provider_name ="The Blodg",
description = "A sample file"))
ncdfgeom::write_timeseries_dsg("polygon.nc",
instance_names = "i",
lats = lon_lat[,1],
lons = lon_lat[,2],
times = times,
data = ts2,
data_unit = "unitless",
data_metadata = dm2,
add_to_existing = TRUE)
@dblodgett-usgs
Copy link
Author

Output:

ncdump polygon.nc 
netcdf polygon {
dimensions:
	instance = 1 ;
	time = 9 ;
	name_strlen = 1 ;
variables:
	double lat(instance) ;
		lat:units = "degrees_north" ;
		lat:_FillValue = -999. ;
		lat:long_name = "latitude of the observation" ;
		lat:standard_name = "latitude" ;
	double lon(instance) ;
		lon:units = "degrees_east" ;
		lon:_FillValue = -999. ;
		lon:long_name = "longitude of the observation" ;
		lon:standard_name = "longitude" ;
	double time(time) ;
		time:units = "days since 1970-01-01 00:00:00" ;
		time:_FillValue = -999. ;
		time:long_name = "time of measurement" ;
		time:standard_name = "time" ;
	char instance_name(instance, name_strlen) ;
		instance_name:long_name = "Station Names" ;
		instance_name:cf_role = "timeseries_id" ;
	double sin(instance, time) ;
		sin:units = "unitless" ;
		sin:_FillValue = -999. ;
		sin:long_name = "the sin function" ;
		sin:coordinates = "time lat lon" ;
	double cos(instance, time) ;
		cos:units = "unitless" ;
		cos:_FillValue = -999. ;
		cos:long_name = "the cos function" ;
		cos:coordinates = "time lat lon" ;

// global attributes:
		:Conventions = "CF-1.8" ;
		:featureType = "timeSeries" ;
		:cdm_data_type = "Station" ;
		:standard_name_vocabulary = "CF-1.8" ;
		:title = "title" ;
		:abstract = "very abstract" ;
		:provider_site = "CIDA" ;
		:provider_name = "The Blodg" ;
		:description = "A sample file" ;
data:

 lat = 25.4545454545455 ;

 lon = 26.969696969697 ;

 time = 0, 1, 2, 3, 4, 5, 6, 7, 8 ;

 instance_name =
  "i" ;

 sin =
  0, 0.707106781186547, 1, 0.707106781186548, 1.22464679914735e-16, 
    -0.707106781186547, -1, -0.707106781186548, -2.44929359829471e-16 ;

 cos =
  1, 0.707106781186548, 6.12323399573677e-17, -0.707106781186547, -1, 
    -0.707106781186548, -1.83697019872103e-16, 0.707106781186547, 1 ;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment