<commit_before># Copyright (C) 2015, <NAME> <<EMAIL>>
# Copyright (C) 2015, <NAME> <<EMAIL>>
# Copyright (C) 2015, <NAME> <<EMAIL>>
# Copyright (C) 2015, <NAME> <<EMAIL>>


import numpy as np
from config import cfg
import os
import shutil

from python import tile_composer
from python import common



def write_vrt_files(tilesFullInfo):
    """
    Merges pieces of data into single VRT files : height map comprising the N pairs, height map for each signle pair, and err_rpc 

    Args:
         - tilesFullInfo : a dictionary that provides all you need to process a tile for a given tile directory : col,row,tw,th,ov,i,j,pos,x,y,w,h,images,NbPairs,cld_msk,roi_msk = tilesFullInfo[tile_dir]
    """
    #VRT file : height map (N pairs)    

    #-------------- tileComposerInfo -------------- 
    # tileComposerInfo : a simple list that gives the size of the tiles (after having removed the overlapping areas), 
    # regarding their positions inside the ROI --> ULw , ULh, Lw , Lh, Uw , Uh, Mw , Mh
    col,row,tw,th,ov,i,j,pos,x,y,w,h,images,NbPairs,cld_msk,roi_msk=tilesFullInfo.values()[0]
    ULw , ULh = tw-ov/2, th-ov/2 #Size of Corner tiles (UL UR BL BR)
    Lw , Lh   = tw-ov/2,th-ov    #Size of Left/Right tile (L R)
    Uw , Uh   = tw-ov,th-ov/2    #Size of Upper/Bottom tile (U B)
    Mw , Mh   = tw-ov,th-ov      #Size of Middle tile

    rangey = np.arange(y, y + h - ov, th - ov)
    rangex = np.arange(x, x + w - ov, tw - ov)
    rowmin,rowmax = rangey[0],rangey[-1]
    colmin,colmax = rangex[0],rangex[-1]
    
    #Tile composer info (bis)
    imax=len(rangey)-1
    jmax=len(rangex)-1
    fh = 2*ULh+(imax-1)*Mh #height of the final height map after removing margins
    fw = 2*ULw+(jmax-1)*Mh #width of the final height map after removing margins
    #-------------- tileComposerInfo (end) --------------
    
    
    tileSizesAndPositions={}
    for tile_dir in tilesFullInfo:

        col,row,tw,th,ov,i,j,pos,x,y,w,h,images,NbPairs,cld_msk,roi_msk=tilesFullInfo[tile_dir]
        
        dicoPos={}
        dicoPos['M']  = [ULw+(j-1)*Mw, ULh+(i-1)*Mh , Mw, Mh]
        dicoPos['UL'] = [0,0, ULw, ULh]
        dicoPos['UR'] = [fw-ULw,0, ULw, ULh]
        dicoPos['BR'] = [fw-ULw,fh-ULh, ULw, ULh]
        dicoPos['BL'] = [0,fh-ULh, ULw, ULh]
        dicoPos['L']  = [0,ULh+(i-1)*Lh, Lw, Lh]
        dicoPos['R']  = [fw-ULw,ULh+(i-1)*Lh, Lw, Lh]
        dicoPos['U']  = [ULw+(j-1)*Uw, 0, Uw, Uh]
        dicoPos['B']  = [ULw+(j-1)*Uw, fh-ULh, Uw, Uh]
        dicoPos['Single'] = [0,0, tw, th]
        
        tile_reldir = 'tile_%d_%d_row_%d/col_%d/' % (tw, th, row, col)

        tileSizesAndPositions[tile_reldir] = dicoPos[pos]  
   
    
    z = cfg['subsampling_factor']
    tile_composer.mosaic_gdal2(cfg['out_dir'] + '/heightMap_N_pairs.vrt', tileSizesAndPositions, 'local_merged_height_map_crop.tif', fw, fh,z)
    

    # VRT file : height map (for each single pair)
    # VRT file : rpc_err (for each single pair)
    for i in range(0,NbPairs):
        
        pair_id = i+1
        
        pairSizesAndPositions={}
        for tile_reldir in tileSizesAndPositions:
            pair_reldir = tile_reldir + '/pair_%d' % (pair_id)
            pairSizesAndPositions[pair_reldir] = tileSizesAndPositions[tile_reldir]  
        
        tile_composer.mosaic_gdal2(cfg['out_dir'] + '/heightMap_pair_%d.vrt' % (pair_id), pairSizesAndPositions, 'height_map_crop.tif', fw, fh,z)
        tile_composer.mosaic_gdal2(cfg['out_dir'] + '/rpc_err_pair_%d.vrt' % (pair_id), pairSizesAndPositions, 'rpc_err_crop.tif', fw, fh,z)    



