Esempio n. 1
0
 def test_jdaytodate_leapyearfirstday_yieldsjan1(self):
     month, day = time_subs.julian_to_date(2020, 1)
     self.assertTrue(month == 1)
     self.assertTrue(day == 1)
Esempio n. 2
0
    def test_jdaytodate_leapyearandcommonyearinjan_yieldssamedate(self):
        month_leap, day_leap = time_subs.julian_to_date(2016, 20)
        month, day = time_subs.julian_to_date(2015, 20)

        self.assertTrue(month == month_leap)
        self.assertTrue(day == day_leap)
Esempio n. 3
0
 def test_jdaytodate_commonyearlastday_yieldsdec31(self):
     month, day = time_subs.julian_to_date(2011, 365)
     self.assertTrue(month == 12)
     self.assertTrue(day == 31)
Esempio n. 4
0
 def test_jdaytodate_commonyearjday60_yieldsmarch1(self):
     month, day = time_subs.julian_to_date(2015, 60)
     self.assertTrue(month == 3)
     self.assertTrue(day == 1)
Esempio n. 5
0
 def test_jdaytodate_leapyearleapday_yieldsfeb29(self):
     month, day = time_subs.julian_to_date(2016, 60)
     self.assertTrue(month == 2)
     self.assertTrue(day == 29)
Esempio n. 6
0
 def test_jdaytodate_leapyearjday365_yieldsdec30(self):
     month, day = time_subs.julian_to_date(2016, 365)
     self.assertTrue(month == 12)
     self.assertTrue(day == 30)
