def do_calc(LATLIMS_AM, LONLIMS_AM, indir, outdir): land_checker = Basemap() if land_checker.is_land(LATLIMS_AM, LONLIMS_AM): print "SOS! Sorry you have selected a land pixel!" pygame.mixer.music.load("SOS.midi") pygame.mixer.music.play() while pygame.mixer.music.get_busy(): # plot animado? time.sleep(1) else: dataAM = extract_series(LATLIMS_AM, LONLIMS_AM, indir) data_am = np.double(dataAM["Series"]) if all(np.isnan(a) for a in data_am): print "THE SOUND OF SILENCE. Also, BATMAN. Everything is Rest and NaN" pygame.mixer.music.load("Batman_song.midi") pygame.mixer.music.play() while pygame.mixer.music.get_busy(): # Anim plot? See Matplotlib.Animation time.sleep(1) else: am = get_music(data_am) music = pygame.mixer.Sound("Oc.midi") pygame.mixer.music.load("Oc.midi") pygame.mixer.music.play() anim = plot_animation( data_am, (u"Music from Lat = %.2f Lon = %.2f" % (dataAM["Lat"], dataAM["Lon"])), "serie.png", t_max=36000, ) # music.get_length())
def pick_sites(lons, lats, obs_lons=[], obs_lats=[]): if obs_lons != []: assert(obs_lats != []) nsites = len(obs_lons) assert(nsites == len(obs_lats)) ssind = [] for i in range(nsites): dists = numpy.linalg.norm(numpy.array(cartes_list( [lons - obs_lons[i], lats - obs_lats[i]])), axis=1) minind = numpy.argmin(dists) indlat = minind % lats.shape[0] indlon = int(minind / lats.shape[0]) if indlon == 23 and indlat == 35: indlat -= 1 if dists[minind] < 1.0: ssind.append([i, indlon, indlat]) else: nlons = len(lons) nlats = len(lats) ssind = [] i = 0 clist = cartes_list([range(nlons), range(nlats)]) bm = Basemap() # default: projection='cyl' for indlon, indlat in clist: if bm.is_land(lons[indlon], lats[indlat]): ssind.append([i, indlon, indlat]) i = i + 1 return numpy.array(ssind)
def gen_world_coords(fileoutput): minLon = -180.0 maxLon = 180.0 minLat = -85.0 maxLat = 85.0 step = 10 lon = minLon lat = minLat fo = open(fileoutput, 'w') bm = Basemap(projection="merc", resolution='i', llcrnrlon=-180.0, llcrnrlat=-85.0, urcrnrlon=180.0, urcrnrlat=85.0) while lon <= maxLon: while lat <= maxLat: x, y = bm(lon, lat) if bm.is_land(x, y): fo.writelines(str(lat) + ' ' + str(lon) + '\n') lat = lat + step lat = minLat lon = lon + step print(lon) fo.close()
def do_calc(LATLIMS_AM, LONLIMS_AM, indir, outdir): land_checker = Basemap() if land_checker.is_land(LATLIMS_AM, LONLIMS_AM): print('SOS! Sorry you have selected a land pixel!') pygame.mixer.music.load('SOS.midi') pygame.mixer.music.play() while pygame.mixer.music.get_busy(): #plot animado? time.sleep(1) else: dataAM = extract_series(LATLIMS_AM, LONLIMS_AM, indir) data_am = np.double(dataAM['Series']) if all(np.isnan(a) for a in data_am): print( 'THE SOUND OF SILENCE. Also, BATMAN. Everything is Rest and NaN' ) pygame.mixer.music.load('Batman_song.midi') pygame.mixer.music.play() while pygame.mixer.music.get_busy(): # Anim plot? See Matplotlib.Animation time.sleep(1) else: am = get_music(data_am) music = pygame.mixer.Sound('Oc.midi') pygame.mixer.music.load('Oc.midi') pygame.mixer.music.play() anim = plot_animation(data_am, ('Music from Lat = %.2f Lon = %.2f' % (dataAM['Lat'], dataAM['Lon'])), 'serie.png', t_max=36000) #music.get_length())
def check_land(grid): """Check co-ordinates.""" bm = Basemap() points = [] for g in grid: g_land = [bm.is_land(p[0], p[1]) for p in g] points.append(g_land) return points
def read_simdata_ytrain(dataset, qois=['gpp']): nqois = len(qois) monthnames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] twelve = len(monthnames) print("Dimensions #######################") for ikey in dataset.dimensions.keys(): # ikey==dataset.dimensions[ikey].name print(ikey + ", size " + str(dataset.dimensions[ikey].size)) # 7 nens = dataset.dimensions['ensemble'].size npfts = dataset.dimensions['pft'].size nyears = dataset.dimensions['time'].size / twelve lons = dataset.variables['lon'][:] - 360. # .shape ilons = range(lons.shape[0]) # range(11,14) nlons = len(ilons) lats = dataset.variables['lat'][:] # .shape ilats = range(lats.shape[0]) # range(36,38) nlats = len(ilats) bm = Basemap() #outqois = np.empty((nsites,nqois,2,twelve)) ytrain = np.empty((nens, 0)) outnames = [] xdata = np.empty((0, 5)) #ydata = np.empty((0,)) #jout = 0 for iqoi in range(nqois): qoi = qois[iqoi] for ilon in ilons: lon = lons[ilon] for ilat in ilats: lat = lats[ilat] if bm.is_land(lon, lat): print('lon={:d}, lat={:d} ({:2.2f}, {:2.2f}) is land'.format(ilon, ilat, lon, lat)) aa = dataset.variables[qoi][ :, 0, :, ilat, ilon].reshape(nens, -1, twelve) # print iqoi, ilat, ilon, aa ytrain = np.append(ytrain, np.average(aa, axis=1), axis=1) ytrain = np.append(ytrain, np.std(aa, axis=1), axis=1) for imo in range(1, twelve + 1): xdata = np.append( xdata, [[lon, lat, iqoi, imo, 0]], axis=0) outnames.append( qoi + ' ' + str(lat) + ' ' + str(lon) + ' mean ' + monthnames[imo - 1]) for imo in range(1, twelve + 1): xdata = np.append( xdata, [[lon, lat, iqoi, imo, 1]], axis=0) outnames.append( qoi + ' ' + str(lat) + ' ' + str(lon) + ' stdev ' + monthnames[imo - 1]) else: print('lon={:d}, lat={:d} ({:2.2f}, {:2.2f}) is not land'.format(ilon, ilat, lon, lat)) return xdata, outnames, ytrain
def propagate_towards_land(proj, res, xmin, xmax, ymin, ymax, lon, lat): rcParams['figure.subplot.hspace'] = 0.1 # less height between subplots fig = plt.figure() # Initialise map startTime = datetime.now() map = Basemap(llcrnrlon=xmin, llcrnrlat=ymin, urcrnrlon=xmax, urcrnrlat=ymax, resolution=res, projection=proj) ax = fig.add_subplot(211) map.drawcoastlines() map.drawmapboundary(fill_color='aqua') map.fillcontinents(color='coral', lake_color='aqua') initTime = datetime.now() - startTime # Propagate single particle until hitting coast lonHistory = np.array([lon]) latHistory = np.array([lat]) startTime = datetime.now() while not map.is_land(lon, lat): lon = lon + 0.01 lat = lat + 0.001 lonHistory = np.append(lonHistory, lon) latHistory = np.append(latHistory, lat) calcTime = datetime.now() - startTime # Plot and save figure plt.text( xmin, ymax, 'initialisation time: ' + str(initTime.total_seconds()) + 's\n' 'calculation time: ' + str(calcTime.total_seconds()) + 's\n' 'iterations: ' + str(len(latHistory)) + ', resolution: ' + res) map.plot(lonHistory, latHistory, '.b') map.plot(lon, lat, '*y') filename = 'figures_test_landmask_map_size/%s-%s-%s-%s-%s-%s.png' \ % (proj, res, xmin, xmax, ymin, ymax) # Fullres zoom around landing point ax = fig.add_subplot(212) subsetmap = Basemap(llcrnrlon=lon - 0.5, llcrnrlat=lat - .5, urcrnrlon=lon + 0.5, urcrnrlat=lat + .5, resolution='f', projection=proj) subsetmap.drawcoastlines() subsetmap.drawmapboundary(fill_color='aqua') subsetmap.fillcontinents(color='coral', lake_color='aqua') subsetmap.plot(lonHistory, latHistory, '*b') subsetmap.plot(lon, lat, '.y') #plt.show() plt.savefig(filename) print 'Saved figure: ' + filename
def read_simdata_ytrain_reg(dataset, qois=['gpp', 'lai'], lonrange=[-97., -66.], latrange=[28., 50.], ens=None): nqois = len(qois) nens = dataset.dimensions['ensemble'].size npfts = dataset.dimensions['pft'].size nmonths = dataset.dimensions['time'].size lons = dataset.variables['lon'][:] - 360. # .shape lonmask = np.array([lons > lonrange[0]])[0] * np.array( [lons < lonrange[1]])[0] nlons = np.sum(lonmask) lats = dataset.variables['lat'][:] # .shape latmask = np.array([lats > latrange[0]])[0] * np.array( [lats < latrange[1]])[0] nlats = np.sum(latmask) if ens is None: ens = nens bm = Basemap() ytrain = np.empty((ens, 0)) outnames = [] xdata = np.empty((0, 4)) #ydata = np.empty((0,)) #jout = 0 for ilon in range(nlons): lon = lons[lonmask][ilon] for ilat in range(nlats): lat = lats[latmask][ilat] if bm.is_land(lon, lat): print('lon={:d}, lat={:d} ({:2.2f}, {:2.2f}) is land'.format( ilon, ilat, lon, lat)) for iqoi in range(nqois): qoi = qois[iqoi] aa = dataset.variables[qoi][:ens, 0, :, ilat, ilon] # print iqoi, ilat, ilon, aa ytrain = np.hstack((ytrain, aa)) for imo in range(nmonths): xdata = np.append(xdata, [[lon, lat, iqoi, imo]], axis=0) outnames.append(qoi + ' ' + str(lat) + ' ' + str(lon) + ' mo' + str(imo)) else: print( 'lon={:d}, lat={:d} ({:2.2f}, {:2.2f}) is not land'.format( ilon, ilat, lon, lat)) return xdata, outnames, ytrain
def cleanContinents(m, xpoints, ypoints): """ Takes in the projection m and the grid of x and y clears out the points that are inside continents returns a matrix where each column corresponds to a value in xpoints """ seaMap = [[j for j in ypoints if not Basemap.is_land(m, m(i, j)[0], m(i, j)[1])] for i in xpoints] return seaMap
def generate_full_landmask(resolution_lat, resolution_lon): """ Similar to the above generate_landmask, but creates a landmask for the entire world, and adds Antarctica as another ice region to ignore """ t0 = t.time() projection = Basemap(projection='cyl') # min lat / min lon / max lat / max lon glm1 = np.array([60., -60., 85., -25.]) glm2 = np.array([70., -25., 85., -10.]) glm3 = np.array([75., -70., 85., -60.]) aam = np.array([-90., -180., -60., 180.]) glmask = [glm1, glm2, glm3, aam] halfstep_lat = 0.5 * resolution_lat halfstep_lon = 0.5 * resolution_lon lons = np.arange(-180., 180. + halfstep_lon, resolution_lon) lats = np.arange(-90., 90. + halfstep_lat, resolution_lat) #print(len(lons), len(lats)) landmask = np.zeros([lats.size, lons.size]) for i, lat in enumerate(lats): for j, lon in enumerate(lons): gl = False for glm in glmask: if glm[0] <= lat <= glm[2] and glm[1] <= lon <= glm[3]: gl = True if not gl: landmask[i, j] = projection.is_land(lon, lat) if resolution_lat >= 1.: resstr_lat = str(int(resolution_lat)) else: resstr_lat = str(resolution_lat).replace('.', '') if resolution_lon >= 1.: resstr_lon = str(int(resolution_lon)) else: resstr_lon = str(resolution_lon).replace('.', '') np.save( "../earth_data/landmask_full_{0}deg_lat_{1}deg_lon.npy".format( resstr_lat, resstr_lon), landmask) print("Generated full landmask in {} seconds".format(t.time() - t0)) return landmask
def propagate_towards_land(proj, res, xmin, xmax, ymin, ymax, lon, lat): rcParams['figure.subplot.hspace'] = 0.1 # less height between subplots fig = plt.figure() # Initialise map startTime = datetime.now() map = Basemap(llcrnrlon=xmin,llcrnrlat=ymin, urcrnrlon=xmax, urcrnrlat=ymax, resolution=res, projection=proj) ax = fig.add_subplot(211) map.drawcoastlines() map.drawmapboundary(fill_color='aqua') map.fillcontinents(color='coral',lake_color='aqua') initTime = datetime.now()-startTime # Propagate single particle until hitting coast lonHistory = np.array([lon]) latHistory = np.array([lat]) startTime = datetime.now() while not map.is_land(lon, lat): lon = lon + 0.01 lat = lat + 0.001 lonHistory = np.append(lonHistory, lon) latHistory = np.append(latHistory, lat) calcTime = datetime.now()-startTime # Plot and save figure plt.text(xmin, ymax, 'initialisation time: ' + str(initTime.total_seconds()) + 's\n' 'calculation time: ' + str(calcTime.total_seconds()) + 's\n' 'iterations: ' + str(len(latHistory)) + ', resolution: ' + res) map.plot(lonHistory, latHistory, '.b') map.plot(lon, lat, '*y') filename = 'figures_test_landmask_map_size/%s-%s-%s-%s-%s-%s.png' \ % (proj, res, xmin, xmax, ymin, ymax) # Fullres zoom around landing point ax = fig.add_subplot(212) subsetmap = Basemap(llcrnrlon=lon-0.5,llcrnrlat=lat-.5, urcrnrlon=lon+0.5, urcrnrlat=lat+.5, resolution='f', projection=proj) subsetmap.drawcoastlines() subsetmap.drawmapboundary(fill_color='aqua') subsetmap.fillcontinents(color='coral',lake_color='aqua') subsetmap.plot(lonHistory, latHistory, '*b') subsetmap.plot(lon, lat, '.y') #plt.show() plt.savefig(filename) print 'Saved figure: ' + filename
def latBucketing(rawData,dx): buckets = {-90 + i*dx:[] for i in range(180/dx+1)} for bucket in buckets: for i in range(len(rawData['lon'])): x = rawData['lon'][i] y = rawData['lat'][i] if bucket + dx > y >= bucket: if not Basemap.is_land(m,m(x,y)[0],m(x,y)[1]): buckets[bucket].append(rawData['classif'][i]) rates = ratesInBuckets(buckets) return rates
def get_ocean_mask(): from mpl_toolkits.basemap import Basemap m = Basemap(rsphere=6378137, resolution='c', projection='cea', lat_0=0., lon_0=0., llcrnrlat=-90., urcrnrlat=90., llcrnrlon=-180., urcrnrlon=180.) (x, y) = m(grd[0], grd[1]) ocean_mask = map(lambda (x, y): not m.is_land(x, y), zip(x, y)) return ocean_mask
class HurricaneFeatureExpander: """ Hurricane Feature Expander Class that expands a data matrix with hurricane data with new features. """ def __init__(self, data_matrix, temp_data): """ Initializes the Hurricane Feature Expander with a data matrix. It assumes the first features are as follows: lat, lon, year, month. """ self.data_matrix = data_matrix self.temp_data = temp_data self.bm = Basemap() def get_data_matrix(self): """Returns the data matrix at the current state.""" return self.data_matrix def add_is_land(self): """ Adds a binary feature of whether the hurricane is on land (1) or on sea (0), for each hurricane sample. Assumes `lat` is column 0 and `lon` is column 1 in the data matrix. """ is_land_feature = [ self.bm.is_land(x[0], x[1]) for x in self.data_matrix ] is_land_feature = np.array(is_land_feature).reshape(-1, 1) np.hstack((self.data_matrix, is_land_feature)) def add_temperature(self): """ Adds the temperature feature to each sample in the data matrix. It uses the lat, lon, and time values to figure out the temperature at the time and location of the hurricane. Assumes `lat` is column 0, `lon` is column 1, `year` is column 2, and `month` is column 3 in the data matrix. """ # 1. Create the temperature feature column temperature_feature = [self.temp_data.get(x) for x in self.data_matrix] temperature_feature = np.array(temperature_feature).reshape(-1, 1) # 2. Append the temperature feature column np.hstack((self.data_matrix, temperature_feature))
def mask_land_basemap(lat, lon): from mpl_toolkits.basemap import Basemap m = Basemap(resolution="l", projection='laea', lat_ts=90, lat_0=90., lon_0=0., llcrnrlon=131.73705463408294, llcrnrlat=79.651813071006984, urcrnrlon=133.06651815041363, urcrnrlat=68.673879575562182, rsphere=6371228) x, y = m(lon, lat) land = m.is_land(x, y) return land
def points_in_region(regions, npoints, is_land): regions = fix_regions(regions) m = Basemap(projection='cyl',llcrnrlat=-65,urcrnrlat=90,\ llcrnrlon=-180,urcrnrlon=180,resolution='c') points = [] coords = get_coords() i = 0 while i < npoints: region = regions[np.random.randint(0, len(regions))] x_point = np.random.randint(coords[region][0][0], coords[region][0][1]) y_point = np.random.randint(coords[region][1][0], coords[region][1][1]) if m.is_land(x_point, y_point) == is_land: points.append((x_point, y_point)) i += 1 return (points)
def grid_is_land(grid, themap='', resolution='l'): """Given a list of (lat,lon) points, return only the points that are on land, according to the GSHHG shoreline database. A Basemap projection map can be specified as input. If it is not, it will build the Basemap from the input grid. In that case, the resolution level can be specified as input ('l'=low,'i'=intermediate,'h'=high,'f'=full, where usually intermediate is good enough, for maps that span less than ~100 miles 'h' should be used)""" y, x = zip(*grid) if themap == '': themap = Basemap(llcrnrlon=min(x), llcrnrlat=min(y), urcrnrlon=max(x), urcrnrlat=max(y), resolution=resolution) elif not isinstance(themap, Basemap): raise Exception('Input projection map themap is not Basemap instance.') grid_out = [p for p in grid if themap.is_land(p[1], p[0])] return grid_out
def cleanContinents(m,xpoints,ypoints,predictions): cleanPredictions = [] seaMap = [] for i in range(len(xpoints)): xi = xpoints[i] pred = [] y = [] for j in range(len(ypoints)): yj = ypoints[j] if not Basemap.is_land(m,m(xi,yj)[0],m(xi,yj)[1]): pred.append(predictions[i][j]) y.append(yj) cleanPredictions.append(pred) seaMap.append(y) return cleanPredictions,seaMap
def get_coasts(lat, lon): import numpy as np from mpl_toolkits.basemap import Basemap lon1 = np.empty(np.shape(lon)) for i in range(0, len(lon)): if lon[i] > 30: lon1[i] = -360 + lon[i] else: lon1[i] = lon[i] llclon = np.min(lon1) llclat = np.min(lat) urclon = np.max(lon1) urclat = np.max(lat) m = Basemap(projection='merc', area_thresh=10000, llcrnrlat=llclat, urcrnrlat=urclat, llcrnrlon=llclon, urcrnrlon=urclon, resolution='i') land = np.empty([len(lat), len(lon1)]) for i in range(0, len(lat)): for j in range(0, len(lon1)): x, y = m(lon1[j], lat[i]) land[i, j] = m.is_land(x, y) EC = np.empty(len(lat)) WC = np.empty(len(lat)) ss = int(len(lon1) / 2) kk = np.diff(land, axis=1) for i in range(0, len(lat)): if any(kk[i, :] == -1): WC[i] = int(np.where(kk[i, 1:ss] == -1)[0][0]) + 2 else: WC[i] = 0 for i in range(0, len(lat)): if any(kk[i, ss:] == 1): EC[i] = int(np.where(kk[i, ss:] == 1)[0][0]) + 1 + ss else: EC[i] = len(lon1) return EC, WC, land
def bucketOceans(m,rawData): oceanDict = {ocean:{} for ocean in oceans} for i in range(len(rawData['lat'])): x = rawData['lon'][i] y = rawData['lat'][i] classif = rawData['classif'][i] ocean = oceanBoundaries(x,y) if not Basemap.is_land(m,m(x,y)[0],m(x,y)[1]): if ocean in oceans: if classif not in oceanDict[ocean]: oceanDict[ocean][classif] = 1 else: oceanDict[ocean][classif] += 1 print float(i)/len(rawData['lat']) print 'Bucketing done with a bucket of len '+str(len(oceanDict)) return oceanDict
def generate_data(eventType, numberOfEvents, radius, eventTime): print "Event Type: %s" % eventType print "Number of Events: %d" % numberOfEvents print "Radius: %d" % radius print "Time per Event: %f" % eventTime bm = Basemap() # For testing purposes: to check it coordinates are valid land coordinates. # print(bm.is_land(long, lat)) # Predetermined long and lat of disaster: disasterLong = -80.883120 disasterLat = 37.621608 i = 0 count = 1 coordinateData = [] while i < numberOfEvents: x, y = uniform(disasterLat - radius, disasterLat + radius), uniform(disasterLong - radius, disasterLong + radius) # Checking for radius to be within range: if disasterLat + radius >= x >= disasterLat - radius and disasterLong + radius >= y >= disasterLong - radius: if bm.is_land(y, x): print(count, x, y) coordinate_input = "new google.maps.LatLng(%f, %f)" % (x, y) coordinateData.append(coordinate_input) # Create call to MySQL Database to insert lat and long insert_to_database(x, y, eventType) i += 1 count += 1 # Sleep for next set of data to generate. sleep(eventTime) seen = set() uniq = [x for x in coordinateData if x not in seen and not seen.add(x)] pprint(uniq) item_count = len(uniq) print("Total items:", item_count)
def plotMap2(data2d): fig = figure(figsize=(12, 6)) # ax=fig.add_axes([0.1,-0.1,0.8,1.3]) m = Basemap(llcrnrlon=-120., llcrnrlat=20., urcrnrlon=-60., urcrnrlat=50., rsphere=(6378137.00, 6356752.3142), resolution='l', projection='merc', lat_0=40., lon_0=-20., lat_ts=0.) m.drawcoastlines() # m.fillcontinents() # draw parallels m.drawparallels(np.arange(50, 30, -10), labels=[1, 1, 0, 1]) # draw meridians m.drawmeridians(np.arange(-120, -60, 30), labels=[1, 1, 0, 1]) m.drawlsmask(land_color='Linen', ocean_color='#CCFFFF') # m.drawstates() # m.drawcountries() x, y = m(data2d[:, 0], data2d[:, 1]) # plot(x,y,'o') triang = tri.Triangulation(x, y) xmid = x[triang.triangles].mean(axis=1) ymid = y[triang.triangles].mean(axis=1) aa = [m.is_land(xm, ym) for xm, ym in zip(xmid, ymid)] mask = np.where(aa, 0, 1) triang.set_mask(mask) # tripcolor(x,y,z) tricontourf(triang, data2d[:, 2], 20) colorbar(fraction=.02, pad=0.1) # location='bottom', pad="10%") savefig('map2.eps') # show() return
def get_ocean_mask(): print('Getting ocean mask...') from mpl_toolkits.basemap import Basemap latmin = grd[1].min() latmax = grd[1].max() lonmin = grd[0].min() lonmax = grd[0].max() print("Latitude {}--{},\n\ Longitude {}--{}".format( round(latmin,2), round(latmax,2), round(lonmin,2), round(lonmax,2))) m = Basemap(rsphere=6378137,resolution=coastres,projection='cea', llcrnrlat=latmin,urcrnrlat=latmax, llcrnrlon=lonmin,urcrnrlon=lonmax) (east,north) = m(grd[0],grd[1]) ocean_mask = [not m.is_land(x,y) for (x,y) in zip(east,north)]#list(map(lambda x,y: not m.is_land(x,y),zip(x,y))) return np.array(ocean_mask)
def get_ocean_mask(): print('Getting ocean mask...') from mpl_toolkits.basemap import Basemap latmin = grd[1].min() latmax = grd[1].max() lonmin = grd[0].min() lonmax = grd[0].max() print("Latitude {}--{},\n\ Longitude {}--{}".format(round(latmin, 2), round(latmax, 2), round(lonmin, 2), round(lonmax, 2))) m = Basemap(rsphere=6378137, resolution=coastres, projection='cea', llcrnrlat=latmin, urcrnrlat=latmax, llcrnrlon=lonmin, urcrnrlon=lonmax) (east, north) = m(grd[0], grd[1]) ocean_mask = [not m.is_land(x, y) for (x, y) in zip(east, north) ] #list(map(lambda x,y: not m.is_land(x,y),zip(x,y))) return np.array(ocean_mask)
def addTrackNoise(hurricane_list, noise_type="normal", cov=np.array([[0.1, 0], [0, 0.1]]), radius=0.25): for hurricane in hurricane_list: track_len = len(hurricane.track) noise = np.zeros((track_len, 2)) for i in range(track_len): if noise_type == "normal": noise[i, :] = np.random.multivariate_normal([0, 0], cov) elif noise_type == "uniform": noise[i, :] = radius * 2 * (np.random.rand(2) - 0.5) hurricane.track = hurricane.track + np.cumsum(noise, axis=0) # Check if landfall bm = Basemap() for i in range(track_len): [lat, lon] = hurricane.track[i] if (bm.is_land(lon, lat)): hurricane.made_landfall = True break else: hurricane.made_landfall = False
print '\t%6.2f seconds to initialise map' % \ (datetime.now()-startTime).total_seconds() # Extract polygons for faster checking of stranding polys = [p.boundary for p in map.landpolygons] # Check beaching of random points within map bounds npoints = 1000 isLand = [0]*npoints np.random.seed(1) x = np.random.uniform(xmin, xmax, npoints) y = np.random.uniform(ymin, ymax, npoints) # using Basemap.is_land() startTime = datetime.now() isLandBasemap = [map.is_land(X, Y) for X,Y in zip(x,y)] print '\t%6.2f seconds to check that %s points out of %s are ' \ 'stranded, using Basemap.is_land()' % \ ((datetime.now()-startTime).total_seconds(), \ sum(isLandBasemap), npoints) # using polygons startTime = datetime.now() isLandPoly = points_in_polys(np.c_[x, y], polys) print '\t%6.2f seconds to check that %s points out of %s are ' \ 'stranded, using matplotlib polygons' % \ ((datetime.now()-startTime).total_seconds(), \ sum(isLandPoly), npoints) print print '########################################'
plt.ylim(Yrange[0], Yrange[1]) # ======================== # plot inlays for land, ocean, and global rgsGlobal = ['Global', 'Global Land', 'Global Ocean'] for sr in range(len(rgsGlobal)): for da in range(len(rgsData)): if rgsData[da] == 'Rad-Soundings': LON = rgrLonAct LAT = rgrLatAct DATA = rgrROdataAct rgiYearsAct = np.array(range(1979, 2018, 1)) rgiSelYY = ((rgiYearsAct >= 1979) & (rgiYearsAct <= 2010)) sColor = 'k' rgiLandOcean = [ bm.is_land(LON[ll], LAT[ll]) for ll in range(len(LAT)) ] if rgsData[da] == 'ERA-20C': LON = rgrLonGrid2D.flatten() LAT = rgrLatGrid2D.flatten() DATA = np.reshape( rgrDataVarAct, (rgrDataVarAct.shape[0], rgrDataVarAct.shape[1], rgrDataVarAct.shape[2] * rgrDataVarAct.shape[3])) / 1000. rgiYearsAct = np.array(range(1900, 2011, 1)) rgiSelYY = ((rgiYearsAct >= 1979) & (rgiYearsAct <= 2010)) sColor = '#1f78b4' # T2M DATAT2M = np.reshape( rgrDataVarActT2M,
point_1 = map(lon_1, lat_1) distance = np.sqrt((point_1[0] - point_0[0])**2 + (point_1[1] - point_0[1])**2) # Set the number of points such that the interval between points is 10 km number_points = int(distance / 1.e4) + 1. # Dissect the line connecting the 2 points Points = map.gcpoints(lon_0,lat_0,lon_1,lat_1,number_points) # Check whether the points are on land i = 0 Land = [] while i < number_points: a = (Points[0])[i] b = (Points[1])[i] Land.append(map.is_land(a,b)) i += 1 print Land # List to record landfall raw data landfall_raw = [] # Check for landfall points i = 0 while i < (number_points -2): a = Land[i] b = Land[i+1] if a != b: #If moving from sea to land then include if a == False:
def main(): usage = """ python create_grid.py """ num_args= 0 parser = OptionParser(usage=usage) parser.add_option('', '--center-x', dest='center_x', default=None, help="Center the grid on x (longitude) coordinate", type=float) parser.add_option('', '--center-y', dest='center_y', default=None, help="Center the grid on y (latitude) coordinate", type=float) parser.add_option('', '--grid_width', dest='grid_width', default=60, help='How wide the grid is in degrees', type=float) parser.add_option('-r', '--resolution', dest='resolution', default=4, help="The resolution we want the grid to be at", type='int') parser.add_option('-o', '--output-file', dest='output_file', default=None, help="A file to dump the output to", type='str') parser.add_option('', '--min-x', dest='min_x', default=None, help='The minimum longitude', type='float') parser.add_option('', '--max-x', dest='max_x', default=None, help='The maximum longitude', type='float') parser.add_option('', '--min-y', dest='min_y', default=None, help='The minimum latitude', type='float') parser.add_option('', '--max-y', dest='max_y', default=None, help='The maximum latitude', type='float') #parser.add_option('-o', '--options', dest='some_option', default='yo', help="Place holder for a real option", type='str') #parser.add_option('-u', '--useless', dest='uselesss', default=False, action='store_true', help='Another useless option') (options, args) = parser.parse_args() if options.center_x is not None: options.min_x = options.center_x - options.grid_width / 2; options.max_x = options.center_x + options.grid_width / 2; if options.center_y is not None: options.min_y = options.center_y - options.grid_width / 2; options.max_y = options.center_y + options.grid_width / 2; if options.max_y > 90: options.max_y = 90 if options.min_y < -90: options.min_y = 90 xs = np.linspace(options.min_x, options.max_x, options.resolution) ys = np.linspace(options.min_y, options.max_y, options.resolution) from mpl_toolkits.basemap import Basemap my_map = Basemap(llcrnrlon=options.min_x,llcrnrlat=options.min_y, urcrnrlon=options.max_x,urcrnrlat=options.max_y,\ rsphere=(6378137.00,6356752.3142),\ resolution='l',area_thresh=1000.,projection='merc',\ lat_1=50.,lon_0=-107.)# draw coastlines, country boundaries, fill continents. is_land = dict() for x, y in it.product(xs, ys): mx, my = my_map(x, y) if my_map.is_land(mx, my): print x, y (options, args) = parser.parse_args() if len(args) < num_args: parser.print_help() sys.exit(1)
print '\t%6.2f seconds to initialise map' % \ (datetime.now()-startTime).total_seconds() # Extract polygons for faster checking of stranding polys = [p.boundary for p in map.landpolygons] # Check beaching of random points within map bounds npoints = 10000 isLand = [0]*npoints np.random.seed(1) x = np.random.uniform(xmin, xmax, npoints) y = np.random.uniform(ymin, ymax, npoints) # using Basemap.is_land() startTime = datetime.now() isLand = [map.is_land(X, Y) for X,Y in zip(x,y)] print '\t%6.2f seconds to check that %s points out of %s are ' \ 'stranded, using Basemap.is_land()' % \ ((datetime.now()-startTime).total_seconds(), \ sum(isLand), npoints) # using polygons startTime = datetime.now() isLand = points_in_polys(np.c_[x, y], polys) print '\t%6.2f seconds to check that %s points out of %s are ' \ 'stranded, using matplotlib polygons' % \ ((datetime.now()-startTime).total_seconds(), \ sum(isLand), npoints) print print '########################################'
from mpl_toolkits.basemap import Basemap, cm import matplotlib.pyplot as plt m = Basemap(projection='npstere', boundinglat=0, lon_0=0, resolution='l') from pyresample import geometry, utils, image from numpy import * import numpy as np x = -179.5 + arange(360) y = 0.5 + arange(90) lons, lats = np.meshgrid(x, y) x2, y2 = m(lons, lats) cmap = zeros((90, 360), float) for i in range(90): for j in range(360): m1 = m.is_land(x2[i, j], y2[i, j]) if m1 is True: cmap[i, j] = 1 import pickle pickle.dump(cmap, open('cmap.pklz', 'wb')) m.drawcoastlines() m.drawparallels(np.arange(-80., 81., 20.)) m.drawmeridians(np.arange(-180., 181., 20.)) x = -179.5 + arange(360) y = 0.5 + arange(90) lons, lats = np.meshgrid(x, y) x2, y2 = m(lons, lats) import matplotlib m.pcolormesh(x2, y2, cmap, cmap='jet', norm=matplotlib.colors.LogNorm())
cbindbackward=np.concatenate( (cbindbackward,time),axis=0 ) cbmmd=np.concatenate( (cbmmd,mmd),axis=0 ) cbupdf=np.concatenate( (cbupdf,updf),axis=1 ) cbalt=np.concatenate( (cbalt,alt),axis=1 ) # except: # pass #%% from mpl_toolkits.basemap import Basemap m = Basemap(projection='merc',resolution='c',llcrnrlon=110.1,llcrnrlat=-30.1, urcrnrlon=150.1,urcrnrlat=5.1) x,y=m(130,-15) m.is_land(x,y) #%% # land see masks totaln=23 cbls=cbindbackward=cbmmd=np.zeros([0,1]) cbls=np.zeros(0,) cbalt=cbupdf=np.ndarray([500,0]) fltlist = list(range(totaln)) fltlist.remove(12) fltlist.remove(13) # for i in fltlist: for i in range(12,13): szi=i+1
from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt map = Basemap(projection='aeqd', lon_0 = 10, lat_0 = 50, resolution='h') x, y = map(0, 0) print map.is_land(x, y)
m.drawcoastlines() m.drawcountries(linewidth=2) m.drawstates(color='red') # m.drawcounties(color='green') # m.etopo() # Draws topography # m.fillcontinents(color='g', lake_color='b') # m.bluemarble() # 29 north 95 west (Houston, TX) lat, lon = 29.7604, -95.368 xpt, ypt = m(lon, lat) # Plot the xpt as longitude and ypt as latitude, with a red marker m.plot(xpt, ypt, 'r*', markersize=15) # Boulder, CO lat, lon = 40.125, -104.237 xpt, ypt = m(lon, lat) m.plot(xpt, ypt, 'bo') # Hanover, MA lat, lon = 42.1162, -70.8477 xpt, ypt = m(lon, lat) m.plot(xpt, ypt, 'g*') land_check = m.is_land(xpt, ypt) print(land_check) plt.title('Basemap Example with Title') plt.show()
class Grib: def __init__(self, file = 'gribs/20171128_151424_.grb'): self.file = file self.gribfile = pygrib.open(file) def plot(self, idx = 0, show = False): grb = self.gribfile.select()[idx] grbU = self.gribfile.select(name='10 metre U wind component')[idx] grbV = self.gribfile.select(name='10 metre V wind component')[idx] lons = np.linspace(float(grb['longitudeOfFirstGridPointInDegrees']), \ float(grb['longitudeOfLastGridPointInDegrees']), int(grb['Ni']) ) lats = np.linspace(float(grb['latitudeOfFirstGridPointInDegrees']), \ float(grb['latitudeOfLastGridPointInDegrees']), int(grb['Nj']) ) data = np.sqrt(np.square(grbU.values)+np.square(grbV.values)) grid_lon, grid_lat = np.meshgrid(lons, lats) #regularly spaced 2D grid self.map = Basemap(projection='cyl', llcrnrlon=lons.min(), \ urcrnrlon=lons.max(),llcrnrlat=lats.min(),urcrnrlat=lats.max(), \ resolution='c') x, y = self.map(grid_lon, grid_lat) cs =self.map.pcolormesh(x,y,data,shading='flat',cmap=plt.cm.rainbow) self.map.drawcoastlines() self.map.drawmapboundary() self.map.drawparallels(np.arange(-90.,120.,30.),labels=[1,0,0,0]) self.map.drawmeridians(np.arange(-180.,180.,60.),labels=[0,0,0,1]) plt.colorbar(cs,orientation='vertical', shrink=0.5) plt.title('Predicted wind strength') plt.savefig('grib.png') if show: plt.show() plt.close() def interpolator(self): grb = self.gribfile.select()[0] grb1 = self.gribfile.select()[1] grb_l = self.gribfile.select()[-1] lons = np.linspace(float(grb['longitudeOfFirstGridPointInDegrees']), \ float(grb['longitudeOfLastGridPointInDegrees']), int(grb['Ni']) ) lats = np.linspace(float(grb['latitudeOfFirstGridPointInDegrees']), \ float(grb['latitudeOfLastGridPointInDegrees']), int(grb['Nj']) ) grbU = self.gribfile.select(name='10 metre U wind component') grbV = self.gribfile.select(name='10 metre V wind component') times = np.array([i for i,g in enumerate(grbU) if g.values.shape == grb.values.shape]) U = np.dstack([g.values for g in grbU if g.values.shape == grb.values.shape]) V = np.dstack([g.values for g in grbV if g.values.shape == grb.values.shape]) print(lats.shape, lons.shape, times.shape, U.shape) if lats[0] > lats[-1]: lats = np.flip(lats,0) U = np.flip(U, 0) V = np.flip(V,0) self.interpolatorU = RegularGridInterpolator((lats, lons, times), U) self.interpolatorV = RegularGridInterpolator((lats, lons, times), V) def getwind(self, p, time): U = self.interpolatorU((p.lat, p.lon, time)) V = self.interpolatorV((p.lat, p.lon, time)) wind = sqrt(U**2 + V**2) winddir = atan2(U,V)*180/pi return wind, winddir def is_land(self,p): return self.map.is_land(p.lat, p.lon)
resolution='h') x, y = map( lons, lats) # Se cambian de las coordenadas angulares a las de la proyeccion r = np.zeros( (x.shape[0], x.shape[1] )) # Se creo un array de zeros con el mismo tamaño de malla de x o y for i in range( x.shape[0] ): # Se evalúa si cada punto, está o no en tierra. De serlo, la función "is_land" da como resultado True, cambiando el valor de 0 a 1. Si es False, deja el valor en cero for j in range(x.shape[1]): xx = x[i, j] yy = y[i, j] r[i, j] = map.is_land(xx, yy) for i in range( r.shape[0] ): # Se necesita que los valores en tierra sean cero, y los puntos en el océano sean uno. for j in range(r.shape[1]): if r[i, j] == 1: r[i, j] = 0 else: r[i, j] = 1 r[r == 0] = np.NaN # Los puntos asignados con 0, se cambian por NaN for i in range( P.shape[0] ): # Se multiplica cada lámina de la serie de presiones por la matríz r, de tal manera que los puntos en tierra no tendrán presiones (nan), mientras que los del océano sí lo harán
# make these smaller to increase the resolution x = np.arange(-7, 3, 0.05) y = np.arange(49, 59, 0.05) Z = np.zeros((len(x), len(y))) X = np.zeros((len(x), len(y))) Y = np.zeros((len(x), len(y))) m.drawcoastlines() for i in range(len(x)): for j in range(len(y)): p = [x[i], y[j]] best = find_nearest(p) xpt, ypt = m(x[i], y[j]) X[i, j] = xpt Y[i, j] = ypt if m.is_land(xpt, ypt) == True: if xpt < 200000 and ypt < 970000: continue if xpt > 950000 and ypt < 235000: continue if xpt > 766000 and ypt < 104000: continue Z[i, j] = z[best] else: continue ''' dx, dy = 0.1, 0.1 y, x = np.mgrid[slice(49, 59 + dy, dy), slice(-7, 3 + dx, dx)] '''
def get_gfdltrk_track_data(path, flagged_entries_file="", include_flagged_entries=False, log=None, duration=None, skip_land_points=False): ''' Read the output file of the GFDL tracker and return the data as a ForecastTrack object. If `include_flagged_entries=False', only return entries in which the value is not flagged. NOTE: The GFDL tracker may create up to three entries for a given forecast hour, in cases where it produces radii for 34, 50 and 64 kt winds. Since we are only concerned with position, maxwind, and MSLP here, we just pick the first entry for a given forecast hour, since these are all the same regardless of radii. NOTE: The GFDL tracker may not produce an entry for one or more forecast hours if it detects that the storm is too weak. In these cases, flag the entry and fill with zeros. @param path - Path to the ATCF file generated by the tracker (can be the fort.64 or fort.69) @param flagged_entries_file file containing newline-separated forecast hours that should be treated as flagged @param include_flagged_entries - should flagged entries be included? @param log logger to use. If None, just use the python built-in one @param duration Duration of the forecast to process, in seconds. If None, read whatever is in the file @param skip_land_points If True, do not include values that are over land. Will use BaseMap's is_land() method for this. RETURNS A [ForecastTrack] object corresponding to the track file. NOTE: Each ForecastTrack contains the list of TrackerData objects, one for each entry in the track file (which should correspond with the history interval). ''' # This must be imported here to avoid circular dependency from pycane.postproc.tracker.objects import ForecastTrack, TrackerData GFDLTRK_TRACK_FILE_START_DATE_IDX = 2 GFDLTRK_TRACK_FILE_FHR_IDX = 5 #GFDLTRK_TRACK_FILE_FLAG_IDX = 3 GFDLTRK_TRACK_FILE_LAT_IDX = 6 GFDLTRK_TRACK_FILE_LON_IDX = 7 GFDLTRK_TRACK_FILE_MSLP_IDX = 9 #mb GFDLTRK_TRACK_FILE_maxwind_KTS_IDX = 8 if log is None: import logging as log if skip_land_points: from mpl_toolkits.basemap import Basemap map = Basemap(projection='cyl', resolution='c') # default: global flagged_fhrs = [] if include_flagged_entries: if os.path.exists(os.path.join(flagged_entries_file)): flagged_fhrs = [ int(x) for x in open(flagged_entries_file).readlines() ] else: log.debug("The flagged_entries_file does not exist.") processed_fhrs = [] elfile = open(path, 'r') cycle_track = ForecastTrack(-1) # hack! lines = elfile.readlines() for lineNbr, line in enumerate(lines): toks = [x.strip() for x in line.split(",")] if len(toks) not in (43, 20): log.warn( 'Unrecognized line found in GFDL tracker output file, ignoring it:\n\t %s' % line) continue if len(toks) == 20: log.debug("fort.69 file detected") is_fort69 = True elif len(toks) == 43: is_fort69 = False # since gfdltrk ATCF file may contain multiple entries per forecast hour... fhr = int(toks[GFDLTRK_TRACK_FILE_FHR_IDX]) if is_fort69: fhr /= 100 # TODO verify fort.69 always gives fhr*100 if fhr in processed_fhrs: continue #print path, fhr, duration if duration is not None and fhr > int(duration / 3600.0): #import pdb ; pdb.set_trace() log.info( "Skipping forecast hour %i since it is after the specified duration" % fhr) continue processed_fhrs.append(fhr) # check if entry is flagged if fhr in flagged_fhrs: flagged_entry = True else: flagged_entry = False log.debug("Parsing GFDL tracker entry: data values: %s" % toks) # populate the rest of the fields start_date = conversions.yyyymmddHH_to_epoch( toks[GFDLTRK_TRACK_FILE_START_DATE_IDX]) # if tracker detects 850 mb winds < [threshold in NL] or if it cannot find a # "good" mslp gradient, it will not produce a value at that time # for position, mslp, or intensity. # In these cases, flag the entry and set the values such that they can be used if toks[GFDLTRK_TRACK_FILE_LAT_IDX] == '0': log.warn( "Flagging fhr %i for forecast %s. Tracker was unsure about it" % (fhr, toks[GFDLTRK_TRACK_FILE_START_DATE_IDX])) flagged_fhrs.append(fhr) flagged_entry = True toks[GFDLTRK_TRACK_FILE_LAT_IDX] = '000N' toks[GFDLTRK_TRACK_FILE_LON_IDX] = '000E' toks[GFDLTRK_TRACK_FILE_MSLP_IDX] = 0 toks[GFDLTRK_TRACK_FILE_maxwind_KTS_IDX] = 0 lat = int(toks[GFDLTRK_TRACK_FILE_LAT_IDX][:-1]) / 10.0 if toks[GFDLTRK_TRACK_FILE_LAT_IDX][-1] == "S": lat = -lat lon = int(toks[GFDLTRK_TRACK_FILE_LON_IDX][:-1]) / 10.0 if toks[GFDLTRK_TRACK_FILE_LON_IDX][-1] == "W": lon = -lon cycle_track.start_date = start_date # this is hackish! in_bounds = True if skip_land_points: x, y = map(lon, lat) if map.is_land(x, y): in_bounds = False log.info("Skipping tracker entry @ ({},{}) since it is in " "land".format(lat, lon)) if in_bounds and (include_flagged_entries or not flagged_entry): trackData = TrackerData( start_date, fhr, flagged_entry, lat, lon, float(toks[GFDLTRK_TRACK_FILE_MSLP_IDX]), float(toks[GFDLTRK_TRACK_FILE_maxwind_KTS_IDX])) log.debug('Appending GFDL tracker entry %s' % trackData) cycle_track.append_entry(trackData) else: log.info('Skipping flagged entry %s' % line.strip()) return cycle_track
def get_diapost_track_data(path, include_flagged_entries=False, logger=None, duration=None, nswe_thresh=None, skip_land_points=False): ''' Reads Diapost data from the given path. If include_flagged_entries=False, only return entries in which the value is not flagged (i.e. only return those whose flag value is 0) ARGS - path - path to diapost ATCF - include_flagged_entries - If True, process entries flagged by the tracker. Default: False - duration - Duration of forecast to process, in seconds. If not passed in, process the entire file - nswe_thresh - 4-tupple specifying threshold for (North, South, East, West) points. Any points that are outside of these bounds are not added to the returned ForecastTrack. The tupple or individual values may be None, in which case no filtering is done. **NOTE: This has not been well tested ** - skip_land_points If True, do not include values that are over land. Will use BaseMap's is_land() method for this. RETURNS an array of [ForecastTrack] objects, one for each cycle. NOTE: Each ForecastTrack file contains the list of TrackerData objects, one for each entry in the track file (which should correspond with the history interval) ''' # This must be imported here to avoid circular dependency from pycane.postproc.tracker.objects import ForecastTrack, TrackerData if logger is None: logger = logging.getLogger() if skip_land_points: from mpl_toolkits.basemap import Basemap map = Basemap(projection='cyl', resolution='c') # default: global elfile = open(path, 'r') track = ForecastTrack(-1) # this is hackish! for line in elfile.readlines(): toks = line.split() if len(toks) != 8: logger.warn( 'Unrecognized line found in Diapost file, ignoring it:\n\t %s' % line) continue fhr = int(toks[DIAPOST_TRACK_FILE_FHR_IDX]) if duration is not None and fhr > int(duration / 3600.0): logger.info( "Skipping forecast hour %i since it is after the specified duration" % fhr) continue if int(toks[DIAPOST_TRACK_FILE_FLAG_IDX]) == 0: flagged_entry = False else: flagged_entry = True start_date = conversions.yyyymmddHH_to_epoch( toks[DIAPOST_TRACK_FILE_START_DATE_IDX]) track.start_date = start_date # this is hackish! lat = float(toks[DIAPOST_TRACK_FILE_LAT_IDX]) lon = float(toks[DIAPOST_TRACK_FILE_LON_IDX]) in_bounds = True if nswe_thresh is not None: in_bounds = _check_if_in_bounds(lat, lon, nswe_thresh) if not in_bounds: logger.info("Skipping tracker entry @ ({},{}) since it lies" #" outside of threshold ({},{},{},{})" " outside of threshold ({})".format( lat, lon, nswe_thresh)) if skip_land_points and in_bounds: # only go in here if w/in nswe_thresh x, y = map(lon, lat) if map.is_land(x, y): in_bounds = False logger.info("Skipping tracker entry @ ({},{}) since it is in " "land".format(lat, lon)) if in_bounds and (include_flagged_entries or not flagged_entry): trackData = TrackerData( start_date, fhr, flagged_entry, float(toks[DIAPOST_TRACK_FILE_LAT_IDX]), float(toks[DIAPOST_TRACK_FILE_LON_IDX]), float(toks[DIAPOST_TRACK_FILE_MSLP_IDX]), float(toks[DIAPOST_TRACK_FILE_maxwind_KTS_IDX])) logger.debug('Appending diapost entry %s' % trackData) track.append_entry(trackData) else: logger.info('Skipping flagged entry %s' % line.strip()) return track
def isOcean(x,y): return not Basemap.is_land(m,m(x,y)[0],m(x,y)[1])
class get_fvcom(): def __init__(self, mod): self.modelname = mod def get_url(self, starttime, trackdays): ''' get different url according to starttime and endtime. urls are monthly. ''' #self.hours = int(round((endtime-starttime).total_seconds()/60/60)) endtime = starttime + timedelta(trackdays) self.starttime = starttime; self.endtime = endtime self.trackdays = trackdays #int(round((endtime-starttime).total_seconds()/60/60/24)) #print self.hours if self.modelname == "global": turl = '''http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_FVCOM_OCEAN_GLOBAL_FORECAST.nc''' try: self.tdata = netCDF4.Dataset(turl).variables #MTime = tdata['time'] MTime = self.tdata['Times'] except: print '"massbay" database is unavailable!' raise Exception() #Times = netCDF4.num2date(MTime[:],MTime.units) Times = [] for i in MTime: strt = '201'+i[3]+'-'+i[5]+i[6]+'-'+i[8]+i[9]+' '+i[11]+i[12]+':'+i[14]+i[15] Times.append(datetime.strptime(strt,'%Y-%m-%d %H:%M'))#''' fmodtime = Times[0]; emodtime = Times[-1]+timedelta(days=1) if starttime<fmodtime or starttime>emodtime or endtime<fmodtime or endtime>emodtime: #print 'Time: Error! Model(global) only works between %s with %s(UTC).'%(fmodtime,emodtime) #raise Exception() url = 'error' return url,fmodtime,emodtime ''' npTimes = np.array(Times) tm1 = npTimes-starttime; #tm2 = mtime-t2 index1 = np.argmin(abs(tm1))# #index1 = netCDF4.date2index(starttime,MTime,select='nearest') index2 = index1 + self.days# #print 'index1,index2',index1,index2 #url = url.format(index1, index2) self.mTime = Times[index1:index2] #''' self.mTime = np.array(Times) #self.url = turl #loncs = self.tdata['lonc'][:]; self.latc = self.tdata['latc'][:] #quantity:165095 # Basic model data. self.basicdata = np.load('/var/www/cgi-bin/ioos/track/FVCOM_global_basic_data.npz') self.lonc, self.latc = self.basicdata['lonc'], self.basicdata['latc'] #quantity:165095 self.lons, self.lats = self.basicdata['lon'], self.basicdata['lat'] self.h = self.basicdata['h']; self.siglay = self.basicdata['siglay']#; print '3' # Real-time model data. self.iy = [i for i in range(len(self.mTime)) if self.mTime[i].day==self.starttime.day] self.mtime = np.array(Times[self.iy[0]:self.iy[0]+int(trackdays)+1]) return turl,fmodtime,emodtime def get_globaltrack(self,lon,lat,depth,track_way): #,b_index,nvdepth,,bcon ''' Get forecast points start at lon,lat ''' modpts = dict(lon=[lon], lat=[lat]) # For boundary. self.gmap=Basemap(projection='cyl',llcrnrlat=lat-self.trackdays, urcrnrlat=lat+self.trackdays, llcrnrlon=lon-self.trackdays, urcrnrlon=lon+self.trackdays,resolution='l') nodeindex = np.argwhere((self.lons >= lon-0.3) & (self.lons <= lon+0.3) & (self.lats >= lat-0.3) & (self.lats <= lat+0.3)) if len(nodeindex)==0: print 'No model data around. Out of Model area.' return modpts waterdepth = self.h[[nodeindex]]; #print waterdepth if np.mean(waterdepth)<(abs(depth)): print 'This point is too shallow.Less than %d meter.'%abs(depth) return modpts #raise Exception() depth_total = self.siglay[:,nodeindex[0]]*np.mean(waterdepth); #print depth_total layer = np.argmin(abs(depth_total+depth)); #print 'layer',layer u = self.tdata['u'][self.iy[0]:self.iy[0]+int(self.trackdays)+1,layer,:]; #print '5' v = self.tdata['v'][self.iy[0]:self.iy[0]+int(self.trackdays)+1,layer,:]; #modpts = [(lon,lat)]#;st = [] ld = self.trackdays*12 for j in xrange(int(ld)): if self.gmap.is_land(lat,lon): return modpts inds = np.argwhere((self.lonc >= lon-0.3) & (self.lonc <= lon+0.3) & (self.latc >= lat-0.3) & (self.latc <= lat+0.3)) if len(inds) == 0: return modpts#print 'inds',inds if track_way=='backward' : # backwards case tratime = self.endtime-timedelta(hours=j*2) iday = [i for i in range(len(self.mtime)) if self.mtime[i].day==tratime.day]; #print iday u_t1 = np.mean(u[iday[0]][inds])*(-1); v_t1 = np.mean(v[iday[0]][inds])*(-1) else: tratime = self.starttime+timedelta(hours=j*2) iday = [i for i in range(len(self.mtime)) if self.mtime[i].day==tratime.day]; #print iday u_t1 = np.mean(u[iday[0]][inds]); v_t1 = np.mean(v[iday[0]][inds]) dx = 2*60*60*u_t1; dy = 2*60*60*v_t1 #pspeed = math.sqrt(u_t1**2+v_t1**2) #modpts['spd'].append(pspeed) lon = lon + (dx/(111111*np.cos(lat*np.pi/180))) lat = lat + dy/111111 #''' modpts['lon'].append(lon); modpts['lat'].append(lat) return modpts def get_data(self, starttime, endtime): ''' get different url according to starttime and trackdays. urls are monthly. ''' #endtime = starttime + timedelta(days=trackdays) self.hours = int(round((endtime-starttime).total_seconds()/3600)) #self.hours = trackdays*24 #print self.hours if self.modelname == "GOM3": turl = '''http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_GOM3_FORECAST.nc''' todata = netCDF4.Dataset(turl).variables mTime = todata['Times'][:] Times = [] for i in mTime: strt = '201'+i[3]+'-'+i[5]+i[6]+'-'+i[8]+i[9]+' '+i[11]+i[12]+':'+i[14]+i[15] Times.append(datetime.strptime(strt,'%Y-%m-%d %H:%M')) fmodtime = Times[0]; emodtime = Times[-1] if starttime<fmodtime or starttime>emodtime or endtime<fmodtime or endtime>emodtime: url = 'error' return url,fmodtime,emodtime npTimes = np.array(Times) tm1 = npTimes-starttime.replace(minute=0,second=0,microsecond=0); #tm2 = mtime-t2 self.ind1 = np.argmin(abs(tm1)) self.ind2 = self.ind1 + self.hours#''' self.datatime = npTimes[self.ind1:self.ind2] # Basic model data. self.basicdata = np.load('/var/www/cgi-bin/ioos/track/FVCOM_GOM3_basic_data.npz') self.lonc, self.latc = self.basicdata['lonc'], self.basicdata['latc'] #quantity:165095 self.lons, self.lats = self.basicdata['lon'], self.basicdata['lat'] self.h = self.basicdata['h']; self.siglay = self.basicdata['siglay']#; print '3' self.b_points = self.basicdata['b_points'] # real-time model data self.u = todata['u']; self.v = todata['v']; elif self.modelname == "massbay": turl = "http://www.smast.umassd.edu:8080/thredds/dodsC/FVCOM/NECOFS/Forecasts/NECOFS_FVCOM_OCEAN_MASSBAY_FORECAST.nc" todata = netCDF4.Dataset(turl).variables mTime = todata['Times'][:] Times = [] for i in mTime: strt = '201'+i[3]+'-'+i[5]+i[6]+'-'+i[8]+i[9]+' '+i[11]+i[12]+':'+i[14]+i[15] Times.append(datetime.strptime(strt,'%Y-%m-%d %H:%M')) fmodtime = Times[0]; emodtime = Times[-1] if starttime<fmodtime or starttime>emodtime or endtime<fmodtime or endtime>emodtime: url = 'error' return url,fmodtime,emodtime npTimes = np.array(Times) tm1 = npTimes-starttime.replace(minute=0,second=0,microsecond=0); #tm2 = mtime-t2 self.ind1 = np.argmin(abs(tm1)) self.ind2 = self.ind1 + self.hours#''' self.datatime = npTimes[self.ind1:self.ind2] # Basic model data. self.basicdata = np.load('/var/www/cgi-bin/ioos/track/FVCOM_massbay_basic_data.npz') self.lonc, self.latc = self.basicdata['lonc'], self.basicdata['latc'] #quantity:165095 self.lons, self.lats = self.basicdata['lon'], self.basicdata['lat'] self.h = self.basicdata['h']; self.siglay = self.basicdata['siglay'] self.b_points = self.basicdata['b_points'] # real-time model data self.u = todata['u']; self.v = todata['v']; elif self.modelname == "30yr": #start at 1977/12/31 23:00, end at 2014/1/1 0:0, time units:hours turl = """http://www.smast.umassd.edu:8080/thredds/dodsC/fvcom/hindcasts/30yr_gom3""" #index1 = int(round((starttime-datetime(1977,12,31,22,58,4,0,pytz.UTC)).total_seconds()/3600)) todata = netCDF4.Dataset(turl).variables mtime = todata['time'][:] fmodtime = datetime(1858,11,17) + timedelta(float(mtime[0])) emodtime = datetime(1858,11,17) + timedelta(float(mtime[-1])) # get number of days from 11/17/1858 t1 = (starttime - datetime(1858,11,17)).total_seconds()/86400 t2 = (endtime - datetime(1858,11,17)).total_seconds()/86400 if not mtime[0]<t1<mtime[-1] or not mtime[0]<t2<mtime[-1]: #raise Exception('massbay works from 1977/12/31 23:00 to 2014/1/1 0:0.') url = 'error' return url,fmodtime,emodtime tm1 = mtime-t1; #tm2 = mtime-t2 self.ind1 = np.argmin(abs(tm1)); #index2 = np.argmin(abs(tm2)); print index1,index2 self.ind2 = self.ind1 + self.hours Times = [] for i in range(self.hours): Times.append(starttime+timedelta(i)) self.datatime = Times # real-time model data self.u = todata['u']; self.v = todata['v']; # Basic model data. self.basicdata = np.load('/var/www/cgi-bin/ioos/track/FVCOM_30yr_basic_data.npz') self.lonc, self.latc = self.basicdata['lonc'], self.basicdata['latc'] #quantity:165095 self.lons, self.lats = self.basicdata['lon'], self.basicdata['lat'] self.h = self.basicdata['h']; self.siglay = self.basicdata['siglay'] self.b_points = self.basicdata['b_points'] return self.b_points,fmodtime,emodtime #,nv lons,lats,lonc,latc,,h,siglay def get_track(self,lon,lat,depth,track_way): #,b_index,nvdepth, ''' Get forecast points start at lon,lat ''' modpts = dict(lon=[lon], lat=[lat]) #model forecast points, layer=[] if lon>90: lon, lat = dm2dd(lon, lat) # produce layer nodeindex = np.argwhere((self.lons >= lon-0.1) & (self.lons <= lon+0.1) & (self.lats >= lat-0.1) & (self.lats <= lat+0.1)) if len(nodeindex) == 0: print 'No model data around. Out of Model area.' return modpts waterdepth = self.h[[nodeindex]]; #print waterdepth if np.mean(waterdepth)<(abs(depth)): print 'This point is too shallow.Less than %d meter.'%abs(depth) return modpts #raise Exception() depth_total = self.siglay[:,nodeindex[0]]*np.mean(waterdepth); #print depth_total layer = np.argmin(abs(depth_total+depth)); #print 'layer',layer u = self.u[self.ind1:self.ind2,layer,:] ; v = self.v[self.ind1:self.ind2,layer,:] t = len(self.datatime) #print t for i in xrange(t): elementindex = np.argwhere((self.lonc >= lon-0.1) & (self.lonc <= lon+0.1) & (self.latc >= lat-0.1) & (self.latc <= lat+0.1)) if len(elementindex) == 0: print 'No model data around. Out of Model area.' return modpts ################## boundary 1 #################### pa = self.eline_path(lon,lat) if track_way=='backward' : # backwards case #u_t1 = np.mean(self.u[self.ind2-i,layer,elementindex])*(-1); v_t1 = np.mean(self.v[self.ind2-i,layer,elementindex])*(-1) u_t1 = np.mean(u[t-i-1,elementindex])*(-1); v_t1 = np.mean(v[t-i-1,elementindex])*(-1) else: #u_t1 = np.mean(self.u[self.ind1+i,layer][elementindex]); v_t1 = np.mean(self.v[self.ind1+i,layer][elementindex]) u_t1 = np.mean(u[i,elementindex]); v_t1 = np.mean(v[i,elementindex]) dx = 60*60*u_t1; dy = 60*60*v_t1 #mapx = Basemap(projection='ortho',lat_0=lat,lon_0=lon,resolution='l') #x,y = mapx(lon,lat) #temlon,temlat = mapx(x+dx,y+dy,inverse=True) temlon = lon + (dx/(111111*np.cos(lat*np.pi/180))) temlat = lat + dy/111111 #''' #print '%d,lat,lon,layer'%(i+1),temlat,temlon,layer #########case for boundary 1 ############# if pa: teml = [(lon,lat),(temlon,temlat)] tempa = Path(teml) if pa.intersects_path(tempa): print 'One point hits land here. Path Condition' return modpts #''' lon = temlon; lat = temlat modpts['lon'].append(lon); modpts['lat'].append(lat) return modpts def eline_path(self,lon,lat): ''' When drifter close to boundary(less than 0.1),find one nearest point to drifter from boundary points, then find two nearest boundary points to previous boundary point, create a boundary path using that three boundary points. ''' def boundary_location(locindex,pointt,wl): ''' Return the index of boundary points nearest to 'locindex'. ''' loca = [] dx = pointt[locindex]; #print 'func',dx for i in dx: # i is a number. #print i if i ==0 : continue dx1 = pointt[i-1]; #print dx1 if 0 in dx1: loca.append(i-1) else: for j in dx1: if j != locindex+1: if wl[j-1] == 1: loca.append(j-1) return loca p = Path.circle((lon,lat),radius=0.02) #0.06 dis = []; bps = []; pa = [] tlons = []; tlats = []; loca = [] for i in self.b_points: if p.contains_point(i): bps.append((i[0],i[1])) d = math.sqrt((lon-i[0])**2+(lat-i[1])**2) dis.append(d) bps = np.array(bps) if not dis: return None else: #print "Close to boundary." dnp = np.array(dis) dmin = np.argmin(dnp) lonp = bps[dmin][0]; latp = bps[dmin][1] index1 = np.where(self.lonc==lonp) index2 = np.where(self.latc==latp) elementindex = np.intersect1d(index1,index2)[0] # location 753''' #print index1,index2,elementindex loc1 = boundary_location(elementindex,self.pointt,self.wl) ; #print 'loc1',loc1 loca.extend(loc1) loca.insert(1,elementindex) for i in range(len(loc1)): loc2 = boundary_location(loc1[i],self.pointt,self.wl); #print 'loc2',loc2 if len(loc2)==1: continue for j in loc2: if j != elementindex: if i ==0: loca.insert(0,j) else: loca.append(j) for i in loca: tlons.append(self.lonc[i]); tlats.append(self.latc[i]) for i in xrange(len(tlons)): pa.append((tlons[i],tlats[i])) path = Path(pa)#,codes return path
print("computing adjacency...", file=sys.stderr, flush=True) adjacency = pd.DataFrame(False, index=regions, columns=regions) regions_set = set(regions) land_regions = set() covered = set() num_neighbors = args.num_neighbors for i in range(num_lats): for j in range(num_lngs): neighbors = [(i + lat_iterator, j + lng_iterator) for lat_iterator in list(range(-num_neighbors, num_neighbors + 1)) for lng_iterator in list(range(-num_neighbors, num_neighbors + 1))] for (n1, n2) in neighbors: if (n1, n2) in regions_set and \ (n1, n2) != (i, j) and \ n1 >= 0 and n2 >= 0 and \ m.is_land(country_lngs_m[n1, n2], country_lats_m[n1, n2]): adjacency.ix[(i, j), (n1, n2)] = True adjacency.ix[(n1, n2), (i, j)] = True land_regions.add((n1, n2)) land_regions = list(land_regions) print("done", file=sys.stderr, flush=True) print('%s land regions (out of %s)' % (len(land_regions), len(regions)), file=sys.stderr, flush=True) else: regions = ['F', 'M'] region_name2int = dict([(name, i) for (i, name) in enumerate(regions)]) land_region_name2int = dict([(name, i) for (i, name) in enumerate(land_regions)]) review_frequency = Counter()
def create_pdfs_from_ds(ds, outfile, only_land=True, proximity=True, lat_bounds=(-60, 75)): """ Creates new file contaning the probabiliy densitiy functions for each month of how often a specific amount of radation can occur. This is done for every grid-cell from the incoming dataset. Parameters ---------- ds: xarray dataset with 'time', 'lat', 'lon' dimensions and data-variable 'SWGDN' outfile: string path and filename where the resulting file should be stored. Must end with .nc4 only_land: bool If true: only gridcells whose center is on land will be computed. False: all cells are computed proximity: bool If true: A cell is also computed if one of the surrounding cells is land. This makes shure that all coastal regions are included, as sometimes the middle of a gridcell can be on the ocean, but still a great part is on land. Without this option, all these cases would not be included. lat_bounds: Tuple containing boundaries for the latitude to be included in the resulting datset. All latitudes between the two values of the tuple will be inlcuded. """ # Create list of all (lat, lon) pairs to be processed: tlat = ds["lat"].values tlon = ds["lon"].values lat_dist = np.unique(np.diff(tlat)) assert len(lat_dist) == 1 lat_dist = int(lat_dist[0]) lon_dist = np.unique(np.diff(tlon)) assert len(lon_dist) == 1 lon_dist = int(lon_dist[0]) time = pd.to_datetime(ds["time"].values) ds["time"] = time.map(lambda x: x.month) ds = ds.rename({"time": "month"}) coord_list = [] if only_land: bm = Basemap() for lat in tlat: if lat >= lat_bounds[0] and lat <= lat_bounds[1]: for lon in tlon: if bm.is_land(lon, lat): coord_list.append((lat, lon)) elif proximity: for prox_lat in [ lat + x * lat_dist for x in range(-1, 2) ]: for prox_lon in [ lon + x * lon_dist for x in range(-1, 2) ]: if bm.is_land(prox_lon, prox_lat): if not (lat, lon) in coord_list: coord_list.append((lat, lon)) else: for lat in tlat: if lat >= lat_bounds[0] and lat <= lat_bounds[1]: for lon in tlon: coord_list.append((lat, lon)) # Processing all the data of the coordinate tuples in coord_list num_cores = multiprocessing.cpu_count() manager = multiprocessing.Manager() assert len(tlat) * len(tlon) >= len(coord_list) shr_mem = manager.list([None] * len(tlat) * len(tlon)) prog_mem = manager.list() with warnings.catch_warnings(): warnings.simplefilter("ignore") Parallel(n_jobs=num_cores)(delayed(calc_pdfs)( ds.sel(lon=coords[1], lat=coords[0]), i, shr_mem, prog_mem, coords, len(coord_list), ) for i, coords in enumerate(coord_list)) print("\nfinished parallel part, stitching together") ds_out = xr.Dataset() xartype = type(ds) for piece in shr_mem: if type(piece) == xartype: ds_out = xr.merge([ds_out, piece]) ds_out = ds_out.sel(lat=slice(lat_bounds[0], lat_bounds[1])) # encoding_params = {'dtype': 'int16', 'scale_factor': 0.00005, '_FillValue': -9999, 'zlib': True, 'complevel': 2} encoding_params = { "dtype": "float32", "_FillValue": -9999, "zlib": True, "complevel": 4, } encoding = { "pk": { "dtype": "float32", "_FillValue": -9999, "zlib": True, "complevel": 5 }, "xk": { "dtype": "int16", "scale_factor": 0.02, "_FillValue": -9999, "zlib": True, "complevel": 5, }, } # encoding = {k: encoding_params for k in list(ds_out.data_vars)} ds_out.to_netcdf(path=outfile, format="NETCDF4", encoding=encoding) print("File is saved")
area_lat_inds = [] area_lon_inds = [] print area area_grid = area_boundaries[area] area_label = area_labels[area] lat_i = 0 lon_i = 0 for i in range(size): #check centre of gridbox within area limits if (lat_c[lat_i] >= area_grid[0]) & (lat_c[lat_i] < area_grid[1]) & (lon_c[lon_i] >= area_grid[2]) & (lon_c[lon_i] < area_grid[3]): #check centre of gridbox is over land if m.is_land(lon_c[lon_i],lat_c[lat_i]) == True: area_lat_inds.append(lat_i) area_lon_inds.append(lon_i) if lon_i == (len(lon_c)-1): lat_i+=1 lon_i=0 else: lon_i+=1 area_lat_inds = np.array(area_lat_inds) area_lon_inds = np.array(area_lon_inds) print seasonal_waveform_2000.shape if plot_type == 'd': model2000_w = np.nanmean(diurnal_ave_waveform_2000[:,area_lat_inds,area_lon_inds],axis=1)
def boxes_1(lat, lon, alt, npart, limit=100): import numpy as np import matplotlib matplotlib.use('agg') ## box order (Upwelling, Sahel, Sahara, West Africa, North Africa, Europe, ## North America, South America, North Atlantic, South Atlantic ) from mpl_toolkits.basemap import Basemap m = Basemap(projection='cyl', llcrnrlat=0,urcrnrlat=80,llcrnrlon=-90, urcrnrlon=38.,\ resolution='c',area_thresh=1000.) coast = m.drawcoastlines(linewidth=.5) boxes = np.zeros(10) over = np.zeros(1) for t in range(len(lon)): for p in range(npart): if alt[t, p] <= limit: checker = False ## Whole domain of interest if lon[t, p] >= -89. and lon[t, p] <= 36. and lat[ t, p] >= 2. and lat[t, p] <= 79.: ## Maritanian Upwelling if lon[t, p] >= -25. and lon[t, p] <= 5. and lat[ t, p] >= 20. and lat[t, p] <= 26.: if distance_from_coast(lon[t, p], lat[t, p], coast, m) == True: if m.is_land(lon[t, p], lat[t, p]) == False: #upwelling[j,t,p]=1 boxes[0] += 1 checker = True ## Sahel Region if lon[t, p] >= -20. and lon[t, p] <= 40. and lat[ t, p] >= 14. and lat[t, p] < 18.: if m.is_land(lon[t, p], lat[t, p]) == True: #sahel[j,t,p]=1 boxes[1] += 1 checker = True elif lon[t, p] >= -14. and lon[t, p] <= 40. and lat[ t, p] >= 14. and lat[t, p] < 18.: #sahel[j,t,p]=1 boxes[1] += 1 checker = True ## Sahara Region if lon[t, p] >= -20. and lon[t, p] < 40. and lat[ t, p] >= 18. and lat[t, p] <= 34.: if m.is_land(lon[t, p], lat[t, p]) == True: if lon[t, p] >= -20. and lon[t, p] < -13. and lat[ t, p] >= 27. and lat[t, p] <= 29.: checker = False else: #sahara[j,t,p]=1 boxes[2] += 1 checker = True ## West African Populated if lon[t, p] >= -20. and lon[t, p] <= 12. and lat[ t, p] >= 4. and lat[t, p] <= 14.: if m.is_land(lon[t, p], lat[t, p]) == True: #west_africa_populated[j,t,p]=1 boxes[3] += 1 checker = True else: checker = False if lon[t, p] >= -5. and lat[t, p] >= 7.0: #west_africa_populated[j,t,p]=1 boxes[3] += 1 checker = True ## BB region if lon[t, p] > 12. and lon[t, p] <= 36. and lat[ t, p] >= 4. and lat[t, p] <= 14.: #burning_region[j,t,p]=1 boxes[4] += 1 checker = True ## Europe if lon[t, p] >= -12. and lon[t, p] <= 45. and lat[ t, p] >= 34. and lat[t, p] <= 73.: if m.is_land(lon[t, p], lat[t, p]) == True or distance_from_coast( lon[t, p], lat[t, p], coast, m, distance=400) == True: if lon[t, p] >= -12. and lon[t, p] <= -9. and lat[ t, p] >= 62. and lat[t, p] <= 75.: checker = False else: #europe[j,t,p]=1 boxes[5] += 1 checker = True elif lon[t, p] >= 2. and lon[t, p] <= 7. and lat[ t, p] >= 55. and lat[t, p] <= 60.: #europe[j,t,p]=1 boxes[5] += 1 checker = True elif lon[t, p] >= 9. and lat[t, p] > 30 and lat[t, p] <= 36.: if m.is_land(lon[t, p], lat[t, p]) == False: #europe[j,t,p]=1 boxes[5] += 1 checker = True elif lon[t, p] >= -9. and lat[t, p] >= 34. and lat[ t, p] <= 36.: #europe[j,t,p]=1 boxes[5] += 1 checker = True ## North America if lon[t, p] >= -90. and lon[t, p] <= -50. and lat[ t, p] >= 15. and lat[t, p] <= 80.: if m.is_land(lon[t, p], lat[t, p]) == True: #north_am[j,t,p]=1 boxes[6] += 1 checker = True elif distance_from_coast(lon[t, p], lat[t, p], coast, m) == True: #north_am[j,t,p]=1 boxes[6] += 1 checker = True elif lon[t, p] <= -50. and lat[t, p] >= 45.: #north_am[j,t,p]=1 boxes[6] += 1 checker = True elif lon[t, p] <= -80. and lat[t, p] >= 20.: #north_am[j,t,p]=1 boxes[6] += 1 checker = True # South America if lon[t, p] >= -90. and lon[t, p] <= -58 and lat[ t, p] >= 0. and lat[t, p] <= 15.: #south_am[j,t,p]=1 boxes[7] += 1 checker = True elif lon[t, p] >= -90. and lon[t, p] <= -50 and lat[ t, p] >= 0. and lat[t, p] <= 15.: if distance_from_coast(lon[t, p], lat[t, p], coast, m) == True: #south_am[j,t,p]=1 boxes[7] += 1 checker = True ## Fill oceans last so there is no overlap if checker == False: if lat[t, p] >= 16.51 and lon[t, p] <= 32: #atlantic_north[j,t,p]=1 boxes[8] += 1 checker = True elif lat[t, p] <= 16.51 and m.is_land( lon[t, p], lat[t, p]) == False and lon[t, p] <= 12: #atlantic_south[j,t,p]=1 boxes[9] += 1 checker = True else: over += 1 SUM = np.nansum(boxes) percent = boxes / SUM * 100 return boxes, percent, over