def write_dsm(tilesFullInfo,n=5):
    """
    Writes the DSM, from the ply files given by each tile.

    Args :
         - tilesFullInfo : a dictionary that provides all you need to process a tile for a given tile directory : col,row,tw,th,ov,i,j,pos,x,y,w,h,images,NbPairs,cld_msk,roi_msk = tilesFullInfo[tile_dir]
    """
    clouds_dir = cfg['out_dir']+'/clouds'
    if (os.path.exists(clouds_dir)):
        shutil.rmtree(clouds_dir)
    os.mkdir(clouds_dir)    
    
    for tile_dir in tilesFullInfo:
        
        col,row,tw,th,ov,i,j,pos,x,y,w,h,images,NbPairs,cld_msk,roi_msk=tilesFullInfo[tile_dir]
        cloud = tile_dir + '/cloud.ply'
        cloud_link_name = clouds_dir + '/cloud_%d_%d_row_%d_col_%d.ply' % (tw, th, row, col)
        common.run('ln -s %s %s' % (cloud,cloud_link_name) )
        
    out_dsm_dir = '%s/dsm' % (cfg['out_dir'])
    if (os.path.exists(out_dsm_dir)):
        shutil.rmtree(out_dsm_dir)
    os.mkdir(out_dsm_dir) 
     
    common.run("ls %s | plyflatten %f %i %s" % (clouds_dir+'/cloud*',cfg['dsm_resolution'], n, out_dsm_dir))
    common.run("gdalbuildvrt %s %s" %(cfg['out_dir']+'/dsm.vrt',out_dsm_dir+'/dsm*'))<commit_msg>test existence of .ply files before doing ln -s<commit_after># Copyright (C) 2015, <NAME> <<EMAIL>>
# Copyright (C) 2015, <NAME> <<EMAIL>>
# Copyright (C) 2015, <NAME> <<EMAIL>>
# Copyright (C) 2015, <NAME> <<EMAIL>>


import numpy as np
from config import cfg
import os
import shutil

from python import tile_composer
from python import common



