Example #1
0
def _magnetic_warp(x, y, decimal_year=2016.0):
    """Apply magnetic warp magic."""
    gc_lat, gc_lon, gc_rad = convert([y, x, 0], GEODETIC_ABOVE_WGS84, GEOCENTRIC_SPHERICAL)
    qd_lat, qd_lon = eval_qdlatlon(gc_lat, gc_lon, gc_rad, decimal_year)
    return qd_lon, qd_lat
Example #2
0
def main(args):
    """Parse arguments and run graticule creation."""
    parser = argparse.ArgumentParser()
    parser.add_argument("output", type=str, help="output vector data")
    parser.add_argument("stepsize", type=int, help="degree step size")
    parser.add_argument(
        "--size_x", type=int, help="model raster x size", default=8192)
    parser.add_argument(
        "--size_y", type=int, help="model raster y size", default=4096)
    parser.add_argument(
        "--driver", type=str, help="output driver", default="ESRI Shapefile")
    parsed = parser.parse_args(args)

    output_file = parsed.output
    stepsize = parsed.stepsize
    size_x = parsed.size_x
    size_y = parsed.size_y

    fieldname = "dd"
    elevation = 0
    bounds = (-180., -90., 180., 90.)
    left, bottom, right, top = bounds
    pixel_x_size = (right - left) / float(size_x)
    pixel_y_size = (top - bottom) / float(size_y)
    affine = Affine.translation(left, top) * Affine.scale(
        pixel_x_size, -pixel_y_size)

    if os.path.isfile(output_file):
        os.remove(output_file)

    lons, lats = np.meshgrid(
        np.linspace(left, right, size_x, endpoint=True),
        np.linspace(top, bottom, size_y, endpoint=True)
    )

    # Geodetic coordinates with elevation above the WGS84 ellipsoid.
    coord_gdt = np.empty((size_y, size_x, 3))
    coord_gdt[:, :, 0] = lats
    coord_gdt[:, :, 1] = lons
    coord_gdt[:, :, 2] = elevation
    decimal_year = np.empty((size_y, size_x))
    decimal_year.fill(2016.1)
    coord_gct = convert(coord_gdt, GEODETIC_ABOVE_WGS84, GEOCENTRIC_SPHERICAL)
    qd_lat, qd_lon = eval_qdlatlon(
        coord_gct[..., 0].ravel(), coord_gct[..., 1].ravel(),
        coord_gct[..., 2].ravel(), decimal_year.ravel())

    # Latitudes
    lat = np.reshape(qd_lat, (size_y, size_x))
    lat_lines = extract_contours(lat, bounds, stepsize, fieldname, 0)

    # Longitudes
    lon = np.reshape(qd_lon, (size_y, size_x))
    lon_lines = extract_contours(
        np.absolute(lon), bounds, stepsize, fieldname, 0)
    meridians = extract_contours(lon, bounds, stepsize, fieldname, 0)
    # for row in range(len(lon)):
    #     diff = lon[row][:-1]-lon[row][1:]
    #     step_idxes = np.argwhere(diff > 180.)
    #     if step_idxes:
    #         lon[row][np.argwhere(diff > 180.)[0]+1:] += 360.
    # lon_lines = extract_contours(lon, bounds, stepsize, fieldname)

    out_schema = dict(
        geometry="LineString",
        properties=dict(
            degrees="int", direction="str", display="str", dd="float",
            scalerank="int"))
    with fiona.open(
        output_file, "w", schema=out_schema, crs={'init': 'epsg:4326'},
        driver=parsed.driver
    ) as dst:
        for feature in lat_lines:
            latitude = feature["properties"]["dd"]
            lat_int = int(round(latitude))
            direction = "N" if lat_int > 0 else "S"
            display_lat = lat_int if lat_int > 0 else -lat_int
            display = "%s %s" % (display_lat, direction)
            if lat_int == 0:
                direction = None
                display = "0"
            feature["properties"].update(
                degrees=lat_int, direction=direction, display=display,
                scalerank=None)
            dst.write(feature)
        for feature in _extract_longitudes(lon_lines, lon, affine):
            dst.write(feature)
            longitude = feature["properties"]["dd"]
            lon_int = int(round(longitude))
            direction = "W" if lon_int > 0 else "E"
            display_lon = lon_int if lon_int > 0 else -lon_int
            display = "%s %s" % (display_lon, direction)
            if lon_int == 0:
                direction = None
                display = "0"
            feature["properties"].update(
                degrees=lon_int, direction=direction, display=display,
                scalerank=None)
            # if longitude > 180.:
            #     feature["properties"].update(dd=longitude-360.)
            # dst.write(feature)

        for feature in meridians:
            longitude = feature["properties"]["dd"]
            if longitude in [0, 180]:
                feature["properties"].update(
                    degrees=0, direction=None, display="0", scalerank=None)
                dst.write(feature)