"""
* Copyright (c) 2009 Stanford University, School of Medicine All rights reserved. 
* 
* STANFORD ACADEMIC SOFTWARE LICENSE AGREEMENT 
* FOR AngioLocalize SOURCE CODE: 
* 
* 1.	This is a legal agreement between you, RECIPIENT, and STANFORD UNIVERSITY.  
*       By accepting, receiving, and using AngioLocalize.including any accompanying information, materials or 
*       manuals (Program), you are agreeing to be bound by the terms of this Agreement.  
*       If you do not agree to the terms of this Agreement, promptly return the Program to the STANFORD.

* 2.	STANFORD grants to RECIPIENT a royalty-free, nonexclusive, and nontransferable license to use the Program 
*       and accompanying source code furnished hereunder, upon the terms and conditions set out below. 
*       documentation and/or other materials provided with the distribution. 
* 3.	RECIPIENT acknowledges that the Program is a research tool still in the development stage and that 
*       it is being supplied as is, without any accompanying services, support or improvements from STANFORD.
* 4.	RECIPIENT agrees to use the Program solely for internal non-commercial purposes. 
* 5.	RECIPIENT may create derivatives and further transfer the Program under the following terms and conditions: 
*        A) Recipient agrees to not commercially exploit the Program and agrees all uses will be for non-commercial purposes; and,
*        B) Recipient may not mark any derivatives with the title of Program and may not to use iPad mark for any derivatives; and,
*        C) Recipient agrees to mark and acknowledge all derivatives as follows: "This is a derivative work based on AngioLocalize:
*           Frameless AVM localization with 2D Digital Subtraction Angiography (DSA) and 3D Rotational Angiography (DRA) Images developed
*           by Dimitre Hristov, Lina Liu.
*        D) Recipient agrees to make all improvements, enhancements and derivatives available under 
*           similar terms and conditions to the research community. 
* 6. 	RECIPIENT warrants that RECIPIENT will not remove or export any part of the software or Program from the United 
*       States except in full compliance with all United States and other applicable laws and regulations.
* 7.	RECIPIENT will use the Program in compliance with all applicable laws, policies and regulations including, but not 
*       limited to, any approvals, informed consent and patient confidentiality principles.
* 8. 	RECIPIENT will indemnify, hold harmless, and defend Stanford against any claim of any kind arising out of or
*       related to the exercise of any rights granted under this Agreement or the breach of this Agreement by RECIPIENT.
* 9.	Title and copyright to the Program and any associated documentation shall at all times remain with STANFORD, 
*       and RECIPIENT agrees to preserve same.
""" 
#----------------------------------------------------------------------------------
#! Macro module AngioLocalizeFileOp
#/*!
# \file    AngioLocalizeFileOp.py
# \author  lina
# \date    2009-02-09
#
#  This file handles file related operations such as save to a file or load data from file
#*/
#----------------------------------------------------------------------------------
"""
Module AngioLocalizeFileOp.
"""

from mevis import *
import os;
import shutil;

## save 3D registration to file
def saveRegToFile(regMatrix, srcID, targetID, patientID):
  saveDir = "C:/MLab2.0UserData";
  if not os.path.isdir(os.path.normpath(saveDir)):
      os.mkdir(os.path.normpath(saveDir), 0777);
  saveDir = saveDir + "/" + patientID;
  if not os.path.isdir(os.path.normpath(saveDir)):
      os.mkdir(os.path.normpath(saveDir), 0777);
  saveDir = saveDir + "/Reg";
  if not os.path.isdir(os.path.normpath(saveDir)):
      os.mkdir(os.path.normpath(saveDir), 0777);
  saveDir = saveDir + "/" + str(srcID) + "-" + str(targetID);
  if not os.path.isdir(os.path.normpath(saveDir)):
    os.mkdir(os.path.normpath(saveDir), 0777);
  fileDir = os.path.normcase(saveDir + "/RegContent");
  f = open(fileDir, 'w');
  f.write(regMatrix);
  MLAB.log("The Registration :" + regMatrix + " has been saved to " + fileDir + " successfully.");
  f.close();  

