Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Suggestion] easier creation of time axis #208

Closed
ryofurue opened this issue Jun 15, 2023 · 4 comments
Closed

[Suggestion] easier creation of time axis #208

ryofurue opened this issue Jun 15, 2023 · 4 comments

Comments

@ryofurue
Copy link

Currently, it's a bit tedious to create a time axis for a new dataset. It would be nice if you could do something like

tax = DateTime(1990, 1, 1):Dates.Day(1):DateTime(2020,1,1)
NCDataset("newfile.nc"; cfconvention=true) do nc
  defDim(nc, "time", length(tax))
  v = defVar(nc, "time", ("time", ); like = tax) #←Asks to use `tax` to determine attributes
  v[:] = tax
  # ⇒ units = "days since yyyy-mm-dd HH:MM:SS" be automatically set.
  #      axis = "T" be automatically set.
end

Here, I expect that the NCDataset library would automatically choose an underlying data type (likely Float64) and an appropriate time origin and other standard and helpful attributes.

@Alexander-Barth
Copy link
Owner

This already works:

tax = DateTime(1990, 1, 1):Dates.Day(1):DateTime(2020,1,1)
NCDataset("newfile.nc", "c") do ds
  defVar(ds,"time",tax,("time",))
end

Essentially you give the data to defVar rather than just the type.

Dataset: newfile.nc
Group: /

Dimensions
   time = 10958

Variables
  time   (10958)
    Datatype:    DateTime (Float64)
    Dimensions:  time
    Attributes:
     units                = days since 1900-01-01 00:00:00

@ryofurue
Copy link
Author

ryofurue commented Jun 17, 2023

[Edit: a typo has been fixed.] Wow, that's nice! Very very nice!

The tutorial gives the impression that you need to define a dimension (defDim) before defining the corresponding dimension variable (defVar). That's the case for the original netCDF library.

But, NCDatasets library just internally defDims when a non-existent dimension is specified in defVar. Is that right?

So, basically you do away with defDim altogether!

xax = 0:2:358 # longitudes
tax = DateTime(1990, 1, 1):Dates.Day(1):DateTime(2020,1,1)
myvar = get_var()
NCDataset("newfile.nc", "c") do ds
  defVar(ds,"time",tax,("time",))
  defVar(ds,"lon",xax,("lon",))
  ds["lon"].attrib["units"] = "degrees_east"
  defVar(ds,"myvar", myvar, ("lon", "time"))
end

Alexander-Barth added a commit that referenced this issue Jun 19, 2023
@Alexander-Barth
Copy link
Owner

Yes, this is correct.
I added this to the README.md
295a715

Note that you can also define the attributes with defVar (see the examples):
https://alexander-barth.github.io/NCDatasets.jl/stable/variables/#CommonDataModel.defVar

@ryofurue
Copy link
Author

With this, defDim() is necessary only when you need a dimension without coordinate values. Perhaps you may want the "ensemble" dimension to be like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants