Esempio n. 1
0
    def removeDuplangles(self):
        ibar = 0
        pbar = ProgressBar(maxval=self.npoin).start()
        ip = 0
        while ip < len(self.poly):
            ibar += len(self.poly[ip])
            lb = len(self.poly[ip])
            self.poly[ip], self.type[ip] = removeDuplangles(
                self.poly[ip], self.type[ip])
            la = len(self.poly[ip])
            if la < lb:
                pbar.write(
                    '    +> removed ' + str(lb - la) + ' points of ' +
                    str(lb) + ' from polygon ' + str(ip + 1), ibar)
            if self.poly[ip] == []:
                pbar.write('    +> removed entire polygon ' + str(ip + 1),
                           ibar)
                self.poly.pop(ip)
                self.type.pop(ip)
            else:
                ip += 1
            pbar.update(ibar)

        pbar.finish()
        return self.poly, self.type
Esempio n. 2
0
    def smoothSubsampleAngle(self, angle):
        ibar = 0
        pbar = ProgressBar(maxval=self.npoin).start()
        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~/^\~~~~~~~~
        #                            subsampling by anlgle \_/
        ip = 0
        while ip < len(self.poly):
            ibar += len(self.poly[ip])
            lb = len(self.poly[ip])
            self.poly[ip], self.type[ip] = subsampleAngle(
                self.poly[ip], self.type[ip], angle)
            la = len(self.poly[ip])
            if la < lb:
                pbar.write(
                    '    +> removed ' + str(lb - la) + ' points of ' +
                    str(lb) + ' from polygon ' + str(ip + 1), ibar)
            if self.poly[ip] == []:
                pbar.write('    +> removed entire polygon ' + str(ip + 1),
                           ibar)
                self.poly.pop(ip)
                self.type.pop(ip)
            else:
                ip += 1
            #pbar.update(ibar)

        pbar.finish()
        return self.poly, self.type
Esempio n. 3
0
    def big2littlePTS(self, fileName, foleName):

        # ~~ Openning files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        fle = open(fileName, 'rb')
        fole = open(foleName, 'wb')
        print '           +> writing the volumes-file: ', foleName

        # ~~ Read/Write dimensions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        n1 = self.NPOIN3 + 1
        minvol = self.minvol * np.ones(self.NPOIN3, dtype=np.float32)

        # ~~ Read volumes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        pbar = ProgressBar(maxval=self.HYDROIT).start()
        for i in range(self.HYDROIT):
            l, it = unpack('>ii', fle.read(4 + 4))
            VOLUME = np.asarray(
                unpack('>' + str(self.NPOIN3) + 'f',
                       fle.read(4 * self.NPOIN3)))
            VOLUME = np.maximum(VOLUME, minvol)
            fle.seek(4, 1)
            if it >= self.tfrom and it <= self.tstop:
                pbar.write('            ~> read iteration: ' + str(it), i)
                fole.write(pack('<ii', 4 * n1, it - self.HYDRO00))
                fole.write(pack('<' + str(self.NPOIN3) + 'f', *(VOLUME)))
                fole.write(pack('<i', 4 * n1))
            else:
                pbar.write('            ~> ignore iteration: ' + str(it), i)
            pbar.update(i)
        pbar.finish()

        fle.close()
        fole.close()
Esempio n. 4
0
    def big2littleARE(self, fileName, foleName):

        # ~~ Openning files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        fle = open(fileName, 'rb')
        fole = open(foleName, 'wb')
        print '           +> writing the areas-file: ', foleName

        # ~~ Read/Write dimensions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        NSEG2 = (3 * self.geo.NELEM3 + self.conlim.NPTFR) / 2
        MBND2 = np.count_nonzero(self.conlim.BOR['lih'] != 2)
        n3 = (NSEG2 + MBND2)
        #n4 = 2*( self.geo.NPLAN-1 )*self.geo.NPOIN3

        # ~~ Read volumes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        pbar = ProgressBar(maxval=self.HYDROIT).start()
        for i in range(self.HYDROIT):
            l, it = unpack('>ii', fle.read(4 + 4))
            AREAS = np.asarray(unpack('>' + str(n3) + 'f', fle.read(4 * n3)))
            fle.seek(4, 1)
            if it >= self.tfrom and it <= self.tstop:
                pbar.write('            ~> read iteration: ' + str(it), i)
                fole.write(pack('<ii', 4 * n3, it - self.HYDRO00))
                fole.write(pack('<' + str(n3) + 'f', *(AREAS)))
                fole.write(pack('<i', 4 * n3))
            else:
                pbar.write('            ~> ignore iteration: ' + str(it), i)
            pbar.update(i)
        pbar.finish()

        fle.close()
        fole.close()
    def big2little_are(self, file_name, fole_name):

        # ~~ Openning files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        fle = open(file_name, 'rb')
        fole = open(fole_name, 'wb')
        print('           +> writing the areas-file: ' + fole_name)

        # ~~ Read/Write dimensions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        nseg2 = (3 * self.geo.nelem3 + self.conlim.nptfr) / 2
        mbnd2 = np.count_nonzero(self.conlim.bor['lih'] != 2)
        n_3 = (nseg2 + mbnd2)

        # ~~ Read volumes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        pbar = ProgressBar(maxval=self.hydroit).start()
        for i in range(self.hydroit):
            _, itime = unpack('>ii', fle.read(4 + 4))
            areas = np.asarray(unpack('>' + str(n_3) + 'f', fle.read(4 * n_3)))
            fle.seek(4, 1)
            if itime >= self.tfrom and itime <= self.tstop:
                pbar.write('            ~> read iteration: ' + str(itime), i)
                fole.write(pack('<ii', 4 * n_3, itime - self.hydro00))
                fole.write(pack('<' + str(n_3) + 'f', *(areas)))
                fole.write(pack('<i', 4 * n_3))
            else:
                pbar.write('            ~> ignore iteration: ' + str(itime), i)
            pbar.update(i)
        pbar.finish()

        fle.close()
        fole.close()
    def big2little_pts(self, file_name, fole_name):

        # ~~ Openning files ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        fle = open(file_name, 'rb')
        fole = open(fole_name, 'wb')
        print('           +> writing the volumes-file: ' + fole_name)

        # ~~ Read/Write dimensions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        n_1 = self.npoin3 + 1
        minvol = self.minvol * np.ones(self.npoin3, dtype=np.float32)

        # ~~ Read volumes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        pbar = ProgressBar(maxval=self.hydroit).start()
        for i in range(self.hydroit):
            _, itime = unpack('>ii', fle.read(4 + 4))
            volume = np.asarray(
                unpack('>' + str(self.npoin3) + 'f',
                       fle.read(4 * self.npoin3)))
            volume = np.maximum(volume, minvol)
            fle.seek(4, 1)
            if itime >= self.tfrom and itime <= self.tstop:
                pbar.write('            ~> read iteration: ' + str(itime), i)
                fole.write(pack('<ii', 4 * n_1, itime - self.hydro00))
                fole.write(pack('<' + str(self.npoin3) + 'f', *(volume)))
                fole.write(pack('<i', 4 * n_1))
            else:
                pbar.write('            ~> ignore iteration: ' + str(itime), i)
            pbar.update(i)
        pbar.finish()

        fle.close()
        fole.close()
