Skip to main content
added 10 characters in body
Source Link
Rachel
  • 366
  • 1
  • 5

This is caused by the format of your variable and initialization. Your variable is a character array. You are Your character array is only initializingholding the first charactervalue of the array. This has to do with howeach string, which is why you initialize your mom array and provide data to your netcdf variableare getting thirty 2's. This will initialize The code below initializes your empty array with empty character values where each. Each array is a character array whose elements create the datetime stamp. See the below output Below is your code updated to use a character array for your desired string data:

>>> import netCDF4 as nc4 >>> import numpy as np >>> >>> lon= np.loadtxt('Longitude_Sea_30.txt')#latitude >>> lat = np.loadtxt('Latitude_Sea_30.txt')#longitude >>> z = np.tile(1,30)#depth >>> tijd=np.tile(1,30)#time # I changed the name to Part1part because Part looked like it was a keyword in # your example. I also changed it to fill with an empty character because I # am using it to initialize a character array. >>> Part1part = np.tile('', 30) >>> Part1part array(['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], dtype='<U1') >>> llon=len(lon) >>> llat=len(lat) >>> lz=len(z) >>> ltijd=len(tijd) # creating length part with Part1part >>> lPart=len(Part1part) >>> ntime=1 >>> nparticles=30 >>> # This is the datetime string given in your example. I just set it as a variable. >>> datetime_string = '2013/04/22;00:00:00' # I am using full so I can set the empty characters to '' explicitly. >>> mom = np.full((30, len(datetime_string)), fill_value='') >>> mom array([['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']], dtype='<U1') # Now I am filling in values for mom. You must break the string into its # character parts to take advantage of the np.array functionality. >>> for i in range(0,30): ... mom[i] = list(datetime_string) ... >>> mom array([['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0']], dtype='<U1') # open your dataset for writing >>> f = nc4.Dataset('Stack2.nc','w', format='NETCDF4') >>> Jellygrp = f.createGroup('Jelly') >>> Jellygrp.createDimension('time',None) <class 'netCDF4._netCDF4.Dimension'> (unlimited): name = 'time', size = 0 # Create the tlendim with the length of your datetime_string >>> Jellygrp.createDimension('tlendim', len(datetime_string)) <class 'netCDF4._netCDF4.Dimension'>: name = 'tlendim', size = 19 >>> Jellygrp.createDimension('Y009', lPart) <class 'netCDF4._netCDF4.Dimension'>: name = 'Y009', size = 30 >>> Jellygrp.createDimension('X006', llon) #lon <class 'netCDF4._netCDF4.Dimension'>: name = 'X006', size = 30 >>> Jellygrp.createDimension('X007', llat) #lat <class 'netCDF4._netCDF4.Dimension'>: name = 'X007', size = 30 >>> Jellygrp.createDimension('X008', lz) <class 'netCDF4._netCDF4.Dimension'>: name = 'X008', size = 30 >>> part_xpos= np.zeros((ntime,nparticles)) >>> part_ypos= np.zeros((ntime,nparticles)) >>> part_zpos= np.zeros((ntime,nparticles)) # Initialize your array to include the length of your desired datetime_string, making it 3 dimensional >>> part_starttime_array= np.full((ntime,nparticles,len(datetime_string)), fill_value='') # create your variables >>> time = Jellygrp.createVariable('time', 'S1', ('time','tlendim')) >>> part_xpos=Jellygrp.createVariable('part_xpos', 'd', ('time','X006')) >>> part_ypos=Jellygrp.createVariable('part_ypos','d',('time','X007')) >>> part_zpos=Jellygrp.createVariable('part_zpos','d',('time','X008')) # you need to add the length of the datetime_string as a dimension >>> part_starttime=Jellygrp.createVariable('part_starttime','S1', ('time', 'Y009', 'tlendim')) # assign to your netCDF variables. >>> part_xpos[0,:] =lon[:] >>> part_ypos[0,:] =lat[:] >>> part_zpos[0,:] = z[:] >>> part_starttime[0,:] = mom[:] >>> f.close() 