def write_vrt_files(tilesFullInfo):
    """
    Merges pieces of data into single VRT files : height map comprising the N pairs, height map for each signle pair, and err_rpc 

    Args:
         - tilesFullInfo : a dictionary that provides all you need to process a tile for a given tile directory : col,row,tw,th,ov,i,j,pos,x,y,w,h,images,NbPairs,cld_msk,roi_msk = tilesFullInfo[tile_dir]
    """
    #VRT file : height map (N pairs)    

    #-------------- tileComposerInfo -------------- 
    # tileComposerInfo : a simple list that gives the size of the tiles (after having removed the overlapping areas), 
    # regarding their positions inside the ROI --> ULw , ULh, Lw , Lh, Uw , Uh, Mw , Mh
    col,row,tw,th,ov,i,j,pos,x,y,w,h,images,NbPairs,cld_msk,roi_msk=tilesFullInfo.values()[0]
    ULw , ULh = tw-ov/2, th-ov/2 #Size of Corner tiles (UL UR BL BR)
    Lw , Lh   = tw-ov/2,th-ov    #Size of Left/Right tile (L R)
    Uw , Uh   = tw-ov,th-ov/2    #Size of Upper/Bottom tile (U B)
    Mw , Mh   = tw-ov,th-ov      #Size of Middle tile

    rangey = np.arange(y, y + h - ov, th - ov)
    rangex = np.arange(x, x + w - ov, tw - ov)
    rowmin,rowmax = rangey[0],rangey[-1]
    colmin,colmax = rangex[0],rangex[-1]
    
    #Tile composer info (bis)
    imax=len(rangey)-1
    jmax=len(rangex)-1
    fh = 2*ULh+(imax-1)*Mh #height of the final height map after removing margins
    fw = 2*ULw+(jmax-1)*Mh #width of the final height map after removing margins
    #-------------- tileComposerInfo (end) --------------
    
    
    tileSizesAndPositions={}
    for tile_dir in tilesFullInfo:

        col,row,tw,th,ov,i,j,pos,x,y,w,h,images,NbPairs,cld_msk,roi_msk=tilesFullInfo[tile_dir]
        
        dicoPos={}
        dicoPos['M']  = [ULw+(j-1)*Mw, ULh+(i-1)*Mh , Mw, Mh]
        dicoPos['UL'] = [0,0, ULw, ULh]
        dicoPos['UR'] = [fw-ULw,0, ULw, ULh]
        dicoPos['BR'] = [fw-ULw,fh-ULh, ULw, ULh]
        dicoPos['BL'] = [0,fh-ULh, ULw, ULh]
        dicoPos['L']  = [0,ULh+(i-1)*Lh, Lw, Lh]
        dicoPos['R']  = [fw-ULw,ULh+(i-1)*Lh, Lw, Lh]
        dicoPos['U']  = [ULw+(j-1)*Uw, 0, Uw, Uh]
        dicoPos['B']  = [ULw+(j-1)*Uw, fh-ULh, Uw, Uh]
        dicoPos['Single'] = [0,0, tw, th]
        
        tile_reldir = 'tile_%d_%d_row_%d/col_%d/' % (tw, th, row, col)

        tileSizesAndPositions[tile_reldir] = dicoPos[pos]  
   
    
    z = cfg['subsampling_factor']
    tile_composer.mosaic_gdal2(cfg['out_dir'] + '/heightMap_N_pairs.vrt', tileSizesAndPositions, 'local_merged_height_map_crop.tif', fw, fh,z)
    

    # VRT file : height map (for each single pair)
    # VRT file : rpc_err (for each single pair)
    for i in range(0,NbPairs):
        
        pair_id = i+1
        
        pairSizesAndPositions={}
        for tile_reldir in tileSizesAndPositions:
            pair_reldir = tile_reldir + '/pair_%d' % (pair_id)
            pairSizesAndPositions[pair_reldir] = tileSizesAndPositions[tile_reldir]  
        
        tile_composer.mosaic_gdal2(cfg['out_dir'] + '/heightMap_pair_%d.vrt' % (pair_id), pairSizesAndPositions, 'height_map_crop.tif', fw, fh,z)
        tile_composer.mosaic_gdal2(cfg['out_dir'] + '/rpc_err_pair_%d.vrt' % (pair_id), pairSizesAndPositions, 'rpc_err_crop.tif', fw, fh,z)    



def write_dsm(tilesFullInfo,n=5):
    """
    Writes the DSM, from the ply files given by each tile.

    Args :
         - tilesFullInfo : a dictionary that provides all you need to process a tile for a given tile directory : col,row,tw,th,ov,i,j,pos,x,y,w,h,images,NbPairs,cld_msk,roi_msk = tilesFullInfo[tile_dir]
    """
    clouds_dir = cfg['out_dir']+'/clouds'
    if (os.path.exists(clouds_dir)):
        shutil.rmtree(clouds_dir)
    os.mkdir(clouds_dir)    
    
    for tile_dir in tilesFullInfo:
        
        col,row,tw,th,ov,i,j,pos,x,y,w,h,images,NbPairs,cld_msk,roi_msk=tilesFullInfo[tile_dir]
        cloud = tile_dir + '/cloud.ply'
        cloud_link_name = clouds_dir + '/cloud_%d_%d_row_%d_col_%d.ply' % (tw, th, row, col)
        if (os.path.exists(cloud)):
            common.run('ln -s %s %s' % (cloud,cloud_link_name) )
        
    out_dsm_dir = '%s/dsm' % (cfg['out_dir'])
    if (os.path.exists(out_dsm_dir)):
        shutil.rmtree(out_dsm_dir)
    os.mkdir(out_dsm_dir) 
     
    common.run("ls %s | plyflatten %f %i %s" % (clouds_dir+'/cloud*',cfg['dsm_resolution'], n, out_dsm_dir))
    common.run("gdalbuildvrt %s %s" %(cfg['out_dir']+'/dsm.vrt',out_dsm_dir+'/dsm*'))
{'fraction_non_alphanumeric': 0.15743654289909798, 'fraction_numerical': 0.010698552548772814, 'mean_word_length': 2.9222542163718632, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2824777', 'n_tokens_mistral': 3984, 'n_tokens_neox': 3850, 'n_words': 809}