Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a rename function #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions uavsar_pytools/rename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pandas as pd
import os
import glob
from os import path
import shutil

def rename(dir_in, clean_dir = False):
"""This function renames UAVSAR data in a directory containing csv file of annotation to a more intuitive name.
The format will be sitename_date-of-first-acquisition_date-of-second-acquisition_XXXpolarization_filetype(coh/amp/hgd).
The renamed files are stored in a new directory called renamed.

Args:
dir_in (_type_): path to the directory containing the tiff files to be renamed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type should probably be string?

clean_dir (bool, optional): wether to delete the old files or not. Defaults to False.
"""

#check if crop directory exists
dir_out = dir_in + '/renamed/'
if not os.path.exists(dir_out):
os.makedirs(dir_out)

#read the metadata from the csv file
metadata = pd.read_csv(glob.glob(dir_in + '/*grd.csv')[0]) #it gives a list, so select the only item in the list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you from glob import glob and then just use glob here to match the pattern in the rest of the repo?


#grab the start time of first acquisition for pass 1
date1 = (metadata.loc[0, 'start time of acquisition for pass 1']).split()[0]

#grab the start time of acquisition for pass 2
date2 = (metadata.loc[0, 'start time of acquisition for pass 2']).split()[0]


#loop through the files
for tiff in glob.glob(dir_in + '/*grd.tiff'):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work for POLSAR, slant range images. Maybe a different script name that clarifies this is only for renaming uavsar interferogram ground projected images? and a check to make sure that the in_dir contains that right type of images?


#grab the site name from the tiff sting
file_name = (tiff.split('/')[-1]).split('_')[0]

#grab the last two sets of strings from the tiff name
set2 = (tiff.split('/')[-1]).split('_')[-2]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are set2 and set1 polarizations? What are these variables?

set1 = (tiff.split('/')[-1]).split('_')[-1]
new_name = dir_out + file_name + '_' + date1 + '_' + date2 + '_' + set2 + '_' + set1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using an f-string here?

join(dir_out, f'{file_name}{date1}{date2}{set2}{set1}


if clean_dir == False:
shutil.copy(tiff, new_name)
elif clean_dir == True:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user passes clean_dir == true, could we move them to this new directory and then have a section that moves the tiffs back into the main directory and removes the newly created renamed directory?

shutil.move(tiff, new_name)