Commits
Jorge Lopez authored and Ville Suoranta committed 03ba04cb248 Merge
6776 6776 | mysep = np.abs(mytimes[0]-mytime) |
6777 6777 | for m in range(1,len(mytimes)): |
6778 6778 | if (np.abs(mytimes[m] - mytime) < mysep): |
6779 6779 | mysep = np.abs(mytimes[m] - mytime) |
6780 6780 | myindex = m |
6781 6781 | return(myindex) |
6782 6782 | |
6783 6783 | def getWeather(vis='', scan='', antenna='0',verbose=False, mymsmd=None): |
6784 6784 | """ |
6785 6785 | Queries the WEATHER and ANTENNA tables of an .ms by scan number or |
6786 - | list of scan numbers in order to return mean values of: angleToSun, |
6786 + | list of scan numbers in order to return median values of: angleToSun, |
6787 6787 | pressure, temperature, humidity, dew point, wind speed, wind direction, |
6788 6788 | azimuth, elevation, solarangle, solarelev, solarazim. |
6789 6789 | If the sun is below the horizon, the solarangle returned is negated. |
6790 6790 | -- Todd Hunter |
6791 6791 | """ |
6792 6792 | if (verbose): |
6793 6793 | print("Entered getWeather with vis,scan,antenna = %s,%s,%s" % (str(vis), str(scan), str(antenna))) |
6794 6794 | try: |
6795 6795 | if str(antenna).isdigit(): |
6796 6796 | antennaName = mymsmd.antennanames(antenna)[0] |
6871 6871 | listfield = "" |
6872 6872 | for field in listfields: |
6873 6873 | listfield += "%s" % field |
6874 6874 | if (field != listfields[-1]): |
6875 6875 | listfield += "," |
6876 6876 | else: |
6877 6877 | listscan = str(scan) |
6878 6878 | listfield = mymsmd.fieldsforscan(scan) |
6879 6879 | [az,el] = ComputeSolarAzElForObservatory(myTimes[0], mymsmd) |
6880 6880 | [az2,el2] = ComputeSolarAzElForObservatory(myTimes[-1], mymsmd) |
6881 - | azsun = np.mean([az,az2]) |
6882 - | elsun = np.mean([el,el2]) |
6881 + | azsun = np.median([az,az2]) |
6882 + | elsun = np.median([el,el2]) |
6883 6883 | direction = subtable.getcol("DIRECTION") |
6884 6884 | azeltime = subtable.getcol("TIME") |
6885 6885 | subtable.close() |
6886 6886 | telescopeName = mymsmd.observatorynames()[0] |
6887 6887 | if (len(direction) > 0 and telescopeName.find('VLA') < 0 and telescopeName.find('NRO') < 0): |
6888 6888 | azimuth = direction[0][0]*180.0/math.pi # a list of values |
6889 6889 | elevation = direction[1][0]*180.0/math.pi # a list of values |
6890 6890 | npat = np.array(azeltime) |
6891 6891 | matches = np.where(npat>myTimes[0])[0] |
6892 6892 | matches2 = np.where(npat<myTimes[-1])[0] |
6893 6893 | if (len(matches2) > 0 and len(matches) > 0): |
6894 6894 | if verbose: print("matches[0]=%d, matches2[-1]=%d" % (matches[0],matches[-1])) |
6895 6895 | matchingIndices = list(range(matches[0],matches2[-1]+1)) |
6896 6896 | else: |
6897 6897 | matchingIndices = [] |
6898 6898 | if (len(matchingIndices) > 0): # CAS-8440 |
6899 - | conditions['azimuth'] = np.mean(azimuth[matches[0]:matches2[-1]+1]) |
6900 - | conditions['elevation'] = np.mean(elevation[matches[0]:matches2[-1]+1]) |
6899 + | conditions['azimuth'] = np.median(azimuth[matches[0]:matches2[-1]+1]) |
6900 + | conditions['elevation'] = np.median(elevation[matches[0]:matches2[-1]+1]) |
6901 6901 | elif (len(matches) > 0): # CAS-8440 |
6902 - | if verbose: print("using mean of all az/el values after time 0") |
6903 - | conditions['azimuth'] = np.mean(azimuth[matches[0]]) |
6904 - | conditions['elevation'] = np.mean(elevation[matches[0]]) |
6902 + | if verbose: print("using median of all az/el values after time 0") |
6903 + | conditions['azimuth'] = np.median(azimuth[matches[0]]) |
6904 + | conditions['elevation'] = np.median(elevation[matches[0]]) |
6905 6905 | else: # CAS-8440 |
6906 - | if verbose: print("using mean of all az/el values") |
6907 - | conditions['azimuth'] = np.mean(azimuth) |
6908 - | conditions['elevation'] = np.mean(elevation) |
6906 + | if verbose: print("using median of all az/el values") |
6907 + | conditions['azimuth'] = np.median(azimuth) |
6908 + | conditions['elevation'] = np.median(elevation) |
6909 6909 | conditions['solarangle'] = angularSeparation(azsun,elsun,conditions['azimuth'],conditions['elevation']) |
6910 6910 | conditions['solarelev'] = elsun |
6911 6911 | conditions['solarazim'] = azsun |
6912 6912 | if (verbose): |
6913 - | print("Using antenna = %s to retrieve mean azimuth and elevation" % (antennaName)) |
6913 + | print("Using antenna = %s to retrieve median azimuth and elevation" % (antennaName)) |
6914 6914 | print("Separation from sun = %f deg" % (abs(conditions['solarangle']))) |
6915 6915 | if (elsun<0): |
6916 6916 | conditions['solarangle'] = -conditions['solarangle'] |
6917 6917 | if (verbose): |
6918 6918 | print("Sun is below horizon (elev=%.1f deg)" % (elsun)) |
6919 6919 | else: |
6920 6920 | if (verbose): |
6921 6921 | print("Sun is above horizon (elev=%.1f deg)" % (elsun)) |
6922 6922 | if (verbose): |
6923 6923 | print("Average azimuth = %.2f, elevation = %.2f degrees" % (conditions['azimuth'],conditions['elevation'])) |
6926 6926 | if (type(scan) == int or type(scan)==np.int32): |
6927 6927 | # compute Az/El for this scan |
6928 6928 | myfieldId = mymsmd.fieldsforscan(scan) |
6929 6929 | if (type(myfieldId) == list or type(myfieldId) == type(np.ndarray(0))): |
6930 6930 | myfieldId = myfieldId[0] |
6931 6931 | fieldName = mymsmd.namesforfields(myfieldId) |
6932 6932 | if (type(fieldName) == list or type(fieldName) == type(np.ndarray(0))): |
6933 6933 | fieldName = fieldName[0] |
6934 6934 | # print("A) fieldname = ", fieldName) |
6935 6935 | # print("myfieldId = ", myfieldId) |
6936 - | myscantime = np.mean(mymsmd.timesforscan(scan)) |
6936 + | myscantime = np.median(mymsmd.timesforscan(scan)) |
6937 6937 | # print("Calling getRADecForField") |
6938 6938 | mydirection = getRADecForField(vis, myfieldId, verbose) |
6939 6939 | if (verbose): print("mydirection= %s" % (str(mydirection))) |
6940 6940 | if (len(telescopeName) < 1): |
6941 6941 | telescopeName = 'ALMA' |
6942 6942 | myazel = computeAzElFromRADecMJD(mydirection, myscantime/86400., telescopeName) |
6943 6943 | conditions['elevation'] = myazel[1] * 180/math.pi |
6944 6944 | conditions['azimuth'] = myazel[0] * 180/math.pi |
6945 6945 | conditions['solarangle'] = angularSeparation(azsun,elsun,conditions['azimuth'],conditions['elevation']) |
6946 6946 | conditions['solarelev'] = elsun |
6963 6963 | print("Scans to loop over = %s" % (str(scan))) |
6964 6964 | for s in scan: |
6965 6965 | fieldName = mymsmd.fieldsforscan(s) |
6966 6966 | if (type(fieldName) == list): |
6967 6967 | # take only the first pointing in the mosaic |
6968 6968 | fieldName = fieldName[0] |
6969 6969 | myfieldId = mymsmd.fieldsforname(fieldName) |
6970 6970 | if (type(myfieldId) == list or type(myfieldId)==type(np.ndarray(0))): |
6971 6971 | # If the same field name has two IDs (this happens in EVLA data) |
6972 6972 | myfieldId = myfieldId[0] |
6973 - | myscantime = np.mean(mymsmd.timesforscan(s)) |
6973 + | myscantime = np.median(mymsmd.timesforscan(s)) |
6974 6974 | mydirection = getRADecForField(vis, myfieldId, verbose) |
6975 6975 | telescopeName = mymsmd.observatorynames()[0] |
6976 6976 | if (len(telescopeName) < 1): |
6977 6977 | telescopeName = 'ALMA' |
6978 6978 | myazel = computeAzElFromRADecMJD(mydirection, myscantime/86400., telescopeName) |
6979 6979 | myaz.append(myazel[0]*180/math.pi) |
6980 6980 | myel.append(myazel[1]*180/math.pi) |
6981 - | conditions['azimuth'] = np.mean(myaz) |
6982 - | conditions['elevation'] = np.mean(myel) |
6981 + | conditions['azimuth'] = np.median(myaz) |
6982 + | conditions['elevation'] = np.median(myel) |
6983 6983 | conditions['solarangle'] = angularSeparation(azsun,elsun,conditions['azimuth'],conditions['elevation']) |
6984 6984 | conditions['solarelev'] = elsun |
6985 6985 | conditions['solarazim'] = azsun |
6986 6986 | if (verbose): |
6987 - | print("Using antenna = %s to retrieve mean azimuth and elevation" % (antennaName)) |
6987 + | print("Using antenna = %s to retrieve median azimuth and elevation" % (antennaName)) |
6988 6988 | print("Separation from sun = %f deg" % (abs(conditions['solarangle']))) |
6989 6989 | if (elsun<0): |
6990 6990 | conditions['solarangle'] = -conditions['solarangle'] |
6991 6991 | if (verbose): |
6992 6992 | print("Sun is below horizon (elev=%.1f deg)" % (elsun)) |
6993 6993 | else: |
6994 6994 | if (verbose): |
6995 6995 | print("Sun is above horizon (elev=%.1f deg)" % (elsun)) |
6996 6996 | if (verbose): |
6997 6997 | print("Average azimuth = %.2f, elevation = %.2f degrees" % (conditions['azimuth'],conditions['elevation'])) |
7008 7008 | print("Could not open the WEATHER table for this ms.") |
7009 7009 | mytb.done() |
7010 7010 | return([conditions,myTimes]) |
7011 7011 | if (True): |
7012 7012 | mjdsec = mytb.getcol('TIME') |
7013 7013 | indices = np.argsort(mjdsec) |
7014 7014 | mjd = mjdsec/86400. |
7015 7015 | pressure = mytb.getcol('PRESSURE') |
7016 7016 | relativeHumidity = mytb.getcol('REL_HUMIDITY') |
7017 7017 | temperature = mytb.getcol('TEMPERATURE') |
7018 - | if (np.mean(temperature) > 100): |
7018 + | if (np.median(temperature) > 100): |
7019 7019 | # must be in units of Kelvin, so convert to C |
7020 7020 | temperature -= 273.15 |
7021 7021 | if 'DEW_POINT' in mytb.colnames(): |
7022 7022 | dewPoint = mytb.getcol('DEW_POINT') |
7023 - | if (np.mean(dewPoint) > 100): |
7023 + | if (np.median(dewPoint) > 100): |
7024 7024 | # must be in units of Kelvin, so convert to C |
7025 7025 | dewPoint -= 273.15 |
7026 - | if (np.mean(dewPoint) == 0): |
7026 + | if (np.median(dewPoint) == 0): |
7027 7027 | # assume it is not measured and use NOAA formula to compute from humidity: |
7028 7028 | dewPoint = ComputeDewPointCFromRHAndTempC(relativeHumidity, temperature) |
7029 7029 | else: |
7030 7030 | dewPoint = None # Nobeyama measurement sets do not have a dewpoint column |
7031 7031 | sinWindDirection = np.sin(mytb.getcol('WIND_DIRECTION')) |
7032 7032 | cosWindDirection = np.cos(mytb.getcol('WIND_DIRECTION')) |
7033 7033 | windSpeed = mytb.getcol('WIND_SPEED') |
7034 7034 | mytb.done() |
7035 7035 | |
7036 7036 | # put values into time order (they mostly are, but there can be small differences) |
7080 7080 | conditions['readings'] = 1 |
7081 7081 | if (verbose): |
7082 7082 | print("selectedValues=%d, myTimes[0]=%.0f, len(matches)=%d, len(matches2)=%d" % (selectedValues, |
7083 7083 | myTimes[0], len(matches), len(matches2))) |
7084 7084 | if (len(matches) > 0): |
7085 7085 | print("matches[0]=%f, matches[-1]=%f" % (matches[0], matches[-1])) |
7086 7086 | if (len(matches2) > 0): |
7087 7087 | print("matches2[0]=%f, matches2[-1]=%d" % (matches2[0], matches2[-1])) |
7088 7088 | else: |
7089 7089 | conditions['readings'] = len(selectedValues) |
7090 - | conditions['pressure'] = np.mean(pressure[selectedValues]) |
7090 + | conditions['pressure'] = np.median(pressure[selectedValues]) |
7091 7091 | if (conditions['pressure'] != conditions['pressure']): |
7092 7092 | # A nan value got through, due to no selected values (should be impossible)" |
7093 7093 | if (verbose): |
7094 7094 | print(">>>>>>>>>>>>>>>>>>>>>>>> selectedValues = %s" % (str(selectedValues))) |
7095 7095 | print("len(matches)=%d, len(matches2)=%d" % (len(matches), len(matches2))) |
7096 7096 | print("matches[0]=%f, matches[-1]=%f, matches2[0]=%f, matches2[-1]=%d" % (matches[0], matches[-1], matches2[0], matches2[-1])) |
7097 - | conditions['temperature'] = np.mean(temperature[selectedValues]) |
7098 - | conditions['humidity'] = np.mean(relativeHumidity[selectedValues]) |
7097 + | conditions['temperature'] = np.median(temperature[selectedValues]) |
7098 + | conditions['humidity'] = np.median(relativeHumidity[selectedValues]) |
7099 7099 | if dewPoint is not None: |
7100 - | conditions['dewpoint'] = np.nanmean(dewPoint[selectedValues]) |
7101 - | conditions['windspeed'] = np.mean(windSpeed[selectedValues]) |
7102 - | conditions['winddirection'] = (180./math.pi)*np.arctan2(np.mean(sinWindDirection[selectedValues]),np.mean(cosWindDirection[selectedValues])) |
7100 + | conditions['dewpoint'] = np.nanmedian(dewPoint[selectedValues]) |
7101 + | conditions['windspeed'] = np.median(windSpeed[selectedValues]) |
7102 + | conditions['winddirection'] = (180./math.pi)*np.arctan2(np.median(sinWindDirection[selectedValues]),np.median(cosWindDirection[selectedValues])) |
7103 7103 | if (conditions['winddirection'] < 0): |
7104 7104 | conditions['winddirection'] += 360 |
7105 7105 | if (verbose): |
7106 - | print("Mean weather values for scan %s (field %s)" % (listscan,listfield)) |
7106 + | print("Median weather values for scan %s (field %s)" % (listscan,listfield)) |
7107 7107 | print(" Pressure = %.2f mb" % (conditions['pressure'])) |
7108 7108 | print(" Temperature = %.2f C" % (conditions['temperature'])) |
7109 7109 | if dewPoint is not None: |
7110 7110 | print(" Dew point = %.2f C" % (conditions['dewpoint'])) |
7111 7111 | print(" Relative Humidity = %.2f %%" % (conditions['humidity'])) |
7112 7112 | print(" Wind speed = %.2f m/s" % (conditions['windspeed'])) |
7113 7113 | print(" Wind direction = %.2f deg" % (conditions['winddirection'])) |
7114 7114 | |
7115 7115 | return([conditions,myTimes]) |
7116 7116 | # end of getWeather |