This is caused by the format of your variable and initialization. Your variable is a character array. You are only initializing the first character of the array. This has to do with how you initialize your mom array and provide data to your netcdf variable. This will initialize your empty array with empty character values where each array is a character array whose elements create the datetime stamp. See the below output:

>>> import netCDF4 as nc4 >>> import numpy as np >>> >>> lon= np.loadtxt('Longitude_Sea_30.txt')#latitude >>> lat = np.loadtxt('Latitude_Sea_30.txt')#longitude >>> z = np.tile(1,30)#depth >>> tijd=np.tile(1,30)#time # I changed the name to Part1 because Part looked like it was a keyword in # your example. I also changed it to fill with an empty character because I # am using it to initialize a character array. >>> Part1 = np.tile('', 30) >>> Part1 array(['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], dtype='<U1') >>> llon=len(lon) >>> llat=len(lat) >>> lz=len(z) >>> ltijd=len(tijd) # creating length part with Part1 >>> lPart=len(Part1) >>> ntime=1 >>> nparticles=30 >>> # This is the datetime string given in your example. I just set it as a variable. >>> datetime_string = '2013/04/22;00:00:00' # I am using full so I can set the empty characters to '' explicitly. >>> mom = np.full((30, len(datetime_string)), fill_value='') >>> mom array([['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']], dtype='<U1') # Now I am filling in values for mom. You must break the string into its # character parts to take advantage of the np.array functionality. >>> for i in range(0,30): ... mom[i] = list(datetime_string) ... >>> mom array([['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0']], dtype='<U1') # open your dataset for writing >>> f = nc4.Dataset('Stack2.nc','w', format='NETCDF4') >>> Jellygrp = f.createGroup('Jelly') >>> Jellygrp.createDimension('time',None) <class 'netCDF4._netCDF4.Dimension'> (unlimited): name = 'time', size = 0 # Create the tlendim with the length of your datetime_string >>> Jellygrp.createDimension('tlendim', len(datetime_string)) <class 'netCDF4._netCDF4.Dimension'>: name = 'tlendim', size = 19 >>> Jellygrp.createDimension('Y009', lPart) <class 'netCDF4._netCDF4.Dimension'>: name = 'Y009', size = 30 >>> Jellygrp.createDimension('X006', llon) #lon <class 'netCDF4._netCDF4.Dimension'>: name = 'X006', size = 30 >>> Jellygrp.createDimension('X007', llat) #lat <class 'netCDF4._netCDF4.Dimension'>: name = 'X007', size = 30 >>> Jellygrp.createDimension('X008', lz) <class 'netCDF4._netCDF4.Dimension'>: name = 'X008', size = 30 >>> part_xpos= np.zeros((ntime,nparticles)) >>> part_ypos= np.zeros((ntime,nparticles)) >>> part_zpos= np.zeros((ntime,nparticles)) # Initialize your array to include the length of your desired datetime_string, making it 3 dimensional >>> part_starttime_array= np.full((ntime,nparticles,len(datetime_string)), fill_value='') # create your variables >>> time = Jellygrp.createVariable('time', 'S1', ('time','tlendim')) >>> part_xpos=Jellygrp.createVariable('part_xpos', 'd', ('time','X006')) >>> part_ypos=Jellygrp.createVariable('part_ypos','d',('time','X007')) >>> part_zpos=Jellygrp.createVariable('part_zpos','d',('time','X008')) # you need to add the length of the datetime_string as a dimension >>> part_starttime=Jellygrp.createVariable('part_starttime','S1', ('time', 'Y009', 'tlendim')) # assign to your netCDF variables. >>> part_xpos[0,:] =lon[:] >>> part_ypos[0,:] =lat[:] >>> part_zpos[0,:] = z[:] >>> part_starttime[0,:] = mom[:] >>> f.close() 

This is caused by the format of your variable and initialization. Your variable is a character array. Your character array is only holding the first value of each string, which is why you are getting thirty 2's. The code below initializes your empty array with empty character values. Each array is a character array whose elements create the datetime stamp. Below is your code updated to use a character array for your desired string data:

>>> import netCDF4 as nc4 >>> import numpy as np >>> >>> lon= np.loadtxt('Longitude_Sea_30.txt')#latitude >>> lat = np.loadtxt('Latitude_Sea_30.txt')#longitude >>> z = np.tile(1,30)#depth >>> tijd=np.tile(1,30)#time # I changed the name to part because Part looked like it was a keyword in # your example. I also changed it to fill with an empty character because I # am using it to initialize a character array. >>> part = np.tile('', 30) >>> part array(['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], dtype='<U1') >>> llon=len(lon) >>> llat=len(lat) >>> lz=len(z) >>> ltijd=len(tijd) # creating length part with part >>> lPart=len(part) >>> ntime=1 >>> nparticles=30 >>> # This is the datetime string given in your example. I just set it as a variable. >>> datetime_string = '2013/04/22;00:00:00' # I am using full so I can set the empty characters to '' explicitly. >>> mom = np.full((30, len(datetime_string)), fill_value='') >>> mom array([['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']], dtype='<U1') # Now I am filling in values for mom. You must break the string into its # character parts to take advantage of the np.array functionality. >>> for i in range(0,30): ... mom[i] = list(datetime_string) ... >>> mom array([['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0']], dtype='<U1') # open your dataset for writing >>> f = nc4.Dataset('Stack2.nc','w', format='NETCDF4') >>> Jellygrp = f.createGroup('Jelly') >>> Jellygrp.createDimension('time',None) <class 'netCDF4._netCDF4.Dimension'> (unlimited): name = 'time', size = 0 # Create the tlendim with the length of your datetime_string >>> Jellygrp.createDimension('tlendim', len(datetime_string)) <class 'netCDF4._netCDF4.Dimension'>: name = 'tlendim', size = 19 >>> Jellygrp.createDimension('Y009', lPart) <class 'netCDF4._netCDF4.Dimension'>: name = 'Y009', size = 30 >>> Jellygrp.createDimension('X006', llon) #lon <class 'netCDF4._netCDF4.Dimension'>: name = 'X006', size = 30 >>> Jellygrp.createDimension('X007', llat) #lat <class 'netCDF4._netCDF4.Dimension'>: name = 'X007', size = 30 >>> Jellygrp.createDimension('X008', lz) <class 'netCDF4._netCDF4.Dimension'>: name = 'X008', size = 30 >>> part_xpos= np.zeros((ntime,nparticles)) >>> part_ypos= np.zeros((ntime,nparticles)) >>> part_zpos= np.zeros((ntime,nparticles)) # Initialize your array to include the length of your desired datetime_string, making it 3 dimensional >>> part_starttime_array= np.full((ntime,nparticles,len(datetime_string)), fill_value='') # create your variables >>> time = Jellygrp.createVariable('time', 'S1', ('time','tlendim')) >>> part_xpos=Jellygrp.createVariable('part_xpos', 'd', ('time','X006')) >>> part_ypos=Jellygrp.createVariable('part_ypos','d',('time','X007')) >>> part_zpos=Jellygrp.createVariable('part_zpos','d',('time','X008')) # you need to add the length of the datetime_string as a dimension >>> part_starttime=Jellygrp.createVariable('part_starttime','S1', ('time', 'Y009', 'tlendim')) # assign to your netCDF variables. >>> part_xpos[0,:] =lon[:] >>> part_ypos[0,:] =lat[:] >>> part_zpos[0,:] = z[:] >>> part_starttime[0,:] = mom[:] >>> f.close() 
Source Link
Rachel
  • 366
  • 1
  • 5

This is caused by the format of your variable and initialization. Your variable is a character array. You are only initializing the first character of the array. This has to do with how you initialize your mom array and provide data to your netcdf variable. This will initialize your empty array with empty character values where each array is a character array whose elements create the datetime stamp. See the below output:

