3
$\begingroup$

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!!

$\endgroup$
  • 1
    $\begingroup$ 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] $\endgroup$ – daniel.neumann Oct 22 '17 at 8:39
  • 1
    $\begingroup$ Have a look at xarray, I think that could make your setup much easier. $\endgroup$ – Tactopoda Oct 22 '17 at 11:21
  • $\begingroup$ 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 linux.die.net/man/1/ncks . see a list here: unidata.ucar.edu/software/netcdf/software.html $\endgroup$ – farrenthorpe Nov 22 '17 at 17:26
1
$\begingroup$

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]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.