Example #1
0
def warp(num, fileList):
    files = fileList
    raster = path + "Middle\\Rasters\\" + str(files[num])
    warped = path + "Middle\\WarpedRasters\\" + str(files[num])
    arcpy.Warp_management(raster, source, target, warped, "POLYORDER1",
                          "BILINEAR")
    pass
Example #2
0
File: rmp.py Project: jasp382/glass
def warp_rst(rst, outrst, srcpnt, tgpnt, rst_format='.tif'):
    """
    Warp Raster

    srcpnt example:
    srcpnt = (
        "'16.2409649994254 48.0598321302268';'16.3212880027982 48.1005354388663';"
        "'16.2409649994254 48.1005354388663';'16.3212880027982 48.0598321302268'"
    )

    tgpnt = (
        "'16.240965 48.0633562';'16.3212877 48.0963069';"
        "'16.240965 48.0963069';'16.3212877 48.0633562'"
    )
    """

    rst_format = '.tif' if not rst_format else rst_format

    if os.path.isdir(rst):
        from glass.pys.oss import lst_ff

        rsts = lst_ff(rst, file_format=rst_format)

    else:
        from glass.pys import obj_to_lst

        rsts = obj_to_lst(rst)

    if os.path.isdir(outrst):
        outrsts = [
            os.path.join(outrst, 'warp_{}'.format(os.path.basename(r)))
            for r in rsts
        ]

    else:
        if type(outrst) != list:
            if len(rsts) > 1:
                outrsts = [
                    os.path.join(os.path.dirname(outrst),
                                 'warp_{}'.format(os.path.basename(r)))
                    for r in rsts
                ]

            else:
                outrsts = [outrst]

        else:
            outrsts = outrst

    for r in range(len(rsts)):
        arcpy.Warp_management(rsts[r], srcpnt, tgpnt, outrsts[r], "POLYORDER1",
                              "BILINEAR")

    return outrst
Example #3
0
def geoProcess(in_data1, destination_data):
    # basePath = .../apps/findbestroute/workfiles/
    print('pleaaaase   ' + basePath)
    arcpy.MinimumBoundingGeometry_management(in_features=destination_data,
                                             out_feature_class=os.path.join(
                                                 basePath, "Trash", "box"),
                                             geometry_type="ENVELOPE")
    # original box over
    box = os.path.join(basePath, "Trash", "box.shp")

    #    fields = arcpy.ListFields(boxobj)
    #    for field in fields:
    #        print(field.name)

    arcpy.AddGeometryAttributes_management(Input_Features=box,
                                           Geometry_Properties="EXTENT")

    #    # arcpy.AddGeometryAttributes_management(Input_Features=os.path.join(basePath, "Trash", "box.shp"), Geometry_Properties="EXTENT")
    #    fields2 = arcpy.ListFields(boxobj)
    #    for field2 in fields2:
    #        print(field2.name)

    #    for field in arcpy.ListFields(dataset=box):
    #        print(field.__str__())

    raster = arcpy.Raster(in_data1)

    inXMax = raster.extent.XMax
    inYMax = raster.extent.YMax
    inXMin = raster.extent.XMin
    inYMin = raster.extent.YMin

    XminValues = [row[0] for row in arcpy.da.SearchCursor(box, "EXT_MIN_X")]
    YMinValues = [row[0] for row in arcpy.da.SearchCursor(box, "EXT_MIN_Y")]
    XMaxValues = [row[0] for row in arcpy.da.SearchCursor(box, "EXT_MAX_X")]
    YMaxValues = [row[0] for row in arcpy.da.SearchCursor(box, "EXT_MAX_Y")]

    destXMin = min(XminValues)
    destYMin = min(YMinValues)
    destXMax = max(XMaxValues)
    destYMax = max(YMaxValues)

    sourceCP = "'" + str(inXMax) + " " + str(inYMax) + "';'" + str(
        inXMax) + " " + str(inYMin) + "';'" + str(inXMin) + " " + str(
            inYMax) + "';'" + str(inXMin) + " " + str(inYMin) + "'"
    targetCP = "'" + str(destXMax) + " " + str(destYMax) + "';'" + str(
        destXMax) + " " + str(destYMin) + "';'" + str(destXMin) + " " + str(
            destYMax) + "';'" + str(destXMin) + " " + str(destYMin) + "'"

    return arcpy.Warp_management(
        raster, sourceCP, targetCP,
        os.path.join(basePath, r"Results", r"geoKart.jpg"), "POLYORDER1")
