Skip to content

Commit

Permalink
Merge pull request #1321 from Unidata/fixindex
Browse files Browse the repository at this point in the history
clean up for loop syntax
  • Loading branch information
jswhit committed May 2, 2024
2 parents 9f74549 + d006b5f commit a3766f4
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions src/netCDF4/_netCDF4.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1593,7 +1593,7 @@ cdef _get_att_names(int grpid, int varid):
ierr = nc_inq_varnatts(grpid, varid, &numatts)
_ensure_nc_success(ierr, err_cls=AttributeError)
attslist = []
for n from 0 <= n < numatts:
for n in range(numatts):
with nogil:
ierr = nc_inq_attname(grpid, varid, n, namstring)
_ensure_nc_success(ierr, err_cls=AttributeError)
Expand Down Expand Up @@ -1868,7 +1868,7 @@ cdef _get_types(group):
enumtypes = dict()

if ntypes > 0:
for n from 0 <= n < ntypes:
for n in range(ntypes):
xtype = typeids[n]
with nogil:
ierr = nc_inq_user_type(_grpid, xtype, namstring,
Expand Down Expand Up @@ -1930,9 +1930,9 @@ cdef _get_dims(group):
ierr = nc_inq_dimids(_grpid, &numdims, dimids, 0)
_ensure_nc_success(ierr)
else:
for n from 0 <= n < numdims:
for n in range(numdims):
dimids[n] = n
for n from 0 <= n < numdims:
for n in range(numdims):
with nogil:
ierr = nc_inq_dimname(_grpid, dimids[n], namstring)
_ensure_nc_success(ierr)
Expand All @@ -1959,7 +1959,7 @@ cdef _get_grps(group):
with nogil:
ierr = nc_inq_grps(_grpid, NULL, grpids)
_ensure_nc_success(ierr)
for n from 0 <= n < numgrps:
for n in range(numgrps):
with nogil:
ierr = nc_inq_grpname(grpids[n], namstring)
_ensure_nc_success(ierr)
Expand Down Expand Up @@ -1994,10 +1994,10 @@ cdef _get_vars(group, bint auto_complex=False):
ierr = nc_inq_varids(_grpid, &numvars, varids)
_ensure_nc_success(ierr)
else:
for n from 0 <= n < numvars:
for n in range(numvars):
varids[n] = n
# loop over variables.
for n from 0 <= n < numvars:
for n in range(numvars):
varid = varids[n]
# get variable name.
with nogil:
Expand Down Expand Up @@ -3761,7 +3761,7 @@ returns `True` if the `Dimension` instance is unlimited, `False` otherwise."""
ierr = nc_inq_unlimdims(self._grpid, &numunlimdims, unlimdimids)
_ensure_nc_success(ierr)
unlimdim_ids = []
for n from 0 <= n < numunlimdims:
for n in range(numunlimdims):
unlimdim_ids.append(unlimdimids[n])
free(unlimdimids)
if dimid in unlimdim_ids:
Expand Down Expand Up @@ -4155,7 +4155,7 @@ behavior is similar to Fortran or Matlab, but different than numpy.
# find dimension ids.
if ndims:
dimids = <int *>malloc(sizeof(int) * ndims)
for n from 0 <= n < ndims:
for n in range(ndims):
dimids[n] = dimensions[n]._dimid
# go into define mode if it's a netCDF 3 compatible
# file format. Be careful to exit define mode before
Expand Down Expand Up @@ -4289,7 +4289,7 @@ behavior is similar to Fortran or Matlab, but different than numpy.
if grp.data_model != 'NETCDF4': grp._enddef()
raise ValueError('chunksizes must be a sequence with the same length as dimensions')
chunksizesp = <size_t *>malloc(sizeof(size_t) * ndims)
for n from 0 <= n < ndims:
for n in range(ndims):
if not dimensions[n].isunlimited() and \
chunksizes[n] > dimensions[n].size:
msg = 'chunksize cannot exceed dimension size'
Expand Down Expand Up @@ -4813,7 +4813,7 @@ each dimension is returned."""
ierr = nc_inq_var_chunking(self._grpid, self._varid, &icontiguous, chunksizesp)
_ensure_nc_success(ierr)
chunksizes=[]
for n from 0 <= n < ndims:
for n in range(ndims):
chunksizes.append(chunksizesp[n])
free(chunksizesp)
if icontiguous:
Expand Down Expand Up @@ -5343,7 +5343,7 @@ rename a `Variable` attribute named `oldname` to `newname`."""
count = [1]*ndims
startp = <size_t *>malloc(sizeof(size_t) * ndims)
countp = <size_t *>malloc(sizeof(size_t) * ndims)
for n from 0 <= n < ndims:
for n in range(ndims):
startp[n] = start[n]
countp[n] = count[n]
if self.dtype == str: # VLEN string
Expand Down Expand Up @@ -5747,7 +5747,7 @@ NC_CHAR).
def _put(self,ndarray data,start,count,stride):
"""Private method to put data into a netCDF variable"""
cdef int ierr, ndims
cdef npy_intp totelem
cdef npy_intp totelem, dataelem, i
cdef size_t *startp
cdef size_t *countp
cdef ptrdiff_t *stridep
Expand All @@ -5769,7 +5769,7 @@ NC_CHAR).
startp = <size_t *>malloc(sizeof(size_t) * ndims)
countp = <size_t *>malloc(sizeof(size_t) * ndims)
stridep = <ptrdiff_t *>malloc(sizeof(ptrdiff_t) * ndims)
for n from 0 <= n < ndims:
for n in range(ndims):
count[n] = abs(count[n]) # make -1 into +1
countp[n] = count[n]
# for neg strides, reverse order (then flip that axis after data read in)
Expand Down Expand Up @@ -5834,7 +5834,7 @@ NC_CHAR).
# each element in struct.
# allocate struct array to hold vlen data.
strdata = <char **>malloc(sizeof(char *)*totelem)
for i from 0<=i<totelem:
for i in range(totelem):
strdata[i] = data[i]
# strides all 1 or scalar variable, use put_vara (faster)
if sum(stride) == ndims or ndims == 0:
Expand All @@ -5855,7 +5855,7 @@ NC_CHAR).
databuff = PyArray_BYTES(<ndarray>data)
# allocate struct array to hold vlen data.
vldata = <nc_vlen_t *>malloc(<size_t>totelem*sizeof(nc_vlen_t))
for i from 0<=i<totelem:
for i in range(totelem):
elptr = (<void**>databuff)[0]
dataarr = <ndarray>elptr
if self.dtype != dataarr.dtype.str[1:]:
Expand Down Expand Up @@ -5884,7 +5884,8 @@ NC_CHAR).

def _get(self,start,count,stride):
"""Private method to retrieve data from a netCDF variable"""
cdef int ierr, ndims, totelem
cdef int ierr, ndims
cdef npy_intp totelem, i
cdef size_t *startp
cdef size_t *countp
cdef ptrdiff_t *stridep
Expand All @@ -5911,7 +5912,7 @@ NC_CHAR).
startp = <size_t *>malloc(sizeof(size_t) * ndims)
countp = <size_t *>malloc(sizeof(size_t) * ndims)
stridep = <ptrdiff_t *>malloc(sizeof(ptrdiff_t) * ndims)
for n from 0 <= n < ndims:
for n in range(ndims):
count[n] = abs(count[n]) # make -1 into +1
countp[n] = count[n]
# for neg strides, reverse order (then flip that axis after data read in)
Expand Down Expand Up @@ -5980,7 +5981,7 @@ NC_CHAR).
# use _Encoding attribute to decode string to bytes - if
# not given, use 'utf-8'.
encoding = getattr(self,'_Encoding','utf-8')
for i from 0<=i<totelem:
for i in range(totelem):
if strdata[i]:
data[i] = strdata[i].decode(encoding)
else:
Expand Down Expand Up @@ -6015,7 +6016,7 @@ NC_CHAR).
_ensure_nc_success(ierr)
# loop over elements of object array, fill array with
# contents of vlarray struct, put array in object array.
for i from 0<=i<totelem:
for i in range(totelem):
arrlen = vldata[i].len
dataarr = numpy.empty(arrlen, self.dtype)
#dataarr.data = <char *>vldata[i].p
Expand Down Expand Up @@ -6253,7 +6254,7 @@ cdef _def_compound(grp, object dt, object dtype_name):
else: # nested array compound element
ndims = len(format.shape)
dim_sizes = <int *>malloc(sizeof(int) * ndims)
for n from 0 <= n < ndims:
for n in range(ndims):
dim_sizes[n] = format.shape[n]
if format.subdtype[0].kind != 'V': # primitive type.
try:
Expand Down Expand Up @@ -6331,7 +6332,7 @@ cdef _read_compound(group, nc_type xtype, endian=None):
names = []
formats = []
offsets = []
for nf from 0 <= nf < nfields:
for nf in range(nfields):
with nogil:
ierr = nc_inq_compound_field(_grpid,
xtype,
Expand Down Expand Up @@ -6359,7 +6360,7 @@ cdef _read_compound(group, nc_type xtype, endian=None):
# if numdims=0, not an array.
field_shape = ()
if numdims != 0:
for ndim from 0 <= ndim < numdims:
for ndim in range(numdims):
field_shape = field_shape + (dim_sizes[ndim],)
free(dim_sizes)
# check to see if this field is a nested compound type.
Expand Down Expand Up @@ -6613,7 +6614,7 @@ cdef _read_enum(group, nc_type xtype, endian=None):
# loop over members, build dict.
enum_dict = {}
enum_val = numpy.empty(1,dt)
for nmem from 0 <= nmem < nmembers:
for nmem in range(nmembers):
with nogil:
ierr = nc_inq_enum_member(grpid, xtype, nmem, \
enum_namstring,PyArray_DATA(enum_val))
Expand Down

0 comments on commit a3766f4

Please sign in to comment.