<commit_before>import {queue} from 'd3-queue';
import {json} from 'd3-request';

// base url for images
var HIMAWARI_BASE_URL = "http://himawari8-dl.nict.go.jp/himawari8/img/";
var DSCOVR_BASE_URL = "http://epic.gsfc.nasa.gov/epic-archive/png/";

// links to online image explorers
var HIMAWARI_EXPLORER = "http://himawari8.nict.go.jp/";
var DSCOVR_EXPLORER = "http://epic.gsfc.nasa.gov/";

// image types
var INFRARED = "INFRARED_FULL";
var VISIBLE_LIGHT = "D531106";
var DSCOVR_EPIC = "EPIC";

var WIDTH = 550;
var BLOCK_SIZES = [1, 4, 8, 16, 20];

var DSCOVR_WIDTH = 2048;

var IMAGE_QUALITY = 0.9;
var RELOAD_INTERVAL = 1 * 60 * 1000;  // 1 minutes
var RELOAD_TIME_INTERVAL = 10 * 1000;  // 10 seconds

// local storage keys
var IMAGE_DATA_KEY = "imageData";
var CACHED_DATE_KEY = "cachedDate";
var CACHED_IMAGE_TYPE_KEY = "cachedImageType";

var isChromeExtension = window.chrome && chrome.app.getDetails();

/**
 * Returns an array of objects containing URLs and metadata
 * for Himawari 8 image tiles based on a given date.
 * Options:
 * - date: Date object
 * - type: boolean (default: visible light)
 * - zoom: number (default: 1)
 * - blocks: alternative to zoom, how many images per row/column (default: 1)
 *      Has to be a valid block number (1, 4, 8, 16, 20)
 *
 * @param  {Object}       options
 */
function himawariURLs(options) {
  options = options || {};
  var baseURL = HIMAWARI_BASE_URL + (options.type || VISIBLE_LIGHT);
  var date = options.date;

  // Define some image parameters
  var blocks = options.blocks || (options.zoom ? BLOCK_SIZES[options.zoom - 1] : BLOCK_SIZES[0]);
  var level = blocks + 'd';
  var time = [date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()].map(function (s) { return pad(s, 2) }).join("");
  var year = date.getUTCFullYear();
  var month = pad(date.getUTCMonth() + 1, 2);
  var day = pad(date.getUTCDate(), 2);

  // compose URL
  var tilesURL = [baseURL, level, WIDTH, year, month, day, time].join("/");
  var tiles = [];

  for (var y = 0; y < blocks; y++) {
    for (var x = 0; x < blocks; x++) {
      var name = x + "_" + y + ".png";
      var url = tilesURL + "_" + name;

      tiles.push({
        name: name,
        url: getCachedUrl(url),
        x: x,
        y: y
      });
    }
  }

  return {
    tiles: tiles,
    blocks: blocks,
    date: date
  };
}

/**
 * Parses a date string into a date object.
 * @param   {string | Date} date  Date as string or date object
 * @returns {Date}                Date object
 */
function resolveDate(date) {
  if (typeof date === "string") {
    // Don't use Date.parse because it doesn't work with YYYY-MM-DD HH:MM:SSZ
    var parts = date.match(/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})Z/);
    parts[2] -= 1; //months are zero-based
    return new Date(Date.UTC.apply(this, parts.slice(1)));
  } else {
    return date;
  }
}

/**
 * Create a cached URL thanks to our friends at Google.
 * See https://gist.github.com/carlo/5379498
 */
function getCachedUrl(url) {
  // 5 days caching, in seconds
  var cache = 5 * 24 * 60 * 60;
  return "https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?url=" + url + "&container=focus&refresh=" + cache
}

/**
 * Pads a number with trailing zeros and makes it a string.
 */
function pad(num, size) {
  var s = num + "";
  while (s.length < size) {
    s = "0" + s;
  }
  return s;
}

/**
 * Get the date of the latest himawari image by making an Ajax request.
 * @param   {boolean}  infrared  gets the latest infrared image
 * @returns {function}           callback function
 */
function getLatestHimawariDate(infrared, cb) {
  json("https://himawari-8.appspot.com/latest" + (infrared ? "?infrared=true" : ""),
    function (error, data) {
      if (error) throw error;
      var latest = data.date;
      cb(resolveDate(latest + "Z"));
    });
}

function getLatestDscovrImageUrlAndDate(cb) {
  json("http://epic.gsfc.nasa.gov/api/images.php",
    function (error, data) {
      if (error) throw error;
      var latest = data[data.length - 1];
      latest.date = resolveDate(latest.date + "Z")
      cb(latest);
    });
}

/**
 * Looks at the screen resolution and figures out a zoom level that returns images at a sufficient resolution.
 */<commit_msg>Don’t try to load image if there is none. <commit_after>import {queue} from 'd3-queue';
import {json} from 'd3-request';