Example #4
0
    def Apply(self, tempGDB, inputRaster, srcPoints, gcpPoints, transType,
              resType):
        arcpy.AddMessage('Start Georeferencing...')
        out_coor_system = arcpy.SpatialReference(4326)

        # georeference to WGS84
        gcsImage_wgs84 = arcpy.Warp_management(
            inputRaster, srcPoints, gcpPoints,
            os.path.join(tempGDB, 'image_gc'), transType, resType)

        # Define projection system for output image after warpping the raw image
        arcpy.DefineProjection_management(gcsImage_wgs84, out_coor_system)
        arcpy.AddMessage('--Georeferencing Done.')
        return gcsImage_wgs84
Example #5
0
def apply_georeferencing(inputRaster, srcPoints, gcpPoints, output_folder,
                         out_img_name, transformation_type, resampling_type):
    arcpy.AddMessage('Start Georeferencing...')
    sr_wgs_84 = arcpy.SpatialReference(4326)
    # georeference to WGS84
    output_geo_ref_image = os.path.join(output_folder, out_img_name + '.tif')

    arcpy.Warp_management(inputRaster, srcPoints, gcpPoints,
                          output_geo_ref_image, transformation_type,
                          resampling_type)
    # arcpy.Warp_management(inputRaster, srcPoints,gcpPoints,output_geo_ref_image)

    arcpy.DefineProjection_management(output_geo_ref_image, sr_wgs_84)
    set_raster_background(output_geo_ref_image)
    arcpy.AddMessage('Output Image(.tif): %s' % output_geo_ref_image)
    arcpy.AddMessage('--Georeferencing Done.')
    return output_geo_ref_image
Example #6
0
   def ConvertFITStoTIFF(self):
       for galaxy in self.galaxy_list:
           composite_filename = self.path_to_files + '\\' + "COMPOSITES" + "\\" + galaxy + "_" + self.instrument + "_" + str(self.numBands) + "Bands.tif"
           if not os.path.exists(composite_filename):    
               for num in range(1,self.numBands+1):
                   filename            = galaxy + "_v7.phot." + str(num)+ ".fits"
                   output_filename     = filename[:-4] + 'rev.tif'
                   geo_output_filename = filename[:-4] + 'rev.WCS.tif'
                   outputfile_and_path = self.output_tiff_path + output_filename
                   geo_file_and_path   = self.output_tiff_path + geo_output_filename
                   file_and_path       = self.input_fits_path + filename
                   print(outputfile_and_path)
                   print(geo_file_and_path)
                   hdulist = fits.open(file_and_path)
                   #print(hdulist.info())
                   w = wcs.WCS(hdulist[0].header)
                   image = hdulist[0].data
                   
                   # Some pixel coordinates of interest.
                   pixcrd = numpy.array([[0, 0], [0, image.shape[0]],[image.shape[1], 0], [image.shape[1], image.shape[0]]], numpy.float_)
                   pixcrd_string = '0 0;' + '0 ' + str(image.shape[0]) + ';' +  str(image.shape[1]) + ' 0;' + str(image.shape[1]) + ' ' + str(image.shape[0])

                   # Convert pixel coordinates to world coordinates
                   # The second argument is "origin" -- in this case we're declaring we
                   # have 1-based (Fortran-like) coordinates.
                   world = w.wcs_pix2world(pixcrd, 1)
                   coordinate_string = str(-1*world[0,0]) + ' ' + str(world[0,1]) + ';' + str(-1*world[1,0]) + ' ' + str(world[1,1]) + ';' + str(-1*world[2,0]) + ' ' + str(world[2,1]) + ';' + str(-1*world[3,0]) + ' ' + str(world[3,1]) 
                   # Convert the same coordinates back to pixel coordinates.
                   pixcrd2 = w.wcs_world2pix(world, 1)
                   
                   # These should be the same as the original pixel coordinates, modulo
                   # some floating-point error.
                   assert numpy.max(numpy.abs(pixcrd - pixcrd2)) < 1e-6
                   flipim= numpy.flipud(image)
                   double_image = numpy.float64(flipim)
                   myRaster = arcpy.NumPyArrayToRaster(double_image)
                   myRaster.save(outputfile_and_path)
                   source_pnt = pixcrd_string
                   target_pnt = coordinate_string
                   arcpy.Warp_management(outputfile_and_path, source_pnt, target_pnt, geo_file_and_path, "POLYORDER1","BILINEAR")
                   arcpy.Delete_management(outputfile_and_path)
           arcpy.env.workspace = output_path
           rasters = arcpy.ListRasters(galaxy + "*", "TIF")
           print(rasters)
           arcpy.CompositeBands_management(rasters,composite_filename)
       print("Done Converting FITS to TIFF.")
