from os import listdir, makedirs
from os.path import isfile, join, splitext, exists
from nansat import Nansat
import shutil

path = '/WebData/maires.nersc.no/public_html/thredds/norwegian500m_asar_wind/'

fileNames = [f for f in listdir(path) if isfile(join(path, f))]

for iFileName in fileNames:
    fileNameStr, fileExt = splitext(iFileName)
    if fileExt == '.nc':
        n = Nansat(path + iFileName)
        projStr = n.vrt.dataset.GetProjection().split('PARAMETER')
        del n
        lat = int(float(projStr[1].split(',')[1].replace(']','')))
        lon = int(float(projStr[2].split(',')[1].replace(']','')))
        if lon < 0 :
            oPath = path + ('E%03dN%2d/' %(abs(lon), lat))
        else:
            oPath = path + ('W%03dN%2d/' %(lon, lat))
        if not exists(oPath):
            makedirs(oPath)

        shutil.move(path + iFileName, oPath + iFileName)




