commit e20cbe4c295af62c2f53c1b9304cd7191ec6dc54 Author: ignis Date: Tue Mar 2 21:10:45 2021 +0900 command line functions and case library api diff --git a/dnstool.py b/dnstool.py new file mode 100644 index 0000000..8c2183b --- /dev/null +++ b/dnstool.py @@ -0,0 +1,267 @@ +class DnsCase: + + data_start = (4+8*6+4) + (4+8*3+4) + 3 * (4+8*2+4) + 4 + + + def __init__ (self, case_root, case_name=None): + import os + import re + + p = re.compile("fort[.][0-9]{4,}") + + # Case name + if case_name is None: + self.name = os.path.basename(case_root) + else: + self.name = case_name + + # Case root directory path + self.case_root = os.path.abspath(case_root) + + # List of data files. fort.xxxx[x*] + self.data_files = (sorted(filter(p.match, os.listdir(case_root)))) + + # Read meta data from the first data file + # t0, it0, data array shape + info = self.read_info(os.path.join(case_root, self.data_files[0])) + self.t0 = info["time"] + self.it0 = info["itime"] + self.shape = (info["nx"], info["ny"], info["nz"]) + + # Read meta data from the first data file + info = self.read_info(os.path.join(case_root, self.data_files[-1])) + self.t1 = info["time"] + self.it1 = info["itime"] + + + self.time = None + self.itime = None + self.st = None + + + def setup_files (self, dst): + import os, shutil + + # Make symbolic links to data files + for datafile in self.data_files: + os.symlink( + os.path.join(self.case_root, datafile), + os.path.join(dst, datafile) + ) + + # Copy Input Parameterfiles + try: + shutil.copy( + os.path.join(self.case_root, "00_lessmag.in"), + dst + ) + except IOError as e: + print(e) + + + + def read_all_info (self): + """ + Loop over all data files to gather all info + """ + import os + if self.time is None: + self.time = [] + self.itime = [] + self.st = [] + for filename in self.data_files: + filepath = os.path.join(self.case_root, filename) + info = self.read_info(filepath) + self.time.append(info["time"]) + self.itime.append(info["itime"]) + self.st.append(info["u0"]) + + + def read_info (self, fname): + ''' + Read Info from a Single DNS Data File + ''' + import struct + + info_dict = {} + + with open(fname, 'rb') as f1 : + f1.seek(0) + + raw_info = f1.read(4+8*6+4)[4:-4] + info_dict['time'] = struct.unpack('d', raw_info[ 0: 8])[0] # fdmtime, double + info_dict['nx'] = struct.unpack('q', raw_info[ 8:16])[0] # nx, integer + info_dict['ny'] = struct.unpack('q', raw_info[16:24])[0] # ny, integer + info_dict['nz'] = struct.unpack('q', raw_info[24:32])[0] # nz integer + # oldsumc, double + # time_int, double + + raw_info = f1.read(4+8*3+4)[4:-4] + info_dict['itime'] = struct.unpack('q', raw_info[ 0: 8])[0] # fdmcyc, integer + info_dict['dt'] = struct.unpack('d', raw_info[ 8:16])[0] # DT, double + info_dict['u0'] = struct.unpack('d', raw_info[16:24])[0] # dummyu_, double + + raw_info = f1.read(4+8*2+4)[4:-4] + # dt_fdmsave, double + # dt_fdm_fullsave, double + + raw_info = f1.read(4+8*2+4)[4:-4] + # t_fdmsave, double + # t_fullsave, double + + raw_info = f1.read(4+8*2+4)[4:-4] + # in_yr, double + # out_yr, double + + return info_dict + + + def read_data (self, fname): + ''' + Read Single DNS Data File + ''' + + offset_data = (4+8*6+4) + (4+8*3+4) + 3 * (4+8*2+4) + 4 + + info = read_info(fname) + + t = info_dict['time'] + nx = info_dict['nx'] + ny = info_dict['ny'] + nz = info_dict['nz'] + + with open(fname, 'rb') as f1 : + f1.seek(offset_data) + + count = nx*ny*nz + + V = np.fromfile(f1, dtype=np.double, count=(3*count)).reshape((3,nz,ny,nx)) + s = np.fromfile(f1, dtype=np.double, count=(2*count)).reshape((2,nz,ny,nx)) + + return t, (nx, ny, nz), V, s + + + def read_single_field (self, fname, ifield): + ''' + Read Single DNS Data File + ''' + + offset_data = self.data_start + + info = read_info(fname) + + t = info_dict['time'] + nx = info_dict['nx'] + ny = info_dict['ny'] + nz = info_dict['nz'] + + with open(fname, 'rb') as f1 : + count = nx*ny*nz + + f1.seek(offset_data + count * 8 * ifield) + + V = np.fromfile(f1, dtype=np.double, count=count).reshape((nz,ny,nx)) + + return t, V + + +def case_library (): + + cases = ''' + IC1 /home1/dhkim/4pi_IC1/4pi_IC1_01_kfmax10_lt58 + IC2 /home1/dhkim/Backup_jinyeong/DNS/run/4pi_IC2/4pi_IC2_01_kfmax19_lt60 + IC3 /home1/dhkim/Backup_jinyeong/DNS/run/4pi_IC3/4pi_IC3_03_kfmax19_lt61_smallDt + IC4 /home1/dhkim/4pi_IC4/moved/4pi-IC4-additional_new + IC8 /home1/dhkim/Backup_jinyeong/DNS/run/4pi_IC8/4pi-IC8-02 + IC10 /home1/dhkim/4pi_IC10/4pi_IC10_06_kfmax_22 + IC11 /home1/dhkim/4pi_IC11 + IC12 /home1/dhkim/Backup_jinyeong/DNS/run/4pi_IC12 + IC13 /home1/dhkim/Backup_jinyeong/DNS/run/4pi_IC13/4pi_IC13_lt60'''.split() + + cic_case_dict = dict(zip(cases[0::2], cases[1::2])) + + database_root = '/home1/ignis/ignis/' + names = 'VIC1 VIC2 VIC4 VIC10 VIC13 '.split() + cases = 'ic1-5dm ic2-5dm ic4-5dm ic10-5dm ic13-5dm'.split() + + for case_name, case_dir in zip(names, cases): + case_root = os.path.join(database_root, case_dir) + cic_case_dict[case_name] = case_root + + + case_dict = dict([ (case_name, DnsCase(cic_case_dict[case_name], case_name=case_name)) + for case_name in cic_case_dict + ]) + + return case_dict + + + +if __name__ == "__main__": + + import os, sys, argparse + + cases = ''' + IC1 /home1/dhkim/4pi_IC1/4pi_IC1_01_kfmax10_lt58 + IC2 /home1/dhkim/Backup_jinyeong/DNS/run/4pi_IC2/4pi_IC2_01_kfmax19_lt60 + IC3 /home1/dhkim/Backup_jinyeong/DNS/run/4pi_IC3/4pi_IC3_03_kfmax19_lt61_smallDt + IC4 /home1/dhkim/4pi_IC4/moved/4pi-IC4-additional_new + IC8 /home1/dhkim/Backup_jinyeong/DNS/run/4pi_IC8/4pi-IC8-02 + IC10 /home1/dhkim/4pi_IC10/4pi_IC10_06_kfmax_22 + IC11 /home1/dhkim/4pi_IC11 + IC12 /home1/dhkim/Backup_jinyeong/DNS/run/4pi_IC12 + IC13 /home1/dhkim/Backup_jinyeong/DNS/run/4pi_IC13/4pi_IC13_lt60'''.split() + + cic_case_dict = dict(zip(cases[0::2], cases[1::2])) + + database_root = '/home1/ignis/ignis/' + names = 'VIC1 VIC2 VIC4 VIC10 VIC13 '.split() + cases = 'ic1-5dm ic2-5dm ic4-5dm ic10-5dm ic13-5dm'.split() + + for case_name, case_dir in zip(names, cases): + case_root = os.path.join(database_root, case_dir) + cic_case_dict[case_name] = case_root + + + # Commandline argument parser + + parser = argparse.ArgumentParser() + parser.add_argument("-l", "--list", help="list all cases", action='store_true') + parser.add_argument("-c", "--create", help="create a case or all cases") + args = parser.parse_args() + params = vars(args) + + + # List all cases + + if params["list"]: + for case_name in sorted(cic_case_dict): + print(case_name, cic_case_dict[case_name]) + sys.exit(0) + + + # Create case directories + + create_case = params["create"] + if create_case: + if create_case == "all": + print ("Creating All DNS Cases for Post-processing") + + for case_name in sorted(cic_case_dict): + print ("Creating DNS Case {} for Post-processing".format(case_name)) + os.mkdir(case_name) + + # Create DNS Case Object + case_obj = DnsCase(cic_case_dict[case_name], case_name=case_name) + case_obj.setup_files(case_name) + + else: + if create_case in cic_case_dict: + print ("Creating DNS Case {} for Post-processing".format(create_case)) + os.mkdir(create_case) + + # Create DNS Case Object + case_obj = DnsCase(cic_case_dict[create_case], case_name=create_case) + case_obj.setup_files(create_case) + else: + print ("Can't find DNS Case {}".format(create_case))