Esempio n. 7
0
    def read_rza(self, rza_file, convert_to_knots=True):
        """
        read ATMS RZA file
        
        input:
            required input: filename
            optional input: 
                 convert_to_knot = True by default

        return: creates class variables 
        """

        #  initialize array variables

        self.rza_p = np.empty(0, dtype=np.float)
        self.rza_d = np.empty(0, dtype=np.float)
        self.rza_t = np.empty(0, dtype=np.float)
        self.rza_gw = np.empty(0, dtype=np.float)

        #  open data file
        try:
            cur_file = open(rza_file, 'r')
        except IOError:
            error_msg = 'Cannot open file ' + rza_file
            LOGGER.exception(error_msg)
            sys.exit(error_codes.EX_IOERR)

        #  read 1st header line
        # 01 20121027 201230118   29.8  -75.6   28.1  -76.9 034 010 075 065 065 AL1812  SANDY
        try:
            first_line = cur_file.readline()
            anum_str,atime1_str,atime2_str,alat_str,alon_str,alatm12_str,alonm12_str,adir_str,\
                aspeed_str,armw_str,avmx_str,avmxm12_str,astorm_str,asname_str\
                 = first_line.split()
            self.ayear = int(atime1_str[0:4])
            self.amonth = int(atime1_str[4:6])
            self.aday = int(atime1_str[6:8])
            self.ajday = int(atime2_str[4:7])
            self.ahour = int(atime2_str[7:9])
            self.abasin = astorm_str[0:2]
            self.asnum = int(astorm_str[2:4])
            self.asyear = int(astorm_str[4:6])
            self.asname = asname_str

            anum,self.alat,self.alon,self.alatm12,self.alonm12,\
                    self.adir,self.aspeed,self.armw,self.avmx,self.avmxm12\
                = int(anum_str),float(alat_str),float(alon_str),float(alatm12_str),float(alonm12_str),\
                    int(adir_str),int(aspeed_str),int(armw_str),int(avmx_str),int(avmxm12_str)
        except:
            error_msg1 = 'Error encountered while reading first line of XYA file ' + rza_file + '\n'
            error_msg2 = 'cannot read line: ' + first_line
            LOGGER.exception(error_msg1 + error_msg2)
            sys.exit(error_codes.EX_DATAERR)

        #  read 2nd header line
        # 01 2012301 180856    29.70   -76.38    29.82   -75.58
        try:
            second_line = cur_file.readline()
            snum_str, stime1_str,stime2_str,slat_str,slon_str,clat_str,clon_str\
                 = second_line.split()
            self.syear = int(stime1_str[0:4])
            self.sjday = int(stime1_str[4:7])
            self.shour = int(stime2_str[0:2])
            self.smin = int(stime2_str[2:4])
            self.ssec = int(stime2_str[4:6])

            snum, self.slat,self.slon,self.clat,self.clon\
             = int(snum_str), float(slat_str),float(slon_str),float(clat_str),float(clon_str)\

        except:
            error_msg1 = 'Error encountered while reading 2nd line of XYA file ' + rza_file + '\n'
            error_msg2 = 'cannot read line: ' + second_line
            LOGGER.exception(error_msg1 + error_msg2)
            sys.exit(error_codes.EX_DATAERR)

        #  calculate swath time
        try:
            #  calculate month and date from julian day
            self.smonth, self.sday = ts.julian_to_date(self.syear, self.sjday)
            self.swath_time = ts.dt_from_60s_timestamp(self.syear,self.smonth,self.sday,\
                self.shour,self.smin,self.ssec)
        except:
            error_msg1 = 'Error encountered while converting swath time from ' + rza_file + '\n'
            error_msg2 = 'cannot convert to swath time: %d %d %d %d %d' % (self.syear, self.sjday,\
                    self.shour,self.smin,self.ssec)
            LOGGER.exception(error_msg1 + error_msg2)
            sys.exit(error_codes.EX_DATAERR)

        #  read 3nd header line
        # AL1812 102718 10271808  r(km): 31    0.0  600.0 z(km): 21    0.0   20.0 lat= 29.82 lon= -75.59
        try:
            third_line = cur_file.readline()
            fields = third_line.split()

            self.nRange = int(fields[4])
            self.minRange = float(fields[5])
            self.maxRange = float(fields[6])
            self.nHeight = int(fields[8])
            self.minHeight = float(fields[9])
            self.maxHeight = float(fields[10])

            self.resRange = (self.maxRange -
                             self.minRange) / (float(self.nRange) - 1.0)
            self.ranges = np.linspace(self.minRange, self.maxRange,
                                      self.nRange)
            self.resHeight = (self.maxHeight -
                              self.minHeight) / (float(self.nHeight) - 1.0)
            self.heights = np.linspace(self.minHeight, self.maxHeight,
                                       self.nHeight)

        except:
            error_msg1 = 'Error encountered while reading 3rd line of XYA file ' + rza_file + '\n'
            error_msg2 = 'cannot read line: ' + third_line
            LOGGER.exception(error_msg1 + error_msg2)
            sys.exit(error_codes.EX_DATAERR)

        #  read the rest of the file
        try:
            for read_line in iter(cur_file):

                #  check that line is not empty (I only saw empty lines at the end of file)
                if len(read_line) > 0:
                    fields = read_line.split()
                else:
                    continue

                #  parse non-empty line
                if fields[0] in dataNames:
                    varName = dataNames[fields[0]]
                elif re.match('[-+]?\d+', fields[0]):
                    setattr(
                        self, varName,
                        np.append(getattr(self, varName),
                                  [float(i) for i in fields]))
                else:
                    error_msg = 'Unexpected data in the file %s Will exit now' % rza_file
                    raise ValueError(error_msg)

        except:
            error_msg = 'Error encountered while reading main data from XYA file ' + rza_file
            LOGGER.exception(error_msg)
            sys.exit(error_codes.EX_DATAERR)

        cur_file.close()

        if convert_to_knots:
            #  convert to knots U,V at pressure levels and at the surface
            self.rza_gw = self.rza_gw * MS2KNOTS

        try:
            #  reshape data

            #  2D variables
            for varName in varNames:
                setattr(
                    self, varName,
                    np.reshape(getattr(self, varName),
                               (self.nRange, self.nHeight),
                               order='F'))

        except:
            error_msg = 'Error encountered while reshaping ' + rza_file
            LOGGER.exception(error_msg)
            sys.exit(error_codes.EX_DATAERR)
