def test_Ease2Transform_init(): Ngrid = Ease2Transform("EASE2_N25km") assert_is(Ngrid.gridname, "EASE2_N25km") N31grid = Ease2Transform("EASE2_N3.125km") assert_is(N31grid.gridname, "EASE2_N3.125km") Sgrid = Ease2Transform("EASE2_S25km") assert_is(Sgrid.gridname, "EASE2_S25km") Tgrid = Ease2Transform("EASE2_T25km") assert_is(Tgrid.gridname, "EASE2_T25km")
def test_high_res_Ease2Transform_grid_to_geographic(): Ngrid = Ease2Transform("EASE2_N3.125km") lat, lon = Ngrid.grid_to_geographic(-0.5, -0.5) assert_almost_equals(lat, -84.634050, places=precision6) assert_almost_equals(lon, -135., places=precision6) Sgrid = Ease2Transform("EASE2_S6.25km") lat, lon = Sgrid.grid_to_geographic(-0.5, -0.5) assert_almost_equals(lat, 84.634050, places=precision6) assert_almost_equals(lon, -45., places=precision6) Tgrid = Ease2Transform("EASE2_T12.5km") lat, lon = Tgrid.grid_to_geographic(-0.5, -0.5) assert_almost_equals(lat, 67.057541, places=precision6) assert_almost_equals(lon, -180., places=precision6)
def test_UL_Ease2Transform_grid_to_map(): Ngrid = Ease2Transform("EASE2_N25km") x, y = Ngrid.grid_to_map(-0.5, -0.5) assert_almost_equals(x, -9000000., places=precision6) assert_almost_equals(y, 9000000., places=precision6) Sgrid = Ease2Transform("EASE2_S25km") x, y = Sgrid.grid_to_map(-0.5, -0.5) assert_almost_equals(x, -9000000., places=precision6) assert_almost_equals(y, 9000000., places=precision6) Tgrid = Ease2Transform("EASE2_T25km") x, y = Tgrid.grid_to_map(-0.5, -0.5) assert_almost_equals(x, -17367530.44, places=precision6) assert_almost_equals(y, 6756820.2, places=precision6)
def test_origin_Ease2Transform_grid_to_map(): Ngrid = Ease2Transform("EASE2_N25km") x, y = Ngrid.grid_to_map(359.5, 359.5) assert_almost_equals(x, 0., places=precision6) assert_almost_equals(y, 0., places=precision6) Sgrid = Ease2Transform("EASE2_S25km") x, y = Sgrid.grid_to_map(359.5, 359.5) assert_almost_equals(x, 0., places=precision6) assert_almost_equals(y, 0., places=precision6) Tgrid = Ease2Transform("EASE2_T25km") x, y = Tgrid.grid_to_map(269.5, 693.5) assert_almost_equals(x, 0., places=precision6) assert_almost_equals(y, 0., places=precision6)
def test_UL_Ease2Transform_geographic_to_grid(): Ngrid = Ease2Transform("EASE2_N25km") row, col = Ngrid.geographic_to_grid(-84.634050, -135.) assert_almost_equals(row, -0.5, places=precision6) assert_almost_equals(col, -0.5, places=precision6) Sgrid = Ease2Transform("EASE2_S25km") row, col = Sgrid.geographic_to_grid(84.634050, -45.) assert_almost_equals(row, -0.5, places=precision6) assert_almost_equals(col, -0.5, places=precision6) Tgrid = Ease2Transform("EASE2_T25km") row, col = Tgrid.geographic_to_grid(67.057541, -180.) assert_almost_equals(row, -0.5, places=precision5) assert_almost_equals(col, -0.5, places=precision5)
def test_origin_Ease2Transform_geographic_to_grid(): Ngrid = Ease2Transform("EASE2_N25km") row, col = Ngrid.geographic_to_grid(90., 0.) assert_almost_equals(row, 359.5, places=precision6) assert_almost_equals(col, 359.5, places=precision6) Sgrid = Ease2Transform("EASE2_S25km") row, col = Sgrid.geographic_to_grid(-90., 0.) assert_almost_equals(row, 359.5, places=precision4) assert_almost_equals(col, 359.5, places=precision4) Tgrid = Ease2Transform("EASE2_T25km") row, col = Tgrid.geographic_to_grid(0., 0.) assert_almost_equals(row, 269.5, places=precision6) assert_almost_equals(col, 693.5, places=precision6)
def test_UL_Ease2Transform_map_to_grid(): Ngrid = Ease2Transform("EASE2_N25km") row, col = Ngrid.map_to_grid(-9000000., 9000000.) assert_almost_equals(row, -0.5, places=precision6) assert_almost_equals(col, -0.5, places=precision6) Sgrid = Ease2Transform("EASE2_S25km") row, col = Sgrid.map_to_grid(-9000000., 9000000.) assert_almost_equals(row, -0.5, places=precision6) assert_almost_equals(col, -0.5, places=precision6) Tgrid = Ease2Transform("EASE2_T25km") row, col = Tgrid.map_to_grid(-17367530.44, 6756820.2) assert_almost_equals(row, -0.5, places=precision6) assert_almost_equals(col, -0.5, places=precision6)
def grid_locations_of_subset(subsetName, lat, lon, cubeDir=None): gpds = ["EASE2_N3.125km", "EASE2_N6.25km", "EASE2_N25km"] types = ["3?V-SIR", "1?V-SIR", "1?V-GRD"] #possibly change to H or V for flexibility print("Input lat, lon = %.6f, %.6f" % (lat, lon)) rows = np.zeros((3)) cols = np.zeros((3)) for i, (thisGpd, thisType) in enumerate(zip(gpds, types)): grid = Ease2Transform(thisGpd) row, col = grid.geographic_to_grid(lat, lon) print("%15s : row, col = %.6f, %.6f" % (thisGpd, row, col)) # Get the cube offsets offset_row, offset_col = find_cube_offset(subsetName, cubeDir=cubeDir, cubeType=thisType) subRow = row - offset_row subCol = col - offset_col print("%15s(%s): row, col = %.6f, %.6f" % (subsetName, thisType, subRow, subCol)) rows[i] = subRow cols[i] = subCol rows = (rows + 0.5).astype('int32') cols = (cols + 0.5).astype('int32') #FIXME: MJB: validate that this routine is returning the correct coordinates #FIXME: This routine should really return a dict structure, not an array print(gpds) print("%d %d %d %d %d %d" % (cols[0], rows[0], cols[1], rows[1], cols[2], rows[2])) return (rows, cols)
def test_origin_Ease2Transform_grid_to_geographic(): Ngrid = Ease2Transform("EASE2_N25km") lat, lon = Ngrid.grid_to_geographic(359.5, 359.5) assert_almost_equals(lat, 90., places=precision6) assert_almost_equals(lon, 0., places=precision6) lat, lon = Ngrid.grid_to_geographic(-0.5, -0.5) assert_almost_equals(lat, -84.634050, places=precision6) assert_almost_equals(lon, -135., places=precision6) Sgrid = Ease2Transform("EASE2_S25km") lat, lon = Sgrid.grid_to_geographic(359.5, 359.5) assert_almost_equals(lat, -90., places=precision6) assert_almost_equals(lon, 0., places=precision6) lat, lon = Sgrid.grid_to_geographic(-0.5, -0.5) assert_almost_equals(lat, 84.634050, places=precision6) assert_almost_equals(lon, -45., places=precision6) Tgrid = Ease2Transform("EASE2_T25km") lat, lon = Tgrid.grid_to_geographic(269.5, 693.5) assert_almost_equals(lat, 0., places=precision6) assert_almost_equals(lon, 0., places=precision6) lat, lon = Tgrid.grid_to_geographic(-0.5, -0.5) assert_almost_equals(lat, 67.057541, places=precision6) assert_almost_equals(lon, -180., places=precision6)
def find_cube_offset(subsetName, cubeDir=None, cubeType=None, verbose=False): # when no cubeDir is specified, assume it's in a regular # location on fringe if not cubeDir: cubeDir = "%s%s" % ("/mnt/data3/cetb/cubes/AQUA_AMSRE/", subsetName) if not cubeType: cubeType = '3*V-SIR' cubePattern = "%s/CETB.cubefile.%s.*-%s-*-v*.*.TB.nc" % ( cubeDir, subsetName, cubeType) # Just use the last one found # Check for no files found and return a reasonable error message list = glob.glob(cubePattern) if not list: raise IOError("No cubefiles found to match the pattern = %s" % (cubePattern)) else: cubeFile = list[-1] print("Reading offset information from cubeFile=%s..." % (cubeFile)) f = Dataset(cubeFile, "r", "NETCDF4") lats = f.variables['latitude'][:] lons = f.variables['longitude'][:] baseGPD = f.variables['crs'].long_name f.close() # find and return the baseGPD (row, col) offset for cubeUL(0, 0) location grid = Ease2Transform(baseGPD) row_offset, col_offset = grid.geographic_to_grid(lats[0, 0], lons[0, 0]) if verbose: print("%10s offsets for subsetName=%s and cubeType=%s" % (baseGPD, subsetName, cubeType)) print( "(Add these offsets to cube (row, col) to get row, col in full hemisphere)" ) print("offset row = %f, offset col = %f" % (row_offset, col_offset)) return row_offset, col_offset
# In[2]: dir(Ease2Transform) # In[3]: help(Ease2Transform) # ### You can instantiate an Ease2Transform object for an EASE-Grid 2.0 projection like this: # In[4]: N25 = Ease2Transform("EASE2_N25km") print N25.gridname # ### And then call any of the transformation functions like this: # ### Find the grid coordinates of the North Pole: # In[5]: lat, lon = 90., 0. row, col = N25.geographic_to_grid(lat, lon) print ("North pole is at:") print (" geog coordinates lat=%.3f, lon=%.3f" % (lat, lon)) print (" grid coordinates row=%.3f, col=%.3f" % (row, col))