-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
shutil.move(tiff, new_name) |
There was a problem hiding this comment.
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?