0

The code is this answer does work Add a new dimension but it does not do what I want it to do.

To illustrate my problem further - I have the following definitions Time(unlimited), latitude, longitude, level as four dimensions and one variable geopotential height. What I want to do is that add a dimension dimz_Z to the existing variable geopotential height.

Right now with the answer in the code above here is how the netCDF file looks like with a ncdump

float hgt(time, level, lat, lon) 

But the dimz_Z dimension is missing as seen below from the output of ncks.

hgt dimension 0: time, size = 1 NC_DOUBLE, chunksize = 1 (Record coordinate is time)
hgt dimension 1: level, size = 1 NC_FLOAT, chunksize = 1 (Coordinate is level)
hgt dimension 2: lat, size = 23 NC_FLOAT, chunksize = 23 (Coordinate is lat)
hgt dimension 3: lon, size = 21 NC_FLOAT, chunksize = 21 (Coordinate is lon)

It does show up in the definitions but as a stand-alone dimension and not part of the dimension of hgt

 dimz_Z: type NC_FLOAT, 1 dimension, 0 attributes, compressed? no, chunked? no, packed? no
 dimz_Z size (RAM) = 483*sizeof(NC_FLOAT) = 483*4 = 1932 bytes
 dimz_Z dimension 0: dimz_Z, size = 483 NC_FLOAT (Coordinate is dimz_Z)

This is the output of the file

time[0]=1879032 level[0]=1000 lat[0]=60 lon[0]=50 hgt[0]=53 m
time[0]=1879032 level[0]=1000 lat[0]=60 lon[1]=52.5 hgt[1]=55 m

What I want is the following

time[0]=1879032 level[0]=1000 lat[0]=60 lon[0]=50 dimz_Z[0]=0 hgt[0]=53 m
time[0]=1879032 level[0]=1000 lat[0]=60 lon[1]=52.5 dimz_Z[0]=0 hgt[1]=55 m

What I have been told by a friend is that I cannot add this dimension to the existing variable. Instead in fortran I must define the variable hgt all over again as a five dimensional variable and put the values all over again. Is that correct or is there an alternative ? I am using Fortran 77 netcdf API.

Community
  • 1
  • 1
gansub
  • 1,164
  • 3
  • 20
  • 47
  • 2
    Your friend is correct. – cmaster - reinstate monica Aug 23 '16 at 16:51
  • Isn't dimz_Z just the unraveled lon/lat grid (21*23=483)? Why do you want to square the amount of data in your file? I'm pretty certain that you are wanting to do something else. Are you sure you just don't want to add a variable with the actual height? This isn't a Fortran question either... – RussF Aug 24 '16 at 00:42
  • @RussF - It is the vertical coordinate in my system. I have lat, lon and eta. Making it three dimensional – gansub Aug 24 '16 at 00:43

1 Answers1

1

Your friend is correct. However, there remains some options that you can consider. There is almost always options when it comes to programming.

  1. Create a new netcdf file from the initial one and define your variable in the new one to have the extra dimension. You can do that in fortran
  2. Use netcdf processing tools (nco) to create a new variable with the properties that you want and delete the other one.
innoSPG
  • 4,588
  • 1
  • 29
  • 42
  • can you add some code for illustrating 1st point ? I will accept your answer – gansub Aug 24 '16 at 14:16
  • I did not have enough information to add some code since you did not add any, it is good to know if you are writing fixed form (f77) or free form (f90 +). However, you can follow the example from https://www.unidata.ucar.edu/software/netcdf/examples/programs/ . https://www.unidata.ucar.edu/software/netcdf/examples/programs/simple_xy_rd.f90 for example shows how to read and https://www.unidata.ucar.edu/software/netcdf/examples/programs/simple_xy_wr.f90 shows how to write. You will learn a lot by following the example and translating it to your case. – innoSPG Aug 24 '16 at 18:11
  • updated the question. The answer I linked to has the code sample. – gansub Aug 25 '16 at 06:41
  • The goal is not to write the code for you! but to provide you with directives. Links that I gave above are enough to help you solve the problem. If you try and are stock somewhere, you can ask for more help. – innoSPG Aug 25 '16 at 12:42
  • i am more than capable of writing the code yes. I do not need any help in that regard. My point was if you want me to accept the answer(i already upvoted your contribution) you need to put in some code into it. End of discussion. – gansub Aug 25 '16 at 13:06
  • I got your point. My interest is not to absolutely get an accepted answer, it is to give back to S.O. what I get everyday from the website. I encourage OP to accept my answer if it helped them, but it is up to them to decide if they accept or not. Eventually in this case, you can add your own answer with some code and accept it. It is going to be helpful because that question pops up from time to time. – innoSPG Aug 25 '16 at 16:45