# How to save a vector into NetCDF?

**URL:** <https://discourse.julialang.org/t/how-to-save-a-vector-into-netcdf/81943>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 31, 2022, 12:00am UTC](https://discourse.julialang.org/t/how-to-save-a-vector-into-netcdf/81943 "2022-05-31T00:00:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Leon6](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leon6/32/205483_2.png) [@Leon6](https://discourse.julialang.org/u/Leon6)\
**Post date:** [May 31, 2022, 12:00am UTC](https://discourse.julialang.org/t/how-to-save-a-vector-into-netcdf/81943/1 "2022-05-31T00:00:51Z")

</div>

I’m trying to use NCDatasets.jl to create a NetCDF file. I wonder if anyone could show me how to save a vector into a NetCDF variable?

I have a vector D as below:  
`D = fill(2022,(18,));`

Right now, the only way I could make it work is as below:

```julia
defDim(ds, "year", 18);
defDim(ds, "col", 1);
v1 = defVar(ds, "year", Int64, ("year", "col"));
v1[:,1] = D;

```

Basically, I had to create a useless `col` dimension. How do I do this without defining it? Many thanks

---

<div class="post-metadata">

**Author:** ![adigitoleo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adigitoleo/32/27478_2.png) [@adigitoleo](https://discourse.julialang.org/u/adigitoleo)\
**Post date:** [May 31, 2022, 1:14am UTC](https://discourse.julialang.org/t/how-to-save-a-vector-into-netcdf/81943/2 "2022-05-31T01:14:29Z")

</div>

The NetCDF format is generally used for data that is dimensional, i.e f(lat, lon) or similar. I’m not sure if it is possible to save just a dimensionless vector to NetCDF. In general when writing NetCDF files I recommend to follow the [CF conventions](http://cfconventions.org/Data/cf-conventions/cf-conventions-1.9/cf-conventions.html). It might seem a bit long, but they do have some nice conventions and then your files will be easily readable in various software. I recently registered [PlateMotionRequests.jl](https://juliahub.com/ui/Packages/PlateMotionRequests/qtCGU/3.1.1) where I have some code to create simple NetCDF files. See [the source](https://git.sr.ht/~adigitoleo/PlateMotionRequests.jl/tree/main/item/src/PlateMotionRequests.jl#L312), in particular `write_netcdf` which creates the NetCDF dataset from a [Table](https://github.com/JuliaData/TypedTables.jl). I don’t know how to write irregular data to NetCDF yet.

---

<div class="post-metadata">

**Author:** ![Leon6](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leon6/32/205483_2.png) [@Leon6](https://discourse.julialang.org/u/Leon6)\
**Post date:** [May 31, 2022, 1:17am UTC](https://discourse.julialang.org/t/how-to-save-a-vector-into-netcdf/81943/3 "2022-05-31T01:17:19Z")

</div>

Thanks for the reply, and the useful information! I’m able to figure it out myself. Below was how I did it:

```julia
defDim(ds, "year", 18);
v1 = defVar(ds, "year", Int64, ("year", ));
v1[:] = D;

```

The trick is the magic comma. Without it, it won’t work.

---

<div class="post-metadata">

**Author:** ![adigitoleo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adigitoleo/32/27478_2.png) [@adigitoleo](https://discourse.julialang.org/u/adigitoleo)\
**Post date:** [May 31, 2022, 2:53am UTC](https://discourse.julialang.org/t/how-to-save-a-vector-into-netcdf/81943/4 "2022-05-31T02:53:07Z")

</div>

aha! thanks forr sharing, good to know that it is possible after all.