Esempio n. 7
0
 def makeClockwise(self):
     ibar = 0
     pbar = ProgressBar(maxval=self.npoin).start()
     for ip in range(len(self.poly)):
         ibar += len(self.poly[ip])
         if self.type[ip] != 0:
             if not isClockwise(self.poly[ip]):
                 pbar.write(
                     '    +> turned clockwise polygon ' + str(ip + 1), ibar)
                 self.poly[ip] = np.flipud(self.poly[ip])
         pbar.update(ibar)
     pbar.finish()
     return self.poly
Esempio n. 8
0
 def smoothSubdivise(self, weight=0.5):
     ibar = 0
     pbar = ProgressBar(maxval=self.npoin).start()
     for ip in range(len(self.poly)):
         ibar += len(self.poly[ip])
         lb = len(self.poly[ip])
         self.poly[ip], self.type[ip] = smoothSubdivise(
             self.poly[ip], self.type[ip], weight)
         la = len(self.poly[ip])
         if la > lb:
             pbar.write(
                 '    +> added ' + str(la - lb) + ' points to polygon ' +
                 str(ip + 1), ibar)
         pbar.update(ibar)
     pbar.finish()
     return self.poly, self.type
Esempio n. 9
0
    def putContent(self, rootName, only2D):

        nbar = 0
        for e in self.experiments:
            nbar += len(e[2])
        ilat = [self.hycomilat[0], self.hycomilat[-1] + 1]
        ilon = [self.hycomilon[0], self.hycomilon[-1] + 1]

        # ~~~~ Time records ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        print '     +> Extract HYCOM time records'
        self.slf3d.tags = {'times': []}
        # ~~~~ Start Date and Time ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.slf3d.tags['times'] = 86400.0 * np.arange(nbar)
        self.slf2d.tags = {'times': self.slf3d.tags['times']}
        self.slf3d.DATETIME = self.experiments[-1][3][0].timetuple()[0:6]
        self.slf2d.DATETIME = self.slf3d.DATETIME
        self.slf3d.IPARAM[9] = 1
        self.slf2d.IPARAM[9] = 1

        #a = np.arange(40).reshape(5,8)[::-1].ravel()  # 5 plans, 8 points
        print '     +> Write SELAFIN headers'
        if not only2D:
            self.slf3d.fole = {}
            self.slf3d.fole.update({'hook': open('t3d_' + rootName, 'wb')})
            self.slf3d.fole.update({'name': 't3d_' + rootName})
            self.slf3d.fole.update({'endian': ">"})  # big endian
            self.slf3d.fole.update({'float': ('f', 4)})  # single precision
            self.slf3d.appendHeaderSLF()
        self.slf2d.fole = {}
        self.slf2d.fole.update({'hook': open('t2d_' + rootName, 'wb')})
        self.slf2d.fole.update({'name': 't2d_' + rootName})
        self.slf2d.fole.update({'endian': ">"})  # big endian
        self.slf2d.fole.update({'float': ('f', 4)})  # single precision
        self.slf2d.appendHeaderSLF()
        # ~~~~ Time loop(s) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        var3d = np.zeros(self.slf3d.NPOIN3, dtype=np.float)
        var2d = np.zeros(self.slf2d.NPOIN3, dtype=np.float)
        print '     +> Write SELAFIN cores'
        ibar = 0
        pbar = ProgressBar(maxval=10 * nbar).start()
        for e in self.experiments[::-1]:
            hycomdata = e[0]
            i1 = min(e[2])
            i2 = max(e[2]) + 1
            for t in range(i1, i2):
                # ~~> time stamp
                pbar.write('        x ' + str(e[3][t - i1]), 10 * ibar + 0)
                pbar.update(10 * ibar + 0)
                if not only2D: self.slf3d.appendCoreTimeSLF(ibar)
                self.slf2d.appendCoreTimeSLF(ibar)

                # ~~> HYCOM variable extraction ( 1L:times, 33L:layers, yyL:NY1D, xxL:NX1D )

                # ~~> ELEVATION
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - ssh', 10 * ibar + 1)
                        v2d = np.swapaxes(
                            hycomdata['ssh']['ssh'].data[t, ilat[0]:ilat[1],
                                                         ilon[0]:ilon[1]][0],
                            0, 1).ravel()
                    except:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.appendCoreVarsSLF([var2d])
                if not only2D:
                    var3d = -np.tile(self.ZPLAN, self.slf3d.NPOIN2).reshape(
                        self.slf3d.NPOIN2, self.slf3d.NPLAN).T.ravel()
                    var3d[self.slf3d.NPOIN3 - self.slf3d.NPOIN2:] = var2d
                    self.slf3d.appendCoreVarsSLF([var3d])
                pbar.update(10 * ibar + 1)

                # ~~> SALINITY
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - surface_salinity_trend',
                                   10 * ibar + 2)
                        v2d = np.swapaxes(
                            hycomdata['surface_salinity_trend']
                            ['surface_salinity_trend'].data[
                                t, ilat[0]:ilat[1], ilon[0]:ilon[1]][0], 0,
                            1).ravel()
                    except:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.appendCoreVarsSLF([var2d])
                pbar.update(10 * ibar + 2)
                if not only2D:
                    success = False
                    while not success:
                        try:
                            success = True
                            pbar.write('             - salinity',
                                       10 * ibar + 3)
                            var = np.swapaxes(
                                hycomdata['salinity']['salinity'].data[
                                    t, 0:self.slf3d.NPLAN, ilat[0]:ilat[1],
                                    ilon[0]:ilon[1]][0], 1, 2)
                        except:
                            success = False
                            pbar.write(
                                '    ... re-attempting because I failed ...',
                                10 * ibar)
                    v3d = var[::-1].ravel()
                    var3d = np.where(v3d < 10000, v3d, 0.0)
                    self.slf3d.appendCoreVarsSLF([var3d])
                pbar.update(10 * ibar + 3)

                # ~~> TEMPERATURE
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - surface_temperature_trend',
                                   10 * ibar + 4)
                        v2d = np.swapaxes(
                            hycomdata['surface_temperature_trend']
                            ['surface_temperature_trend'].data[
                                t, ilat[0]:ilat[1], ilon[0]:ilon[1]][0], 0,
                            1).ravel()
                    except:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.appendCoreVarsSLF([var2d])
                pbar.update(10 * ibar + 4)
                if not only2D:
                    success = False
                    while not success:
                        try:
                            success = True
                            pbar.write('             - temperature',
                                       10 * ibar + 5)
                            var = np.swapaxes(
                                hycomdata['temperature']['temperature'].data[
                                    t, 0:self.slf3d.NPLAN, ilat[0]:ilat[1],
                                    ilon[0]:ilon[1]][0], 1, 2)
                        except:
                            success = False
                            pbar.write(
                                '    ... re-attempting because I failed ...',
                                10 * ibar)
                    v3d = var[::-1].ravel()
                    var3d = np.where(v3d < 10000, v3d, 0.0)
                    self.slf3d.appendCoreVarsSLF([var3d])
                pbar.update(10 * ibar + 5)

                # ~~> VELOCITY U
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - u-velocity', 10 * ibar + 6)
                        if only2D:
                            var = np.swapaxes(
                                hycomdata['u']['u'].data[t, 0:1,
                                                         ilat[0]:ilat[1],
                                                         ilon[0]:ilon[1]][0],
                                1, 2)
                        else:
                            var = np.swapaxes(
                                hycomdata['u']['u'].data[t, 0:self.slf3d.NPLAN,
                                                         ilat[0]:ilat[1],
                                                         ilon[0]:ilon[1]][0],
                                1, 2)
                    except:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                v2d = var[0].ravel()
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.appendCoreVarsSLF([var2d])
                if not only2D:
                    v3d = var[::-1].ravel()
                    var3d = np.where(v3d < 10000, v3d, 0.0)
                    self.slf3d.appendCoreVarsSLF([var3d])
                pbar.update(10 * ibar + 6)

                # ~~> VELOCITY V
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - v-velocity', 10 * ibar + 7)
                        if only2D:
                            var = np.swapaxes(
                                hycomdata['v']['v'].data[t, 0:1,
                                                         ilat[0]:ilat[1],
                                                         ilon[0]:ilon[1]][0],
                                1, 2)
                        else:
                            var = np.swapaxes(
                                hycomdata['v']['v'].data[t, 0:self.slf3d.NPLAN,
                                                         ilat[0]:ilat[1],
                                                         ilon[0]:ilon[1]][0],
                                1, 2)
                    except:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                v2d = var[0].ravel()
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.appendCoreVarsSLF([var2d])
                if not only2D:
                    v3d = var[::-1].ravel()
                    var3d = np.where(v3d < 10000, v3d, 0.0)
                    self.slf3d.appendCoreVarsSLF([var3d])
                pbar.update(10 * ibar + 7)

                # ~~> VELOCITY W
                if not only2D:
                    var3d = 0. * var3d
                    self.slf3d.appendCoreVarsSLF([var3d])

                # ~~> EMP ???
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - emp', 10 * ibar + 8)
                        v2d = np.swapaxes(
                            hycomdata['emp']['emp'].data[t, ilat[0]:ilat[1],
                                                         ilon[0]:ilon[1]][0],
                            0, 1).ravel()
                    except:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.appendCoreVarsSLF([var2d])
                pbar.update(10 * ibar + 8)

                # ~~> TEMPERATURE
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - qtot', 10 * ibar + 9)
                        v2d = np.swapaxes(
                            hycomdata['qtot']['qtot'].data[t, ilat[0]:ilat[1],
                                                           ilon[0]:ilon[1]][0],
                            0, 1).ravel()
                    except:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.appendCoreVarsSLF([var2d])
                pbar.update(10 * ibar + 9)

                ibar += 1

        pbar.finish()
        if not only2D: self.slf3d.fole['hook'].close()
        self.slf2d.fole['hook'].close()
