1

I am trying to export RH on 9 and 15 o'clock to a CSV, but my code give me blank on column RH9 and RH15

var shapefile = ee.FeatureCollection("projects/ee-dimasfqr/assets/karawang_kec_shp"),
geometry = 
/* color: #98ff00 */
/* shown: false */
/* displayProperties: [
  {
    "type": "rectangle"
  }
] */
ee.Geometry.Polygon(
    [[[107.04729994333013, -5.903692785106611],
      [107.04729994333013, -7.120740977827176],
      [108.43707045114263, -7.120740977827176],
      [108.43707045114263, -5.903692785106611]]], null, false);
var listkec =['Majalaya','Tempuran','Ciampel','Cikampek','Tirta Jaya','Tegalwaru','Rawamerta','Rengas Dengklok','Kotabaru','Telukjambe Barat','Talagasari','Pangkalan','Telukjambe Timur','Banyu Sari','Pedes','Cilebar','Tirtamulya','Klari','Jayakerta','Karawang Timur','Cilamaya Kulon','Cilamaya Wetan','Kutawaluya','Pakisjaya','Batu Jaya','Lemahabang','Cibuaya','Karawang Barat','Jatisari','Purwasari']
print(listkec)
var ic9 = ee.ImageCollection("ECMWF/ERA5_LAND/HOURLY").filterDate('2018-1-1','2023-1-31')
          .filter(ee.Filter.calendarRange(2, 2, 'hour'))
var ic15 = ee.ImageCollection("ECMWF/ERA5_LAND/HOURLY").filterDate('2018-1-1','2023-1-31')
          .filter(ee.Filter.calendarRange(8, 8, 'hour'))          
var dates = ee.List.sequence(0, 1825).map(function(day){
    return ee.Date.fromYMD(2018,01,01,'Asia/Jakarta').advance(day, "day");
  });
print(dates)
var kecamatan = shapefile.filter(ee.Filter.eq('WADMKC','Majalaya'));
var res = dates.map(function(date){
        var image = ic9.filterBounds(kecamatan).filterDate(date, ee.Date(date).advance(1, "day"))
        var image1 = image.select('temperature_2m');
        var b = ee.Image(273.15);
        var t9 = image1.min();
        var t91 = t9.clip(kecamatan);
        var temp9 = t91.subtract(b).reduceRegion({
             reducer: ee.Reducer.mean(),
             geometry: kecamatan.geometry(),
             scale: 10
            }).get("temperature_2m");
        var image2 = image.select('dewpoint_temperature_2m');
        var td9 = image2.min();
        var td91 = td9.clip(kecamatan);
        var tempd9 = td91.subtract(b).reduceRegion({
             reducer: ee.Reducer.mean(),
             geometry: kecamatan.geometry(),
             scale: 10
            }).get("dewpoint_temperature_2m");
        var image0 = ic15.filterBounds(kecamatan).filterDate(date, ee.Date(date).advance(1, "day"))
        var image01 = image0.select('temperature_2m');
        var b = ee.Image(273.15);
        var t15 = image01.min();
        var t151 = t15.clip(kecamatan);
        var temp15 = t151.subtract(b).reduceRegion({
             reducer: ee.Reducer.mean(),
             geometry: kecamatan.geometry(),
             scale: 10
            }).get("temperature_2m");
        var image02 = image0.select('dewpoint_temperature_2m');
        var td15 = image02.min();
        var td151 = td15.clip(kecamatan);
        var tempd15 = td151.subtract(b).reduceRegion({
             reducer: ee.Reducer.mean(),
             geometry: kecamatan.geometry(),
             scale: 10
            }).get("dewpoint_temperature_2m");
        // Calculate the vapor pressure (E) https://iridl.ldeo.columbia.edu/dochelp/QA/Basic/dewpoint.html
        var E0 = 0.611; // kPa
        var L_Rv = 5423; // K
        var T0 = 273; // K
        var K = 273.15
        var E9 = E0 * ee.Number(Math.E).pow((L_Rv) * ((1/T0) - (1/(tempd9+K)))); 
        var Es9 = E0 * ee.Number(Math.E).pow((L_Rv) * ((1/T0) - (1/(temp9+K))));
        var E15 = E0 * ee.Number(Math.E).pow((L_Rv) * ((1/T0) - (1/(tempd15+K))));
        var Es15 = E0 * ee.Number(Math.E).pow((L_Rv) * ((1/T0) - (1/(temp15+K))));
        var RH9 = 100* (E9/Es9);
        var RH15 = 100* (E15/Es15);
        return ee.Feature(null, {
          'date': ee.Date(date).advance(1, 'day').format('YYYY-MM-dd'),
          'Temp9': temp9,
          'TempD9': tempd9,
          'Temp15': temp15,
          'TempD15': tempd15,
          'E9': E9,
          'Es9':Es9,
          'RH9': RH9,
          'RH15': RH15,
          })});
