def getTimeIndxs(ncFile): #Validate case of list of dates vs start and end date... st = ed = None if START_DATE and END_DATE: st = ncu.parseDate(START_DATE) ed = ncu.parseDate(END_DATE) return ncu.getPeriodIndexes(ncFile, st, ed)
def getFileDate(fileName): #Would be best if provided some kind of format or regex to exactly match the format of the date in the file name #Regex not generic (just works for TRMM data) dat = re.match(r'^.+\.(\d{10})', fileName).group(1) return ncu.parseDate('-'.join([dat[:4], dat[4:6], dat[6:8]]) + 'T' + dat[8:])
#Means want to extract from a bunch of files, get list of files contained in it print('multi file mode...') ncFiles = ncu.getNCFiles(DATA) #See if all the files (only the first, lets be optimist) can be opened with MFDataset (NC4 and time dim), else #Open one by one. if isPartOfMFDataset(ncFiles[0]): print('Opening all files as MFDataset...') else: print('Files not eligible for MFDataset, opening one by one...') #This could be that the files dont have a time dimension (then the date must be on the file name) #or that the files are not in a supported version of netCDF (and could have time dimension anyways) #Lets asume that the dates are in the file name then ncFiles = [(getFileDate(f), f) for f in ncFiles] #Filter files to get only the wanted period if START_DATE and END_DATE: ncFiles = [f for f in ncFiles if f[0] >= ncu.parseDate(START_DATE) and f[0] <= ncu.parseDate(END_DATE)] #else select files from wanted date list ncFiles = sorted(ncFiles) #Could be more specific (if for some case there are also a time dimension, but not now) #Now get the indexes of the area wanted to extract latIndexes = lonIndexes = None lats = lons = None with Dataset(ncFiles[0][1], 'r') as ncFile: latIndexes, lonIndexes = getLocIndxs(ncFile) lats = ncFile.variables[ncu.LAT_KEY][latIndexes] lons = ncFile.variables[ncu.LON_KEY][lonIndexes] #Once the dates (files) and location (indexes) are set, start writing the data to the file... if FORMAT == FMT_CSV:
num2date(timeStep[0], timeUnits)) else: #This may never happen, except for bad parsed date... print('Can\'t extract time data, bad date format!') #Once the dates and location (indexes) are set, start writing the data to the file... else: print('Files not eligible for MFDataset, opening one by one...') #This could be that the files dont have a time dimension (then the date must be on the file name) #or that the files are not in a supported version of netCDF (and could have time dimension anyways) #Lets asume that the dates are in the file name then ncFiles = [(getFileDate(f), f) for f in ncFiles] #Filter files to get only the wanted period if START_DATE and END_DATE: ncFiles = [ f for f in ncFiles if f[0] >= ncu.parseDate(START_DATE) and f[0] <= ncu.parseDate(END_DATE) ] #else select files from wanted date list ncFiles = sorted(ncFiles) #Could be more specific (if for some case there are also a time dimension, but not now) #Now get the indexes of the area wanted to extract latIndexes = lonIndexes = None lats = lons = None with Dataset(ncFiles[0][1], 'r') as ncFile: latIndexes, lonIndexes = getLocIndxs(ncFile) lats = ncFile.variables[ncu.LAT_KEY][latIndexes] lons = ncFile.variables[ncu.LON_KEY][lonIndexes] #Once the dates (files) and location (indexes) are set, start writing the data to the file...