def load_raws_observations(obs_file,glat,glon,grid_dist_km): """ Loads all of the RAWS observations valid at the time in question and converts them to Observation objects. """ # load observations & register them to grid orig_obs = [] if os.path.exists(obs_file): orig_obs = np.loadtxt(obs_file,dtype=np.object,delimiter=',') else: print('WARN: no observation file found.') obss = [] omin, omax = 0.6, 0.0 # format of file # 0 1 2 3 4 5 6 7 8 9 10 11 # yyyy,mm,dd,hh,MM,ss,lat,lon,elevation,var_id,value,variance for oo in orig_obs: ts = datetime(int(oo[0]),int(oo[1]),int(oo[2]),int(oo[3]),int(oo[4]),int(oo[5]),tzinfo=pytz.timezone('GMT')) lat, lon, elev = float(oo[6]), float(oo[7]), float(oo[8]) obs, ovar = float(oo[10]), float(oo[11]) i, j = find_closest_grid_point(lat,lon,glat,glon) # compute distance to grid points dist_grid_pt = great_circle_distance(lon,lat,glon[i,j],glat[i,j]) # check & remove nonsense zero-variance (or negative variance) observations if ovar > 0 and dist_grid_pt < grid_dist_km / 2.0: obss.append(Observation(ts,lat,lon,elev,oo[9],obs,ovar,(i,j))) omin = min(omin, obs) omax = max(omax, obs) print('INFO: loaded %d observations in range %g to %g [%d available]' % (len(obss),omin,omax,len(obss))) return obss
def register_to_grid(self, wrf_data): """ Find the nearest grid point to the current location. """ # only co-register to grid if required mlon, mlat = wrf_data.get_lons(), wrf_data.get_lats() self.grid_pt = find_closest_grid_point(self.lon, self.lat, mlon, mlat) self.dist_grid_pt = great_circle_distance(self.lon, self.lat, mlon[self.grid_pt], mlat[self.grid_pt])
def get_grid_values_from_file(ncpath,varname,lat,lon): d = netCDF4.Dataset(ncpath) glat, glon = d.variables['Lat'][:,:], d.variables['Lon'][:,:] V = d.variables[varname] i, j = find_closest_grid_point(lon, lat, glon, glat) dist = great_circle_distance(glon[i,j],glat[i,j],lon,lat) vals = V[i,j,...] d.close() return dist, i, j, vals
def get_ngp(lat,lon,ncpath): """ Finds the closest grid point to lat/lon, prints the indices and shows the distance to the point. """ d = netCDF4.Dataset(ncpath) # find closest grid point glat,glon = d.variables['XLAT'][0,:,:], d.variables['XLONG'][0,:,:] i,j = find_closest_grid_point(lon,lat,glon,glat) dist = great_circle_distance(lon,lat,glon[i,j],glat[i,j]) print('GP closest to lat/lon %g,%g is %d,%d with distance %g km.' % (lat,lon,i,j,dist))
def get_ngp(lat, lon, ncpath): """ Finds the closest grid point to lat/lon, prints the indices and shows the distance to the point. """ d = netCDF4.Dataset(ncpath) # find closest grid point glat, glon = d.variables['XLAT'][0, :, :], d.variables['XLONG'][0, :, :] i, j = find_closest_grid_point(lon, lat, glon, glat) dist = great_circle_distance(lon, lat, glon[i, j], glat[i, j]) print('GP closest to lat/lon %g,%g is %d,%d with distance %g km.' % (lat, lon, i, j, dist))
def load_raws_observations(obs_file, glat, glon, grid_dist_km): """ Loads all of the RAWS observations valid at the time in question and converts them to Observation objects. """ # load observations & register them to grid orig_obs = [] if os.path.exists(obs_file): orig_obs = np.loadtxt(obs_file, dtype=np.object, delimiter=',') else: print('WARN: no observation file found.') obss = [] omin, omax = 0.6, 0.0 # format of file # 0 1 2 3 4 5 6 7 8 9 10 11 # yyyy,mm,dd,hh,MM,ss,lat,lon,elevation,var_id,value,variance for oo in orig_obs: ts = datetime(int(oo[0]), int(oo[1]), int(oo[2]), int(oo[3]), int(oo[4]), int(oo[5]), tzinfo=pytz.timezone('GMT')) lat, lon, elev = float(oo[6]), float(oo[7]), float(oo[8]) obs, ovar = float(oo[10]), float(oo[11]) i, j = find_closest_grid_point(lat, lon, glat, glon) # compute distance to grid points dist_grid_pt = great_circle_distance(lon, lat, glon[i, j], glat[i, j]) # check & remove nonsense zero-variance (or negative variance) observations if ovar > 0 and dist_grid_pt < grid_dist_km / 2.0: obss.append( Observation(ts, lat, lon, elev, oo[9], obs, ovar, (i, j))) omin = min(omin, obs) omax = max(omax, obs) print('INFO: loaded %d observations in range %g to %g [%d available]' % (len(obss), omin, omax, len(obss))) return obss
def extract_nearest_time_series(varnames, lat, lon, ncpath): """ Extract the time series in varnames for the grid point that is closest to the given lat/lon and store them in a CSV file in the same order as varnames. This assumes that the time dimension is first. Each variable name is either a name or a tuple with name and index (for 4d vars). """ d = netCDF4.Dataset(ncpath) # find closest grid point glat, glon = d.variables['XLAT'][0, :, :], d.variables['XLONG'][0, :, :] i, j = find_closest_grid_point(lon, lat, glon, glat) # retrieve all time series tss = [] for vi in varnames: if isinstance(vi, tuple): vn, ndx = vi tss.append(d.variables[vn][:, ndx, i, j]) else: tss.append(d.variables[vi][:, i, j]) # extract time strings (example 2012-09-14_23:00:00) & convert to datetime esmft = d.variables['Times'][:, :] times = map(lambda x: ''.join(x), esmft) dts = map( lambda x: datetime(int(x[:4]), int(x[5:7]), int(x[8:10]), int(x[ 11:13]), int(x[14:16]), 0), times) d.close() for i in range(len(dts)): dt = dts[i] toks = map(lambda x: str(x), [dt.year, dt.month, dt.day, dt.hour, dt.minute, 0]) for ts in tss: toks.append(str(ts[i])) print(','.join(toks))
def load_raws_observations(obs_file,glat,glon): """ Loads all of the RAWS observations valid at the time in question and converts them to Observation objects. """ # load observations & register them to grid orig_obs = [] if os.path.exists(obs_file): orig_obs = np.loadtxt(obs_file,delimiter=',') else: print('WARN: no observation file found, only performing model time step.') obss = [] omin, omax = 0.6, 0.0 for oo in orig_obs: i, j = find_closest_grid_point(oo[6],oo[7],glat,glon) obst = datetime(int(oo[0]),int(oo[1]),int(oo[2]),int(oo[3]),int(oo[4]),int(oo[5])) # check & remove non-sense zero observations if oo[8] > 0: obss.append(Observation(obst,oo[6],oo[7],oo[8],oo[9],(i,j))) omin = min(omin, oo[8]) omax = max(omax, oo[8]) print('INFO: loaded %d observations in range %g to %g' % (len(obss),omin,omax)) return obss
def extract_nearest_time_series(varnames,lat,lon,ncpath): """ Extract the time series in varnames for the grid point that is closest to the given lat/lon and store them in a CSV file in the same order as varnames. This assumes that the time dimension is first. Each variable name is either a name or a tuple with name and index (for 4d vars). """ d = netCDF4.Dataset(ncpath) # find closest grid point glat,glon = d.variables['XLAT'][0,:,:], d.variables['XLONG'][0,:,:] i,j = find_closest_grid_point(lon,lat,glon,glat) # retrieve all time series tss = [] for vi in varnames: if isinstance(vi,tuple): vn,ndx = vi tss.append(d.variables[vn][:,ndx,i,j]) else: tss.append(d.variables[vi][:,i,j]) # extract time strings (example 2012-09-14_23:00:00) & convert to datetime esmft = d.variables['Times'][:,:] times = map(lambda x: ''.join(x), esmft) dts = map(lambda x: datetime(int(x[:4]),int(x[5:7]),int(x[8:10]),int(x[11:13]), int(x[14:16]), 0), times) d.close() for i in range(len(dts)): dt = dts[i] toks = map(lambda x: str(x), [dt.year,dt.month,dt.day,dt.hour,dt.minute,0]) for ts in tss: toks.append(str(ts[i])) print(','.join(toks))