Export.table.toDrive({
collection: ee.FeatureCollection(res),
description: 'jam9_Majalaya',
fileNamePrefix : "Percobaan_jam9_majalaya",
fileFormat: 'CSV',
selectors: ['date','Temp9','TempD9','Temp15','TempD15','E9','Es9','RH9','RH15']});

1 Answer 1

1

When calculating E9, Es9, E15, Es15, RH9, RH15, you are using client-side operations (/, *, -) on server-side objects. Read up on that here.

I had problem running your script without exporting. The script ran out of memory when printing out the first features. There are a number of best practices that are often good to adopt.

A couple of specific performance points for you script:

  • Avoid clipping when you can
  • Don't work with large lists, use collections when you can
  • Reduce the number of times you invoke reduceRegion(). Combine reducers and/or apply it on multiple bands at the same time
  • Don't use a scale smaller than your native imagery in reduceRegion()

You could perhaps implement it something like the below. Note that I don't know the formulas in your script, so some error might have sneaked in.

var aoi = ee.FeatureCollection("projects/ee-dimasfqr/assets/karawang_kec_shp")
  .filter(ee.Filter.eq('WADMKC', 'Majalaya'))
  .first()
  .geometry()
var startDate = '2018-01-01'
var endDate = '2023-02-01'
  
var collection = ee.ImageCollection("ECMWF/ERA5_LAND/HOURLY")
  .select(['dewpoint_temperature_2m', 'temperature_2m'])
  .filterDate(startDate, endDate)  
  .filterBounds(aoi)
var rhCollection = ee.ImageCollection(ee.Join.saveFirst('hour_8')
  .apply({
    primary: collection.filter(ee.Filter.eq('hour', 2)), 
    secondary: collection.filter(ee.Filter.eq('hour', 8)), 
    condition: ee.Filter.maxDifference({
      difference: 7 * 60 * 60 * 1000, 
      leftField: 'system:time_start', 
      rightField: 'system:time_start'
    })
  }))
  .map(function (hour2) {
    var hour8 = ee.Image(hour2.get('hour_8'))
    var reduced = hour2.rename(['tempd9', 'temp9'])
      .addBands(hour8.rename(['tempd15', 'temp15']))
      .reduceRegion({
        reducer: ee.Reducer.mean(), 
        geometry: aoi, 
        scale: 11132,
        maxPixels: 1e13
      })
    var temp9 = reduced.getNumber('temp9')
    var tempd9 = reduced.getNumber('tempd9')
    var temp15 = reduced.getNumber('temp15')
    var tempd15 = reduced.getNumber('tempd15')
    var E9 = calculateE(tempd9)
    var Es9 = calculateE(temp9)
    var E15 = calculateE(tempd15)
    var Es15 = calculateE(temp15)
    var RH9 = E9.divide(Es9).multiply(100)
    var RH15 = E15.divide(Es15).multiply(100)
    return ee.Feature(null, {
      date: hour2.date().format('YYYY-MM-dd'),
      Temp9: temp9,
      TempD9: tempd9,
      Temp15: temp15,
      TempD15: tempd15,
      E9: E9,
      Es9: Es9,
      RH9: RH9,
      RH15: RH15,
    })
  })
  

function calculateE(temp) {
  return ee.Number.expression(
    'E0 * exp((L_Rv) * ((1 / T0) - (1 / (temp + K))))', {
      E0: 0.611, // kPa
      L_Rv: 5423, // K
      T0: 273, // K
      K: 273.15,
      temp: temp
    }
  )
}  

https://code.earthengine.google.com/14636f4550975d145de52bb43198512b

1
  • thank you, it works, i just correct the formula because the temp doesnt need to be added by K anymore, because it is in K already, thank you Aug 21, 2023 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.