-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_toh5.py
More file actions
139 lines (125 loc) · 5.69 KB
/
convert_toh5.py
File metadata and controls
139 lines (125 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/python3
#
############################# PROGRAM DESCRIPTION #####################################
#
#
# Convert file to a hdf5 table.
#
#
############################# BLOCK IMPORTS AND STUFF #################################
import sys
import os
import numpy as np
from astropy.table import Table, Column, MaskedColumn
import pandas as pd
from glob import glob
import argparse as argp
import os, errno
import shutil
############################# BLOCK READ INPUT ########################################
parser = argp.ArgumentParser(description='Convert data to hdf5.')
parser.add_argument('-tile',help='tile: the tilename as dxxx. Example: python3 /net/nas3/popes/efsokmen/MW/SCRIPTS/convert_toh5.py -tile d037 -pathraw /media/sdc1/RDISK/d037/d037/ -pathcp /net/nas3/popes/efsokmen/MW/DATA/VVV/DISK/ ', type=str, required=True)
parser.add_argument('-pathraw',help='pathraw: path to output of DAOMASTER, preferable CALIB_LB_*.raw file. ', type=str, required=True)
parser.add_argument('-pathcp',help='pathcp: path to cp the compressed output hdf5. ', type=str, required=True)
#######################################################################################
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
##########################################################
args = parser.parse_args()
tilename = args.tile
pathraw = args.pathraw
pathcp = args.pathcp
os.chdir(pathraw)
print ("Working in the directory {}".format(pathraw))
##########################################################
# This to ensure that the files actually exist...
for afile in glob(pathraw + "CALIB_LB_???"+tilename+"JK.raw"):
if not os.path.isfile(afile):
sys.exit(bcolors.FAIL + 'File ' + afile + ' not found!' + bcolors.ENDC)
for bfile in glob(pathraw + "CALIB_LB_???"+tilename+"JK.rawh5"):
if os.path.isfile(bfile):
calibh5 = glob("CALIB_LB_*h5")
calibh5.sort()
longdir = pathcp
try:
os.makedirs(longdir+tilename)
print ("made the directory {}".format(longdir+tilename))
except OSError as e:
print ("Folder {} exists! Skipped creating the same folder.".format(longdir+tilename))
if e.errno != errno.EEXIST:
raise
for i in calibh5:
print ("Copying {} to {}".format(i, pathcp))
shutil.copy(pathraw + i, pathcp + tilename+'/'+i)
sys.exit("Exiting without recreating h5 file.")
##########################################################
def convert_toh5(data_raw):
o = pd.read_table(data_raw,header=1,names=("ID","L","B","J","Jerr","K","Kerr","Chi","Sharp"),delim_whitespace=True,low_memory=False, skiprows=3)
to = Table.from_pandas(o)
#m = (~np.isnan(to["CS"])) # to mask the NAN values
too = to#[m]
namefile = data_raw+"h5"
filo = open(namefile, "w+")
tt = Table([too["ID"],too["L"],too["B"],too["J"],too["Jerr"],too["K"],too["Kerr"],too["Chi"],too["Sharp"]], names=["ID", "L","B", "J", "Jerr", "K", "Kerr", "Chi", "Sharp"])
tt.write(namefile, format='hdf5',path=data_raw[:-6], serialize_meta=True, compression=True,overwrite=True)
print ("Created {}.".format(namefile))
filo.close()
return 0
myraws = glob('CALIB_LB_???'+tilename+'JK.raw')
myraws.sort()
for ind,val in enumerate(myraws):
print ("Writing {}.".format(val))
convert_toh5(val)
#cp *.rawh5 /net/nas3/popes/efsokmen/MW/DATA/VVV/DISK/
longdir = pathcp
try:
os.makedirs(longdir+tilename)
print ("made the directory {}".format(longdir+tilename))
except OSError as e:
print ("Folder {} exists! Skipped creating the same folder, but copying the new files.".format(longdir+tilename))
if e.errno != errno.EEXIST:
raise
calibh5 = glob("CALIB_LB_*h5")
calibh5.sort()
for i in calibh5:
shutil.copy(pathraw + i, pathcp + tilename+'/'+i)
print ("Copied {} to {}".format(pathraw + i, pathcp+tilename))
##########################################################
#def convert_toh5_cs(data_raw):
# o = pd.read_table(data_raw,header=1,names=("ID","L","B","J","Jerr","K","Kerr","Chi","Sharp", "CS"),delim_whitespace=True,low_memory=False, skiprows=3)
# to = Table.from_pandas(o)
# m = (~np.isnan(to["CS"])) # to mask the NAN values
# too = to#[m]
# namefile = data_raw+"h5"
# filo = open(namefile, "w+")
# tt = Table([too["ID"],too["L"],too["B"],too["J"],too["Jerr"],too["K"],too["Kerr"],too["Chi"],too["Sharp"],too["CS"]], names=["ID", "L","B", "J", "Jerr", "K", "Kerr", "Chi", "Sharp","CS"])
# tt.write(namefile, format='hdf5',path=data_raw[:-6], serialize_meta=True, compression=True,overwrite=True)
# print ("Created {}.".format(namefile))
# filo.close()
# return 0
#def convert_toh5_ee(data_raw):
# o = pd.read_table(data_raw,header=1,names=("ID","L","B","J","Jerr","K","Kerr","Chi","Sharp", "CS", "EE"),delim_whitespace=True,low_memory=False, skiprows=3)
# to = Table.from_pandas(o)
# m = (~np.isnan(to["CS"])) # to mask the NAN values
# too = to#[m]
# namefile = data_raw+"h5"
# filo = open(namefile, "w+")
# tt = Table([too["ID"],too["L"],too["B"],too["J"],too["Jerr"],too["K"],too["Kerr"],too["Chi"],too["Sharp"],too["CS"], too["EE"]], names=["ID", "L","B", "J", "Jerr", "K", "Kerr", "Chi", "Sharp","CS","EE"])
# tt.write(namefile, format='hdf5',path=data_raw[:-6], serialize_meta=True, compression=True,overwrite=True)
# print ("Created {}.".format(namefile))
# filo.close()
# return 0
#
### Read the h5:
#from astropy.table import Table
#l = []; n = glob("*h5")
#for ind, val in enumerate(n):
# t = Table.read(val, path=val[:17])
# l.append(t)