>>> import netCDF4 as nc4 >>> import numpy as np >>> >>> lon= np.loadtxt('Longitude_Sea_30.txt')#latitude >>> lat = np.loadtxt('Latitude_Sea_30.txt')#longitude >>> z = np.tile(1,30)#depth >>> tijd=np.tile(1,30)#time # I changed the name to Part1 because Part looked like it was a keyword in # your example. I also changed it to fill with an empty character because I # am using it to initialize a character array. >>> Part1 = np.tile('', 30) >>> Part1 array(['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], dtype='<U1') >>> llon=len(lon) >>> llat=len(lat) >>> lz=len(z) >>> ltijd=len(tijd) # creating length part with Part1 >>> lPart=len(Part1) >>> ntime=1 >>> nparticles=30 >>> # This is the datetime string given in your example. I just set it as a variable. >>> datetime_string = '2013/04/22;00:00:00' # I am using full so I can set the empty characters to '' explicitly. >>> mom = np.full((30, len(datetime_string)), fill_value='') >>> mom array([['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''], ['', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '']], dtype='<U1') # Now I am filling in values for mom. You must break the string into its # character parts to take advantage of the np.array functionality. >>> for i in range(0,30): ... mom[i] = list(datetime_string) ... >>> mom array([['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0'], ['2', '0', '1', '3', '/', '0', '4', '/', '2', '2', ';', '0', '0', ':', '0', '0', ':', '0', '0']], dtype='<U1') # open your dataset for writing >>> f = nc4.Dataset('Stack2.nc','w', format='NETCDF4') >>> Jellygrp = f.createGroup('Jelly') >>> Jellygrp.createDimension('time',None) <class 'netCDF4._netCDF4.Dimension'> (unlimited): name = 'time', size = 0 # Create the tlendim with the length of your datetime_string >>> Jellygrp.createDimension('tlendim', len(datetime_string)) <class 'netCDF4._netCDF4.Dimension'>: name = 'tlendim', size = 19 >>> Jellygrp.createDimension('Y009', lPart) <class 'netCDF4._netCDF4.Dimension'>: name = 'Y009', size = 30 >>> Jellygrp.createDimension('X006', llon) #lon <class 'netCDF4._netCDF4.Dimension'>: name = 'X006', size = 30 >>> Jellygrp.createDimension('X007', llat) #lat <class 'netCDF4._netCDF4.Dimension'>: name = 'X007', size = 30 >>> Jellygrp.createDimension('X008', lz) <class 'netCDF4._netCDF4.Dimension'>: name = 'X008', size = 30 >>> part_xpos= np.zeros((ntime,nparticles)) >>> part_ypos= np.zeros((ntime,nparticles)) >>> part_zpos= np.zeros((ntime,nparticles)) # Initialize your array to include the length of your desired datetime_string, making it 3 dimensional >>> part_starttime_array= np.full((ntime,nparticles,len(datetime_string)), fill_value='') # create your variables >>> time = Jellygrp.createVariable('time', 'S1', ('time','tlendim')) >>> part_xpos=Jellygrp.createVariable('part_xpos', 'd', ('time','X006')) >>> part_ypos=Jellygrp.createVariable('part_ypos','d',('time','X007')) >>> part_zpos=Jellygrp.createVariable('part_zpos','d',('time','X008')) # you need to add the length of the datetime_string as a dimension >>> part_starttime=Jellygrp.createVariable('part_starttime','S1', ('time', 'Y009', 'tlendim')) # assign to your netCDF variables. >>> part_xpos[0,:] =lon[:] >>> part_ypos[0,:] =lat[:] >>> part_zpos[0,:] = z[:] >>> part_starttime[0,:] = mom[:] >>> f.close() 

Now you can use ncdump to view the part_starttime variable.

$ ncdump -v part_starttime Stack2.nc netcdf Stack2 { // global attributes: :_NCProperties = "version=1|netcdflibversion=4.4.1.1|hdf5libversion=1.8.18" ; group: Jelly { dimensions: time = UNLIMITED ; // (1 currently) tlendim = 19 ; Y009 = 30 ; X006 = 30 ; X007 = 30 ; X008 = 30 ; variables: char time(time, tlendim) ; double part_xpos(time, X006) ; double part_ypos(time, X007) ; double part_zpos(time, X008) ; char part_starttime(time, Y009, tlendim) ; data: part_starttime = "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00", "2013/04/22;00:00:00" ; } // group Jelly }