Example #7
0
def warp(input_image, output_gdb):
    """Georeference the input raster by a predetermined extent."""
    try:
        # Set output workspace
        arcpy.env.workspace = output_gdb

        # Output raster
        output_raster = os.path.basename(
            input_image.replace("-", "_").replace(" ",
                                                  "_").replace(".png", ""))

        # Predefined extents
        src_pnt = "'-0.5  0.5';'4330.5 0.5';'-0.5 -1796.5';'4330.5 -1796.5'"
        tar_pnt = "'-179.77501667257 71.7596743910802';'-65.5369154883902 71.7596743910802';'-179.77501667257 24.3665227454068';'-65.5369154883902 24.3665227454068'"

        # Run warp
        arcpy.Warp_management(input_image, src_pnt, tar_pnt, output_raster)

        # Define SRS
        srs = arcpy.SpatialReference("WGS 1984")
        arcpy.DefineProjection_management(output_raster, srs)
    except Exception as e:
        abort("Warp procedure failed!\n", e)
gcppath = "D:\\181-0404\\181 - 副本\\181-GCP-ALL"  #GCP 文件夹
stifpath = "D:\\181-0404\\181 - 副本\\181_2\\"  #待校正 tif 文件夹
mappath = "D:\\181-0404\\181 - 副本\\181_2\\tsxt\\"  #校正后 tif 文件夹

gcppath = unicode(gcppath, 'utf-8')
stifpath = unicode(stifpath, 'utf-8')
mappath = unicode(mappath, 'utf-8')

gcplist = os.listdir(gcppath)
filelist = os.listdir(stifpath)
for fs in filelist:
    fsextension = os.path.splitext(fs)[1]
    if fsextension == ".tif":
        fsname1 = os.path.splitext(fs)[0]
        fsname = fsname1[:26]
        for gcp in gcplist:
            gcpname1 = os.path.splitext(gcp)[1]
            gcpname = gcpname1[:26]
            if fsname == gcpname:
                gcptxt = gcpname1 + ".txt"
                stifname = fsname1 + ".tif"
                gcptifname = mappath + stifname
                arcpy.Warp_management(
                    stifname,
                    gcptifname,
                    gcptxt,
                    "POLYORDER1",
                    "",
                )
                print stifname + "success update"
Example #9
0
    pixcrd2 = w.wcs_world2pix(world, 1)

    print('Converting numpy array to TIFF.')

    #Converting raster to TIFF image
    assert numpy.max(numpy.abs(pixcrd - pixcrd2)) < 1e-6
    #Reversing the array
    flipim = numpy.flipud(image)
    #Writing the array to a raster
    double_image = numpy.float64(flipim)
    myRaster = arcpy.NumPyArrayToRaster(double_image)
    myRaster.save(temp_file_and_path)
    #Giving raster the sky coordinates
    source_pnt = pixcrd_string
    target_pnt = coordinate_string
    arcpy.Warp_management(temp_file_and_path, source_pnt, target_pnt,
                          output_file_and_path, "POLYORDER1", "BILINEAR")
    #Deleting the unreference image
    arcpy.Delete_management(temp_file_and_path)
    print(output_file_and_path)

#Composite bands
arcpy.env.workspace = output_image_path
rasters = arcpy.ListRasters(galaxy + "*", "TIF")
arcpy.CompositeBands_management(rasters, composite_path_and_name)

