def find_polygon_of_points(latitude, longitude, polygons): ''' Point in polygon test It uses the topological plate boundary network to assign a polygon. So it needs pyGPlates. ''' for ind, i in enumerate(latitude): inputlatLon = pygplates.LatLonPoint(latitude[ind], longitude[ind]) latLonPoint = pygplates.convert_lat_lon_point_to_point_on_sphere( inputlatLon) #print latLonPoint isPoly = [] #get geometry of static polygons at reconstructed time for coastline in reconstructed_coastlines: #print coastline coastline = coastline.get_reconstructed_geometry() #if int(coastline.is_point_in_polygon(latLonPoint)) == 1: #break#print tmp tmp = int(coastline.is_point_in_polygon(latLonPoint)) isPoly.append(tmp) if tmp == 1: latitude[ind] = None longitude[ind] = None zvals[ind] = None latitude = filter(None, latitude) longitude = filter(None, longitude) zvals = filter(None, zvals) return latitude, longitude, zvals
def parse_points(input_lat_lon_points_filename): """Parse ascii file containing a lat/lon point per line.""" points = [] with open(input_lat_lon_points_filename, 'r') as lat_lon_points_file: for line_number, line in enumerate(lat_lon_points_file): lat_lon = line.split() if len(lat_lon) != 2: print( 'Line {0}: Ignoring point - line does not have exactly two white-space separated strings.' .format(line_number), file=sys.stderr) continue try: lat = float(lat_lon[0]) lon = float(lat_lon[1]) except ValueError: print('Line {0}: Ignoring point - cannot read lat/lon values.'. format(line_number), file=sys.stderr) continue lat_lon_point = pygplates.LatLonPoint(lat, lon) point = pygplates.convert_lat_lon_point_to_point_on_sphere( lat_lon_point) points.append(point) return points
def latlon2pygplates(lat,lon): ''' Convert lat lon to the pygplates formats ''' pointLatLon = pygplates.LatLonPoint(lat,lon) pointXYZ = pygplates.convert_lat_lon_point_to_point_on_sphere(pointLatLon) pointXYZcart = numpy.array([pointXYZ.get_x(), pointXYZ.get_y(), pointXYZ.get_z()]) return(pointXYZ,pointXYZcart)
def get_plate_ID_of_points(plate_partitioner, latitude, longitude): latLonPoints = [] for i, j in zip(latitude, longitude): inputlatLon = pygplates.LatLonPoint(i, j) latLonPoints.append( pygplates.convert_lat_lon_point_to_point_on_sphere(inputlatLon)) plateIDs = [] for point in latLonPoints: tmp_partition = plate_partitioner.partition_point(point) if tmp_partition == None: plateIDs.append(tmp_partition) else: plateIDs.append( plate_partitioner.partition_point( point).get_feature().get_reconstruction_plate_id()) return plateIDs
def seds_filter_points_in_polygon(latitude, longitude, zvalue, reconstructed_coastlines): ''' This function returns the plateID of latitude/longitude points at a given time from a rotation file It uses the topological plate boundary network to assign a plate ID. So it needs pyGPlates. ''' #because of how the data is formatted we need two for loops to check each point #because we are loading in a 2d array of longitude and latitude *each* filtered_lats = [] filtered_lons = [] filtered_zvals = [] for x, y, z in itertools.izip(latitude, longitude, zvalue): inputlatLon = pygplates.LatLonPoint(x, y) latLonPoint = pygplates.convert_lat_lon_point_to_point_on_sphere( inputlatLon) #print latLonPoint isPoly = [] #get geometry of static polygons at reconstructed time for coastline in reconstructed_coastlines: #print coastline coastline = pygplates.PolygonOnSphere( coastline.get_reconstructed_geometry()) #if int(coastline.is_point_in_polygon(latLonPoint)) == 1: #break#print tmp tmp = int(coastline.is_point_in_polygon(latLonPoint)) isPoly.append(tmp) if np.sum(isPoly) == 0: filtered_lats.append(x) filtered_lons.append(y) filtered_zvals.append(z) return filtered_lats, filtered_lons, filtered_zvals
def find_polygon_of_points(latitude, longitude, polygons): ''' Point in polygon test It uses the topological plate boundary network to assign a polygon. So it needs pyGPlates. taken from simon williams github ''' polygons_of_points = [] for ind, i in enumerate(latitude): #print ind,i inputlatLon = pygplates.LatLonPoint(latitude[ind], longitude[ind]) latLonPoint = pygplates.convert_lat_lon_point_to_point_on_sphere( inputlatLon) for polygon in polygons: polygon = polygon.get_resolved_geometry() tmp = int(polygon.is_point_in_polygon(latLonPoint)) if tmp == 1: polygons_of_points.append(polygon) return polygons_of_points
def recalculate_lat_lons_for_time(latitude, longitude, plateIDs, reconstruct_to_time, reconstruct_from_time, rotation_model, age_grid_mask): ''' This function recalculates the latitude and longitude of a point given a rotation file and a to/from time ''' recon_lats = [] recon_lons = [] shape = np.shape(latitude) #get shape of desired array for i, j, k in itertools.izip(latitude.data, longitude.data, plateIDs): for l, m, n in itertools.izip(i, j, k): #print l,m,n inputlatLon = pygplates.LatLonPoint(l, m) latLonPoint = pygplates.convert_lat_lon_point_to_point_on_sphere( inputlatLon) #to time, plate id, from time #make_raster_at, plate id, CCD_intersection_time point_rotation = rotation_model.get_rotation( int(reconstruct_to_time), n, reconstruct_from_time) reconstructed_point = point_rotation * latLonPoint recon_lats.append(reconstructed_point.to_lat_lon()[0]) recon_lons.append(reconstructed_point.to_lat_lon()[1]) #print len(recon_lats) #print len(plateIDs), 'plateIDs' #print len(recon_lats) #create arrays of recon lats/lons in the same shape as our input data with the appropriate age grid mask masked_recon_lats = np.ma.masked_array( np.asarray(recon_lats).reshape(shape), age_grid_mask.mask) masked_recon_lons = np.ma.masked_array( np.asarray(recon_lons).reshape(shape), age_grid_mask.mask) return masked_recon_lats, masked_recon_lons
def filter_points_in_polygon(latitude, longitude, zvals, reconstructed_coastlines): ''' This function filters out points that are overlapping continental crust (once overlapping we assume they are subducted). It uses the topological plate boundary network to assign a plate ID. So it needs pyGPlates. ''' for ind, i in enumerate(latitude): if not np.isnan(i): inputlatLon = pygplates.LatLonPoint(latitude[ind], longitude[ind]) latLonPoint = pygplates.convert_lat_lon_point_to_point_on_sphere( inputlatLon) #print latLonPoint isPoly = [] #get geometry of static polygons at reconstructed time for coastline in reconstructed_coastlines: #print coastline coastline = coastline.get_reconstructed_geometry() #if int(coastline.is_point_in_polygon(latLonPoint)) == 1: #break#print tmp tmp = int(coastline.is_point_in_polygon(latLonPoint)) isPoly.append(tmp) if tmp == 1: latitude[ind] = None longitude[ind] = None zvals[ind] = None latitude = filter(None, latitude) longitude = filter(None, longitude) zvals = filter(None, zvals) return latitude, longitude, zvals
def coregLoop(pointlist, ages, plateIDs): ''' coregLoop This script reconstructs a shapefile of points to their birth time and coregisters each point with another set of points, or a raster file. INPUTS: pointlist - list of lat/lon ages - list of ages corresponding to lat/lon point plateIDs - list of plateIDs corresponding to lat/lon point. Hardcoded filenames/variables must be changed below: input_rotation_filename - Rotation file rasterfile - Time dependent raster files f - Time dependent kinemtic csv outputs from 'convergence.py' OUTPUTS: Coregistered array: List of lat/lons with properties. METHOD: Takes a set of points Rotates the points back to their birth position Determines the point's birth position geophysical properties (coregisters) ''' #Set up a list to store the data timeSteps = 1 # +- Myrs around point to store. Default 1 which means just the measured point age. noVariables = 18 #The number of variables you save Nshp = len(pointlist) coregData = numpy.zeros((Nshp, timeSteps, noVariables)) #Create a rotation model to rotate the points back in time input_rotation_filename = "Muller_gplates/Global_EarthByte_230-0Ma_GK07_AREPS.rot" file_registry = pygplates.FeatureCollectionFileFormatRegistry() rotation_feature_collection = file_registry.read(input_rotation_filename) rotation_model = pygplates.RotationModel([rotation_feature_collection]) #Loop over all the samples, coregistering each one. for i, currentPoint in enumerate(pointlist): lat = currentPoint[1] lon = currentPoint[0] age = ages[i] plateID = int(plateIDs[i]) print("Deposit:", i, "of", Nshp, "Lat:", lat, "Lon:", lon, "Age:", age, "PlateID:", plateID) #Loop through each time step in the plate model for time in xrange(0, 230, 1): #If the point was formed at the current time (or [timeStepsMyr ] prior/after) then we #want to find the surrounding plate properties. #if (time > (age-10)) and (time < (age+10)) or (time > (age+20)) and (time < (age+30)): if (time > (age - timeSteps)) and (time < (age + timeSteps)): t = int(numpy.floor(time - age)) #print(t,time,age) #A raster file, to coregister with, these points have already been rotated rasterfile = "Muller_etal_2016_AREPS_Agegrids_v1.11/netCDF_0-230Ma/EarthByte_AREPS_v1.11_Muller_etal_2016_AgeGrid-" + str( time) + ".nc" [x, y, z] = gridRead(rasterfile) #A vector file to coregister with, these points have already been rotated f = numpy.loadtxt("Muller_convergence/subStats_" + str(time) + ".csv", delimiter=',') lonlat = f[:, 0:2] #-------------------# #Reconstruct a point to its birth position #-------------------# latlonPoint = pygplates.LatLonPoint(lat, lon) point_to_rotate = pygplates.convert_lat_lon_point_to_point_on_sphere( latlonPoint) finite_rotation = rotation_model.get_rotation(time, plateID) birthPosition = finite_rotation * point_to_rotate latlonBirth = pygplates.convert_point_on_sphere_to_lat_lon_point( birthPosition) #allPoints = finite_rotation * pointSet_to_rotate latBirth = latlonBirth.get_latitude() lonBirth = latlonBirth.get_longitude() #-------------------# #Set the points for coregistering region = 5.0 #degrees #-------------------# #Coregisterring raster 1 #-------------------# #Find the region in index units r = numpy.round(region / (x[1] - x[0])) #Find the index unit of lat and lon idxLon = (numpy.abs(x - lonBirth)).argmin() idxLat = (numpy.abs(y - latBirth)).argmin() #Raster 1 c2 = coregRaster([idxLon, idxLat], z, r) #Hack to search further around the age grid if it can't find a match, note index units (not degrees) if numpy.isnan(c2): c2 = coregRaster([idxLon, idxLat], z, r + 150.0) print("Trying raster region: ", r + 150.0) #-------------------# #Coregisterring vector 1 #-------------------# index = coregPoint([lonBirth, latBirth], lonlat, region) if index == 'inf': print("trying index region", region + 15) index = coregPoint([lonBirth, latBirth], lonlat, region + 15.0) if numpy.isnan(c2) or index == 'inf': print("Skipping:", i, age, t, time, c2, index, lonBirth, latBirth) else: #Vector 1 segmentLength = f[index, 3] slabLength = f[index, 9] distSlabEdge = f[index, 15] SPcoregNor = f[index, 4] SPcoregPar = f[index, 12] OPcoregNor = f[index, 5] OPcoregPar = f[index, 13] CONVcoregNor = f[index, 10] CONVcoregPar = f[index, 11] subPolCoreg = f[index, 8] subOblCoreg = f[index, 7] coregData[i, t, :] = [ lon, lat, lonBirth, latBirth, age, t, c2, segmentLength, slabLength, distSlabEdge, SPcoregNor, SPcoregPar, OPcoregNor, OPcoregPar, CONVcoregNor, CONVcoregPar, subPolCoreg, subOblCoreg ] #Return the filled coregistered array return (coregData)
def coregLoopHistory(pointlist, ts0=0, ts1=230, plateID=201): ''' coregLoopHistory This script reconstructs a list of points throughout history and coregisters each point with another set of points, or a raster file. INPUTS: pointlist - list of lat/lon ts0 - time step to rotate points from (probably 0Ma) ts1 - time step to rotate points to (probably 230Ma). Hardcoded filenames/variables must be changed below: input_rotation_filename - Rotation file rasterfile - Time dependent raster files f - Time dependent kinemtic csv outputs from 'convergence.py' OUTPUTS: Coregistered array: List of lat/lons with properties. METHOD: Takes a set of points Rotates the points back to their birth position Determines the point's birth position geophysical properties (coregisters) ''' #Set up an array to store the data timeSteps = ts1 - ts0 #The length of time before mineralisation you care about noVariables = 18 #The number of variables you save Nshp = len(pointlist) coregData = numpy.zeros((Nshp, timeSteps, noVariables)) #Create a rotation model to rotate the points back in time input_rotation_filename = "Muller_gplates/Global_EarthByte_230-0Ma_GK07_AREPS.rot" file_registry = pygplates.FeatureCollectionFileFormatRegistry() rotation_feature_collection = file_registry.read(input_rotation_filename) rotation_model = pygplates.RotationModel([rotation_feature_collection]) #Loop through each time step in the plate model for time in xrange(ts0, ts1, 1): #A raster file, to coregister with, these points have already been rotated rasterfile = "Muller_etal_2016_AREPS_Agegrids_v1.11/netCDF_0-230Ma/EarthByte_AREPS_v1.11_Muller_etal_2016_AgeGrid-" + str( time) + ".nc" [x, y, z] = gridRead(rasterfile) #A vector file to coregister with, these points have already been rotated f = numpy.loadtxt("Muller_convergence/subStats_" + str(time) + ".csv", delimiter=',') lonlat = f[:, 0:2] for i, currentPoint in enumerate(pointlist): shapeArray = currentPoint age = ts0 t = time - age print(i, age, time, t, plateID) #-------------------# #Reconstruct a point to its birth position #-------------------# latlonPoint = pygplates.LatLonPoint(shapeArray[1], shapeArray[0]) point_to_rotate = pygplates.convert_lat_lon_point_to_point_on_sphere( latlonPoint) finite_rotation = rotation_model.get_rotation(time, plateID) birthPosition = finite_rotation * point_to_rotate latlonBirth = pygplates.convert_point_on_sphere_to_lat_lon_point( birthPosition) #allPoints = finite_rotation * pointSet_to_rotate latBirth = latlonBirth.get_latitude() lonBirth = latlonBirth.get_longitude() #-------------------# #Set the points for coregistering region = 5.0 #degrees #-------------------# #Coregisterring raster 1 #-------------------# #Find the region in index units r = numpy.round(region / (x[1] - x[0])) #Find the index unit of lat and lon idxLon = (numpy.abs(x - lonBirth)).argmin() idxLat = (numpy.abs(y - latBirth)).argmin() #Raster 1 c2 = coregRaster([idxLon, idxLat], z, r) #Hack to search further around the age grid if it can't find a match, \ #note index units (not degrees) if numpy.isnan(c2): c2 = coregRaster([idxLon, idxLat], z, r + 150.0) print("Trying raster region: ", r + 150.0) #-------------------# #Coregisterring vector 1 #-------------------# index = coregPoint([lonBirth, latBirth], lonlat, region) if index == 'inf': print("trying index region", region + 15) index = coregPoint([lonBirth, latBirth], lonlat, region + 15.0) if numpy.isnan(c2) or index == 'inf': #if we have some null data, let's save it anyway, see what happens print("Skipping:", i, age, t, time, c2, index, lonBirth, latBirth) else: #Vector 1 segmentLength = f[index, 3] slabLength = f[index, 9] distSlabEdge = f[index, 15] SPcoregNor = f[index, 4] SPcoregPar = f[index, 12] OPcoregNor = f[index, 5] OPcoregPar = f[index, 13] CONVcoregNor = f[index, 10] CONVcoregPar = f[index, 11] subPolCoreg = f[index, 8] subOblCoreg = f[index, 7] coregData[i, t, :] = [ shapeArray[0], shapeArray[1], lonBirth, latBirth, age, t, c2, segmentLength, slabLength, distSlabEdge, SPcoregNor, SPcoregPar, OPcoregNor, OPcoregPar, CONVcoregNor, CONVcoregPar, subPolCoreg, subOblCoreg ] return (coregData)
def subLoop(recsSub,shapesSub,fieldsSub,NshpSub,recsTopo,shapesTopo,fieldsTopo,NshpTopo,isright): ''' Determines the overriding and subducting plate ID along a trench. Takes the subduction zone and plate polygon shapefile data as input. Returns a list of points of the form [Lon, Lat, SPid, TRENCHid, OPid] And puts some header information indicated by '>' for each subduction segment. ''' #Initiialise the output array: [lon, lat, SPid, TRENCHid, OPid] testingArray=[] FinalArray=[] #Go through subduction zones 1 by 1 #print "Subdction zone number in file, and PlateID:" #NshpSub=[8,9] for nshpSub in xrange(NshpSub): #Get the Lat Lon points along the trench shapeArray=shapesSub[nshpSub].points #Reverse the order of LH arrays so convergence is +ve reversedSub=0 if not isright: #print "Reversing, LH array" shapeArray=shapeArray[::-1] reversedSub=1 #Get the Trench ID and Time # Note this section looks both for a PLATE_ID and a PLATEID1 field for the trench # index, due to changes between different GPlates versions. If both are present for # some reason, the TRENCHid would be taken from the second one to be encountered for i in range(len(fieldsSub)): if 'PLATE_ID' in fieldsSub[i]: TRENCHidIndex = i-1 elif 'PLATEID1' in fieldsSub[i]: TRENCHidIndex = i-1 elif 'TIME' in fieldsSub[i]: TimeIndex = i-1 elif 'FEATURE_ID' in fieldsSub[i]: GplatesTagIndex = i-1 elif 'NAME' in fieldsSub[i]: NameIndex = i-1 #Get the Trench ID and Time TRENCHid=recsSub[nshpSub][TRENCHidIndex] #3 new, 0 old (GPlates 1.2) Time=recsSub[nshpSub][TimeIndex] #2 old GplatesTag=recsSub[nshpSub][GplatesTagIndex] name=recsSub[nshpSub][NameIndex] #print nshpSub,TRENCHid,GplatesTag,name #Store the Lat and Lon in seperate arrays, remove end points because #they are prone to errors and confusing triple junctions! shapeArrayLon = [round(i[0],4) for i in shapeArray[:]] shapeArrayLat = [round(i[1],4) for i in shapeArray[:]] shapeArray=zip(shapeArrayLon,shapeArrayLat) #Append some header information #NPB!!! This is currently not used for much FinalArray.append(['> Name:' , name]) FinalArray.append(['> GplatesTag:' , GplatesTag]) FinalArray.append(['> PlateId:' , TRENCHid]) FinalArray.append(['> Age:' , Time]) FinalArray.append(['> Begin:' , 0]) FinalArray.append(['> End:' , 0]) FinalArray.append(['> Type:' , 0]) FinalArray.append(['> Polygon:' , 0]) FinalArray.append(['> Reversed:' , reversedSub]) FinalArray.append(['> BoundaryPoints:' , len(shapeArray)]) #Now we must loop through every point along the subduction zone and #check the polygons that it is attached to. for index, point in enumerate(shapeArray): OPcount=0 SPcount=0 for nshpTopo in xrange(NshpTopo): #Get the Lat Lon points of the plate polygon topoArray=shapesTopo[nshpTopo].points #Store the Lat and Lon in seperate arrays topoArrayLon = [round(i[0],4) for i in topoArray] topoArrayLat = [round(i[1],4) for i in topoArray] topoArray=zip(topoArrayLon,topoArrayLat) #topoArray=numpy.around(shapesTopo[nshpTopo].points,decimals=5) #Test whether the subduction point is in the current #polygon. If it is, we can find out more information... try: i = topoArray.index(point) pointFlag = 1 except: pointFlag = 0 ######################################### #Now if the point exists on the polygon # #Let's find out whether it is OP or SP # ######################################### if pointFlag == 1: #Get the PLATEid of the polygon (same comment as above - looks for field called either PLATE_ID or PLATEID1) for i in range(len(fieldsTopo)): if 'PLATE_ID' in fieldsTopo[i]: PlateIndex = i-1 elif 'PLATEID1' in fieldsTopo[i]: PlateIndex = i-1 PLATEid=recsTopo[nshpTopo][PlateIndex] #3 new, 0 old (GPlates 1.2) #Now get the entire polygon as a pygplates entitiy latlonPoints=[] for latlon in topoArray: pointLatLon = pygplates.LatLonPoint(latlon[1],latlon[0]) latlonPoints.append(pygplates.convert_lat_lon_point_to_point_on_sphere(pointLatLon)) polygon = pygplates.PolygonOnSphere(latlonPoints) #Convert our trench segment to pygplates format if index < len(shapeArray)-1: latPoint1 = shapeArray[index][1] lonPoint1 = shapeArray[index][0] pointXYZ, pointXYZcart = latlon2pygplates(latPoint1,lonPoint1) latPoint2 = shapeArray[index+1][1] lonPoint2 = shapeArray[index+1][0] pointXYZ2, pointXYZcart2 = latlon2pygplates(latPoint2,lonPoint2) #If we are at the last trench segment, just jump back a point else: latPoint1 = shapeArray[index-1][1] lonPoint1 = shapeArray[index-1][0] pointXYZ, pointXYZcart = latlon2pygplates(latPoint1,lonPoint1) latPoint2 = shapeArray[index][1] lonPoint2 = shapeArray[index][0] pointXYZ2, pointXYZcart2 = latlon2pygplates(latPoint2,lonPoint2) #Find the vector between 2 points on the trench line = pygplates.PolylineOnSphere([pointXYZ,pointXYZ2]) #Find the midpoint between those two points midPoint = line.get_centroid() midLatLon = pygplates.convert_point_on_sphere_to_lat_lon_point(midPoint) midPointLatLon = numpy.array([midLatLon.get_longitude(), midLatLon.get_latitude()]) #Find the bearing of point 1 to point 2 (ALL IN RADIANS) #eqns from http://www.movable-type.co.uk/scripts/latlong.html radLat1 = numpy.radians(latPoint1) radLat2 = numpy.radians(latPoint2) radLon1 = numpy.radians(lonPoint1) radLon2 = numpy.radians(lonPoint2) #θ = atan2( sin(Δλ).cos(φ2), cos(φ1).sin(φ2) − sin(φ1).cos(φ2).cos(Δλ) ) bearingRad = numpy.arctan2( numpy.sin(radLon2-radLon1) * numpy.cos(radLat2),\ numpy.cos(radLat1)*numpy.sin(radLat2) - numpy.sin(radLat1)*numpy.cos(radLat2)*numpy.cos(radLon2-radLon1)) #Now we march inside the polygon, to check if it is OP or SP #Turn 90 degrees to the left or right #if isright: bearingPerp = bearingRad + 1.57 #else: # bearingPerp = bearingRad - 1.57 #March into the plate a few km along the bearing. #new Lat Lon are given below d = 20.0 #km #Arbitrary small distance... R=6371.0 #km Radius of Earth km #φ2 = asin( sin(φ1)*cos(d/R) + cos(φ1)*sin(d/R)*cos(θ) ) lat2 = numpy.arcsin(numpy.sin(radLat1)*numpy.cos(d/R) + numpy.cos(radLat1)*numpy.sin(d/R)*numpy.cos(bearingPerp)) #λ2 = λ1 + atan2( sin(θ)*sin(d/R)*cos(φ1), cos(d/R)−sin(φ1)*sin(φ2) ) lon2 = radLon1 + numpy.arctan2(numpy.sin(bearingPerp)*numpy.sin(d/R)*numpy.cos(radLat1),numpy.cos(d/R)-numpy.sin(radLat1)*numpy.sin(radLat2)) pointSurface = pygplates.LatLonPoint(numpy.degrees(lat2),numpy.degrees(lon2)) pointSurfaceXYZ = pygplates.convert_lat_lon_point_to_point_on_sphere(pointSurface) #Test whether it is in the polygon or not cwcheck = polygon.is_point_in_polygon(pointSurfaceXYZ) #If it is then we have marched into the cojugate plate (probably)! if cwcheck: OPid=PLATEid OPcount+=1 else: SPid=PLATEid SPcount+=1 #If the point does not exist on the polygon, pass else: pass #After looping through the polygons, we should have one OPid, #and one SPid, but these may be wrong for triple junctions. if (OPcount == 1) and (SPcount == 1): #Now exit the Polygon loop and save the information about the point and move on #print point[0],point[1], SPid, TRENCHid, OPid FinalArray.append([point[0],point[1], SPid, TRENCHid, OPid]) else: pass #print OPcount, SPcount #print "Warning, point: ", point, TRENCHid, " found no match in polygons." #print "'BoundaryPoints' will not match actual points!" #print FinalArray return(FinalArray)
def coregLoopTimeFirst(recs, shapes, fields, Nshp, shapeType): ''' coregLoop This script reconstructs a shapefile of points to their birth time and coregisters each point with another set of points, or a raster file. INPUTS: Can best be used with the output of readTopologyPlatepolygonFile() which reads a shapefile and returns the 'records', 'shapes', 'fields', and 'number of shapes'. Hardcoded filenames/variables must be changed below: Rotation file - input_rotation_filename Time dependent raster files - rasterfile Time dependent vector csv outputs from 'convergence.py' - f OUTPUTS: Coregistered array: List of lat/lons with properties. METHOD: Takes a set of points (from a shapefile) Rotates the points back to their birth position Determines the point's birth position geophysical properties (coregisters) ''' #Set up a list to store the data if shapeType == 0: timeSteps = 230 #The length of time before mineralisation you care about else: timeSteps = 1 noVariables = 18 #The number of variables you save input_rotation_filename = "Muller_gplates/Global_EarthByte_230-0Ma_GK07_AREPS.rot" # input_rotation_filename = "/Users/nbutter/Geodata/PlateModel/Shephard_etal_ESR2013_Global_EarthByte_2013.rot" #Create a rotation model to rotate the points back in time file_registry = pygplates.FeatureCollectionFileFormatRegistry() rotation_feature_collection = file_registry.read(input_rotation_filename) rotation_model = pygplates.RotationModel([rotation_feature_collection]) #Initialise the array to store the data coregData = numpy.zeros((Nshp, timeSteps, noVariables)) #Loop through each time step in the plate model for time in xrange(0, 230, 1): #A raster file, to coregister with, these points have already been rotated rasterfile = "Muller_etal_2016_AREPS_Agegrids_v1.11/netCDF_0-230Ma/EarthByte_AREPS_v1.11_Muller_etal_2016_AgeGrid-" + str( time) + ".nc" # rasterfile="/Users/nbutter/Geodata/AgeGrid_20111110_Seton_etal_ESR/agegrid_final_mask_"+str(time)+".grd" [x, y, z] = gridRead(rasterfile) #A vector file to coregister with, these points have already been rotated f = readCSV("Muller_convergence/subStats_" + str(time) + ".csv") # f = readCSV("/Users/nbutter/Geodata/CONVERGENCE/Shep07/subStats_"+str(time)+".csv") lonlat = f[:, 0:2] for i, nshpSub in enumerate(xrange(Nshp)): #!!! Warning: These are shapefile specific attributes, #if you use a different shapefile, you must change these hard codes #to the corresponding age and plateID column in the shapefile if shapeType == 0: shapeArray = numpy.array(shapes[nshpSub]) age = 0 plateID = 201 else: shapeArray = numpy.array(shapes[nshpSub].points) #Here age is in the last column, and plateID in 7th. #1, 2 For porcu/main_edited.shp use 43 for the plateID #1, 2 For porcu/main_edited.shp use 9 for the age and 42 for the random age #3, 4 For XYBer14_t2_ANDES.shp use 7 for plateID #3, 4 For XYBer14_t2_ANDES.shp use 6 for age and -1 for Random if shapeType == 1: age = round(recs[nshpSub][9]) plateID = recs[nshpSub][43] elif shapeType == 2: age = round(recs[nshpSub][42]) plateID = recs[nshpSub][43] elif shapeType == 3: age = round(recs[nshpSub][6]) plateID = recs[nshpSub][7] elif shapeType == 4: age = round(recs[nshpSub][-1]) plateID = recs[nshpSub][7] t = time - age print i, age, time, t, plateID #-------------------# #Reconstruct a point to its birth position #-------------------# if shapeType == 0: latlonPoint = pygplates.LatLonPoint(shapeArray[1], shapeArray[0]) else: latlonPoint = pygplates.LatLonPoint(shapeArray[0][1], shapeArray[0][0]) point_to_rotate = pygplates.convert_lat_lon_point_to_point_on_sphere( latlonPoint) finite_rotation = rotation_model.get_rotation(time, plateID) birthPosition = finite_rotation * point_to_rotate latlonBirth = pygplates.convert_point_on_sphere_to_lat_lon_point( birthPosition) #allPoints = finite_rotation * pointSet_to_rotate latBirth = latlonBirth.get_latitude() lonBirth = latlonBirth.get_longitude() #-------------------# #Set the points for coregistering region = 5.0 #degrees #-------------------# #Coregisterring raster 1 #-------------------# #Find the region in index units r = numpy.round(region / (x[1] - x[0])) #Find the index unit of lat and lon idxLon = (numpy.abs(x - lonBirth)).argmin() idxLat = (numpy.abs(y - latBirth)).argmin() #Raster 1 c2 = coregRaster([idxLon, idxLat], z, r) #Hack to search further around the age grid if it can't find a match, note index units (not degrees) # if numpy.isnan(c2): # print "Trying raster region: ", r+100.0 # c2=coregRaster([idxLon,idxLat],z,r+100.0) if numpy.isnan(c2): c2 = coregRaster([idxLon, idxLat], z, r + 150.0) print "Trying raster region: ", r + 150.0 #-------------------# #Coregisterring vector 1 #-------------------# index = coregPoint([lonBirth, latBirth], lonlat, region) # if index=='inf': # print "trying index region", region+10 # index=coregPoint([lonBirth,latBirth],lonlat,region+10.0) if index == 'inf': print "trying index region", region + 15 index = coregPoint([lonBirth, latBirth], lonlat, region + 15.0) if numpy.isnan(c2) or index == 'inf': #if we have some null data, let's save it anyway, see what happens #allData.append([shapeArray[0][0],shapeArray[0][1],0,0,\ # age,age-time,0,0,0,0]) print "Skipping:", i, age, t, time, c2, index, lonBirth, latBirth else: #Vector 1 segmentLength = f[index, 3] slabLength = f[index, 9] distSlabEdge = f[index, 15] SPcoregNor = f[index, 4] SPcoregPar = f[index, 12] OPcoregNor = f[index, 5] OPcoregPar = f[index, 13] CONVcoregNor = f[index, 10] CONVcoregPar = f[index, 11] subPolCoreg = f[index, 8] subOblCoreg = f[index, 7] # 0 longitude, # 1 latitude, # 2 convRate(mm/yr), # 3 segmentLength(km), # 4 subPlateVel(mm/yr), # 5 opVelocity(mm/yr), # 6 trenchVel(mm/yr), # 7 subObliquity(degrees), # 8 subPolarity(degrees), # 9 slabDistance(km), # 10 OrthogonalConvergence(mm/yr), # 11 ParallelConvergence(mm/yr), # 12 ParallelSubPlateVel(mm/yr), # 13 ParallelOPvel(mm/yr), # 14 ParallelTrenchVel(mm/yr), # 15 DistanceToSlabEdge(km), # 16 SPid, # 17 TRENCHid, # 18 OPid if shapeType == 0: coregData[i,t,:]=[shapeArray[0],shapeArray[1],lonBirth,latBirth,\ age,t,c2,\ segmentLength,slabLength,distSlabEdge,\ SPcoregNor,SPcoregPar,OPcoregNor,OPcoregPar,CONVcoregNor,CONVcoregPar,\ subPolCoreg,subOblCoreg] else: coregData[i,t,:]=[shapeArray[0][0],shapeArray[0][1],lonBirth,latBirth,\ age,t,c2,\ segmentLength,slabLength,distSlabEdge,\ SPcoregNor,SPcoregPar,OPcoregNor,OPcoregPar,CONVcoregNor,CONVcoregPar,\ subPolCoreg,subOblCoreg] return (coregData)
def coregLoop(sampleData, agegrid, kinfolder, input_rotation_filename): ''' coregLoop This script reconstructs a shapefile of points to their birth time and coregisters each point with another set of points, or a raster file. METHOD: Takes a set of points Rotates the points back to their birth position Determines the point's birth position geophysical properties (coregisters) ''' #Set up a list to store the data timeSteps = 1 noVariables = 18 #The number of variables you save Nshp = len(sampleData) #Initialise the array to store the data coregData = numpy.zeros((Nshp, timeSteps, noVariables)) #Create a rotation model to rotate the points back in time file_registry = pygplates.FeatureCollectionFileFormatRegistry() rotation_feature_collection = file_registry.read(input_rotation_filename) rotation_model = pygplates.RotationModel([rotation_feature_collection]) #Loop over all the sample, coregistering each one. for i, nshpSub in enumerate(sampleData): lat = nshpSub[0] lon = nshpSub[1] age = int(nshpSub[2]) plateID = int(nshpSub[3]) print("Deposit:", i, "of", Nshp, "Lat:", lat, "Lon:", lon, "Age:", age, "PlateID:", plateID) #Loop through each time step in the plate model for time in xrange(0, 230, 1): #If the point was formed at the current time (or 10Myr prior) then we #want to find the surrounding plate properties. #if (time > (age-10)) and (time < (age+10)) or (time > (age+20)) and (time < (age+30)): if (time > (age - 1)) and (time < (age + timeSteps)): t = time - age # print t, time #A raster file, to coregister with, these points have already been rotated rasterfile = agegrid + str(time) + ".nc" [x, y, z] = gridRead(rasterfile) #A vector file to coregister with, these points have already been rotated f = numpy.loadtxt(kinfolder + "subStats_" + str(time) + ".csv", delimiter=',') lonlat = f[:, 0:2] #-------------------# #Reconstruct a point to its birth position #-------------------# latlonPoint = pygplates.LatLonPoint(lat, lon) point_to_rotate = pygplates.convert_lat_lon_point_to_point_on_sphere( latlonPoint) finite_rotation = rotation_model.get_rotation(time, plateID) birthPosition = finite_rotation * point_to_rotate latlonBirth = pygplates.convert_point_on_sphere_to_lat_lon_point( birthPosition) #allPoints = finite_rotation * pointSet_to_rotate latBirth = latlonBirth.get_latitude() lonBirth = latlonBirth.get_longitude() #-------------------# #Set the points for coregistering region = 5.0 #degrees #-------------------# #Coregisterring raster 1 #-------------------# #Find the region in index units r = numpy.round(region / (x[1] - x[0])) #Find the index unit of lat and lon idxLon = (numpy.abs(x - lonBirth)).argmin() idxLat = (numpy.abs(y - latBirth)).argmin() #Raster 1 c2 = coregRaster([idxLon, idxLat], z, r) #Hack to search further around the age grid if it can't find a match, note index units (not degrees) if numpy.isnan(c2): c2 = coregRaster([idxLon, idxLat], z, r + 150.0) print("Trying raster region: ", r + 150.0) #-------------------# #Coregisterring vector 1 #-------------------# index = coregPoint([lonBirth, latBirth], lonlat, region) if index == 'inf': print("trying index region", region + 15) index = coregPoint([lonBirth, latBirth], lonlat, region + 15.0) if numpy.isnan(c2) or index == 'inf': print("Skipping:", nshpSub, age, t, time, c2, index, lonBirth, latBirth) else: #Vector 1 segmentLength = f[index, 3] slabLength = f[index, 9] distSlabEdge = f[index, 15] SPcoregNor = f[index, 4] SPcoregPar = f[index, 12] OPcoregNor = f[index, 5] OPcoregPar = f[index, 13] CONVcoregNor = f[index, 10] CONVcoregPar = f[index, 11] subPolCoreg = f[index, 8] subOblCoreg = f[index, 7] coregData[i, t, :] = [ lon, lat, lonBirth, latBirth, age, t, c2, segmentLength, slabLength, distSlabEdge, SPcoregNor, SPcoregPar, OPcoregNor, OPcoregPar, CONVcoregNor, CONVcoregPar, subPolCoreg, subOblCoreg ] #Return the filled coregistered array return (coregData)