<p>There are two issues (and one optimization) with/for the code above:</p>

<ol>
<li>Array indexing starts with <code>0</code> (not <code>1</code>) in Python. Therefore, it should be <code>if data[0,m] == countries[i,j]:</code> because you are interested in the first row;</li>
<li><code>m</code> goes from <code>0</code> to 1 (<code>for m in xrange(0,1):</code>) but it should go from <code>0</code> to <code>number_of_countries</code> so that all columns of the energy are iterated. </li>
<li>The order of the <code>n</code> and <code>m</code> for loops seems to be quite ineffective. Better would be: <code>for m ...: if data[1,m] == ...: for n in ...: nc.variables...=....</code> Or replace latter for loop by <code>nc.variables["energy"][:,i,j]=data[:,m]</code></li>
</ol>

<p>This could work (please improve => community Wiki) but I cannot test it due to missing input data:</p>

<pre><code>y = nc.createDimension("y", 950)  #y
x = nc.createDimension("x", 1000) #x
time = nc.createDimension("time", 87648) #time

latitude=nc.createVariable("y", "f8", ("y", ))
longitude=nc.createVariable("x", "f8", ("x", ))
time = nc.createVariable("time","f8",("time",))
countries = nc.createVariable("countries", "f8", ("y", "x"));
energy = nc.createVariable("energy", "f8", ("time", "y", "x"), 
                           fill_value=-9999, chunksizes=(1, 950, 1000));

time.standard_name='time'
time.units ='hours since 2006-01-01 00:00:00.0'
time.calendar='proleptic_gregorian'

lats =  np.arange(5497500,747500,-5000)    #y  
lons =  np.arange(2502500,7502500,5000)    #x 

# Fill variables
latitude[:] = lats
longitude[:] = lons

countries [:]=value

for i in xrange(0,950):  # grid rows
    for j in xrange(0,1000):    # grid columns
        for m in xrange(0,int(max(countries))):    # countries
            if data[0,m] == countries[i,j]:   #correspondance
                nc.variables["energy"][:,i,j]=data[1:87648,m]
</code></pre>

CC BY-SA 3.0
2017-10-23T16:07:38.110Z
2017-10-23T16:07:38.110Z
daniel.heydebreck
<p>I have created a x,y netcdf file and now want to add additional time energy variable. So that for each grid cell I have a energy time value.</p>

<p>My script is like this in Python:</p>

<pre><code>value = loadtxt("countries.txt",skiprows=0) % countries
data = loadtxt("energy.txt",skiprows=0) % energy
</code></pre>

<p>The <code>energy.txt</code> file is like:</p>

<pre><code>[country id]    ...   51      52      54      55      56      59       60      62    ...  
[time step 1]   ...  nan     649     nan     nan    1083    9979      nan   13602    ...
[time step 2]   ... 5743    1283    9062    4026     nan    6365    43444     nan    ...
[time step n]   ...  ...     ...     ...     ...     ...     ...      ...     ...    ...
</code></pre>

<p>etc (till 87648 steps), where cells of interest are the first row:</p>

<p><code>cellsOfInt=data[0,:]</code></p>

<ul>
<li>For some cells I have nan values since there are not values for those time steps. </li>
<li>Each row correspond to one time step so for cell of interest 51, 
in the first time step we have nan value, in the second time step we have 5743 and so on till 87648.</li>
<li>All cells of interest are in countries.txt file that is 950 X 1000 with cells of interest ids or nan when there is no value. </li>
</ul>

<p>My code:</p>

<pre><code>y = nc.createDimension("y", 950)  #y
x = nc.createDimension("x", 1000) #x
time = nc.createDimension("time", 87648) #time

latitude=nc.createVariable("y", "f8", ("y", ))
longitude=nc.createVariable("x", "f8", ("x", ))
time = nc.createVariable("time","f8",("time",))
countries = nc.createVariable("countries", "f8", ("y", "x"));
energy = nc.createVariable("energy", "f8", ("time", "y", "x"), 
                           fill_value=-9999, chunksizes=(1, 950, 1000));

