def organise_data(folder,prefix='NACO',suffix='.fits',dry_run=True, save_dir='./',silent=False): """ Looks at all fits files in a folder and moves them to a consistent directory structure. dry_run: if this is set to True, no files will be moved, but their suggested locations will be printed Calib/[date]/[type]/Raw/ : the location of raw calibration data. The current types are: flats. Science/[date]/[target_name]/Targ/ : the location of the target data Science/[date]/[target_name]/Sky/ : the location of the skies (for AGPM data) Science/[date]/[target_name]/Flux/ : the location of the flux frames """ # Find the files files=glob.glob(folder+prefix+'*'+suffix) files.sort() if save_dir[-1] != '/': save_dir=save_dir+'/' if len(files) ==0: if not silent: print('No files to move!') else: if not silent: print(str(len(files))+' files to be sorted') for filename in files: hdr=pyfits.getheader(filename) # Now find the relevant information try: # this will fail for cal files target_name=hdr['HIERARCH ESO OBS TARG NAME'] # target_name=hdr['HIERARCH ESO OBS NAME']# this was changed for astcals except: target_name='' mjd=hdr['MJD-OBS'] science_flag=hdr['HIERARCH ESO DPR CATG'] obstype,folder_string=detect_filetype(hdr,get_folder_string=True) # Get the date in a nice format and convert so that observations made in # the same night have the same date. # MJD is in UTC, so split by midday by subtracting 12hrs and then # taking the *raw date* . date_time=Time(mjd-0.5,format='mjd') date,dummy=date_time.iso.split(' ') # We have to handle the astrometric calibrators first since they can # look like target obs if 'AstCal' in obstype: location=save_dir+'Calib/'+date+'/'+folder_string+'/' elif science_flag=='SCIENCE': location=save_dir+'Science/'+date+'/'+target_name+'/'+folder_string+'/' elif science_flag=='CALIB': location=save_dir+'Calib/'+date+'/'+folder_string+'/' elif science_flag=='ACQUISITION': location=save_dir+'Science/'+date+'/'+target_name+'/Acq/' else: if not silent: print('Unrecognised science_flag: '+science_flag) location='' if not os.path.isdir(location): os.makedirs(location) if not silent: print('Making new folder') # Now move the file: outname=filename.split('/')[-1] # this line is not particularly robust if dry_run: if not silent: print("Moving to:",location+outname) else: try: os.rename(filename,location+outname) except: if not silent: print("Failed to move: "+filename+' to '+location+outname)