Esempio n. 8
0
    def read_xya(self, xya_file, convert_to_knots=True):
        """
        read ATMS XYA file
        
        input:
            required input: filename
            optional input: 
                 convert_to_knot = True by default

        return: creates class variables 
        """

        # --- initialize array variables
        self.xya_p = np.empty(0, dtype=np.float)
        self.xya_lat = np.empty(0, dtype=np.float)
        self.xya_lon = np.empty(0, dtype=np.float)
        self.xya_u = np.empty(0, dtype=np.float)
        self.xya_v = np.empty(0, dtype=np.float)
        self.xya_t = np.empty(0, dtype=np.float)
        self.xya_z = np.empty(0, dtype=np.float)
        self.xya_slat = np.empty(0, dtype=np.float)
        self.xya_slon = np.empty(0, dtype=np.float)
        self.xya_su = np.empty(0, dtype=np.float)
        self.xya_sv = np.empty(0, dtype=np.float)
        self.xya_st = np.empty(0, dtype=np.float)
        self.xya_slp = np.empty(0, dtype=np.float)
        self.xya_sclw = np.empty(0, dtype=np.float)

        # --- open data file
        try:
            cur_file = open(xya_file, 'r')
        except IOError:
            error_msg = 'Cannot open file ' + xya_file
            LOGGER.exception(error_msg)
            sys.exit(error_codes.EX_IOERR)

        # --- read 1st header line
        try:
            first_line = cur_file.readline()
            anum_str,atime1_str,atime2_str,alat_str,alon_str,alatm12_str,alonm12_str,adir_str,\
                aspeed_str,armw_str,avmx_str,avmxm12_str,astorm_str,asname_str\
                 = first_line.split()
            self.ayear = int(atime1_str[0:4])
            self.amonth = int(atime1_str[4:6])
            self.aday = int(atime1_str[6:8])
            self.ajday = int(atime2_str[4:7])
            self.ahour = int(atime2_str[7:9])
            self.abasin = astorm_str[0:2]
            self.asnum = int(astorm_str[2:4])
            self.asyear = int(astorm_str[4:6])
            self.asname = asname_str

            anum,self.alat,self.alon,self.alatm12,self.alonm12,\
                    self.adir,self.aspeed,self.armw,self.avmx,self.avmxm12\
                = int(anum_str),float(alat_str),float(alon_str),float(alatm12_str),float(alonm12_str),\
                    int(adir_str),int(aspeed_str),int(armw_str),int(avmx_str),int(avmxm12_str)
        except:
            error_msg1 = 'Error encountered while reading first line of XYA file ' + xya_file + '\n'
            error_msg2 = 'cannot read line: ' + first_line
            LOGGER.exception(error_msg1 + error_msg2)
            sys.exit(error_codes.EX_DATAERR)

        # --- read 2nd header line
        try:
            second_line = cur_file.readline()
            snum_str, stime1_str,stime2_str,slat_str,slon_str,clat_str,clon_str\
                 = second_line.split()
            self.syear = int(stime1_str[0:4])
            self.sjday = int(stime1_str[4:7])
            self.shour = int(stime2_str[0:2])
            self.smin = int(stime2_str[2:4])
            self.ssec = int(stime2_str[4:6])

            snum, self.slat,self.slon,self.clat,self.clon\
             = int(snum_str), float(slat_str),float(slon_str),float(clat_str),float(clon_str)\

        except:
            error_msg1 = 'Error encountered while reading 2nd line of XYA file ' + xya_file + '\n'
            error_msg2 = 'cannot read line: ' + second_line
            LOGGER.exception(error_msg1 + error_msg2)
            sys.exit(error_codes.EX_DATAERR)

        # --- calculate swath time
        try:
            # --- calculate month and date from julian day
            self.smonth, self.sday = ts.julian_to_date(self.syear, self.sjday)
            self.swath_time = ts.dt_from_60s_timestamp(self.syear,self.smonth,self.sday,\
                self.shour,self.smin,self.ssec)
        except:
            error_msg1 = 'Error encountered while converting swath time from ' + xya_file + '\n'
            error_msg2 = 'cannot convert to swath time: %d %d %d %d %d' % (self.syear, self.sjday,\
                    self.shour,self.smin,self.ssec)
            LOGGER.exception(error_msg1 + error_msg2)
            sys.exit(error_codes.EX_DATAERR)

        # --- read the rest of the file
        try:
            prs_level_data = False
            sfc_level_data = False
            for read_line in iter(cur_file):

                # --- check that line is not empty (I only saw empty lines at the end of file)
                if len(read_line) > 0:
                    split_line = read_line.split()
                else:
                    continue

                # --- parse non-empty line
                if split_line[0] == 'Pressure':
                    # --- header line for pressure level data
                    prs_level_data = True
                    # --- read pressure level
                    current_plev = split_line[-3]
                    # --- read 'Pressure level (Pa)=100000.0', where there is no space
                    # --- after '='
                    if re.match('.*=.*', current_plev):
                        current_plev = current_plev.split('=')[-1]
                    self.xya_p = np.append(self.xya_p, float(current_plev))
                    # --- read number of lat,lon points
                    nlat = int(split_line[-2].split('=')[-1])
                    nlon = int(split_line[-2].split('=')[-1])
                elif split_line[0] == 'Lat':
                    # --- header line with column names
                    pass
                elif split_line[0] == 'Surface':
                    # --- header line for surface data
                    prs_level_data = False
                    sfc_level_data = True
                elif re.match(
                        '[-+]?\d+', split_line[0]
                ) and prs_level_data is True and sfc_level_data is False:
                    # --- read data for pressure level
                    self.xya_lat = np.append(self.xya_lat,
                                             float(split_line[0]))
                    self.xya_lon = np.append(self.xya_lon,
                                             float(split_line[1]))
                    self.xya_u = np.append(self.xya_u, float(split_line[2]))
                    self.xya_v = np.append(self.xya_v, float(split_line[3]))
                    self.xya_t = np.append(self.xya_t, float(split_line[4]))
                    self.xya_z = np.append(self.xya_z, float(split_line[5]))
                elif re.match(
                        '[-+]?\d+', split_line[0]
                ) and prs_level_data is False and sfc_level_data is True:
                    # --- read data for surface
                    self.xya_slat = np.append(self.xya_slat,
                                              float(split_line[0]))
                    self.xya_slon = np.append(self.xya_slon,
                                              float(split_line[1]))
                    self.xya_su = np.append(self.xya_su, float(split_line[2]))
                    self.xya_sv = np.append(self.xya_sv, float(split_line[3]))
                    self.xya_st = np.append(self.xya_st, float(split_line[4]))
                    self.xya_slp = np.append(self.xya_slp,
                                             float(split_line[5]))
                    self.xya_sclw = np.append(self.xya_sclw,
                                              float(split_line[6]))
                else:
                    #print split_line
                    error_msg = 'Unexpected data in the file %s Will exit now' % xya_file
                    raise ValueError(error_msg)

        except:
            error_msg = 'Error encountered while reading main data from XYA file ' + xya_file
            LOGGER.exception(error_msg)
            sys.exit(error_codes.EX_DATAERR)

        cur_file.close()

        if convert_to_knots:
            # --- convert to knots U,V at pressure levels and at the surface
            self.xya_u = self.xya_u * MS2KNOTS
            self.xya_v = self.xya_v * MS2KNOTS
            self.xya_su = self.xya_su * MS2KNOTS
            self.ya_sv = self.xya_sv * MS2KNOTS

        try:
            # --- reshape data
            npres = len(self.xya_p)  #number of pressure levels
            # --- 3d variables
            # --- MUST use order='F' to get correct fields!!!!!
            self.xya_lat = np.reshape(self.xya_lat, (nlon, nlat, npres),
                                      order='F')
            self.xya_lon = np.reshape(self.xya_lon, (nlon, nlat, npres),
                                      order='F')
            self.xya_u = np.reshape(self.xya_u, (nlon, nlat, npres), order='F')
            self.xya_v = np.reshape(self.xya_v, (nlon, nlat, npres), order='F')
            self.xya_t = np.reshape(self.xya_t, (nlon, nlat, npres), order='F')
            self.xya_z = np.reshape(self.xya_z, (nlon, nlat, npres), order='F')

            # --- surface variables
            self.xya_slat = np.reshape(self.xya_slat, (nlon, nlat), order='F')
            self.xya_slon = np.reshape(self.xya_slon, (nlon, nlat), order='F')
            self.xya_su = np.reshape(self.xya_su, (nlon, nlat), order='F')
            self.xya_sv = np.reshape(self.xya_sv, (nlon, nlat), order='F')
            self.xya_st = np.reshape(self.xya_st, (nlon, nlat), order='F')
            self.xya_slp = np.reshape(self.xya_slp, (nlon, nlat), order='F')
            self.xya_sclw = np.reshape(self.xya_sclw, (nlon, nlat), order='F')
        except:
            error_msg = 'Error encountered while reshaping ' + xya_file
            LOGGER.exception(error_msg)
            sys.exit(error_codes.EX_DATAERR)