#Create the mosaic and add images to it
coordinate_sys = "PROJCS['WGS_1984_Web_Mercator_Auxiliary_Sphere',GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Mercator_Auxiliary_Sphere'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',0.0],PARAMETER['Standard_Parallel_1',0.0],PARAMETER['Auxiliary_Sphere_Type',0.0],UNIT['Meter',1.0]];-20037700 -30241100 10000;-100000 10000;-100000 10000;0.001;0.001;0.001;IsHighPrecision"
imagery_spatial_ref = "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision"
mosaic_gdb = r"C:\PROJECTS\R&D\ASTROARC\SINGS\Spitzer.gdb"
mosaic_name = "SINGS"
mosaic_dataset = os.path.join(mosaic_gdb, mosaic_name)
Example #10
0
def mosaic(dnight, sets, filter):
    '''
    This module creates the mosaic of median filtered images for each data set.
    '''
    #set arcpy environment variables part 2/2
    arcpy.CheckOutExtension("Spatial")
    arcpy.env.workspace = filepath.rasters + 'scratch_median/'
    arcpy.env.scratchWorkspace = filepath.rasters + 'scratch_median'

    #filter paths
    F = {'V': '', 'B': 'B/'}
    f = {'V': '', 'B': 'b'}

    for s in sets:
        #file paths
        calsetp = filepath.calibdata + dnight + '/S_0%s/%s' % (s[0], F[filter])
        gridsetp = filepath.griddata + dnight + '/S_0%s/%smedian/' % (
            s[0], F[filter])
        if os.path.exists(gridsetp):
            shutil.rmtree(gridsetp)
        os.makedirs(gridsetp)

        #read in the registered images coordinates
        file = filepath.calibdata + dnight + '/pointerr_%s.txt' % s[0]
        Obs_AZ, Obs_ALT = n.loadtxt(file, usecols=(3, 4)).T
        Obs_AZ[n.where(Obs_AZ > 180)] -= 360
        Obs_AZ[35] %= 360

        #read in the best-fit zeropoint and plate scale
        file = filepath.calibdata + dnight + '/extinction_fit_%s.txt' % filter
        zeropoint, platescale, exptime = n.loadtxt(file,
                                                   usecols=(2, 8, 9),
                                                   unpack=True,
                                                   ndmin=2)

        #loop through each file in the set
        for w in range(len(Obs_AZ) + 1):

            v = w + 1
            if w == 45:
                w = 35
                Obs_AZ[w] -= 360

            if v in range(0, 50, 5): print 'Generating median image %i/45' % v

            arcpy.CopyRaster_management(
                calsetp + '/tiff/median_ib%03d.tif' % (w + 1),
                'ib%03d.tif' % v, "DEFAULTS", "", "", "", "",
                "16_BIT_UNSIGNED")

            #re-define projection to topocentric coordinates
            arcpy.DefineProjection_management("ib%03d.tif" % v,
                                              tc(Obs_AZ[w], Obs_ALT[w]))

            #warp image to remove barrel distortion image
            arcpy.Warp_management('ib%03d.tif' % v, source_pnt, target_pnt,
                                  'ibw%03d.tif' % v, "POLYORDER3", "BILINEAR")

            #reproject into GCS
            arcpy.ProjectRaster_management('ibw%03d.tif' % v,
                                           'wib%03d.tif' % v, geogcs,
                                           "BILINEAR", "0.0266")

            #clip to image boundary
            rectangle = clip_envelope(Obs_AZ, Obs_ALT, w)
            arcpy.Clip_management("wib%03d.tif" % v, rectangle, "cib%03d" % v)

        #mosaic raster list must start with an image with max pixel value > 256
        v = 1
        mstart = 1
        while v < (len(Obs_AZ) + 1):
            im = imread(filepath.rasters + 'scratch_median/ib%03d.tif' % v)
            if n.max(im) > 255:
                mstart = v
                break
            v += 1

        #mosaic raster list
        R1 = ';'.join(['cib%03d' % i for i in range(mstart, 47)])
        R2 = ';'.join(['cib%03d' % i for i in range(1, mstart)])
        R = R1 + ';' + R2

        #mosaic to topocentric coordinate image; save in Griddata\
        print "Mosaicking into all sky median image"
        arcpy.MosaicToNewRaster_management(R, gridsetp, 'skytopom', geogcs,
                                           "32_BIT_FLOAT", "0.0266", "1",
                                           "BLEND", "FIRST")

        #re-sampling to 0.05 degree resolution
        gridname = gridsetp + "skybrightmags"
        arcpy.Resample_management(gridsetp + 'skytopom',
                                  gridsetp + 'skybright', '0.05', 'BILINEAR')

        #convert to magnitudes per square arc second
        print "Converting the mosaic to mag per squard arcsec"
        psa = 2.5 * n.log10(
            (platescale[int(s[0]) - 1] * 60)**2)  # platescale adjustment
        skytopomags = zeropoint[int(s[0]) - 1] + psa - 2.5 * arcpy.sa.Log10(
            arcpy.sa.Raster(gridsetp + 'skybright') / exptime[0])

        #save mags mosaic to disk
        skytopomags.save(gridsetp + 'skybrightmags')

        print "Creating layer files for median mosaic"
        layerfile = filepath.griddata + dnight + '/skybrightmags%s%s.lyr' % (
            f[filter], s[0])
        arcpy.MakeRasterLayer_management(
            gridsetp + 'skybrightmags',
            dnight + '_%s_median%s' % (s[0], f[filter]))
        arcpy.SaveToLayerFile_management(
            dnight + '_%s_median%s' % (s[0], f[filter]), layerfile, "ABSOLUTE")

        #Set layer symbology to magnitudes layer
        symbologyLayer = filepath.rasters + 'magnitudes.lyr'
        arcpy.ApplySymbologyFromLayer_management(layerfile, symbologyLayer)
        lyrFile = arcpy.mapping.Layer(layerfile)
        lyrFile.replaceDataSource(gridsetp, 'RASTER_WORKSPACE',
                                  'skybrightmags', 'FALSE')
        lyrFile.save()

        #Downscale the raster and save it as a fits file
        file = filepath.griddata + dnight + '/S_0%s/%smedian/skybrightmags' % (
            s[0], F[filter])
        arcpy_raster = arcpy.sa.Raster(file)
        A = arcpy.RasterToNumPyArray(arcpy_raster, "#", "#", "#", -9999)
        A_small = downscale_local_mean(A[:1800, :7200], (25, 25))  #72x288
        fname = filepath.griddata + dnight + '/skybrightmags%s%s.fits' % (
            f[filter], s[0])
        fits.writeto(fname, A_small, overwrite=True)

    #create mask.tif for horizon masking in the later process
    mask = filepath.griddata + dnight + '/mask.tif'
    if not os.path.isfile(mask):
        arcpy.CopyRaster_management(gridsetp + 'skybright', mask, "DEFAULTS",
                                    "0", "0", "", "", "16_BIT_UNSIGNED")
            TargetX = nextRecord.getValue(longitude_field)
            TargetY = nextRecord.getValue(latitude_field)
            GCPX = nextRecord.getValue("Point_X")
            GCPY = nextRecord.getValue("Point_Y")
            target_points = target_points + ";'" + str(TargetX) + " " + str(
                TargetY) + "'"
            source_points = source_points + ";'" + str(GCPX) + " " + str(
                GCPY) + "'"

        arcpy.AddMessage(target_points)
        arcpy.AddMessage(source_points)

        # Use the ordered lists of points as input to the warp method for georeferencing
        savedLocation = arcpy.GetParameterAsText(7)
        # arcpy.MakeRasterLayer_management("Composite", "Composite_lyr")
        arcpy.Warp_management(InputGridRed, source_points, target_points,
                              "Red")
        arcpy.Warp_management(InputGridGreen, source_points, target_points,
                              "Green")
        arcpy.Warp_management(InputGridBlue, source_points, target_points,
                              "Blue")

        # Composite georeferenced bands
        arcpy.CompositeBands_management(["Red", "Green", "Blue"],
                                        savedLocation)

    except Exception as e:
        # If unsuccessful, end gracefully by indicating why
        arcpy.AddError('\n' + "Script failed because: \t\t" + e.message)
        # ... and where
        exceptionreport = sys.exc_info()[2]
        fullermessage = traceback.format_tb(exceptionreport)[0]
