def readFileMonthly(fileName,var2Read,yearC,logPath): out = np.array([]) try: scale = 84600 dataSet = nc.Dataset(fileName,'r') data = dataSet.variables[var2Read][:]*scale lPos = -1 days = int(data.shape[0]) for m in range(len(months)): fPos = lPos + 1 if(isLeap(int(yearC)) and m == 1 and days==366): lPos = months[m] + fPos + 1# Leap year else: lPos = months[m] + fPos newMonth = np.mean(data[fPos:lPos,:,:],axis=0) if out.size != 0: if out.ndim < 3: out = np.concatenate((out[...,np.newaxis],newMonth[...,np.newaxis]),axis=2) else: out = np.concatenate((out[...],newMonth[...,np.newaxis]),axis=2) else: out = newMonth print 'Data saved: %s' % (yearC) fid = open(logPath+'log.txt', 'a+') fid.write('[SAVED] '+fileName+'\n') fid.close() dataSet.close() except: e = sys.exc_info()[1] fid = open(logPath+'log.txt', 'a+') fid.write('[ERROR] '+fileName+' '+str(e)+'\n\n') fid.close() return out
def getDate(day, month, year): if (day < 1) or (month < 1) or (year < date.today().year): return None if month > 12: return None if year > date.today().year + staticValues["MAX_YEARS_AHEAD_DATES"]: return None if month in [1, 3, 5, 7, 8, 10, 12]: if day > 31: return None elif month == 2: if calendar.isLeap(year): if day > 29: return None else: if day > 28: return None else: if day > 30: return None newDate = date(year, month, day) return newDate
def readFileSeasonal(fileName, var2Read, yearC, logPath, lastDec): out = np.array([]) try: scale = 84600 dataSet = nc.Dataset(fileName, 'r') data = dataSet.variables[var2Read][:] * scale seasonMap = [2, 5, 8, 11] lPos = 0 days = int(data.shape[0]) for s in range(len(seasonsName)): fPos = lPos if s > 0: init = seasonMap[s - 1] else: init = 0 for m in range(init, seasonMap[s]): if (isLeap(int(yearC)) and s == 0 and days == 366 and m == 1): lPos += months[m] + 1 # Leap year else: lPos += months[m] nSeason = np.mean(data[fPos:lPos, :, :], axis=0) if s < 1 and lastDec.size != 0: nSeason = (nSeason + lastDec) / 2 if out.size != 0: if out.ndim < 3: out = np.concatenate( (out[..., np.newaxis], nSeason[..., np.newaxis]), axis=2) else: out = np.concatenate((out[...], nSeason[..., np.newaxis]), axis=2) else: out = nSeason if s == 3: lastDec = np.mean(data[lPos:-1, :, :], axis=0) #print 'f: %d - l: %d ' %(fPos,lPos) #fPos += 1 print 'Data saved: %s' % (yearC) fid = open(logPath + 'log.txt', 'a+') fid.write('[SAVED] ' + fileName + '\n') fid.close() dataSet.close() except: e = sys.exc_info()[1] fid = open(logPath + 'log.txt', 'a+') fid.write('[ERROR] ' + fileName + ' ' + str(e) + '\n\n') fid.close() return [out, lastDec]
def readFileSeasonal(fileName,var2Read,yearC,logPath,lastDec): out = np.array([]) try: scale = 84600 dataSet = nc.Dataset(fileName,'r') data = dataSet.variables[var2Read][:]*scale seasonMap = [2,5,8,11] lPos = 0 days = int(data.shape[0]) for s in range(len(seasonsName)): fPos = lPos if s > 0: init = seasonMap[s-1] else: init = 0 for m in range(init,seasonMap[s]): if(isLeap(int(yearC)) and s == 0 and days==366 and m==1): lPos += months[m] + 1# Leap year else: lPos += months[m] nSeason = np.mean(data[fPos:lPos,:,:],axis=0) if s < 1 and lastDec.size != 0: nSeason = (nSeason+lastDec)/2 if out.size != 0: if out.ndim < 3: out = np.concatenate((out[...,np.newaxis],nSeason[...,np.newaxis]),axis=2) else: out = np.concatenate((out[...],nSeason[...,np.newaxis]),axis=2) else: out = nSeason if s == 3: lastDec = np.mean(data[lPos:-1,:,:],axis=0) #print 'f: %d - l: %d ' %(fPos,lPos) #fPos += 1 print 'Data saved: %s' % (yearC) fid = open(logPath+'log.txt', 'a+') fid.write('[SAVED] '+fileName+'\n') fid.close() dataSet.close() except: e = sys.exc_info()[1] fid = open(logPath+'log.txt', 'a+') fid.write('[ERROR] '+fileName+' '+str(e)+'\n\n') fid.close() return [out,lastDec]
def readFileMonthly(fileName, var2Read, yearC, logPath): out = np.array([]) try: scale = 84600 dataSet = nc.Dataset(fileName, 'r') data = dataSet.variables[var2Read][:] * scale lPos = -1 days = int(data.shape[0]) for m in range(len(months)): fPos = lPos + 1 if (isLeap(int(yearC)) and m == 1 and days == 366): lPos = months[m] + fPos + 1 # Leap year else: lPos = months[m] + fPos newMonth = np.mean(data[fPos:lPos, :, :], axis=0) if out.size != 0: if out.ndim < 3: out = np.concatenate( (out[..., np.newaxis], newMonth[..., np.newaxis]), axis=2) else: out = np.concatenate((out[...], newMonth[..., np.newaxis]), axis=2) else: out = newMonth print 'Data saved: %s' % (yearC) fid = open(logPath + 'log.txt', 'a+') fid.write('[SAVED] ' + fileName + '\n') fid.close() dataSet.close() except: e = sys.exc_info()[1] fid = open(logPath + 'log.txt', 'a+') fid.write('[ERROR] ' + fileName + ' ' + str(e) + '\n\n') fid.close() return out