Beispiel #1
0
    def linear_time_interpolate(self, t0, t1, xlat, xlon, xalt, time):
        """Interpolates between files

		t0, t1, and time are assumed to be datetime objects"""
        if self.interpolators.has_key((t0, t1)):
            model0, model1 = self.interpolators[(t0, t1)]
        else:  # need to get interpolator if it's available
            file0 = self.get_file_for_time(t0)
            if file0 is None:
                print 'no file for time', t0
                for filename, interval in self.file_times.items():
                    if time_in_interval(time, interval):
                        print 'found time in interval'
                for k, v in self.file_times.items():
                    print k, v, v[0] <= t0 <= v[1]
                raise IOError("no file for {}".format(t0))
            file1 = self.get_file_for_time(t1)
            try:
                model0 = self.interpolators[self.file_times[file0]]
                model1 = self.interpolators[self.file_times[file1]]
            except:
                print file0, file1
                raise
            self.interpolators[(t0, t1)] = model0, model1
        d0 = model0.density(xlat, xlon, xalt,
                            model0.ut[-1])  #end time of last file
        d1 = model1.density(xlat, xlon, xalt,
                            model1.ut[0])  # first time in next file
        w = (time - t0) / (t1 - t0)
        return d0 * (1.0 - w) + d1 * w
	def density(self, xlat, xlon, xalt, gregorian_string, raise_errors = False, debug = False):
		self.interpolations +=1
		if self.interpolations%1000 == 0:	
			print xlat, xlon, xalt, gregorian_string, self.interpolations
		try:
			time = pd.to_datetime(gregorian_string)
			if not time_in_interval(time, self.last_interval):
				filename = self.get_file_for_time(time) # look for time in file date ranges
				if filename is not None: # found time in file
					self.last_interval = self.file_times[filename] # so we can jump straight to interpolation next time
				else: # file not in time ranges
					closest_files = self.time_in_range(time) # see if time is in data gap between files
					if closest_files is not None: # linearly interpolate between these two
						t0 = self.file_times[closest_files[0]][1]
						t1 = self.file_times[closest_files[1]][0]
						return self.linear_time_interpolate(t0, t1, xlat, xlon, xalt, time)
					else: # outside all available data
						raise ValueError('Could not find time in files')
			model = self.interpolators[self.last_interval]
			try:
				density = model.density(xlat, xlon, xalt, model.time_to_ut(time))
			except:
				# import ipdb; ipdb.set_trace()
				raise
			
			return density
		except:
			if raise_errors:
				print 'TIEGCM error at xlat: {}, xlon: {}, xalt: {}, gregorian str: {}'.format(xlat, xlon, xalt, gregorian_string)
				print 'last interval:', self.last_interval
				raise
			else:
				return 0
Beispiel #3
0
 def get_file_for_time(self, time):
     for filename, interval in self.file_times.items():
         if time_in_interval(time, interval):
             return filename