def movefiles(rc):  
    '''
    Change CMIP5 directory struture to obs4MIPS directory structure.
    Create new path and rename files following with new convention 
    Remove old directory tree.
    '''
    # ----------------------
    # New path for obs4MIPS
    # ----------------------
    path = rc['institute_id'      ]    + '/' + \
           rc['instrument'        ]    + '/' + \
           rc['project_id'        ]    + '/' + \
           rc['product'           ]    + '/' + \
           rc['modeling_realm'    ]    + '/' + \
           rc['cvrt_cmor_var'     ]    + '/' + \
           rc['frequency'         ]    + '/' + \
           rc['data_structure'    ]    + '/' + \
           rc['institute_id'      ]    + '/' + \
           rc['instrument'        ]    
 
   # -------------------------------------------------------------
   # For TRMM data set version_directory is set to 'false' so that
   # no version will be created in the directory
   # -------------------------------------------------------------
    try:
           rc['version_directory'] == 'false'
           pass
    except:
           path +=  '/V' + rc['processing_version'] + '/'

   # -----------------------------
   # Make sure the directory exist 
   # ------------------------------
    try:
        os.makedirs(path)
    except:
        None

    # -----------------
    # Rename all files
    # -----------------
    cmorpath = rc['project_id'  ]       + '/' + \
               rc['product'     ]       + '/' + \
               rc['institute_id']       + '/' 

    # -----------------
    # Manage attributes
    # -----------------
    for r,d,f in os.walk(cmorpath):
        for files in f:
            if files.endswith(".nc"):
                filetimestamp = files.split('_')[-1].strip(".nc")
                file = os.path.join(r,files)
                print file
                # -----------------
                # Delete attributes
                # ------------------
                Attr=CMORAttributes( file )
                DelGlbAttributes=eval(rc['DelGlbAttributes'].\
                                      replace('\\','\''))
                for attribute in DelGlbAttributes:
                    print "Deleting attribute: %s" % attribute
                    Attr.GlbDel(attribute)
                # -----------------
                # set attributes
                # ------------------
                SetGlbAttributes=eval(rc['SetGlbAttributes'].\
                                      replace('\\','\''))
                for (attribute,Value) in SetGlbAttributes:
                    print "Assigning attribute (%s,%s)" % (attribute,Value)
                    Attr.GlbSet(attribute,Value)
                Attr.close()

                source = ''
                if( rc['source_fn'] == 'SYNOPTIC' ):
                    source = rc['SYNOPTIC']+"z_"
                elif( rc['source_fn'] != '' ):
                    source = rc['source_fn'].split('_')[0] + '_'
                
                newfilename = rc['cvrt_cmor_var'     ] + '_' + \
                              rc['instrument'        ] + '-' + \
                              source                         + \
                              rc['processing_level'  ] + '_v'+ \
                              rc['processing_version'] + '_' + \
                              filetimestamp            + '.nc'
                
                newfilename = os.path.join(path,newfilename)
                # -----------
                # Move files
                # -----------
                print file
                print newfilename
                os.rename(file,newfilename)

                    
    # ----------------
    # Remove cmor path
    # ----------------
    shutil.rmtree( rc['project_id'] )
    return 0
dataset/ETHPy150Open apache/climate/obs4MIPs/Toolbox/ESGFresources.py/movefiles/original