// base url for images
var HIMAWARI_BASE_URL = "http://himawari8-dl.nict.go.jp/himawari8/img/";
var DSCOVR_BASE_URL = "http://epic.gsfc.nasa.gov/epic-archive/png/";

// links to online image explorers
var HIMAWARI_EXPLORER = "http://himawari8.nict.go.jp/";
var DSCOVR_EXPLORER = "http://epic.gsfc.nasa.gov/";

// image types
var INFRARED = "INFRARED_FULL";
var VISIBLE_LIGHT = "D531106";
var DSCOVR_EPIC = "EPIC";

var WIDTH = 550;
var BLOCK_SIZES = [1, 4, 8, 16, 20];

var DSCOVR_WIDTH = 2048;

var IMAGE_QUALITY = 0.9;
var RELOAD_INTERVAL = 1 * 60 * 1000;  // 1 minutes
var RELOAD_TIME_INTERVAL = 10 * 1000;  // 10 seconds

// local storage keys
var IMAGE_DATA_KEY = "imageData";
var CACHED_DATE_KEY = "cachedDate";
var CACHED_IMAGE_TYPE_KEY = "cachedImageType";

var isChromeExtension = window.chrome && chrome.app.getDetails();

/**
 * Returns an array of objects containing URLs and metadata
 * for Himawari 8 image tiles based on a given date.
 * Options:
 * - date: Date object
 * - type: boolean (default: visible light)
 * - zoom: number (default: 1)
 * - blocks: alternative to zoom, how many images per row/column (default: 1)
 *      Has to be a valid block number (1, 4, 8, 16, 20)
 *
 * @param  {Object}       options
 */
function himawariURLs(options) {
  options = options || {};
  var baseURL = HIMAWARI_BASE_URL + (options.type || VISIBLE_LIGHT);
  var date = options.date;

  // Define some image parameters
  var blocks = options.blocks || (options.zoom ? BLOCK_SIZES[options.zoom - 1] : BLOCK_SIZES[0]);
  var level = blocks + 'd';
  var time = [date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds()].map(function (s) { return pad(s, 2) }).join("");
  var year = date.getUTCFullYear();
  var month = pad(date.getUTCMonth() + 1, 2);
  var day = pad(date.getUTCDate(), 2);

  // compose URL
  var tilesURL = [baseURL, level, WIDTH, year, month, day, time].join("/");
  var tiles = [];

  for (var y = 0; y < blocks; y++) {
    for (var x = 0; x < blocks; x++) {
      var name = x + "_" + y + ".png";
      var url = tilesURL + "_" + name;

      tiles.push({
        name: name,
        url: getCachedUrl(url),
        x: x,
        y: y
      });
    }
  }

  return {
    tiles: tiles,
    blocks: blocks,
    date: date
  };
}

/**
 * Parses a date string into a date object.
 * @param   {string | Date} date  Date as string or date object
 * @returns {Date}                Date object
 */
function resolveDate(date) {
  if (typeof date === "string") {
    // Don't use Date.parse because it doesn't work with YYYY-MM-DD HH:MM:SSZ
    var parts = date.match(/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})Z/);
    parts[2] -= 1; //months are zero-based
    return new Date(Date.UTC.apply(this, parts.slice(1)));
  } else {
    return date;
  }
}

/**
 * Create a cached URL thanks to our friends at Google.
 * See https://gist.github.com/carlo/5379498
 */
function getCachedUrl(url) {
  // 5 days caching, in seconds
  var cache = 5 * 24 * 60 * 60;
  return "https://images1-focus-opensocial.googleusercontent.com/gadgets/proxy?url=" + url + "&container=focus&refresh=" + cache
}

/**
 * Pads a number with trailing zeros and makes it a string.
 */
function pad(num, size) {
  var s = num + "";
  while (s.length < size) {
    s = "0" + s;
  }
  return s;
}

/**
 * Get the date of the latest himawari image by making an Ajax request.
 * @param   {boolean}  infrared  gets the latest infrared image
 * @returns {function}           callback function
 */
function getLatestHimawariDate(infrared, cb) {
  json("https://himawari-8.appspot.com/latest" + (infrared ? "?infrared=true" : ""),
    function (error, data) {
      if (error) throw error;
      var latest = data.date;
      cb(resolveDate(latest + "Z"));
    });
}

function getLatestDscovrImageUrlAndDate(cb) {
  json("http://epic.gsfc.nasa.gov/api/images.php",
    function (error, data) {
      if (error) throw error;
      if (data.length === 0) return;
      var latest = data[data.length - 1];
      latest.date = resolveDate(latest.date + "Z")
      cb(latest);
    });
}

/**
 * Looks at the screen resolution and figures out a zoom level that returns images at a sufficient resolution.
 */
{'fraction_non_alphanumeric': 0.15248889927553166, 'fraction_numerical': 0.020916101892965645, 'mean_word_length': 3.031559114460669, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2036526', 'n_tokens_mistral': 3203, 'n_tokens_neox': 3023, 'n_words': 875}