Example #12
0
    def convert_to_tiff(self):
        arcpy.env.pyramid = "NONE"
        arcpy.env.rasterStatistics = "NONE"

        if not os.path.exists(self.output_TIFF_path):
            os.makedirs(self.output_TIFF_path)
            #Get list of files that will be converted
        fits_files = [
            f for f in listdir(self.write_to_folder)
            if isfile(join(self.write_to_folder, f))
        ]

        #For every file...
        for fits_file in enumerate(fits_files):
            print("Processing file: " + str(fits_file[0] + 1) + ", " +
                  fits_file[1] + "...")
            filename = fits_file[1]
            output_filename = filename[:-4] + 'rev.tif'
            geo_output_filename = filename[:-4] + 'rev.' + self.projection + '.tif'
            outputfile_and_path = join(self.output_TIFF_path, output_filename)
            geo_file_and_path = join(self.output_TIFF_path,
                                     geo_output_filename)
            file_and_path = join(self.write_to_folder, filename)
            if not os.path.exists(geo_file_and_path):
                hdulist = fits.open(file_and_path)
                w = wcs.WCS(hdulist[0].header)
                image = hdulist[0].data
                # Some pixel coordinates of interest.
                pixcrd = numpy.array(
                    [[0, 0], [0, image.shape[0]], [image.shape[1], 0],
                     [image.shape[1], image.shape[0]]], numpy.float_)
                pixcrd_string = '0 0;' + '0 ' + str(
                    image.shape[0]) + ';' + str(image.shape[1]) + ' 0;' + str(
                        image.shape[1]) + ' ' + str(image.shape[0])
                #print(pixcrd_string)

                # Convert pixel coordinates to world coordinates
                # The second argument is "origin" -- in this case we're declaring we
                # have 1-based (Fortran-like) coordinates.
                world = w.wcs_pix2world(pixcrd, 1)
                #print(world)
                #GLIMPSE is Backwards! The data is originially in GALACTIC so
                #There is no need to change the projection
                longitude = []
                latitude = []
                if (self.projection == 'GALACTIC'):
                    coord_string = str(-1 * world[0, 0]) + ' ' + str(
                        world[0, 1]) + ';' + str(-1 * world[1, 0]) + ' ' + str(
                            world[1, 1]) + ';' + str(
                                -1 * world[2, 0]) + ' ' + str(
                                    world[2, 1]) + ';' + str(
                                        -1 * world[3, 0]) + ' ' + str(world[3,
                                                                            1])
                    #coord_string = str(world[0,0]) + ' ' + str(world[0,1]) + ';' + str(world[1,0]) + ' ' + str(world[1,1]) + ';' + str(world[2,0]) + ' ' + str(world[2,1]) + ';' + str(world[3,0]) + ' ' + str(world[3,1])
                    print(coord_string)
                    arg_space = coord_string.replace(';', ' ')
                    args = arg_space.split(' ')
                    #Fixing the Coordinates
                    longitude.append(self.normalize_x(float(args[0])))
                    longitude.append(self.normalize_x(float(args[2])))
                    longitude.append(self.normalize_x(float(args[4])))
                    longitude.append(self.normalize_x(float(args[6])))
                    latitude.append(float(args[1]))
                    latitude.append(float(args[3]))
                    latitude.append(float(args[5]))
                    latitude.append(float(args[7]))
                if (self.projection == 'WCS'):
                    # Convert the same coordinates back to pixel coordinates.
                    #pixcrd2 = w.wcs_world2pix(world, 1)
                    coordinate = SkyCoord(world, frame='icrs', unit='deg')
                    coord = coordinate.galactic
                    print(coord.to_string('decimal'))
                    coord_string = coord.to_string('decimal')
                    longitude.append(
                        self.normalize_x(float(coord_string[0].split()[0])))
                    longitude.append(
                        self.normalize_x(float(coord_string[1].split()[0])))
                    longitude.append(
                        self.normalize_x(float(coord_string[2].split()[0])))
                    longitude.append(
                        self.normalize_x(float(coord_string[3].split()[0])))
                    latitude.append(float(coord_string[0].split()[1]))
                    latitude.append(float(coord_string[1].split()[1]))
                    latitude.append(float(coord_string[2].split()[1]))
                    latitude.append(float(coord_string[3].split()[1]))

                coordinate_string = str(longitude[0]) + ' ' + str(
                    latitude[0]) + ';' + str(longitude[1]) + ' ' + str(
                        latitude[1]) + ';' + str(longitude[2]) + ' ' + str(
                            latitude[2]) + ';' + str(longitude[3]) + ' ' + str(
                                latitude[3])
                print(coordinate_string)
                # These should be the same as the original pixel coordinates, modulo
                # some floating-point error.
                #assert numpy.max(numpy.abs(pixcrd - pixcrd2)) < 1e-6
                flipim = numpy.flipud(image)
                double_image = numpy.float64(flipim)
                myRaster = arcpy.NumPyArrayToRaster(double_image)
                myRaster.save(outputfile_and_path)
                source_pnt = pixcrd_string
                target_pnt = coordinate_string
                arcpy.Warp_management(outputfile_and_path, source_pnt,
                                      target_pnt, geo_file_and_path,
                                      "POLYORDER1", "BILINEAR")
                arcpy.Delete_management(outputfile_and_path)