Esempio n. 10
0
    def putContent(self, rootName, only2D):

        nbar = 0
        for e in self.experiments:
            nbar += len(e[2])
        ilat = [self.jcope2ilat[0], self.jcope2ilat[-1] + 1]
        ilon = [self.jcope2ilon[0], self.jcope2ilon[-1] + 1]

        # ~~~~ Time records ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        print '     +> Extract JCOPE2 time records'
        self.slf3d.tags = {'times': []}
        # ~~~~ Start Date and Time ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.slf3d.tags['times'] = 86400.0 * np.arange(nbar)
        self.slf2d.tags = {'times': self.slf3d.tags['times']}
        self.slf3d.DATETIME = self.experiments[-1][3][0].timetuple()[0:6]
        self.slf2d.DATETIME = self.slf3d.DATETIME
        self.slf3d.IPARAM[9] = 1
        self.slf2d.IPARAM[9] = 1

        #a = np.arange(40).reshape(5,8)[::-1].ravel()  # 5 plans, 8 points
        print '     +> Write SELAFIN headers'
        if not only2D:
            self.slf3d.fole = {}
            self.slf3d.fole.update({'hook': open('t3d_' + rootName, 'wb')})
            self.slf3d.fole.update({'name': 't3d_' + rootName})
            self.slf3d.fole.update({'endian': ">"})  # big endian
            self.slf3d.fole.update({'float': ('f', 4)})  # single precision
            self.slf3d.appendHeaderSLF()
        self.slf2d.fole = {}
        self.slf2d.fole.update({'hook': open('t2d_' + rootName, 'wb')})
        self.slf2d.fole.update({'name': 't2d_' + rootName})
        self.slf2d.fole.update({'endian': ">"})  # big endian
        self.slf2d.fole.update({'float': ('f', 4)})  # single precision
        self.slf2d.appendHeaderSLF()
        # ~~~~ Time loop(s) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        #var3d = np.zeros(self.slf3d.NPOIN3,dtype=np.float)
        #var2d = np.zeros(self.slf2d.NPOIN3,dtype=np.float)
        print '     +> Write SELAFIN cores'
        ibar = 0
        pbar = ProgressBar(maxval=6 * nbar).start()
        for e in self.experiments:
            jcope2data = e[0]
            i1 = min(e[2])
            i2 = max(e[2]) + 1
            for t in range(i1, i2):
                # ~~> time stamp
                pbar.write('        x ' + str(e[3][t - i1]), 6 * ibar + 0)
                pbar.update(6 * ibar + 0)
                if not only2D: self.slf3d.appendCoreTimeSLF(ibar)
                self.slf2d.appendCoreTimeSLF(ibar)

                # ~~> ELEVATION
                var2d = np.swapaxes(
                    jcope2data['el']['el'].data[t, 0, ilat[0]:ilat[1],
                                                ilon[0]:ilon[1]][0], 1,
                    2).ravel()[self.MASK2]
                self.slf2d.appendCoreVarsSLF([var2d])
                if not only2D:
                    var3d = -np.tile(self.ZPLAN, self.slf3d.NPOIN2).reshape(
                        self.slf3d.NPOIN2, self.slf3d.NPLAN).T.ravel()
                    var3d[self.slf3d.NPOIN3 - self.slf3d.NPOIN2:] = var2d
                    self.slf3d.appendCoreVarsSLF([var3d])
                pbar.write('             - elevation', 6 * ibar + 1)
                pbar.update(6 * ibar + 1)

                # ~~> SALINITY
                if only2D:
                    var = np.swapaxes(
                        jcope2data['salt']['salt'].data[t, 0:1,
                                                        ilat[0]:ilat[1],
                                                        ilon[0]:ilon[1]][0], 1,
                        2)
                else:
                    var = np.swapaxes(
                        jcope2data['salt']['salt'].data[t, 0:self.slf3d.NPLAN,
                                                        ilat[0]:ilat[1],
                                                        ilon[0]:ilon[1]][0], 1,
                        2)
                var2d = var[0].ravel()[self.MASK2]
                self.slf2d.appendCoreVarsSLF([var2d])
                if not only2D:
                    var3d = var[::-1].ravel()[self.MASK3]
                    for ipoin in range(self.slf3d.NPOIN2):
                        for iplan in range(self.slf3d.NPLAN - 1, 0, -1):
                            if var3d[ipoin +
                                     (iplan - 1) * self.slf3d.NPOIN2] < -99.0:
                                var3d[ipoin +
                                      (iplan - 1) * self.slf3d.NPOIN2] = var3d[
                                          ipoin + iplan * self.slf3d.NPOIN2]
                    self.slf3d.appendCoreVarsSLF([var3d])
                    pbar.write('             - salinity', 6 * ibar + 2)
                pbar.update(6 * ibar + 2)

                # ~~> TEMPERATURE
                if only2D:
                    var = np.swapaxes(
                        jcope2data['temp']['temp'].data[t, 0:1,
                                                        ilat[0]:ilat[1],
                                                        ilon[0]:ilon[1]][0], 1,
                        2)
                else:
                    var = np.swapaxes(
                        jcope2data['temp']['temp'].data[t, 0:self.slf3d.NPLAN,
                                                        ilat[0]:ilat[1],
                                                        ilon[0]:ilon[1]][0], 1,
                        2)
                var2d = var[0].ravel()[self.MASK2]
                self.slf2d.appendCoreVarsSLF([var2d])
                if not only2D:
                    var3d = var[::-1].ravel()[self.MASK3]
                    for ipoin in range(self.slf3d.NPOIN2):
                        for iplan in range(self.slf3d.NPLAN - 1, 0, -1):
                            if var3d[ipoin +
                                     (iplan - 1) * self.slf3d.NPOIN2] < -99.0:
                                var3d[ipoin +
                                      (iplan - 1) * self.slf3d.NPOIN2] = var3d[
                                          ipoin + iplan * self.slf3d.NPOIN2]
                    self.slf3d.appendCoreVarsSLF([var3d])
                    pbar.write('             - temperature', 6 * ibar + 3)
                pbar.update(6 * ibar + 3)

                # ~~> VELOCITY U
                if only2D:
                    var = np.swapaxes(
                        jcope2data['u']['u'].data[t, 0:1, ilat[0]:ilat[1],
                                                  ilon[0]:ilon[1]][0], 1, 2)
                else:
                    var = np.swapaxes(
                        jcope2data['u']['u'].data[t, 0:self.slf3d.NPLAN,
                                                  ilat[0]:ilat[1],
                                                  ilon[0]:ilon[1]][0], 1, 2)
                var2d = var[0].ravel()[self.MASK2]
                self.slf2d.appendCoreVarsSLF([var2d])
                if not only2D:
                    var3d = var[::-1].ravel()[self.MASK3]
                    for ipoin in range(self.slf3d.NPOIN2):
                        for iplan in range(self.slf3d.NPLAN - 1, 0, -1):
                            if var3d[ipoin +
                                     (iplan - 1) * self.slf3d.NPOIN2] < -99.0:
                                var3d[ipoin +
                                      (iplan - 1) * self.slf3d.NPOIN2] = var3d[
                                          ipoin + iplan * self.slf3d.NPOIN2]
                    self.slf3d.appendCoreVarsSLF([var3d])
                pbar.write('             - u-velocity', 6 * ibar + 4)
                pbar.update(6 * ibar + 4)

                # ~~> VELOCITY V
                if only2D:
                    var = np.swapaxes(
                        jcope2data['v']['v'].data[t, 0:1, ilat[0]:ilat[1],
                                                  ilon[0]:ilon[1]][0], 1, 2)
                else:
                    var = np.swapaxes(
                        jcope2data['v']['v'].data[t, 0:self.slf3d.NPLAN,
                                                  ilat[0]:ilat[1],
                                                  ilon[0]:ilon[1]][0], 1, 2)
                var2d = var[0].ravel()[self.MASK2]
                self.slf2d.appendCoreVarsSLF([var2d])
                if not only2D:
                    var3d = var[::-1].ravel()[self.MASK3]
                    for ipoin in range(self.slf3d.NPOIN2):
                        for iplan in range(self.slf3d.NPLAN - 1, 0, -1):
                            if var3d[ipoin +
                                     (iplan - 1) * self.slf3d.NPOIN2] < -99.0:
                                var3d[ipoin +
                                      (iplan - 1) * self.slf3d.NPOIN2] = var3d[
                                          ipoin + iplan * self.slf3d.NPOIN2]
                    self.slf3d.appendCoreVarsSLF([var3d])
                pbar.write('             - v-velocity', 6 * ibar + 5)
                pbar.update(6 * ibar + 5)

                # ~~> VELOCITY W
                if not only2D:
                    var3d = 0. * var3d
                    self.slf3d.appendCoreVarsSLF([var3d])

                ibar += 1

        pbar.finish()
        if not only2D: self.slf3d.fole['hook'].close()
        self.slf2d.fole['hook'].close()
    def put_content(self, root_name, only_2d):

        nbar = 0
        for exp in self.experiments:
            nbar += len(exp[2])
        ilat = [self.hycomilat[0], self.hycomilat[-1] + 1]
        ilon = [self.hycomilon[0], self.hycomilon[-1] + 1]

        # ~~~~ Time records ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        print('     +> Extract HYCOM time records')
        self.slf3d.tags = {'times': []}
        # ~~~~ Start Date and Time ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.slf3d.tags['times'] = 86400.0 * np.arange(nbar)
        self.slf2d.tags = {'times': self.slf3d.tags['times']}
        self.slf3d.datetime = self.experiments[-1][3][0].timetuple()[0:6]
        self.slf2d.datetime = self.slf3d.datetime
        self.slf3d.iparam[9] = 1
        self.slf2d.iparam[9] = 1

        print('     +> Write Selafin headers')
        if not only_2d:
            self.slf3d.fole = {}
            self.slf3d.fole.update({'hook': open('t3d_' + root_name, 'wb')})
            self.slf3d.fole.update({'name': 't3d_' + root_name})
            self.slf3d.fole.update({'endian': ">"})  # big endian
            self.slf3d.fole.update({'float': ('f', 4)})  # single precision
            self.slf3d.append_header_slf()
        self.slf2d.fole = {}
        self.slf2d.fole.update({'hook': open('t2d_' + root_name, 'wb')})
        self.slf2d.fole.update({'name': 't2d_' + root_name})
        self.slf2d.fole.update({'endian': ">"})  # big endian
        self.slf2d.fole.update({'float': ('f', 4)})  # single precision
        self.slf2d.append_header_slf()
        # ~~~~ Time loop(s) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        var3d = np.zeros(self.slf3d.npoin3, dtype=np.float)
        var2d = np.zeros(self.slf2d.npoin3, dtype=np.float)
        print('     +> Write Selafin cores')
        ibar = 0
        pbar = ProgressBar(maxval=10 * nbar).start()
        for exp in self.experiments[::-1]:
            hycomdata = exp[0]
            i_1 = min(exp[2])
            i_2 = max(exp[2]) + 1
            for itime in range(i_1, i_2):
                # ~~> time stamp
                pbar.write('        x ' + str(exp[3][itime - i_1]),
                           10 * ibar + 0)
                pbar.update(10 * ibar + 0)
                if not only_2d:
                    self.slf3d.append_core_time_slf(ibar)
                self.slf2d.append_core_time_slf(ibar)

                # ~~> HYCOM variable extraction
                #     ( 1L:times, 33L:layers, yyL:ny1d, xxL:nx1d )

                # ~~> ELEVATION
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - ssh', 10 * ibar + 1)
                        data = hycomdata['ssh']['ssh']
                        v2d = np.swapaxes(
                            data.data[itime, ilat[0]:ilat[1],
                                      ilon[0]:ilon[1]][0], 0, 1).ravel()
                    except Exception:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.append_core_vars_slf([var2d])
                if not only_2d:
                    var3d = - np.tile(self.zplan, self.slf3d.npoin2)\
                                .reshape(self.slf3d.npoin2, self.slf3d.nplan)\
                                .itime.ravel()
                    var3d[self.slf3d.npoin3 - self.slf3d.npoin2:] = var2d
                    self.slf3d.append_core_vars_slf([var3d])
                pbar.update(10 * ibar + 1)

                # ~~> SALINITY
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - surface_salinity_trend',
                                   10 * ibar + 2)
                        data = hycomdata['surface_salinity_trend']\
                                             ['surface_salinity_trend']
                        v2d = np.swapaxes(\
                                  data.data[itime, ilat[0]:ilat[1],
                                            ilon[0]:ilon[1]][0],
                                  0, 1).ravel()
                    except Exception:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.append_core_vars_slf([var2d])
                pbar.update(10 * ibar + 2)
                if not only_2d:
                    success = False
                    while not success:
                        try:
                            success = True
                            pbar.write('             - salinity',
                                       10 * ibar + 3)
                            data = hycomdata['salinity']['salinity']
                            var = np.swapaxes(\
                                    data.data[itime, 0:self.slf3d.nplan,
                                              ilat[0]:ilat[1],
                                              ilon[0]:ilon[1]][0],
                                    1, 2)
                        except Exception:
                            success = False
                            pbar.write(\
                                  '    ... re-attempting because I failed ...',
                                  10*ibar)
                    v3d = var[::-1].ravel()
                    var3d = np.where(v3d < 10000, v3d, 0.0)
                    self.slf3d.append_core_vars_slf([var3d])
                pbar.update(10 * ibar + 3)

                # ~~> TEMPERATURE
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - surface_temperature_trend',
                                   10 * ibar + 4)
                        data = hycomdata['surface_temperature_trend']\
                                        ['surface_temperature_trend']
                        v2d = np.swapaxes(\
                                  data.data[itime, ilat[0]:ilat[1],
                                            ilon[0]:ilon[1]][0],
                                  0, 1).ravel()
                    except Exception:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.append_core_vars_slf([var2d])
                pbar.update(10 * ibar + 4)
                if not only_2d:
                    success = False
                    while not success:
                        try:
                            success = True
                            pbar.write('             - temperature',
                                       10 * ibar + 5)
                            data = hycomdata['temperature']['temperature']
                            var = np.swapaxes(\
                                      data.data[itime, 0:self.slf3d.nplan,
                                                ilat[0]:ilat[1],
                                                ilon[0]:ilon[1]][0],
                                      1, 2)
                        except Exception:
                            success = False
                            pbar.write(\
                                 '    ... re-attempting because I failed ...',
                                 10*ibar)
                    v3d = var[::-1].ravel()
                    var3d = np.where(v3d < 10000, v3d, 0.0)
                    self.slf3d.append_core_vars_slf([var3d])
                pbar.update(10 * ibar + 5)

                # ~~> VELOCITY U
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - u-velocity', 10 * ibar + 6)
                        data = hycomdata['u']['u']
                        if only_2d:
                            var = np.swapaxes(
                                data.data[itime, 0:1, ilat[0]:ilat[1],
                                          ilon[0]:ilon[1]][0], 1, 2)
                        else:
                            var = np.swapaxes(
                                data.data[itime, 0:self.slf3d.nplan,
                                          ilat[0]:ilat[1], ilon[0]:ilon[1]][0],
                                1, 2)
                    except Exception:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                v2d = var[0].ravel()
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.append_core_vars_slf([var2d])
                if not only_2d:
                    v3d = var[::-1].ravel()
                    var3d = np.where(v3d < 10000, v3d, 0.0)
                    self.slf3d.append_core_vars_slf([var3d])
                pbar.update(10 * ibar + 6)

                # ~~> VELOCITY V
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - v-velocity', 10 * ibar + 7)
                        data = hycomdata['v']['v']
                        if only_2d:
                            var = np.swapaxes(
                                data.data[itime, 0:1, ilat[0]:ilat[1],
                                          ilon[0]:ilon[1]][0], 1, 2)
                        else:
                            var = np.swapaxes(
                                data.data[itime, 0:self.slf3d.nplan,
                                          ilat[0]:ilat[1], ilon[0]:ilon[1]][0],
                                1, 2)
                    except Exception:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                v2d = var[0].ravel()
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.append_core_vars_slf([var2d])
                if not only_2d:
                    v3d = var[::-1].ravel()
                    var3d = np.where(v3d < 10000, v3d, 0.0)
                    self.slf3d.append_core_vars_slf([var3d])
                pbar.update(10 * ibar + 7)

                # ~~> VELOCITY W
                if not only_2d:
                    var3d = 0. * var3d
                    self.slf3d.append_core_vars_slf([var3d])

                # ~~> EMP ???
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - emp', 10 * ibar + 8)
                        data = hycomdata['emp']['emp']
                        v2d = np.swapaxes(
                            data.data[itime, ilat[0]:ilat[1],
                                      ilon[0]:ilon[1]][0], 0, 1).ravel()
                    except Exception:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.append_core_vars_slf([var2d])
                pbar.update(10 * ibar + 8)

                # ~~> TEMPERATURE
                success = False
                while not success:
                    try:
                        success = True
                        pbar.write('             - qtot', 10 * ibar + 9)
                        data = hycomdata['qtot']['qtot']
                        v2d = np.swapaxes(
                            data.data[itime, ilat[0]:ilat[1],
                                      ilon[0]:ilon[1]][0], 0, 1).ravel()
                    except Exception:
                        success = False
                        pbar.write(
                            '    ... re-attempting because I failed ...',
                            10 * ibar)
                var2d = np.where(v2d < 10000, v2d, 0.0)
                self.slf2d.append_core_vars_slf([var2d])
                pbar.update(10 * ibar + 9)

                ibar += 1

        pbar.finish()
        if not only_2d:
            self.slf3d.fole['hook'].close()
        self.slf2d.fole['hook'].close()
    def put_content(self, root_name, only_2d):

        nbar = 0
        for exp in self.experiments:
            nbar += len(exp[2])
        ilat = [self.jcope2ilat[0], self.jcope2ilat[-1] + 1]
        ilon = [self.jcope2ilon[0], self.jcope2ilon[-1] + 1]

        # ~~~~ Time records ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        print('     +> Extract JCOPE2 time records')
        self.slf3d.tags = {'times': []}
        # ~~~~ Start Date and Time ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        self.slf3d.tags['times'] = 86400.0 * np.arange(nbar)
        self.slf2d.tags = {'times': self.slf3d.tags['times']}
        self.slf3d.datetime = self.experiments[-1][3][0].timetuple()[0:6]
        self.slf2d.datetime = self.slf3d.datetime
        self.slf3d.iparam[9] = 1
        self.slf2d.iparam[9] = 1

        print('     +> Write Selafin headers')
        if not only_2d:
            self.slf3d.fole = {}
            self.slf3d.fole.update({'hook': open('t3d_' + root_name, 'wb')})
            self.slf3d.fole.update({'name': 't3d_' + root_name})
            self.slf3d.fole.update({'endian': ">"})  # big endian
            self.slf3d.fole.update({'float': ('f', 4)})  # single precision
            self.slf3d.append_header_slf()
        self.slf2d.fole = {}
        self.slf2d.fole.update({'hook': open('t2d_' + root_name, 'wb')})
        self.slf2d.fole.update({'name': 't2d_' + root_name})
        self.slf2d.fole.update({'endian': ">"})  # big endian
        self.slf2d.fole.update({'float': ('f', 4)})  # single precision
        self.slf2d.append_header_slf()
        # ~~~~ Time loop(s) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        print('     +> Write Selafin cores')
        ibar = 0
        pbar = ProgressBar(maxval=6 * nbar).start()
        for exp in self.experiments:
            jcope2data = exp[0]
            i_1 = min(exp[2])
            i_2 = max(exp[2]) + 1
            for itime in range(i_1, i_2):
                # ~~> time stamp
                pbar.write('        x ' + str(exp[3][itime - i_1]),
                           6 * ibar + 0)
                pbar.update(6 * ibar + 0)
                if not only_2d:
                    self.slf3d.append_core_time_slf(ibar)
                self.slf2d.append_core_time_slf(ibar)

                # ~~> ELEVATION
                data = jcope2data['el']['el']
                var2d = np.swapaxes(
                    data.data[itime, 0, ilat[0]:ilat[1], ilon[0]:ilon[1]][0],
                    1, 2).ravel()[self.mask2]
                self.slf2d.append_core_vars_slf([var2d])
                if not only_2d:
                    var3d = - np.tile(self.zplan, self.slf3d.npoin2)\
                                .reshape(self.slf3d.npoin2, self.slf3d.nplan)\
                                .T.ravel()
                    var3d[self.slf3d.npoin3 - self.slf3d.npoin2:] = var2d
                    self.slf3d.append_core_vars_slf([var3d])
                pbar.write('             - elevation', 6 * ibar + 1)
                pbar.update(6 * ibar + 1)

                # ~~> SALINITY
                data = jcope2data['salt']['salt']
                if only_2d:
                    var = np.swapaxes(
                        data.data[itime, 0:1, ilat[0]:ilat[1],
                                  ilon[0]:ilon[1]][0], 1, 2)
                else:
                    var = np.swapaxes(
                        data.data[itime, 0:self.slf3d.nplan, ilat[0]:ilat[1],
                                  ilon[0]:ilon[1]][0], 1, 2)
                var2d = var[0].ravel()[self.mask2]
                self.slf2d.append_core_vars_slf([var2d])
                if not only_2d:
                    var3d = var[::-1].ravel()[self.mask3]
                    for ipoin in range(self.slf3d.npoin2):
                        for iplan in range(self.slf3d.nplan - 1, 0, -1):
                            if var3d[ipoin +
                                     (iplan - 1) * self.slf3d.npoin2] < -99.0:
                                var3d[ipoin+(iplan-1)*self.slf3d.npoin2] = \
                                          var3d[ipoin+iplan*self.slf3d.npoin2]
                    self.slf3d.append_core_vars_slf([var3d])
                    pbar.write('             - salinity', 6 * ibar + 2)
                pbar.update(6 * ibar + 2)

                # ~~> TEMPERATURE
                data = jcope2data['temp']['temp']
                if only_2d:
                    var = np.swapaxes(
                        data.data[itime, 0:1, ilat[0]:ilat[1],
                                  ilon[0]:ilon[1]][0], 1, 2)
                else:
                    var = np.swapaxes(
                        data.data[itime, 0:self.slf3d.nplan, ilat[0]:ilat[1],
                                  ilon[0]:ilon[1]][0], 1, 2)
                var2d = var[0].ravel()[self.mask2]
                self.slf2d.append_core_vars_slf([var2d])
                if not only_2d:
                    var3d = var[::-1].ravel()[self.mask3]
                    for ipoin in range(self.slf3d.npoin2):
                        for iplan in range(self.slf3d.nplan - 1, 0, -1):
                            if var3d[ipoin +
                                     (iplan - 1) * self.slf3d.npoin2] < -99.0:
                                var3d[ipoin+(iplan-1)*self.slf3d.npoin2] = \
                                          var3d[ipoin+iplan*self.slf3d.npoin2]
                    self.slf3d.append_core_vars_slf([var3d])
                    pbar.write('             - temperature', 6 * ibar + 3)
                pbar.update(6 * ibar + 3)

                # ~~> VELOCITY U
                data = jcope2data['u']['u']
                if only_2d:
                    var = np.swapaxes(
                        data.data[itime, 0:1, ilat[0]:ilat[1],
                                  ilon[0]:ilon[1]][0], 1, 2)
                else:
                    var = np.swapaxes(
                        data.data[itime, 0:self.slf3d.nplan, ilat[0]:ilat[1],
                                  ilon[0]:ilon[1]][0], 1, 2)
                var2d = var[0].ravel()[self.mask2]
                self.slf2d.append_core_vars_slf([var2d])
                if not only_2d:
                    var3d = var[::-1].ravel()[self.mask3]
                    for ipoin in range(self.slf3d.npoin2):
                        for iplan in range(self.slf3d.nplan - 1, 0, -1):
                            if var3d[ipoin +
                                     (iplan - 1) * self.slf3d.npoin2] < -99.0:
                                var3d[ipoin+(iplan-1)*self.slf3d.npoin2] = \
                                          var3d[ipoin+iplan*self.slf3d.npoin2]
                    self.slf3d.append_core_vars_slf([var3d])
                pbar.write('             - u-velocity', 6 * ibar + 4)
                pbar.update(6 * ibar + 4)

                # ~~> VELOCITY V
                data = jcope2data['v']['v']
                if only_2d:
                    var = np.swapaxes(
                        data.data[itime, 0:1, ilat[0]:ilat[1],
                                  ilon[0]:ilon[1]][0], 1, 2)
                else:
                    var = np.swapaxes(
                        data.data[itime, 0:self.slf3d.nplan, ilat[0]:ilat[1],
                                  ilon[0]:ilon[1]][0], 1, 2)
                var2d = var[0].ravel()[self.mask2]
                self.slf2d.append_core_vars_slf([var2d])
                if not only_2d:
                    var3d = var[::-1].ravel()[self.mask3]
                    for ipoin in range(self.slf3d.npoin2):
                        for iplan in range(self.slf3d.nplan - 1, 0, -1):
                            if var3d[ipoin +
                                     (iplan - 1) * self.slf3d.npoin2] < -99.0:
                                var3d[ipoin+(iplan-1)*self.slf3d.npoin2] = \
                                          var3d[ipoin+iplan*self.slf3d.npoin2]
                    self.slf3d.append_core_vars_slf([var3d])
                pbar.write('             - v-velocity', 6 * ibar + 5)
                pbar.update(6 * ibar + 5)

                # ~~> VELOCITY W
                if not only_2d:
                    var3d = 0. * var3d
                    self.slf3d.append_core_vars_slf([var3d])

                ibar += 1

        pbar.finish()
        if not only_2d:
            self.slf3d.fole['hook'].close()
        self.slf2d.fole['hook'].close()
            # Construct the destination path and file path
            file_name = '{}_{}-{}.hdf'.format(
                detector, entry['GPSstart'],
                str(int(entry['GPSstart']) + int(entry['duration'])))
            file_path = os.path.join(directory, file_name)

            # Either print the file (dry mode), or actually download it
            if dry:
                print('--', entry['url'], '->', file_path)

            else:

                # Print the progress bar and which file is being downloaded
                # Remember, dataset has been decorated using ProgressBar
                dataset.write(extras=['Downloading: {}'.format(entry['url'])])

                # Perform the actual download
                request = requests.get(entry['url'])
                with open(file_path, "wb") as f:
                    f.write(request.content)

        # Clear up the progress bar and show that downloads are finished!
        if isinstance(dataset, ProgressBar):
            dataset.write(clear_line=True)
        if not dry:
            print('\nDownload for {} is complete!\n'.format(detector))

    # Finally print the overall runtime of the script
    print('This took {:.2f} seconds!'.format(time.time() - script_start_time))
    print('')