## save Projection registration to file
def saveProjRegToFile(affineMatrix, perspectMatrix, postAffineM, srcID, targetID, patientID):
  saveDir = "C:/MLab2.0UserData";
  if not os.path.isdir(os.path.normpath(saveDir)):
      os.mkdir(os.path.normpath(saveDir), 0777);
  saveDir = saveDir + "/" + patientID;
  if not os.path.isdir(os.path.normpath(saveDir)):
      os.mkdir(os.path.normpath(saveDir), 0777);
  saveDir = saveDir + "/2D32DReg";
  if not os.path.isdir(os.path.normpath(saveDir)):
      os.mkdir(os.path.normpath(saveDir), 0777);
  saveDir = saveDir + "/" + str(srcID) + "-" + str(targetID);
  if not os.path.isdir(os.path.normpath(saveDir)):
    os.mkdir(os.path.normpath(saveDir), 0777);
  fileDir = os.path.normcase(saveDir + "/RegContent");
  MLAB.log("The matrices" +  affineMatrix + " " + perspectMatrix + " " + postAffineM + " has been saved into file:" + fileDir + ".");
  f = open(fileDir, 'w');
  f.write(affineMatrix);
  f.write("\n");
  f.write(perspectMatrix);
  f.write("\n");
  f.write(postAffineM);
  f.close();
  
## get Structure directory 
def getCSODir(patientID, fileName):
  saveDir = "C:/MLab2.0UserData";
  if not os.path.isdir(os.path.normpath(saveDir)):
      os.mkdir(os.path.normpath(saveDir), 0777);
  saveDir = saveDir + "/" + patientID;
  if not os.path.isdir(os.path.normpath(saveDir)):
      os.mkdir(os.path.normpath(saveDir), 0777);
  saveDir = saveDir + "/CSO";
  if not os.path.isdir(os.path.normpath(saveDir)):
      os.mkdir(os.path.normpath(saveDir), 0777);
    
    # we use the format: "patientID(time).dcm" as unique name for the image
  CSOName = os.path.normpath(saveDir + "/" + fileName);
  return CSOName;

## save the Image to file  
def saveImageToFile(patientID, fileName, type):
  saveDir = "C:/MLab2.0UserData";
  if not os.path.isdir(os.path.normpath(saveDir)):
    os.mkdir(os.path.normpath(saveDir), 0777);
  saveDir = saveDir + "/" + patientID;
  if not os.path.isdir(os.path.normpath(saveDir)):
    os.mkdir(os.path.normpath(saveDir), 0777);
  saveDir = saveDir + "/Images";
  if not os.path.isdir(os.path.normpath(saveDir)):
    os.mkdir(os.path.normpath(saveDir), 0777);
  if type == "/Src" or type == "/Target":
      saveDir = saveDir + type;
      if not os.path.isdir(os.path.normpath(saveDir)):
        os.mkdir(os.path.normpath(saveDir), 0777);

  ## we use the format: "patientID(time).dcm" as unique name for the image
  imageName = saveDir + "/" + fileName;
  ret =  os.path.normpath(imageName);
  MLAB.log("image to load:" + ret);
  return ret;

## delete 3D registration file
def delete3DReg(patientID, srcID, targetID):
  regDir = "C:/MLab2.0UserData/" + patientID + "/Reg/" + str(srcID) + "-" + str(targetID);
  regFile = regDir + "/regcontent";
  normalPath = os.path.normpath(regDir);
  
  if not os.path.exists(normalPath):
    return;
  os.remove(regFile);
  MLAB.log("Registration file :" + normalPath + " has been deleted. ");
  if os.listdir(regDir) == []:
    os.rmdir(regDir);
   
## delete 2D 3D registration file   
def delete2D3DReg(patientID, srcID, targetID):
  regDir = "C:/MLab2.0UserData/" + patientID + "/2D3DReg/" + str(srcID) + "-" + str(targetID);
  regFile = regDir + "/regcontent";
  normalPath = os.path.normpath(regDir);
  if not os.path.exists(normalPath):
    return;
  os.remove(regFile); 
  MLAB.log("Registration file :" + normalPath + " has been deleted. ");
  if os.listdir(regDir) == []:
    os.rmdir(regDir);
    
## delete the patient and its images and registrations, CSOs
def deletePatientInFile(patientID):
  regDir = "C:/MLab2.0UserData/" + patientID;
  if not os.path.exists(regDir):
    return;
  shutil.rmtree(regDir);

//# MeVis signature v1
//# key: MFowDQYJKoZIhvcNAQEBBQADSQAwRgJBAL7SwKNHkHBgB9qUD4diwRQbzCvcmo5tOThuQpDaNbbnER+0rr4pvcNBFPryRY4cWd6PoeyJo5hbomE3XIM5LbsCARE=:aBiq8DiLE2yV5zrB7kV6AaR9N2l9ufzF1dUU9eeV6gX/Bo6rwms1p+87SmalYNHOF2kWEFnNO9FJ5n4BJUVPQg==
//# owner: STU
//# date: 2009-08-14T12:07:35
//# hash: H5Xij0IF2lS4oAEnngWIMNeXG/YQAEF03TxP6jRRIKsILF1ENTMYiM931JDcf0LQXGo++qBPKIG5bWPOXVBYxQ==
//# MeVis end