Example #13
0
def mosaic(dnight, sets, filter):
    '''
    This module creates the mosaic of full-resolution images for each data set.
    '''
    #set arcpy environment variables part 2/2
    arcpy.CheckOutExtension("Spatial")
    arcpy.env.workspace = filepath.rasters + 'scratch_fullres/'
    arcpy.env.scratchWorkspace = filepath.rasters + 'scratch_fullres'

    #filter paths
    F = {'V': '', 'B': 'B/'}
    f = {'V': '', 'B': 'b'}

    for s in sets:
        #file paths
        calsetp = filepath.calibdata + dnight + '/S_0%s/%s' % (s[0], F[filter])
        gridsetp = filepath.griddata + dnight + '/S_0%s/%sfullres/' % (
            s[0], F[filter])
        if os.path.exists(gridsetp):
            shutil.rmtree(gridsetp)
        os.makedirs(gridsetp)

        #read in the registered images coordinates
        file = filepath.calibdata + dnight + '/pointerr_%s.txt' % s[0]
        Obs_AZ, Obs_ALT = n.loadtxt(file, usecols=(3, 4)).T
        Obs_AZ[n.where(Obs_AZ > 180)] -= 360
        Obs_AZ[35] %= 360

        #read in the best-fit zeropoint and plate scale
        file = filepath.calibdata + dnight + '/extinction_fit_%s.txt' % filter
        zeropoint, platescale, exptime = n.loadtxt(file,
                                                   usecols=(2, 8, 9),
                                                   unpack=True,
                                                   ndmin=2)

        #loop through each file in the set
        for w in range(len(Obs_AZ) + 1):

            v = w + 1
            if w == 45:
                w = 35
                Obs_AZ[w] -= 360

            if v in range(0, 50, 5): print 'Generating fullres image %i/45' % v

            arcpy.CopyRaster_management(calsetp + '/tiff/ib%03d.tif' % (w + 1),
                                        'ib%03d.tif' % v, "DEFAULTS", "", "",
                                        "", "", "16_BIT_UNSIGNED")

            #re-define projection to topocentric coordinates
            arcpy.DefineProjection_management("ib%03d.tif" % v,
                                              tc(Obs_AZ[w], Obs_ALT[w]))

            #warp image to remove barrel distortion image
            arcpy.Warp_management("ib%03d.tif" % v, source_pnt, target_pnt,
                                  'ibw%03d.tif' % v, "POLYORDER3", "BILINEAR")

            #reproject into GCS
            arcpy.ProjectRaster_management('ibw%03d.tif' % v,
                                           'fwib%03d.tif' % v, geogcs,
                                           "BILINEAR", "0.0261")

            #clip to image boundary
            rectangle = clip_envelope(Obs_AZ, Obs_ALT, w)
            arcpy.Clip_management("fwib%03d.tif" % v, rectangle,
                                  "fcib%03d" % v)

        #mosaic raster list must start with an image with max pixel value > 256
        v = 1
        mstart = 1
        while v < (len(Obs_AZ) + 1):
            im = imread(filepath.rasters + 'scratch_fullres/ib%03d.tif' % v)
            if n.max(im) > 255:
                mstart = v
                break
            v += 1

        #mosaic raster list
        R1 = ';'.join(['fcib%03d' % i for i in range(mstart, 47)])
        R2 = ';'.join(['fcib%03d' % i for i in range(1, mstart)])
        R = R1 + ';' + R2

        #mosaic to topocentric coordinate image; save in Griddata\
        print "Mosaicking into all sky full-resolution image"
        arcpy.MosaicToNewRaster_management(R, gridsetp, 'skytopo', geogcs,
                                           "32_BIT_FLOAT", "0.0261", "1",
                                           "BLEND", "FIRST")

        #convert to magnitudes per square arc second
        print "Converting the mosaic to mag per squard arcsec"
        psa = 2.5 * n.log10(
            (platescale[int(s[0]) - 1] * 60)**2)  # platescale adjustment
        stm1 = arcpy.sa.Raster(gridsetp + os.sep + 'skytopo')
        stm2 = stm1 / exptime[0]
        stm3 = arcpy.sa.Log10(stm2)
        stm4 = 2.5 * stm3
        skytopomags = zeropoint[int(s[0]) - 1] + psa - stm4

        #save mags mosaic to disk
        skytopomags.save(gridsetp + os.sep + 'skytopomags')

        print "Creating layer files for full-resolution mosaic"
        layerfile = filepath.griddata + dnight + '/skytopomags%s%s.lyr' % (
            f[filter], s[0])
        arcpy.MakeRasterLayer_management(
            gridsetp + 'skytopomags',
            dnight + '_%s_fullres%s' % (s[0], f[filter]))
        arcpy.SaveToLayerFile_management(
            dnight + '_%s_fullres%s' % (s[0], f[filter]), layerfile,
            "RELATIVE")

        #Set layer symbology to magnitudes layer
        symbologyLayer = filepath.rasters + 'magnitudes.lyr'
        arcpy.ApplySymbologyFromLayer_management(layerfile, symbologyLayer)
        lyrFile = arcpy.mapping.Layer(layerfile)
        lyrFile.replaceDataSource(gridsetp, 'RASTER_WORKSPACE', 'skytopomags',
                                  'FALSE')
        lyrFile.save()
    def ConvertToTIFF(self):
        arcpy.env.pyramid = "NONE"
        arcpy.env.rasterStatistics = "NONE"

        if not os.path.exists(self.output_TIFF_path):
            os.makedirs(self.output_TIFF_path)
            #Get list of files that will be converted
        fits_files = [
            f for f in listdir(self.write_to_folder)
            if isfile(join(self.write_to_folder, f))
        ]

        counter = 1
        #For every file...
        for fits_file in fits_files:
            print("Processing file: " + str(counter) + ", " + fits_file +
                  "...")
            counter = counter + 1
            filename = fits_file
            output_filename = filename[:-4] + 'rev.tif'
            geo_output_filename = filename[:-4] + 'rev.' + self.projection + '.tif'
            outputfile_and_path = self.output_TIFF_path + "\\" + output_filename
            geo_file_and_path = self.output_TIFF_path + "\\" + geo_output_filename
            file_and_path = self.write_to_folder + "\\" + filename
            if not os.path.exists(geo_file_and_path):
                #Print statements just to be sure everything is right
                #print(outputfile_and_path)
                #print(geo_file_and_path)
                hdulist = fits.open(file_and_path)
                w = wcs.WCS(hdulist[0].header)
                image = hdulist[0].data
                # Some pixel coordinates of interest.
                pixcrd = numpy.array(
                    [[0, 0], [0, image.shape[0]], [image.shape[1], 0],
                     [image.shape[1], image.shape[0]]], numpy.float_)
                pixcrd_string = '0 0;' + '0 ' + str(
                    image.shape[0]) + ';' + str(image.shape[1]) + ' 0;' + str(
                        image.shape[1]) + ' ' + str(image.shape[0])
                #print(pixcrd_string)
                # Convert pixel coordinates to world coordinates
                # The second argument is "origin" -- in this case we're declaring we
                # have 1-based (Fortran-like) coordinates.
                world = w.wcs_pix2world(pixcrd, 1)
                #print(world)
                if (self.projection == 'WCS'):
                    coordinate_string = str(-1 * world[0, 0]) + ' ' + str(
                        world[0, 1]) + ';' + str(-1 * world[1, 0]) + ' ' + str(
                            world[1, 1]) + ';' + str(
                                -1 * world[2, 0]) + ' ' + str(
                                    world[2, 1]) + ';' + str(
                                        -1 * world[3, 0]) + ' ' + str(world[3,
                                                                            1])
                    #print(coordinate_string)
                if (self.projection == 'GALACTIC'):
                    # Convert the same coordinates back to pixel coordinates.
                    pixcrd2 = w.wcs_world2pix(world, 1)
                    coordinate = SkyCoord(world, frame='icrs', unit='deg')
                    coord = coordinate.galactic
                    print(coord.to_string('decimal'))
                    coord_string = coord.to_string('decimal')
                    longitude = []
                    latitude = []
                    #print(coord_string[0].split()[1])
                    longitude.append(float(coord_string[0].split()[0]))
                    longitude.append(float(coord_string[1].split()[0]))
                    longitude.append(float(coord_string[2].split()[0]))
                    longitude.append(float(coord_string[3].split()[0]))
                    latitude.append(float(coord_string[0].split()[1]))
                    latitude.append(float(coord_string[1].split()[1]))
                    latitude.append(float(coord_string[2].split()[1]))
                    latitude.append(float(coord_string[3].split()[1]))
                if longitude[0] >= 1:
                    coordinate_string = str(longitude[0]) + ' ' + str(
                        latitude[0]) + ';' + str(longitude[1]) + ' ' + str(
                            latitude[1]) + ';' + str(longitude[2]) + ' ' + str(
                                latitude[2]) + ';' + str(
                                    longitude[3]) + ' ' + str(latitude[3])
                else:
                    longitude[2] = longitude[2] - 360
                    longitude[3] = longitude[3] - 360
                    coordinate_string = str(longitude[0]) + ' ' + str(
                        latitude[0]) + ';' + str(longitude[1]) + ' ' + str(
                            latitude[1]) + ';' + str(
                                (longitude[2])) + ' ' + str(
                                    latitude[2]) + ';' + str(
                                        (longitude[3])) + ' ' + str(
                                            latitude[3])
                    #print(coordinate_string)
                if longitude[0] >= 180:
                    longitude[0] = (longitude[0] - 360)
                    longitude[1] = (longitude[1] - 360)
                    longitude[2] = (longitude[2] - 360)
                    longitude[3] = (longitude[3] - 360)
                #coordinate_string = str(longitude[0]) + ' ' + str(latitude[0]) + ';' + str(longitude[1]) + ' ' + str(latitude[1]) + ';' + str(longitude[2]) + ' ' + str(latitude[2]) + ';' + str(longitude[3]) + ' ' + str(latitude[3])
                longitude[0] = -1 * (longitude[0])
                longitude[1] = -1 * (longitude[1])
                longitude[2] = -1 * (longitude[2])
                longitude[3] = -1 * (longitude[3])
                coordinate_string = str(longitude[0]) + ' ' + str(
                    latitude[0]) + ';' + str(longitude[1]) + ' ' + str(
                        latitude[1]) + ';' + str(longitude[2]) + ' ' + str(
                            latitude[2]) + ';' + str(longitude[3]) + ' ' + str(
                                latitude[3])
                print(coordinate_string)
                # These should be the same as the original pixel coordinates, modulo
                # some floating-point error.
                assert numpy.max(numpy.abs(pixcrd - pixcrd2)) < 1e-6
                flipim = numpy.flipud(image)
                double_image = numpy.float64(flipim)
                myRaster = arcpy.NumPyArrayToRaster(double_image)
                myRaster.save(outputfile_and_path)
                source_pnt = pixcrd_string
                #"'0 0';'0 2237';'2408 0';'2408 2237'"
                target_pnt = coordinate_string
                arcpy.Warp_management(outputfile_and_path, source_pnt,
                                      target_pnt, geo_file_and_path,
                                      "POLYORDER1", "BILINEAR")
                arcpy.Delete_management(outputfile_and_path)