def save(self, overwriteAll=False, overwriteIncomplete=True): ''' Splits the data in blocks of 1 month and stores them in NetCDF files ''' tmpDates = np.array(self.loaded['dates']) monthIdxs = np.array(self._splitByMonth(self.loaded['dates'])) uniqueMonthIdxs = np.unique(monthIdxs) print('Saving NetCDFs:') tmpPeriods = len(uniqueMonthIdxs) for c0, i0 in enumerate(uniqueMonthIdxs): self._printProgress(c0, tmpPeriods, prefix = 'Progress:', suffix = 'Complete', barLength = 50) tmp = np.where(monthIdxs==i0)[0] monthDates = tmpDates[tmp] if not overwriteAll: if monthDates[0].year in self.netCDFDict.keys() and monthDates[0].month in self.netCDFDict[ monthDates[0].year].keys(): if self.netCDFDict[monthDates[0].year][monthDates[0].month][1]==True: # prevents complete files from being overwritten continue else: # incomplete file if not overwriteIncomplete: # prevents overwriting continue monthData = self.loaded['data'][tmp, :, :] monthMissing = self.loaded['missing'][tmp] rootgrp = Dataset(os.path.join(self.dataFolder, self.filePrefix + '_%04d.%02d.nc' % (monthDates[0].year, monthDates[0].month)), 'w', format='NETCDF4', clobber=True) time = rootgrp.createDimension('time', None) lat = rootgrp.createDimension('lat', monthData.shape[1]) lon = rootgrp.createDimension('lon', monthData.shape[2]) times = rootgrp.createVariable('time', np.double, dimensions=('time',), zlib=True) lats = rootgrp.createVariable('lat', np.double, dimensions=('lat',), zlib=True) lons = rootgrp.createVariable('lon', np.double, dimensions=('lon',), zlib=True) precips = rootgrp.createVariable('precipitation', self.precision, dimensions=('time', 'lat', 'lon'), zlib=True, least_significant_digit=self.significantDigits) missing = rootgrp.createVariable('missing', np.int8, dimensions=('time'), zlib=True) rootgrp.description = 'Rainfall data (' + self.filePrefix + ')' rootgrp.history = 'Created the ' + str(dt.datetime.now()) lats.units = 'degrees of the center of the pixel (WGS84)' lons.units = 'degrees of the center of the pixel (WGS84)' times.units = "hours since 0001-01-01 00:00:00.0" times.calendar = 'standard' precips.units = 'mm of rain accumulated over a 3-hour interval centered on the time reference [-1.5, +1.5]' # Check completeness monthDates[0] + relativedelta(months=1) tmp = self._filePeriod(dateIni=monthDates[-1] - relativedelta(months=1), dateEnd=monthDates[0] + relativedelta(months=1)) tmp = [dt0 for dt0 in tmp if dt0.month==monthDates[0].month and dt0.year==monthDates[0].year] if len(self._ismember(tmp, monthDates)) == len(tmp): # The month is complete if np.all(np.logical_not(monthMissing)): rootgrp.complete = 1 else: rootgrp.complete = 0 else: # The month is not complete rootgrp.complete = 0 if rootgrp.complete==0: warnings.warn(' netCDF not complete (' + self.filePrefix + '_%04d.%02d.nc' % (monthDates[0].year, monthDates[0].month) + ').', UserWarning) lats[:] = self.loaded['lat'] lons[:] = self.loaded['lon'] times[:] = date2num(monthDates, units=times.units, calendar=times.calendar) precips[:, :, :] = monthData missing[:] = monthMissing rootgrp.close() self._printProgress(tmpPeriods, tmpPeriods, prefix = 'Progress:', suffix = 'Complete', barLength = 50)