#!/usr/bin/env python2
# Script for reading RAW Bruker NMR spectra with nmrglue
# Created by Daniel Canueto

import nmrglue as ng
import numpy as np
import os
import zipfile
import csv
import shutil

# Input path where initial zip is located   
pre_path= "/data/MTBLS1.zip"   

# Path where the Bruker folders are located
path="/data/MTBLS1/"

# Extraction of Bruker folder from zip file
zip_ref = zipfile.ZipFile(pre_path, 'r')
zip_ref.extractall(path)
zip_ref.close()
for file in os.listdir(path):
    if file.endswith(".zip"):
        zip_ref = zipfile.ZipFile(path+file, 'r')
        zip_ref.extractall(path+"nmr")
        zip_ref.close()

# Remove __MACOSX folder
shutil.rmtree(path+"nmr"+"/__MACOSX")

# Create ppm scale necessary for pre-processing
bruker_folders=os.listdir(path+"nmr")
dummy, needed_data = ng.bruker.read_pdata(path+"nmr/"+bruker_folders[0]+'/10/pdata/1')
dic, data = ng.bruker.read(path+"nmr/"+bruker_folders[0]+'/10')
udic = ng.bruker.guess_udic(dic, data)
uc = ng.fileiobase.unit_conversion(needed_data.size,'True',udic[0]['sw'],udic[0]['obs'],udic[0]['car'])
ppm = uc.ppm_scale()
np.savetxt("data_ppm.csv", ppm, delimiter=",")

# Preallocation of fid info and dataset of processed spectra
initial_data=np.zeros((len(bruker_folders),needed_data.size))
fid_info=np.zeros((len(bruker_folders),9))

for x in range(0, len(bruker_folders)):
    dic, initial_data[x,:] = ng.bruker.read_pdata(path+"nmr/"+bruker_folders[x]+'/10/pdata/1')
    fid_info[x,:]=[dic['acqus']['TD'],dic['acqus']['BYTORDA'],dic['acqus']['DIGMOD'],dic['acqus']['DECIM'],dic['acqus']['DSPFVS'],dic['acqus']['SW_h'],dic['acqus']['SW'],dic['acqus']['O1'],1/(2*dic['acqus']['SW_h'])]


# Create csv dataset
csv_out = open("data_spectra.csv",'wb')
mywriter = csv.writer(csv_out)
for row in initial_data:
    mywriter.writerow(row)
csv_out.close()

# Create fid info dataset
csv_out = open("data_fid.csv",'wb')
mywriter = csv.writer(csv_out)
for row in fid_info:
    mywriter.writerow(row)
csv_out.close()