time.standard_name='time'
time.units ='hours since 2006-01-01 00:00:00.0'
time.calendar='proleptic_gregorian'

lats =  np.arange(5497500,747500,-5000)    #y  
lons =  np.arange(2502500,7502500,5000)    #x 

# Fill variables
latitude[:] = lats
longitude[:] = lons

countries [:]=value
</code></pre>

<p>UNTIL HERE WORKS FINE I have my 2d country but then it does not give any result for energy:</p>

<pre><code>for i in xrange(0,950):  #rows
    for j in xrange(0,1000):    #columns
        for n in xrange(0,87648):  #rows
            for m in xrange(0,1):    #columns
                if data[1,m] == countries[i,j]:   #correspondance
                    nc.variables["energy"][n]=data[n,m]
</code></pre>

<p>Any idea how to fix this so I can have in country.netcdf file time dimension with energy values at each time step? Thanks a lot!!</p>

CC BY-SA 3.0
2017-10-22T08:39:13.950Z
Three comments (I am not familiar with python): **(a)** array indexing starts with 0 (not 1) in Python, right? Then it should be `if data[0,m] == countries[i,j]:` because you are interested in the first row; **(b)** `m` goes from `0` to `1` (`for m in xrange(0,1):`) but it should go from `0` to `number_of_countries` so that all columns of the `energy` are iterated. **(c)** The order of the `n` and `m` for loops seems to be quite ineffective. Better would be: `for m ...: if data[1,m] == ...: for n in ...: nc.variables...=...`. Or replace latter for loop by `nc.variables["energy"][:]=data[:,m]`
daniel.heydebreck
CC BY-SA 3.0
2017-10-22T11:21:53.937Z
Have a look at [xarray](http://xarray.pydata.org/en/stable/), I think that could make your setup much easier.
user2821
CC BY-SA 3.0
2017-11-22T17:26:28.060Z
I would think using nco and/or other tools would be a much more straightforward way of creating and augmenting netcdf files. e.g. netcdf kitchen sink  https://linux.die.net/man/1/ncks . see a list here: https://www.unidata.ucar.edu/software/netcdf/software.html
f.thorpe
<netcdf><time>
Adding time dimension and corresponding new variable to a netcdf file
Sensation
# Adding time dimension and corresponding new variable to a netcdf file

(Asked by: username_0 on 2017-10-20)

I have created a x,y netcdf file and now want to add additional time energy variable. So that for each grid cell I have a energy time value.

My script is like this in Python:

```
value = loadtxt("countries.txt",skiprows=0) % countries
data = loadtxt("energy.txt",skiprows=0) % energy

```

The `energy.txt` file is like:

```
[country id]    ...   51      52      54      55      56      59       60      62    ...  
[time step 1]   ...  nan     649     nan     nan    1083    9979      nan   13602    ...
[time step 2]   ... 5743    1283    9062    4026     nan    6365    43444     nan    ...
[time step n]   ...  ...     ...     ...     ...     ...     ...      ...     ...    ...

```

etc (till 87648 steps), where cells of interest are the first row:

`cellsOfInt=data[0,:]`

* For some cells I have nan values since there are not values for those time steps.
* Each row correspond to one time step so for cell of interest 51,
  in the first time step we have nan value, in the second time step we have 5743 and so on till 87648.
* All cells of interest are in countries.txt file that is 950 X 1000 with cells of interest ids or nan when there is no value.

My code:

```
y = nc.createDimension("y", 950)  #y
x = nc.createDimension("x", 1000) #x
time = nc.createDimension("time", 87648) #time

latitude=nc.createVariable("y", "f8", ("y", ))
longitude=nc.createVariable("x", "f8", ("x", ))
time = nc.createVariable("time","f8",("time",))
countries = nc.createVariable("countries", "f8", ("y", "x"));
energy = nc.createVariable("energy", "f8", ("time", "y", "x"), 
                           fill_value=-9999, chunksizes=(1, 950, 1000));

time.standard_name='time'
time.units ='hours since 2006-01-01 00:00:00.0'
time.calendar='proleptic_gregorian'

lats =  np.arange(5497500,747500,-5000)    #y  
lons =  np.arange(2502500,7502500,5000)    #x 

# Fill variables
latitude[:] = lats
longitude[:] = lons

countries [:]=value

```

UNTIL HERE WORKS FINE I have my 2d country but then it does not give any result for energy:

```
for i in xrange(0,950):  #rows
    for j in xrange(0,1000):    #columns
        for n in xrange(0,87648):  #rows
            for m in xrange(0,1):    #columns
                if data[1,m] == countries[i,j]:   #correspondance
                    nc.variables["energy"][n]=data[n,m]

```

Any idea how to fix this so I can have in country.netcdf file time dimension with energy values at each time step? Thanks a lot!!

**Tags:** netcdf, time

> **Comments:**
> **username_1**: Three comments (I am not familiar with python): **(a)** array indexing starts with 0 (not 1) in Python, right? Then it should be `if data[0,m] == countries[i,j]:` because you are interested in the first row; **(b)** `m` goes from `0` to `1` (`for m in xrange(0,1):`) but it should go from `0` to `number_of_countries` so that all columns of the `energy` are iterated. **(c)** The order of the `n` and `m` for loops seems to be quite ineffective. Better would be: `for m ...: if data[1,m] == ...: for n in ...: nc.variables...=...`. Or replace latter for loop by `nc.variables["energy"][:]=data[:,m]`  
> **username_2**: Have a look at [xarray](http://xarray.pydata.org/en/stable/), I think that could make your setup much easier.  
> **username_3**: I would think using nco and/or other tools would be a much more straightforward way of creating and augmenting netcdf files. e.g. netcdf kitchen sink  https://linux.die.net/man/1/ncks . see a list here: https://www.unidata.ucar.edu/software/netcdf/software.html  

---

## 1 answers

### Answer by username_1 on 2017-10-23. Score: 1

There are two issues (and one optimization) with/for the code above:

1. Array indexing starts with `0` (not `1`) in Python. Therefore, it should be `if data[0,m] == countries[i,j]:` because you are interested in the first row;
2. `m` goes from `0` to 1 (`for m in xrange(0,1):`) but it should go from `0` to `number_of_countries` so that all columns of the energy are iterated.
3. The order of the `n` and `m` for loops seems to be quite ineffective. Better would be: `for m ...: if data[1,m] == ...: for n in ...: nc.variables...=....` Or replace latter for loop by `nc.variables["energy"][:,i,j]=data[:,m]`

This could work (please improve => community Wiki) but I cannot test it due to missing input data:

```
y = nc.createDimension("y", 950)  #y
x = nc.createDimension("x", 1000) #x
time = nc.createDimension("time", 87648) #time

latitude=nc.createVariable("y", "f8", ("y", ))
longitude=nc.createVariable("x", "f8", ("x", ))
time = nc.createVariable("time","f8",("time",))
countries = nc.createVariable("countries", "f8", ("y", "x"));
energy = nc.createVariable("energy", "f8", ("time", "y", "x"), 
                           fill_value=-9999, chunksizes=(1, 950, 1000));

time.standard_name='time'
time.units ='hours since 2006-01-01 00:00:00.0'
time.calendar='proleptic_gregorian'

lats =  np.arange(5497500,747500,-5000)    #y  
lons =  np.arange(2502500,7502500,5000)    #x 

# Fill variables
latitude[:] = lats
longitude[:] = lons

countries [:]=value

for i in xrange(0,950):  # grid rows
    for j in xrange(0,1000):    # grid columns
        for m in xrange(0,int(max(countries))):    # countries
            if data[0,m] == countries[i,j]:   #correspondance
                nc.variables["energy"][:,i,j]=data[1:87648,m]

```


---

